# Gradient of Image in PyTorch - for Gradient Penalty calculation in WGAN

I am following this Github Repo for the WGAN implementation with Gradient Penalty.

And I am trying to understand the following method, which does the job of unit-testing the gradient-penalty calulations.

``````def test_gradient_penalty(image_shape):

image_size = torch.prod(torch.Tensor(image_shape[1:]))

assert torch.abs(random_gradient_penalty - 1) < 0.1

# Now pass tuple argument for image dimenstion of
# (batch_size, channel, height, width)
``````

### I donâ€™t understand the below line

`good_gradient = torch.ones(*image_shape) / torch.sqrt(image_size)`

In above the `torch.ones(*image_shape)` is just filling a 4-D Tensor filled up with 1 and then
`torch.sqrt(image_size)` is just representing the value of `tensor(28.)`

### So, what I am trying to understand why I need to divide the 4-D Tensor by `tensor(28.)` to get the `good_gradient`

If I print bad_gradient, it will be a 4-D Tensor as below

``````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.]]],

---
---
``````

If I print `good_gradient`, the output will be

``````tensor([[[[0.0357, 0.0357, 0.0357,  ..., 0.0357, 0.0357, 0.0357],
[0.0357, 0.0357, 0.0357,  ..., 0.0357, 0.0357, 0.0357],
[0.0357, 0.0357, 0.0357,  ..., 0.0357, 0.0357, 0.0357],
...,
[0.0357, 0.0357, 0.0357,  ..., 0.0357, 0.0357, 0.0357],
[0.0357, 0.0357, 0.0357,  ..., 0.0357, 0.0357, 0.0357],
[0.0357, 0.0357, 0.0357,  ..., 0.0357, 0.0357, 0.0357]]],

---
---
``````