Fixed warnings in MLPPDualSVC.

This commit is contained in:
Relintai 2023-02-10 22:33:32 +01:00
parent 1e793de7f7
commit 3036db18fb
2 changed files with 21 additions and 16 deletions

View File

@ -14,9 +14,14 @@
#include <iostream> #include <iostream>
#include <random> #include <random>
MLPPDualSVC::MLPPDualSVC(std::vector<std::vector<real_t>> p_inputSet, std::vector<real_t> p_outputSet, real_t p_C, std::string p_kernel) {
inputSet = p_inputSet;
outputSet = p_outputSet;
n = p_inputSet.size();
k = p_inputSet[0].size();
C = p_C;
kernel = p_kernel;
MLPPDualSVC::MLPPDualSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C, std::string kernel) :
inputSet(inputSet), outputSet(outputSet), n(inputSet.size()), k(inputSet[0].size()), C(C), kernel(kernel) {
y_hat.resize(n); y_hat.resize(n);
bias = MLPPUtilities::biasInitialization(); bias = MLPPUtilities::biasInitialization();
alpha = MLPPUtilities::weightInitialization(n); // One alpha for all training examples, as per the lagrangian multipliers. alpha = MLPPUtilities::weightInitialization(n); // One alpha for all training examples, as per the lagrangian multipliers.
@ -49,10 +54,10 @@ void MLPPDualSVC::gradientDescent(real_t learning_rate, int max_epoch, bool UI)
// Calculating the bias // Calculating the bias
real_t biasGradient = 0; real_t biasGradient = 0;
for (int i = 0; i < alpha.size(); i++) { for (uint32_t i = 0; i < alpha.size(); i++) {
real_t sum = 0; real_t sum = 0;
if (alpha[i] < C && alpha[i] > 0) { if (alpha[i] < C && alpha[i] > 0) {
for (int j = 0; j < alpha.size(); j++) { for (uint32_t j = 0; j < alpha.size(); j++) {
if (alpha[j] > 0) { if (alpha[j] > 0) {
sum += alpha[j] * outputSet[j] * alg.dot(inputSet[j], inputSet[i]); // TO DO: DON'T forget to add non-linear kernelizations. sum += alpha[j] * outputSet[j] * alg.dot(inputSet[j], inputSet[i]); // TO DO: DON'T forget to add non-linear kernelizations.
} }
@ -153,12 +158,12 @@ void MLPPDualSVC::gradientDescent(real_t learning_rate, int max_epoch, bool UI)
// } // }
real_t MLPPDualSVC::score() { real_t MLPPDualSVC::score() {
MLPPUtilities util; MLPPUtilities util;
return util.performance(y_hat, outputSet); return util.performance(y_hat, outputSet);
} }
void MLPPDualSVC::save(std::string fileName) { void MLPPDualSVC::save(std::string fileName) {
MLPPUtilities util; MLPPUtilities util;
util.saveParameters(fileName, alpha, bias); util.saveParameters(fileName, alpha, bias);
} }
@ -175,9 +180,9 @@ std::vector<real_t> MLPPDualSVC::Evaluate(std::vector<std::vector<real_t>> X) {
std::vector<real_t> MLPPDualSVC::propagate(std::vector<std::vector<real_t>> X) { std::vector<real_t> MLPPDualSVC::propagate(std::vector<std::vector<real_t>> X) {
MLPPLinAlg alg; MLPPLinAlg alg;
std::vector<real_t> z; std::vector<real_t> z;
for (int i = 0; i < X.size(); i++) { for (uint32_t i = 0; i < X.size(); i++) {
real_t sum = 0; real_t sum = 0;
for (int j = 0; j < alpha.size(); j++) { for (uint32_t j = 0; j < alpha.size(); j++) {
if (alpha[j] != 0) { if (alpha[j] != 0) {
sum += alpha[j] * outputSet[j] * alg.dot(inputSet[j], X[i]); // TO DO: DON'T forget to add non-linear kernelizations. sum += alpha[j] * outputSet[j] * alg.dot(inputSet[j], X[i]); // TO DO: DON'T forget to add non-linear kernelizations.
} }
@ -196,7 +201,7 @@ real_t MLPPDualSVC::Evaluate(std::vector<real_t> x) {
real_t MLPPDualSVC::propagate(std::vector<real_t> x) { real_t MLPPDualSVC::propagate(std::vector<real_t> x) {
MLPPLinAlg alg; MLPPLinAlg alg;
real_t z = 0; real_t z = 0;
for (int j = 0; j < alpha.size(); j++) { for (uint32_t j = 0; j < alpha.size(); j++) {
if (alpha[j] != 0) { if (alpha[j] != 0) {
z += alpha[j] * outputSet[j] * alg.dot(inputSet[j], x); // TO DO: DON'T forget to add non-linear kernelizations. z += alpha[j] * outputSet[j] * alg.dot(inputSet[j], x); // TO DO: DON'T forget to add non-linear kernelizations.
} }
@ -206,7 +211,6 @@ real_t MLPPDualSVC::propagate(std::vector<real_t> x) {
} }
void MLPPDualSVC::forwardPass() { void MLPPDualSVC::forwardPass() {
MLPPLinAlg alg;
MLPPActivation avn; MLPPActivation avn;
z = propagate(inputSet); z = propagate(inputSet);
@ -214,7 +218,7 @@ void MLPPDualSVC::forwardPass() {
} }
void MLPPDualSVC::alphaProjection() { void MLPPDualSVC::alphaProjection() {
for (int i = 0; i < alpha.size(); i++) { for (uint32_t i = 0; i < alpha.size(); i++) {
if (alpha[i] > C) { if (alpha[i] > C) {
alpha[i] = C; alpha[i] = C;
} else if (alpha[i] < 0) { } else if (alpha[i] < 0) {
@ -227,12 +231,16 @@ real_t MLPPDualSVC::kernelFunction(std::vector<real_t> u, std::vector<real_t> v,
MLPPLinAlg alg; MLPPLinAlg alg;
if (kernel == "Linear") { if (kernel == "Linear") {
return alg.dot(u, v); return alg.dot(u, v);
} // warning: non-void function does not return a value in all control paths [-Wreturn-type] }
return 0;
} }
std::vector<std::vector<real_t>> MLPPDualSVC::kernelFunction(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B, std::string kernel) { std::vector<std::vector<real_t>> MLPPDualSVC::kernelFunction(std::vector<std::vector<real_t>> A, std::vector<std::vector<real_t>> B, std::string kernel) {
MLPPLinAlg alg; MLPPLinAlg alg;
if (kernel == "Linear") { if (kernel == "Linear") {
return alg.matmult(inputSet, alg.transpose(inputSet)); return alg.matmult(inputSet, alg.transpose(inputSet));
} // warning: non-void function does not return a value in all control paths [-Wreturn-type] }
return std::vector<std::vector<real_t>>();
} }

View File

@ -16,8 +16,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
class MLPPDualSVC { class MLPPDualSVC {
public: public:
MLPPDualSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C, std::string kernel = "Linear"); MLPPDualSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C, std::string kernel = "Linear");
@ -68,5 +66,4 @@ private:
void UI(int epoch, real_t cost_prev); void UI(int epoch, real_t cost_prev);
}; };
#endif /* DualSVC_hpp */ #endif /* DualSVC_hpp */