2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// OutputLayer.cpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 11/4/20.
|
|
|
|
//
|
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "output_layer.h"
|
|
|
|
#include "../utilities/utilities.h"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-02-04 12:44:00 +01:00
|
|
|
int MLPPOutputLayer::get_n_hidden() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _n_hidden;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_n_hidden(const int val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_n_hidden = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MLPPActivation::ActivationFunction MLPPOutputLayer::get_activation() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _activation;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_activation(const MLPPActivation::ActivationFunction val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_activation = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
2023-02-04 13:30:33 +01:00
|
|
|
MLPPCost::CostTypes MLPPOutputLayer::get_cost() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _cost;
|
2023-02-04 13:30:33 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_cost(const MLPPCost::CostTypes val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_cost = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 13:30:33 +01:00
|
|
|
}
|
|
|
|
|
2023-02-04 12:44:00 +01:00
|
|
|
Ref<MLPPMatrix> MLPPOutputLayer::get_input() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _input;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_input(const Ref<MLPPMatrix> &val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_input = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<MLPPVector> MLPPOutputLayer::get_weights() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _weights;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_weights(const Ref<MLPPVector> &val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_weights = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t MLPPOutputLayer::MLPPOutputLayer::get_bias() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _bias;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_bias(const real_t val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_bias = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<MLPPVector> MLPPOutputLayer::get_z() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _z;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_z(const Ref<MLPPVector> &val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_z = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<MLPPVector> MLPPOutputLayer::get_a() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _a;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_a(const Ref<MLPPVector> &val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_a = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 16:55:00 +01:00
|
|
|
real_t MLPPOutputLayer::get_z_test() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _z_test;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
2023-02-17 16:55:00 +01:00
|
|
|
void MLPPOutputLayer::set_z_test(const real_t val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_z_test = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 16:55:00 +01:00
|
|
|
real_t MLPPOutputLayer::get_a_test() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _a_test;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
2023-02-17 16:55:00 +01:00
|
|
|
void MLPPOutputLayer::set_a_test(const real_t val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_a_test = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<MLPPVector> MLPPOutputLayer::get_delta() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _delta;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_delta(const Ref<MLPPVector> &val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_delta = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MLPPReg::RegularizationType MLPPOutputLayer::get_reg() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _reg;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_reg(const MLPPReg::RegularizationType val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_reg = val;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t MLPPOutputLayer::get_lambda() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _lambda;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_lambda(const real_t val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_lambda = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
real_t MLPPOutputLayer::get_alpha() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _alpha;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_alpha(const real_t val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_alpha = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MLPPUtilities::WeightDistributionType MLPPOutputLayer::get_weight_init() {
|
2023-02-13 00:19:16 +01:00
|
|
|
return _weight_init;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
void MLPPOutputLayer::set_weight_init(const MLPPUtilities::WeightDistributionType val) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_weight_init = val;
|
2023-02-06 14:24:43 +01:00
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MLPPOutputLayer::is_initialized() {
|
|
|
|
return _initialized;
|
|
|
|
}
|
|
|
|
void MLPPOutputLayer::initialize() {
|
|
|
|
if (_initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_weights->resize(_n_hidden);
|
2023-02-06 14:24:43 +01:00
|
|
|
|
|
|
|
MLPPUtilities utils;
|
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
utils.weight_initializationv(_weights, _weight_init);
|
|
|
|
_bias = utils.bias_initializationr();
|
2023-02-06 14:24:43 +01:00
|
|
|
|
|
|
|
_initialized = true;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MLPPOutputLayer::forward_pass() {
|
2023-02-06 14:24:43 +01:00
|
|
|
if (!_initialized) {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
2023-02-04 12:44:00 +01:00
|
|
|
MLPPActivation avn;
|
|
|
|
|
2023-04-29 20:10:49 +02:00
|
|
|
_z = _input->mult_vec(_weights)->scalar_addn(_bias);
|
2023-02-13 00:19:16 +01:00
|
|
|
_a = avn.run_activation_norm_vector(_activation, _z);
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MLPPOutputLayer::test(const Ref<MLPPVector> &x) {
|
2023-02-06 14:24:43 +01:00
|
|
|
if (!_initialized) {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
2023-02-04 12:44:00 +01:00
|
|
|
MLPPActivation avn;
|
|
|
|
|
2023-04-29 20:10:49 +02:00
|
|
|
_z_test = _weights->dot(x) + _bias;
|
2023-02-17 16:55:00 +01:00
|
|
|
_a_test = avn.run_activation_norm_real(_activation, _z_test);
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 16:55:00 +01:00
|
|
|
MLPPOutputLayer::MLPPOutputLayer(int p_n_hidden, MLPPActivation::ActivationFunction p_activation, MLPPCost::CostTypes p_cost, Ref<MLPPMatrix> p_input, MLPPUtilities::WeightDistributionType p_weight_init, MLPPReg::RegularizationType p_reg, real_t p_lambda, real_t p_alpha) {
|
2023-02-13 00:19:16 +01:00
|
|
|
_n_hidden = p_n_hidden;
|
|
|
|
_activation = p_activation;
|
2023-02-17 16:55:00 +01:00
|
|
|
_cost = p_cost;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_input = p_input;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
|
|
|
// Regularization Params
|
2023-02-13 00:19:16 +01:00
|
|
|
_reg = p_reg;
|
|
|
|
_lambda = p_lambda; /* Regularization Parameter */
|
|
|
|
_alpha = p_alpha; /* This is the controlling param for Elastic Net*/
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_weight_init = p_weight_init;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_z.instance();
|
|
|
|
_a.instance();
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-17 16:55:00 +01:00
|
|
|
_z_test = 0;
|
|
|
|
_a_test = 0;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_delta.instance();
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_weights.instance();
|
|
|
|
_bias = 0;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_weights->resize(_n_hidden);
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-04 13:30:33 +01:00
|
|
|
MLPPUtilities utils;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
utils.weight_initializationv(_weights, _weight_init);
|
|
|
|
_bias = utils.bias_initializationr();
|
2023-02-06 14:24:43 +01:00
|
|
|
|
|
|
|
_initialized = true;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MLPPOutputLayer::MLPPOutputLayer() {
|
2023-02-13 00:19:16 +01:00
|
|
|
_n_hidden = 0;
|
|
|
|
_activation = MLPPActivation::ACTIVATION_FUNCTION_LINEAR;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
|
|
|
// Regularization Params
|
|
|
|
//reg = 0;
|
2023-02-13 00:19:16 +01:00
|
|
|
_lambda = 0; /* Regularization Parameter */
|
|
|
|
_alpha = 0; /* This is the controlling param for Elastic Net*/
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_weight_init = MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_z.instance();
|
|
|
|
_a.instance();
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-17 16:55:00 +01:00
|
|
|
_z_test = 0;
|
|
|
|
_a_test = 0;
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_delta.instance();
|
2023-02-04 12:44:00 +01:00
|
|
|
|
2023-02-13 00:19:16 +01:00
|
|
|
_weights.instance();
|
|
|
|
_bias = 0;
|
2023-02-06 14:24:43 +01:00
|
|
|
|
|
|
|
_initialized = false;
|
2023-02-04 12:44:00 +01:00
|
|
|
}
|
|
|
|
MLPPOutputLayer::~MLPPOutputLayer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MLPPOutputLayer::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_n_hidden"), &MLPPOutputLayer::get_n_hidden);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_n_hidden", "val"), &MLPPOutputLayer::set_n_hidden);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "n_hidden"), "set_n_hidden", "get_n_hidden");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_activation"), &MLPPOutputLayer::get_activation);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_activation", "val"), &MLPPOutputLayer::set_activation);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "activation"), "set_activation", "get_activation");
|
|
|
|
|
2023-02-04 13:30:33 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_cost"), &MLPPOutputLayer::get_cost);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_cost", "val"), &MLPPOutputLayer::set_cost);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "cost"), "set_cost", "get_cost");
|
|
|
|
|
2023-02-04 12:44:00 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_input"), &MLPPOutputLayer::get_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_input", "val"), &MLPPOutputLayer::set_input);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input", "get_input");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_weights"), &MLPPOutputLayer::get_weights);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_weights", "val"), &MLPPOutputLayer::set_weights);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "weights", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_weights", "get_weights");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bias"), &MLPPOutputLayer::get_bias);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bias", "val"), &MLPPOutputLayer::set_bias);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "bias"), "set_bias", "get_bias");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_z"), &MLPPOutputLayer::get_z);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_z", "val"), &MLPPOutputLayer::set_z);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "z", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_z", "get_z");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_a"), &MLPPOutputLayer::get_a);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_a", "val"), &MLPPOutputLayer::set_a);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "a", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_a", "get_a");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_z_test"), &MLPPOutputLayer::get_z_test);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_z_test", "val"), &MLPPOutputLayer::set_z_test);
|
2023-02-17 16:55:00 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "z_test"), "set_z_test", "get_z_test");
|
2023-02-04 12:44:00 +01:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_a_test"), &MLPPOutputLayer::get_a_test);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_a_test", "val"), &MLPPOutputLayer::set_a_test);
|
2023-02-17 16:55:00 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "a_test"), "set_a_test", "get_a_test");
|
2023-02-04 12:44:00 +01:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_delta"), &MLPPOutputLayer::get_delta);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_delta", "val"), &MLPPOutputLayer::set_delta);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "delta", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_delta", "get_delta");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_reg"), &MLPPOutputLayer::get_reg);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_reg", "val"), &MLPPOutputLayer::set_reg);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "reg"), "set_reg", "get_reg");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_lambda"), &MLPPOutputLayer::get_lambda);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_lambda", "val"), &MLPPOutputLayer::set_lambda);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "lambda"), "set_lambda", "get_lambda");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_alpha"), &MLPPOutputLayer::get_alpha);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_alpha", "val"), &MLPPOutputLayer::set_alpha);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "alpha"), "set_alpha", "get_alpha");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_weight_init"), &MLPPOutputLayer::get_weight_init);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_weight_init", "val"), &MLPPOutputLayer::set_weight_init);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "set_weight_init"), "set_weight_init", "get_weight_init");
|
|
|
|
|
2023-02-06 14:24:43 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_initialized"), &MLPPOutputLayer::is_initialized);
|
|
|
|
ClassDB::bind_method(D_METHOD("initialize"), &MLPPOutputLayer::initialize);
|
|
|
|
|
2023-02-04 12:44:00 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("forward_pass"), &MLPPOutputLayer::forward_pass);
|
|
|
|
ClassDB::bind_method(D_METHOD("test", "x"), &MLPPOutputLayer::test);
|
|
|
|
}
|