【问题标题】:Keras Input raises value error when dimensioons match当维度匹配时,Keras Input 会引发值错误
【发布时间】:2020-02-13 15:53:57
【问题描述】:

我开始开发模型,但被尺寸卡住了。我的 X_train 和 Y_train 是形状为 (65337, 19) 的 numpy 数组

Input_1= Input(shape=(19,))
x = Dense(100, activation='relu')(Input_1)

out1 = Dense(1,  activation='linear')(x)
out2 = Dense(1,  activation='linear')(x)
...
out19 = Dense(1, activation = 'linear')(x)
model = Model(inputs=Input_1, outputs=[out1,out2,out3,out4,out5,out6,
                                  out7,out8,out9,out10,out11,out12,
                                  out13,out14,out15,out16,out17,out18,out19])
model.compile(optimizer = "rmsprop", loss = 'mse')
model.fit(X_train,y_train,epochs=5)

当我运行这个时,我得到了值错误:

ValueError: 检查模型目标时出错:Numpy 数组列表 您传递给模型的大小不是模型预期的大小。 预计会看到 19 个数组,但得到了以下 1 个数组 数组:

在这里查看其他问题,似乎使用.fit(np.array(X_train) , np.array(y_train) 有所帮助,但我得到了同样的错误(这是有道理的,因为它告诉我我有一个数组)。

【问题讨论】:

    标签: python keras


    【解决方案1】:

    您期望 19 个不同的输出,因此您需要使用标签数组的 19 个切片向您的网络提供数据:

    model.fit(X_train,[y_train[:,0], y_train[:,1], y_train[:,2],[...], y_train[:, 18]] , epochs=5)
    

    【讨论】:

    • 这引发了 ValueError: Error when checking target: expected dense_316 to have shape (1,) but got array with shape (19,) 但我应该只使用切片吗?像 [y_train[:,1],....y_train[:,19])
    • 使用切片消除了错误。感谢您的帮助(如果您编辑答案,我会接受)
    猜你喜欢
    • 1970-01-01
    • 2018-06-17
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多