X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=mygpt.py;h=b137cdbca1cd085ee8dec0185c514118e928b5d3;hb=9112db2ed7d8c262c4ef8298cf6637515675f967;hp=0414bb615435d610d2d7c1c0123763081462f330;hpb=ec5bad2e7911bdf9b7851342a3ee007d41b80963;p=mygptrnn.git diff --git a/mygpt.py b/mygpt.py index 0414bb6..b137cdb 100755 --- a/mygpt.py +++ b/mygpt.py @@ -504,6 +504,7 @@ class Caterpillar(nn.Module): self.gate_dropout_proba = args.gate_dropout_proba self.gate_dropout_sync = args.gate_dropout_sync + self.gate_dropout_replace = args.gate_dropout_replace ###################################################################### @@ -527,14 +528,14 @@ class Caterpillar(nn.Module): dim_v, ) - def reset_inner_loss(self): - self.acc_attention = 0 - self.acc_nb = 0 + # def reset_inner_loss(self): + # self.acc_attention = 0 + # self.acc_nb = 0 - def get_inner_loss(self): - # warnings.warn("l2 regularization", RuntimeWarning) - # return (self.acc_attention / self.acc_nb).pow(2).sum() - return torch.tensor([0], device=self.w_Q.device) + # def get_inner_loss(self): + # warnings.warn("l2 regularization", RuntimeWarning) + # return (self.acc_attention / self.acc_nb).pow(2).sum() + # return torch.tensor([0], device=self.w_Q.device) def forward(self, bs): # Dimensions to make the source a bit clearer, that's needed @@ -612,11 +613,8 @@ class Caterpillar(nn.Module): gated_V = gated_V.unflatten(2, (-1, L)) gated_K = gated_K.unflatten(2, (-1, L)) - next_V = pscan_dim(A, gated_V, init_rec_V, dim=2) - next_K = pscan_dim(A, gated_K, init_rec_K, dim=2) - - next_V = next_V.flatten(2, 3) - next_K = next_K.flatten(2, 3) + next_V = pscan_dim(A, gated_V, init_rec_V, dim=2).flatten(2, 3) + next_K = pscan_dim(A, gated_K, init_rec_K, dim=2).flatten(2, 3) return next_V, next_K @@ -629,15 +627,21 @@ class Caterpillar(nn.Module): warnings.warn("gate dropout", RuntimeWarning) + if self.gate_dropout_sync: + shape_kill = (N, 1, 1) + else: + shape_kill = (N, H, R) + # Pick a point in each of the NxHxR timeline and set this # entry and the following to 1 kill = ( - torch.rand(N, H, R, t1 - t0, device=G.device).sort(dim=3).indices == 0 + torch.rand(*shape_kill, t1 - t0, device=G.device).sort(dim=3).indices + == 0 ).cumsum(dim=3) # Keep these mask for only some of the NxHxR kill = kill * ( - torch.rand(N, H, R, 1, device=G.device) <= self.gate_dropout_proba + torch.rand(*shape_kill, 1, device=G.device) <= self.gate_dropout_proba ) # The coefficient to keep are the complementary @@ -645,10 +649,14 @@ class Caterpillar(nn.Module): masked_next_V, masked_next_K = recurrence(G * mask, V, K) - next_V = next_V.detach() + (masked_next_V - masked_next_V.detach()) / ( + if self.gate_dropout_replace: + next_V = next_V.detach() + next_K = next_K.detach() + + next_V = next_V + (masked_next_V - masked_next_V.detach()) / ( 1 - self.gate_dropout_proba ) - next_K = next_K.detach() + (masked_next_K - masked_next_K.detach()) / ( + next_K = next_K + (masked_next_K - masked_next_K.detach()) / ( 1 - self.gate_dropout_proba )