【问题标题】:How to feed a Neural Network with two inputs of different sizes?如何为神经网络提供两个不同大小的输入?
【发布时间】:2019-07-06 23:02:54
【问题描述】:

我想为神经网络提供两个输入。 第一个数据集(元素)将具有 (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 值,这意味着使用了元素数据集中的前两行的值,并且它们的几何平均值被计算为输出.基本上,第二个输入数据集包含 one-hot 编码。
  • 我不确定这种方法是否正确。您是否考虑过将数据嵌入到第一个输入中以获取特征?然后对于第二个数据集的每一行,您可以在投入分类器之前对第一个数据集(或使用 LSTM)中参与行的特征求和。
  • 我尝试了不同的方法,但我被要求尝试这个特定的方法。我的问题是找到一种方法(如果有的话)让 2 个输入具有不同的行数
  • 还有一件事。我真的不知道为什么这个问题被否决了。请在您这样做之前,说明原因。这是一种非常糟糕的行为。

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


【解决方案1】:

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

请注意,此线程上的其他人已注意到,这种方法似乎是错误的做法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2017-04-24
    • 2021-04-05
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多