Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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.
|
| 16 |
|
| 17 |
def forward(self, x):
|
| 18 |
-
|
| 19 |
-
mu = torch.exp(
|
| 20 |
-
alpha = torch.
|
| 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'))
|