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

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

我知道如何构建具有相同行数的多个输入的模型,我的当前方法如下:

# define two sets of inputsinputA = Input(shape=(1,))inputB = Input(shape=(elements.shape[0],))# the first branch operates on the first inputx = Dense(100, activation="relu")(inputA)x = Dense(50, activation="relu")(x)x = Model(inputs=inputA, outputs=x)# the second branch opreates on the second inputy = 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 branchescombined = concatenate([x.output, y.output])# apply a FC layer and then a regression prediction on the# combined outputsz = 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 valuemodel = Model(inputs=[x.input, y.input], outputs=z)model.compile(loss="mean_squared_error", optimizer=Adam())# train the modelprint("[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)]

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


回答:

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

请注意,正如这个线程中的其他人所指出的,这种方法似乎是不正确的做法。

Related Posts

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

在sklearn和pandas中将字符串特征转换为数值特征

我目前正在使用sklearn(我还是个初学者),我想训…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注