【发布时间】:2020-02-18 08:24:39
【问题描述】:
我尝试创建一个神经网络,有两个特定大小的输入(这里是四个),每个输入和一个相同大小的输出(所以也是四个)。不幸的是,我在运行我的代码时总是遇到这个错误:
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), but instead got the following list of 1 arrays:
[array([[[-1.07920336, 1.16782929, 1.40131554, -0.30052492],
[-0.50067655, 0.54517916, -0.87033621, -0.22922157]],
[[-0.53766128, -0.03527806, -0.14637072, 2.32319071],
[ 0...
我认为,问题在于,一旦我将数据传递给训练,输入形状要么不正确,要么我有数据类型问题。因此,数组周围有一个额外的列表括号。
我使用的是 Tensorflow 1.9.0(由于项目限制)。我已经检查了搜索功能并尝试了here 提供的解决方案。以下是重现我的错误的示例代码:
import numpy as np
import tensorflow as tf
from tensorflow import keras
import keras.backend as K
from tensorflow.keras import layers, models
def main():
ip1 = keras.layers.Input(shape=(4,))
ip2 = keras.layers.Input(shape=(4,))
dense = layers.Dense(3, activation='sigmoid', input_dim=4) # Passing the value in a weighted manner
merge_layer = layers.Concatenate()([ip1, ip2]) # Concatenating the outputs of the first network
y = layers.Dense(6, activation='sigmoid')(merge_layer) # Three fully connected layers
y = layers.Dense(4, activation='sigmoid')(y)
model = keras.Model(inputs=[ip1, ip2], outputs=y)
model.compile(optimizer='adam',
loss='mean_squared_error')
model.summary()
# dataset shape: 800 samples, 2 inputs for sequential model, 4 input size
X_train = np.random.randn(800, 2, 4)
y_train = np.random.randn(800, 4)
X_test = np.random.randn(200, 2, 4)
y_test = np.random.randn(200, 4)
history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=1000, batch_size=32)
if __name__ == '__main__':
main()
【问题讨论】:
-
传递 [(200,4),(200,4)] 的列表而不是 (200,2,4) 的单个数组
-
感谢您的快速回答。将
[X_train[:,0], X_train[:,1]]传递给 fit 函数时,我得到了同样的错误。同样,当我创建两个形状为(800, 4)的新变量并将它们传递到列表中时。所以不幸的是,没有帮助。 -
你是否也对验证集进行了同样的更改?
-
非常感谢!这确实解决了问题。真傻,没想到!
标签: python tensorflow keras multiple-input