繁体   English   中英

Keras: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

[英]Keras: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

我只是在制作网络时遇到了麻烦,我无法获得输入,张量的形状与我在项目中需要的形状相同。 但我不断收到这个错误。 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. 预计会看到 2 个数组,用于输入 ['input_1', 'input_2'] 但得到以下 1 个 arrays 列表:[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:

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

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 ) 中的标签形状与 model output ( num_examples x 3 x 3 x 1 ) 的形状不匹配,但这是一个不同的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM