如何使用两个大小不同的输入来馈入神经网络?

时间:2019-07-06 23:02:54

标签: python machine-learning keras neural-network deep-learning

我想为神经网络提供两个输入。 第一个数据集(元素)将具有固定的形状(20,1),这意味着在训练和测试阶段它将是相同的(永远不变)。它由1-100之间的值组成。 第二个输入数据集将由20个二元特征(列)和N个数据(形状:(N,20))组成,该数据集的每一行将指示元素数据集的哪些行被组合。 输出将具有(N,1)的形状,并且是在对它们应用特定功能之后,对相应元素进行组合的结果。

当两个数据集中的行数相同时,我知道如何构建具有多个输入的模型,到目前为止,我的方法是:

# define two sets of inputs
inputA = Input(shape=(1,))
inputB = Input(shape=(elements.shape[0],))

# the first branch operates on the first input
x = Dense(100, activation="relu")(inputA)
x = Dense(50, activation="relu")(x)
x = Model(inputs=inputA, outputs=x)

# the second branch opreates on the second input
y = Dense(100, activation="relu")(inputB)
y = Dense(100, activation="relu")(y)
y = Dense(50, activation="relu")(y)
y = Model(inputs=inputB, outputs=y)

# combine the output of the two branches
combined = concatenate([x.output, y.output])

# apply a FC layer and then a regression prediction on the
# combined outputs
z = Dense(50, activation="relu")(combined)
z = Dense(1, activation="linear")(z)

# our model will accept the inputs of the two branches and
# then output a single value
model = Model(inputs=[x.input, y.input], outputs=z)

model.compile(loss="mean_squared_error", optimizer=Adam())

# train the model
print("[INFO] training model...")
model.fit([elements, X_train], y_train, epochs=200, verbose=1)

但是,由于“元素”数据集是固定的,因此第一个输入的行与第二个输入的行不同。发生以下错误。

ValueError: All input arrays (x) should have the same number of samples. Got array shapes: [(20, 1), (33, 20)]

你知道我该如何克服这个问题?

1 个答案:

答案 0 :(得分:0)

简短的答案是所有输入(和输出)的batch_size必须相同。但是,没有什么能阻止您针对批次大小中的每个条目重复(20,1)数据集,从而形成(N,20,1)的形状。

请注意,在该线程上有其他人指出,这种方法似乎是错误的事情。