【发布时间】:2019-09-20 12:25:47
【问题描述】:
我正在训练一个回归模型,该模型近似于方程的权重: Y = R+B+G 为此,我提供了 R、B 和 G 和 Y 的预定值作为训练数据。
R = np.array([-4, -10, -2, 8, 5, 22, 3], dtype=float)
B = np.array([4, -10, 0, 0, 15, 5, 1], dtype=float)
G = np.array([0, 10, 5, 8, 1, 2, 38], dtype=float)
Y = np.array([0, -10, 3, 16, 21, 29, 42], dtype=float)
训练批次由 1x3 数组组成,对应于 R、B 和 G 的第 I 个值。
RBG = np.array([R,B,G]).transpose()
print(RBG)
[[ -4. 4. 0.]
[-10. -10. 10.]
[ -2. 0. 5.]
[ 8. 0. 8.]
[ 5. 15. 1.]
[ 22. 5. 2.]
[ 3. 1. 38.]]
我使用的神经网络有 3 个输入,1 个密集层(隐藏层)有 2 个神经元,输出层(输出)有一个神经元。
hidden = tf.keras.layers.Dense(units=2, input_shape=[3])
output = tf.keras.layers.Dense(units=1)
此外,我训练了模型
model = tf.keras.Sequential([hidden, output])
model.compile(loss='mean_squared_error',
optimizer=tf.keras.optimizers.Adam(0.1))
history = model.fit(RBG,Y, epochs=500, verbose=False)
print("Finished training the model")
loss vs epoch plot 正常,先递减,然后持平。
但是当我测试模型时,使用 R、B 和 G 的随机值作为
print(model.predict([[1],[1],[1]]))
期望输出为 1+1+1 = 3,但得到值错误:
ValueError: Error when checking input: expected dense_2_input to have shape (3,) but got array with shape (1,)
知道我可能在哪里出错了吗?
令人惊讶的是,它响应的唯一输入是训练数据本身。即,
print(model.predict(RBG))
[[ 2.1606684e-07]
[-3.0000000e+01]
[-3.2782555e-07]
[ 2.4000002e+01]
[ 4.4999996e+01]
[ 2.9000000e+01]
[ 4.2000000e+01]]
【问题讨论】:
标签: python-3.x tensorflow linear-regression