我想向神经网络输入两个数据集。第一个数据集(元素)具有固定的形状(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)。
请注意,正如这个线程中的其他人所指出的,这种方法似乎是不正确的做法。