我已经成功实现了LSTM教程来生成音乐。但是,我正在努力为语言创建一个(主要兴趣)。我有一个单词索引,这是我的数据中的两个示例句子。
样本预测变量:
[[1],
[1, 6],
[1, 6, 241],
[1, 6, 241, 252],
[1, 6, 241, 252, 11],
[1, 6, 241, 252, 11, 59],
[1, 6, 241, 252, 11, 59, 2],
[1, 6, 241, 252, 11, 59, 2, 62],
[1, 6, 241, 252, 11, 59, 2, 62, 663],
[1, 6, 241, 252, 11, 59, 2, 62, 663, 41],
[1],
[1, 3],
[1, 3, 216],
[1, 3, 216, 227],
[1, 3, 216, 227, 26],
[1, 3, 216, 227, 26, 30],
[1, 3, 216, 227, 26, 30, 5]]
样本标签:
[[6],
[241],
[252],
[11],
[59],
[2],
[62],
[663],
[41],
[1],
[3],
[216],
[227],
[26],
[30],
[5],
[1]]
我的LSTM代码是
from keras.models import Model
from keras import layers
from keras import Input
vocabulary_size = len(word_index)
dimensions = 200
text_input = Input(shape=(None,))
embedded = layers.Embedding(vocabulary_size, dimensions)(text_input)
encoded = layers.LSTM(vocabulary_size)(embedded)
output = layers.Dense(vocabulary_size, activation='softmax')(encoded)
model = Model(text_input, output)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['acc'])
model.summary()
model.fit(x, y, epochs=10, batch_size=1)
为了适应可变的句子长度,我设置了
batch_size = 1
,因为句子长度可变shape
到Input
的(None, )
但是,出现以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-64-95228d843a72> in <module>()
25 metrics=['acc'])
26 model.summary()
---> 27 model.fit(x, y, epochs=1, batch_size=1)
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
100 'Expected to see ' + str(len(names)) + ' array(s), '
101 'but instead got the following list of ' +
--> 102 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
103 elif len(names) > 1:
104 raise ValueError(
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 17 arrays: [array([[1]]), array([[1],
[6]]), array([[ 1],
[ 6],
[241]]), array([[ 1],
[ 6],
[241],
[252]]), array([[ 1],
[ 6],
[241],
[252],
...
我没有使用列表列表,而是尝试将它们转换为numpy数组列表,但这并没有改变错误。在这里建议: keras list of Numpy arrays not the size model expected
x = [np.array(i) for i in x]
y = [np.array(i) for i in y]
即使我故意构造模型以处理可变长度的数组,为什么仍会收到此错误?
由于我的预测变量(x)的格式,发生了错误。至少,我认为这是错误所指示的。
答案 0 :(得分:0)
我可能记错了,但是我相信你应该做类似
y = y.reshape(-1)
以获得Keras在这里想要的标签列表。同样,batch_size为1的训练也会影响您的表现。建议您使用固定数量的0
预先填充数据,在LSTM中设置mask=True
并使用标准的批量大小