gpt4 book ai didi

python - Keras:如何减去两个不同模型的输出并输入到另一个模型?

转载 作者:行者123 更新时间:2023-12-01 00:24:21 26 4
gpt4 key购买 nike

我从不同的两个模型获得相同尺寸的输出。我想减去这两个输出并将结果作为输入以形成新模型。但不幸的是,我收到以下错误:

'Subtract' object is not subscriptable

我的代码:

# create a data generator
datagen2 = ImageDataGenerator(horizontal_flip=True,brightness_range=[0.7,1.0],rotation_range=10,width_shift_range=[-10,10],preprocessing_function=preprocess_input)
# load anad iterate training dataset
train_it2 = datagen2.flow_from_directory('DATA/train/', class_mode='categorical', batch_size=50,subset='training',target_size=(224, 224),shuffle=True)
test_it2 = datagen2.flow_from_directory('DATA/val/', class_mode='categorical', batch_size=50,target_size=(224, 224))
#subtract layer model

model_del1 = VGG16(include_top=False, weights='imagenet')
#add layers
model_del1_x = Conv2D(512, (3, 3), activation='relu',kernel_regularizer=l2(0.001), bias_regularizer=l2(0.002))(model_del1.output)
model_del1_x = Conv2D(4,(1,1), activation='relu')(model_del1_x)
model_del1_x = GlobalAveragePooling2D()(model_del1_x)

model_del1 = Model(inputs=model_del1.input, outputs=model_del1_x)
#print(model_del1.summary())


model_del2 = VGG16(include_top=False, weights='imagenet')
#add layers
model_del2_x = Conv2D(512, (3, 3), activation='relu',kernel_regularizer=l2(0.001), bias_regularizer=l2(0.002))(model_del2.output)
model_del2_x = Conv2D(4,(1,1), activation='relu')(model_del2_x)
model_del2_x = GlobalAveragePooling2D()(model_del2_x)

model_del2 = Model(inputs=model_del2.input, outputs=model_del2_x)
#print(model_del2.summary())

model_del_fin = keras.layers.Subtract()[model_del1_x, model_del2_x]

model_del_fin_x = Activation('softmax')(model_del_fin)
model_del_fin = Model(inputs=model_del_fin.input, outputs=model_del1_x)
print(model_del_fin.summary())

我还尝试了 model_del_fin = keras.layers.Subtract()[model_del1.output, model_del2.output] 但遇到了相同的错误。

请告诉我我犯的错误以及如何解决它?

最佳答案

在Python中,object[a]始终意味着:通过a索引到('下标')object;您正在寻找的是调用 对象来触发其 call方法(因此将其用作函数):

keras.layers.Subtract()[model_del1.output, model_del2.output]   # INCORRECT
keras.layers.Subtract()([model_del1.output, model_del2.output]) # CORRECT

然而,这并不能解决整个问题——这取决于预期的用途;等待澄清。

<小时/>

更新:查看评论; OP 似乎已经弄清楚了。为了完成这个答案:目标是将减法模型的输出反向传播到整个整体,包括输出被减去的两个模型。

这样做需要从两个模型的每个输入到第三个模型的输出的完全连接图;例如model3 = Model(inputs=[model1.input, model2.input], out)。然后可以将数据作为 model3.fit([x1, x2]) 提供,或者如果 x1 == x2, model3.fit([x1, x1] ),其中 x1 被馈送到 model1.inputx2 被馈送到 model2.input .

关于python - Keras:如何减去两个不同模型的输出并输入到另一个模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58719795/

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