如何用两个不同大小的输入来喂养神经网络



我想用两个输入为神经网络提供。第一个数据集(元素(将具有(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)]

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

简短的答案是,您所有输入(和输出(的批次尺寸必须相同。但是,没有什么可以阻止您对批处理大小的每个条目重复(20,1(数据集,从而导致(n,20,1(的形状。

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

最新更新