我想用联邦学习微调预训练模型,所以我这样做:
def create_keras_model():
baseModel = tf.keras.models.load_model(path\to\model)
headModel = baseModel.output
model_output = tf.keras.layers.Dense(3)(headModel)
model = tf.keras.Model(inputs=baseModel.input, outputs=model_output)
for layer in baseModel.layers:
layer.trainable = False
return model
state = iterative_process.initialize()
keras_model = create_keras_model()
state = tff.learning.state_with_new_model_weights(
state,
trainable_weights=[v.numpy() for v in keras_model.trainable_weights],
non_trainable_weights=[
v.numpy() for v in keras_model.non_trainable_weights
])
evaluation = tff.learning.build_federated_evaluation(model_fn)
这是训练循环:
for round_num in range(1, NUM_ROUNDS):
state, _ = iterative_process.next(state, train_data)
test_metrics = evaluation(state.model, test_data)
print(test_metrics))
问题是测试精度仍然保持不变,并且始终没有增加:
round 1, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.8680933)])
round 2, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.836558)])
round 3, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.82953715)])
round 4, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.82713753)])
round 5, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.82613766)])
round 6, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.8256878)])
round 7, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.82548285)])
round 8, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.825384)])
round 9, metrics=OrderedDict([('categorical_accuracy', 0.67105263), ('loss', 0.825332)])
我想了解原因,如果有其他方法可以做到这一点?知道我的数据集是一个具有 3 类的图像数据集。