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

我想向神经网络输入两个数据集。第一个数据集(元素)具有固定的形状(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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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