Skip to content

Instantly share code, notes, and snippets.

@groverpr
Created December 28, 2017 08:35
Show Gist options
  • Select an option

  • Save groverpr/a56aa993e393820375ade5dc4e3c8c83 to your computer and use it in GitHub Desktop.

Select an option

Save groverpr/a56aa993e393820375ade5dc4e3c8c83 to your computer and use it in GitHub Desktop.
cf nn
# nh = dimension of hidden linear layer
# p1 = dropout1
# p2 = dropout2
class EmbeddingNet(nn.Module):
def __init__(self, n_users, _n_movies, nh = 10, p1 = 0.05, p2= 0.5):
super().__init__()
(self.u, self.m, self.ub, self.mb) = [get_emb(*o) for o in [
(n_users, n_factors), (n_movies, n_factors),
(n_users,1), (n_movies,1)
]]
self.lin1 = nn.Linear(n_factors*2, nh) # bias is True by default
self.lin2 = nn.Linear(nh, 1)
self.drop1 = nn.Dropout(p = p1)
self.drop2 = nn.Dropout(p = p2)
def forward(self, cats, conts): # forward pass i.e. dot product of vector from movie embedding matrixx
# and vector from user embeddings matrix
# torch.cat : concatenates both embedding matrix to make more columns, same rows i.e. n_factors*2, n : rows
# u(users) is doing lookup for indexed mentioned in users
# users has indexes to lookup in embedding matrix.
users,movies = cats[:,0],cats[:,1]
u2,m2 = self.u(users) , self.m(movies)
x = self.drop1(torch.cat([u2,m2], 1)) # drop initialized weights
x = self.drop2(F.relu(self.lin1(x))) # drop 1st linear + nonlinear wt
r = F.sigmoid(self.lin2(x)) * (max_rating - min_rating) + min_rating
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment