Although it is a legacy compat.v1 API, this symbol is compatible with eager
execution and tf.function.
To switch to TF2 APIs, move to using either
tf.initializers.variance_scaling or tf.keras.initializers.VarianceScaling
(neither from compat.v1) and
pass the dtype when calling the initializer.
No change to defaults.
'normal' maps to 'truncated_normal'
seed
seed
dtype
dtype
The TF2 api only takes it
as a __call__ arg, not a constructor arg.
partition_info
-
(__call__ arg in TF1) Not supported
Description
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".
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.variance_scaling_initializer\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/init_ops.py#L741-L892) |\n\nInitializer capable of adapting its scale to the shape of weights tensors.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.initializers.variance_scaling`](https://www.tensorflow.org/api_docs/python/tf/compat/v1/variance_scaling_initializer)\n\n\u003cbr /\u003e\n\n tf.compat.v1.variance_scaling_initializer(\n scale=1.0,\n mode='fan_in',\n distribution='truncated_normal',\n seed=None,\n dtype=../../../tf/dtypes#float32\n )\n\n\u003cbr /\u003e\n\nMigrate to TF2\n--------------\n\n\u003cbr /\u003e\n\n| **Caution:** This API was designed for TensorFlow v1. Continue reading for details on how to migrate from this API to a native TensorFlow v2 equivalent. See the [TensorFlow v1 to TensorFlow v2 migration guide](https://www.tensorflow.org/guide/migrate) for instructions on how to migrate the rest of your code.\n\nAlthough it is a legacy [`compat.v1`](../../../tf/compat/v1) API, this symbol is compatible with eager\nexecution and [`tf.function`](../../../tf/function).\n\nTo switch to TF2 APIs, move to using either\n`tf.initializers.variance_scaling` or [`tf.keras.initializers.VarianceScaling`](../../../tf/keras/initializers/VarianceScaling)\n(neither from [`compat.v1`](../../../tf/compat/v1)) and\npass the dtype when calling the initializer.\n\n#### Structural Mapping to TF2\n\nBefore: \n\n initializer = tf.compat.v1.variance_scaling_initializer(\n scale=scale,\n mode=mode,\n distribution=distribution\n seed=seed,\n dtype=dtype)\n\n weight_one = tf.Variable(initializer(shape_one))\n weight_two = tf.Variable(initializer(shape_two))\n\nAfter: \n\n initializer = tf.keras.initializers.VarianceScaling(\n scale=scale,\n mode=mode,\n distribution=distribution\n seed=seed)\n\n weight_one = tf.Variable(initializer(shape_one, dtype=dtype))\n weight_two = tf.Variable(initializer(shape_two, dtype=dtype))\n\n#### How to Map Arguments\n\n| TF1 Arg Name | TF2 Arg Name | Note |\n|------------------|----------------|-----------------------------------------------------------------------|\n| `scale` | `scale` | No change to defaults |\n| `mode` | `mode` | No change to defaults |\n| `distribution` | `distribution` | No change to defaults. 'normal' maps to 'truncated_normal' |\n| `seed` | `seed` | |\n| `dtype` | `dtype` | The TF2 api only takes it as a `__call__` arg, not a constructor arg. |\n| `partition_info` | - | (`__call__` arg in TF1) Not supported |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nDescription\n-----------\n\nWith `distribution=\"truncated_normal\" or \"untruncated_normal\"`,\nsamples are drawn from a truncated/untruncated normal\ndistribution with a mean of zero and a standard deviation (after truncation,\nif used) `stddev = sqrt(scale / n)`\nwhere n is:\n\n- number of input units in the weight tensor, if mode = \"fan_in\"\n- number of output units, if mode = \"fan_out\"\n- average of the numbers of input and output units, if mode = \"fan_avg\"\n\nWith `distribution=\"uniform\"`, samples are drawn from a uniform distribution\nwithin \\[-limit, limit\\], with `limit = sqrt(3 * scale / n)`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|------------------------------------------------------------------------------------------------------------------------------------------|\n| `scale` | Scaling factor (positive float). |\n| `mode` | One of \"fan_in\", \"fan_out\", \"fan_avg\". |\n| `distribution` | Random distribution to use. One of \"normal\", \"uniform\". |\n| `seed` | A Python integer. Used to create random seeds. See [`tf.compat.v1.set_random_seed`](../../../tf/compat/v1/set_random_seed) for behavior. |\n| `dtype` | Default data type, used if no `dtype` argument is provided when calling the initializer. Only floating point types are supported. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------------------------------------------------|\n| `ValueError` | In case of an invalid value for the \"scale\", mode\" or \"distribution\" arguments. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `from_config`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/init_ops.py#L75-L94) \n\n @classmethod\n from_config(\n config\n )\n\nInstantiates an initializer from a configuration dictionary.\n\n#### Example:\n\n initializer = RandomUniform(-1, 1)\n config = initializer.get_config()\n initializer = RandomUniform.from_config(config)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|-----------------------------------------------------------------------|\n| `config` | A Python dictionary. It will typically be the output of `get_config`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| An Initializer instance. ||\n\n\u003cbr /\u003e\n\n### `get_config`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/init_ops.py#L885-L892) \n\n get_config()\n\nReturns the configuration of the initializer as a JSON-serializable dict.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A JSON-serializable Python dict. ||\n\n\u003cbr /\u003e\n\n### `__call__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/init_ops.py#L858-L883) \n\n __call__(\n shape, dtype=None, partition_info=None\n )\n\nReturns a tensor object initialized as specified by the initializer.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------------|--------------------------------------------------------------------------|\n| `shape` | Shape of the tensor. |\n| `dtype` | Optional dtype of the tensor. If not provided use the initializer dtype. |\n| `partition_info` | Optional information about the possible partitioning of a tensor. |\n\n\u003cbr /\u003e"]]