Module keras.initializers.initializers_v1

Keras initializers for TF 1.

Expand source code
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Keras initializers for TF 1."""

import tensorflow.compat.v2 as tf
from tensorflow.python.util.tf_export import keras_export


_v1_zeros_initializer = tf.compat.v1.zeros_initializer
_v1_ones_initializer = tf.compat.v1.ones_initializer
_v1_constant_initializer = tf.compat.v1.constant_initializer
_v1_variance_scaling_initializer = tf.compat.v1.variance_scaling_initializer
_v1_orthogonal_initializer = tf.compat.v1.orthogonal_initializer
_v1_identity = tf.compat.v1.initializers.identity
_v1_glorot_uniform_initializer = tf.compat.v1.glorot_uniform_initializer
_v1_glorot_normal_initializer = tf.compat.v1.glorot_normal_initializer

keras_export(v1=['keras.initializers.Zeros', 'keras.initializers.zeros'], allow_multiple_exports=True)(
    _v1_zeros_initializer)
keras_export(v1=['keras.initializers.Ones', 'keras.initializers.ones'], allow_multiple_exports=True)(
    _v1_ones_initializer)
keras_export(v1=['keras.initializers.Constant', 'keras.initializers.constant'], allow_multiple_exports=True)(
    _v1_constant_initializer)
keras_export(v1=['keras.initializers.VarianceScaling'], allow_multiple_exports=True)(
    _v1_variance_scaling_initializer)
keras_export(v1=['keras.initializers.Orthogonal',
                 'keras.initializers.orthogonal'], allow_multiple_exports=True)(_v1_orthogonal_initializer)
keras_export(v1=['keras.initializers.Identity',
                 'keras.initializers.identity'], allow_multiple_exports=True)(_v1_identity)
keras_export(v1=['keras.initializers.glorot_uniform'], allow_multiple_exports=True)(
    _v1_glorot_uniform_initializer)
keras_export(v1=['keras.initializers.glorot_normal'], allow_multiple_exports=True)(
    _v1_glorot_normal_initializer)


@keras_export(v1=['keras.initializers.RandomNormal',
                  'keras.initializers.random_normal',
                  'keras.initializers.normal'])
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)


@keras_export(v1=['keras.initializers.RandomUniform',
                  'keras.initializers.random_uniform',
                  'keras.initializers.uniform'])
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)


@keras_export(v1=['keras.initializers.TruncatedNormal',
                  'keras.initializers.truncated_normal'])
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)


@keras_export(v1=['keras.initializers.lecun_normal'])
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}


@keras_export(v1=['keras.initializers.lecun_uniform'])
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}


@keras_export(v1=['keras.initializers.he_normal'])
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}


@keras_export(v1=['keras.initializers.he_uniform'])
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}

Classes

class HeNormal (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: normal is a deprecated alias for truncated_normal

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 HeUniform (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: normal is a deprecated alias for truncated_normal

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 LecunNormal (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: normal is a deprecated alias for truncated_normal

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 LecunUniform (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: normal is a deprecated alias for truncated_normal

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 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 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 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