Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. import torch
  2. import torch.nn as nn
  3. import numpy as np
  4.  
  5. class CBOW(torch.nn.Module):
  6.  
  7. def __init__(self, inp_size , vocab_size, embedding_dim=100):
  8. super(CBOW, self).__init__()
  9. self.embeddings = nn.Embedding(vocab_size, embedding_dim)
  10. self.linear1 = nn.Linear(embedding_dim, 100)
  11. self.activation_function1 = nn.ReLU()
  12. self.linear2 = nn.Linear(100, vocab_size)
  13. self.activation_function2 = nn.LogSoftmax(dim = -1)
  14.  
  15.  
  16. def forward(self, inputs):
  17. embeds = sum(self.embeddings(torch.from_numpy(inputs).long())).view(1,-1)
  18. out = self.linear1(embeds)
  19. out = self.activation_function1(out)
  20. out = self.linear2(out)
  21. out = self.activation_function2(out)
  22. return out
  23.  
  24. model = CBOW(window_size*2,vocab_size)
  25.  
  26. loss_function = nn.NLLLoss()
  27. optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement