属性错误:"张量"对象在尝试使用"模型"时没有属性"输入"



我在Keras中使用"Model"模型时遇到此错误。这是我的代码:

embedding_units = 300
model = Sequential([
Dense(embedding_units, input_shape=(2048,), activation='relu'),
RepeatVector(max_length) 
])
modelb = Sequential([
Embedding(vocab_size, embedding_units, input_length=max_length),
LSTM(256, return_sequences=True),
TimeDistributed(Dense(300)),
])
merged_model = concatenate([model.output, modelb.output], axis=1)
merged_model = Bidirectional(LSTM(256, return_sequences=True))(merged_model)
merged_model = TimeDistributed(Dense(vocab_size))(merged_model)
final_model = Activation('softmax')(merged_model)
model = Model([model.input, modelb.input], final_model)

我得到的错误如下:

AttributeError                            Traceback (most recent call last)
<ipython-input-54-664f1b1c74f6> in <module>()
22 final_model = Activation('softmax')(merged_model)
23 
---> 24 model = Model([model_imgs.input, captioning_model.input], out)
AttributeError: 'Tensor' object has no attribute 'input'

是否有意用RepeatVector层覆盖model_imgs密集层?

现在,您正在使用RepeatVector层覆盖密集层model_imgs。但是,您根本不是将此RepeatVector层连接到模型。我建议如下:

model = Input(shape=(2048,))
model_imgs = Dense(embedding_units, activation='relu')(model)
model_imgs = RepeatVector(max_length)(model_imgs) # <--- 1. initialize RepeatVector 2. call the created object => now model_imgs is connected to the whole model starting from input
captioning_model = Embedding(vocab_size, embedding_units, input_length=max_length)(model)
captioning_model = LSTM(256, return_sequences=True)(captioning_model)
captioning_model = TimeDistributed(Dense(256))(captioning_model)

merged_model = concatenate([model_imgs, captioning_model], axis=1)
merged_model = Bidirectional(LSTM(256, return_sequences=True))(x)
merged_model = TimeDistributed(Dense(vocab_size))(merged_model)
final_model = Activation('softmax')(merged_model)

最新更新