在 Keras 模型中使用预训练的词向量

欢迎关注公众号: pythonhonor,一起学习python。
 
文章信息通过本教程,你可以掌握技能:使用预先训练的词向量和卷积神经网络解决一个文本分类问题。
本文代码已上传到  Github
本文作者: Francois Chollet

1. 什么是词向量?

”词向量”(词嵌入)是将一类将词的语义映射到向量空间中去的自然语言处理技术。即将一个词用特定的向量来表示,向量之间的距离(例如,任意两个向量之间的 L2 范式距离或更常用的余弦距离)一定程度上表征了的词之间的语义关系。由这些向量形成的几何空间被称为一个嵌入空间。
 
例如,“椰子”和“北极熊”是语义上完全不同的词,所以它们的词向量在一个合理的嵌入空间的距离将会非常遥远。但“厨房”和“晚餐”是相关的话,所以它们的词向量之间的距离会相对小。
 
理想的情况下,在一个良好的嵌入空间里,从“厨房”向量到“晚餐”向量的“路径”向量会精确地捕捉这两个概念之间的语义关系。在这种情况下,“路径”向量表示的是“发生的地点”,所以你会期望“厨房”向量 - “晚餐 ” 向量(两个词向量的差异)捕捉到“发生的地点”这样的语义关系。基本上,我们应该有向量等式:晚餐 + 发生的地点 = 厨房(至少接近)。如果真的是这样的话,那么我们可以使用这样的关系向量来回答某些问题。例如,应用这种语义关系到一个新的向量,比如“工作”,我们应该得到一个有意义的等式,工作 + 发生的地点 = 办公室,来回答“工作发生在哪里?”。
 
词向量通过降维技术表征文本数据集中的词的共现信息。方法包括神经网络 ( Word2vec ”技术 ),或矩阵分解。

2. GloVe 词向量

本文使用  GloVe 词向量 GloVe “Global Vectors for Word Representation” 的缩写,一种基于共现矩阵分解的词向量。本文所使用的 GloVe 词向量是在 2014 年的英文维基百科上训练的,有 400k 个不同的词,每个词用 100 维向量表示。 点此下载 ( 友情提示,词向量文件大小约为 822M)

3. 20 Newsgroup dataset

本文使用的数据集是著名的 ”20 Newsgroup dataset” 。该数据集共有 20 种新闻文本数据,我们将实现对该数据集的文本分类任务。数据集的说明和下载请参考 这里
 
不同类别的新闻包含大量不同的单词,在语义上存在极大的差别。一些新闻类别如下所示:
  • comp.sys.ibm.pc.hardware
  • comp.graphics
  • comp.os.ms-windows.misc
  • comp.sys.mac.hardware
  • comp.windows.x
  • rec.autos
  • rec.motorcycles
  • rec.sport.baseball
  • rec.sport.hockey

4. 实验方法

以下是我们如何解决分类问题的步骤:
  • 将所有的新闻样本转化为词索引序列。所谓词索引就是为每一个词依次分配一个整数 ID。遍历所有的新闻文本,我们只保留最参见的 20,000 个词,而且每个新闻文本最多保留 1000 个词。
  • 生成一个词向量矩阵。第 i 列表示词索引为 i 的词的词向量。
  • 将词向量矩阵载入 Keras Embedding 层,设置该层的权重不可再训练(也就是说在之后的网络训练过程中,词向量不再改变)。
  • Keras Embedding 层之后连接一个 1D 的卷积层,并用一个 softmax 全连接输出新闻类别。

5. 数据预处理

我们首先遍历下语料文件下的所有文件夹,获得不同类别的新闻以及对应的类别标签,代码如下所示
texts = []  # list of text samples
labels_index = {}  # dictionary mapping label name to numeric id
labels = []  # list of label ids

for name in sorted(os.listdir(TEXT_DATA_DIR)):
    path = os.path.join(TEXT_DATA_DIR, name)

    if os.path.isdir(path):
        label_id = len(labels_index)
        labels_index[name] = label_id

        for fname in sorted(os.listdir(path)):
            if fname.isdigit():
                fpath = os.path.join(path, fname)

                if sys.version_info < (3,):
                    f = open(fpath)
                else:
                    f = open(fpath, encoding='latin-1')
                t = f.read()
                i = t.find('\n\n')  # skip header

                if 0 < i:
                    t = t[i:]
                texts.append(t)

                f.close()
                labels.append(label_id)

print('Found %s texts.' % len(texts))
之 后, 我 们 可 以 新 闻 样 本 转 化 为 神 经 网 络 训 练 所 用 的 张 量。 所 用 到 的 Keras 库 是keras.preprocessing.text.Tokenizer keras.preprocessing.sequence.pad_sequences 。代码如下所示:
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences

tokenizer = Tokenizer(nb_words=MAX_NB_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=MAX_SEQUENCE_LENGTH)

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

# split the data into a training set and a validation set
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]
nb_validation_samples = int(VALIDATION_SPLIT * data.shape[0])

x_train = data[:-nb_validation_samples]
y_train = labels[:-nb_validation_samples]
x_val = data[-nb_validation_samples:]
y_val = labels[-nb_validation_samples:]

6. Embedding layer 设置

接下来,我们从 GloVe 文件中解析出每个词和它所对应的词向量,并用字典的方式存储。
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))
此时,我们可以根据得到的字典生成上文所定义的词向量矩阵。
embedding_matrix = np.zeros((len(word_index) + 1, EMBEDDING_DIM))

for word, i in word_index.items():
    embedding_vector = embeddings_index.get(word)

    if embedding_vector is not None:
        # words not found in embedding index will be all-zeros.
        embedding_matrix[i] = embedding_vector
现在我们将这个词向量矩阵加载到 Embedding 层中,注意,我们设置 trainable=False 使得这个编码层不可再训练。
from keras.layers import Embedding

embedding_layer = Embedding(len(word_index) + 1,
                            EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            input_length=MAX_SEQUENCE_LENGTH,
                            trainable=False)
一个 Embedding 层的输入应该是一系列的整数序列,比如一个 2D 的输入,它的 shape 值为 (samples, indices) ,也就是一个 samples 行, indeces 列的矩阵。每一次的 batch 训练的输入应该被 padded 成相同大小(尽管 Embedding 层有能力处理不定长序列,如果你不指定数列长度这一参数) dim). 所有的序列中的整数都将被对应的词向量矩阵中对应的列(也就是它的词向量)代替 , 比如序列 [1,2] 将被序列 [ 词向量 [1], 词向量 [2]] 代替。这样,输入一个 2D 张量后,我们可以得到一个 3D 张量。

7. 训练 1D 卷积

最后,我们可以使用一个小型的 1D 卷积解决这个新闻分类问题。
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
x = Conv1D(128, 5, activation='relu')(embedded_sequences)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(35)(x)  # global max pooling
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
preds = Dense(len(labels_index), activation='softmax')(x)

model = Model(sequence_input, preds)
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])

# happy learning!
model.fit(x_train, y_train, validation_data=(x_val, y_val),
          epochs=2, batch_size=128)
在两次迭代之后,这个模型最后可以达到 0.95 的分类准确率( 4:1 分割训练和测试集合)。你可以利用正则方法(例如 dropout )或在 Embedding 层上进行 fine-tuning 获得更高的准确率。
 
我们可以做一个对比实验,直接使用 Keras 自带的 Embedding 层训练词向量而不用 GloVe 向量。代码如下所示:
embedding_layer = Embedding(len(word_index) + 1,
                            EMBEDDING_DIM,
                            input_length=MAX_SEQUENCE_LENGTH)
两次迭代之后,我们可以得到 0.9 的准确率。所以使用预训练的词向量作为特征是非常有效的。一般来说,在自然语言处理任务中,当样本数量非常少时,使用预训练的词向量是可行的(实际上,预训练的词向量引入了外部语义信息,往往对模型很有帮助)。
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Sure! Here's an example code for building a textCNN model for text binary classification using pre-trained word2vec embeddings in the Keras framework: ```python from keras.layers import Conv1D, MaxPooling1D, Embedding, Flatten, Dense from keras.models import Sequential from gensim.models import KeyedVectors # Load pre-trained word2vec embeddings word2vec_path = 'path/to/word2vec.bin' word_vectors = KeyedVectors.load_word2vec_format(word2vec_path, binary=True) # Set the embedding dimension based on the pre-trained word2vec embeddings embedding_dim = word_vectors.vector_size # Define the model architecture model = Sequential() # Embedding layer with pre-trained word2vec weights model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim, weights=[word_vectors.vectors], trainable=False, input_length=max_sequence_length)) # Convolutional layers with different filter sizes filters = [3, 4, 5] conv_blocks = [] for filter_size in filters: conv = Conv1D(filters=128, kernel_size=filter_size, activation='relu')(model.layers[-1].output) pool = MaxPooling1D(pool_size=2)(conv) flatten = Flatten()(pool) conv_blocks.append(flatten) # Concatenate the outputs of the conv blocks concat = concatenate(conv_blocks, axis=-1) # Fully connected layer model.add(Dense(128, activation='relu')) model.add(Dense(1, activation='sigmoid')) # Compile the model model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # Print the model summary model.summary() ``` Make sure to replace `vocab_size`, `word2vec_path`, and `max_sequence_length` with appropriate values for your dataset and provide the correct path to your pre-trained word2vec embeddings file.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值