gpt4 book ai didi

python - tf.keras.layers.pop() 不起作用,但 tf.keras._layers.pop() 起作用

转载 作者:行者123 更新时间:2023-11-28 18:56:52 27 4
gpt4 key购买 nike

我想弹出模型的最后一层。所以我使用了 tf.keras.layers.pop(),但它不起作用。

base_model.summary()

enter image description here

base_model.layers.pop()

base_model.summary()

enter image description here

当我使用 tf.keras._layers.pop() 时,它起作用了。

base_model.summary()

enter image description here

base_model._layers.pop()
base_model.summary()

enter image description here

我找不到有关此用法的文档。有人可以帮忙解释一下吗?

最佳答案

我同意这令人困惑。原因是 model.layers 返回层列表的浅拷贝,因此:

tldr 不使用 model.layers.pop() 删除最后一层。相反,我们应该创建一个新模型,除了最后一层。也许是这样的:

new_model = tf.keras.models.Sequential(base_model.layers[:-1])

检查这个 github issue了解更多详情

关于python - tf.keras.layers.pop() 不起作用,但 tf.keras._layers.pop() 起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57316557/

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