Created
April 12, 2015 16:08
-
-
Save anonymous/f2c906f2afee32cbbbb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'nn' | |
| input = torch.DoubleTensor{4} --input word index | |
| target = torch.DoubleTensor{6,5,4,2} --target word indices. So we have 1 true context ("6") and 3 negative contexts ("5,4,2") | |
| label = torch.DoubleTensor({1,0,0,0}) --the first label is true sample, rest are neg samples | |
| model1 = nn.Sequential() | |
| model1:add(nn.LookupTable(1000,4)) -- word embedding layer, vocab size is 1000 and each word is 50 dimensional | |
| model2 = nn.Sequential() | |
| model2:add(nn.LookupTable(1000,4)) -- context embedding layer, this will be dot producted with an element of the word embedding layer | |
| sig_layer = nn.SoftMax() -- SoftMax layer to get probabilities | |
| criterion = nn.BCECriterion() -- simple binary case, so using BCE Criterion | |
| dot_prod = model2:forward(input):cmul(model1:forward(input):view(4)) -- results in a tensor where each element is the log odds of being a true sample | |
| probs = sig_layer:forward(dot_prod) --get probabilities | |
| -- get errors | |
| err = criterion:forward(probs, label) | |
| dl_do = criterion:backward(probs, label) | |
| --backpropagate through both lookuptables | |
| model1:zeroGradParameters() | |
| model1:backward(input, dl_do) | |
| model1:updateParameters(0.01) --update word embeddings | |
| model2:zeroGradParameters() | |
| model2:backward(input, dl_do) --update context embeddings | |
| model2:updateParameters(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think there is an error in line 14 and 27