简体   繁体   English

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

[英]How to feed a Neural Network with two inputs of different sizes?

I want to feed a Neural Network with two inputs.我想用两个输入输入神经网络。 The first dataset (elements) will have a fixed shape of (20, 1), which means that it will be the same for both the training and the test phase (it will never change).第一个数据集(元素)将具有 (20, 1) 的固定形状,这意味着它在训练和测试阶段都相同(永远不会改变)。 It consists of values between 1-100.它由 1-100 之间的值组成。 The second input dataset will consist of 20 binary features (columns) and N data (shape: (N,20)), and each row of this dataset will indicate which rows of the elements dataset were combined.第二个输入数据集将包含 20 个二元特征(列)和 N 个数据(形状:(N,20)),该数据集的每一行将指示元素数据集的哪些行被组合。 The output will have a shape of (N,1), and it will be the result of the corresponding elements' combinations, after applying a specific function to them.输出将具有 (N,1) 的形状,它将是相应元素组合的结果,在对它们应用特定函数后。

I know how to build a model with multiple inputs when we have the same number of rows in both datasets, and my approach so far is the following:当两个数据集中的行数相同时,我知道如何构建具有多个输入的模型,到目前为止我的方法如下:

# 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)

However, since the "elements" dataset is fixed, the rows of the 1st input are different from the rows of the 2nd input.但是,由于“元素”数据集是固定的,因此第一个输入的行与第二个输入的行不同。 The following error occurs.发生以下错误。

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

Do you know how could I overcome this problem?你知道我怎样才能克服这个问题吗?

The short answer is that the batch_size of all your inputs (and outputs) must be the same.简短的回答是所有输入(和输出)的 batch_size 必须相同。 But nothing prevents you from repeating your (20, 1) dataset for each entry in the batch size, thus resulting in a shape of (N, 20, 1).但是没有什么可以阻止您为批量大小中的每个条目重复您的 (20, 1) 数据集,从而导致形状为 (N, 20, 1)。

Please note that has the other people on this thread noted, this approach seems like the wrong thing to do.请注意,该线程上的其他人是否注意到,这种方法似乎是错误的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM