【问题标题】:fiting a model with multiple inputs拟合具有多个输入的模型
【发布时间】:2020-12-27 08:10:52
【问题描述】:

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

我的模型有两个输入,但是无论我如何将数据发送到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.arraynp.stack 但我试过的都没有用...

【问题讨论】:

    标签: tensorflow keras training-data


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

      为了匹配“样本数”,我​​尝试了许多输入形状,但只有将 tensorflow 版本从 2.7.0 切换到 2.0.0 才解决了我的问题。我猜这是上个版本的一些错误。

      【讨论】:

        猜你喜欢
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        • 2021-07-13
        • 1970-01-01
        • 2018-11-12
        • 2019-08-09
        • 2020-08-10
        相关资源
        最近更新 更多