adapt.feature_based.DeepCORAL
- classadapt.feature_based.DeepCORAL(encoder=None,task=None,Xt=None,lambda_=1.0,match_mean=False,copy=True,verbose=1,random_state=None,**params)[source]
DeepCORAL: Deep CORrelation ALignment
DeepCORAL is an extension of CORAL method. It learns a nonlineartransformation which aligns correlations of layer activations indeep neural networks.
The method consists in training both anencoder and atasknetwork. Theencoder network maps input features into newencoded ones on which thetask network is trained.
The parameters of the two networks are optimized in order tominimize the following loss function:
\[\mathcal{L} = \mathcal{L}_{task} + \lambda ||C_S - C_T||_F^2\]Where:
\(\mathcal{L}_{task}\) is the task loss computed withsource labeled data.
\(C_S\) is the correlation matrix of source data in theencoded feature space.
\(C_T\) is the correlation matrix of target data in theencoded feature space.
\(||.||_F\) is the Frobenius norm.
\(\lambda\) is a trade-off parameter.
Thus theencoder network learns a new feature representationon wich the correlation matrixes of source and target data are“close” and where atask network is able to learn the taskwith source labeled data.
Notice that DeepCORAL only uses labeled source and unlabeled targetdata. It belongs then to “unsupervised” domain adaptation methods.
DeepCORAL architecture (source: [1])
- Parameters
- encodertensorflow Model (default=None)
Encoder netwok. If
None
, a shallow network with 10neurons and ReLU activation is used as encoder network.- tasktensorflow Model (default=None)
Task netwok. If
None
, a two layers network with 10neurons per layer and ReLU activation is used as task network.- Xtnumpy array (default=None)
Target input data.
- lambda_float or tensorflow Variable (default=1.)
Trade-off parameter.
- match_meanbool (default=False)
Weither to match the means of sourceand target or not. If
False
onlythe second moment is matched as in theoriginal algorithm.- copyboolean (default=True)
Whether to make a copy of
estimator
or not.- verboseint (default=1)
Verbosity level.
- random_stateint (default=None)
Seed of random generator.
- paramskey, value arguments
Arguments given at the different level of the adapt object.It can be, for instance, compile or fit parameters of theestimator or kernel parameters etc…Accepted parameters can be found by calling the method
_get_legal_params(params)
.
- Yields
- optimizerstr or instance of tf.keras.optimizers (default=”rmsprop”)
Optimizer for the task. It should be aninstance of tf.keras.optimizers as:
tf.keras.optimizers.SGD(0.001)
ortf.keras.optimizers.Adam(lr=0.001,beta_1=0.5)
.A string can also be given as"adam"
.Default optimizer isrmsprop
.- lossstr or instance of tf.keras.losses (default=”mse”)
Loss for the task. It should be aninstance of tf.keras.losses as:
tf.keras.losses.MeanSquaredError()
ortf.keras.losses.CategoricalCrossentropy()
.A string can also be given as"mse"
orcategorical_crossentropy
.Default loss ismse
.- metricslist of str or list of tf.keras.metrics.Metric instance
List of metrics to be evaluated by the model during trainingand testing. Typically you will use
metrics=['accuracy']
.- optimizer_encstr or instance of tf.keras.optimizers
If the Adapt Model has an
encoder
attribute,a specific optimizer for theencoder
network canbe given. Typically, this parameter can be used togive a smaller learning rate to the encoder.If not specified,optimizer_enc=optimizer
.- optimizer_discstr or instance of tf.keras.optimizers
If the Adapt Model has a
discriminator
attribute,a specific optimizer for thediscriminator
network canbe given. If not specified,optimizer_disc=optimizer
.- kwargskey, value arguments
Any arguments of the
fit
method from the TensorflowModel can be given, asepochs
andbatch_size
.Specific arguments fromoptimizer
can also be givenaslearning_rate
orbeta_1
forAdam
.This allows to performGridSearchCV
from scikit-learnon these arguments.
References
- 1
[1] Sun B. and Saenko K. “Deep CORAL: correlation alignment for deep domain adaptation.” In ICCV, 2016.
Examples
>>>fromadapt.utilsimportmake_classification_da>>>fromadapt.feature_basedimportDeepCORAL>>>Xs,ys,Xt,yt=make_classification_da()>>>model=DeepCORAL(lambda_=1.,Xt=Xt,metrics=["acc"],random_state=0)>>>model.fit(Xs,ys,epochs=100,verbose=0)>>>model.score(Xt,yt)1/1 [==============================] - 0s 99ms/step - loss: 0.2029 - acc: 0.68000.2029329240322113
- Attributes
- encoder_tensorflow Model
encoder network.
- task_tensorflow Model
task network.
- history_dict
history of the losses and metrics across the epochs.If
yt
is given infit
method, target metricsand losses are recorded too.
Methods
__init__
([encoder, task, Xt, lambda_, ...])compile
([optimizer, loss, metrics, ...])Configures the model for training.
fit
(X[, y, Xt, yt, domains])Fit Model.
get_params
([deep])Get parameters for this estimator.
load_weights
(filepath[, skip_mismatch, ...])Loads all layer weights from a saved files.
predict
(x[, batch_size, verbose, steps, ...])Generates output predictions for the input samples.
predict_disc
(X)Return predictions of the discriminator on the encoded features.
predict_task
(X)Return predictions of the task on the encoded features.
save_weights
(filepath[, overwrite, ...])Saves all layer weights.
score
(X, y[, sample_weight])Return the evaluation of the model on X, y.
set_params
(**params)Set the parameters of this estimator.
transform
(X)Return the encoded features of X.
unsupervised_score
(Xs, Xt)Return unsupervised score.
- __init__(encoder=None,task=None,Xt=None,lambda_=1.0,match_mean=False,copy=True,verbose=1,random_state=None,**params)[source]
- compile(optimizer=None,loss=None,metrics=None,loss_weights=None,weighted_metrics=None,run_eagerly=None,steps_per_execution=None,**kwargs)[source]
Configures the model for training.
- Parameters
- optimizer: str or `tf.keras.optimizer` instance
Optimizer
- loss: str or `tf.keras.losses.Loss` instance
Loss function. A loss function is any callablewith the signatureloss = fn(y_true, y_pred),wherey_true are the ground truth values, andy_pred are the model’s predictions.y_true should have shape(batch_size, d0, .. dN) (except in the case ofsparse loss functions such assparse categorical crossentropy which expects integer arrays of shape(batch_size, d0, .. dN-1)).y_pred should have shape(batch_size, d0, .. dN).The loss function should return a float tensor.If a customLoss instance isused and reduction is set toNone, return value has shape(batch_size, d0, .. dN-1) i.e. per-sample or per-timestep lossvalues; otherwise, it is a scalar. If the model has multiple outputs,you can use a different loss on each output by passing a dictionaryor a list of losses. The loss value that will be minimized by themodel will then be the sum of all individual losses, unlessloss_weights is specified.
- metrics: list of str or list of `tf.keras.metrics.Metric` instance
List of metrics to be evaluated by the model during trainingand testing. Typically you will usemetrics=[‘accuracy’]. Afunction is any callable with the signatureresult = fn(y_true,y_pred). To specify different metrics for different outputs of amulti-output model, you could also pass a dictionary, such asmetrics={‘output_a’: ‘accuracy’, ‘output_b’: [‘accuracy’, ‘mse’]}.You can also pass a list to specify a metric or a list of metricsfor each output, such asmetrics=[[‘accuracy’], [‘accuracy’, ‘mse’]]ormetrics=[‘accuracy’, [‘accuracy’, ‘mse’]]. When you pass thestrings ‘accuracy’ or ‘acc’, we convert this to one oftf.keras.metrics.BinaryAccuracy,tf.keras.metrics.CategoricalAccuracy,tf.keras.metrics.SparseCategoricalAccuracy based on the lossfunction used and the model output shape. We do a similarconversion for the strings ‘crossentropy’ and ‘ce’ as well.
- loss_weights: List or dict of floats
Scalars to weight the loss contributions of different modeloutputs. The loss value that will be minimized by the model will thenbe theweighted sum of all individual losses, weighted by theloss_weights coefficients.If a list, it is expected to have a 1:1 mapping to the model’soutputs. If a dict, it is expected to map output names (strings)to scalar coefficients.
- weighted_metrics: list of metrics
List of metrics to be evaluated and weighted bysample_weight orclass_weight during training and testing.
- run_eagerly: bool (default=False)
IfTrue, thisModel’s logic will not be wrappedin atf.function. Recommended to leavethis asNone unless yourModel cannot be run inside atf.function.run_eagerly=True is not supported when usingtf.distribute.experimental.ParameterServerStrategy.
- steps_per_execution: int (default=1)
The number of batches to run during eachtf.function call. Running multiple batchesinside a singletf.function call can greatly improve performanceon TPUs or small models with a large Python overhead.At most, one full epoch will be run eachexecution. If a number larger than the size of the epoch is passed,the execution will be truncated to the size of the epoch.Note that ifsteps_per_execution is set toN,Callback.on_batch_begin andCallback.on_batch_end methodswill only be called everyN batches(i.e. before/after eachtf.function execution).
- **kwargs: key, value arguments
Arguments supported for backwards compatibility only.
- Returns
- None: None
- fit(X,y=None,Xt=None,yt=None,domains=None,**fit_params)[source]
Fit Model. Note that
fit
does not resetthe model but extend the training.Notice also that the compile method will be called if the model has not been compiled yet.
- Parameters
- Xarray or Tensor
Source input data.
- yarray or Tensor (default=None)
Source output data.
- Xtarray (default=None)
Target input data. If None, theXt argumentgiven ininit is used.
- ytarray (default=None)
Target input data. Only needed for supervisedand semi-supervised Adapt model.If None, theyt argument given ininit is used.
- domainsarray (default=None)
Vector giving the domain for each sourcedata. Can be used for multisource purpose.
- fit_paramskey, value arguments
Arguments given to the fit method of the model(epochs, batch_size, callbacks…).
- Returns
- selfreturns an instance of self
- get_params(deep=True)[source]
Get parameters for this estimator.
- Parameters
- deepbool, default=True
Not used, here for scikit-learn compatibility.
- Returns
- paramsdict
Parameter names mapped to their values.
- load_weights(filepath,skip_mismatch=False,by_name=False,options=None)[source]
Loads all layer weights from a saved files.
The saved file could be a SavedModel file, a.keras file (v3 savingformat), or a file created viamodel.save_weights().
By default, weights are loaded based on the network’stopology. This means the architecture should be the same as when theweights were saved. Note that layers that don’t have weights are nottaken into account in the topological ordering, so adding or removinglayers is fine as long as they don’t have weights.
Partial weight loading
If you have modified your model, for instance by adding a new layer(with weights) or by changing the shape of the weights of a layer,you can choose to ignore errors and continue loadingby settingskip_mismatch=True. In this case any layer withmismatching weights will be skipped. A warning will be displayedfor each skipped layer.
Weight loading by name
If your weights are saved as a.h5 file createdviamodel.save_weights(), you can use the argumentby_name=True.
In this case, weights are loaded into layers only if they sharethe same name. This is useful for fine-tuning or transfer-learningmodels where some of the layers have changed.
Note that only topological loading (by_name=False) is supported whenloading weights from the.keras v3 format or from the TensorFlowSavedModel format.
- Args:
- filepath: String, path to the weights file to load. For weight files
in TensorFlow format, this is the file prefix (the same as waspassed tosave_weights()). This can also be a path to aSavedModel or a.keras file (v3 saving format) savedviamodel.save().
- skip_mismatch: Boolean, whether to skip loading of layers where
there is a mismatch in the number of weights, or a mismatch inthe shape of the weights.
- by_name: Boolean, whether to load weights by name or by topological
order. Only topological loading is supported for weight files inthe.keras v3 format or in the TensorFlow SavedModel format.
- options: Optionaltf.train.CheckpointOptions object that specifies
options for loading weights (only valid for a SavedModel file).
- predict(x,batch_size=None,verbose=0,steps=None,callbacks=None,max_queue_size=10,workers=1,use_multiprocessing=False)[source]
Generates output predictions for the input samples.
Computation is done in batches. This method is designed for performance inlarge scale inputs. For small amount of inputs that fit in one batch,directly using__call__() is recommended for faster execution, e.g.,model(x), ormodel(x, training=False) if you have layers such astf.keras.layers.BatchNormalization that behaves differently duringinference. Also, note the fact that test loss is not affected byregularization layers like noise and dropout.
- Parameters
- x: array
Input samples.
- batch_size: int (default=`None`)
Number of samples per batch.If unspecified,batch_size will default to 32.Do not specify thebatch_size if your data is in theform of dataset, generators, orkeras.utils.Sequence instances(since they generate batches).
- verbose: int (default=0)
Verbosity mode, 0 or 1.
- steps: int (default=None)
Total number of steps (batches of samples)before declaring the prediction round finished.Ignored with the default value ofNone. If x is atf.datadataset andsteps is None,predict() willrun until the input dataset is exhausted.
- callbacks: List of `keras.callbacks.Callback` instances.
List of callbacks to apply during prediction.See [callbacks](/api_docs/python/tf/keras/callbacks).
- max_queue_size: int (default=10)
Used for generator orkeras.utils.Sequenceinput only. Maximum size for the generator queue.If unspecified,max_queue_size will default to 10.
- workers: int (default=1)
Used for generator orkeras.utils.Sequence inputonly. Maximum number of processes to spin up when usingprocess-based threading. If unspecified,workers will defaultto 1.
- use_multiprocessing: bool (default=False)
Used for generator orkeras.utils.Sequence input only.IfTrue, use process-basedthreading. If unspecified,use_multiprocessing will default toFalse. Note that because this implementation relies onmultiprocessing, you should not pass non-picklable arguments tothe generator as they can’t be passed easily to children processes.
- Returns
- y_predarray
Numpy array(s) of predictions.
- predict_disc(X)[source]
Return predictions of the discriminator on the encoded features.
- Parameters
- Xarray
input data
- Returns
- y_discarray
predictions of discriminator network
- predict_task(X)[source]
Return predictions of the task on the encoded features.
- Parameters
- Xarray
input data
- Returns
- y_taskarray
predictions of task network
- save_weights(filepath,overwrite=True,save_format=None,options=None)[source]
Saves all layer weights.
Either saves in HDF5 or in TensorFlow format based on thesave_formatargument.
- When saving in HDF5 format, the weight file has:
- layer_names (attribute), a list of strings
(ordered names of model layers).
- For every layer, agroup namedlayer.name
- For every such layer group, a group attributeweight_names,
a list of strings(ordered names of weights tensor of the layer).
- For every weight in the layer, a dataset
storing the weight value, named after the weight tensor.
When saving in TensorFlow format, all objects referenced by the networkare saved in the same format astf.train.Checkpoint, including anyLayer instances orOptimizer instances assigned to objectattributes. For networks constructed from inputs and outputs usingtf.keras.Model(inputs, outputs),Layer instances used by the networkare tracked/saved automatically. For user-defined classes which inheritfromtf.keras.Model,Layer instances must be assigned to objectattributes, typically in the constructor. See the documentation oftf.train.Checkpoint andtf.keras.Model for details.
While the formats are the same, do not mixsave_weights andtf.train.Checkpoint. Checkpoints saved byModel.save_weights shouldbe loaded usingModel.load_weights. Checkpoints saved usingtf.train.Checkpoint.save should be restored using the correspondingtf.train.Checkpoint.restore. Prefertf.train.Checkpoint oversave_weights for training checkpoints.
The TensorFlow format matches objects and variables by starting at aroot object,self forsave_weights, and greedily matching attributenames. ForModel.save this is theModel, and forCheckpoint.savethis is theCheckpoint even if theCheckpoint has a model attached.This means saving atf.keras.Model usingsave_weights and loadinginto atf.train.Checkpoint with aModel attached (or vice versa)will not match theModel’s variables. See the[guide to training checkpoints](https://www.tensorflow.org/guide/checkpoint) for details onthe TensorFlow format.
- Args:
- filepath: String or PathLike, path to the file to save the weights
to. When saving in TensorFlow format, this is the prefix usedfor checkpoint files (multiple files are generated). Note thatthe ‘.h5’ suffix causes weights to be saved in HDF5 format.
- overwrite: Whether to silently overwrite any existing file at the
target location, or provide the user with a manual prompt.
- save_format: Either ‘tf’ or ‘h5’. Afilepath ending in ‘.h5’ or
‘.keras’ will default to HDF5 ifsave_format isNone.Otherwise,None becomes ‘tf’. Defaults toNone.
- options: Optionaltf.train.CheckpointOptions object that specifies
options for saving weights.
- Raises:
- ImportError: Ifh5py is not available when attempting to save in
HDF5 format.
- score(X,y,sample_weight=None)[source]
Return the evaluation of the model on X, y.
Callevaluate on tensorflow Model.
- Parameters
- Xarray
input data
- yarray
output data
- sample_weightarray (default=None)
Sample weights
- Returns
- scorefloat
Score.
- set_params(**params)[source]
Set the parameters of this estimator.
- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.