Skip to content

Commit 1440f36

Browse files
authored
Change BCELoss in MLP model (microsoft#756)
1 parent f1fae41 commit 1440f36

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

qlib/contrib/model/pytorch_nn.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def get_loss(self, pred, w, target, loss_type):
267267
loss = torch.mul(sqr_loss, w).mean()
268268
return loss
269269
elif loss_type == "binary":
270-
loss = nn.BCELoss(weight=w)
270+
loss = nn.BCEWithLogitsLoss(weight=w)
271271
return loss(pred, target)
272272
else:
273273
raise NotImplementedError("loss {} is not supported!".format(loss_type))
@@ -334,16 +334,8 @@ def __init__(self, input_dim, output_dim, layers=(256, 512, 768, 512, 256, 128,
334334
dnn_layers.append(seq)
335335
drop_input = nn.Dropout(0.05)
336336
dnn_layers.append(drop_input)
337-
if loss == "mse":
338-
fc = nn.Linear(hidden_units, output_dim)
339-
dnn_layers.append(fc)
340-
341-
elif loss == "binary":
342-
fc = nn.Linear(hidden_units, output_dim)
343-
sigmoid = nn.Sigmoid()
344-
dnn_layers.append(nn.Sequential(fc, sigmoid))
345-
else:
346-
raise NotImplementedError("loss {} is not supported!".format(loss))
337+
fc = nn.Linear(hidden_units, output_dim)
338+
dnn_layers.append(fc)
347339
# optimizer
348340
self.dnn_layers = nn.ModuleList(dnn_layers)
349341
self._weight_init()

0 commit comments

Comments
 (0)