COSC-4P80-Assignment-2/include/assign2/functions.h

72 lines
2.0 KiB
C
Raw Normal View History

2024-10-21 19:25:00 -04:00
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef COSC_4P80_ASSIGNMENT_2_FUNCTIONS_H
#define COSC_4P80_ASSIGNMENT_2_FUNCTIONS_H
#include <assign2/common.h>
#include <cmath>
namespace assign2
{
2024-10-25 01:22:32 -04:00
struct sigmoid_function : public function_t
2024-10-21 19:25:00 -04:00
{
2024-10-25 01:22:32 -04:00
[[nodiscard]] Scalar call(const Scalar s) const final
2024-10-21 19:25:00 -04:00
{
return 1 / (1 + std::exp(-s));
}
2024-10-25 01:22:32 -04:00
[[nodiscard]] Scalar derivative(const Scalar s) const final
2024-10-21 19:25:00 -04:00
{
2024-10-25 01:22:32 -04:00
auto v = call(s);
return v * (1 - v);
2024-10-21 19:25:00 -04:00
}
};
2024-10-27 18:09:37 -04:00
struct tanh_function : public function_t
2024-10-21 19:25:00 -04:00
{
2024-10-27 18:09:37 -04:00
[[nodiscard]] Scalar call(Scalar s) const final
2024-10-25 01:22:32 -04:00
{
2024-10-27 18:09:37 -04:00
auto x = std::exp(s);
auto nx = std::exp(-s);
return (x - nx) / (x + nx);
2024-10-25 01:22:32 -04:00
}
[[nodiscard]] Scalar derivative(Scalar s) const final
{
2024-10-27 18:09:37 -04:00
auto tanh = std::tanh(s);
return 1 - (tanh * tanh);
2024-10-25 01:22:32 -04:00
}
};
2024-10-21 19:25:00 -04:00
2024-10-25 01:22:32 -04:00
struct relu_function : public function_t
{
[[nodiscard]] Scalar call(const Scalar s) const final
{
return std::max(static_cast<Scalar>(0), s);
}
[[nodiscard]] Scalar derivative(Scalar s) const final
{
2024-10-27 18:09:37 -04:00
return s >= 0 ? 1 : 0;
2024-10-25 01:22:32 -04:00
}
2024-10-21 19:25:00 -04:00
};
}
#endif //COSC_4P80_ASSIGNMENT_2_FUNCTIONS_H