use candle_core::{Result, Tensor};
use candle_nn::loss::{binary_cross_entropy_with_logit, cross_entropy, mse, nll};
#[derive(Debug, Clone)]
pub enum LossKind {
NLL, CrossEntropy, MSE, BCEWithLogits, }
#[derive(Debug, Clone)]
pub struct LossFn {
kind: LossKind,
}
impl LossFn {
pub fn new(kind: LossKind) -> Self {
Self { kind }
}
pub fn compute(&self, inp: &Tensor, target: &Tensor) -> Result<Tensor> {
match self.kind {
LossKind::NLL => nll(inp, target),
LossKind::CrossEntropy => cross_entropy(inp, target),
LossKind::MSE => mse(inp, target),
LossKind::BCEWithLogits => binary_cross_entropy_with_logit(inp, target),
}
}
}