utterworks / fast-bert

Super easy library for BERT based NLP models
Apache License 2.0
1.85k stars 342 forks source link

i write a model for single class, but get y_pred like this [0.4,0.5] #180

Open wac81 opened 4 years ago

wac81 commented 4 years ago

I write a model for a single class and labels is 2 label neg and pos, but get y_pred like this [0.4,0.5]

then i can't get F1 because fbeta is error! and in your code:

def fbeta(
    y_pred: Tensor,
    y_true: Tensor,
    thresh: float = 0.3,
    beta: float = 2,
    eps: float = 1e-9,
    sigmoid: bool = True,
):
    "Computes the f_beta between `preds` and `targets`"
    beta2 = beta ** 2
    if sigmoid:
        y_pred = y_pred.sigmoid()
    y_pred = (y_pred > thresh).float()
    y_true = y_true.float()
    TP = (y_pred * y_true).sum(dim=1)

why not like this [0.4]?

wac81 commented 4 years ago
Traceback (most recent call last):
  File "/home/wac/fast-bert/single_classifier.py", line 76, in <module>
    optimizer_type="lamb")
  File "/home/wac/fast-bert/fast_bert/learner_cls.py", line 405, in fit
    results = self.validate()
  File "/home/wac/fast-bert/fast_bert/learner_cls.py", line 523, in validate
    all_logits, all_labels
  File "/home/wac/fast-bert/fast_bert/metrics.py", line 112, in F1
    return fbeta(y_pred, y_true, thresh=threshold, beta=1)
  File "/home/wac/fast-bert/fast_bert/metrics.py", line 58, in fbeta
    TP = (y_pred * y_true).sum(dim=1)
  File "/home/wac/.local/lib/python3.6/site-packages/apex/amp/wrap.py", line 53, in wrapper
    return orig_fn(*args, **kwargs)
RuntimeError: The size of tensor a (2) must match the size of tensor b (860) at non-singleton dimension 1
aaronbriel commented 4 years ago

I think this is because y_pred contains values for pos and neg. I think you would thus need to customize the fbeta by adding logic to convert y_pred to 'pos' or 'neg' based on these values - perhaps whichever has the highest value, then compare that to y_true.

SC4RECOIN commented 4 years ago

softmax on final layer