gpt4 book ai didi

python - 如何在 Keras 的迁移学习中使用 input_shape 和 input_tensor?

转载 作者:行者123 更新时间:2023-12-05 01:40:11 29 4
gpt4 key购买 nike

当我们在 Keras2 中进行迁移学习时,Arguments 需要“input_shape”和“input_tensor”。但我只使用 input_tensor 并且从未使用过 input_shape。我觉得只有input_tensor就够了,不知道什么时候用input_shape。我应该如何分别使用它们?

我同时使用 input_tensor 和 input_shape 分别取值,只采用了 input_tensor 的值而忽略了 input_shape。

vgg16_model = VGG16(include_top=False, weights='imagenet', 
input_tensor = Input(shape=(150, 150, 3)),
input_shape=(224,224,3))

top_model = Sequential()
top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dense(1, activation='sigmoid'))
model = Model(input=vgg16_model.input, output=top_model(vgg16_model.output))

model.summary()
Layer (type)                 Output Shape              Param #   
================================================================
input_6 (InputLayer) (None, 150, 150, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 150, 150, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 150, 150, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 75, 75, 64) 0
_________________________________________________________________
block2_conv......

我预计这段代码会出现一些错误,但没有错误,而且这个模型可以接受 (150, 150, 3) 的形状。 Input_shape=(224,224,3) 被忽略了。

你能帮我一点忙吗?谢谢。

最佳答案

VGG16 代码可能只是忘记检查两个参数。

当然,两者都没有意义。

  • 当您希望模型自动创建具有该尺寸的输入层时,您可以使用 input_shape
  • 当您有一个要作为输入的张量时,您可以使用 input_tensor

您可以在input_tensor中使用任何张量,这意味着使用其他模型/层的输出作为VGG16的输入.当然,你可以像你一样传递一个虚拟输入张量,代码没有理由提示,它收到了一个张量,好的。

唯一的问题是编码员忘记验证“如果两个参数都存在,则抛出错误”。

关于python - 如何在 Keras 的迁移学习中使用 input_shape 和 input_tensor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56897864/

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