pmlpp/mlpp/output_layer/output_layer.cpp

129 lines
4.5 KiB
C++
Raw Normal View History

//
// OutputLayer.cpp
//
// Created by Marc Melikyan on 11/4/20.
//
2023-01-24 18:12:23 +01:00
#include "output_layer.h"
#include "../lin_alg/lin_alg.h"
#include "../utilities/utilities.h"
#include <iostream>
#include <random>
2023-01-24 19:20:18 +01:00
2023-01-27 13:01:16 +01:00
MLPPOutputLayer::MLPPOutputLayer(int n_hidden, std::string activation, std::string cost, std::vector<std::vector<real_t>> input, std::string weightInit, std::string reg, real_t lambda, real_t alpha) :
2023-01-24 19:00:54 +01:00
n_hidden(n_hidden), activation(activation), cost(cost), input(input), weightInit(weightInit), reg(reg), lambda(lambda), alpha(alpha) {
weights = MLPPUtilities::weightInitialization(n_hidden, weightInit);
bias = MLPPUtilities::biasInitialization();
2023-01-24 19:23:30 +01:00
activation_map["Linear"] = &MLPPActivation::linear;
activationTest_map["Linear"] = &MLPPActivation::linear;
2023-01-24 19:23:30 +01:00
activation_map["Sigmoid"] = &MLPPActivation::sigmoid;
activationTest_map["Sigmoid"] = &MLPPActivation::sigmoid;
2023-01-24 19:23:30 +01:00
activation_map["Swish"] = &MLPPActivation::swish;
activationTest_map["Swish"] = &MLPPActivation::swish;
2023-01-24 19:23:30 +01:00
activation_map["Mish"] = &MLPPActivation::mish;
activationTest_map["Mish"] = &MLPPActivation::mish;
2023-01-24 19:23:30 +01:00
activation_map["SinC"] = &MLPPActivation::sinc;
activationTest_map["SinC"] = &MLPPActivation::sinc;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Softplus"] = &MLPPActivation::softplus;
activationTest_map["Softplus"] = &MLPPActivation::softplus;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Softsign"] = &MLPPActivation::softsign;
activationTest_map["Softsign"] = &MLPPActivation::softsign;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["CLogLog"] = &MLPPActivation::cloglog;
activationTest_map["CLogLog"] = &MLPPActivation::cloglog;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Logit"] = &MLPPActivation::logit;
activationTest_map["Logit"] = &MLPPActivation::logit;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["GaussianCDF"] = &MLPPActivation::gaussianCDF;
activationTest_map["GaussianCDF"] = &MLPPActivation::gaussianCDF;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["RELU"] = &MLPPActivation::RELU;
activationTest_map["RELU"] = &MLPPActivation::RELU;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["GELU"] = &MLPPActivation::GELU;
activationTest_map["GELU"] = &MLPPActivation::GELU;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Sign"] = &MLPPActivation::sign;
activationTest_map["Sign"] = &MLPPActivation::sign;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["UnitStep"] = &MLPPActivation::unitStep;
activationTest_map["UnitStep"] = &MLPPActivation::unitStep;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Sinh"] = &MLPPActivation::sinh;
activationTest_map["Sinh"] = &MLPPActivation::sinh;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Cosh"] = &MLPPActivation::cosh;
activationTest_map["Cosh"] = &MLPPActivation::cosh;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Tanh"] = &MLPPActivation::tanh;
activationTest_map["Tanh"] = &MLPPActivation::tanh;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Csch"] = &MLPPActivation::csch;
activationTest_map["Csch"] = &MLPPActivation::csch;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Sech"] = &MLPPActivation::sech;
activationTest_map["Sech"] = &MLPPActivation::sech;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Coth"] = &MLPPActivation::coth;
activationTest_map["Coth"] = &MLPPActivation::coth;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Arsinh"] = &MLPPActivation::arsinh;
activationTest_map["Arsinh"] = &MLPPActivation::arsinh;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Arcosh"] = &MLPPActivation::arcosh;
activationTest_map["Arcosh"] = &MLPPActivation::arcosh;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Artanh"] = &MLPPActivation::artanh;
activationTest_map["Artanh"] = &MLPPActivation::artanh;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Arcsch"] = &MLPPActivation::arcsch;
activationTest_map["Arcsch"] = &MLPPActivation::arcsch;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Arsech"] = &MLPPActivation::arsech;
activationTest_map["Arsech"] = &MLPPActivation::arsech;
2023-01-24 19:00:54 +01:00
2023-01-24 19:23:30 +01:00
activation_map["Arcoth"] = &MLPPActivation::arcoth;
activationTest_map["Arcoth"] = &MLPPActivation::arcoth;
2023-01-24 19:00:54 +01:00
2023-01-24 19:37:08 +01:00
costDeriv_map["MSE"] = &MLPPCost::MSEDeriv;
cost_map["MSE"] = &MLPPCost::MSE;
costDeriv_map["RMSE"] = &MLPPCost::RMSEDeriv;
cost_map["RMSE"] = &MLPPCost::RMSE;
costDeriv_map["MAE"] = &MLPPCost::MAEDeriv;
cost_map["MAE"] = &MLPPCost::MAE;
costDeriv_map["MBE"] = &MLPPCost::MBEDeriv;
cost_map["MBE"] = &MLPPCost::MBE;
costDeriv_map["LogLoss"] = &MLPPCost::LogLossDeriv;
cost_map["LogLoss"] = &MLPPCost::LogLoss;
costDeriv_map["CrossEntropy"] = &MLPPCost::CrossEntropyDeriv;
cost_map["CrossEntropy"] = &MLPPCost::CrossEntropy;
costDeriv_map["HingeLoss"] = &MLPPCost::HingeLossDeriv;
cost_map["HingeLoss"] = &MLPPCost::HingeLoss;
costDeriv_map["WassersteinLoss"] = &MLPPCost::HingeLossDeriv;
cost_map["WassersteinLoss"] = &MLPPCost::HingeLoss;
2023-01-24 19:00:54 +01:00
}
2023-01-25 00:54:50 +01:00
void MLPPOutputLayer::forwardPass() {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
z = alg.scalarAdd(bias, alg.mat_vec_mult(input, weights));
a = (avn.*activation_map[activation])(z, 0);
}
2023-01-27 13:01:16 +01:00
void MLPPOutputLayer::Test(std::vector<real_t> x) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-24 19:23:30 +01:00
MLPPActivation avn;
2023-01-24 19:00:54 +01:00
z_test = alg.dot(weights, x) + bias;
a_test = (avn.*activationTest_map[activation])(z_test, 0);
}