Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在串联keras模型中设置可训练参数_Python_Tensorflow_Machine Learning_Keras_Conv Neural Network - Fatal编程技术网

Python 如何在串联keras模型中设置可训练参数

Python 如何在串联keras模型中设置可训练参数,python,tensorflow,machine-learning,keras,conv-neural-network,Python,Tensorflow,Machine Learning,Keras,Conv Neural Network,原始代码太bg了,所以我将尝试用一个简化的示例来解释这个问题 首先,导入我们需要的库: import tensorflow as tf from keras.applications.resnet50 import ResNet50 from keras.models import Model from keras.layers import Dense, Input 然后加载一个预训练的模型并打印出摘要 model = ResNet50(weights='imagenet') model.s

原始代码太bg了,所以我将尝试用一个简化的示例来解释这个问题

首先,导入我们需要的库:

import tensorflow as tf
from keras.applications.resnet50 import ResNet50
from keras.models import Model
from keras.layers import Dense, Input
然后加载一个预训练的模型并打印出摘要

model = ResNet50(weights='imagenet')
model.summary()
以下是“摘要”的输出:

(我剪切了
summary()
函数的输出以节省一些空间。) 现在,所有图层参数都是可训练的。为了举例,我将一个trainable参数设置为
False
,如下所示

model.get_layer('bn5c_branch2c').trainable = False
现在,除了层bn5c\u branch2c之外,所有层都可以训练

接下来,使用这个原始模型创建一个新模型,但让它成为一个连接的模型

in1 = Input(shape=(224, 224, 3), name="in1")
in2 = Input(shape=(224, 224, 3), name="in2")

out1 = model(in1)
out2 = model(in2)

new_model = Model(inputs=[in1, in2], outputs=[out1, out2])
然后再次打印总结:

new_model.summary()
以及输出:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
in1 (InputLayer)                (None, 224, 224, 3)  0                                            
__________________________________________________________________________________________________
in2 (InputLayer)                (None, 224, 224, 3)  0                                            
__________________________________________________________________________________________________
resnet50 (Model)                (None, 1000)         25636712    in1[0][0]                        
                                                                 in2[0][0]                        
==================================================================================================
Total params: 25,636,712
Trainable params: 25,583,592
Non-trainable params: 53,120
__________________________________________________________________________________________________
在这一点上,我已经无法看到哪些层是可训练的,哪些层是不可训练的,因为原始ResNet50模型的所有层现在都显示为一个层。 如果我运行以下代码,它会给出
True

new_model.get_layer('resnet50').trainable    # Returns True
问题1)我确实在模型中将上面的层bn5c\u branch2c的可训练参数设置为False。我是否可以假设,即使在新模型中,bn5c\u branch2c的可训练值仍然为假


问题2)如果上述问题的答案是肯定的(这意味着层bn5c\u branch2c的可训练参数值在新的\u模型中仍然为假)。。。如果我以后保存这个新的_模型的架构和权重,并再次加载它们以进一步训练这个新的_模型。。。我可以相信bn5c\u branch2c的可训练参数值将保持为False吗?

注意:您可以使用
.layers[idx]
属性访问模型的层,其中
idx
是模型中层的索引(从零开始)。或者,如果您已经为层设置了名称,则可以使用
.get\u layer(layer\u name)
方法访问它们

A1)是的,您可以通过以下方式确认:

print(new_model.layers[2].get_layer('bn5c_branch2c').trainable) # output: False
# save it
new_model.save('my_new_model.hd5')

# load it again
new_model = load_model('my_new_model.hd5')

print(new_model.layers[2].get_layer('bn5c_branch2c').trainable) # output: False
此外,您可以通过查看模型摘要中不可训练参数的数量来确认这一点

A2)是的,您可以通过以下方式确认:

print(new_model.layers[2].get_layer('bn5c_branch2c').trainable) # output: False
# save it
new_model.save('my_new_model.hd5')

# load it again
new_model = load_model('my_new_model.hd5')

print(new_model.layers[2].get_layer('bn5c_branch2c').trainable) # output: False

伟大的非常感谢。我不知道我能以你在答案中显示的方式达到参数。解决了的!