NLP(二十二)使用LSTM进行语言建模以预测最优词
原文链接:http://www.one2know.cn/nlp22/
- 预处理
数据集使用Facebook上的BABI数据集
将文件提取成可训练的数据集,包括:文章 问题 答案
def get_data(infile):
stories,questions,answers = [],[],[]
story_text = []
fin = open(infile,'rb')
for line in fin:
line = line.decode('utf-8').strip()
lno,text = line.split(' ',1)
if '\t' in text:
question,answer,_ = text.split('\t')
stories.append(story_text)
questions.append(question)
answers.append(answer)
story_text = []
else:
story_text.append(text)
fin.close()
return stories,questions,answers
data_train = get_data('qa1_single-supporting-fact_train.txt')
data_test = get_data('qa1_single-supporting-fact_test.txt')
print('\nTrain observations:',len(data_train[0]),
'Test observations:',len(data_test[0]),'\n')
输出:
Train observations: 10000 Test observations: 1000
- 如何实现
1.预处理:创建字典并将文章,问题和答案映射到词表,进一步映射成向量形式
2.模型创建和验证:训练模型并在验证数据集上测试
3.预测结果:测试集测试数据的结果 - 代码
from __future__ import division,print_function
import collections
import itertools
import nltk
import numpy as np
import matplotlib.pyplot as plt
import os
import random
def get_data(infile):
stories,questions,answers = [],[],[]
story_text = []
fin = open(infile,'rb')
for line in fin:
line = line.decode('utf-8').strip()
lno,text = line.split(' ',1) # 去掉前面的数字标记
if '\t' in text: # 有制表符的是 问题 和 答案
question,answer,_ = text.split('\t')
stories.append(story_text)
questions.append(question)
answers.append(answer)
story_text = []
else: # 没制表符的是文章
story_text.append(text)
fin.close()
return stories,questions,answers
data_train = get_data('qa1_single-supporting-fact_train.txt')
data_test = get_data('qa1_single-supporting-fact_test.txt')
print('\nTrain observations:',len(data_train[0]),
'Test observations:',len(data_test[0]),'\n')
print(data_train[0][1],data_train[1][1],data_train[2][1])
# ['Daniel went back to the hallway.', 'Sandra moved to the garden.'] Where is Daniel? hallway
print(np.array(data_train).shape)
# (3, 10000)
dictnry = collections.Counter() # 返回列表元素出现次数的 字典,这里没有参数是一个空字典
for stories,questions,answers in [data_train,data_test]:
for story in stories:
for sent in story:
for word in nltk.word_tokenize(sent):
dictnry[word.lower()] += 1
for question in questions:
for word in nltk.word_tokenize(question):
dictnry[word.lower()] += 1
for answer in answers:
for word in nltk.word_tokenize(answer):
dictnry[word.lower()] += 1
word2indx = {w:(i+1) for i,(w,_) in enumerate(dictnry.most_common())} # 按词频排序
word2indx['PAD'] = 0
indx2word = {v:k for k,v in word2indx.items()}
vocab_size = len(word2indx) # 一共有22个不重复单词
print('vocabulary size:',len(word2indx))
story_maxlen = 0
question_maxlen = 0
for stories,questions,answers in [data_train,data_test]:
for story in stories:
story_len = 0
for sent in story:
swords = nltk.word_tokenize(sent)
story_len += len(swords)
if story_len > story_maxlen:
story_maxlen = story_len
for question in questions:
question_len = len(nltk.word_tokenize(question))
if question_len > question_maxlen:
question_maxlen = question_len
print('Story maximum length:',story_maxlen,'Question maximum length:',question_maxlen)
# 文章单词最大长度为14,问题中的单词最大长度为4,长度不够的补0,维度相同便于并向计算
from keras.layers import Input
from keras.layers.core import Activation,Dense,Dropout,Permute
from keras.layers.embeddings import Embedding
from keras.layers.merge import add,concatenate,dot
from keras.layers.recurrent import LSTM
from keras.models import Model
from keras.preprocessing.sequence import pad_sequences
from keras.utils import np_utils
def data_vectorization(data,word2indx,story_maxlen,question_maxlen): # 词 => 词向量
Xs,Xq,Y = [],[],[]
stories,questions,answers = data
for story,question,answer in zip(stories,questions,answers):
xs = [[word2indx[w.lower()] for w in nltk.word_tokenize(s)] for s in story] #
xs = list(itertools.chain.from_iterable(xs))
xq = [word2indx[w.lower()] for w in nltk.word_tokenize(question)]
Xs.append(xs)
Xq.append(xq)
Y.append(word2indx[answer.lower()])
return pad_sequences(Xs,maxlen=story_maxlen),pad_sequences(Xq,maxlen=question_maxlen),\
np_utils.to_categorical(Y,num_classes=len(word2indx))
Xstrain,Xqtrain,Ytrain = data_vectorization(data_train,word2indx,story_maxlen,question_maxlen)
Xstest,Xqtest,Ytest = data_vectorization(data_test,word2indx,story_maxlen,question_maxlen)
print('Train story',Xstrain.shape,'Train question',Xqtrain.shape,'Train answer',Ytrain.shape)
print('Test story',Xstest.shape,'Test question',Xqtest.shape,'Test answer',Ytest.shape)
# 超参数
EMBEDDING_SIZE = 128
LATENT_SIZE = 64
BATCH_SIZE = 64
NUM_EPOCHS = 40
# 输入层
story_input = Input(shape=(story_maxlen,))
question_input = Input(shape=(question_maxlen,))
# Story encoder embedding
# 将正整数(索引)转换为固定大小的密集向量。
# 例如,[[4],[20]]->[[0.25,0.1],[0.6,-0.2]] 此层只能用作模型中的第一层
story_encoder = Embedding(input_dim=vocab_size,output_dim=EMBEDDING_SIZE,input_length=story_maxlen)(story_input)
story_encoder = Dropout(0.2)(story_encoder)
# Question encoder embedding
question_encoder = Embedding(input_dim=vocab_size,output_dim=EMBEDDING_SIZE,input_length=question_maxlen)(question_input)
question_encoder = Dropout(0.3)(question_encoder)
# 返回两个张量的点积
match = dot([story_encoder,question_encoder],axes=[2,2])
# 将故事编码为问题的向量空间
story_encoder_c = Embedding(input_dim=vocab_size,output_dim=question_maxlen,input_length=story_maxlen)(story_input)
story_encoder_c = Dropout(0.3)(story_encoder_c)
# 结合两个向量 match和story_encoder_c
response = add([match,story_encoder_c])
response = Permute((2,1))(response)
# 结合两个向量 response和question_encoder
answer = concatenate([response, question_encoder], axis=-1)
answer = LSTM(LATENT_SIZE)(answer)
answer = Dropout(0.2)(answer)
answer = Dense(vocab_size)(answer)
output = Activation("softmax")(answer)
model = Model(inputs=[story_input, question_input], outputs=output)
model.compile(optimizer="adam", loss="categorical_crossentropy",metrics=["accuracy"])
print(model.summary())
# 模型训练
history = model.fit([Xstrain,Xqtrain],[Ytrain],batch_size=BATCH_SIZE,epochs=NUM_EPOCHS,
validation_data=([Xstest,Xqtest],[Ytest]))
# 画出准确率和损失函数
plt.title('Episodic Memory Q&A Accuracy')
plt.plot(history.history['acc'],color='g',label='train')
plt.plot(history.history['val_acc'],color='r',label='validation')
plt.legend(loc='best')
plt.show()
# get predictions of labels
ytest = np.argmax(Ytest, axis=1)
Ytest_ = model.predict([Xstest, Xqtest])
ytest_ = np.argmax(Ytest_, axis=1)
# 随机选择几个问题测试
NUM_DISPLAY = 10
for i in random.sample(range(Xstest.shape[0]),NUM_DISPLAY):
story = " ".join([indx2word[x] for x in Xstest[i].tolist() if x != 0])
question = " ".join([indx2word[x] for x in Xqtest[i].tolist()])
label = indx2word[ytest[i]]
prediction = indx2word[ytest_[i]]
print(story, question, label, prediction)
输出:

NLP(二十二)使用LSTM进行语言建模以预测最优词的更多相关文章
- NLP(二十三)使用LSTM进行语言建模以预测最优词
N元模型 预测要输入的连续词,比如 如果抽取两个连续的词汇,则称之为二元模型 准备工作 数据集使用 Alice in Wonderland 将初始数据提取N-grams import nltk imp ...
- JAVA基础知识总结:一到二十二全部总结
>一: 一.软件开发的常识 1.什么是软件? 一系列按照特定顺序组织起来的计算机数据或者指令 常见的软件: 系统软件:Windows\Mac OS \Linux 应用软件:QQ,一系列的播放器( ...
- (C/C++学习笔记) 二十二. 标准模板库
二十二. 标准模板库 ● STL基本介绍 标准模板库(STL, standard template library): C++提供的大量的函数模板(通用算法)和类模板. ※ 为什么我们一般不需要自己写 ...
- 智课雅思词汇---二十二、-al即是名词性后缀又是形容词后缀
智课雅思词汇---二十二.-al即是名词性后缀又是形容词后缀 一.总结 一句话总结: 后缀:-al ②[名词后缀] 1.构成抽象名词,表示行为.状况.事情 refusal 拒绝 proposal 提议 ...
- 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环
目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...
- [分享] IT天空的二十二条军规
Una 发表于 2014-9-19 20:25:06 https://www.itsk.com/thread-335975-1-1.html IT天空的二十二条军规 第一条.你不是什么都会,也不是什么 ...
- Bootstrap <基础二十二>超大屏幕(Jumbotron)
Bootstrap 支持的另一个特性,超大屏幕(Jumbotron).顾名思义该组件可以增加标题的大小,并为登陆页面内容添加更多的外边距(margin).使用超大屏幕(Jumbotron)的步骤如下: ...
- Web 前端开发精华文章推荐(HTML5、CSS3、jQuery)【系列二十二】
<Web 前端开发精华文章推荐>2014年第一期(总第二十二期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML ...
- 二十二、OGNL的一些其他操作
二十二.OGNL的一些其他操作 投影 ?判断满足条件 动作类代码: ^ $ public class Demo2Action extends ActionSupport { public ...
随机推荐
- datatables editor fields type
其实editor fields type 默认支持的输入类型就是w3c输入框类型. text number password textarea select checkbox ...
- internet连接共享被启用时 出现了一个错误 (null)
服务Firewall和Internet Sharing Service都已启动还是出现此问题 注意到点选允许共享时的提示:当前已为wireless_95364开启...... 随便选一个别的适配器,开 ...
- CentOS 配置阿里云 NTP 服务
NTP 是网络时间协议(Network Time Protocol),NTP 服务能保证服务器的本地时间与标准时间同步. ▶ 配置时区信息 1.删除系统里的当地时间链接 sudo rm /etc/lo ...
- ES 23 - 检索和过滤的区别 (query vs. filter)
目录 1 filter与query示例 1.1 准备测试数据 1.2 搜索测试 2 filter与query的区别 2.1 相关度处理上的不同 2.2 性能上的对比 2.3 对比结论 本文以 ES 6 ...
- 归并排序(从上到下、从下到上)——C语言
归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divide)成一些小的问题然后递归 ...
- Redis Sentinel基本实现原理
一.出现的背景: Redis 主从复制模式下一旦主节点由于故障不能提供服务,需要人工将从节点晋升为主节点,同时还要通知应用方更新主节点地址,对于很多应用这种场景的这种故障处理方式是非常浪费人力的.为了 ...
- java 8中新的日期和时间API
java 8中新的日期和时间API 使用LocalDate和LocalTime LocalDate的实例是一个不可变对象,它只提供了简单的日期,并不含当天的时间信息.另外,它也不附带任何与时区相关的信 ...
- 理解分布式一致性与Raft算法
理解分布式一致性与Raft算法 永远绕不开的CAP定理 出于可用性及负载方面考虑,一个分布式系统中数据必然不会只存在于一台机器,一致性简单地说就是分布式系统中的各个部分保持数据一致 但让数据保持一致往 ...
- 记录eclipse中文出现空格宽度不一致的bug
起因 不久前更新了 eclipse(2019-03) 版本:突然发现出现了,使用注释使用中出现的空格的间隔大小不一致的问题,具体可以看下图: 遇到这种问题简直逼不能忍,在网上搜一下解决方式: 谷歌 搜 ...
- mysql新建用户及授权
添加用户 CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; -- username : 自定义用户名 -- localhost ...