Ejemplo n.º 1
0
 def test_keras_evaluate(self):
     keras_model = _create_test_cnn_model()
     sample_data = [
         collections.OrderedDict(x=np.ones([1, 28, 28, 1],
                                           dtype=np.float32),
                                 y=np.ones([1], dtype=np.int32))
     ]
     metrics = [tf.keras.metrics.SparseCategoricalAccuracy()]
     metrics = dp_fedavg.keras_evaluate(keras_model, sample_data, metrics)
     accuracy = metrics[0].result()
     self.assertIsInstance(accuracy, tf.Tensor)
     self.assertBetween(accuracy, 0.0, 1.0)
Ejemplo n.º 2
0
    def test_tff_learning_evaluate(self):
        it_process = dp_fedavg.build_federated_averaging_process(
            _tff_learning_model_fn)
        server_state = it_process.initialize()
        sample_data = [
            collections.OrderedDict(x=np.ones([1, 28, 28, 1],
                                              dtype=np.float32),
                                    y=np.ones([1], dtype=np.int32))
        ]
        keras_model = _create_test_cnn_model()
        server_state.model.assign_weights_to(keras_model)

        sample_data = [
            collections.OrderedDict(x=np.ones([1, 28, 28, 1],
                                              dtype=np.float32),
                                    y=np.ones([1], dtype=np.int32))
        ]
        metrics = [tf.keras.metrics.SparseCategoricalAccuracy()]
        metrics = dp_fedavg.keras_evaluate(keras_model, sample_data, metrics)
        accuracy = metrics[0].result()
        self.assertIsInstance(accuracy, tf.Tensor)
        self.assertBetween(accuracy, 0.0, 1.0)
Ejemplo n.º 3
0
 def evaluate_fn(model_weights, dataset):
     model.from_weights(model_weights)
     metrics = dp_fedavg.keras_evaluate(model.keras_model, dataset,
                                        keras_metics)
     return collections.OrderedDict(
         (metric.name, metric.result().numpy()) for metric in metrics)