X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;ds=inline;f=mygpt.py;h=f9547972e6a937c02ef299aabf639570b85b111f;hb=3ae0c8f3767e4285ab548e4548576a6ddf6003bb;hp=954f4f088c29116052f3258dd13eda2f5a6b3b0c;hpb=b6a9cc237cdadac2351814f92c20607d46b0f583;p=mygpt.git diff --git a/mygpt.py b/mygpt.py index 954f4f0..f954797 100755 --- a/mygpt.py +++ b/mygpt.py @@ -66,9 +66,9 @@ class QKVAttention(nn.Module): a = torch.einsum('nhtd,nhsd->nhts', q, k) / math.sqrt(q.size(3)) if self.causal: - mask = torch.arange(a.size(2), device = q.device)[None, None, :, None] \ - < torch.arange(a.size(3), device = q.device)[None, None, None, :] - a = a.masked_fill(mask, float('-inf')) + forbidden_attention = torch.arange(a.size(2), device = q.device)[None, None, :, None] \ + < torch.arange(a.size(3), device = q.device)[None, None, None, :] + a = a.masked_fill(forbidden_attention, float('-inf')) a = a.softmax(dim = 3) a = F.dropout(a, self.attention_dropout, self.training) @@ -124,12 +124,19 @@ class MyGPT(nn.Module): self.readout = nn.Linear(in_features = dim_model, out_features = vocabulary_size) + with torch.no_grad(): + for m in self.modules(): + if isinstance(m, nn.Embedding): + m.weight.normal_(mean = 0, std = 2e-2) + elif isinstance(m, nn.LayerNorm): + m.bias.zero_() + m.weight.fill_(1.0) + def forward(self, x): - x = F.pad(x, (1, 0)) + x = F.pad(x, (1, -1)) x = self.embedding(x) x = self.trunk(x) x = self.readout(x) - x = F.pad(x, (0, 0, 0, -1)) return x ######################################################################