首页 > 解决方案 > Keras `Input` 层返回层 [(None, 32)] 而不是 (None, 32) 总结

问题描述

Keras 2.4.3 版

我正在创建一个简单的图像字幕模型,它有两个输入和一个输出。

模型定义代码如下:

# 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看起来像这样:

__________________________________________________________________________________________________
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)但它们似乎是一个列表。因此,ValueError: no grad available for the variables虽然我确实测试了 python 数据生成器,但我得到了一个结果。我相信这Input一层 api 会导致一些奇怪的错误。

有任何想法吗 ?

标签: pythontensorflowkeraskeras-layertf.keras

解决方案


我在 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)

推荐阅读