jonathanjordan21 commited on
Commit
7616bd7
·
verified ·
1 Parent(s): 571904c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -14
app.py CHANGED
@@ -12,23 +12,14 @@ class NegBinomialModel(nn.Module):
12
  def __init__(self, in_features):
13
  super().__init__()
14
  self.linear = nn.Linear(in_features, 1)
15
- self.log_alpha = nn.Parameter(torch.tensor(0.0))
16
 
17
  def forward(self, x):
18
- eta = self.linear(x)
19
- mu = torch.exp(eta).squeeze(-1)
20
- alpha = torch.exp(self.log_alpha)
21
- return mu, alpha
22
 
23
- def negbinom_loss(y, mu, alpha):
24
- log_prob = (
25
- torch.lgamma(y + 1/alpha)
26
- - torch.lgamma(1/alpha)
27
- - torch.lgamma(y + 1)
28
- + (1/alpha) * torch.log(1 / (1 + alpha * mu))
29
- + y * torch.log((alpha * mu) / (1 + alpha * mu))
30
- )
31
- return -torch.mean(log_prob)
32
 
33
  model = NegBinomialModel(17)
34
  model.load_state_dict(torch.load("model_weights.pt", map_location='cpu'))
 
12
  def __init__(self, in_features):
13
  super().__init__()
14
  self.linear = nn.Linear(in_features, 1)
15
+ self.alpha = nn.Parameter(torch.tensor(0.5))
16
 
17
  def forward(self, x):
18
+ # safer activation than exp()
19
+ mu = torch.exp(torch.clamp(self.linear(x), min=-5, max=5))
20
+ alpha = torch.clamp(self.alpha, min=1e-3, max=10)
21
+ return mu.squeeze(), alpha
22
 
 
 
 
 
 
 
 
 
 
23
 
24
  model = NegBinomialModel(17)
25
  model.load_state_dict(torch.load("model_weights.pt", map_location='cpu'))