回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我训练了一个Keras模型,但是,我很难预测它。我的输入数组的形状是<code>(400,2)</code>,输出数组的形状是<code>(400,1)</code>。现在,当我将参数<code>array([1,2])</code>传递给<code>model.predict()</code>函数时,得到以下错误:</p>
<pre><code> ValueError: Error when checking input: expected dense_1_input to have shape (2,) but got array with shape (1,).
</code></pre>
<p><strong>这是毫无意义的,因为<code>shape(array([1,2])) = (2,)</code>,因此<code>model.predict</code>函数应该接受它作为有效输入。</strong></p>
<p>相反,当我传递一个形状为<code>(1,2)</code>的数组时,它的效果很好。那么Keras实现中有没有bug呢?你知道吗</p>
<p>我的模型如下:</p>
<pre><code>from keras.models import Sequential
from keras.layers import Dense
from keras import optimizers
import numpy as np
data = np.random.rand(400,2)
Y = np.random.rand(400,1)
def base():
model = Sequential()
model.add(Dense(4,activation = 'tanh', input_dim = 2))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer = optimizers.RMSprop(lr = 0.1), loss = 'binary_crossentropy', metrics= ['accuracy'])
return model
model = base()
model.fit(data,Y, epochs = 10, batch_size =1)
model.predict(np.array([1,2]).reshape(2,1)) #Error
model.predict(np.array([1,2]).reshape(2,)) #Error
model.predict(np.array([1,2]).reshape(1,2)) #Works
</code></pre>