Skip to content

Instantly share code, notes, and snippets.

@santhalakshminarayana
Last active January 6, 2020 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santhalakshminarayana/c55b600cb955c7a4de24d3bf8b9ac0e7 to your computer and use it in GitHub Desktop.
Save santhalakshminarayana/c55b600cb955c7a4de24d3bf8b9ac0e7 to your computer and use it in GitHub Desktop.
Quotes Glove Model - Medium
def get_batch(batch_size):
ind = np.random.permutation(occs.size).tolist()
i = 0
for i in range(0, tot_pairs, batch_size):
batch_ids = ind[i:i+batch_size]
yield p1[batch_ids], p2[batch_ids], occs[batch_ids]
device = None
if torch.cuda.is_available():
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
class Glove(nn.Module) :
def __init__(self, vocab_len, num_dim,):
super(Glove, self).__init__()
self.ui = nn.Embedding(vocab_len, num_dim).to(device)
self.uj = nn.Embedding(vocab_len, num_dim).to(device)
self.bi = nn.Embedding(vocab_len, 1).to(device)
self.bj = nn.Embedding(vocab_len, 1).to(device)
self.ui.weight.data.uniform_(-1,1)
self.uj.weight.data.uniform_(-1,1)
self.bi.weight.data.zero_()
self.bj.weight.data.zero_()
def forward(self, i_vecs, j_vecs):
i_s = self.ui(i_vecs)
j_s = self.uj(j_vecs)
b_i = torch.squeeze(self.bi(i_vecs))
b_j = torch.squeeze(self.bj(j_vecs))
return torch.sum(i_s * j_s, dim = 1) + b_i + b_j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment