将Gensim词向量输入Pytorch双向RNN以进行词预测

时间:2019-02-12 05:35:50

标签: python pytorch recurrent-neural-network

我尝试使用下面的代码自己执行此操作,但是我的Pytorch双向RNN输出所有NaN吗?有人可以建议或提供代码,将Gensim词向量输入到Pytorch RNN并输出下一个词预测。代码使用Python 3.6和Pytorch的最新版本(2019年2月)。

import gensim
import gensim.downloader as api
import torch
import torch, torch.nn as nn
from torch.autograd import Variable

word_vectors = api.load("glove-wiki-gigaword-100")

before_blank_vectors = []
after_blank_vectors = []
for word in before_blank:
    before_blank_vectors.append(word_vectors.get_vector(word))
for word in after_blank:
    after_blank_vectors.append(word_vectors.get_vector(word))

seq_len = 3
batch_size = 1
embedding_size = 100
hidden_size = 1
output_size = 1

before_blank_vectors_tensor = Variable(torch.FloatTensor(3, 1, 100))

bi_rnn = torch.nn.RNN(input_size=100, hidden_size=1, num_layers=1, batch_first=False, bidirectional=True)

bi_output, bi_hidden = bi_rnn(before_blank_vectors_tensor)

print(bi_output)

打印出:

tensor([[[nan, nan]],

        [[nan, nan]],

        [[nan, nan]]], grad_fn=<CatBackward>)

0 个答案:

没有答案