| 4 | |
| 5 | |
| 6 | class BiN(pl.LightningModule): |
| 7 | def __init__(self, d2, d1, t1, t2): |
| 8 | super().__init__() |
| 9 | self.t1 = t1 |
| 10 | self.d1 = d1 |
| 11 | self.t2 = t2 |
| 12 | self.d2 = d2 |
| 13 | |
| 14 | bias1 = torch.Tensor(t1, 1) |
| 15 | self.B1 = nn.Parameter(bias1) |
| 16 | nn.init.constant_(self.B1, 0) |
| 17 | |
| 18 | l1 = torch.Tensor(t1, 1) |
| 19 | self.l1 = nn.Parameter(l1) |
| 20 | nn.init.xavier_normal_(self.l1) |
| 21 | |
| 22 | bias2 = torch.Tensor(d1, 1) |
| 23 | self.B2 = nn.Parameter(bias2) |
| 24 | nn.init.constant_(self.B2, 0) |
| 25 | |
| 26 | l2 = torch.Tensor(d1, 1) |
| 27 | self.l2 = nn.Parameter(l2) |
| 28 | nn.init.xavier_normal_(self.l2) |
| 29 | |
| 30 | y1 = torch.Tensor(1, ) |
| 31 | self.y1 = nn.Parameter(y1) |
| 32 | nn.init.constant_(self.y1, 0.5) |
| 33 | |
| 34 | y2 = torch.Tensor(1, ) |
| 35 | self.y2 = nn.Parameter(y2) |
| 36 | nn.init.constant_(self.y2, 0.5) |
| 37 | |
| 38 | def forward(self, x): |
| 39 | |
| 40 | # if the two scalars are negative then we setting them to 0 |
| 41 | if (self.y1[0] < 0): |
| 42 | y1 = torch.cuda.FloatTensor(1, ) |
| 43 | self.y1 = nn.Parameter(y1) |
| 44 | nn.init.constant_(self.y1, 0.01) |
| 45 | |
| 46 | if (self.y2[0] < 0): |
| 47 | y2 = torch.cuda.FloatTensor(1, ) |
| 48 | self.y2 = nn.Parameter(y2) |
| 49 | nn.init.constant_(self.y2, 0.01) |
| 50 | |
| 51 | # normalization along the temporal dimensione |
| 52 | T2 = torch.ones([self.t1, 1], device="cuda") |
| 53 | x2 = torch.mean(x, dim=2) |
| 54 | x2 = torch.reshape(x2, (x2.shape[0], x2.shape[1], 1)) |
| 55 | |
| 56 | std = torch.std(x, dim=2) |
| 57 | std = torch.reshape(std, (std.shape[0], std.shape[1], 1)) |
| 58 | # it can be possible that the std of some temporal slices is 0, and this produces inf values, so we have to set them to one |
| 59 | std[std < 1e-4] = 1 |
| 60 | |
| 61 | diff = x - (x2 @ (T2.T)) |
| 62 | Z2 = diff / (std @ (T2.T)) |
| 63 | |