tf_agents.utils.tensor_normalizer.parallel_variance_calculation
Stay organized with collections
Save and categorize content based on your preferences.
Calculate the sufficient statistics (average & second moment) of two sets.
tf_agents.utils.tensor_normalizer.parallel_variance_calculation(
n_a: tf_agents.typing.types.Int
,
avg_a: tf_agents.typing.types.Float
,
m2_a: tf_agents.typing.types.Float
,
n_b: tf_agents.typing.types.Int
,
avg_b: tf_agents.typing.types.Float
,
m2_b: tf_agents.typing.types.Float
,
m2_b_c: tf_agents.typing.types.Float
) -> Tuple[tf_agents.typing.types.Int
, tf_agents.typing.types.Float
, tf_agents.typing.types.Float
, tf_agents.typing.types.Float
]
For better precision if sets are of different sizes, a
should be the smaller
and b
the bigger.
For more details, see the parallel algorithm of Chan et al. at:
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
For stability we use kahan_summation
to accumulate second moments.
Takes in the sufficient statistics for sets A
and B
and calculates the
variance and sufficient statistics for the union of A
and B
.
If e.g. B
is a single observation x_b
, use n_b=1
, avg_b = x_b
, and
m2_b = 0
.
To get avg_a
and m2_a
from a tensor x
of shape [n_a, ...]
, use:
n_a = tf.shape(x)[0]
avg_a = tf.math.reduce_mean(x, axis=[0])
m2_a = tf.math.reduce_sum(tf.math.squared_difference(t, avg_a), axis=[0])
Args |
n_a
|
Number of elements in A .
|
avg_a
|
The sample average of A .
|
m2_a
|
The sample second moment of A .
|
n_b
|
Number of elements in B .
|
avg_b
|
The sample average of B .
|
m2_b
|
The sample second moment of B .
|
m2_b_c
|
Carry for accumulation of the sample second moment of B .
|
Returns |
A tuple (n_ab, avg_ab, m2_ab, m2_ab_c) such that var_ab ,
the variance of A|B , may be calculated via var_ab = m2_ab / n_ab ,
and the sample variance assample_var_ab = m2_ab / (n_ab - 1) .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-04-26 UTC.
[[["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_agents.utils.tensor_normalizer.parallel_variance_calculation\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/utils/tensor_normalizer.py#L397-L450) |\n\nCalculate the sufficient statistics (average \\& second moment) of two sets. \n\n tf_agents.utils.tensor_normalizer.parallel_variance_calculation(\n n_a: ../../../tf_agents/typing/types/Int,\n avg_a: ../../../tf_agents/typing/types/Float,\n m2_a: ../../../tf_agents/typing/types/Float,\n n_b: ../../../tf_agents/typing/types/Int,\n avg_b: ../../../tf_agents/typing/types/Float,\n m2_b: ../../../tf_agents/typing/types/Float,\n m2_b_c: ../../../tf_agents/typing/types/Float\n ) -\u003e Tuple[../../../tf_agents/typing/types/Int, ../../../tf_agents/typing/types/Float, ../../../tf_agents/typing/types/Float, ../../../tf_agents/typing/types/Float]\n\nFor better precision if sets are of different sizes, `a` should be the smaller\nand `b` the bigger.\n\nFor more details, see the parallel algorithm of Chan et al. at:\n\u003chttps://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm\u003e\n\nFor stability we use `kahan_summation` to accumulate second moments.\n\nTakes in the sufficient statistics for sets `A` and `B` and calculates the\nvariance and sufficient statistics for the union of `A` and `B`.\n\nIf e.g. `B` is a single observation `x_b`, use `n_b=1`, `avg_b = x_b`, and\n`m2_b = 0`.\n\nTo get `avg_a` and `m2_a` from a tensor `x` of shape `[n_a, ...]`, use: \n\n n_a = tf.shape(x)[0]\n avg_a = tf.math.reduce_mean(x, axis=[0])\n m2_a = tf.math.reduce_sum(tf.math.squared_difference(t, avg_a), axis=[0])\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|------------------------------------------------------------|\n| `n_a` | Number of elements in `A`. |\n| `avg_a` | The sample average of `A`. |\n| `m2_a` | The sample second moment of `A`. |\n| `n_b` | Number of elements in `B`. |\n| `avg_b` | The sample average of `B`. |\n| `m2_b` | The sample second moment of `B`. |\n| `m2_b_c` | Carry for accumulation of the sample second moment of `B`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A tuple `(n_ab, avg_ab, m2_ab, m2_ab_c)` such that `var_ab`, the variance of `A|B`, may be calculated via `var_ab = m2_ab / n_ab`, and the sample variance as`sample_var_ab = m2_ab / (n_ab - 1)`. ||\n\n\u003cbr /\u003e"]]