Module keras.api.keras.initializers

Public API for tf.keras.initializers namespace.

Expand source code
# This file is MACHINE GENERATED! Do not edit.
# Generated by: tensorflow/python/tools/api/generator/create_python_api.py script.
"""Public API for tf.keras.initializers namespace.
"""

from __future__ import print_function as _print_function

import sys as _sys

from keras.initializers import deserialize
from keras.initializers import get
from keras.initializers import serialize
from keras.initializers.initializers_v1 import HeNormal as he_normal
from keras.initializers.initializers_v1 import HeUniform as he_uniform
from keras.initializers.initializers_v1 import LecunNormal as lecun_normal
from keras.initializers.initializers_v1 import LecunUniform as lecun_uniform
from keras.initializers.initializers_v1 import RandomNormal
from keras.initializers.initializers_v1 import RandomNormal as normal
from keras.initializers.initializers_v1 import RandomNormal as random_normal
from keras.initializers.initializers_v1 import RandomUniform
from keras.initializers.initializers_v1 import RandomUniform as random_uniform
from keras.initializers.initializers_v1 import RandomUniform as uniform
from keras.initializers.initializers_v1 import TruncatedNormal
from keras.initializers.initializers_v1 import TruncatedNormal as truncated_normal
from keras.initializers.initializers_v1 import _v1_constant_initializer as Constant
from keras.initializers.initializers_v1 import _v1_constant_initializer as constant
from keras.initializers.initializers_v1 import _v1_glorot_normal_initializer as glorot_normal
from keras.initializers.initializers_v1 import _v1_glorot_uniform_initializer as glorot_uniform
from keras.initializers.initializers_v1 import _v1_identity as Identity
from keras.initializers.initializers_v1 import _v1_identity as identity
from keras.initializers.initializers_v1 import _v1_ones_initializer as Ones
from keras.initializers.initializers_v1 import _v1_ones_initializer as ones
from keras.initializers.initializers_v1 import _v1_orthogonal_initializer as Orthogonal
from keras.initializers.initializers_v1 import _v1_orthogonal_initializer as orthogonal
from keras.initializers.initializers_v1 import _v1_variance_scaling_initializer as VarianceScaling
from keras.initializers.initializers_v1 import _v1_zeros_initializer as Zeros
from keras.initializers.initializers_v1 import _v1_zeros_initializer as zeros
from keras.initializers.initializers_v2 import Initializer

del _print_function

from tensorflow.python.util import module_wrapper as _module_wrapper

if not isinstance(_sys.modules[__name__], _module_wrapper.TFModuleWrapper):
  _sys.modules[__name__] = _module_wrapper.TFModuleWrapper(
      _sys.modules[__name__], "keras.initializers", public_apis=None, deprecation=True,
      has_lite=False)

Functions

def deserialize(config, custom_objects=None)

Return an Initializer object from its config.

Expand source code
@keras_export('keras.initializers.deserialize')
def deserialize(config, custom_objects=None):
  """Return an `Initializer` object from its config."""
  populate_deserializable_objects()
  return generic_utils.deserialize_keras_object(
      config,
      module_objects=LOCAL.ALL_OBJECTS,
      custom_objects=custom_objects,
      printable_module_name='initializer')
def get(identifier)

Retrieve a Keras initializer by the identifier.

The identifier may be the string name of a initializers function or class ( case-sensitively).

>>> identifier = 'Ones'
>>> tf.keras.initializers.deserialize(identifier)
<...keras.initializers.initializers_v2.Ones...>

You can also specify config of the initializer to this function by passing dict containing class_name and config as an identifier. Also note that the class_name must map to a Initializer class.

>>> cfg = {'class_name': 'Ones', 'config': {}}
>>> tf.keras.initializers.deserialize(cfg)
<...keras.initializers.initializers_v2.Ones...>

In the case that the identifier is a class, this method will return a new instance of the class by its constructor.

Args

identifier
String or dict that contains the initializer name or configurations.

Returns

Initializer instance base on the input identifier.

Raises

ValueError
If the input identifier is not a supported type or in a bad format.
Expand source code
@keras_export('keras.initializers.get')
def get(identifier):
  """Retrieve a Keras initializer by the identifier.

  The `identifier` may be the string name of a initializers function or class (
  case-sensitively).

  >>> identifier = 'Ones'
  >>> tf.keras.initializers.deserialize(identifier)
  <...keras.initializers.initializers_v2.Ones...>

  You can also specify `config` of the initializer to this function by passing
  dict containing `class_name` and `config` as an identifier. Also note that the
  `class_name` must map to a `Initializer` class.

  >>> cfg = {'class_name': 'Ones', 'config': {}}
  >>> tf.keras.initializers.deserialize(cfg)
  <...keras.initializers.initializers_v2.Ones...>

  In the case that the `identifier` is a class, this method will return a new
  instance of the class by its constructor.

  Args:
    identifier: String or dict that contains the initializer name or
      configurations.

  Returns:
    Initializer instance base on the input identifier.

  Raises:
    ValueError: If the input identifier is not a supported type or in a bad
      format.
  """

  if identifier is None:
    return None
  if isinstance(identifier, dict):
    return deserialize(identifier)
  elif isinstance(identifier, str):
    identifier = str(identifier)
    return deserialize(identifier)
  elif callable(identifier):
    if inspect.isclass(identifier):
      identifier = identifier()
    return identifier
  else:
    raise ValueError('Could not interpret initializer identifier: ' +
                     str(identifier))
def serialize(initializer)
Expand source code
@keras_export('keras.initializers.serialize')
def serialize(initializer):
  return generic_utils.serialize_keras_object(initializer)

Classes

class Constant (value=0, dtype=tf.float32, verify_shape=False)

Initializer that generates tensors with constant values.

The resulting tensor is populated with values of type dtype, as specified by arguments value following the desired shape of the new tensor (see examples below).

The argument value can be a constant value, or a list of values of type dtype. If value is a list, then the length of the list must be less than or equal to the number of elements implied by the desired shape of the tensor. In the case where the total number of elements in value is less than the number of elements required by the tensor shape, the last element in value will be used to fill the remaining entries. If the total number of elements in value is greater than the number of elements required by the tensor shape, the initializer will raise a ValueError.

Args

value
A Python scalar, list or tuple of values, or a N-dimensional numpy array. All elements of the initialized variable will be set to the corresponding value in the value argument.
dtype
Default data type, used if no dtype argument is provided when calling the initializer.
verify_shape
Boolean that enables verification of the shape of value. If True, the initializer will throw an error if the shape of value is not compatible with the shape of the initialized tensor.

Raises

TypeError
If the input value is not one of the expected types.

Examples

The following example can be rewritten using a numpy.ndarray instead of the value list, even reshaped, as shown in the two commented lines below the value list initialization.

>>> value = [0, 1, 2, 3, 4, 5, 6, 7]
>>> init = tf.compat.v1.constant_initializer(value)
>>> # fitting shape
>>> with tf.compat.v1.Session():
...   x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
...   x.initializer.run()
...   print(x.eval())
[[0. 1. 2. 3.]
 [4. 5. 6. 7.]]
>>> # Larger shape
>>> with tf.compat.v1.Session():
...   y = tf.compat.v1.get_variable('y', shape=[3, 4], initializer=init)
...   y.initializer.run()
...   print(y.eval())
[[0.  1.  2.  3.]
 [4.  5.  6.  7.]
 [7.  7.  7.  7.]]
>>> # Smaller shape
>>> with tf.compat.v1.Session():
...   z = tf.compat.v1.get_variable('z', shape=[2, 3], initializer=init)
Traceback (most recent call last):
...
ValueError: Too many elements provided. Needed at most 6, but received 8
>>> # Shape verification
>>> init_verify = tf.compat.v1.constant_initializer(value, verify_shape=True)
>>> with tf.compat.v1.Session():
...  u = tf.compat.v1.get_variable('u', shape=[3, 4],
...                                initializer=init_verify)
Traceback (most recent call last):
...
TypeError: Expected Tensor's shape: (3, 4), got (8,).

DEPRECATED FUNCTION ARGUMENTS (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Warning: SOME ARGUMENTS ARE DEPRECATED: (verify_shape). They will be removed in a future version. Instructions for updating: Objects must now be the required shape or no shape can be specified

Expand source code
class Constant(Initializer):
  """Initializer that generates tensors with constant values.

  The resulting tensor is populated with values of type `dtype`, as
  specified by arguments `value` following the desired `shape` of the
  new tensor (see examples below).

  The argument `value` can be a constant value, or a list of values of type
  `dtype`. If `value` is a list, then the length of the list must be less
  than or equal to the number of elements implied by the desired shape of the
  tensor. In the case where the total number of elements in `value` is less
  than the number of elements required by the tensor shape, the last element
  in `value` will be used to fill the remaining entries. If the total number of
  elements in `value` is greater than the number of elements required by the
  tensor shape, the initializer will raise a `ValueError`.

  Args:
    value: A Python scalar, list or tuple of values, or a N-dimensional numpy
      array. All elements of the initialized variable will be set to the
      corresponding value in the `value` argument.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer.
    verify_shape: Boolean that enables verification of the shape of `value`. If
      `True`, the initializer will throw an error if the shape of `value` is not
      compatible with the shape of the initialized tensor.

  Raises:
    TypeError: If the input `value` is not one of the expected types.

  Examples:
    The following example can be rewritten using a numpy.ndarray instead
    of the `value` list, even reshaped, as shown in the two commented lines
    below the `value` list initialization.

  >>> value = [0, 1, 2, 3, 4, 5, 6, 7]
  >>> init = tf.compat.v1.constant_initializer(value)
  >>> # fitting shape
  >>> with tf.compat.v1.Session():
  ...   x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
  ...   x.initializer.run()
  ...   print(x.eval())
  [[0. 1. 2. 3.]
   [4. 5. 6. 7.]]
  >>> # Larger shape
  >>> with tf.compat.v1.Session():
  ...   y = tf.compat.v1.get_variable('y', shape=[3, 4], initializer=init)
  ...   y.initializer.run()
  ...   print(y.eval())
  [[0.  1.  2.  3.]
   [4.  5.  6.  7.]
   [7.  7.  7.  7.]]
  >>> # Smaller shape
  >>> with tf.compat.v1.Session():
  ...   z = tf.compat.v1.get_variable('z', shape=[2, 3], initializer=init)
  Traceback (most recent call last):
  ...
  ValueError: Too many elements provided. Needed at most 6, but received 8
  >>> # Shape verification
  >>> init_verify = tf.compat.v1.constant_initializer(value, verify_shape=True)
  >>> with tf.compat.v1.Session():
  ...  u = tf.compat.v1.get_variable('u', shape=[3, 4],
  ...                                initializer=init_verify)
  Traceback (most recent call last):
  ...
  TypeError: Expected Tensor's shape: (3, 4), got (8,).
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  @deprecated_args(None, "Objects must now be the required shape or no shape "
                   "can be specified", "verify_shape")
  def __init__(self, value=0, dtype=dtypes.float32, verify_shape=False):
    if not (np.isscalar(value) or isinstance(value, (list, tuple, np.ndarray))):
      raise TypeError(
          "Invalid type for initial value: %s (expected Python scalar, list or "
          "tuple of values, or numpy.ndarray)." % type(value))

    self.value = value
    self.dtype = dtypes.as_dtype(dtype)
    self._verify_shape = verify_shape

  def __call__(self, shape, dtype=None, partition_info=None, verify_shape=None):
    if dtype is None:
      dtype = self.dtype
    if verify_shape is None:
      verify_shape = self._verify_shape
    return constant_op.constant_v1(
        self.value, dtype=dtype, shape=shape, verify_shape=verify_shape)

  def get_config(self):
    # We don't include `verify_shape` for compatibility with Keras.
    # `verify_shape` should be passed as an argument to `__call__` rather
    # than as a constructor argument: conceptually it isn't a property
    # of the initializer.
    return {"value": self.value, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  # We don't include `verify_shape` for compatibility with Keras.
  # `verify_shape` should be passed as an argument to `__call__` rather
  # than as a constructor argument: conceptually it isn't a property
  # of the initializer.
  return {"value": self.value, "dtype": self.dtype.name}
class constant (value=0, dtype=tf.float32, verify_shape=False)

Initializer that generates tensors with constant values.

The resulting tensor is populated with values of type dtype, as specified by arguments value following the desired shape of the new tensor (see examples below).

The argument value can be a constant value, or a list of values of type dtype. If value is a list, then the length of the list must be less than or equal to the number of elements implied by the desired shape of the tensor. In the case where the total number of elements in value is less than the number of elements required by the tensor shape, the last element in value will be used to fill the remaining entries. If the total number of elements in value is greater than the number of elements required by the tensor shape, the initializer will raise a ValueError.

Args

value
A Python scalar, list or tuple of values, or a N-dimensional numpy array. All elements of the initialized variable will be set to the corresponding value in the value argument.
dtype
Default data type, used if no dtype argument is provided when calling the initializer.
verify_shape
Boolean that enables verification of the shape of value. If True, the initializer will throw an error if the shape of value is not compatible with the shape of the initialized tensor.

Raises

TypeError
If the input value is not one of the expected types.

Examples

The following example can be rewritten using a numpy.ndarray instead of the value list, even reshaped, as shown in the two commented lines below the value list initialization.

>>> value = [0, 1, 2, 3, 4, 5, 6, 7]
>>> init = tf.compat.v1.constant_initializer(value)
>>> # fitting shape
>>> with tf.compat.v1.Session():
...   x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
...   x.initializer.run()
...   print(x.eval())
[[0. 1. 2. 3.]
 [4. 5. 6. 7.]]
>>> # Larger shape
>>> with tf.compat.v1.Session():
...   y = tf.compat.v1.get_variable('y', shape=[3, 4], initializer=init)
...   y.initializer.run()
...   print(y.eval())
[[0.  1.  2.  3.]
 [4.  5.  6.  7.]
 [7.  7.  7.  7.]]
>>> # Smaller shape
>>> with tf.compat.v1.Session():
...   z = tf.compat.v1.get_variable('z', shape=[2, 3], initializer=init)
Traceback (most recent call last):
...
ValueError: Too many elements provided. Needed at most 6, but received 8
>>> # Shape verification
>>> init_verify = tf.compat.v1.constant_initializer(value, verify_shape=True)
>>> with tf.compat.v1.Session():
...  u = tf.compat.v1.get_variable('u', shape=[3, 4],
...                                initializer=init_verify)
Traceback (most recent call last):
...
TypeError: Expected Tensor's shape: (3, 4), got (8,).

DEPRECATED FUNCTION ARGUMENTS (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Warning: SOME ARGUMENTS ARE DEPRECATED: (verify_shape). They will be removed in a future version. Instructions for updating: Objects must now be the required shape or no shape can be specified

Expand source code
class Constant(Initializer):
  """Initializer that generates tensors with constant values.

  The resulting tensor is populated with values of type `dtype`, as
  specified by arguments `value` following the desired `shape` of the
  new tensor (see examples below).

  The argument `value` can be a constant value, or a list of values of type
  `dtype`. If `value` is a list, then the length of the list must be less
  than or equal to the number of elements implied by the desired shape of the
  tensor. In the case where the total number of elements in `value` is less
  than the number of elements required by the tensor shape, the last element
  in `value` will be used to fill the remaining entries. If the total number of
  elements in `value` is greater than the number of elements required by the
  tensor shape, the initializer will raise a `ValueError`.

  Args:
    value: A Python scalar, list or tuple of values, or a N-dimensional numpy
      array. All elements of the initialized variable will be set to the
      corresponding value in the `value` argument.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer.
    verify_shape: Boolean that enables verification of the shape of `value`. If
      `True`, the initializer will throw an error if the shape of `value` is not
      compatible with the shape of the initialized tensor.

  Raises:
    TypeError: If the input `value` is not one of the expected types.

  Examples:
    The following example can be rewritten using a numpy.ndarray instead
    of the `value` list, even reshaped, as shown in the two commented lines
    below the `value` list initialization.

  >>> value = [0, 1, 2, 3, 4, 5, 6, 7]
  >>> init = tf.compat.v1.constant_initializer(value)
  >>> # fitting shape
  >>> with tf.compat.v1.Session():
  ...   x = tf.compat.v1.get_variable('x', shape=[2, 4], initializer=init)
  ...   x.initializer.run()
  ...   print(x.eval())
  [[0. 1. 2. 3.]
   [4. 5. 6. 7.]]
  >>> # Larger shape
  >>> with tf.compat.v1.Session():
  ...   y = tf.compat.v1.get_variable('y', shape=[3, 4], initializer=init)
  ...   y.initializer.run()
  ...   print(y.eval())
  [[0.  1.  2.  3.]
   [4.  5.  6.  7.]
   [7.  7.  7.  7.]]
  >>> # Smaller shape
  >>> with tf.compat.v1.Session():
  ...   z = tf.compat.v1.get_variable('z', shape=[2, 3], initializer=init)
  Traceback (most recent call last):
  ...
  ValueError: Too many elements provided. Needed at most 6, but received 8
  >>> # Shape verification
  >>> init_verify = tf.compat.v1.constant_initializer(value, verify_shape=True)
  >>> with tf.compat.v1.Session():
  ...  u = tf.compat.v1.get_variable('u', shape=[3, 4],
  ...                                initializer=init_verify)
  Traceback (most recent call last):
  ...
  TypeError: Expected Tensor's shape: (3, 4), got (8,).
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  @deprecated_args(None, "Objects must now be the required shape or no shape "
                   "can be specified", "verify_shape")
  def __init__(self, value=0, dtype=dtypes.float32, verify_shape=False):
    if not (np.isscalar(value) or isinstance(value, (list, tuple, np.ndarray))):
      raise TypeError(
          "Invalid type for initial value: %s (expected Python scalar, list or "
          "tuple of values, or numpy.ndarray)." % type(value))

    self.value = value
    self.dtype = dtypes.as_dtype(dtype)
    self._verify_shape = verify_shape

  def __call__(self, shape, dtype=None, partition_info=None, verify_shape=None):
    if dtype is None:
      dtype = self.dtype
    if verify_shape is None:
      verify_shape = self._verify_shape
    return constant_op.constant_v1(
        self.value, dtype=dtype, shape=shape, verify_shape=verify_shape)

  def get_config(self):
    # We don't include `verify_shape` for compatibility with Keras.
    # `verify_shape` should be passed as an argument to `__call__` rather
    # than as a constructor argument: conceptually it isn't a property
    # of the initializer.
    return {"value": self.value, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  # We don't include `verify_shape` for compatibility with Keras.
  # `verify_shape` should be passed as an argument to `__call__` rather
  # than as a constructor argument: conceptually it isn't a property
  # of the initializer.
  return {"value": self.value, "dtype": self.dtype.name}
class glorot_normal (seed=None, dtype=tf.float32)

The Glorot normal initializer, also called Xavier normal initializer.

It draws samples from a truncated normal distribution centered on 0 with standard deviation (after truncation) given by stddev = sqrt(2 / (fan_in + fan_out)) where fan_in is the number of input units in the weight tensor and fan_out is the number of output units in the weight tensor.

Args

seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

References

Glorot et al., 2010 (pdf)

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class GlorotNormal(VarianceScaling):
  """The Glorot normal initializer, also called Xavier normal initializer.

  It draws samples from a truncated normal distribution centered on 0
  with standard deviation (after truncation) given by
  `stddev = sqrt(2 / (fan_in + fan_out))` where `fan_in` is the number
  of input units in the weight tensor and `fan_out` is the number of
  output units in the weight tensor.

  Args:
    seed: A Python integer. Used to create random seeds. See
      `tf.compat.v1.set_random_seed` for behavior.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer. Only floating point types are supported.
  References:
      [Glorot et al., 2010](http://proceedings.mlr.press/v9/glorot10a.html)
      ([pdf](http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf))
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, seed=None, dtype=dtypes.float32):
    super(GlorotNormal, self).__init__(
        scale=1.0, mode="fan_avg", distribution="truncated_normal", seed=seed)

  def get_config(self):
    return {"seed": self.seed, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.VarianceScaling
  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"seed": self.seed, "dtype": self.dtype.name}
class glorot_uniform (seed=None, dtype=tf.float32)

The Glorot uniform initializer, also called Xavier uniform initializer.

It draws samples from a uniform distribution within [-limit, limit] where limit is sqrt(6 / (fan_in + fan_out)) where fan_in is the number of input units in the weight tensor and fan_out is the number of output units in the weight tensor.

Args

seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

References

Glorot et al., 2010 (pdf)

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class GlorotUniform(VarianceScaling):
  """The Glorot uniform initializer, also called Xavier uniform initializer.

  It draws samples from a uniform distribution within [-limit, limit]
  where `limit` is `sqrt(6 / (fan_in + fan_out))`
  where `fan_in` is the number of input units in the weight tensor
  and `fan_out` is the number of output units in the weight tensor.

  Args:
    seed: A Python integer. Used to create random seeds. See
      `tf.compat.v1.set_random_seed` for behavior.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer. Only floating point types are supported.
  References:
      [Glorot et al., 2010](http://proceedings.mlr.press/v9/glorot10a.html)
      ([pdf](http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf))
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, seed=None, dtype=dtypes.float32):
    super(GlorotUniform, self).__init__(
        scale=1.0, mode="fan_avg", distribution="uniform", seed=seed)

  def get_config(self):
    return {"seed": self.seed, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.VarianceScaling
  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"seed": self.seed, "dtype": self.dtype.name}
class he_normal (seed=None)

Initializer capable of adapting its scale to the shape of weights tensors.

With distribution="truncated_normal" or "untruncated_normal", samples are drawn from a truncated/untruncated normal distribution with a mean of zero and a standard deviation (after truncation, if used) stddev = sqrt(scale / n) where n is: - number of input units in the weight tensor, if mode = "fan_in" - number of output units, if mode = "fan_out" - average of the numbers of input and output units, if mode = "fan_avg"

With distribution="uniform", samples are drawn from a uniform distribution within [-limit, limit], with limit = sqrt(3 * scale / n).

Args

scale
Scaling factor (positive float).
mode
One of "fan_in", "fan_out", "fan_avg".
distribution
Random distribution to use. One of "normal", "uniform".
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

Raises

ValueError
In case of an invalid value for the "scale", mode" or "distribution" arguments.

DEPRECATED FUNCTION ARGUMENT VALUES (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Warning: SOME ARGUMENT VALUES ARE DEPRECATED: (distribution='normal'). They will be removed in a future version. Instructions for updating: RandomNormal is a deprecated alias for TruncatedNormal

Expand source code
class HeNormal(tf.compat.v1.variance_scaling_initializer):

  def __init__(self, seed=None):
    super(HeNormal, self).__init__(
        scale=2., mode='fan_in', distribution='truncated_normal', seed=seed)

  def get_config(self):
    return {'seed': self.seed}

Ancestors

  • tensorflow.python.ops.init_ops.VarianceScaling
  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {'seed': self.seed}
class he_uniform (seed=None)

Initializer capable of adapting its scale to the shape of weights tensors.

With distribution="truncated_normal" or "untruncated_normal", samples are drawn from a truncated/untruncated normal distribution with a mean of zero and a standard deviation (after truncation, if used) stddev = sqrt(scale / n) where n is: - number of input units in the weight tensor, if mode = "fan_in" - number of output units, if mode = "fan_out" - average of the numbers of input and output units, if mode = "fan_avg"

With distribution="uniform", samples are drawn from a uniform distribution within [-limit, limit], with limit = sqrt(3 * scale / n).

Args

scale
Scaling factor (positive float).
mode
One of "fan_in", "fan_out", "fan_avg".
distribution
Random distribution to use. One of "normal", "uniform".
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

Raises

ValueError
In case of an invalid value for the "scale", mode" or "distribution" arguments.

DEPRECATED FUNCTION ARGUMENT VALUES (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Warning: SOME ARGUMENT VALUES ARE DEPRECATED: (distribution='normal'). They will be removed in a future version. Instructions for updating: RandomNormal is a deprecated alias for TruncatedNormal

Expand source code
class HeUniform(tf.compat.v1.variance_scaling_initializer):

  def __init__(self, seed=None):
    super(HeUniform, self).__init__(
        scale=2., mode='fan_in', distribution='uniform', seed=seed)

  def get_config(self):
    return {'seed': self.seed}

Ancestors

  • tensorflow.python.ops.init_ops.VarianceScaling
  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {'seed': self.seed}
class Identity (gain=1.0, dtype=tf.float32)

Initializer that generates the identity matrix.

Only use for 2D matrices.

Args

gain
Multiplicative factor to apply to the identity matrix.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Identity(Initializer):
  """Initializer that generates the identity matrix.

  Only use for 2D matrices.

  Args:
    gain: Multiplicative factor to apply to the identity matrix.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer. Only floating point types are supported.
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, gain=1.0, dtype=dtypes.float32):
    self.gain = gain
    self.dtype = _assert_float_dtype(dtypes.as_dtype(dtype))

  def __call__(self, shape, dtype=None, partition_info=None):
    full_shape = shape if partition_info is None else partition_info.full_shape
    if len(full_shape) != 2:
      raise ValueError(
          "Identity matrix initializer can only be used for 2D matrices.")
    if dtype is None:
      dtype = self.dtype
    if isinstance(full_shape, tensor_shape.TensorShape):
      full_shape = full_shape.as_list()
    initializer = linalg_ops_impl.eye(*full_shape, dtype=dtype)
    if partition_info is not None:
      initializer = array_ops.slice(initializer, partition_info.var_offset,
                                    shape)
    return self.gain * initializer

  def get_config(self):
    return {"gain": self.gain, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"gain": self.gain, "dtype": self.dtype.name}
class identity (gain=1.0, dtype=tf.float32)

Initializer that generates the identity matrix.

Only use for 2D matrices.

Args

gain
Multiplicative factor to apply to the identity matrix.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Identity(Initializer):
  """Initializer that generates the identity matrix.

  Only use for 2D matrices.

  Args:
    gain: Multiplicative factor to apply to the identity matrix.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer. Only floating point types are supported.
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, gain=1.0, dtype=dtypes.float32):
    self.gain = gain
    self.dtype = _assert_float_dtype(dtypes.as_dtype(dtype))

  def __call__(self, shape, dtype=None, partition_info=None):
    full_shape = shape if partition_info is None else partition_info.full_shape
    if len(full_shape) != 2:
      raise ValueError(
          "Identity matrix initializer can only be used for 2D matrices.")
    if dtype is None:
      dtype = self.dtype
    if isinstance(full_shape, tensor_shape.TensorShape):
      full_shape = full_shape.as_list()
    initializer = linalg_ops_impl.eye(*full_shape, dtype=dtype)
    if partition_info is not None:
      initializer = array_ops.slice(initializer, partition_info.var_offset,
                                    shape)
    return self.gain * initializer

  def get_config(self):
    return {"gain": self.gain, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"gain": self.gain, "dtype": self.dtype.name}
class Initializer

Initializer base class: all Keras initializers inherit from this class.

Initializers should implement a __call__ method with the following signature:

def __call__(self, shape, dtype=None, **kwargs):
  # returns a tensor of shape `shape` and dtype `dtype`
  # containing values drawn from a distribution of your choice.

Optionally, you an also implement the method get_config and the class method from_config in order to support serialization – just like with any Keras object.

Here's a simple example: a random normal initializer.

import tensorflow as tf

class ExampleRandomNormal(tf.keras.initializers.Initializer):

  def __init__(self, mean, stddev):
    self.mean = mean
    self.stddev = stddev

  def __call__(self, shape, dtype=None, **kwargs):
    return tf.random.normal(
        shape, mean=self.mean, stddev=self.stddev, dtype=dtype)

  def get_config(self):  # To support serialization
    return {"mean": self.mean, "stddev": self.stddev}

Note that we don't have to implement from_config in the example above since the constructor arguments of the class the keys in the config returned by get_config are the same. In this case, the default from_config works fine.

Expand source code
class Initializer(object):
  """Initializer base class: all Keras initializers inherit from this class.

  Initializers should implement a `__call__` method with the following
  signature:

  ```python
  def __call__(self, shape, dtype=None, **kwargs):
    # returns a tensor of shape `shape` and dtype `dtype`
    # containing values drawn from a distribution of your choice.
  ```

  Optionally, you an also implement the method `get_config` and the class
  method `from_config` in order to support serialization -- just like with
  any Keras object.

  Here's a simple example: a random normal initializer.

  ```python
  import tensorflow as tf

  class ExampleRandomNormal(tf.keras.initializers.Initializer):

    def __init__(self, mean, stddev):
      self.mean = mean
      self.stddev = stddev

    def __call__(self, shape, dtype=None, **kwargs):
      return tf.random.normal(
          shape, mean=self.mean, stddev=self.stddev, dtype=dtype)

    def get_config(self):  # To support serialization
      return {"mean": self.mean, "stddev": self.stddev}
  ```

  Note that we don't have to implement `from_config` in the example above since
  the constructor arguments of the class the keys in the config returned by
  `get_config` are the same. In this case, the default `from_config`
  works fine.
  """

  def __call__(self, shape, dtype=None, **kwargs):
    """Returns a tensor object initialized as specified by the initializer.

    Args:
      shape: Shape of the tensor.
      dtype: Optional dtype of the tensor.
      **kwargs: Additional keyword arguments.
    """
    raise NotImplementedError

  def get_config(self):
    """Returns the configuration of the initializer as a JSON-serializable dict.

    Returns:
      A JSON-serializable Python dict.
    """
    return {}

  @classmethod
  def from_config(cls, config):
    """Instantiates an initializer from a configuration dictionary.

    Example:

    ```python
    initializer = RandomUniform(-1, 1)
    config = initializer.get_config()
    initializer = RandomUniform.from_config(config)
    ```

    Args:
      config: A Python dictionary, the output of `get_config`.

    Returns:
      A `tf.keras.initializers.Initializer` instance.
    """
    config.pop('dtype', None)
    return cls(**config)

Subclasses

Static methods

def from_config(config)

Instantiates an initializer from a configuration dictionary.

Example:

initializer = RandomUniform(-1, 1)
config = initializer.get_config()
initializer = RandomUniform.from_config(config)

Args

config
A Python dictionary, the output of get_config.

Returns

A tf.keras.initializers.Initializer instance.

Expand source code
@classmethod
def from_config(cls, config):
  """Instantiates an initializer from a configuration dictionary.

  Example:

  ```python
  initializer = RandomUniform(-1, 1)
  config = initializer.get_config()
  initializer = RandomUniform.from_config(config)
  ```

  Args:
    config: A Python dictionary, the output of `get_config`.

  Returns:
    A `tf.keras.initializers.Initializer` instance.
  """
  config.pop('dtype', None)
  return cls(**config)

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  """Returns the configuration of the initializer as a JSON-serializable dict.

  Returns:
    A JSON-serializable Python dict.
  """
  return {}
class lecun_normal (seed=None)

Initializer capable of adapting its scale to the shape of weights tensors.

With distribution="truncated_normal" or "untruncated_normal", samples are drawn from a truncated/untruncated normal distribution with a mean of zero and a standard deviation (after truncation, if used) stddev = sqrt(scale / n) where n is: - number of input units in the weight tensor, if mode = "fan_in" - number of output units, if mode = "fan_out" - average of the numbers of input and output units, if mode = "fan_avg"

With distribution="uniform", samples are drawn from a uniform distribution within [-limit, limit], with limit = sqrt(3 * scale / n).

Args

scale
Scaling factor (positive float).
mode
One of "fan_in", "fan_out", "fan_avg".
distribution
Random distribution to use. One of "normal", "uniform".
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

Raises

ValueError
In case of an invalid value for the "scale", mode" or "distribution" arguments.

DEPRECATED FUNCTION ARGUMENT VALUES (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Warning: SOME ARGUMENT VALUES ARE DEPRECATED: (distribution='normal'). They will be removed in a future version. Instructions for updating: RandomNormal is a deprecated alias for TruncatedNormal

Expand source code
class LecunNormal(tf.compat.v1.variance_scaling_initializer):

  def __init__(self, seed=None):
    super(LecunNormal, self).__init__(
        scale=1., mode='fan_in', distribution='truncated_normal', seed=seed)

  def get_config(self):
    return {'seed': self.seed}

Ancestors

  • tensorflow.python.ops.init_ops.VarianceScaling
  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {'seed': self.seed}
class lecun_uniform (seed=None)

Initializer capable of adapting its scale to the shape of weights tensors.

With distribution="truncated_normal" or "untruncated_normal", samples are drawn from a truncated/untruncated normal distribution with a mean of zero and a standard deviation (after truncation, if used) stddev = sqrt(scale / n) where n is: - number of input units in the weight tensor, if mode = "fan_in" - number of output units, if mode = "fan_out" - average of the numbers of input and output units, if mode = "fan_avg"

With distribution="uniform", samples are drawn from a uniform distribution within [-limit, limit], with limit = sqrt(3 * scale / n).

Args

scale
Scaling factor (positive float).
mode
One of "fan_in", "fan_out", "fan_avg".
distribution
Random distribution to use. One of "normal", "uniform".
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

Raises

ValueError
In case of an invalid value for the "scale", mode" or "distribution" arguments.

DEPRECATED FUNCTION ARGUMENT VALUES (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Warning: SOME ARGUMENT VALUES ARE DEPRECATED: (distribution='normal'). They will be removed in a future version. Instructions for updating: RandomNormal is a deprecated alias for TruncatedNormal

Expand source code
class LecunUniform(tf.compat.v1.variance_scaling_initializer):

  def __init__(self, seed=None):
    super(LecunUniform, self).__init__(
        scale=1., mode='fan_in', distribution='uniform', seed=seed)

  def get_config(self):
    return {'seed': self.seed}

Ancestors

  • tensorflow.python.ops.init_ops.VarianceScaling
  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {'seed': self.seed}
class Ones (dtype=tf.float32)

Initializer that generates tensors initialized to 1.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Ones(Initializer):
  """Initializer that generates tensors initialized to 1."""

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, dtype=dtypes.float32):
    self.dtype = dtypes.as_dtype(dtype)

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    return array_ops.ones(shape, dtype)

  def get_config(self):
    return {"dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"dtype": self.dtype.name}
class ones (dtype=tf.float32)

Initializer that generates tensors initialized to 1.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Ones(Initializer):
  """Initializer that generates tensors initialized to 1."""

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, dtype=dtypes.float32):
    self.dtype = dtypes.as_dtype(dtype)

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    return array_ops.ones(shape, dtype)

  def get_config(self):
    return {"dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"dtype": self.dtype.name}
class Orthogonal (gain=1.0, seed=None, dtype=tf.float32)

Initializer that generates an orthogonal matrix.

If the shape of the tensor to initialize is two-dimensional, it is initialized with an orthogonal matrix obtained from the QR decomposition of a matrix of random numbers drawn from a normal distribution. If the matrix has fewer rows than columns then the output will have orthogonal rows. Otherwise, the output will have orthogonal columns.

If the shape of the tensor to initialize is more than two-dimensional, a matrix of shape (shape[0] * ... * shape[n - 2], shape[n - 1]) is initialized, where n is the length of the shape vector. The matrix is subsequently reshaped to give a tensor of the desired shape.

Args

gain
multiplicative factor to apply to the orthogonal matrix
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

References

Saxe et al., 2014 (pdf)

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Orthogonal(Initializer):
  """Initializer that generates an orthogonal matrix.

  If the shape of the tensor to initialize is two-dimensional, it is initialized
  with an orthogonal matrix obtained from the QR decomposition of a matrix of
  random numbers drawn from a normal distribution.
  If the matrix has fewer rows than columns then the output will have orthogonal
  rows. Otherwise, the output will have orthogonal columns.

  If the shape of the tensor to initialize is more than two-dimensional,
  a matrix of shape `(shape[0] * ... * shape[n - 2], shape[n - 1])`
  is initialized, where `n` is the length of the shape vector.
  The matrix is subsequently reshaped to give a tensor of the desired shape.

  Args:
    gain: multiplicative factor to apply to the orthogonal matrix
    seed: A Python integer. Used to create random seeds. See
      `tf.compat.v1.set_random_seed` for behavior.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer. Only floating point types are supported.
  References:
      [Saxe et al., 2014](https://openreview.net/forum?id=_wzZwKpTDF_9C)
      ([pdf](https://arxiv.org/pdf/1312.6120.pdf))
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, gain=1.0, seed=None, dtype=dtypes.float32):
    self.gain = gain
    self.dtype = _assert_float_dtype(dtypes.as_dtype(dtype))
    self.seed = seed

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    # Check the shape
    if len(shape) < 2:
      raise ValueError("The tensor to initialize must be "
                       "at least two-dimensional")
    # Flatten the input shape with the last dimension remaining
    # its original shape so it works for conv2d
    num_rows = 1
    for dim in shape[:-1]:
      num_rows *= dim
    num_rows = int(num_rows)
    num_cols = int(shape[-1])
    if num_rows < num_cols:
      flat_shape = (num_cols, num_rows)
    else:
      flat_shape = (num_rows, num_cols)

    # Generate a random matrix
    a = random_ops.random_normal(flat_shape, dtype=dtype, seed=self.seed)
    # Compute the qr factorization
    q, r = gen_linalg_ops.qr(a, full_matrices=False)
    # Make Q uniform
    d = array_ops.diag_part(r)
    q *= math_ops.sign(d)
    if num_rows < num_cols:
      q = array_ops.matrix_transpose(q)
    return self.gain * array_ops.reshape(q, shape)

  def get_config(self):
    return {"gain": self.gain, "seed": self.seed, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"gain": self.gain, "seed": self.seed, "dtype": self.dtype.name}
class orthogonal (gain=1.0, seed=None, dtype=tf.float32)

Initializer that generates an orthogonal matrix.

If the shape of the tensor to initialize is two-dimensional, it is initialized with an orthogonal matrix obtained from the QR decomposition of a matrix of random numbers drawn from a normal distribution. If the matrix has fewer rows than columns then the output will have orthogonal rows. Otherwise, the output will have orthogonal columns.

If the shape of the tensor to initialize is more than two-dimensional, a matrix of shape (shape[0] * ... * shape[n - 2], shape[n - 1]) is initialized, where n is the length of the shape vector. The matrix is subsequently reshaped to give a tensor of the desired shape.

Args

gain
multiplicative factor to apply to the orthogonal matrix
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

References

Saxe et al., 2014 (pdf)

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Orthogonal(Initializer):
  """Initializer that generates an orthogonal matrix.

  If the shape of the tensor to initialize is two-dimensional, it is initialized
  with an orthogonal matrix obtained from the QR decomposition of a matrix of
  random numbers drawn from a normal distribution.
  If the matrix has fewer rows than columns then the output will have orthogonal
  rows. Otherwise, the output will have orthogonal columns.

  If the shape of the tensor to initialize is more than two-dimensional,
  a matrix of shape `(shape[0] * ... * shape[n - 2], shape[n - 1])`
  is initialized, where `n` is the length of the shape vector.
  The matrix is subsequently reshaped to give a tensor of the desired shape.

  Args:
    gain: multiplicative factor to apply to the orthogonal matrix
    seed: A Python integer. Used to create random seeds. See
      `tf.compat.v1.set_random_seed` for behavior.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer. Only floating point types are supported.
  References:
      [Saxe et al., 2014](https://openreview.net/forum?id=_wzZwKpTDF_9C)
      ([pdf](https://arxiv.org/pdf/1312.6120.pdf))
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, gain=1.0, seed=None, dtype=dtypes.float32):
    self.gain = gain
    self.dtype = _assert_float_dtype(dtypes.as_dtype(dtype))
    self.seed = seed

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    # Check the shape
    if len(shape) < 2:
      raise ValueError("The tensor to initialize must be "
                       "at least two-dimensional")
    # Flatten the input shape with the last dimension remaining
    # its original shape so it works for conv2d
    num_rows = 1
    for dim in shape[:-1]:
      num_rows *= dim
    num_rows = int(num_rows)
    num_cols = int(shape[-1])
    if num_rows < num_cols:
      flat_shape = (num_cols, num_rows)
    else:
      flat_shape = (num_rows, num_cols)

    # Generate a random matrix
    a = random_ops.random_normal(flat_shape, dtype=dtype, seed=self.seed)
    # Compute the qr factorization
    q, r = gen_linalg_ops.qr(a, full_matrices=False)
    # Make Q uniform
    d = array_ops.diag_part(r)
    q *= math_ops.sign(d)
    if num_rows < num_cols:
      q = array_ops.matrix_transpose(q)
    return self.gain * array_ops.reshape(q, shape)

  def get_config(self):
    return {"gain": self.gain, "seed": self.seed, "dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"gain": self.gain, "seed": self.seed, "dtype": self.dtype.name}
class RandomNormal (mean=0.0, stddev=0.05, seed=None, dtype=tf.float32)

Initializer that generates tensors with a normal distribution.

Args

mean
a python scalar or a scalar tensor. Mean of the random values to generate.
stddev
a python scalar or a scalar tensor. Standard deviation of the random values to generate.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class RandomNormal(tf.compat.v1.random_normal_initializer):

  def __init__(self, mean=0.0, stddev=0.05, seed=None, dtype=tf.float32):
    super(RandomNormal, self).__init__(
        mean=mean, stddev=stddev, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.RandomNormal
  • tensorflow.python.ops.init_ops.Initializer
class normal (mean=0.0, stddev=0.05, seed=None, dtype=tf.float32)

Initializer that generates tensors with a normal distribution.

Args

mean
a python scalar or a scalar tensor. Mean of the random values to generate.
stddev
a python scalar or a scalar tensor. Standard deviation of the random values to generate.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class RandomNormal(tf.compat.v1.random_normal_initializer):

  def __init__(self, mean=0.0, stddev=0.05, seed=None, dtype=tf.float32):
    super(RandomNormal, self).__init__(
        mean=mean, stddev=stddev, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.RandomNormal
  • tensorflow.python.ops.init_ops.Initializer
class random_normal (mean=0.0, stddev=0.05, seed=None, dtype=tf.float32)

Initializer that generates tensors with a normal distribution.

Args

mean
a python scalar or a scalar tensor. Mean of the random values to generate.
stddev
a python scalar or a scalar tensor. Standard deviation of the random values to generate.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class RandomNormal(tf.compat.v1.random_normal_initializer):

  def __init__(self, mean=0.0, stddev=0.05, seed=None, dtype=tf.float32):
    super(RandomNormal, self).__init__(
        mean=mean, stddev=stddev, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.RandomNormal
  • tensorflow.python.ops.init_ops.Initializer
class RandomUniform (minval=-0.05, maxval=0.05, seed=None, dtype=tf.float32)

Initializer that generates tensors with a uniform distribution.

Args

minval
A python scalar or a scalar tensor. Lower bound of the range of random values to generate.
maxval
A python scalar or a scalar tensor. Upper bound of the range of random values to generate. Defaults to 1 for float types.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class RandomUniform(tf.compat.v1.random_uniform_initializer):

  def __init__(self, minval=-0.05, maxval=0.05, seed=None,
               dtype=tf.float32):
    super(RandomUniform, self).__init__(
        minval=minval, maxval=maxval, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.RandomUniform
  • tensorflow.python.ops.init_ops.Initializer
class random_uniform (minval=-0.05, maxval=0.05, seed=None, dtype=tf.float32)

Initializer that generates tensors with a uniform distribution.

Args

minval
A python scalar or a scalar tensor. Lower bound of the range of random values to generate.
maxval
A python scalar or a scalar tensor. Upper bound of the range of random values to generate. Defaults to 1 for float types.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class RandomUniform(tf.compat.v1.random_uniform_initializer):

  def __init__(self, minval=-0.05, maxval=0.05, seed=None,
               dtype=tf.float32):
    super(RandomUniform, self).__init__(
        minval=minval, maxval=maxval, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.RandomUniform
  • tensorflow.python.ops.init_ops.Initializer
class uniform (minval=-0.05, maxval=0.05, seed=None, dtype=tf.float32)

Initializer that generates tensors with a uniform distribution.

Args

minval
A python scalar or a scalar tensor. Lower bound of the range of random values to generate.
maxval
A python scalar or a scalar tensor. Upper bound of the range of random values to generate. Defaults to 1 for float types.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class RandomUniform(tf.compat.v1.random_uniform_initializer):

  def __init__(self, minval=-0.05, maxval=0.05, seed=None,
               dtype=tf.float32):
    super(RandomUniform, self).__init__(
        minval=minval, maxval=maxval, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.RandomUniform
  • tensorflow.python.ops.init_ops.Initializer
class TruncatedNormal (mean=0.0, stddev=0.05, seed=None, dtype=tf.float32)

Initializer that generates a truncated normal distribution.

These values are similar to values from a random_normal_initializer except that values more than two standard deviations from the mean are discarded and re-drawn. This is the recommended initializer for neural network weights and filters.

Args

mean
a python scalar or a scalar tensor. Mean of the random values to generate.
stddev
a python scalar or a scalar tensor. Standard deviation of the random values to generate.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class TruncatedNormal(tf.compat.v1.truncated_normal_initializer):

  def __init__(self, mean=0.0, stddev=0.05, seed=None, dtype=tf.float32):
    super(TruncatedNormal, self).__init__(
        mean=mean, stddev=stddev, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.TruncatedNormal
  • tensorflow.python.ops.init_ops.Initializer
class truncated_normal (mean=0.0, stddev=0.05, seed=None, dtype=tf.float32)

Initializer that generates a truncated normal distribution.

These values are similar to values from a random_normal_initializer except that values more than two standard deviations from the mean are discarded and re-drawn. This is the recommended initializer for neural network weights and filters.

Args

mean
a python scalar or a scalar tensor. Mean of the random values to generate.
stddev
a python scalar or a scalar tensor. Standard deviation of the random values to generate.
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class TruncatedNormal(tf.compat.v1.truncated_normal_initializer):

  def __init__(self, mean=0.0, stddev=0.05, seed=None, dtype=tf.float32):
    super(TruncatedNormal, self).__init__(
        mean=mean, stddev=stddev, seed=seed, dtype=dtype)

Ancestors

  • tensorflow.python.ops.init_ops.TruncatedNormal
  • tensorflow.python.ops.init_ops.Initializer
class VarianceScaling (scale=1.0, mode='fan_in', distribution='truncated_normal', seed=None, dtype=tf.float32)

Initializer capable of adapting its scale to the shape of weights tensors.

With distribution="truncated_normal" or "untruncated_normal", samples are drawn from a truncated/untruncated normal distribution with a mean of zero and a standard deviation (after truncation, if used) stddev = sqrt(scale / n) where n is: - number of input units in the weight tensor, if mode = "fan_in" - number of output units, if mode = "fan_out" - average of the numbers of input and output units, if mode = "fan_avg"

With distribution="uniform", samples are drawn from a uniform distribution within [-limit, limit], with limit = sqrt(3 * scale / n).

Args

scale
Scaling factor (positive float).
mode
One of "fan_in", "fan_out", "fan_avg".
distribution
Random distribution to use. One of "normal", "uniform".
seed
A Python integer. Used to create random seeds. See tf.compat.v1.set_random_seed for behavior.
dtype
Default data type, used if no dtype argument is provided when calling the initializer. Only floating point types are supported.

Raises

ValueError
In case of an invalid value for the "scale", mode" or "distribution" arguments.

DEPRECATED FUNCTION ARGUMENT VALUES (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Warning: SOME ARGUMENT VALUES ARE DEPRECATED: (distribution='normal'). They will be removed in a future version. Instructions for updating: RandomNormal is a deprecated alias for TruncatedNormal

Expand source code
class VarianceScaling(Initializer):
  """Initializer capable of adapting its scale to the shape of weights tensors.

  With `distribution="truncated_normal" or "untruncated_normal"`,
  samples are drawn from a truncated/untruncated normal
  distribution with a mean of zero and a standard deviation (after truncation,
  if used) `stddev = sqrt(scale / n)`
  where n is:
    - number of input units in the weight tensor, if mode = "fan_in"
    - number of output units, if mode = "fan_out"
    - average of the numbers of input and output units, if mode = "fan_avg"

  With `distribution="uniform"`, samples are drawn from a uniform distribution
  within [-limit, limit], with `limit = sqrt(3 * scale / n)`.

  Args:
    scale: Scaling factor (positive float).
    mode: One of "fan_in", "fan_out", "fan_avg".
    distribution: Random distribution to use. One of "normal", "uniform".
    seed: A Python integer. Used to create random seeds. See
      `tf.compat.v1.set_random_seed` for behavior.
    dtype: Default data type, used if no `dtype` argument is provided when
      calling the initializer. Only floating point types are supported.

  Raises:
    ValueError: In case of an invalid value for the "scale", mode" or
      "distribution" arguments.
  """

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  @deprecated_arg_values(
      None,
      "`normal` is a deprecated alias for `truncated_normal`",
      distribution="normal")
  def __init__(self,
               scale=1.0,
               mode="fan_in",
               distribution="truncated_normal",
               seed=None,
               dtype=dtypes.float32):
    if scale <= 0.:
      raise ValueError("`scale` must be positive float.")
    if mode not in {"fan_in", "fan_out", "fan_avg"}:
      raise ValueError("Invalid `mode` argument:", mode)
    distribution = distribution.lower()
    if distribution not in {
        "normal", "uniform", "truncated_normal", "untruncated_normal"
    }:
      raise ValueError("Invalid `distribution` argument:", distribution)
    self.scale = scale
    self.mode = mode
    self.distribution = distribution
    self.seed = seed
    self.dtype = _assert_float_dtype(dtypes.as_dtype(dtype))

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    scale = self.scale
    scale_shape = shape
    if partition_info is not None:
      scale_shape = partition_info.full_shape
    fan_in, fan_out = _compute_fans(scale_shape)
    if self.mode == "fan_in":
      scale /= max(1., fan_in)
    elif self.mode == "fan_out":
      scale /= max(1., fan_out)
    else:
      scale /= max(1., (fan_in + fan_out) / 2.)
    if self.distribution == "normal" or self.distribution == "truncated_normal":
      # constant taken from scipy.stats.truncnorm.std(a=-2, b=2, loc=0., scale=1.)
      stddev = math.sqrt(scale) / .87962566103423978
      return random_ops.truncated_normal(
          shape, 0.0, stddev, dtype, seed=self.seed)
    elif self.distribution == "untruncated_normal":
      stddev = math.sqrt(scale)
      return random_ops.random_normal(shape, 0.0, stddev, dtype, seed=self.seed)
    else:
      limit = math.sqrt(3.0 * scale)
      return random_ops.random_uniform(
          shape, -limit, limit, dtype, seed=self.seed)

  def get_config(self):
    return {
        "scale": self.scale,
        "mode": self.mode,
        "distribution": self.distribution,
        "seed": self.seed,
        "dtype": self.dtype.name
    }

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Subclasses

  • HeNormal
  • HeUniform
  • LecunNormal
  • LecunUniform
  • tensorflow.python.keras.initializers.initializers_v1.HeNormal
  • tensorflow.python.keras.initializers.initializers_v1.HeUniform
  • tensorflow.python.keras.initializers.initializers_v1.LecunNormal
  • tensorflow.python.keras.initializers.initializers_v1.LecunUniform
  • tensorflow.python.ops.init_ops.GlorotNormal
  • tensorflow.python.ops.init_ops.GlorotUniform

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {
      "scale": self.scale,
      "mode": self.mode,
      "distribution": self.distribution,
      "seed": self.seed,
      "dtype": self.dtype.name
  }
class Zeros (dtype=tf.float32)

Initializer that generates tensors initialized to 0.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Zeros(Initializer):
  """Initializer that generates tensors initialized to 0."""

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, dtype=dtypes.float32):
    self.dtype = dtypes.as_dtype(dtype)

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    return array_ops.zeros(shape, dtype)

  def get_config(self):
    return {"dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"dtype": self.dtype.name}
class zeros (dtype=tf.float32)

Initializer that generates tensors initialized to 0.

DEPRECATED FUNCTION ARGUMENTS

Warning: SOME ARGUMENTS ARE DEPRECATED: (dtype). They will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor

Expand source code
class Zeros(Initializer):
  """Initializer that generates tensors initialized to 0."""

  @deprecated_args(None,
                   "Call initializer instance with the dtype argument instead "
                   "of passing it to the constructor", "dtype")
  def __init__(self, dtype=dtypes.float32):
    self.dtype = dtypes.as_dtype(dtype)

  def __call__(self, shape, dtype=None, partition_info=None):
    if dtype is None:
      dtype = self.dtype
    return array_ops.zeros(shape, dtype)

  def get_config(self):
    return {"dtype": self.dtype.name}

Ancestors

  • tensorflow.python.ops.init_ops.Initializer

Methods

def get_config(self)

Returns the configuration of the initializer as a JSON-serializable dict.

Returns

A JSON-serializable Python dict.

Expand source code
def get_config(self):
  return {"dtype": self.dtype.name}