# Poisson loss function

**URL:** <https://discuss.pytorch.org/t/poisson-loss-function/44301>\
**Category:** Uncategorized\
**Created:** [May 3, 2019, 5:45pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301 "2019-05-03T17:45:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chilango](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@chilango](https://discuss.pytorch.org/u/chilango)\
**Post date:** [May 3, 2019, 5:45pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301/1 "2019-05-03T17:45:50Z")

</div>

I want to predict count data using a simple fully connected network. I could in principle frame it as a classification problem where each class corresponds to the event count, but I would like to do it properly using a Poisson loss function. Two quick questions:

1. I can’t seem to find the implementation of this loss function, am I missing anything?
2. I also cannot seem to find any examples of using pytorch to predict count data, any pointers?

Thank you.

---

<div class="post-metadata">

**Author:** ![Tony-Y](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony-y/32/8373_2.png) [@Tony-Y](https://discuss.pytorch.org/u/Tony-Y)\
**Post date:** [May 4, 2019, 1:02pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301/2 "2019-05-04T13:02:49Z")

</div>

> **[Loss Functions in Neural Networks | Isaac Changhau](https://isaacchanghau.github.io/post/loss_functions/)**
>
> Loss function is an important part in artificial neural networks, which is used to measure the inconsistency between predicted value ($\\hat{y}$) and actual label ($y$). It is a non-negative value, where the robustness of model increases along with...

This article provides the definition of the Poisson loss. You can calculate it using a few pytorch functions.

---

<div class="post-metadata">

**Author:** ![chilango](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@chilango](https://discuss.pytorch.org/u/chilango)\
**Post date:** [May 8, 2019, 2:11pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301/3 "2019-05-08T14:11:00Z")

</div>

Super simple question: since lambda \> 0 [because of the log(lambda\_hat)] how could I go about defining the loss function to guarantee proper lambdas? In principle implementing it with pytorch functions is straightforward:

```auto
def poissonLoss(predicted, observed):
    """Custom loss function for Poisson model."""
    loss=torch.mean(predicted-observed*torch.log(predicted))
    return loss

```

But I obviously need to force the output to be strictly positive otherwise I’ll get -inf and nans. What sort of correction for the last layer could I use? Like ReLU with a floor\>0 of sorts?

---

<div class="post-metadata">

**Author:** ![Tony-Y](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony-y/32/8373_2.png) [@Tony-Y](https://discuss.pytorch.org/u/Tony-Y)\
**Post date:** [May 8, 2019, 4:19pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301/4 "2019-05-08T16:19:51Z")

</div>

> **[Poisson regression](https://en.m.wikipedia.org/wiki/Poisson_regression)**
>
> In statistics, Poisson regression is a generalized linear model form of regression analysis used to model count data and contingency tables. Poisson regression assumes the response variable Y has a Poisson distribution, and assumes the logarithm of its expected value can be modeled by a linear combination of unknown parameters. A Poisson regression model is sometimes known as a log-linear model, especially when used to model contingency tables.
> Negative binomial regression is a popular generaliz...

Lambda should be exp(theta \* x).

---

<div class="post-metadata">

**Author:** ![Intel\_Novel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/intel_novel/32/9466_2.png) [@Intel\_Novel](https://discuss.pytorch.org/u/Intel_Novel)\
**Post date:** [May 8, 2019, 10:43pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301/5 "2019-05-08T22:43:18Z")

</div>

I think that Poisson loss function is not supported by default in PyTorch. You may try this:

> [@\[Solved\] How to implement custom loss function?](https://discuss.pytorch.org/t/solved-how-to-implement-custom-loss-function/27009/6):
>
> I’ve read your loss function but I can’t see what is the difference except you use torch and F’s operation (like torch.mean(), F.softplus()) at the end of each function This is code I’m writing https://github.com/youngminpark2559/test/tree/master/update\_CNN Would you tell me what is the difference? Generating network https://github.com/youngminpark2559/test/blob/master/update\_CNN/train/train.py#L65 Obtaining prediction https://github.com/youngminpark2559/test/blob/master/update\_CNN/train…

BTW, I think that one should focus on loss functions that can manage learning rates self adaption instead of probabilities.

---

<div class="post-metadata">

**Author:** ![chilango](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/e47c2d/32.png) [@chilango](https://discuss.pytorch.org/u/chilango)\
**Post date:** [May 10, 2019, 2:33pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301/6 "2019-05-10T14:33:28Z")

</div>

I’ve implemented the following poisson loss function using the first suggested link:

```auto
def poissonLoss(xbeta, y):
    """Custom loss function for Poisson model."""
    loss=torch.mean(torch.exp(xbeta)-y*xbeta)
    return loss

```

Training:

```auto
optimizer=torch.optim.SGD(net.parameters())
lossFn=poissonLoss

output_ = net(input_)
loss = lossFn(output_, target_)
net.zero_grad()
loss.backward()
optimizer.step()

```

Since I’m using pytorch internals, is this the right way to train/backprop/optimize? Not sure about this because my model is not learning.

Thanks,

---

<div class="post-metadata">

**Author:** ![Tony-Y](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tony-y/32/8373_2.png) [@Tony-Y](https://discuss.pytorch.org/u/Tony-Y)\
**Post date:** [May 10, 2019, 3:44pm UTC](https://discuss.pytorch.org/t/poisson-loss-function/44301/7 "2019-05-10T15:44:16Z")

</div>

> [@chilango](#):
>
> net.zero\_grad()

You should use optimizer.zero\_grad() instead of it.

[https://pytorch.org/tutorials/beginner/pytorch\_with\_examples.html#pytorch-optim](https://pytorch.org/tutorials/beginner/pytorch_with_examples.html#pytorch-optim)
