1

I'm trying to obtain the losses of all clients in tensorflow model without luck. The answer to post how to print local outputs in tensorflow federated? suggests to create our NN model from scratch. However, I already have my keras NN model. So is there a way to still access the local client losses without having to build NN from scratch?

I tried to use tff.federated_collect(), but not sure how is that possible.

This is partly my attempt:

    trainer_Itr_Process = tff.learning.build_federated_averaging_process(model_fn_Federated,server_optimizer_fn=(lambda : tf.keras.optimizers.SGD(learning_rate=learn_rate)),client_weight_fn=None)
    FLstate = trainer_Itr_Process.initialize()

    @tff.learning.Model
    def federated_output_computation():
        return{
            'num_examples': tff.federated_sum(metrics.num_examples),
            'loss': tff.federated_mean(metrics.loss, metrics.num_examples),
            'accuracy': tff.federated_mean(metrics.accuracy, metrics.num_examples),
            'per_client/num_examples': tff.federated_collect(metrics.num_examples),
            'per_client/loss': tff.federated_collect(metrics.loss),
            'per_client/accuracy': tff.federated_collect(metrics.accuracy),
            }

This is the error I received:

   @tff.learning.Model
TypeError: object() takes no parameters

RiDaLa
  • 41
  • 3

1 Answers1

0

tff.learning.Model is not a decorator for functions, it is the class interface used by the tff.learning module.

Probably the best way to change the implementation of tff.learning.Model.federated_output_computation (what is recommended in how to print local outputs in tensorflow federated?) is to create your own subclass of tff.learning.Model, that implements a different federated_output_computation property. This would be close to re-implementing tff.learning.from_keras_model(), except providing the custom metric aggregation; so looking at the implementation (here) can be useful, but ingesting Keras models is non-trivial at the moment.

Zachary Garrett
  • 2,911
  • 15
  • 23