任务描述:pytorch实现简单的神经网络语言模型NNLM
一、数据
sentences=[‘i like dog’, ‘i love coffee’, ‘i hate milk’]
二、目标
当输入[‘i’,‘like’], [‘i’,‘love’], [‘i’,‘hate’]时,分别预测下一个单词为[‘dog’], [‘coffee’],[‘milk’]
三、完整代码
1. 导入需要的库,设置数据类型
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
dtype = torch.FloatTensor
2. 分词,创建字典
sentences = ['i like dog', 'i love coffee', 'i hate milk']
words = ' '.join(sentences).split()
words_dict = list(set(words))
num2word = {
index:word for index, word in enumerate(words_dict)}
word2num = {
word:index for index, word in enumerate(words_dict)}
3. 准备数据
def make_batch(sentences):
input_batch = []
target_batch = []
for sen in sentences