【问题标题】:The dataset seems to be missing the batch dimension - AKA how to construct a dataset with mutiple outputs数据集似乎缺少批处理维度 - AKA 如何构建具有多个输出的数据集
【发布时间】:2021-12-28 20:55:48
【问题描述】:

我很难理解问题所在。考虑以下模型:

Model: "model_8"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input (InputLayer)             [(None, 15)]         0           []                               
                                                                                                  
 dense_1 (Dense)                (None, 128)          2048        ['input[0][0]']                  
                                                                                                  
 dense_2 (Dense)                (None, 1024)         132096      ['dense_1[0][0]']                
                                                                                                  
 dense_3 (Dense)                (None, 5120)         5248000     ['dense_2[0][0]']                
                                                                                                  
 a_out (Dense)                  (None, 17)           87057       ['dense_3[0][0]']                
                                                                                                  
 b_out (Dense)                  (None, 27)           138267      ['dense_3[0][0]']                
                                                                                                  
 c_out (Dense)                  (None, 71)           363591      ['dense_3[0][0]']                
                                                                                                  
 d_out (Dense)                  (None, 29)           148509      ['dense_3[0][0]']                
                                                                                                  
==================================================================================================
Total params: 6,119,568
Trainable params: 6,119,568
Non-trainable params: 0

这是一个相当简单的模型,具有一个输入和 4 个输出(a_outb_outc_outd_out)。我试图通过提供一些数据集来拟合模型:

dataset = tf.data.Dataset.from_tensor_slices((inputs, {'a_out': targets[:, 0],
                                                       'b_out': targets[:, 1],
                                                       'c_out': targets[:, 2],
                                                       'd_out': targets[:, 3]}))

inputstargets 是两个具有形状的 numpy 数组:分别为 (525081, 15)(525081, 4)。当我运行 fit 方法时:

model.fit(dataset, epochs=10, batch_size=128)

我收到以下错误:

ValueError: Exception encountered when calling layer "model_8" (type Functional).
    
    Input 0 of layer "dense_1" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (15,)
    
    Call arguments received:
      • inputs=tf.Tensor(shape=(15,), dtype=float64)
      • training=True
      • mask=None

在我看来,发送到layer_1 的张量缺少批量维度,这对我来说没有意义。我是不是在构建我的数据集错误?

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    使用tf.data 时,model.fit 中的batch_size 参数将被忽略。

    应使用tf.data.Dataset.batch() 方法进行批处理。

    在你的情况下应该是dataset.batch(batch_size)

    【讨论】:

    • 谢谢。我完全错过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多