python笔记 词嵌入在电影评论情感判断的应用

6.1处理文本数据

深度学习模型不会接收原始文本作为输入,它只能处理数值张量。文本向量化(vectorize)是指将文本转换为数值张量的过程。

 

6.1.1单词和字符的 one-hot 编码P149

6.1.2 使用词嵌入

将单词与向量相关联还有另一种常用的强大方法,就是使用密集的词向量(word vector),

也叫词嵌入

  1. 利用 Embedding 层学习词嵌入:单词索引- >Embedding层->对应的词向量
    from keras.layers import Embedding
    embedding_layer = Embedding(1000, 64)
    
    #此处书本是from keras.layers import preprocessing,但是我下载的keras,preprocessing是在keras文件夹下的
    from keras.datasets import imdb
    from keras import preprocessing
    #作为特征的单词个数
    max_features = 10000
    #在这么多单词后截断文本(这些单词都属于前 max_features 个最常见的单词)
    maxlen = 20
    
    
    #将数据加载为整数列表
    (x_train, y_train), (x_test, y_test) = imdb.load_data(
    num_words=max_features)
    
    
    #将整数列表转换成形状为 (samples,maxlen) 的二维整数张量
    x_train = preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen)
    x_test = preprocessing.sequence.pad_sequences(x_test, maxlen=maxlen)
    
    
    from keras.models import Sequential
    from keras.layers import Flatten, Dense, Embedding
    #指定 Embedding 层的最大输入长度,以便后面将嵌入输入展平。 Embedding 层激活的形状为 (samples, maxlen, 8)
    model = Sequential()
    model.add(Embedding(10000, 8, input_length=maxlen))
    #将三维的嵌入张量展平成形状为 (samples, maxlen * 8) 的二维张量
    model.add(Flatten())
    #在上面添加分类器
    model.add(Dense(1, activation='sigmoid'))
    model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
    model.summary()
    history = model.fit(x_train, y_train,
    epochs=10,
    batch_size=32,
    validation_split=0.2)

    得到结果将近75%:

6.1.3 整合在一起:从原始文本到词嵌入

使用预处理方法再处理一次电影评论。预训练的词嵌入对训练数据很少的问题特别有用,因此只限定数据集为前200个样本。将训练评论转换成字符串列表,每个字符串对应一条评论。

首先,打开 http://mng.bz/0tIo,下载原始 IMDB 数据集并解压。我的解压地址是/home/jack/文档/deeplearn/第六章/预训练词嵌入/aclImdb。

import os
imdb_dir = '/home/jack/文档/deeplearn/第六章/预训练词嵌入/aclImdb'
train_dir = os.path.join(imdb_dir, 'train')
labels = []
texts = []
for label_type in ['neg', 'pos']:
    dir_name = os.path.join(train_dir, label_type)
    for fname in os.listdir(dir_name):
        if fname[-4:] == '.txt':
            f = open(os.path.join(dir_name, fname))
            texts.append(f.read())
            f.close()
            if label_type == 'neg':
                labels.append(0)
            else:
                labels.append(1)



from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import numpy as np

#在 100 个单词后截断评论
maxlen = 100
#在 200 个样本上训练
training_samples = 200
#在 10 000 个样本上验证
validation_samples = 10000
#只考虑数据集中前 10 000 个最常见的单词
max_words = 10000

tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)

word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))

data = pad_sequences(sequences, maxlen=maxlen)
labels = np.asarray(labels)
print('Shape of data tensor:', data.shape)
print('Shape of label tensor:', labels.shape)

'''将数据划分为训练集和验证集,但首先要打乱数据,因为一开始数据中的样本是排好序的(所有负面评论都在前面,然后是所有正面评论)'''
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]

x_train = data[:training_samples]
y_train= labels[:training_samples]
x_val =data[training_samples: training_samples + validation_samples]
y_val =labels[training_samples: training_samples + validation_samples]

  打开 https://nlp.stanford.edu/projects/glove,下载 2014 年英文维基百科的预计算嵌入。这是一个 822 MB 的压缩文件,文件名是 glove.6B.zip,里面包含 400 000 个单词(或非单词的标记)的 100 维嵌入向量。解压文件。

  解压到的文件夹:/home/jack/文档/deeplearn/第六章/预训练词嵌入/glove.6B

  #解析 GloVe 词嵌入文件。我们对解压后的文件(一个 .txt 文件)进行解析,构建一个将单词(字符串)映射为其向量表示(数值向量)的索引。

glove_dir = '/home/jack/文档/deeplearn/第六章/预训练词嵌入/glove.6B'
embeddings_index = {}
f = open(os.path.join(glove_dir, 'glove.6B.100d.txt'))
for line in f:
    values = line.split()
    word = values[0]
    coefs = np.asarray(values[1:], dtype='float32')
    embeddings_index[word] = coefs
f.close()
print('Found %s word vectors.' % len(embeddings_index))


#准备 GloVe 词嵌入矩阵
embedding_dim = 100
embedding_matrix = np.zeros((max_words, embedding_dim))
#嵌入索引( embeddings_index )中找不到的词,其嵌入向量全为 0
for word, i in word_index.items():
    if i < max_words:
        embedding_vector = embeddings_index.get(word)
        if embedding_vector is not None:
            embedding_matrix[i] = embedding_vector



#定义模型
from keras.models import Sequential
from keras.layers import Embedding, Flatten, Dense
model = Sequential()
model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()


#将预训练的词嵌入加载到 Embedding 层中
model.layers[0].set_weights([embedding_matrix])
#需要冻结 Embedding 层(即将其 trainable 属性设为 False )
model.layers[0].trainable = False


#训练模型
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(x_train, y_train,
epochs=10,
batch_size=32,
validation_data=(x_val, y_val))
model.save_weights('pre_trained_glove_model.h5')


#在不使用预训练词嵌入的情况下,训练相同的模型
# from keras.models import Sequential
# from keras.layers import Embedding, Flatten, Dense
# model = Sequential()
# model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
# model.add(Flatten())
# model.add(Dense(32, activation='relu'))
# model.add(Dense(1, activation='sigmoid'))
# model.summary()
# model.compile(optimizer='rmsprop',
# loss='binary_crossentropy',
# metrics=['acc'])
# history = model.fit(x_train, y_train,
# epochs=10,
# batch_size=32,
# validation_data=(x_val, y_val))




#训练精度和验证精度
get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

#训练损失和验证损失
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()

大约精度是百分之58%。

如果将模型更换为不使用预训练词嵌入。

精度大约为50%。

 

  因为样本太小,所以精度主要取决于200个样本的选择,重新随机选择200个样本精确度的差距都可能很大。

 

 

 

最后,我们在测试数据上评估模型

 

#对测试集数据进行分词
test_dir = os.path.join(imdb_dir, 'test')
labels = []
texts = []
for label_type in ['neg', 'pos']:
    dir_name = os.path.join(test_dir, label_type)
    for fname in sorted(os.listdir(dir_name)):
        if fname[-4:] == '.txt':
            f = open(os.path.join(dir_name, fname))
            texts.append(f.read())
            f.close()
            if label_type == 'neg':
                labels.append(0)
            else:
                labels.append(1)
sequences = tokenizer.texts_to_sequences(texts)
x_test = pad_sequences(sequences, maxlen=maxlen)
y_test = np.asarray(labels)

#在测试集上评估模型
model.load_weights('pre_trained_glove_model.h5')
model.evaluate(x_test, y_test)

 

精确度为57%,相对来说只选取了200个样本,这个数据应该是很好的了。 

 

github地址:https://github.com/GiveMeLuna/deeplearning.git

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值