Keras:ValueError:检查模型输入时出错:传递给模型的Numpy数组列表不是模型预期的大小



我只是在制作网络时遇到了问题,我无法获得正确的输入,张量的形状与我在项目中需要的形状相同。但我总是犯这个错误。ValueError:检查模型输入时出错:传递给模型的Numpy数组列表的大小不是模型所期望的大小。对于输入['input_1','input_2'],应看到2个数组,但却得到了以下1个数组的列表:[array([[[[[0,0,0],[0,0,0],[0,0,0]],

[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]],

这是我的代码

x1_train = [[[[0, 0, 0],[0, 0, 0],[0, 0, 0]],[[0, 0, 0],[0, 0, 0],[0, 0, 0]],[[0, 0, 0],[0, 0, 0],[0, 0, 0]]],[[[0, 0, 0],[0, 0, 0],[0, 0, 0]],[[0, 0, 0],[0, 0, 0],[0, 0, 0]],[[0, 0, 0],[0, 0, 0],[0, 0, 0]]]]
y_train = [[0.3]]
# define two sets of inputs
inputA = tf.keras.Input(shape=(3,3,3))
inputB = tf.keras.Input(shape=(3,3,3))
# the first branch operates on the first input
x = tf.keras.layers.Dense(8, activation="relu")(inputA)
x = tf.keras.layers.Dense(4, activation="relu")(x)
x = tf.keras.Model(inputs=inputA, outputs=x)
# the second branch opreates on the second input
y = tf.keras.layers.Dense(64, activation="relu")(inputB)
y = tf.keras.layers.Dense(32, activation="relu")(y)
y = tf.keras.layers.Dense(4, activation="relu")(y)
y = tf.keras.Model(inputs=inputB, outputs=y)
# combine the output of the two branches
combined = tf.keras.layers.concatenate([x.output, y.output])
# apply a FC layer and then a regression prediction on the
# combined outputs
z = tf.keras.layers.Dense(2, activation="relu")(combined)
z = tf.keras.layers.Dense(1, activation="sigmoid")(z)
# our model will accept the inputs of the two branches and
# then output a single value
model = tf.keras.models.Model(inputs=[x.input, y.input], outputs=z)
model.compile(optimizer='adam',
loss='mean_absolute_percentage_error', #mean_absolute_percentage_error
metrics=['accuracy'])
model.fit(x=[x_train, x1_train], y=y_train, epochs = 1)````

I get the error message 
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 2 array(s), for inputs ['input_1', 'input_2'] but instead got the following list of 1 arrays:

您的输入数组必须是NumPy数组,而不是列表。所以你可以有:

import numpy as np
x1_train = np.array([
[[[0, 0, 0],[0, 0, 0],[0, 0, 0]],
[[0, 0, 0],[0, 0, 0],[0, 0, 0]],
[[0, 0, 0],[0, 0, 0],[0, 0, 0]]],
[[[0, 0, 0],[0, 0, 0],[0, 0, 0]],
[[0, 0, 0],[0, 0, 0],[0, 0, 0]],
[[0, 0, 0],[0, 0, 0],[0, 0, 0]]]])
x_train = x1_train
y_train = np.array([[0.3], [0.3]])

那么你就不会再收到那个错误了。然而,训练仍然失败,因为y_train(num_examples x 1(中标签的形状与模型输出(num_examples x 3 x 3 x 1(的形状不匹配,但这是另一个问题。

最新更新