我是 TFF 的新手,我正在学习本教程。我想用keras_evaluate
TFF 的预定义函数替换函数:evaluation = tff.learning.build_federated_evaluation(model)
那么如何编辑这些行:
def keras_evaluate(state, round_num):
# Take our global model weights and push them back into a Keras model to
# use its standard `.evaluate()` method.
keras_model = load_model(batch_size=BATCH_SIZE)
keras_model.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[FlattenedCategoricalAccuracy()])
state.model.assign_weights_to(keras_model)
loss, accuracy = keras_model.evaluate(example_dataset, steps=2, verbose=0)
print('\tEval: loss={l:.3f}, accuracy={a:.3f}'.format(l=loss, a=accuracy))
for round_num in range(NUM_ROUNDS):
print('Round {r}'.format(r=round_num))
keras_evaluate(state, round_num)
state, metrics = fed_avg.next(state, train_datasets)
train_metrics = metrics['train']
print('\tTrain: loss={l:.3f}, accuracy={a:.3f}'.format(
l=train_metrics['loss'], a=train_metrics['accuracy']))
print('Final evaluation')
keras_evaluate(state, NUM_ROUNDS + 1)
在这一行:
loss, accuracy = keras_model.evaluate(example_dataset, steps=2, verbose=0)
该函数仅对与 相反的数据集示例进行build_federated_evaluation
评估,它federated_test_data
完全评估。那么如何修改此函数以评估federated_test_data
其他教程中的总体情况: test_metrics = evaluation(state.model, federated_test_data)