1- < p > This rule raises an issue when the axis argument is not provided to TensorFlow’s reduction operations.</ p >
1+ < p > This rule raises an issue when the < code > axis</ code > / < code > dim` </ code > argument is not provided to reduction operations.</ p >
22< h2 > Why is this an issue?</ h2 >
3- < p > The result of TensorFlow’s reduction operations (i.e. < code > tf.math.reduce_sum</ code > , < code > tf.math.reduce_std</ code > ), highly depends on the
4- shape of the Tensor provided.</ p >
3+ < h3 > TensorFlow</ h3 >
4+ < p > The result of reduction operations (i.e. < code > tf.math.reduce_sum</ code > , < code > tf.math.reduce_std</ code > , < code > torch.sum</ code > ,
5+ < code > torch.mean</ code > , etc…), highly depends on the shape of the Tensor provided.</ p >
56< pre >
67import tensorflow as tf
78
@@ -42,7 +43,9 @@ <h2>Why is this an issue?</h2>
4243< p > In the example above, specifying the axis clarifies the intent, as the result now is < code > [5, 7]</ code > . If the intent was to effectively reduce
4344across all dimensions the user should provide the list of axis < code > axis=[0,1]</ code > or clearly state the default behavior should be applied with
4445< code > axis=None</ code > .</ p >
45- < h2 > How to fix it</ h2 >
46+ < h3 > The PyTorch equivalent</ h3 >
47+ < p > The same behavior occurs in PyTorch, but the argument is called < code > dim</ code > instead of < code > axis</ code > .</ p >
48+ < h2 > How to fix it in TensorFlow</ h2 >
4649< p > To fix this issue provide the axis argument when using a TensorFlow reduction operation such as < code > tf.math.reduce_sum</ code > ,
4750< code > tf.math.reduce_prod</ code > , < code > tf.math.reduce_mean</ code > , etc…</ p >
4851< h3 > Code examples</ h3 >
@@ -60,6 +63,24 @@ <h4>Compliant solution</h4>
6063x = tf.constant([[1, 1, 1], [1, 1, 1]])
6164tf.math.reduce_sum(x, axis=0) # Compliant: the reduction will happen only on the axis 0, resulting in `[2,2,2]`
6265</ pre >
66+ < h2 > How to fix it in PyTorch</ h2 >
67+ < p > To fix this issue provide the dim argument when using a PyTorch reduction operation such as < code > torch.sum</ code > , < code > torch.prod</ code > ,
68+ < code > torch.mean</ code > , etc…</ p >
69+ < h3 > Code examples</ h3 >
70+ < h4 > Noncompliant code example</ h4 >
71+ < pre data-diff-id ="2 " data-diff-type ="noncompliant ">
72+ import torch
73+
74+ x = torch.tensor([[1, 1, 1], [1, 1, 1]])
75+ torch.sum(x) # Noncompliant: the dim argument defaults to None
76+ </ pre >
77+ < h4 > Compliant solution</ h4 >
78+ < pre data-diff-id ="2 " data-diff-type ="compliant ">
79+ import torch
80+
81+ x = torch.tensor([[1, 1, 1], [1, 1, 1]])
82+ torch.sum(x, dim=None) # Compliant: all dimensions will be reduced
83+ </ pre >
6384< h2 > Resources</ h2 >
6485< h3 > Documentation</ h3 >
6586< ul >
@@ -71,6 +92,7 @@ <h3>Documentation</h3>
7192 < li > TensorFlow Documentation - < a href ="https://www.tensorflow.org/api_docs/python/tf/math/reduce_sum "> tf.math.reduce_sum reference</ a > </ li >
7293 < li > TensorFlow Documentation - < a href ="https://www.tensorflow.org/api_docs/python/tf/math/reduce_variance "> tf.math.reduce_variance reference</ a >
7394 </ li >
95+ < li > PyTorch Documentation - < a href ="https://pytorch.org/docs/stable/torch.html#reduction-ops "> Reduction operations</ a > </ li >
7496</ ul >
7597< h3 > Articles & blog posts</ h3 >
7698< ul >
0 commit comments