赞
踩
import torch import torch.nn as nn import torch.optim as optimizer import torch.utils.data as Data dtype = torch.FloatTensor sentences = ['i like cat','i love coffee','i hate milk'] sentences_list = " ".join(sentences).split() # ['i', 'like', 'cat', 'i', 'love', 'coffee', 'i', 'hate', 'milk'] # print(sentences_list) # 构建词汇表 vocab = list(set(sentences_list)) # 用set去重,再转换成list # ['i', 'like', 'cat', 'love', 'coffee', 'hate', 'milk'] word2idx = { w:i for i, w in enumerate(vocab)} # {'i':0, 'like':1, 'cat':2, 'love':3, 'coffee':4, 'hate':5, 'milk':6} idx2word = { i:w for i, w in enumerate(vocab)} # {0:'i', 1:'like', 2:'cat', 3:'love', 4:'coffee', 5:'hate', 6:'milk'} n_class = len(vocab) # number of Vocabulary, just like |V|, in this task n_class=7 # 定义参数 NNLM(Neural Network Language Model) Parameter m = 2 # m in paper, word embedding dim n_step = 2 # 输入句子的长度 # n-1 in paper, look back n_step words and predict next word. In this task n_step=2 n_hidden = 10 # 隐藏层神经元个数 # h in paper # 构建X def make_data(sentences): input_data = [
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。