Skip to main content
 首页 » 编程设计

python之Keras:训练期间卡住图层不会提供一致的输出

2025年02月15日8tuyile006

我正在尝试使用 keras 微调模型,根据以下描述:https://keras.io/applications/#inceptionv3
然而,在训练过程中,我发现当使用相同的输入(同时所有相关层都被卡住)时,网络的输出在训练后不会保持不变,这是我不想要的。

我构建了以下玩具示例来对此进行调查:

import keras.applications.resnet50 as resnet50 
from keras.layers import Dense, Flatten, Input 
from keras.models import Model 
from keras.utils import to_categorical 
from keras import optimizers 
from keras.preprocessing.image import ImageDataGenerator 
import numpy as np 
 
# data   
i = np.random.rand(1,224,224,3) 
X = np.random.rand(32,224,224,3) 
y = to_categorical(np.random.randint(751, size=32), num_classes=751) 
 
# model 
base_model = resnet50.ResNet50(weights='imagenet', include_top=False, input_tensor=Input(shape=(224,224,3))) 
layer = base_model.output 
layer = Flatten(name='myflatten')(layer) 
layer = Dense(751, activation='softmax', name='fc751')(layer) 
model = Model(inputs=base_model.input, outputs=layer) 
 
# freeze all layers 
for layer in model.layers: 
    layer.trainable = False 
model.compile(optimizer='rmsprop', loss='categorical_crossentropy') 
 
# features and predictions before training 
feat0 = base_model.predict(i) 
pred0 = model.predict(i) 
weights0 = model.layers[-1].get_weights() 
 
# before training output is consistent 
feat00 = base_model.predict(i) 
pred00 = model.predict(i) 
print(np.allclose(feat0, feat00)) # True 
print(np.allclose(pred0, pred00)) # True 
 
# train 
model.fit(X, y, batch_size=2, epochs=3, shuffle=False) 
 
# features and predictions after training 
feat1 = base_model.predict(i) 
pred1 = model.predict(i) 
weights1 = model.layers[-1].get_weights() 
 
# these are not the same 
print(np.allclose(feat0, feat1)) # False 
# Optionally: printing shows they are in fact very different 
# print(feat0) 
# print(feat1) 
 
# these are not the same 
print(np.allclose(pred0, pred1)) # False 
# Optionally: printing shows they are in fact very different 
# print(pred0) 
# print(pred1) 
 
# these are the same and loss does not change during training 
# so layers were actually frozen 
print(np.allclose(weights0[0], weights1[0])) # True 
 
# Check again if all layers were in fact untrainable 
for layer in model.layers: 
     assert layer.trainable == False # All succeed 
# Being overly cautious also checking base_model 
for layer in base_model.layers: 
     assert layer.trainable == False # All succeed 

由于我卡住了所有层,所以我完全希望预测和特征都相等,但令人惊讶的是它们并非如此。

所以我可能犯了某种错误,但我想不出是什么..任何建议将不胜感激!

请您参考如下方法:

所以问题似乎是该模型使用了批量归一化层,这些层确实会根据训练期间看到的数据更新它们的内部状态(即它们的权重)。当他们的可训练标志被设置为 False 时,甚至会发生这种情况。随着权重的更新,输出也会发生变化。您可以使用问题中的代码并更改以下代码行来检查这一点:

这个weights0 = model.layers[-1].get_weights()
到 weights0 = model.layers[2].get_weights()
和这个 weights1 = model.layers[-1].get_weights()
到 weights1 = model.layers[2].get_weights()
或任何其他批量归一化层的索引。

因为接下来的断言将不再成立:
print(np.allclose(weights0, weights1)) # 现在这是 False

据我所知,目前还没有解决方案..

另见我的 issue在 Keras 的 Github 页面上。