我正在尝试使用Keras使用LSTM进行一些香草模式识别来预测序列中的下一个元素.
我的数据如下所示:
其中训练序列的标签是列表中的最后一个元素:X_train['Sequence'][n][-1].
因为我的Sequence列在序列中可以有可变数量的元素,所以我认为RNN是最好的模型.以下是我在Keras建立LSTM的尝试:
# Build the model
# A few arbitrary constants...
max_features = 20000
out_size = 128
# The max length should be the length of the longest sequence (minus one to account for the label)
max_length = X_train['Sequence'].apply(len).max() - 1
# Normal LSTM model construction with sigmoid activation
model = Sequential()
model.add(Embedding(max_features, out_size, input_length=max_length, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Run Code Online (Sandbox Code Playgroud)
以下是我尝试训练模型的方法: …
variable-length python-3.x lstm keras recurrent-neural-network
我在用 Python进行深度深度学习的第 7 章第 1 节中找到了一段代码,如下所示?
from keras.models import Model
from keras import layers
from keras import Input
text_vocabulary_size = 10000
question_vocabulary_size = 10000
answer_vocabulary_size = 500
# Our text input is a variable-length sequence of integers.
# Note that we can optionally name our inputs!
text_input = Input(shape=(None,), dtype='int32', name='text')
# Which we embed into a sequence of vectors of size 64
embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input)
# Which we encoded in a single vector via …Run Code Online (Sandbox Code Playgroud) 在Embedding https://keras.io/layers/embeddings/的Keras文档中,给出的解释mask_zero是
mask_zero:输入值0是否是应该被屏蔽掉的特殊"填充"值.当使用可能需要可变长度输入的循环层时,这很有用.如果这是True,则模型中的所有后续层都需要支持屏蔽,否则将引发异常.如果mask_zero设置为True,那么索引0不能在词汇表中使用(input_dim应该等于|词汇| + 2).
为什么input_dim需要2 + +词汇表中的单词数量?假设0被屏蔽并且无法使用,它不应该只是1 +字数吗?另外一个额外条目是什么?