ValueError: 检查模型目标时出错:您传递给模型的Numpy数组列表大小与模型期望的不符

我有多个输出

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(y_onehot): 5000

print(“y_onehot”, y_onehot[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 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]]

print(“y_onehot”, len(y_onehot[0]))

y_onehot 4

我尝试了这个。但仍然没有效果。

感谢您的帮助。


回答:

这是一个示例。请注意您的y。您必须在fit中分别传递每个输出

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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注