Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BatchNorm to specify population variance #3402

Merged
merged 4 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18648,6 +18648,11 @@ This version of the operator has been available since version 14 of the default

current_mean = ReduceMean(X, axis=all_except_channel_index)
current_var = ReduceVar(X, axis=all_except_channel_index)

Notice that ReduceVar refers to the population variance, and it equals to
sum(sqrd(x_i - x_avg)) / N
where N is the population size (this formula does not use sample size N - 1).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to consider the comment here: #3406 (comment)


```

When training_mode=False:
Expand Down Expand Up @@ -18697,7 +18702,7 @@ This version of the operator has been available since version 14 of the default
<dt><tt>running_mean</tt> (optional, non-differentiable) : T</dt>
<dd>The running mean after the BatchNormalization operator.</dd>
<dt><tt>running_var</tt> (optional, non-differentiable) : T</dt>
<dd>The running variance after the BatchNormalization operator.</dd>
<dd>The running variance after the BatchNormalization operator. This op uses the population size (N) for calculating variance, and not the sample size N-1.</dd>
</dl>

#### Type Constraints
Expand Down
35 changes: 20 additions & 15 deletions docs/Operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,11 @@ expect(node, inputs=[x], outputs=[y], name='test_averagepool_3d_default')

current_mean = ReduceMean(X, axis=all_except_channel_index)
current_var = ReduceVar(X, axis=all_except_channel_index)

Notice that ReduceVar refers to the population variance, and it equals to
sum(sqrd(x_i - x_avg)) / N
where N is the population size (this formula does not use sample size N - 1).

```

When training_mode=False:
Expand Down Expand Up @@ -1929,7 +1934,7 @@ Other versions of this operator: <a href="Changelog.md#BatchNormalization-1">1</
<dt><tt>running_mean</tt> (optional, non-differentiable) : T</dt>
<dd>The running mean after the BatchNormalization operator.</dd>
<dt><tt>running_var</tt> (optional, non-differentiable) : T</dt>
<dd>The running variance after the BatchNormalization operator.</dd>
<dd>The running variance after the BatchNormalization operator. This op uses the population size (N) for calculating variance, and not the sample size N-1.</dd>
</dl>

#### Type Constraints
Expand All @@ -1946,12 +1951,12 @@ Other versions of this operator: <a href="Changelog.md#BatchNormalization-1">1</
<summary>batchnormalization</summary>

```python
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
mean = np.random.randn(3).astype(np.float32)
var = np.random.rand(3).astype(np.float32)
y = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)

node = onnx.helper.make_node(
Expand All @@ -1960,7 +1965,7 @@ node = onnx.helper.make_node(
outputs=['y'],
)

# output size: (1, 2, 1, 3)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
name='test_batchnorm_example')

Expand Down Expand Up @@ -1992,12 +1997,12 @@ expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
<summary>train</summary>

```python
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
mean = np.random.randn(3).astype(np.float32)
var = np.random.rand(3).astype(np.float32)
# using np.bool(1) while generating test data with "'bool' object has no attribute 'dtype'"
# working around by using np.byte(1).astype(bool)
training_mode = 1
Expand All @@ -2010,7 +2015,7 @@ node = onnx.helper.make_node(
training_mode=training_mode
)

# output size: (1, 2, 1, 3)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias, mean, var],
outputs=[y, output_mean, output_var],
name='test_batchnorm_example_training_mode')
Expand Down
28 changes: 14 additions & 14 deletions docs/TestCoverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -1323,12 +1323,12 @@ There are 2 test cases, listed as following:
<summary>batchnormalization</summary>

```python
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
mean = np.random.randn(3).astype(np.float32)
var = np.random.rand(3).astype(np.float32)
y = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)

node = onnx.helper.make_node(
Expand All @@ -1337,7 +1337,7 @@ node = onnx.helper.make_node(
outputs=['y'],
)

# output size: (1, 2, 1, 3)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
name='test_batchnorm_example')

Expand Down Expand Up @@ -1367,12 +1367,12 @@ expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
<summary>train</summary>

```python
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
mean = np.random.randn(3).astype(np.float32)
var = np.random.rand(3).astype(np.float32)
# using np.bool(1) while generating test data with "'bool' object has no attribute 'dtype'"
# working around by using np.byte(1).astype(bool)
training_mode = 1
Expand All @@ -1385,7 +1385,7 @@ node = onnx.helper.make_node(
training_mode=training_mode
)

# output size: (1, 2, 1, 3)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias, mean, var],
outputs=[y, output_mean, output_var],
name='test_batchnorm_example_training_mode')
Expand Down
28 changes: 14 additions & 14 deletions onnx/backend/test/case/node/batchnorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def _batchnorm_training_mode(x, s, bias, mean, var, momentum=0.9, epsilon=1e-5):
class BatchNormalization(Base):
@staticmethod
def export(): # type: () -> None
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
mean = np.random.randn(3).astype(np.float32)
var = np.random.rand(3).astype(np.float32)
y = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)

node = onnx.helper.make_node(
Expand All @@ -49,7 +49,7 @@ def export(): # type: () -> None
outputs=['y'],
)

# output size: (1, 2, 1, 3)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias, mean, var], outputs=[y],
name='test_batchnorm_example')

Expand All @@ -75,12 +75,12 @@ def export(): # type: () -> None

@staticmethod
def export_train(): # type: () -> None
# input size: (1, 2, 1, 3)
x = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)
s = np.array([1.0, 1.5]).astype(np.float32)
bias = np.array([0, 1]).astype(np.float32)
mean = np.array([0, 3]).astype(np.float32)
var = np.array([1, 1.5]).astype(np.float32)
# input size: (2, 3, 4, 5)
x = np.random.randn(2, 3, 4, 5).astype(np.float32)
s = np.random.randn(3).astype(np.float32)
bias = np.random.randn(3).astype(np.float32)
mean = np.random.randn(3).astype(np.float32)
var = np.random.rand(3).astype(np.float32)
# using np.bool(1) while generating test data with "'bool' object has no attribute 'dtype'"
# working around by using np.byte(1).astype(bool)
training_mode = 1
Expand All @@ -93,7 +93,7 @@ def export_train(): # type: () -> None
training_mode=training_mode
)

# output size: (1, 2, 1, 3)
# output size: (2, 3, 4, 5)
expect(node, inputs=[x, s, bias, mean, var],
outputs=[y, output_mean, output_var],
name='test_batchnorm_example_training_mode')
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BsJ ٺ�>*������>
BsJ ��6?�a:���V?
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BbiasJ ǩ?3�1��9
BbiasJ =��?tt���K
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BmeanJ r�޾���?�,?
BmeanJ �D��f�<����
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BvarJ ǻy?�[?n�?<
BvarJ c�6=l�L?W��=
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BsJ ٺ�>*������>
BsJ ��6?�a:���V?
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BbiasJ ǩ?3�1��9
BbiasJ =��?tt���K
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BmeanJ r�޾���?�,?
BmeanJ �D��f�<����
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BvarJ ǻy?�[?n�?<
BvarJ c�6=l�L?W��=
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
B output_meanJ ��¾u�?N2?
B output_meanJ X���� 2�����
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
B
output_varJ �`v?�/c?P� >
output_varJ �>�X?��!>
20 changes: 10 additions & 10 deletions onnx/backend/test/data/node/test_batchnorm_example/model.onnx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
vary"BatchNormalizationtest_batchnorm_exampleZ
x




Z


Z
s


Z
Z
bias


Z
Z
mean


Z
Z
var


b
b
y




B


B
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ output_var"BatchNormalization*
training_mode�$test_batchnorm_example_training_modeZ
x




Z


Z
s


Z
Z
bias


Z
Z
mean


Z
Z
var


b
b
y




b


b
output_mean


b
b

output_var


B
B
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
B
output_varJwww?UU�?
B
output_varJ �`v?�/c?P� >
8 changes: 7 additions & 1 deletion onnx/defs/nn/defs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,11 @@ Y = (X - current_mean) / sqrt(current_var + epsilon) * scale + B

current_mean = ReduceMean(X, axis=all_except_channel_index)
current_var = ReduceVar(X, axis=all_except_channel_index)

Notice that ReduceVar refers to the population variance, and it equals to
sum(sqrd(x_i - x_avg)) / N
where N is the population size (this formula does not use sample size N - 1).

```

When training_mode=False:
Expand Down Expand Up @@ -1718,7 +1723,8 @@ ONNX_OPERATOR_SET_SCHEMA(
.Output(
2,
"running_var",
"The running variance after the BatchNormalization operator.",
"The running variance after the BatchNormalization operator. This op uses the population size (N) for "
"calculating variance, and not the sample size N-1.",
"T",
OpSchema::Optional,
true,
Expand Down