From 00a50af8d5d75906ee908e9880e171eca8d8cbe8 Mon Sep 17 00:00:00 2001 From: Tao Luo Date: Tue, 28 Dec 2021 19:05:00 +0800 Subject: [PATCH] refine amax/amin example(#38525) --- python/paddle/tensor/math.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 7d790934c38..12335283cd1 100755 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1802,11 +1802,24 @@ def amax(x, axis=None, keepdim=False, name=None): x = paddle.to_tensor([[0.1, 0.9, 0.9, 0.9], [0.9, 0.9, 0.6, 0.7]], dtype='float64', stop_gradient=False) + # There are 5 maximum elements: + # 1) amax evenly distributes gradient between these equal values, + # thus the corresponding gradients are 1/5=0.2; + # 2) while max propagates gradient to all of them, + # thus the corresponding gradient are 1. result1 = paddle.amax(x) result1.backward() print(result1, x.grad) #[0.9], [[0., 0.2, 0.2, 0.2], [0.2, 0.2, 0., 0.]] + x.clear_grad() + result1_max = paddle.max(x) + result1_max.backward() + print(result1_max, x.grad) + #[0.9], [[0., 1.0, 1.0, 1.0], [1.0, 1.0, 0., 0.]] + + ############################### + x.clear_grad() result2 = paddle.amax(x, axis=0) result2.backward() @@ -1901,11 +1914,24 @@ def amin(x, axis=None, keepdim=False, name=None): x = paddle.to_tensor([[0.2, 0.1, 0.1, 0.1], [0.1, 0.1, 0.6, 0.7]], dtype='float64', stop_gradient=False) + # There are 5 minimum elements: + # 1) amin evenly distributes gradient between these equal values, + # thus the corresponding gradients are 1/5=0.2; + # 2) while min propagates gradient to all of them, + # thus the corresponding gradient are 1. result1 = paddle.amin(x) result1.backward() print(result1, x.grad) #[0.1], [[0., 0.2, 0.2, 0.2], [0.2, 0.2, 0., 0.]] + x.clear_grad() + result1_min = paddle.min(x) + result1_min.backward() + print(result1_min, x.grad) + #[0.1], [[0., 1.0, 1.0, 1.0], [1.0, 1.0, 0., 0.]] + + ############################### + x.clear_grad() result2 = paddle.amin(x, axis=0) result2.backward() -- GitLab