Saves a model as a TensorFlow SavedModel or HDF5 file.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.models.save_model
See the Serialization and Saving guide for details.
The SavedModel and HDF5 file contains:
Thus models can be reinstantiated in the exact same state, without any of the code used for model definition or training.
Note that the model weights may have different scoped names after being loaded. Scoped names include the model/layer names, such as "dense_1/kernel:0". It is recommended that you use the layer properties to access specific variables, e.g. model.get_layer("dense_1").kernel.
SavedModel serialization format
Keras SavedModel uses tf.saved_model.save to save the model and all trackable objects attached to the model (e.g. layers and variables). The model config, weights, and optimizer are saved in the SavedModel. Additionally, for every Keras layer attached to the model, the SavedModel stores:
The traced functions allow the SavedModel format to save and load custom layers without the original class definition.
You can choose to not save the traced functions by disabling the save_traces option. This will decrease the time it takes to save the model and the amount of disk space occupied by the output SavedModel. If you enable this option, then you must provide all custom class definitions when loading the model. See the custom_objects argument in tf.keras.models.load_model.


Built-in optimizer classes.
Main aliases
tf.optimizers
For more examples see the base class tf.keras.optimizers.Optimizer.
schedules module: Public API for tf.keras.optimizers.schedules namespace.
class Adadelta: Optimizer that implements the Adadelta algorithm.
class Adagrad: Optimizer that implements the Adagrad algorithm.
class Adam: Optimizer that implements the Adam algorithm.
class Adamax: Optimizer that implements the Adamax algorithm.
class Ftrl: Optimizer that implements the FTRL algorithm.
class Nadam: Optimizer that implements the NAdam algorithm.
class Optimizer: Base class for Keras optimizers.
class RMSprop: Optimizer that implements the RMSprop algorithm.
class SGD: Gradient descent (with momentum) optimizer.
deserialize(...): Inverse of the serialize function.
get(...): Retrieves a Keras Optimizer instance.
serialize(...)


Optimizer that implements the Adadelta algorithm.
Inherits From: Optimizer
Main aliases
tf.optimizers.Adadelta
See Migration guide for more details.
tf.compat.v1.keras.optimizers.Adadelta
Adadelta optimization is a stochastic gradient descent method that is based on adaptive learning rate per dimension to address two drawbacks:
Adadelta is a more robust extension of Adagrad that adapts learning rates based on a moving window of gradient updates, instead of accumulating all past gradients. This way, Adadelta continues learning even when many updates have been done. Compared to Adagrad, in the original version of Adadelta you don't have to set an initial learning rate. In this version, initial learning rate can be set, as in most other Keras optimizers.
According to section 4.3 ("Effective Learning rates"), near the end of training step sizes converge to 1 which is effectively a high learning rate which would cause divergence. This occurs only near the end of the training as gradients and step sizes are small, and the epsilon constant in the numerator and denominator dominate past gradients and parameter updates which converge the learning rate to 1.
According to section 4.4("Speech Data"),where a large neural network with 4 hidden layers was trained on a corpus of US English data, ADADELTA was used with 100 network replicas.The epsilon used is 1e-6 with rho=0.95 which converged faster than ADAGRAD, by the following construction: def init(self, lr=1.0, rho=0.95, epsilon=1e-6, decay=0., **kwargs):


Optimizer that implements the Adagrad algorithm.
Inherits From: Optimizer
Main aliases
tf.optimizers.Adagrad
See Migration guide for more details.
tf.compat.v1.keras.optimizers.Adagrad
Adagrad is an optimizer with parameter-specific learning rates, which are adapted relative to how frequently a parameter gets updated during training. The more updates a parameter receives, the smaller the updates.


Optimizer that implements the Adam algorithm.
Inherits From: Optimizer
Main aliases
tf.optimizers.Adam
See Migration guide for more details.
tf.compat.v1.keras.optimizers.Adam
Adam optimization is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments.
According to Kingma et al., 2014, the method is "computationally efficient, has little memory requirement, invariant to diagonal rescaling of gradients, and is well suited for problems that are large in terms of data/parameters".
The default value of 1e-7 for epsilon might not be a good default in general. For example, when training an Inception network on ImageNet a current good choice is 1.0 or 0.1. Note that since Adam uses the formulation just before Section 2.1 of the Kingma and Ba paper rather than the formulation in Algorithm 1, the "epsilon" referred to here is "epsilon hat" in the paper.
The sparse implementation of this algorithm (used when the gradient is an IndexedSlices object, typically because of tf.gather or an embedding lookup in the forward pass) does apply momentum to variable slices even if they were not used in the forward pass (meaning they have a gradient equal to zero). Momentum decay (beta1) is also applied to the entire momentum accumulator. This means that the sparse behavior is equivalent to the dense behavior (in contrast to some momentum implementations which ignore momentum unless a variable slice was actually used).


Optimizer that implements the Adamax algorithm.
Inherits From: Optimizer
Main aliases
tf.optimizers.Adamax
See Migration guide for more details.
tf.compat.v1.keras.optimizers.Adamax
It is a variant of Adam based on the infinity norm. Default parameters follow those provided in the paper. Adamax is sometimes superior to adam, specially in models with embeddings.
The update rule for parameter w with gradient g is described at the end of section 7.1 of the paper:
Similarly to Adam, the epsilon is added for numerical stability (especially to get rid of division by zero when v_t == 0).
In contrast to Adam, the sparse implementation of this algorithm (used when the gradient is an IndexedSlices object, typically because of tf.gather or an embedding lookup in the forward pass) only updates variable slices and corresponding m_t, v_t terms when that part of the variable was used in the forward pass. This means that the sparse behavior is contrast to the dense behavior (similar to some momentum implementations which ignore momentum unless a variable slice was actually used).


Inverse of the serialize function.
Main aliases
tf.optimizers.deserialize
See Migration guide for more details.
tf.compat.v1.keras.optimizers.deserialize


Optimizer that implements the FTRL algorithm.
Inherits From: Optimizer
Main aliases
tf.optimizers.Ftrl
See Migration guide for more details.
tf.compat.v1.keras.optimizers.Ftrl
See Algorithm 1 of this paper. This version has support for both online L2 (the L2 penalty given in the paper above) and shrinkage-type L2 (which is the addition of an L2 penalty to the loss function).
Update (
is variable index,
is the learning rate):
Check the documentation for the l2_shrinkage_regularization_strength parameter for more details when shrinkage is enabled, in which case gradient is replaced with gradient_with_shrinkage.


Retrieves a Keras Optimizer instance.
Main aliases
tf.optimizers.get
See Migration guide for more details.
tf.compat.v1.keras.optimizers.get


Optimizer that implements the NAdam algorithm.
Inherits From: Optimizer
Main aliases
tf.optimizers.Nadam
See Migration guide for more details.
tf.compat.v1.keras.optimizers.Nadam
Much like Adam is essentially RMSprop with momentum, Nadam is Adam with Nesterov momentum.


Base class for Keras optimizers.
Main aliases
tf.optimizers.Optimizer
See Migration guide for more details.
tf.compat.v1.keras.optimizers.Optimizer
You should not use this class directly, but instead instantiate one of its subclasses such as tf.keras.optimizers.SGD, tf.keras.optimizers.Adam, etc.
In Keras models, sometimes variables are created when the model is first called, instead of construction time. Examples include 1) sequential models without input shape pre-defined, or 2) subclassed models. Pass var_list as callable in these cases.
Calling minimize() takes care of both computing the gradients and applying them to the variables. If you want to process the gradients before applying them you can instead use the optimizer in three steps:
This optimizer class is tf.distribute.Strategy aware, which means it automatically sums gradients across all replicas. To average gradients, you divide your loss by the global batch size, which is done automatically if you use tf.keras built-in training or evaluation loops. See the reduction argument of your loss which should be set to tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE for averaging or tf.keras.losses.Reduction.SUM for not.
To aggregate gradients yourself, call apply_gradients with experimental_aggregate_gradients set to False. This is useful if you need to process aggregated gradients.
If you are not using these and you want to average gradients, you should use tf.math.reduce_sum to add up your per-example losses and then divide by the global batch size. Note that when using tf.distribute.Strategy, the first component of a tensor's shape is the replica-local batch size, which is off by a factor equal to the number of replicas being used to compute a single step. As a result, using tf.math.reduce_mean will give the wrong answer, resulting in gradients that can be many times too big.
All Keras optimizers respect variable constraints. If constraint function is passed to any variable, the constraint will be applied to the variable after the gradient has been applied to the variable. Important: If gradient is sparse tensor, variable constraint is not supported.
The entire optimizer is currently thread compatible, not thread-safe. The user needs to perform synchronization if necessary.
Many optimizer subclasses, such as Adam and Adagrad allocate and manage additional variables associated with the variables to train. These are called Slots. Slots have names and you can ask the optimizer for the names of the slots that it uses. Once you have a slot name you can ask the optimizer for the variable it created to hold the slot value.
This can be useful if you want to log debug a training algorithm, report stats about the slots, etc.
These are arguments passed to the optimizer subclass constructor (the __init__ method), and then passed to self._set_hyper(). They can be either regular Python values (like 1.0), tensors, or callables. If they are callable, the callable will be called during apply_gradients() to get the value for the hyper parameter.
Hyperparameters can be overwritten through user code:
Optimizer accepts a callable learning rate in two ways. The first way is through built-in or customized tf.keras.optimizers.schedules.LearningRateSchedule. The schedule will be called on each iteration with schedule(iteration), a tf.Variable owned by the optimizer.
The second way is through a callable function that does not accept any arguments.
If you intend to create your own optimization algorithm, simply inherit from this class and override the following methods:
View source
Add a new slot variable for var.
View source
View source
Apply gradients to variables.
This is the second part of minimize(). It returns an Operation that applies gradients.
The method sums gradients from all replicas in the presence of tf.distribute.Strategy by default. You can aggregate gradients yourself by passing experimental_aggregate_gradients=False.
View source
Creates an optimizer from its config.
This method is the reverse of get_config, capable of instantiating the same optimizer from the config dictionary.
View source
Returns the config of the optimizer.
An optimizer config is a Python dictionary (serializable) containing the configuration of an optimizer. The same optimizer can be reinstantiated later (without any saved state) from this configuration.
View source
Returns gradients of loss with respect to params.
Should be used only in legacy v1 graph mode.
View source
View source
A list of names for this optimizer's slots.
View source
View source
Returns the current weights of the optimizer.
The weights of an optimizer are its state (ie, variables). This function returns the weight values associated with this optimizer as a list of Numpy arrays. The first value is always the iterations count of the optimizer, followed by the optimizer's state variables in the order they were created. The returned list can in turn be used to load state into similarly parameterized optimizers.
For example, the RMSprop optimizer for this simple model returns a list of three values-- the iteration count, followed by the root-mean-square value of the kernel and bias of the single Dense layer:
View source
Minimize loss by updating var_list.
This method simply computes gradient using tf.GradientTape and calls apply_gradients(). If you want to process the gradient before applying then call tf.GradientTape and apply_gradients() explicitly instead of using this function.
View source
Set the weights of the optimizer.
The weights of an optimizer are its state (ie, variables). This function takes the weight values associated with this optimizer as a list of Numpy arrays. The first value is always the iterations count of the optimizer, followed by the optimizer's state variables in the order they are created. The passed values are used to set the new state of the optimizer.
For example, the RMSprop optimizer for this simple model takes a list of three values-- the iteration count, followed by the root-mean-square value of the kernel and bias of the single Dense layer:
View source
Returns variables of this Optimizer based on the order created.


Optimizer that implements the RMSprop algorithm.
Inherits From: Optimizer
Main aliases
tf.optimizers.RMSprop
See Migration guide for more details.
tf.compat.v1.keras.optimizers.RMSprop
The gist of RMSprop is to:
This implementation of RMSprop uses plain momentum, not Nesterov momentum.
The centered version additionally maintains a moving average of the gradients, and uses that average to estimate the variance.
Note that in the dense implementation of this algorithm, variables and their corresponding accumulators (momentum, gradient moving average, square gradient moving average) will be updated even if the gradient is zero (i.e. accumulators will decay, momentum will be applied). The sparse implementation (used when the gradient is an IndexedSlices object, typically because of tf.gather or an embedding lookup in the forward pass) will not update variable slices or their accumulators unless those slices were used in the forward pass (nor is there an "eventual" correction to account for these omitted updates). This leads to more efficient updates for large embedding lookup tables (where most of the slices are not accessed in a particular graph execution), but differs from the published algorithm.


Public API for tf.keras.optimizers.schedules namespace.
Main aliases
tf.optimizers.schedules
class ExponentialDecay: A LearningRateSchedule that uses an exponential decay schedule.
class InverseTimeDecay: A LearningRateSchedule that uses an inverse time decay schedule.
class LearningRateSchedule: A serializable learning rate decay schedule.
class PiecewiseConstantDecay: A LearningRateSchedule that uses a piecewise constant decay schedule.
class PolynomialDecay: A LearningRateSchedule that uses a polynomial decay schedule.
deserialize(...)
serialize(...)


Main aliases
tf.optimizers.schedules.deserialize
See Migration guide for more details.
tf.compat.v1.keras.optimizers.schedules.deserialize


A LearningRateSchedule that uses an exponential decay schedule.
Inherits From: LearningRateSchedule
Main aliases
tf.optimizers.schedules.ExponentialDecay
See Migration guide for more details.
tf.compat.v1.keras.optimizers.schedules.ExponentialDecay
When training a model, it is often recommended to lower the learning rate as the training progresses. This schedule applies an exponential decay function to an optimizer step, given a provided initial learning rate.
The schedule a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as:
If the argument staircase is True, then step / decay_steps is an integer division and the decayed learning rate follows a staircase function.
You can pass this schedule directly into a tf.keras.optimizers.Optimizer as the learning rate. Example: When fitting a Keras model, decay every 100000 steps with a base of 0.96:
The learning rate schedule is also serializable and deserializable using tf.keras.optimizers.schedules.serialize and tf.keras.optimizers.schedules.deserialize.
View source
Instantiates a LearningRateSchedule from its config.
View source
View source
Call self as a function.


A LearningRateSchedule that uses an inverse time decay schedule.
Inherits From: LearningRateSchedule
Main aliases
tf.optimizers.schedules.InverseTimeDecay
See Migration guide for more details.
tf.compat.v1.keras.optimizers.schedules.InverseTimeDecay
When training a model, it is often recommended to lower the learning rate as the training progresses. This schedule applies the inverse decay function to an optimizer step, given a provided initial learning rate. It requires a step value to compute the decayed learning rate. You can just pass a TensorFlow variable that you increment at each training step.
The schedule a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as:
or, if staircase is True, as:
You can pass this schedule directly into a tf.keras.optimizers.Optimizer as the learning rate. Example: Fit a Keras model when decaying 1/t with a rate of 0.5:
View source
Instantiates a LearningRateSchedule from its config.
View source
View source
Call self as a function.


A serializable learning rate decay schedule.
Main aliases
tf.optimizers.schedules.LearningRateSchedule
See Migration guide for more details.
tf.compat.v1.keras.optimizers.schedules.LearningRateSchedule
LearningRateSchedules can be passed in as the learning rate of optimizers in tf.keras.optimizers. They can be serialized and deserialized using tf.keras.optimizers.schedules.serialize and tf.keras.optimizers.schedules.deserialize.
View source
Instantiates a LearningRateSchedule from its config.
View source
View source
Call self as a function.


A LearningRateSchedule that uses a piecewise constant decay schedule.
Inherits From: LearningRateSchedule
Main aliases
tf.optimizers.schedules.PiecewiseConstantDecay
See Migration guide for more details.
tf.compat.v1.keras.optimizers.schedules.PiecewiseConstantDecay
The function returns a 1-arg callable to compute the piecewise constant when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions.
Example: use a learning rate that's 1.0 for the first 100001 steps, 0.5 for the next 10000 steps, and 0.1 for any additional steps.
You can pass this schedule directly into a tf.keras.optimizers.Optimizer as the learning rate. The learning rate schedule is also serializable and deserializable using tf.keras.optimizers.schedules.serialize and tf.keras.optimizers.schedules.deserialize.
The output of the 1-arg function that takes the step is values[0] when step <= boundaries[0], values[1] when step > boundaries[0] and step <= boundaries[1], ..., and values[-1] when step > boundaries[-1].
View source
Instantiates a LearningRateSchedule from its config.
View source
View source
Call self as a function.


A LearningRateSchedule that uses a polynomial decay schedule.
Inherits From: LearningRateSchedule
Main aliases
tf.optimizers.schedules.PolynomialDecay
See Migration guide for more details.
tf.compat.v1.keras.optimizers.schedules.PolynomialDecay
It is commonly observed that a monotonically decreasing learning rate, whose degree of change is carefully chosen, results in a better performing model. This schedule applies a polynomial decay function to an optimizer step, given a provided initial_learning_rate, to reach an end_learning_rate in the given decay_steps.
It requires a step value to compute the decayed learning rate. You can just pass a TensorFlow variable that you increment at each training step.
The schedule is a 1-arg callable that produces a decayed learning rate when passed the current optimizer step. This can be useful for changing the learning rate value across different invocations of optimizer functions. It is computed as:
If cycle is True then a multiple of decay_steps is used, the first one that is bigger than step.
You can pass this schedule directly into a tf.keras.optimizers.Optimizer as the learning rate. Example: Fit a model while decaying from 0.1 to 0.01 in 10000 steps using sqrt (i.e. power=0.5):
The learning rate schedule is also serializable and deserializable using tf.keras.optimizers.schedules.serialize and tf.keras.optimizers.schedules.deserialize.
View source
Instantiates a LearningRateSchedule from its config.
View source
View source
Call self as a function.


Main aliases
tf.optimizers.schedules.serialize
See Migration guide for more details.
tf.compat.v1.keras.optimizers.schedules.serialize


Main aliases
tf.optimizers.serialize
See Migration guide for more details.
tf.compat.v1.keras.optimizers.serialize


Gradient descent (with momentum) optimizer.
Inherits From: Optimizer
Main aliases
tf.optimizers.SGD
See Migration guide for more details.
tf.compat.v1.keras.optimizers.SGD
Update rule for parameter w with gradient g when momentum is 0:
Update rule when momentum is larger than 0:
When nesterov=True, this rule becomes:


Keras data preprocessing utils.
image module: Set of tools for real-time data augmentation on image data.
sequence module: Utilities for preprocessing sequence data.
text module: Utilities for text input preprocessing.
image_dataset_from_directory(...): Generates a tf.data.Dataset from image files in a directory.
text_dataset_from_directory(...): Generates a tf.data.Dataset from text files in a directory.
timeseries_dataset_from_array(...): Creates a dataset of sliding windows over a timeseries provided as array.


Set of tools for real-time data augmentation on image data.
class DirectoryIterator: Iterator capable of reading images from a directory on disk.
class ImageDataGenerator: Generate batches of tensor image data with real-time data augmentation.
class Iterator: Base class for image data iterators.
class NumpyArrayIterator: Iterator yielding data from a Numpy array.
apply_affine_transform(...): Applies an affine transformation specified by the parameters given.
apply_brightness_shift(...): Performs a brightness shift.
apply_channel_shift(...): Performs a channel shift.
array_to_img(...): Converts a 3D Numpy array to a PIL Image instance.
img_to_array(...): Converts a PIL Image instance to a Numpy array.
load_img(...): Loads an image into PIL format.
random_brightness(...): Performs a random brightness shift.
random_channel_shift(...): Performs a random channel shift.
random_rotation(...): Performs a random rotation of a Numpy image tensor.
random_shear(...): Performs a random spatial shear of a Numpy image tensor.
random_shift(...): Performs a random spatial shift of a Numpy image tensor.
random_zoom(...): Performs a random spatial zoom of a Numpy image tensor.
save_img(...): Saves an image stored as a Numpy array to a path or file object.
smart_resize(...): Resize images to a target size without aspect ratio distortion.


Applies an affine transformation specified by the parameters given.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.apply_affine_transform


Performs a brightness shift.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.apply_brightness_shift


Performs a channel shift.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.apply_channel_shift


Converts a 3D Numpy array to a PIL Image instance.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.array_to_img


Iterator capable of reading images from a directory on disk.
Inherits From: Iterator, Sequence
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.DirectoryIterator
View source
For python 2.x.
View source
View source
View source
Sets attributes to use later for processing files into a batch.
View source
View source
View source


Generate batches of tensor image data with real-time data augmentation.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.ImageDataGenerator
The data will be looped over (in batches).
Example of using .flow(x, y):
Example of using .flow_from_directory(directory):
Example of transforming images and masks together.
View source
Applies a transformation to an image according to given parameters.
View source
Fits the data generator to some sample data.
This computes the internal data stats related to the data-dependent transformations, based on an array of sample data.
Only required if featurewise_center or featurewise_std_normalization or zca_whitening are set to True.
When rescale is set to a value, rescaling is applied to sample data before computing the internal data stats.
View source
Takes data & label arrays, generates batches of augmented data.
View source
Takes the dataframe and the path to a directory + generates batches.
The generated batches contain augmented/normalized data.
**A simple tutorial can be found **here.
View source
Takes the path to a directory & generates batches of augmented data.
View source
Generates random parameters for a transformation.
View source
Applies a random transformation to an image.
View source
Applies the normalization configuration in-place to a batch of inputs.
x is changed in-place since the function is mainly used internally to standardize images and feed them to your network. If a copy of x would be created instead it would have a significant performance cost. If you want to apply this method without changing the input in-place you can call the method creating a copy before:
standardize(np.copy(x))


Converts a PIL Image instance to a Numpy array.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.img_to_array


Base class for image data iterators.
Inherits From: Sequence
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.Iterator
Every Iterator must implement the _get_batches_of_transformed_samples method.
View source
For python 2.x.
View source
View source
View source
View source
View source


Loads an image into PIL format.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.load_img


Iterator yielding data from a Numpy array.
Inherits From: Iterator, Sequence
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.NumpyArrayIterator
View source
For python 2.x.
View source
View source
View source
View source
View source


Performs a random brightness shift.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.random_brightness


Performs a random channel shift.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.random_channel_shift


Performs a random rotation of a Numpy image tensor.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.random_rotation


Performs a random spatial shear of a Numpy image tensor.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.random_shear


Performs a random spatial shift of a Numpy image tensor.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.random_shift


Performs a random spatial zoom of a Numpy image tensor.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.random_zoom


Saves an image stored as a Numpy array to a path or file object.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.image.save_img


Resize images to a target size without aspect ratio distortion.
TensorFlow image datasets typically yield images that have each a different size. However, these images need to be batched before they can be processed by Keras layers. To be batched, images need to share the same height and width.


Generates a tf.data.Dataset from image files in a directory.
If your directory structure is:
Then calling image_dataset_from_directory(main_directory, labels='inferred') will return a tf.data.Dataset that yields batches of images from the subdirectories class_a and class_b, together with labels 0 and 1 (0 corresponding to class_a and 1 corresponding to class_b).
Supported image formats: jpeg, png, bmp, gif. Animated gifs are truncated to the first frame.
Rules regarding labels format:
Rules regarding number of channels in the yielded images:


Utilities for preprocessing sequence data.
class TimeseriesGenerator: Utility class for generating batches of temporal data.
make_sampling_table(...): Generates a word rank-based probabilistic sampling table.
pad_sequences(...): Pads sequences to the same length.
skipgrams(...): Generates skipgram word pairs.


Generates a word rank-based probabilistic sampling table.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.sequence.make_sampling_table
Used for generating the sampling_table argument for skipgrams. sampling_table[i] is the probability of sampling the word i-th most common word in a dataset (more common words should be sampled less frequently, for balance).
The sampling probabilities are generated according to the sampling distribution used in word2vec:
We assume that the word frequencies follow Zipf's law (s=1) to derive a numerical approximation of frequency(rank):
frequency(rank) ~ 1/(rank * (log(rank) + gamma) + 1/2 - 1/(12*rank)) where gamma is the Euler-Mascheroni constant.


Pads sequences to the same length.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.sequence.pad_sequences
This function transforms a list (of length num_samples) of sequences (lists of integers) into a 2D Numpy array of shape (num_samples, num_timesteps). num_timesteps is either the maxlen argument if provided, or the length of the longest sequence in the list.
Sequences that are shorter than num_timesteps are padded with value until they are num_timesteps long.
Sequences longer than num_timesteps are truncated so that they fit the desired length.
The position where padding or truncation happens is determined by the arguments padding and truncating, respectively. Pre-padding or removing values from the beginning of the sequence is the default.


Generates skipgram word pairs.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.sequence.skipgrams
This function transforms a sequence of word indexes (list of integers) into tuples of words of the form:
Read more about Skipgram in this gnomic paper by Mikolov et al.: Efficient Estimation of Word Representations in Vector Space
By convention, index 0 in the vocabulary is a non-word and will be skipped.


Utility class for generating batches of temporal data.
Inherits From: Sequence
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.sequence.TimeseriesGenerator
This class takes in a sequence of data-points gathered at equal intervals, along with time series parameters such as stride, length of history, etc., to produce batches for training/validation. Arguments: data: Indexable generator (such as list or Numpy array) containing consecutive data points (timesteps). The data should be at 2D, and axis 0 is expected to be the time dimension. targets: Targets corresponding to timesteps in data. It should have same length as data. length: Length of the output sequences (in number of timesteps). sampling_rate: Period between successive individual timesteps within sequences. For rate r, timesteps data[i], data[i-r], ... data[i - length] are used for create a sample sequence. stride: Period between successive output sequences. For stride s, consecutive output samples would be centered around data[i], data[i+s], data[i+2*s], etc. start_index: Data points earlier than start_index will not be used in the output sequences. This is useful to reserve part of the data for test or validation. end_index: Data points later than end_index will not be used in the output sequences. This is useful to reserve part of the data for test or validation. shuffle: Whether to shuffle output samples, or instead draw them in chronological order. reverse: Boolean: if true, timesteps in each output sample will be in reverse chronological order. batch_size: Number of timeseries samples in each batch (except maybe the last one). Returns: A Sequence instance. Examples:
View source
Returns the TimeseriesGenerator configuration as Python dictionary.
View source
Method called at the end of every epoch.
View source
Returns a JSON string containing the timeseries generator configuration. To load a generator from a JSON string, use keras.preprocessing.sequence.timeseries_generator_from_json(json_string).
View source
View source
Create a generator that iterate over the Sequence.
View source


Utilities for text input preprocessing.
class Tokenizer: Text tokenization utility class.
hashing_trick(...): Converts a text to a sequence of indexes in a fixed-size hashing space.
one_hot(...): One-hot encodes a text into a list of word indexes of size n.
text_to_word_sequence(...): Converts a text to a sequence of words (or tokens).
tokenizer_from_json(...): Parses a JSON tokenizer configuration file and returns a


Converts a text to a sequence of indexes in a fixed-size hashing space.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.text.hashing_trick
0 is a reserved index that won't be assigned to any word.
Two or more words may be assigned to the same index, due to possible collisions by the hashing function. The probability of a collision is in relation to the dimension of the hashing space and the number of distinct objects.


One-hot encodes a text into a list of word indexes of size n.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.text.one_hot
This function receives as input a string of text and returns a list of encoded integers each corresponding to a word (or token) in the given input string.


Converts a text to a sequence of words (or tokens).
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.text.text_to_word_sequence
This function transforms a string of text into a list of words while ignoring filters which include punctuations by default.


Text tokenization utility class.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.text.Tokenizer
This class allows to vectorize a text corpus, by turning each text into either a sequence of integers (each integer being the index of a token in a dictionary) or into a vector where the coefficient for each token could be binary, based on word count, based on tf-idf...
By default, all punctuation is removed, turning the texts into space-separated sequences of words (words maybe include the ' character). These sequences are then split into lists of tokens. They will then be indexed or vectorized.
0 is a reserved index that won't be assigned to any word.
View source
Updates internal vocabulary based on a list of sequences.
Required before using sequences_to_matrix (if fit_on_texts was never called).
View source
Updates internal vocabulary based on a list of texts.
In the case where texts contains lists, we assume each entry of the lists to be a token.
Required before using texts_to_sequences or texts_to_matrix.
View source
Returns the tokenizer configuration as Python dictionary. The word count dictionaries used by the tokenizer get serialized into plain JSON, so that the configuration can be read by other projects.
View source
Converts a list of sequences into a Numpy matrix.
View source
Transforms each sequence into a list of text.
Only top num_words-1 most frequent words will be taken into account. Only words known by the tokenizer will be taken into account.
View source
Transforms each sequence in sequences to a list of texts(strings).
Each sequence has to a list of integers. In other words, sequences should be a list of sequences
Only top num_words-1 most frequent words will be taken into account. Only words known by the tokenizer will be taken into account.
View source
Convert a list of texts to a Numpy matrix.
View source
Transforms each text in texts to a sequence of integers.
Only top num_words-1 most frequent words will be taken into account. Only words known by the tokenizer will be taken into account.
View source
Transforms each text in texts to a sequence of integers.
Each item in texts can also be a list, in which case we assume each item of that list to be a token.
Only top num_words-1 most frequent words will be taken into account. Only words known by the tokenizer will be taken into account.
View source
Returns a JSON string containing the tokenizer configuration. To load a tokenizer from a JSON string, use keras.preprocessing.text.tokenizer_from_json(json_string).


Parses a JSON tokenizer configuration file and returns a
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.preprocessing.text.tokenizer_from_json
tokenizer instance.


Generates a tf.data.Dataset from text files in a directory.
If your directory structure is:
Then calling text_dataset_from_directory(main_directory, labels='inferred') will return a tf.data.Dataset that yields batches of texts from the subdirectories class_a and class_b, together with labels 0 and 1 (0 corresponding to class_a and 1 corresponding to class_b).
Only .txt files are supported at this time.
Rules regarding labels format:


Creates a dataset of sliding windows over a timeseries provided as array.
This function takes in a sequence of data-points gathered at equal intervals, along with time series parameters such as length of the sequences/windows, spacing between two sequence/windows, etc., to produce batches of timeseries inputs and targets.
Consider indices [0, 1, ... 99]. With sequence_length=10, sampling_rate=2, sequence_stride=3, shuffle=False, the dataset will yield batches of sequences composed of the following indices:
In this case the last 3 data points are discarded since no full sequence can be generated to include them (the next sequence would have started at index 81, and thus its last step would have gone over 99).
Example 2: temporal regression. Consider an array data of scalar values, of shape (steps,). To generate a dataset that uses the past 10 timesteps to predict the next timestep, you would use:


Built-in regularizers.
class L1: A regularizer that applies a L1 regularization penalty.
class L1L2: A regularizer that applies both L1 and L2 regularization penalties.
class L2: A regularizer that applies a L2 regularization penalty.
class Regularizer: Regularizer base class.
class l1: A regularizer that applies a L1 regularization penalty.
class l2: A regularizer that applies a L2 regularization penalty.
deserialize(...)
get(...): Retrieve a regularizer instance from a config or identifier.
l1_l2(...): Create a regularizer that applies both L1 and L2 penalties.
serialize(...)


Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.regularizers.deserialize


Retrieve a regularizer instance from a config or identifier.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.regularizers.get


A regularizer that applies a L1 regularization penalty.
Inherits From: Regularizer
Main aliases
tf.keras.regularizers.l1
See Migration guide for more details.
tf.compat.v1.keras.regularizers.L1, tf.compat.v1.keras.regularizers.l1
The L1 regularization penalty is computed as: loss = l1 * reduce_sum(abs(x))
L1 may be passed to a layer as a string identifier:
In this case, the default value used is l1=0.01.
View source
Creates a regularizer from its config.
This method is the reverse of get_config, capable of instantiating the same regularizer from the config dictionary.
This method is used by Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Returns the config of the regularizer.
An regularizer config is a Python dictionary (serializable) containing all configuration parameters of the regularizer. The same regularizer can be reinstantiated later (without any saved state) from this configuration.
This method is optional if you are just training and executing models, exporting to and from SavedModels, or using weight checkpoints.
This method is required for Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Compute a regularization penalty from an input tensor.


A regularizer that applies both L1 and L2 regularization penalties.
Inherits From: Regularizer
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.regularizers.L1L2
The L1 regularization penalty is computed as: loss = l1 * reduce_sum(abs(x))
The L2 regularization penalty is computed as loss = l2 * reduce_sum(square(x))
L1L2 may be passed to a layer as a string identifier:
In this case, the default values used are l1=0.01 and l2=0.01.
View source
Creates a regularizer from its config.
This method is the reverse of get_config, capable of instantiating the same regularizer from the config dictionary.
This method is used by Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Returns the config of the regularizer.
An regularizer config is a Python dictionary (serializable) containing all configuration parameters of the regularizer. The same regularizer can be reinstantiated later (without any saved state) from this configuration.
This method is optional if you are just training and executing models, exporting to and from SavedModels, or using weight checkpoints.
This method is required for Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Compute a regularization penalty from an input tensor.


Create a regularizer that applies both L1 and L2 penalties.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.regularizers.l1_l2
The L1 regularization penalty is computed as: loss = l1 * reduce_sum(abs(x))
The L2 regularization penalty is computed as: loss = l2 * reduce_sum(square(x))


A regularizer that applies a L2 regularization penalty.
Inherits From: Regularizer
Main aliases
tf.keras.regularizers.l2
See Migration guide for more details.
tf.compat.v1.keras.regularizers.L2, tf.compat.v1.keras.regularizers.l2
The L2 regularization penalty is computed as: loss = l2 * reduce_sum(square(x))
L2 may be passed to a layer as a string identifier:
In this case, the default value used is l2=0.01.
View source
Creates a regularizer from its config.
This method is the reverse of get_config, capable of instantiating the same regularizer from the config dictionary.
This method is used by Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Returns the config of the regularizer.
An regularizer config is a Python dictionary (serializable) containing all configuration parameters of the regularizer. The same regularizer can be reinstantiated later (without any saved state) from this configuration.
This method is optional if you are just training and executing models, exporting to and from SavedModels, or using weight checkpoints.
This method is required for Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Compute a regularization penalty from an input tensor.


Regularizer base class.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.regularizers.Regularizer
Regularizers allow you to apply penalties on layer parameters or layer activity during optimization. These penalties are summed into the loss function that the network optimizes.
Regularization penalties are applied on a per-layer basis. The exact API will depend on the layer, but many layers (e.g. Dense, Conv1D, Conv2D and Conv3D) have a unified API.
These layers expose 3 keyword arguments:
All layers (including custom layers) expose activity_regularizer as a settable property, whether or not it is in the constructor arguments.
The value returned by the activity_regularizer is divided by the input batch size so that the relative weighting between the weight regularizers and the activity regularizers does not change with the batch size.
You can access a layer's regularization penalties by calling layer.losses after calling the layer on inputs.
Compute a regularization loss on a tensor by directly calling a regularizer as if it is a one-argument function.
E.g.
Any function that takes in a weight matrix and returns a scalar tensor can be used as a regularizer, e.g.:
Alternatively, you can write your custom regularizers in an object-oriented way by extending this regularizer base class, e.g.:
Registering the regularizers as serializable is optional if you are just training and executing models, exporting to and from SavedModels, or saving and loading weight checkpoints.
Registration is required for Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON. If using this functionality, you must make sure any python process running your model has also defined and registered your custom regularizer.
tf.keras.utils.register_keras_serializable is only available in TF 2.1 and beyond. In earlier versions of TensorFlow you must pass your custom regularizer to the custom_objects argument of methods that expect custom regularizers to be registered as serializable.
View source
Creates a regularizer from its config.
This method is the reverse of get_config, capable of instantiating the same regularizer from the config dictionary.
This method is used by Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Returns the config of the regularizer.
An regularizer config is a Python dictionary (serializable) containing all configuration parameters of the regularizer. The same regularizer can be reinstantiated later (without any saved state) from this configuration.
This method is optional if you are just training and executing models, exporting to and from SavedModels, or using weight checkpoints.
This method is required for Keras model_to_estimator, saving and loading models to HDF5 formats, Keras model cloning, some visualization utilities, and exporting models to and from JSON.
View source
Compute a regularization penalty from an input tensor.


Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.regularizers.serialize


Sequential groups a linear stack of layers into a tf.keras.Model.
Inherits From: Model, Layer, Module
Main aliases
tf.keras.models.Sequential
See Migration guide for more details.
tf.compat.v1.keras.Sequential, tf.compat.v1.keras.models.Sequential
Sequential provides training and inference features on this model.
Running eagerly means that your model will be run step by step, like Python code. Your model might run slower, but it should become easier for you to debug it by stepping into individual layer calls.
By default, we will attempt to compile your model to a static graph to deliver the best execution performance.
View source
Adds a layer instance on top of the layer stack.
View source
Configures the model for training.
View source
Returns the loss value & metrics values for the model in test mode.
Computation is done in batches (see the batch_size arg.)
See the discussion of Unpacking behavior for iterator-like inputs for Model.fit.
View source
Evaluates the model on a data generator.
Model.evaluate now supports generators, so there is no longer any need to use this endpoint.
View source
Trains the model for a fixed number of epochs (iterations on a dataset).
Unpacking behavior for iterator-like inputs: A common pattern is to pass a tf.data.Dataset, generator, or tf.keras.utils.Sequence to the x argument of fit, which will in fact yield not only features (x) but optionally targets (y) and sample weights. Keras requires that the output of such iterator-likes be unambiguous. The iterator should return a tuple of length 1, 2, or 3, where the optional second and third elements will be used for y and sample_weight respectively. Any other type provided will be wrapped in a length one tuple, effectively treating everything as 'x'. When yielding dicts, they should still adhere to the top-level tuple structure. e.g. ({"x0": x0, "x1": x1}, y). Keras will not attempt to separate features, targets, and weights from the keys of a single dict. A notable unsupported data type is the namedtuple. The reason is that it behaves like both an ordered datatype (tuple) and a mapping datatype (dict). So given a namedtuple of the form: namedtuple("example_tuple", ["y", "x"]) it is ambiguous whether to reverse the order of the elements when interpreting the value. Even worse is a tuple of the form: namedtuple("other_tuple", ["x", "y", "z"]) where it is unclear if the tuple was intended to be unpacked into x, y, and sample_weight or passed through as a single element to x. As a result the data processing code will simply raise a ValueError if it encounters a namedtuple. (Along with instructions to remedy the issue.)
View source
Fits the model on data yielded batch-by-batch by a Python generator.
Model.fit now supports generators, so there is no longer any need to use this endpoint.
View source
Retrieves a layer based on either its name (unique) or index.
If name and index are both provided, index will take precedence. Indices are based on order of horizontal graph traversal (bottom-up).
View source
Loads all layer weights, either from a TensorFlow or an HDF5 weight file.
If by_name is False weights are loaded based on the network's topology. This means the architecture should be the same as when the weights were saved. Note that layers that don't have weights are not taken into account in the topological ordering, so adding or removing layers is fine as long as they don't have weights.
If by_name is True, weights are loaded into layers only if they share the same name. This is useful for fine-tuning or transfer-learning models where some of the layers have changed.
Only topological loading (by_name=False) is supported when loading weights from the TensorFlow format. Note that topological loading differs slightly between TensorFlow and HDF5 formats for user-defined classes inheriting from tf.keras.Model: HDF5 loads based on a flattened list of weights, while the TensorFlow format loads based on the object-local names of attributes to which layers are assigned in the Model's constructor.
When loading weights in HDF5 format, returns None.
View source
Creates a function that executes one step of inference.
This method can be overridden to support custom inference logic. This method is called by Model.predict and Model.predict_on_batch.
Typically, this method directly controls tf.function and tf.distribute.Strategy settings, and delegates the actual evaluation logic to Model.predict_step.
This function is cached the first time Model.predict or Model.predict_on_batch is called. The cache is cleared whenever Model.compile is called.
View source
Creates a function that executes one step of evaluation.
This method can be overridden to support custom evaluation logic. This method is called by Model.evaluate and Model.test_on_batch.
Typically, this method directly controls tf.function and tf.distribute.Strategy settings, and delegates the actual evaluation logic to Model.test_step.
This function is cached the first time Model.evaluate or Model.test_on_batch is called. The cache is cleared whenever Model.compile is called.
View source
Creates a function that executes one step of training.
This method can be overridden to support custom training logic. This method is called by Model.fit and Model.train_on_batch.
Typically, this method directly controls tf.function and tf.distribute.Strategy settings, and delegates the actual training logic to Model.train_step.
This function is cached the first time Model.fit or Model.train_on_batch is called. The cache is cleared whenever Model.compile is called.
View source
Removes the last layer in the model.
View source
Generates output predictions for the input samples.
Computation is done in batches. This method is designed for performance in large scale inputs. For small amount of inputs that fit in one batch, directly using __call__ is recommended for faster execution, e.g., model(x), or model(x, training=False) if you have layers such as tf.keras.layers.BatchNormalization that behaves differently during inference. Also, note the fact that test loss is not affected by regularization layers like noise and dropout.
See the discussion of Unpacking behavior for iterator-like inputs for Model.fit. Note that Model.predict uses the same interpretation rules as Model.fit and Model.evaluate, so inputs must be unambiguous for all three methods.
View source
Generate class predictions for the input samples.
The input samples are processed batch by batch.
View source
Generates predictions for the input samples from a data generator.
Model.predict now supports generators, so there is no longer any need to use this endpoint.
View source
Returns predictions for a single batch of samples.
View source
Generates class probability predictions for the input samples.
The input samples are processed batch by batch.
View source
The logic for one inference step.
This method can be overridden to support custom inference logic. This method is called by Model.make_predict_function.
This method should contain the mathematical logic for one step of inference. This typically includes the forward pass.
Configuration details for how this logic is run (e.g. tf.function and tf.distribute.Strategy settings), should be left to Model.make_predict_function, which can also be overridden.
View source
Resets the state of all the metrics in the model.
View source
View source
Saves the model to Tensorflow SavedModel or a single HDF5 file.
Please see tf.keras.models.save_model or the Serialization and Saving guide for details.
View source
Saves all layer weights.
Either saves in HDF5 or in TensorFlow format based on the save_format argument.
When saving in HDF5 format, the weight file has:
When saving in TensorFlow format, all objects referenced by the network are saved in the same format as tf.train.Checkpoint, including any Layer instances or Optimizer instances assigned to object attributes. For networks constructed from inputs and outputs using tf.keras.Model(inputs, outputs), Layer instances used by the network are tracked/saved automatically. For user-defined classes which inherit from tf.keras.Model, Layer instances must be assigned to object attributes, typically in the constructor. See the documentation of tf.train.Checkpoint and tf.keras.Model for details.
While the formats are the same, do not mix save_weights and tf.train.Checkpoint. Checkpoints saved by Model.save_weights should be loaded using Model.load_weights. Checkpoints saved using tf.train.Checkpoint.save should be restored using the corresponding tf.train.Checkpoint.restore. Prefer tf.train.Checkpoint over save_weights for training checkpoints.
The TensorFlow format matches objects and variables by starting at a root object, self for save_weights, and greedily matching attribute names. For Model.save this is the Model, and for Checkpoint.save this is the Checkpoint even if the Checkpoint has a model attached. This means saving a tf.keras.Model using save_weights and loading into a tf.train.Checkpoint with a Model attached (or vice versa) will not match the Model's variables. See the guide to training checkpoints for details on the TensorFlow format.
View source
Prints a string summary of the network.
View source
Test the model on a single batch of samples.
View source
The logic for one evaluation step.
This method can be overridden to support custom evaluation logic. This method is called by Model.make_test_function.
This function should contain the mathematical logic for one step of evaluation. This typically includes the forward pass, loss calculation, and metrics updates.
Configuration details for how this logic is run (e.g. tf.function and tf.distribute.Strategy settings), should be left to Model.make_test_function, which can also be overridden.
View source
Returns a JSON string containing the network configuration.
To load a network from a JSON save file, use keras.models.model_from_json(json_string, custom_objects={}).
View source
Returns a yaml string containing the network configuration.
To load a network from a yaml save file, use keras.models.model_from_yaml(yaml_string, custom_objects={}).
custom_objects should be a dictionary mapping the names of custom losses / layers / etc to the corresponding functions / classes.
View source
Runs a single gradient update on a single batch of data.
View source
The logic for one training step.
This method can be overridden to support custom training logic. This method is called by Model.make_train_function.
This method should contain the mathematical logic for one step of training. This typically includes the forward pass, loss calculation, backpropagation, and metric updates.
Configuration details for how this logic is run (e.g. tf.function and tf.distribute.Strategy settings), should be left to Model.make_train_function, which can also be overridden.


Public API for tf.keras.utils namespace.
class CustomObjectScope: Exposes custom classes/functions to Keras deserialization internals.
class GeneratorEnqueuer: Builds a queue out of a data generator.
class OrderedEnqueuer: Builds a Enqueuer from a Sequence.
class Progbar: Displays a progress bar.
class Sequence: Base object for fitting to a sequence of data, such as a dataset.
class SequenceEnqueuer: Base class to enqueue inputs.
class custom_object_scope: Exposes custom classes/functions to Keras deserialization internals.
deserialize_keras_object(...): Turns the serialized form of a Keras object back into an actual object.
get_custom_objects(...): Retrieves a live reference to the global dictionary of custom objects.
get_file(...): Downloads a file from a URL if it not already in the cache.
get_registered_name(...): Returns the name registered to an object within the Keras framework.
get_registered_object(...): Returns the class associated with name if it is registered with Keras.
get_source_inputs(...): Returns the list of input tensors necessary to compute tensor.
model_to_dot(...): Convert a Keras model to dot format.
normalize(...): Normalizes a Numpy array.
pack_x_y_sample_weight(...): Packs user-provided data into a tuple.
plot_model(...): Converts a Keras model to dot format and save to a file.
register_keras_serializable(...): Registers an object with the Keras serialization framework.
serialize_keras_object(...): Serialize a Keras object into a JSON-compatible representation.
to_categorical(...): Converts a class vector (integers) to binary class matrix.
unpack_x_y_sample_weight(...): Unpacks user-provided data tuple.


Exposes custom classes/functions to Keras deserialization internals.
Main aliases
tf.keras.utils.custom_object_scope
See Migration guide for more details.
tf.compat.v1.keras.utils.CustomObjectScope, tf.compat.v1.keras.utils.custom_object_scope
Under a scope with custom_object_scope(objects_dict), Keras methods such as tf.keras.models.load_model or tf.keras.models.model_from_config will be able to deserialize any custom object referenced by a saved config (e.g. a custom layer or metric).
Consider a custom regularizer my_regularizer:
View source
View source


Turns the serialized form of a Keras object back into an actual object.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.deserialize_keras_object


Builds a queue out of a data generator.
Inherits From: SequenceEnqueuer
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.GeneratorEnqueuer
The provided generator can be finite in which case the class will throw a StopIteration exception.
Used in fit_generator, evaluate_generator, predict_generator.
View source
Creates a generator to extract data from the queue.
Skip the data if it is None.
View source
View source
Starts the handler's workers.
View source
Stops running threads and wait for them to exit, if necessary.
Should be called by the same thread which called start().


Retrieves a live reference to the global dictionary of custom objects.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.get_custom_objects
Updating and clearing custom objects using custom_object_scope is preferred, but get_custom_objects can be used to directly access the current collection of custom objects.


Downloads a file from a URL if it not already in the cache.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.get_file
By default the file at the url origin is downloaded to the cache_dir ~/.keras, placed in the cache_subdir datasets, and given the filename fname. The final location of a file example.txt would therefore be ~/.keras/datasets/example.txt.
Files in tar, tar.gz, tar.bz, and zip formats can also be extracted. Passing a hash will verify the file after download. The command line programs shasum and sha256sum can compute the hash.


Returns the name registered to an object within the Keras framework.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.get_registered_name
This function is part of the Keras serialization and deserialization framework. It maps objects to the string names associated with those objects for serialization/deserialization.


Returns the class associated with name if it is registered with Keras.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.get_registered_object
This function is part of the Keras serialization and deserialization framework. It maps strings to the objects associated with them for serialization/deserialization.


Returns the list of input tensors necessary to compute tensor.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.get_source_inputs
Output will always be a list of tensors (potentially with 1 element).


Convert a Keras model to dot format.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.model_to_dot


Normalizes a Numpy array.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.normalize


Builds a Enqueuer from a Sequence.
Inherits From: SequenceEnqueuer
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.OrderedEnqueuer
Used in fit_generator, evaluate_generator, predict_generator.
View source
Creates a generator to extract data from the queue.
Skip the data if it is None.
View source
View source
Starts the handler's workers.
View source
Stops running threads and wait for them to exit, if necessary.
Should be called by the same thread which called start().


Packs user-provided data into a tuple.
This is a convenience utility for packing data into the tuple formats that Model.fit uses.


Converts a Keras model to dot format and save to a file.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.plot_model


Displays a progress bar.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.Progbar
View source
View source
Updates the progress bar.


Registers an object with the Keras serialization framework.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.register_keras_serializable
This decorator injects the decorated class or function into the Keras custom object dictionary, so that it can be serialized and deserialized without needing an entry in the user-provided custom object dict. It also injects a function that Keras will call to get the object's serializable string key.
Note that to be serialized and deserialized, classes must implement the get_config() method. Functions do not have this requirement.
The object will be registered under the key 'package>name' where name, defaults to the object name if not passed.


Base object for fitting to a sequence of data, such as a dataset.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.Sequence
Every Sequence must implement the __getitem__ and the __len__ methods. If you want to modify your dataset between epochs you may implement on_epoch_end. The method __getitem__ should return a complete batch.
Sequence are a safer way to do multiprocessing. This structure guarantees that the network will only train once on each sample per epoch which is not the case with generators.
View source
Method called at the end of every epoch.
View source
Gets batch at position index.
View source
Create a generator that iterate over the Sequence.
View source
Number of batch in the Sequence.


Base class to enqueue inputs.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.SequenceEnqueuer
The task of an Enqueuer is to use parallelism to speed up preprocessing. This is done with processes or threads.
The enqueuer.get() should be an infinite stream of datas.
View source
Creates a generator to extract data from the queue.
Skip the data if it is None. Returns: Generator yielding tuples (inputs, targets) or (inputs, targets, sample_weights).
View source
View source
Starts the handler's workers.
View source
Stops running threads and wait for them to exit, if necessary.
Should be called by the same thread which called start().


Serialize a Keras object into a JSON-compatible representation.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.serialize_keras_object


Converts a class vector (integers) to binary class matrix.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.utils.to_categorical
E.g. for use with categorical_crossentropy.


Unpacks user-provided data tuple.
This is a convenience utility to be used when overriding Model.train_step, Model.test_step, or Model.predict_step. This utility makes it easy to support data of the form (x,), (x, y), or (x, y, sample_weight).
Example in overridden Model.train_step:


Public API for tf.keras.wrappers namespace.
scikit_learn module: Wrapper for using the Scikit-Learn API with Keras models.


Wrapper for using the Scikit-Learn API with Keras models.
class KerasClassifier: Implementation of the scikit-learn classifier API for Keras.
class KerasRegressor: Implementation of the scikit-learn regressor API for Keras.


Implementation of the scikit-learn classifier API for Keras.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.wrappers.scikit_learn.KerasClassifier
View source
Checks for user typos in params.
View source
Filters sk_params and returns those in fn's arguments.
View source
Constructs a new model with build_fn & fit the model to (x, y).
View source
Gets parameters for this estimator.
View source
Returns the class predictions for the given test data.
View source
Returns class probability estimates for the given test data.
View source
Returns the mean accuracy on the given test data and labels.
View source
Sets the parameters of this estimator.


Implementation of the scikit-learn regressor API for Keras.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.keras.wrappers.scikit_learn.KerasRegressor
View source
Checks for user typos in params.
View source
Filters sk_params and returns those in fn's arguments.
View source
Constructs a new model with build_fn & fit the model to (x, y).
View source
Gets parameters for this estimator.
View source
Returns predictions for the given test data.
View source
Returns the mean loss on the given test data and labels.
View source
Sets the parameters of this estimator.


Operations for linear algebra.
experimental module: Public API for tf.linalg.experimental namespace.
class LinearOperator: Base class defining a [batch of] linear operator[s].
class LinearOperatorAdjoint: LinearOperator representing the adjoint of another operator.
class LinearOperatorBlockDiag: Combines one or more LinearOperators in to a Block Diagonal matrix.
class LinearOperatorBlockLowerTriangular: Combines LinearOperators into a blockwise lower-triangular matrix.
class LinearOperatorCirculant: LinearOperator acting like a circulant matrix.
class LinearOperatorCirculant2D: LinearOperator acting like a block circulant matrix.
class LinearOperatorCirculant3D: LinearOperator acting like a nested block circulant matrix.
class LinearOperatorComposition: Composes one or more LinearOperators.
class LinearOperatorDiag: LinearOperator acting like a [batch] square diagonal matrix.
class LinearOperatorFullMatrix: LinearOperator that wraps a [batch] matrix.
class LinearOperatorHouseholder: LinearOperator acting like a [batch] of Householder transformations.
class LinearOperatorIdentity: LinearOperator acting like a [batch] square identity matrix.
class LinearOperatorInversion: LinearOperator representing the inverse of another operator.
class LinearOperatorKronecker: Kronecker product between two LinearOperators.
class LinearOperatorLowRankUpdate: Perturb a LinearOperator with a rank K update.
class LinearOperatorLowerTriangular: LinearOperator acting like a [batch] square lower triangular matrix.
class LinearOperatorPermutation: LinearOperator acting like a [batch] of permutation matrices.
class LinearOperatorScaledIdentity: LinearOperator acting like a scaled [batch] identity matrix A = c I.
class LinearOperatorToeplitz: LinearOperator acting like a [batch] of toeplitz matrices.
class LinearOperatorTridiag: LinearOperator acting like a [batch] square tridiagonal matrix.
class LinearOperatorZeros: LinearOperator acting like a [batch] zero matrix.
adjoint(...): Transposes the last two dimensions of and conjugates tensor matrix.
band_part(...): Copy a tensor setting everything outside a central band in each innermost matrix to zero.
banded_triangular_solve(...): Solve triangular systems of equations with a banded solver.
cholesky(...): Computes the Cholesky decomposition of one or more square matrices.
cholesky_solve(...): Solves systems of linear eqns A X = RHS, given Cholesky factorizations.
cross(...): Compute the pairwise cross product.
det(...): Computes the determinant of one or more square matrices.
diag(...): Returns a batched diagonal tensor with given batched diagonal values.
diag_part(...): Returns the batched diagonal part of a batched tensor.
eig(...): Computes the eigen decomposition of a batch of matrices.
eigh(...): Computes the eigen decomposition of a batch of self-adjoint matrices.
eigvals(...): Computes the eigenvalues of one or more matrices.
eigvalsh(...): Computes the eigenvalues of one or more self-adjoint matrices.
einsum(...): Tensor contraction over specified indices and outer product.
expm(...): Computes the matrix exponential of one or more square matrices.
eye(...): Construct an identity matrix, or a batch of matrices.
global_norm(...): Computes the global norm of multiple tensors.
inv(...): Computes the inverse of one or more square invertible matrices or their adjoints (conjugate transposes).
l2_normalize(...): Normalizes along dimension axis using an L2 norm.
logdet(...): Computes log of the determinant of a hermitian positive definite matrix.
logm(...): Computes the matrix logarithm of one or more square matrices:
lstsq(...): Solves one or more linear least-squares problems.
lu(...): Computes the LU decomposition of one or more square matrices.
lu_matrix_inverse(...): Computes the inverse given the LU decomposition(s) of one or more matrices.
lu_reconstruct(...): The reconstruct one or more matrices from their LU decomposition(s).
lu_solve(...): Solves systems of linear eqns A X = RHS, given LU factorizations.
matmul(...): Multiplies matrix a by matrix b, producing a * b.
matrix_rank(...): Compute the matrix rank of one or more matrices.
matrix_transpose(...): Transposes last two dimensions of tensor a.
matvec(...): Multiplies matrix a by vector b, producing a * b.
norm(...): Computes the norm of vectors, matrices, and tensors.
normalize(...): Normalizes tensor along dimension axis using specified norm.
pinv(...): Compute the Moore-Penrose pseudo-inverse of one or more matrices.
qr(...): Computes the QR decompositions of one or more matrices.
set_diag(...): Returns a batched matrix tensor with new batched diagonal values.
slogdet(...): Computes the sign and the log of the absolute value of the determinant of
solve(...): Solves systems of linear equations.
sqrtm(...): Computes the matrix square root of one or more square matrices:
svd(...): Computes the singular value decompositions of one or more matrices.
tensor_diag(...): Returns a diagonal tensor with a given diagonal values.
tensor_diag_part(...): Returns the diagonal part of the tensor.
tensordot(...): Tensor contraction of a and b along specified axes and outer product.
trace(...): Compute the trace of a tensor x.
triangular_solve(...): Solve systems of linear equations with upper or lower triangular matrices.
tridiagonal_matmul(...): Multiplies tridiagonal matrix by matrix.
tridiagonal_solve(...): Solves tridiagonal systems of equations.


Transposes the last two dimensions of and conjugates tensor matrix.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.adjoint


Solve triangular systems of equations with a banded solver.
bands is a tensor of shape [..., K, M], where K represents the number of bands stored. This corresponds to a batch of M by M matrices, whose K subdiagonals (when lower is True) are stored.
This operator broadcasts the batch dimensions of bands and the batch dimensions of rhs.
Storing 2 bands of a 3x3 matrix. Note that first element in the second row is ignored due to the 'LEFT_RIGHT' padding.
Storing 2 superdiagonals of a 4x4 matrix. Because of the 'LEFT_RIGHT' padding the last element of the first row is ignored.


Copy a tensor setting everything outside a central band in each innermost matrix to zero.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.band_part, tf.compat.v1.matrix_band_part
The band part is computed as follows: Assume input has k dimensions [I, J, K, ..., M, N], then the output is a tensor with the same shape where
band[i, j, k, ..., m, n] = in_band(m, n) * input[i, j, k, ..., m, n].
The indicator function
in_band(m, n) = (num_lower < 0 || (m-n) <= num_lower)) && (num_upper < 0 || (n-m) <= num_upper).


Computes the Cholesky decomposition of one or more square matrices.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.cholesky, tf.compat.v1.linalg.cholesky
The input is a tensor of shape [..., M, M] whose inner-most 2 dimensions form square matrices.
The input has to be symmetric and positive definite. Only the lower-triangular part of the input will be used for this operation. The upper-triangular part will not be read.
The output is a tensor of the same shape as the input containing the Cholesky decompositions for all input submatrices [..., :, :].


Solves systems of linear eqns A X = RHS, given Cholesky factorizations.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.cholesky_solve, tf.compat.v1.linalg.cholesky_solve
Specifically, returns X from A X = RHS, where A = L L^T, L is the chol arg and RHS is the rhs arg.


Compute the pairwise cross product.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.cross, tf.compat.v1.linalg.cross
a and b must be the same shape; they can either be simple 3-element vectors, or any shape where the innermost dimension is 3. In the latter case, each pair of corresponding 3-element vectors is cross-multiplied independently.


Computes the determinant of one or more square matrices.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.det, tf.compat.v1.matrix_determinant
The input is a tensor of shape [..., M, M] whose inner-most 2 dimensions form square matrices. The output is a tensor containing the determinants for all input submatrices [..., :, :].


Returns a batched diagonal tensor with given batched diagonal values.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.diag, tf.compat.v1.matrix_diag
Returns a tensor with the contents in diagonal as k[0]-th to k[1]-th diagonals of a matrix, with everything else padded with padding. num_rows and num_cols specify the dimension of the innermost matrix of the output. If both are not specified, the op assumes the innermost matrix is square and infers its size from k and the innermost dimension of diagonal. If only one of them is specified, the op assumes the unspecified value is the smallest possible based on other criteria.
Let diagonal have r dimensions [I, J, ..., L, M, N]. The output tensor has rank r+1 with shape [I, J, ..., L, M, num_rows, num_cols] when only one diagonal is given (k is an integer or k[0] == k[1]). Otherwise, it has rank r with shape [I, J, ..., L, num_rows, num_cols].
The second innermost dimension of diagonal has double meaning. When k is scalar or k[0] == k[1], M is part of the batch size [I, J, ..., M], and the output tensor is:
Otherwise, M is treated as the number of diagonals for the matrix in the same batch (M = k[1]-k[0]+1), and the output tensor is:
where d = n - m, diag_index = k[1] - d, and index_in_diag = n - max(d, 0) + offset.
offset is zero except when the alignment of the diagonal is to the right.
where diag_len(d) = min(cols - max(d, 0), rows + min(d, 0)).


Returns the batched diagonal part of a batched tensor.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.diag_part, tf.compat.v1.matrix_diag_part
Returns a tensor with the k[0]-th to k[1]-th diagonals of the batched input.
Assume input has r dimensions [I, J, ..., L, M, N]. Let max_diag_len be the maximum length among all diagonals to be extracted, max_diag_len = min(M + min(k[1], 0), N + min(-k[0], 0)) Let num_diags be the number of diagonals to extract, num_diags = k[1] - k[0] + 1.
If num_diags == 1, the output tensor is of rank r - 1 with shape [I, J, ..., L, max_diag_len] and values:
where y = max(-k[1], 0), x = max(k[1], 0).
Otherwise, the output tensor has rank r with dimensions [I, J, ..., L, num_diags, max_diag_len] with values:
where d = k[1] - m, y = max(-d, 0) - offset, and x = max(d, 0) - offset.
offset is zero except when the alignment of the diagonal is to the right.
where diag_len(d) = min(cols - max(d, 0), rows + min(d, 0)).
The input must be at least a matrix.


Computes the eigen decomposition of a batch of matrices.
Main aliases
tf.eig
The eigenvalues and eigenvectors for a non-Hermitian matrix in general are complex. The eigenvectors are not guaranteed to be linearly independent.
Computes the eigenvalues and right eigenvectors of the innermost N-by-N matrices in tensor such that tensor[...,:,:] * v[..., :,i] = e[..., i] * v[...,:,i], for i=0...N-1.


Computes the eigen decomposition of a batch of self-adjoint matrices.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.eigh, tf.compat.v1.self_adjoint_eig
Computes the eigenvalues and eigenvectors of the innermost N-by-N matrices in tensor such that tensor[...,:,:] * v[..., :,i] = e[..., i] * v[...,:,i], for i=0...N-1.


Computes the eigenvalues of one or more matrices.
Main aliases
tf.eigvals


Computes the eigenvalues of one or more self-adjoint matrices.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.eigvalsh, tf.compat.v1.self_adjoint_eigvals


Public API for tf.linalg.experimental namespace.
conjugate_gradient(...): Conjugate gradient solver.


Conjugate gradient solver.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.experimental.conjugate_gradient
Solves a linear system of equations A*x = rhs for self-adjoint, positive definite matrix A and right-hand side vector rhs, using an iterative, matrix-free algorithm where the action of the matrix A is represented by operator. The iteration terminates when either the number of iterations exceeds max_iter or when the residual norm has been reduced to tol times its initial value, i.e. \(||rhs - A x_k|| <= tol ||rhs||\).


Computes the matrix exponential of one or more square matrices.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.expm
The exponential is computed using a combination of the scaling and squaring method and the Pade approximation. Details can be found in: Nicholas J. Higham, "The scaling and squaring method for the matrix exponential revisited," SIAM J. Matrix Anal. Applic., 26:1179-1193, 2005.
The input is a tensor of shape [..., M, M] whose inner-most 2 dimensions form square matrices. The output is a tensor of the same shape as the input containing the exponential for all input submatrices [..., :, :].
Equivalent to scipy.linalg.expm


Computes the global norm of multiple tensors.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.global_norm, tf.compat.v1.linalg.global_norm
Given a tuple or list of tensors t_list, this operation returns the global norm of the elements in all tensors in t_list. The global norm is computed as:
global_norm = sqrt(sum([l2norm(t)**2 for t in t_list]))
Any entries in t_list that are of type None are ignored.


Computes the inverse of one or more square invertible matrices or their adjoints (conjugate transposes).
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.inv, tf.compat.v1.matrix_inverse
The input is a tensor of shape [..., M, M] whose inner-most 2 dimensions form square matrices. The output is a tensor of the same shape as the input containing the inverse for all input submatrices [..., :, :].
The op uses LU decomposition with partial pivoting to compute the inverses.
If a matrix is not invertible there is no guarantee what the op does. It may detect the condition and raise an exception or it may simply return a garbage result.


Base class defining a [batch of] linear operator[s].
Inherits From: Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.LinearOperator
Subclasses of LinearOperator provide access to common methods on a (batch) matrix, without the need to materialize the matrix. This allows:
To enable a public method, subclasses should implement the leading-underscore version of the method. The argument signature should be identical except for the omission of name="...". For example, to enable matmul(x, adjoint=False, name="matmul") a subclass should implement _matmul(x, adjoint=False).
Subclasses should only implement the assert methods (e.g. assert_non_singular) if they can be done in less than O(N^3) time.
Class docstrings should contain an explanation of computational complexity. Since this is a high-performance library, attention should be paid to detail, and explanations can include constants as well as Big-O notation.
LinearOperator subclasses should operate on a [batch] matrix with compatible shape. Class docstrings should define what is meant by compatible shape. Some subclasses may not support batching.
x is a batch matrix with compatible shape for matmul if
rhs is a batch matrix with compatible shape for solve if
This operator acts like a (batch) matrix A with shape [B1,...,Bb, M, N] for some b >= 0. The first b indices index a batch member. For every batch index (i1,...,ib), A[i1,...,ib, : :] is an m x n matrix. Again, this matrix A may not be materialized, but for purposes of identifying and working with compatible arguments the shape is relevant.
This operator acts on batch matrices with compatible shape. FILL IN WHAT IS MEANT BY COMPATIBLE SHAPE
FILL THIS IN
This LinearOperator is initialized with boolean flags of the form is_X, for X = non_singular, self_adjoint, positive_definite, square. These have the following meaning:
All subclasses of LinearOperator are expected to pass a parameters argument to super().__init__(). This should be a dict containing the unadulterated arguments passed to the subclass __init__. For example, MyLinearOperator with an initializer should look like:
ConditionNumber < max{100, range_dimension, domain_dimension} * eps, eps := np.finfo(self.dtype.as_numpy_dtype).eps ```
View source
Returns an Op that asserts this operator is positive definite.
Here, positive definite means that the quadratic form x^H A x has positive real part for all nonzero x. Note that we do not require the operator to be self-adjoint to be positive definite.
View source
Returns an Op that asserts this operator is self-adjoint.
Here we check that this operator is exactly equal to its hermitian transpose.
View source
Shape of batch dimensions of this operator, determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns a Tensor holding [B1,...,Bb].
View source
Returns a Cholesky factor as a LinearOperator.
Given A representing this LinearOperator, if A is positive definite self-adjoint, return L, where A = L L^T, i.e. the cholesky decomposition.
View source
Returns the condition number of this linear operator.
View source
Determinant for every batch member.
View source
Efficiently get the [batch] diagonal part of this operator.
If this operator has shape [B1,...,Bb, M, N], this returns a Tensor diagonal, of shape [B1,...,Bb, min(M, N)], where diagonal[b1,...,bb, i] = self.to_dense()[b1,...,bb, i, i].
View source
Dimension (in the sense of vector spaces) of the domain of this operator.
Determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns N.
View source
Returns the eigenvalues of this linear operator.
If the operator is marked as self-adjoint (via is_self_adjoint) this computation can be more efficient.
View source
Returns the Inverse of this LinearOperator.
Given A representing this LinearOperator, return a LinearOperator representing A^-1.
View source
Log absolute value of determinant for every batch member.
View source
Transform [batch] matrix x with left multiplication: x --> Ax.
View source
Transform [batch] vector x with left multiplication: x --> Ax.
View source
Dimension (in the sense of vector spaces) of the range of this operator.
Determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns M.
View source
Shape of this LinearOperator, determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns a Tensor holding [B1,...,Bb, M, N], equivalent to tf.shape(A).
View source
Solve (exact or approx) R (batch) systems of equations: A X = rhs.
The returned Tensor will be close to an exact solution if A is well conditioned. Otherwise closeness will vary. See class docstring for details.
View source
Solve single equation with best effort: A X = rhs.
The returned Tensor will be close to an exact solution if A is well conditioned. Otherwise closeness will vary. See class docstring for details.
View source
Rank (in the sense of tensors) of matrix corresponding to this operator.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns b + 2.
View source
Return a dense (batch) matrix representing this operator.
View source
Trace of the linear operator, equal to sum of self.diag_part().
If the operator is square, this is also the sum of the eigenvalues.
View source


LinearOperator representing the adjoint of another operator.
Inherits From: LinearOperator, Module
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.linalg.LinearOperatorAdjoint
This operator represents the adjoint of another operator.
The performance of LinearOperatorAdjoint depends on the underlying operators performance.
This LinearOperator is initialized with boolean flags of the form is_X, for X = non_singular, self_adjoint, positive_definite, square. These have the following meaning:
Given A representing this LinearOperator, return A*. Note that calling self.adjoint() and self.H are equivalent.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns TensorShape([B1,...,Bb]), equivalent to A.shape[:-2]
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns N.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns M.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns TensorShape([B1,...,Bb, M, N]), equivalent to A.shape.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns b + 2.
View source
Add matrix represented by this operator to x. Equivalent to A + x.
View source
Returns the adjoint of the current LinearOperator.
Given A representing this LinearOperator, return A*. Note that calling self.adjoint() and self.H are equivalent.
View source
Returns an Op that asserts this operator is non singular.
This operator is considered non-singular if
View source
Returns an Op that asserts this operator is positive definite.
Here, positive definite means that the quadratic form x^H A x has positive real part for all nonzero x. Note that we do not require the operator to be self-adjoint to be positive definite.
View source
Returns an Op that asserts this operator is self-adjoint.
Here we check that this operator is exactly equal to its hermitian transpose.
View source
Shape of batch dimensions of this operator, determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns a Tensor holding [B1,...,Bb].
View source
Returns a Cholesky factor as a LinearOperator.
Given A representing this LinearOperator, if A is positive definite self-adjoint, return L, where A = L L^T, i.e. the cholesky decomposition.
View source
Returns the condition number of this linear operator.
View source
Determinant for every batch member.
View source
Efficiently get the [batch] diagonal part of this operator.
If this operator has shape [B1,...,Bb, M, N], this returns a Tensor diagonal, of shape [B1,...,Bb, min(M, N)], where diagonal[b1,...,bb, i] = self.to_dense()[b1,...,bb, i, i].
View source
Dimension (in the sense of vector spaces) of the domain of this operator.
Determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns N.
View source
Returns the eigenvalues of this linear operator.
If the operator is marked as self-adjoint (via is_self_adjoint) this computation can be more efficient.
View source
Returns the Inverse of this LinearOperator.
Given A representing this LinearOperator, return a LinearOperator representing A^-1.
View source
Log absolute value of determinant for every batch member.
View source
Transform [batch] matrix x with left multiplication: x --> Ax.
View source
Transform [batch] vector x with left multiplication: x --> Ax.
View source
Dimension (in the sense of vector spaces) of the range of this operator.
Determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns M.
View source
Shape of this LinearOperator, determined at runtime.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns a Tensor holding [B1,...,Bb, M, N], equivalent to tf.shape(A).
View source
Solve (exact or approx) R (batch) systems of equations: A X = rhs.
The returned Tensor will be close to an exact solution if A is well conditioned. Otherwise closeness will vary. See class docstring for details.
View source
Solve single equation with best effort: A X = rhs.
The returned Tensor will be close to an exact solution if A is well conditioned. Otherwise closeness will vary. See class docstring for details.
View source
Rank (in the sense of tensors) of matrix corresponding to this operator.
If this operator acts like the batch matrix A with A.shape = [B1,...,Bb, M, N], then this returns b + 2.
View source
Return a dense (batch) matrix representing this operator.
View source
Trace of the linear operator, equal to sum of self.diag_part().
If the operator is square, this is also the sum of the eigenvalues.
View source