[DL]基于pytorch的Elman RNN语言模型
来源:互联网 发布:nat123免费域名 编辑:程序博客网 时间:2024/06/03 22:39
留个坑,先放代码,后面再解释
#qa_pairs_parallel.txtimport torchimport torch.nn as nnimport jiebaclass Sentences(object): def __init__(self,dirname): self.dirname=dirname def __iter__(self): for line in open(self.dirname,'r').readlines(): yield line.replace('\n','')import jiebaimport copysentences=Sentences('./corpus_data/qa_pairs_parallel.txt')x=[]y=[]for line in sentences: arrs=line.split('$$') for arr in arrs[:2]: temp=list(arr) tempx=copy.copy(temp);tempy=copy.copy(temp) tempx.insert(0,'<start>');x.append(tempx) tempy.append('<end>');y.append(tempy)print(len(x),x[:5])print(len(y),y[:5])dictionary=dict()for lists in [x,y]: for words in lists: for word in words: if word not in dictionary: dictionary[word]=len(dictionary)print(len(dictionary))id2word=dict()for word in dictionary: id2word[dictionary[word]]=wordprint(dictionary['<end>'])print(dictionary['<start>'])print(id2word[1])print(id2word[222])class RNN(nn.Module): def __init__(self,vocab_size,input_size,hidden_size): super(RNN,self).__init__() self.input_size=input_size self.hidden_size=hidden_size self.vocab_size=vocab_size self.sigmoid=nn.Sigmoid() self.softmax=nn.Softmax() self.linear1=nn.Linear(self.vocab_size+self.hidden_size,self.hidden_size) self.linear2=nn.Linear(self.hidden_size,self.vocab_size) def forward(self,input_x,input_hidden): x_t=torch.cat((input_x.view(1,-1),input_hidden.view(1,-1)),dim=1) hidden=self.sigmoid(self.linear1(x_t)) output=self.softmax(self.linear2(hidden)) return [output,hidden]hidden_size=256vocab_size=len(dictionary)input_size=1000model=RNN(vocab_size,input_size,256)if torch.cuda.is_available(): model.cuda()loss_fun = nn.CrossEntropyLoss()optimizer=torch.optim.SGD(model.parameters(),lr=0.1)embedding = torch.nn.Embedding(vocab_size, vocab_size, padding_idx=0)one2n=torch.eye(vocab_size)for _ in range(5): for i,xi in enumerate(x[:]): input_hidden=torch.autograd.Variable(torch.randn(1,hidden_size)) loss=0.0 model.zero_grad() for j,word in enumerate(xi): input=torch.autograd.Variable(one2n[dictionary[word]],requires_grad=True).view(1,-1) target=torch.autograd.Variable(torch.cuda.LongTensor([dictionary[y[i][j]]])) out,input_hidden=model(input.cuda(),input_hidden.cuda()) loss+=loss_fun(out,target) print('-'*89) loss/=len(xi) print('loss=',loss.data) loss.backward(retain_graph=True) optimizer.step()torch.save(model,'rnn_language_model.m')
阅读全文
0 0
- [DL]基于pytorch的Elman RNN语言模型
- [DL]基于Pytorch的seq2seq模型
- [DL]基于Pytorch的Linear classified model
- 基于RNN的语言模型与机器翻译NMT
- 基于RNN的语言模型与机器翻译NMT
- LightRNN —— 基于RNN的轻量级语言模型
- [DL]基于Pytorch的N-gram Language Model
- 深度学习之六,基于RNN(GRU,LSTM)的语言模型分析与theano代码实现
- 基于循环神经网络实现基于字符的语言模型(char-level RNN Language Model)-tensorflow实现
- pytorch 模型的加载
- 基于RNN的文本分类模型(Tensorflow)
- 语言模型(三) RNN
- tensorflow RNN LSTM语言模型
- PyTorch关于RNN序列数据的pack_pad处理
- Tensorflow中Demo学习:RNN语言模型
- 用 RNN 训练语言模型生成文本
- 自然语言处理(二 RNN语言模型)
- 如何用pyTorch改造基于Keras的MIT情感理解模型
- Java 深入学习(4) —— 一切都是对象及内存分配
- Java 实现阶乘算法
- 206. Reverse Linked List
- 热更新机制
- 教你用300万共享单车出行数据,预测骑行目的地 !(附源码)
- [DL]基于pytorch的Elman RNN语言模型
- [RK3399][Android7.1] Ubuntu 编译环境之Jdk版本切换
- gunicorn 部署 flask 应用
- Hive2.0函数大全(中文版)
- 39. Combination Sum
- flask转换器
- 最长上升子序列(LIS)和最长下降子序列(LDS)
- 抽象类(abstract class)和接口(interface)有什么异同?
- linux 下 du, df, top, free, pstack, su, sudo(sudo -, sudo -s), adduser, password命令介绍