【发布时间】:2019-10-29 14:06:21
【问题描述】:
我正在尝试在 Keras 中创建一个基本的神经网络模型,该模型将学习将正数相加,但在塑造训练数据以适应模型时遇到了麻烦:
我已经为第一个 Dense 层的“input_shape”属性尝试了许多配置,但似乎没有任何效果。
# training data
training = np.array([[2,3],[4,5],[3,8],[2,9],[11,4],[13,5],[2,9]], dtype=float)
answers = np.array([5, 9, 11, 11, 15, 18, 11], dtype=float)
# create our model now:
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=16, input_shape=(2,)),
tf.keras.layers.Dense(units=16, activation=tf.nn.relu),
tf.keras.layers.Dense(units=1)
])
# set up the compile parameters:
model.compile(loss='mean_squared_error',
optimizer=tf.keras.optimizers.Adam(.1))
#fit the model:
model.fit(training, answers, epochs=550, verbose=False)
print(model.predict([[7,9]]))
我希望它可以正常运行并产生结果“16”,但我收到以下错误:
"Traceback (most recent call last):
File "c2f", line 27, in <module>
print(model.predict([[7,9]]))
File "C:\Users\Aalok\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1096, in predict
x, check_steps=True, steps_name='steps', steps=steps)
File "C:\Users\Aalok\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2382, in _standardize_user_data
exception_prefix='input')
File "C:\Users\Aalok\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 362, in standardize_input_data
' but got array with shape ' + str(data_shape))
ValueError: 检查输入时出错:预期dense_input 的形状为(2,),但得到的数组的形状为(1,)
【问题讨论】:
-
我已经提供了完整的错误信息,但我相信主要关注的应该是错误“ValueError: Error...”的最后一行
-
training.shape的输出是什么? -
@ShubhamPnchal 它是 (7,2) 是有道理的。但是,在输入 input_shape 的值时,您不应该概括“row_size”,而只输入 column_size?
标签: python numpy tensorflow keras reshape