/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * Geophysical Computational Tools & Library (GCTL) * * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) * * GCTL is distributed under a dual licensing scheme. You can redistribute * it and/or modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation, either version 2 * of the License, or (at your option) any later version. You should have * received a copy of the GNU Lesser General Public License along with this * program. If not, see . * * If the terms and conditions of the LGPL v.2. would prevent you from using * the GCTL, please consider the option to obtain a commercial license for a * fee. These licenses are offered by the GCTL's original author. As a rule, * licenses are provided "as-is", unlimited in time for a one time fee. Please * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget * to include some description of your company and the realm of its activities. * Also add information on how to contact you by electronic and paper mail. ******************************************************/ #include "loss_func.h" gctl::loss_func::loss_func() { uncer_type_ = 0; } gctl::loss_func::loss_func(const array &tar, norm_type_e n_type) { uncer_type_ = 0; init(tar, n_type); } gctl::loss_func::~loss_func(){} void gctl::loss_func::init(const array &tar, norm_type_e n_type) { tar_num_ = tar.size(); tars_ = tar; norm_type_ = n_type; return; } void gctl::loss_func::set_uncertainty(double uncer) { uncer_type_ = 1; uncer_ = uncer; return; } void gctl::loss_func::set_uncertainty(const array &uncer) { uncer_type_ = 2; uncers_ = uncer; return; } double gctl::loss_func::get_loss() { double l = loss_; loss_ = 0.0; return l; } double gctl::loss_func::evaluate(double inp, int id) { double val = (inp - tars_[id]); if (uncer_type_ == 1) val /= uncer_; else if (uncer_type_ == 2) val /= uncers_[id]; if (norm_type_ == L1) val = fabs(val); if (norm_type_ == L2) val = val*val; loss_ += val; return val/tar_num_; } double gctl::loss_func::gradient(double inp, int id) { double c; if (uncer_type_ == 1) c = uncer_; else if (uncer_type_ == 2) c = uncers_[id]; double val = (inp - tars_[id]); if (norm_type_ == L1 && val >= 0) val = 1.0; if (norm_type_ == L1 && val < 0) val = -1.0; if (norm_type_ == L2) val = 2.0*val; if (norm_type_ == L1 && uncer_type_ != 0) val /= c; else if (norm_type_ == L2 && uncer_type_ != 0) val /= (c*c); return val/tar_num_; }