mirror of
https://github.com/Relintai/pmlpp.git
synced 2025-04-24 04:33:21 +02:00
Initial cleanup pass on MLPPMANN.
This commit is contained in:
parent
7bc5a5bc1d
commit
f5bd46c211
@ -13,106 +13,128 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
MLPPMANN::MLPPMANN(std::vector<std::vector<real_t>> inputSet, std::vector<std::vector<real_t>> outputSet) :
|
/*
|
||||||
inputSet(inputSet), outputSet(outputSet), n(inputSet.size()), k(inputSet[0].size()), n_output(outputSet[0].size()) {
|
Ref<MLPPMatrix> MLPPMANN::get_input_set() {
|
||||||
|
return input_set;
|
||||||
|
}
|
||||||
|
void MLPPMANN::set_input_set(const Ref<MLPPMatrix> &val) {
|
||||||
|
input_set = val;
|
||||||
|
|
||||||
|
_initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
MLPPMANN::~MLPPMANN() {
|
Ref<MLPPMatrix> MLPPMANN::get_output_set() {
|
||||||
delete outputLayer;
|
return output_set;
|
||||||
}
|
}
|
||||||
|
void MLPPMANN::set_output_set(const Ref<MLPPMatrix> &val) {
|
||||||
|
output_set = val;
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> MLPPMANN::modelSetTest(std::vector<std::vector<real_t>> X) {
|
_initialized = false;
|
||||||
if (!network.empty()) {
|
}
|
||||||
network[0].input = X;
|
*/
|
||||||
network[0].forwardPass();
|
|
||||||
|
|
||||||
for (uint32_t i = 1; i < network.size(); i++) {
|
std::vector<std::vector<real_t>> MLPPMANN::model_set_test(std::vector<std::vector<real_t>> X) {
|
||||||
network[i].input = network[i - 1].a;
|
ERR_FAIL_COND_V(!_initialized, std::vector<std::vector<real_t>>());
|
||||||
network[i].forwardPass();
|
|
||||||
|
if (!_network.empty()) {
|
||||||
|
_network[0].input = X;
|
||||||
|
_network[0].forwardPass();
|
||||||
|
|
||||||
|
for (uint32_t i = 1; i < _network.size(); i++) {
|
||||||
|
_network[i].input = _network[i - 1].a;
|
||||||
|
_network[i].forwardPass();
|
||||||
}
|
}
|
||||||
outputLayer->input = network[network.size() - 1].a;
|
_output_layer->input = _network[_network.size() - 1].a;
|
||||||
} else {
|
} else {
|
||||||
outputLayer->input = X;
|
_output_layer->input = X;
|
||||||
}
|
}
|
||||||
outputLayer->forwardPass();
|
|
||||||
return outputLayer->a;
|
_output_layer->forwardPass();
|
||||||
|
|
||||||
|
return _output_layer->a;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<real_t> MLPPMANN::modelTest(std::vector<real_t> x) {
|
std::vector<real_t> MLPPMANN::model_test(std::vector<real_t> x) {
|
||||||
if (!network.empty()) {
|
ERR_FAIL_COND_V(!_initialized, std::vector<real_t>());
|
||||||
network[0].Test(x);
|
|
||||||
for (uint32_t i = 1; i < network.size(); i++) {
|
if (!_network.empty()) {
|
||||||
network[i].Test(network[i - 1].a_test);
|
_network[0].Test(x);
|
||||||
|
for (uint32_t i = 1; i < _network.size(); i++) {
|
||||||
|
_network[i].Test(_network[i - 1].a_test);
|
||||||
}
|
}
|
||||||
outputLayer->Test(network[network.size() - 1].a_test);
|
_output_layer->Test(_network[_network.size() - 1].a_test);
|
||||||
} else {
|
} else {
|
||||||
outputLayer->Test(x);
|
_output_layer->Test(x);
|
||||||
}
|
}
|
||||||
return outputLayer->a_test;
|
return _output_layer->a_test;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPMANN::gradientDescent(real_t learning_rate, int max_epoch, bool UI) {
|
void MLPPMANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
||||||
class MLPPCost cost;
|
ERR_FAIL_COND(!_initialized);
|
||||||
|
|
||||||
|
MLPPCost mlpp_cost;
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
MLPPLinAlg alg;
|
MLPPLinAlg alg;
|
||||||
MLPPReg regularization;
|
MLPPReg regularization;
|
||||||
|
|
||||||
real_t cost_prev = 0;
|
real_t cost_prev = 0;
|
||||||
int epoch = 1;
|
int epoch = 1;
|
||||||
forwardPass();
|
|
||||||
|
forward_pass();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
cost_prev = Cost(y_hat, outputSet);
|
cost_prev = cost(_y_hat, _output_set);
|
||||||
|
|
||||||
if (outputLayer->activation == "Softmax") {
|
if (_output_layer->activation == "Softmax") {
|
||||||
outputLayer->delta = alg.subtraction(y_hat, outputSet);
|
_output_layer->delta = alg.subtraction(_y_hat, _output_set);
|
||||||
} else {
|
} else {
|
||||||
auto costDeriv = outputLayer->costDeriv_map[outputLayer->cost];
|
auto costDeriv = _output_layer->costDeriv_map[_output_layer->cost];
|
||||||
auto outputAvn = outputLayer->activation_map[outputLayer->activation];
|
auto outputAvn = _output_layer->activation_map[_output_layer->activation];
|
||||||
outputLayer->delta = alg.hadamard_product((cost.*costDeriv)(y_hat, outputSet), (avn.*outputAvn)(outputLayer->z, 1));
|
_output_layer->delta = alg.hadamard_product((mlpp_cost.*costDeriv)(_y_hat, _output_set), (avn.*outputAvn)(_output_layer->z, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> outputWGrad = alg.matmult(alg.transpose(outputLayer->input), outputLayer->delta);
|
std::vector<std::vector<real_t>> outputWGrad = alg.matmult(alg.transpose(_output_layer->input), _output_layer->delta);
|
||||||
|
|
||||||
outputLayer->weights = alg.subtraction(outputLayer->weights, alg.scalarMultiply(learning_rate / n, outputWGrad));
|
_output_layer->weights = alg.subtraction(_output_layer->weights, alg.scalarMultiply(learning_rate / _n, outputWGrad));
|
||||||
outputLayer->weights = regularization.regWeights(outputLayer->weights, outputLayer->lambda, outputLayer->alpha, outputLayer->reg);
|
_output_layer->weights = regularization.regWeights(_output_layer->weights, _output_layer->lambda, _output_layer->alpha, _output_layer->reg);
|
||||||
outputLayer->bias = alg.subtractMatrixRows(outputLayer->bias, alg.scalarMultiply(learning_rate / n, outputLayer->delta));
|
_output_layer->bias = alg.subtractMatrixRows(_output_layer->bias, alg.scalarMultiply(learning_rate / _n, _output_layer->delta));
|
||||||
|
|
||||||
if (!network.empty()) {
|
if (!_network.empty()) {
|
||||||
auto hiddenLayerAvn = network[network.size() - 1].activation_map[network[network.size() - 1].activation];
|
auto hiddenLayerAvn = _network[_network.size() - 1].activation_map[_network[_network.size() - 1].activation];
|
||||||
network[network.size() - 1].delta = alg.hadamard_product(alg.matmult(outputLayer->delta, alg.transpose(outputLayer->weights)), (avn.*hiddenLayerAvn)(network[network.size() - 1].z, 1));
|
_network[_network.size() - 1].delta = alg.hadamard_product(alg.matmult(_output_layer->delta, alg.transpose(_output_layer->weights)), (avn.*hiddenLayerAvn)(_network[_network.size() - 1].z, true));
|
||||||
std::vector<std::vector<real_t>> hiddenLayerWGrad = alg.matmult(alg.transpose(network[network.size() - 1].input), network[network.size() - 1].delta);
|
std::vector<std::vector<real_t>> hiddenLayerWGrad = alg.matmult(alg.transpose(_network[_network.size() - 1].input), _network[_network.size() - 1].delta);
|
||||||
|
|
||||||
network[network.size() - 1].weights = alg.subtraction(network[network.size() - 1].weights, alg.scalarMultiply(learning_rate / n, hiddenLayerWGrad));
|
_network[_network.size() - 1].weights = alg.subtraction(_network[_network.size() - 1].weights, alg.scalarMultiply(learning_rate / _n, hiddenLayerWGrad));
|
||||||
network[network.size() - 1].weights = regularization.regWeights(network[network.size() - 1].weights, network[network.size() - 1].lambda, network[network.size() - 1].alpha, network[network.size() - 1].reg);
|
_network[_network.size() - 1].weights = regularization.regWeights(_network[_network.size() - 1].weights, _network[_network.size() - 1].lambda, _network[_network.size() - 1].alpha, _network[_network.size() - 1].reg);
|
||||||
network[network.size() - 1].bias = alg.subtractMatrixRows(network[network.size() - 1].bias, alg.scalarMultiply(learning_rate / n, network[network.size() - 1].delta));
|
_network[_network.size() - 1].bias = alg.subtractMatrixRows(_network[_network.size() - 1].bias, alg.scalarMultiply(learning_rate / _n, _network[_network.size() - 1].delta));
|
||||||
|
|
||||||
for (int i = network.size() - 2; i >= 0; i--) {
|
for (int i = _network.size() - 2; i >= 0; i--) {
|
||||||
hiddenLayerAvn = network[i].activation_map[network[i].activation];
|
hiddenLayerAvn = _network[i].activation_map[_network[i].activation];
|
||||||
network[i].delta = alg.hadamard_product(alg.matmult(network[i + 1].delta, network[i + 1].weights), (avn.*hiddenLayerAvn)(network[i].z, 1));
|
_network[i].delta = alg.hadamard_product(alg.matmult(_network[i + 1].delta, _network[i + 1].weights), (avn.*hiddenLayerAvn)(_network[i].z, true));
|
||||||
hiddenLayerWGrad = alg.matmult(alg.transpose(network[i].input), network[i].delta);
|
hiddenLayerWGrad = alg.matmult(alg.transpose(_network[i].input), _network[i].delta);
|
||||||
network[i].weights = alg.subtraction(network[i].weights, alg.scalarMultiply(learning_rate / n, hiddenLayerWGrad));
|
_network[i].weights = alg.subtraction(_network[i].weights, alg.scalarMultiply(learning_rate / _n, hiddenLayerWGrad));
|
||||||
network[i].weights = regularization.regWeights(network[i].weights, network[i].lambda, network[i].alpha, network[i].reg);
|
_network[i].weights = regularization.regWeights(_network[i].weights, _network[i].lambda, _network[i].alpha, _network[i].reg);
|
||||||
network[i].bias = alg.subtractMatrixRows(network[i].bias, alg.scalarMultiply(learning_rate / n, network[i].delta));
|
_network[i].bias = alg.subtractMatrixRows(_network[i].bias, alg.scalarMultiply(learning_rate / _n, _network[i].delta));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
forwardPass();
|
forward_pass();
|
||||||
|
|
||||||
if (UI) {
|
if (ui) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet));
|
MLPPUtilities::CostInfo(epoch, cost_prev, cost(_y_hat, _output_set));
|
||||||
std::cout << "Layer " << network.size() + 1 << ": " << std::endl;
|
std::cout << "Layer " << _network.size() + 1 << ": " << std::endl;
|
||||||
MLPPUtilities::UI(outputLayer->weights, outputLayer->bias);
|
MLPPUtilities::UI(_output_layer->weights, _output_layer->bias);
|
||||||
if (!network.empty()) {
|
if (!_network.empty()) {
|
||||||
std::cout << "Layer " << network.size() << ": " << std::endl;
|
std::cout << "Layer " << _network.size() << ": " << std::endl;
|
||||||
for (int i = network.size() - 1; i >= 0; i--) {
|
for (int i = _network.size() - 1; i >= 0; i--) {
|
||||||
std::cout << "Layer " << i + 1 << ": " << std::endl;
|
std::cout << "Layer " << i + 1 << ": " << std::endl;
|
||||||
MLPPUtilities::UI(network[i].weights, network[i].bias);
|
MLPPUtilities::UI(_network[i].weights, _network[i].bias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -120,69 +142,120 @@ void MLPPMANN::gradientDescent(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPMANN::score() {
|
real_t MLPPMANN::score() {
|
||||||
|
ERR_FAIL_COND_V(!_initialized, 0);
|
||||||
|
|
||||||
MLPPUtilities util;
|
MLPPUtilities util;
|
||||||
forwardPass();
|
|
||||||
return util.performance(y_hat, outputSet);
|
forward_pass();
|
||||||
|
|
||||||
|
return util.performance(_y_hat, _output_set);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPMANN::save(std::string fileName) {
|
void MLPPMANN::save(std::string fileName) {
|
||||||
|
ERR_FAIL_COND(!_initialized);
|
||||||
|
|
||||||
MLPPUtilities util;
|
MLPPUtilities util;
|
||||||
if (!network.empty()) {
|
if (!_network.empty()) {
|
||||||
util.saveParameters(fileName, network[0].weights, network[0].bias, 0, 1);
|
util.saveParameters(fileName, _network[0].weights, _network[0].bias, false, 1);
|
||||||
for (uint32_t i = 1; i < network.size(); i++) {
|
for (uint32_t i = 1; i < _network.size(); i++) {
|
||||||
util.saveParameters(fileName, network[i].weights, network[i].bias, 1, i + 1);
|
util.saveParameters(fileName, _network[i].weights, _network[i].bias, true, i + 1);
|
||||||
}
|
}
|
||||||
util.saveParameters(fileName, outputLayer->weights, outputLayer->bias, 1, network.size() + 1);
|
util.saveParameters(fileName, _output_layer->weights, _output_layer->bias, true, _network.size() + 1);
|
||||||
} else {
|
} else {
|
||||||
util.saveParameters(fileName, outputLayer->weights, outputLayer->bias, 0, network.size() + 1);
|
util.saveParameters(fileName, _output_layer->weights, _output_layer->bias, false, _network.size() + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPMANN::addLayer(int n_hidden, std::string activation, std::string weightInit, std::string reg, real_t lambda, real_t alpha) {
|
void MLPPMANN::add_layer(int n_hidden, std::string activation, std::string weightInit, std::string reg, real_t lambda, real_t alpha) {
|
||||||
if (network.empty()) {
|
if (_network.empty()) {
|
||||||
network.push_back(MLPPOldHiddenLayer(n_hidden, activation, inputSet, weightInit, reg, lambda, alpha));
|
_network.push_back(MLPPOldHiddenLayer(n_hidden, activation, _input_set, weightInit, reg, lambda, alpha));
|
||||||
network[0].forwardPass();
|
_network[0].forwardPass();
|
||||||
} else {
|
} else {
|
||||||
network.push_back(MLPPOldHiddenLayer(n_hidden, activation, network[network.size() - 1].a, weightInit, reg, lambda, alpha));
|
_network.push_back(MLPPOldHiddenLayer(n_hidden, activation, _network[_network.size() - 1].a, weightInit, reg, lambda, alpha));
|
||||||
network[network.size() - 1].forwardPass();
|
_network[_network.size() - 1].forwardPass();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPMANN::addOutputLayer(std::string activation, std::string loss, std::string weightInit, std::string reg, real_t lambda, real_t alpha) {
|
void MLPPMANN::add_output_layer(std::string activation, std::string loss, std::string weightInit, std::string reg, real_t lambda, real_t alpha) {
|
||||||
if (!network.empty()) {
|
if (!_network.empty()) {
|
||||||
outputLayer = new MLPPOldMultiOutputLayer(n_output, network[0].n_hidden, activation, loss, network[network.size() - 1].a, weightInit, reg, lambda, alpha);
|
_output_layer = new MLPPOldMultiOutputLayer(_n_output, _network[0].n_hidden, activation, loss, _network[_network.size() - 1].a, weightInit, reg, lambda, alpha);
|
||||||
} else {
|
} else {
|
||||||
outputLayer = new MLPPOldMultiOutputLayer(n_output, k, activation, loss, inputSet, weightInit, reg, lambda, alpha);
|
_output_layer = new MLPPOldMultiOutputLayer(_n_output, _k, activation, loss, _input_set, weightInit, reg, lambda, alpha);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
real_t MLPPMANN::Cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y) {
|
bool MLPPMANN::is_initialized() {
|
||||||
|
return _initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MLPPMANN::initialize() {
|
||||||
|
if (_initialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//ERR_FAIL_COND(!input_set.is_valid() || !output_set.is_valid() || n_hidden == 0);
|
||||||
|
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MLPPMANN::MLPPMANN(std::vector<std::vector<real_t>> p_input_set, std::vector<std::vector<real_t>> p_output_set) {
|
||||||
|
_input_set = p_input_set;
|
||||||
|
_output_set = p_output_set;
|
||||||
|
_n = _input_set.size();
|
||||||
|
_k = _input_set[0].size();
|
||||||
|
_n_output = _output_set[0].size();
|
||||||
|
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MLPPMANN::MLPPMANN() {
|
||||||
|
_initialized = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MLPPMANN::~MLPPMANN() {
|
||||||
|
delete _output_layer;
|
||||||
|
}
|
||||||
|
|
||||||
|
real_t MLPPMANN::cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y) {
|
||||||
MLPPReg regularization;
|
MLPPReg regularization;
|
||||||
class MLPPCost cost;
|
class MLPPCost cost;
|
||||||
real_t totalRegTerm = 0;
|
real_t totalRegTerm = 0;
|
||||||
|
|
||||||
auto cost_function = outputLayer->cost_map[outputLayer->cost];
|
auto cost_function = _output_layer->cost_map[_output_layer->cost];
|
||||||
if (!network.empty()) {
|
if (!_network.empty()) {
|
||||||
for (uint32_t i = 0; i < network.size() - 1; i++) {
|
for (uint32_t i = 0; i < _network.size() - 1; i++) {
|
||||||
totalRegTerm += regularization.regTerm(network[i].weights, network[i].lambda, network[i].alpha, network[i].reg);
|
totalRegTerm += regularization.regTerm(_network[i].weights, _network[i].lambda, _network[i].alpha, _network[i].reg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (cost.*cost_function)(y_hat, y) + totalRegTerm + regularization.regTerm(outputLayer->weights, outputLayer->lambda, outputLayer->alpha, outputLayer->reg);
|
return (cost.*cost_function)(y_hat, y) + totalRegTerm + regularization.regTerm(_output_layer->weights, _output_layer->lambda, _output_layer->alpha, _output_layer->reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MLPPMANN::forwardPass() {
|
void MLPPMANN::forward_pass() {
|
||||||
if (!network.empty()) {
|
if (!_network.empty()) {
|
||||||
network[0].input = inputSet;
|
_network[0].input = _input_set;
|
||||||
network[0].forwardPass();
|
_network[0].forwardPass();
|
||||||
|
|
||||||
for (uint32_t i = 1; i < network.size(); i++) {
|
for (uint32_t i = 1; i < _network.size(); i++) {
|
||||||
network[i].input = network[i - 1].a;
|
_network[i].input = _network[i - 1].a;
|
||||||
network[i].forwardPass();
|
_network[i].forwardPass();
|
||||||
}
|
}
|
||||||
outputLayer->input = network[network.size() - 1].a;
|
_output_layer->input = _network[_network.size() - 1].a;
|
||||||
} else {
|
} else {
|
||||||
outputLayer->input = inputSet;
|
_output_layer->input = _input_set;
|
||||||
}
|
}
|
||||||
outputLayer->forwardPass();
|
|
||||||
y_hat = outputLayer->a;
|
_output_layer->forwardPass();
|
||||||
|
_y_hat = _output_layer->a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MLPPMANN::_bind_methods() {
|
||||||
|
/*
|
||||||
|
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPMANN::get_input_set);
|
||||||
|
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPMANN::set_input_set);
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input_set", "get_input_set");
|
||||||
|
|
||||||
|
ClassDB::bind_method(D_METHOD("get_output_set"), &MLPPMANN::get_output_set);
|
||||||
|
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPMANN::set_output_set);
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_output_set", "get_output_set");
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,13 @@
|
|||||||
|
|
||||||
#include "core/math/math_defs.h"
|
#include "core/math/math_defs.h"
|
||||||
|
|
||||||
|
#include "core/object/reference.h"
|
||||||
|
|
||||||
|
#include "../regularization/reg.h"
|
||||||
|
|
||||||
|
#include "../lin_alg/mlpp_matrix.h"
|
||||||
|
#include "../lin_alg/mlpp_vector.h"
|
||||||
|
|
||||||
#include "../hidden_layer/hidden_layer.h"
|
#include "../hidden_layer/hidden_layer.h"
|
||||||
#include "../multi_output_layer/multi_output_layer.h"
|
#include "../multi_output_layer/multi_output_layer.h"
|
||||||
|
|
||||||
@ -19,33 +26,56 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class MLPPMANN {
|
class MLPPMANN : public Reference {
|
||||||
public:
|
GDCLASS(MLPPMANN, Reference);
|
||||||
MLPPMANN(std::vector<std::vector<real_t>> inputSet, std::vector<std::vector<real_t>> outputSet);
|
|
||||||
~MLPPMANN();
|
|
||||||
std::vector<std::vector<real_t>> modelSetTest(std::vector<std::vector<real_t>> X);
|
|
||||||
std::vector<real_t> modelTest(std::vector<real_t> x);
|
|
||||||
void gradientDescent(real_t learning_rate, int max_epoch, bool UI = false);
|
|
||||||
real_t score();
|
|
||||||
void save(std::string fileName);
|
|
||||||
|
|
||||||
void addLayer(int n_hidden, std::string activation, std::string weightInit = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
public:
|
||||||
void addOutputLayer(std::string activation, std::string loss, std::string weightInit = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
/*
|
||||||
|
Ref<MLPPMatrix> get_input_set();
|
||||||
|
void set_input_set(const Ref<MLPPMatrix> &val);
|
||||||
|
|
||||||
|
Ref<MLPPMatrix> get_output_set();
|
||||||
|
void set_output_set(const Ref<MLPPMatrix> &val);
|
||||||
|
*/
|
||||||
|
|
||||||
|
std::vector<std::vector<real_t>> model_set_test(std::vector<std::vector<real_t>> X);
|
||||||
|
std::vector<real_t> model_test(std::vector<real_t> x);
|
||||||
|
|
||||||
|
void gradient_descent(real_t learning_rate, int max_epoch, bool ui = false);
|
||||||
|
real_t score();
|
||||||
|
|
||||||
|
void save(std::string file_name);
|
||||||
|
|
||||||
|
void add_layer(int n_hidden, std::string activation, std::string weight_init = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
||||||
|
void add_output_layer(std::string activation, std::string loss, std::string weight_init = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
||||||
|
|
||||||
|
bool is_initialized();
|
||||||
|
void initialize();
|
||||||
|
|
||||||
|
MLPPMANN(std::vector<std::vector<real_t>> p_input_set, std::vector<std::vector<real_t>> p_output_set);
|
||||||
|
|
||||||
|
MLPPMANN();
|
||||||
|
~MLPPMANN();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
real_t Cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
|
real_t cost(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
|
||||||
void forwardPass();
|
|
||||||
|
|
||||||
std::vector<std::vector<real_t>> inputSet;
|
void forward_pass();
|
||||||
std::vector<std::vector<real_t>> outputSet;
|
|
||||||
std::vector<std::vector<real_t>> y_hat;
|
|
||||||
|
|
||||||
std::vector<MLPPOldHiddenLayer> network;
|
static void _bind_methods();
|
||||||
MLPPOldMultiOutputLayer *outputLayer;
|
|
||||||
|
|
||||||
int n;
|
std::vector<std::vector<real_t>> _input_set;
|
||||||
int k;
|
std::vector<std::vector<real_t>> _output_set;
|
||||||
int n_output;
|
std::vector<std::vector<real_t>> _y_hat;
|
||||||
|
|
||||||
|
std::vector<MLPPOldHiddenLayer> _network;
|
||||||
|
MLPPOldMultiOutputLayer *_output_layer;
|
||||||
|
|
||||||
|
int _n;
|
||||||
|
int _k;
|
||||||
|
int _n_output;
|
||||||
|
|
||||||
|
bool _initialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* MANN_hpp */
|
#endif /* MANN_hpp */
|
@ -99,7 +99,7 @@ protected:
|
|||||||
real_t lambda; /* Regularization Parameter */
|
real_t lambda; /* Regularization Parameter */
|
||||||
real_t alpha; /* This is the controlling param for Elastic Net*/
|
real_t alpha; /* This is the controlling param for Elastic Net*/
|
||||||
|
|
||||||
int _initialized;
|
bool _initialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* MLP_hpp */
|
#endif /* MLP_hpp */
|
||||||
|
@ -627,10 +627,16 @@ void MLPPTests::test_dynamically_sized_mann(bool ui) {
|
|||||||
std::vector<std::vector<real_t>> inputSet = { { 1, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 }, { 4, 8, 12 } };
|
std::vector<std::vector<real_t>> inputSet = { { 1, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 }, { 4, 8, 12 } };
|
||||||
std::vector<std::vector<real_t>> outputSet = { { 1, 5 }, { 2, 10 }, { 3, 15 }, { 4, 20 } };
|
std::vector<std::vector<real_t>> outputSet = { { 1, 5 }, { 2, 10 }, { 3, 15 }, { 4, 20 } };
|
||||||
|
|
||||||
|
MLPPMANNOld mann_old(inputSet, outputSet);
|
||||||
|
mann_old.addOutputLayer("Linear", "MSE");
|
||||||
|
mann_old.gradientDescent(0.001, 80000, false);
|
||||||
|
alg.printMatrix(mann_old.modelSetTest(inputSet));
|
||||||
|
std::cout << "ACCURACY (old): " << 100 * mann_old.score() << "%" << std::endl;
|
||||||
|
|
||||||
MLPPMANN mann(inputSet, outputSet);
|
MLPPMANN mann(inputSet, outputSet);
|
||||||
mann.addOutputLayer("Linear", "MSE");
|
mann.add_output_layer("Linear", "MSE");
|
||||||
mann.gradientDescent(0.001, 80000, 0);
|
mann.gradient_descent(0.001, 80000, false);
|
||||||
alg.printMatrix(mann.modelSetTest(inputSet));
|
alg.printMatrix(mann.model_set_test(inputSet));
|
||||||
std::cout << "ACCURACY: " << 100 * mann.score() << "%" << std::endl;
|
std::cout << "ACCURACY: " << 100 * mann.score() << "%" << std::endl;
|
||||||
}
|
}
|
||||||
void MLPPTests::test_train_test_split_mann(bool ui) {
|
void MLPPTests::test_train_test_split_mann(bool ui) {
|
||||||
@ -662,11 +668,18 @@ void MLPPTests::test_train_test_split_mann(bool ui) {
|
|||||||
PLOG_MSG(split_data.test->get_input()->to_string());
|
PLOG_MSG(split_data.test->get_input()->to_string());
|
||||||
PLOG_MSG(split_data.test->get_output()->to_string());
|
PLOG_MSG(split_data.test->get_output()->to_string());
|
||||||
|
|
||||||
|
MLPPMANNOld mann_old(split_data.train->get_input()->to_std_vector(), split_data.train->get_output()->to_std_vector());
|
||||||
|
mann_old.addLayer(100, "RELU", "XavierNormal");
|
||||||
|
mann_old.addOutputLayer("Softmax", "CrossEntropy", "XavierNormal");
|
||||||
|
mann_old.gradientDescent(0.1, 80000, ui);
|
||||||
|
alg.printMatrix(mann_old.modelSetTest(split_data.test->get_input()->to_std_vector()));
|
||||||
|
std::cout << "ACCURACY (old): " << 100 * mann_old.score() << "%" << std::endl;
|
||||||
|
|
||||||
MLPPMANN mann(split_data.train->get_input()->to_std_vector(), split_data.train->get_output()->to_std_vector());
|
MLPPMANN mann(split_data.train->get_input()->to_std_vector(), split_data.train->get_output()->to_std_vector());
|
||||||
mann.addLayer(100, "RELU", "XavierNormal");
|
mann.add_layer(100, "RELU", "XavierNormal");
|
||||||
mann.addOutputLayer("Softmax", "CrossEntropy", "XavierNormal");
|
mann.add_output_layer("Softmax", "CrossEntropy", "XavierNormal");
|
||||||
mann.gradientDescent(0.1, 80000, ui);
|
mann.gradient_descent(0.1, 80000, ui);
|
||||||
alg.printMatrix(mann.modelSetTest(split_data.test->get_input()->to_std_vector()));
|
alg.printMatrix(mann.model_set_test(split_data.test->get_input()->to_std_vector()));
|
||||||
std::cout << "ACCURACY: " << 100 * mann.score() << "%" << std::endl;
|
std::cout << "ACCURACY: " << 100 * mann.score() << "%" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user