amaresh8053 commited on
Commit
5f9f2be
·
1 Parent(s): 12fcc4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -14,8 +14,8 @@ nltk.download(['punkt', 'punkt_tab'], quiet=True)
14
 
15
  DEVICE = torch.device("cpu")
16
 
17
- CACHE_FILE = "ubuntu_data_cache.pt" # from your notebook
18
- MODEL_FILE = "ubuntu_chatbot_best.pt" # trained model checkpoint
19
 
20
 
21
  # ------------- tokenization + helpers -------------
@@ -93,21 +93,21 @@ class Encoder(nn.Module):
93
  )
94
  # projection from 1024 (2 * 512) back to 512
95
  self.fc = nn.Linear(1024, 512)
96
- self.norm = nn.LayerNorm(512) # defined in notebook (even if not used there)
97
 
98
  def forward(self, x):
99
  # x: [B, T]
100
- e = self.emb(x) # [B, T, 256]
101
- out, h = self.gru(e) # out:[B,T,1024], h:[4,B,512] (2 layers * 2 dirs)
102
 
103
- # project encoder outputs back to 512
104
- out = self.fc(out) # [B,T,512]
105
 
106
- # combine directions in h: reshape [layers*dirs, B, H] -> [layers, dirs, B, H]
107
- h = h.view(2, 2, h.size(1), -1) # [2,2,B,512]
108
- h = torch.sum(h, dim=1) # sum over directions -> [2,B,512]
109
 
110
- return out, h # enc_out:[B,T,512], h:[2,B,512]
111
 
112
 
113
  class Decoder(nn.Module):
@@ -132,19 +132,19 @@ class Decoder(nn.Module):
132
  hidden: [2, B, 512] encoder hidden (num_layers, batch, hidden)
133
  enc_out:[B, T, 512]
134
  """
135
- e = self.dropout(self.emb(inp)) # [B,1,256]
136
 
137
  # attention over encoder outputs
138
- energy = self.attn(enc_out) # [B,T,512]
139
  # use top layer hidden state for attention
140
  attn_scores = torch.bmm(hidden[-1].unsqueeze(1), energy.transpose(1, 2)) # [B,1,T]
141
  attn_weights = F.softmax(attn_scores.squeeze(1), dim=-1).unsqueeze(1) # [B,1,T]
142
- ctx = torch.bmm(attn_weights, enc_out) # [B,1,512]
143
 
144
- x = torch.cat((e, ctx), dim=-1) # [B,1,768]
145
- out, hidden = self.gru(x, hidden) # out:[B,1,512], hidden:[2,B,512]
146
- out = self.norm(out.squeeze(1)) # [B,512]
147
- logits = self.out(out) # [B,vocab]
148
  return logits, hidden
149
 
150
 
 
14
 
15
  DEVICE = torch.device("cpu")
16
 
17
+ CACHE_FILE = "ubuntu_data_cache.pt" # To get the Vocab from cache
18
+ MODEL_FILE = "ubuntu_chatbot_best.pt" # trained model
19
 
20
 
21
  # ------------- tokenization + helpers -------------
 
93
  )
94
  # projection from 1024 (2 * 512) back to 512
95
  self.fc = nn.Linear(1024, 512)
96
+ self.norm = nn.LayerNorm(512)
97
 
98
  def forward(self, x):
99
  # x: [B, T]
100
+ e = self.emb(x)
101
+ out, h = self.gru(e)
102
 
103
+
104
+ out = self.fc(out)
105
 
106
+
107
+ h = h.view(2, 2, h.size(1), -1)
108
+ h = torch.sum(h, dim=1)
109
 
110
+ return out, h
111
 
112
 
113
  class Decoder(nn.Module):
 
132
  hidden: [2, B, 512] encoder hidden (num_layers, batch, hidden)
133
  enc_out:[B, T, 512]
134
  """
135
+ e = self.dropout(self.emb(inp))
136
 
137
  # attention over encoder outputs
138
+ energy = self.attn(enc_out)
139
  # use top layer hidden state for attention
140
  attn_scores = torch.bmm(hidden[-1].unsqueeze(1), energy.transpose(1, 2)) # [B,1,T]
141
  attn_weights = F.softmax(attn_scores.squeeze(1), dim=-1).unsqueeze(1) # [B,1,T]
142
+ ctx = torch.bmm(attn_weights, enc_out)
143
 
144
+ x = torch.cat((e, ctx), dim=-1)
145
+ out, hidden = self.gru(x, hidden)
146
+ out = self.norm(out.squeeze(1))
147
+ logits = self.out(out)
148
  return logits, hidden
149
 
150