python - 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 会导致一些奇怪的错误。
有任何想法吗 ?
解决方案
我在 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)
推荐阅读
- java - 当我单击通知中的按钮时如何更新活动 UI?
- visual-c++ - 在 C++ 中更改 MFC 菜单弹出文本
- c# - 在任务栏应用程序悬停时显示控件
- c++ - 类型定义 X
=T::UserType1,但如果不适用,typedef X =用户类型2 - mdx - MDX 格式化结果
- angular - 带有LatestFrom警告的Angular7 Ngrx6
- json - 如何在同一条消息中多次循环不同类型的嵌套 JSON 对象
- tensorflow - 在评估预训练的 RPN 期间获取“图形已完成且无法修改”
- python - 使用没有id作为主键的python库flask-rest-jsonapi
- android - 使用 Fragment WebView 在 ViewPager 中删除和添加新选项卡