Skip to content

Commit 4509b0a

Browse files
authored
NB loss and poisson loss
1 parent de548ef commit 4509b0a

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

spatial/models/monet_ae.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,22 @@ def __init__(
9898

9999
@staticmethod
100100
def calc_loss(pred, val, losstype, celltype_data=None, celltype=None):
101-
# standard losses
102101
if losstype == "mse_against_log1pdata":
103-
return torch.mean((pred - torch.log(1 + val)) ** 2)
102+
return torch.mean((pred - torch.log1p(val)) ** 2)
104103
elif losstype == "mse":
105104
return torch.mean((pred - val) ** 2)
106105
elif losstype == "mae":
107106
return torch.mean(torch.abs(pred - val))
107+
elif losstype == "poisson_nll":
108+
mu = F.softplus(pred) # ensure positive mean
109+
dist = Poisson(mu)
110+
return -dist.log_prob(val).mean()
111+
elif losstype == "nb_nll":
112+
mu = F.softplus(pred) # mean
113+
theta = torch.tensor(1.0, device=pred.device) # dispersion
114+
probs = mu / (mu + theta)
115+
dist = NegativeBinomial(total_count=theta, probs=probs)
116+
return -dist.log_prob(val).mean()
108117
else:
109118
raise NotImplementedError
110119

0 commit comments

Comments
 (0)