gpt4 book ai didi

python - 值错误: Please initialize `TimeDistributed` layer with a `Layer` instance

转载 作者:行者123 更新时间:2023-11-30 09:03:03 26 4
gpt4 key购买 nike

我正在尝试构建一个可以在音频和视频样本上进行训练的模型,但出现此错误
ValueError:请使用“Layer”实例初始化“TimeDistributed”层。您传递了:Tensor("input_13:0", shape=(None, 5, 648, 384, 3), dtype=float32)

这是我的三个模型函数:

def build_convnet(shape=(648, 384, 3)):
momentum = .9
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(64, (2,2), input_shape=shape,padding='same', activation='relu'))
model.add(tf.keras.layers.Conv2D(64, (2,2), padding='same', activation='relu'))
model.add(tf.keras.layers.BatchNormalization(momentum=momentum))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Conv2D(128, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.Conv2D(128, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.BatchNormalization(momentum=momentum))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Conv2D(256, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.Conv2D(256, (3,3), padding='same', activation='relu'))
model.add(tf.keras.layers.BatchNormalization(momentum=momentum))
model.add(tf.keras.layers.GlobalMaxPool2D())

print(model.summary())
return model
def action_model(shape=(5, 648, 384, 3)):
# Create our convnet with (112, 112, 3) input shape
convnet = build_convnet(shape[1:])
# then create our final model
# model = tf.keras.Sequential()
# add the convnet with (5, 112, 112, 3) shape
input_shape = tf.keras.layers.Input(shape)
TD = tf.keras.layers.TimeDistributed(input_shape)(convnet)
# here, you can also use tf.keras.layers.GRU or LSTM
LSTM1 = tf.keras.layers.LSTM(1024)(TD)

Dense1 = tf.keras.layers.Dense(512, activation='relu')(LSTM1)
Drop1 = tf.keras.layers.Dropout(.2)(Dense1)
Dense2 = tf.keras.layers.Dense(128, activation='relu')(Drop1)
Drop2 = tf.keras.layers.Dropout(.2)(Dense2)
Dense3 = tf.keras.layers.Dense(64, activation='relu')(Drop2)
# Dense4 = tf.keras.layers.Dense(2, activation='softmax')(Dense3)

model = tf.keras.models.Model(inputs=input_shape,outputs=Dense3)

return model
def audio_and_final_model():
input_shape = tf.keras.layers.Input(shape=(220941,1))
Conv1 = tf.keras.layers.Conv1D(16,activation='relu',kernel_size=(10))(input_shape)
MaxPool1 = tf.keras.layers.MaxPool1D()(Conv1)
Dropout1 = tf.keras.layers.Dropout(0.2)(MaxPool1)
Conv2 = tf.keras.layers.Conv1D(32,activation='relu',kernel_size=(10))(Dropout1)
MaxPool2 = tf.keras.layers.MaxPool1D()(Conv2)
Dropout2 = tf.keras.layers.Dropout(0.2)(MaxPool2)
Conv3 = tf.keras.layers.Conv1D(16,activation='relu',kernel_size=(10))(Dropout2)
MaxPool3 = tf.keras.layers.MaxPool1D()(Conv3)
Dropout3 = tf.keras.layers.Dropout(0.2)(MaxPool3)
Flatten = tf.keras.layers.Flatten()(Dropout3)
Dense1 = tf.keras.layers.Dense(128,activation='relu')(Flatten)
Dense2 = tf.keras.layers.Dense(64,activation='relu')(Dense1)


model = tf.keras.models.Model(inputs=input_shape,outputs=Dense2)

return model
INSHAPEAM = (5, 648, 384, 3)
INSHAPEAFM = (220941,1)
am = action_model()
afm = audio_and_final_model()

combined = tf.keras.layers.Concatenate([am.output,afm.output])
z = tf.keras.layers.Dense(2,activation='softmax')(combined)

model = tf.keras.models.Model(inputs=[INSHAPEAM,INSHAPEAFM],outputs=z)

我尝试搜索,但只能找到一个答案 here但我真的不太明白,所以如果有人可以在这里帮助我,那将是很大的帮助。提前致谢!

最佳答案

问题出在下面的部分。从函数中更改这部分:只需尝试使用功能模型而不是顺序模型将 build_convnet 上的部分嵌入到 Action 模型中

 TD = tf.keras.layers.TimeDistributed(convnet)(input_shape)

关于python - 值错误: Please initialize `TimeDistributed` layer with a `Layer` instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com