如何解决Keras中的Trainable-False不起作用的问题?

时间:2019-04-12 08:18:44

标签: tensorflow keras deep-learning

我现在遇到了“ trainable = false”的故障。

当我开发具有这样结构的代码时,

该模型具有两个细分模型(FC模型,CN模型),它们以串行方式连接。

仅训练了FC模型之后,我想冻结FC并训练整个模型FC + CN。

但是,使用可训练的冻结无效,并且出现了奇怪的情况。

不冻结时:

model.FCnetwork.trainable = True
model.FCnetwork.summary()
Total params: 2,584,576
Trainable params: 2,578,432
Non-trainable params: 6,144

以及冻结时:

model.FCnetwork.trainable = False
model.FCnetwork.summary()
Total params: 5,163,008
Trainable params: 2,578,432
Non-trainable params: 2,584,576

总参数增加。当然,冻结是行不通的。

这是我设计的课程

class MYMAP():
    def __init__(self):
        # Input shape


        optimizer = optimizers.Adam()

        self.CNnetwork= self.Convolutional_network()
        self.CNnetwork.compile()




        self.FCnetwork = self.Fullyconnected_network()
        self.FCnetwork.compile(loss='mse',
            optimizer=optimizer)

        z = Input(shape=(input_size,))
        img = self.FCnetwork(z)

        valid = self.CNnetwork(img)

        self.combined = Model(z, valid)

        optimizer_DG = optimizers.Adam()
        self.combined.compile(loss='mse', optimizer=optimizer_DG)

    def Fullyconnected_network(self):

        noise = Input(shape=(input_size,))
        img = model(noise)

        return Model(noise, img)




    def Convolutional_network(self):

        img = Input(shape=(image_size_vectored,))
        validity = model(img)

        return Model(img, validity)

我很难找出解决方法。

非常感谢您。

1 个答案:

答案 0 :(得分:0)

警告清楚地表明

  

您是否设置了model.trainable而没有致电model.compile

正确的示例代码:

class MYMAP():
    def __init__(self):        
        self.optimizer = optimizers.Adam()
        self.FCnetwork = self.Fullyconnected_network()

        self.FCnetwork.compile(loss='mse',
            optimizer=self.optimizer)

        z = Input(shape=(32,))
        img = self.FCnetwork(z)


    def Fullyconnected_network(self):            
        noise = Input(shape=(32,))        
        img = Dense(8)(noise)
        return Model(noise, img)

model = MYMAP()
model.FCnetwork.trainable = True
model.FCnetwork.compile(loss='mse', optimizer=optimizers.Adam())
model.FCnetwork.summary()
model.FCnetwork.trainable = False
model.FCnetwork.compile(loss='mse', optimizer=optimizers.Adam())
model.FCnetwork.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_39 (InputLayer)        (None, 32)                0         
_________________________________________________________________
dense_15 (Dense)             (None, 8)                 264       
=================================================================
Total params: 264
Trainable params: 264
Non-trainable params: 0
_________________________________________________________________
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_39 (InputLayer)        (None, 32)                0         
_________________________________________________________________
dense_15 (Dense)             (None, 8)                 264       
=================================================================
Total params: 264
Trainable params: 0

因此,请确保在更改模型的可训练参数后运行model.compile。