backwardcompatibilityml.tensorflow package

Submodules

backwardcompatibilityml.tensorflow.helpers module

backwardcompatibilityml.tensorflow.helpers.bc_fit(h2, training_set=None, testing_set=None, epochs=None, bc_loss=None, optimizer=None)

This function is used to train a model h2, using an instance of a Tensorflow BCLoss function that has been instantiated using an existing model h1 and regularization parameter lambda_c.

Example usage:

h2 = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28, 1)), tf.keras.layers.Dense(128,activation=’relu’), tf.keras.layers.Dense(10, activation=’softmax’)

])

lambda_c = 0.9 h1.trainable = False bc_loss = BCCrossEntropyLoss(h1, h2, lambda_c)

optimizer = tf.keras.optimizers.Adam(0.001)

tf_helpers.bc_fit(
h2, training_set=ds_train, testing_set=ds_test, epochs=6, bc_loss=bc_loss, optimizer=optimizer)
Parameters:
  • h2 – A Tensorflow model that we want to train using backward compatibility.
  • training_set – The training set for our model.
  • testing_set – The testing set for validating our model.
  • epochs – The number of training epochs.
  • bc_loss – An instance of a Tensorflow BCLoss function.
  • optimizer – The optimizer to use.
Returns:

Does not return anything. But it updates the weights of the model h2.

backwardcompatibilityml.tensorflow.models module

class backwardcompatibilityml.tensorflow.models.BCNewErrorCompatibilityModel(*args, h1=None, lambda_c=0.0, **kwargs)

Bases: sphinx.ext.autodoc.importer._MockObject

BackwardCompatibility base model for Tensorflow

You may create a new Tensorflow model by subclassing your new model h2 from this model. This allows you to train or update a new model h2, using the backward compatibility loss, with respect to an existing model h1, using the Tensorflow fit method, h2.fit(…).

Assuming that you have a pre-trained model h1 and you would like to create a new model h2 trained using the backward compatibility loss with respect to h1, the following describes the example usage:

h1.trainable = False

h2 = BCNewErrorCompatibilityModel([
tf.keras.layers.Flatten(input_shape=(28, 28, 1)), tf.keras.layers.Dense(128,activation=’relu’), tf.keras.layers.Dense(10, activation=’softmax’)

], h1=h1, lambda_c=0.7)

h2.compile(
loss=tf.keras.losses.sparse_categorical_crossentropy, optimizer=tf.keras.optimizers.Adam(0.001), metrics=[‘accuracy’]

)

h2.fit(
dataset_train, epochs=6, validation_data=dataset_test,

)

dissonance(h2_output, target_labels, loss)

The dissonance function, which uses the loss function specified by the user to calculate the loss on a subset of the target.

loss_func(x, y, loss=None)

Backward compatibility loss function to be used by the model

train_step(data)

This is a custom train step which allows us to use to train our model using the fit() method, using a non-standard loss funtion.

Module contents