Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include "eigen/Eigen/Core"
- using Eigen::MatrixXd;
- using Eigen::VectorXd;
- enum class Threshold_Id {
- Sigmoid,
- ReLu
- };
- struct Threshold_Database {
- template<Threshold_Id>
- static double evaluate_0(double);
- template<Threshold_Id>
- static double evaluate_1(double);
- template<>
- inline double evaluate_0<Threshold_Id::Sigmoid>(double x) {
- return 1. / (1. + std::exp(-x));
- }
- template<>
- inline double evaluate_1<Threshold_Id::Sigmoid>(double x) {
- return std::exp(-x) * evaluate_0<Threshold_Id::Sigmoid>(x) * evaluate_0<Threshold_Id::Sigmoid>(x);
- }
- template<>
- inline double evaluate_0<Threshold_Id::ReLu>(double x) {
- return x > 0 ? x : 0;
- }
- template<>
- inline double evaluate_1<Threshold_Id::ReLu>(double x) {
- return x > 0 ? 1 : 0;
- }
- };
- template<Threshold_Id>
- struct F_ID {
- };
- class Threshold_Func {
- using FunctionType = std::function<double(double)>;
- public:
- Threshold_Func(FunctionType evaluate_0, FunctionType evaluate_1) : evaluate_0_(std::move(evaluate_0)),
- evaluate_1_(std::move(evaluate_1)) {
- }
- template<Threshold_Id Id>
- Threshold_Func(F_ID<Id>): evaluate_0_(Threshold_Database::evaluate_0<Id>),
- evaluate_1_(Threshold_Database::evaluate_1<Id>) {
- }
- double evaluate_0(double x) const {
- return evaluate_0_(x);
- }
- double evaluate_1(double x) const {
- return evaluate_1_(x);
- }
- VectorXd apply(const VectorXd &vec) const {
- return vec.unaryExpr([this](double x) { return evaluate_0(x); });
- }
- VectorXd derive(const VectorXd &vec) const {
- return vec.unaryExpr([this](double x) { return evaluate_1(x); });
- }
- private:
- FunctionType evaluate_0_;
- FunctionType evaluate_1_;
- };
- class Layer {
- public:
- VectorXd apply(const VectorXd &x) const { // vector of values
- return sigma_.apply(A_ * x + b_);
- }
- MatrixXd derive(const VectorXd &vec) const { // vec is a matrix of y_i = (Ax + b)_i - result of apply
- return sigma_.derive(vec).asDiagonal();
- }
- MatrixXd gradA(const VectorXd &x, const VectorXd &u, const VectorXd &vec) const { // u is a gradient vector
- return derive(vec) * u.transpose() * x.transpose();
- }
- MatrixXd gradb(const VectorXd &u, const VectorXd &vec) const {
- return derive(vec) * u.transpose();
- }
- VectorXd gradx(const VectorXd &x, const VectorXd &u, const VectorXd &vec) const {
- return (A_.transpose() * derive(vec) * u.transpose()).transpose();
- }
- private:
- Threshold_Func sigma_;
- MatrixXd A_;
- VectorXd b_;
- };
- double Score(MatrixXd res, MatrixXd reference) {
- return (res - reference).cwiseAbs().sum();
- }
- int main() {
- Threshold_Func(F_ID<Threshold_Id::Sigmoid>());
- std::cout << "Hello, World!" << std::endl;
- using Vector3f = Eigen::Matrix<float, 3, 1>;
- Vector3f a;
- for (auto el: a) {
- std::cout << el << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement