def evaluate(self, theta: Theta, xy_test: Partition) -> Tuple[float, float]:
     model = self.model_provider.init_model()
     model.set_weights(theta)
     ds_val = prep.init_ds_val(xy_test)
     # Assume the validation `tf.data.Dataset` to yield exactly one batch containing
     # all examples in the validation set
     loss, accuracy = model.evaluate(ds_val, steps=1, verbose=0)
     return loss, accuracy
Exemplo n.º 2
0
    def fit(self, model: tf.keras.Model, epochs: int) -> KerasHistory:
        ds_train = prep.init_ds_train(self.xy_train, self.num_classes,
                                      self.batch_size)
        ds_val = prep.init_ds_val(self.xy_val, self.num_classes)

        hist = model.fit(
            ds_train,
            epochs=epochs,
            validation_data=ds_val,
            callbacks=[LoggingCallback(str(self.cid), logging.info)],
            shuffle=False,  # Shuffling is handled via tf.data.Dataset
            steps_per_epoch=self.steps_train,
            validation_steps=self.steps_val,
            verbose=0,
        )
        return cast_to_float(hist.history)
Exemplo n.º 3
0
def create_evalueate_fn(
        orig_model: tf.keras.Model,
        xy_val: Partition) -> Callable[[Theta], Tuple[float, float]]:
    ds_val = prep.init_ds_val(xy_val)
    model = tf.keras.models.clone_model(orig_model)
    # FIXME refactor model compilation
    model.compile(
        loss=tf.keras.losses.categorical_crossentropy,
        optimizer=tf.keras.optimizers.Adam(),
        metrics=["accuracy"],
    )

    def fn(theta: Theta) -> Tuple[float, float]:
        model.set_weights(theta)
        # Assume the validation `tf.data.Dataset` to yield exactly one batch containing
        # all examples in the validation set
        return model.evaluate(ds_val, steps=1)

    return fn
Exemplo n.º 4
0
 def evaluate(self, xy_val: Partition) -> Tuple[float, float]:
     ds_val = prep.init_ds_val(xy_val)
     # Assume the validation `tf.data.Dataset` to yield exactly one batch containing
     # all examples in the validation set
     loss, accuracy = self.model.evaluate(ds_val, steps=1)
     return float(loss), float(accuracy)
Exemplo n.º 5
0
 def __init__(
     self, model: tf.keras.Model, xy_val: Tuple[np.ndarray, np.ndarray]
 ) -> None:
     self.model = model
     self.ds_val = prep.init_ds_val(xy_val)
Exemplo n.º 6
0
 def __init__(self, model: tf.keras.Model, xy_val: Partition) -> None:
     self.model = model
     self.ds_val = prep.init_ds_val(xy_val)