AnsenH's picture
feat: add our model
24615d9
raw
history blame contribute delete
921 Bytes
import torch
def binary_accuracy(y_pred, y_true):
assert y_true.ndim == 1 and y_true.size() == y_pred.size()
y_prob = torch.sigmoid(y_pred)
y_prob = y_prob > 0.5
return (y_true == y_prob).float().sum().item() / y_true.size(0)
def precision(y_pred, y_true):
assert y_true.ndim == 1 and y_true.size() == y_pred.size()
y_prob = torch.sigmoid(y_pred)
y_prob = y_prob > 0.5
y_positive = y_true >= 1
tp = (y_positive * y_prob).float().sum().item()
n_positive = y_positive.float().sum().item()
if tp == 0: return 0
return tp / n_positive
def recall(y_pred, y_true):
assert y_true.ndim == 1 and y_true.size() == y_pred.size()
y_prob = torch.sigmoid(y_pred)
y_prob = y_prob > 0.5
y_positive = y_true >= 1
tp = (y_positive * y_prob).float().sum().item()
n_pred_positive = y_prob.float().sum().item()
if tp == 0: return 0
return tp / n_pred_positive