我打算进行以下设计:


machine-learning - 与Keras的层级连接-LMLPHP


但是我的代码似乎不起作用:


import numpy as np
from keras.models import Model
from keras.layers import Dense, Input, Concatenate
from keras import optimizers

trainX1 = np.array([[1,2],[3,4],[5,6],[7,8]])   # fake training data
trainY1 = np.array([[1],[2],[3],[4]])           # fake label

trainX2 = np.array([[2,3],[4,5],[6,7]])
trainY2 = np.array([[1],[2],[3]])

trainX3 = np.array([[0,1],[2,3]])
trainY3 = np.array([[1],[2]])

numFeatures = 2
trainXList = [trainX1, trainX2, trainX3]
trainYStack = np.vstack((trainY1,trainY2,trainY3))
inputList = []
modelList = []
for i,_ in enumerate(trainXList):
    tempInput= Input(shape = (numFeatures,))
    m = Dense(10, activation='tanh')(tempInput)
    inputList.append(tempInput)
    modelList.append(m)

mAll = Concatenate()(modelList)
out = Dense(1, activation='tanh')(mAll)
model = Model(inputs=inputList, outputs=out)

rmsp = optimizers.rmsprop(lr=0.00001)
model.compile(optimizer=rmsp,loss='mse', dropout = 0.1)
model.fit(trainXList, trainYStack, epochs = 1, verbose=0)


错误消息说我的输入数据集的形状不一样,但是在填充训练集以使所有3个样本集的样本数均等于4之后,我仍然会收到错误消息,指出维数不正确。我可以知道如何正确设计此网络吗?谢谢!



ps。这是填充前的错误消息:

ValueError: All input arrays (x) should have the same number of samples. Got array shapes: [(4, 2), (3, 2), (2, 2)]


这是填充后的错误消息(发生在代码的最后一行):

ValueError: Input arrays should have the same number of samples as target arrays. Found 4 input samples and 12 target samples.

最佳答案

输入形状对于给定的输入错误。
您为输入分配了numFeatures的大小,但实际上您拥有二维数组,并且它们是不同的(4,2)(3,2)(2,2)。我不确定您的问题,但样本数量和功能数量似乎相反。

tempInput= Input(shape = (numFeatures,))


此外,您的y也很奇怪。通常,您有X(样本数量,数量特征)和y(样本数量,标签)。

使用model.summary()查看您的网络外观。

关于machine-learning - 与Keras的层级连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51884171/

10-12 18:16