Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:检查模型目标时出错:传递给模型的Numpy数组列表的大小不是模型预期的大小_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

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

Python ValueError:检查模型目标时出错:传递给模型的Numpy数组列表的大小不是模型预期的大小,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我有多重输出 out = [Dense(19, name='one', activation='softmax')(out), Dense(19, name='two', activation='softmax')(out), Dense(19, name='three', activation='softmax')(out), Dense(19, name='four', activation='softmax')(out)]

我有多重输出

out = [Dense(19, name='one', activation='softmax')(out),
           Dense(19, name='two', activation='softmax')(out),
           Dense(19, name='three', activation='softmax')(out),
           Dense(19, name='four', activation='softmax')(out)]


model.fit(reshape_train_X,  y_onehot, batch_size=400, epochs=100, verbose=2,
          validation_split=0.2, callbacks=callbacks_list)
这是我的y_onehot格式:

[array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]],
      dtype=uint8), array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 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, 1, 0, 0, 0]],dtype=uint8),.....]
我收到了这个错误信息

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 4 array(s), but instead got the following list of 5000 arrays: [array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 ...
我不知道为什么当y_onehot在数组中有四个列表时会发生此错误

len(YuOnehot):5000

打印(“y_onehot”,y_onehot[0])

打印(“y_onehot”,len(y_onehot[0]))

我试试看。但还是不起作用


谢谢你的帮助。

这是一个虚构的例子。注意你的眼睛。您必须将每个分开的输出进行拟合

inp = Input((50))
x = Dense(32)(inp)
x1 = Dense(19, name='one', activation='softmax')(x)
x2 = Dense(19, name='two', activation='softmax')(x)
x3 = Dense(19, name='three', activation='softmax')(x)
x4 = Dense(19, name='four', activation='softmax')(x)

model = Model(inp, [x1,x2,x3,x4])
model.compile('adam', 'categorical_crossentropy')

X = np.random.uniform(0,1, (5000,50))
y1 = np.random.randint(0,2, (5000,19))
y2 = np.random.randint(0,2, (5000,19))
y3 = np.random.randint(0,2, (5000,19))
y4 = np.random.randint(0,2, (5000,19))

model.fit(X, [y1,y2,y3,y4], epochs=10)

您是否尝试将
y\u onehot
传递到
model.fit
作为
np.array(y\u onehot)
?是的,但仍然收到此错误消息。这很有效!通过拟合每个分离的输出都是正确的!非常感谢你。
y_onehot 4
inp = Input((50))
x = Dense(32)(inp)
x1 = Dense(19, name='one', activation='softmax')(x)
x2 = Dense(19, name='two', activation='softmax')(x)
x3 = Dense(19, name='three', activation='softmax')(x)
x4 = Dense(19, name='four', activation='softmax')(x)

model = Model(inp, [x1,x2,x3,x4])
model.compile('adam', 'categorical_crossentropy')

X = np.random.uniform(0,1, (5000,50))
y1 = np.random.randint(0,2, (5000,19))
y2 = np.random.randint(0,2, (5000,19))
y3 = np.random.randint(0,2, (5000,19))
y4 = np.random.randint(0,2, (5000,19))

model.fit(X, [y1,y2,y3,y4], epochs=10)