【问题标题】:Keras: Embedding in LSTMKeras:嵌入 LSTM
【发布时间】:2017-06-26 11:57:26
【问题描述】:

在 LSTM 上用于建模 IMDB 序列数据 (https://github.com/fchollet/keras/blob/master/examples/imdb_lstm.py) 的 keras 示例中,在输入到 LSTM 层之前有一个嵌入层:

model.add(Embedding(max_features,128)) #max_features=20000
model.add(LSTM(128))

嵌入层的真正作用是什么?在这种情况下,这是否意味着 LSTM 层的输入序列长度为 128?如果是这样,我可以将 LSTM 层写成:

model.add(LSTM(128,input_shape=(128,1))

但也注意到输入X_train已经过pad_sequences处理:

print('Pad sequences (samples x time)')
X_train = sequence.pad_sequences(X_train, maxlen=maxlen) #maxlen=80
X_test = sequence.pad_sequences(X_test, maxlen=maxlen) #maxlen=80

输入序列长度好像是80?

【问题讨论】:

    标签: keras lstm embedding


    【解决方案1】:

    To quote the documentation:

    将正整数(索引)转换为固定大小的密集向量。 例如。 [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]

    基本上,这会将索引(表示您的 IMDB 评论包含哪些单词)转换为具有给定大小的向量(在您的情况下为 128)。

    如果你不知道嵌入通常是什么,here is the wikipedia definition

    词嵌入是一组语言建模的统称 自然语言处理 (NLP) 中的特征学习技术 其中词汇表中的单词或短语被映射到 相对于词汇表的低维空间中的实数 大小(“连续空间”)。

    回到您提出的另一个问题:

    在这种情况下,这是否意味着输入序列的长度 LSTM层是128?

    不完全是。对于循环网络,您将有一个时间维度和一个特征维度。 128 是你的特征维度,就像每个嵌入向量应该有多少维度一样。您示例中的时间维度是存储在maxlen 中的内容,用于生成训练序列。

    无论您以 128 的形式向 LSTM layer is the actual number of output units of the LSTM 提供什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多