# Build your own loss function in PyTorch

**URL:** <https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235>\
**Category:** Uncategorized\
**Created:** [January 28, 2017, 11:07am UTC](https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235 "2017-01-28T11:07:05Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![Ismail\_Elezi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ismail_elezi/32/192_2.png) [@Ismail\_Elezi](https://discuss.pytorch.org/u/Ismail_Elezi)\
**Post date:** [January 30, 2017, 10:41am UTC](https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235/19 "2017-01-30T10:41:42Z")

</div>

@apaszke

In your example, it just prints a Tensor of shape 5x5 with all ones in it. If I use a working example (for example the tutorial on CIFAR-10 dataset: [https://github.com/pytorch/tutorials/blob/master/Deep%20Learning%20with%20PyTorch.ipynb](https://github.com/pytorch/tutorials/blob/master/Deep%20Learning%20with%20PyTorch.ipynb)) - doing a single iteration - and I write:

`loss.register_hook(lambda g: print(g))`

I get:

```
Variable containing:
1
[torch.FloatTensor of size 1]

Variable containing:
1
[torch.FloatTensor of size 1]

```

which isn’t very helpful. Now, on my example, if I want all the gradients which are computed on my loss function, how can I use register\_hook to do so?

---

_[View the full topic](https://discuss.pytorch.org/t/build-your-own-loss-function-in-pytorch/235)._
