【问题标题】:Keras `Input` Layer is returning layer [(None, 32)] rather than (None, 32) in summaryKeras `Input` 层返回层 [(None, 32)] 而不是 (None, 32) 总结
【发布时间】:2020-11-15 07:30:20
【问题描述】:

Keras 2.4.3 版

我正在创建一个简单的 Image-Caption 模型,它有两个输入和一个输出。

模型定义代码如下:

# Image feat part
imginp = Input(shape=(512,))
imglay1 = Dropout(0.5)(imginp)
imglay2 = Dense(EMBED_SIZE, activation=act)(imglay1)
# LSTM Part
textinp = Input(shape=(39,))
textlay1 = Embedding(VOCAB_SIZE, EMBED_SIZE, mask_zero=True)(textinp) 
textlay2 = Dropout(0.5)(textlay1)
textlay3 = LSTM(EMBED_SIZE)(textlay2)
# # Decoder part that combines both
declay1 = Add()([imglay2, textlay3])
declay2 = Dense(EMBED_SIZE, activation=act)(declay1)
output = Dense(VOCAB_SIZE, activation="softmax")(declay2)
# Creating keras model
model = tf.keras.models.Model(inputs=[imginp,textinp],outputs=output)
model.summary()

然而,模型在model.fit() 上给出了一个错误,我注意到输入层给出了一个奇怪的输出,我认为这是导致错误的原因。 summary 的 sn-p 如下所示:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_82 (InputLayer)           [(None, 39)]         0                                            
__________________________________________________________________________________________________
input_81 (InputLayer)           [(None, 512)]        0                                            
__________________________________________________________________________________________________
embedding_31 (Embedding)        (None, 39, 300)      511800      input_82[0][0]                   
__________________________________________________________________________________________________
dropout_79 (Dropout)            (None, 512)          0           input_81[0][0]                   

正如您所见,输入层的输出形状需要(None, 512)(None, 39),但它们似乎是一个列表。因此,尽管我确实测试了 python 数据生成器,但我得到了 ValueError: no grad available for the variables。我相信这个Input layer api 会导致一些奇怪的错误。

有什么想法吗?

【问题讨论】:

    标签: python tensorflow keras keras-layer tf.keras


    【解决方案1】:

    我在 keras 2.4.0 中测试过。我有与 [(None, 32)] 相同的 model.summary()。

    但您的代码(keras 2.4.0)没有出现任何错误:

    import tensorflow as tf
    from tensorflow.keras.layers import Input, LSTM, Dense, Dropout, Embedding, Add
    from tensorflow.keras import Model, Sequential
    
    EMBED_SIZE = 512
    VOCAB_SIZE = 100
    # Image feat part
    imginp = Input(shape=(512,))
    imglay1 = Dropout(0.5)(imginp)
    imglay2 = Dense(EMBED_SIZE)(imglay1)
    # LSTM Part
    textinp = Input(shape=(39,))
    textlay1 = Embedding(VOCAB_SIZE, EMBED_SIZE, mask_zero=True)(textinp) 
    textlay2 = Dropout(0.5)(textlay1)
    textlay3 = LSTM(EMBED_SIZE)(textlay2)
    # # Decoder part that combines both
    declay1 = Add()([imglay2, textlay3])
    declay2 = Dense(EMBED_SIZE)(declay1)
    output = Dense(VOCAB_SIZE, activation="softmax")(declay2)
    # Creating keras model
    model = tf.keras.models.Model(inputs=[imginp,textinp],outputs=output)
    model.summary()
    
    img = tf.random.uniform([10, 512], dtype=tf.float32)
    txt = tf.random.uniform([10, 39], 0, VOCAB_SIZE, dtype=tf.int32)
    labels = tf.random.uniform([10], 0, VOCAB_SIZE, dtype=tf.int32)
    #pred = model((img, txt))
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
    model.fit((img, txt), labels)
    

    【讨论】:

    • 这就是问题所在。我将模型和数据生成器代码写在 py 文件中,而我将 Google Collab 用于计算资源目的。我会上传 py 文件,将模型创建和数据生成器代码导入笔记本,然后尝试拟合模型。这会引发错误。但是,如果我将整个代码从 py 移动到相同的笔记本块,错误就会消失。我猜 collab 从 py 文件导入时会讨厌它。
    • 因此,我错误地认为 [(None, 39)] 会发生一些奇怪的事情,原因是在列表中表示输入张量的方式。
    【解决方案2】:

    我想指出导致这种差异的两件事 -

    (1) 在本地运行代码与在 GoogleCollab 上运行代码会产生差异,如果代码块在 Collab 上运行,则会在这种情况下引发错误,说明渐变无法传播。所以,这似乎是 Collab 的问题。

    (2) 另外,Collab 的另一个奇怪之处是,如果有用 py 文件编写的函数被导入到 Collab env 中,然后从 notebook 作为 api 调用,则会弹出此错误。它与 Keras 版本无关,但与我无法调试的 Collab runtine 的底层行为更相关。

    很高兴我能解决这个问题并进行测试。更多详情,请评论,我会尽力解答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-04
      • 2022-01-17
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      相关资源
      最近更新 更多