拟合具有多个输入的模型

时间:2020-12-27 08:10:52

标签: tensorflow keras training-data

我的代码也有类似的问题,我写了一个简化版本:

我的模型有两个输入,但无论我如何将数据发送到 fit,它都不起作用。 这是问题的一个简短示例:

input1 = Input(shape=(1,), dtype='int64')
input2 = Input(shape=(1,), dtype='int64')

embeding1 = Embedding(1, 5, input_length=1, embeddings_regularizer=l2(1e-4) )(input1)
embeding2 = Embedding(1, 5, input_length=1, embeddings_regularizer=l2(1e-4) )(input2)



x = concatenate([embeding1, embeding2])
x = Flatten()(x)
x = Dense(1, activation='relu')(x)

model = Model([input1, input2],x)
model.compile(loss='mse', metrics=['accuracy'], optimizer='adam')

但我不知道如何跑步,我试过:

history = model.fit(
     [[1,1], [1,1], [1,1]],
     [1,1,1]
     )

考虑到可能样本中的每个项目都需要有一个包含其两个属性的列表, 但后来我收到了错误

ValueError: Layer model_3 expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 2) dtype=int64>]

我也试过

history = model.fit(
     [[1,1,1], [1,1,1]],
     [1,1,1]
     )

想也许我需要每个功能的两个列表。 错误:

ValueError: Data cardinality is ambiguous:
  x sizes: 2
  y sizes: 3
Make sure all arrays contain the same number of samples.

我还尝试使用 []()np.array 切换为 np.stack 但我试过的都没有效果...

1 个答案:

答案 0 :(得分:1)

每个输入和输出都应该具有 (batch_size, 1) 的形状。所以这是有效的(批量大小为 32):

input_1 = np.zeros((32, 1))
input_2 = np.zeros((32, 1))
outs = np.ones((32, 1))
history = model.fit([input_1, input_2], outs)