Here’ s my demo and why x_binary
's grad is always None ?
I do understand the grad of torch.sign() does not exist mathematically, but F.conv2d should give x_binary
grad even though it won’t be passed to x
.
import torch
import torch.nn.functional as F
x = torch.randn(2,3,4,4, requires_grad=True)
# print x
x_binary = x.sign().requires_grad_()
# print x_binary
weight = torch.ones(3,3,1,1, requires_grad=True)
# print weight
out = F.conv2d(x_binary, weight, None, 1, 0, 1, 1)
# out = F.conv2d(x, weight, None, 1, 0, 1, 1)
# print out
loss = out.mean()
print loss
tensor(-0.3750)
print x.grad, x_binary.grad
None None
loss.backward()
print x.grad, x_binary.grad
tensor([[[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]],
[[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]]]) None
print weight.grad
tensor([[[[-6.2500e-02]],
[[-6.2500e-02]],
[[ 1.8626e-09]]],
[[[-6.2500e-02]],
[[-6.2500e-02]],
[[ 1.8626e-09]]],
[[[-6.2500e-02]],
[[-6.2500e-02]],
[[ 1.8626e-09]]]])