输入元素具有3行,每行具有199列,而输出具有46行和1列
Input.shape, output.shape
((204563, 3, 199), (204563, 46, 1))
给定输入后,将引发以下错误:
from keras.layers import Dense
from keras.models import Sequential
from keras.layers.recurrent import SimpleRNN
model = Sequential()
model.add(SimpleRNN(100, input_shape = (Input.shape[1], Input.shape[2])))
model.add(Dense(output.shape[1], activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(Input, output, epochs = 20, batch_size = 200)
引发错误:
Epoch 1/20
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-134-378dd431cf45> in <module>()
3 model.add(Dense(y_target.shape[1], activation = 'softmax'))
4 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
----> 5 model.fit(X_input, y_target, epochs = 20, batch_size = 200)
.
.
.
ValueError: Error when checking model target: expected dense_6 to have 2 dimensions, but got array with shape (204563, 46, 1)
请说明问题的原因和可能的后果
最佳答案
问题是SimpleRNN(100)
返回形状为(204563, 100)
的张量,因此Dense(46)
(因为output.shape[1]=46
)将返回形状为(204563, 46)
的张量,但是您的y_target
具有形状为(204563, 46, 1)
。您需要使用例如y_target = np.squeeze(y_target)
删除最后一个尺寸,以便尺寸一致
关于python-3.x - keras多维输入到simpleRNN:尺寸不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45296500/