include/tenzr/modules/nn/loss.h¶
Namespaces¶
| Name |
|---|
| tenzr |
| tenzr::nn |
Classes¶
| Name | |
|---|---|
| class | tenzr::nn::MSELoss |
| class | tenzr::nn::BCELoss Callable wrapper around the binary cross-entropy loss. |
| class | tenzr::nn::CrossEntropyLoss Callable wrapper around the cross-entropy loss over logits. |
Source code¶
// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef TENZR_MODULES_NN_LOSS_H_
#define TENZR_MODULES_NN_LOSS_H_
#include <string>
#include "tenzr/frontend/tensor.h"
namespace tenzr::nn {
// Functional Loss APIs
Tensor MseLoss(const Tensor& pred, const Tensor& target, std::string reduction = "mean");
Tensor BceLoss(const Tensor& pred, const Tensor& target, double eps = 1.0e-7, std::string reduction = "mean");
Tensor cross_entropy_loss(const Tensor& logits, const Tensor& targets, double eps = 1.0e-7, std::string reduction = "mean");
// Class-based Loss APIs
class MSELoss {
public:
explicit MSELoss(std::string reduction = "mean") : reduction_(std::move(reduction)) {}
Tensor operator()(const Tensor& pred, const Tensor& target) const { return MseLoss(pred, target, reduction_); }
Tensor Forward(const Tensor& pred, const Tensor& target) const { return operator()(pred, target); }
private:
std::string reduction_;
};
class BCELoss {
public:
explicit BCELoss(double eps = 1.0e-7, std::string reduction = "mean") : eps_(eps), reduction_(std::move(reduction)) {}
Tensor operator()(const Tensor& pred, const Tensor& target) const { return BceLoss(pred, target, eps_, reduction_); }
Tensor Forward(const Tensor& pred, const Tensor& target) const { return operator()(pred, target); }
private:
double eps_;
std::string reduction_;
};
class CrossEntropyLoss {
public:
explicit CrossEntropyLoss(double eps = 1.0e-7, std::string reduction = "mean") : eps_(eps), reduction_(std::move(reduction)) {}
Tensor operator()(const Tensor& logits, const Tensor& targets) const { return cross_entropy_loss(logits, targets, eps_, reduction_); }
Tensor Forward(const Tensor& logits, const Tensor& targets) const { return operator()(logits, targets); }
private:
double eps_;
std::string reduction_;
};
} // namespace tenzr::nn
#endif // TENZR_MODULES_NN_LOSS_H_