mirror of
https://github.com/Relintai/pmlpp.git
synced 2025-04-17 03:46:04 +02:00
Fixed warnings in SVC.
This commit is contained in:
parent
62492c8fde
commit
605a10d8f6
@ -14,14 +14,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
|
|
||||||
MLPPSVC::MLPPSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C) :
|
|
||||||
inputSet(inputSet), outputSet(outputSet), n(inputSet.size()), k(inputSet[0].size()), C(C) {
|
|
||||||
y_hat.resize(n);
|
|
||||||
weights = MLPPUtilities::weightInitialization(k);
|
|
||||||
bias = MLPPUtilities::biasInitialization();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<real_t> MLPPSVC::modelSetTest(std::vector<std::vector<real_t>> X) {
|
std::vector<real_t> MLPPSVC::modelSetTest(std::vector<std::vector<real_t>> X) {
|
||||||
return Evaluate(X);
|
return Evaluate(X);
|
||||||
}
|
}
|
||||||
@ -78,7 +70,7 @@ void MLPPSVC::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
std::uniform_int_distribution<int> distribution(0, int(n - 1));
|
std::uniform_int_distribution<int> distribution(0, int(n - 1));
|
||||||
int outputIndex = distribution(generator);
|
int outputIndex = distribution(generator);
|
||||||
|
|
||||||
real_t y_hat = Evaluate(inputSet[outputIndex]);
|
//real_t y_hat = Evaluate(inputSet[outputIndex]);
|
||||||
real_t z = propagate(inputSet[outputIndex]);
|
real_t z = propagate(inputSet[outputIndex]);
|
||||||
cost_prev = Cost({ z }, { outputSet[outputIndex] }, weights, C);
|
cost_prev = Cost({ z }, { outputSet[outputIndex] }, weights, C);
|
||||||
|
|
||||||
@ -91,12 +83,13 @@ void MLPPSVC::SGD(real_t learning_rate, int max_epoch, bool UI) {
|
|||||||
// Bias updation
|
// Bias updation
|
||||||
bias -= learning_rate * costDeriv;
|
bias -= learning_rate * costDeriv;
|
||||||
|
|
||||||
y_hat = Evaluate({ inputSet[outputIndex] });
|
//y_hat = Evaluate({ inputSet[outputIndex] });
|
||||||
|
|
||||||
if (UI) {
|
if (UI) {
|
||||||
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ z }, { outputSet[outputIndex] }, weights, C));
|
MLPPUtilities::CostInfo(epoch, cost_prev, Cost({ z }, { outputSet[outputIndex] }, weights, C));
|
||||||
MLPPUtilities::UI(weights, bias);
|
MLPPUtilities::UI(weights, bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
epoch++;
|
epoch++;
|
||||||
|
|
||||||
if (epoch > max_epoch) {
|
if (epoch > max_epoch) {
|
||||||
@ -116,7 +109,9 @@ void MLPPSVC::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, boo
|
|||||||
|
|
||||||
// Creating the mini-batches
|
// Creating the mini-batches
|
||||||
int n_mini_batch = n / mini_batch_size;
|
int n_mini_batch = n / mini_batch_size;
|
||||||
auto [inputMiniBatches, outputMiniBatches] = MLPPUtilities::createMiniBatches(inputSet, outputSet, n_mini_batch);
|
auto batches = MLPPUtilities::createMiniBatches(inputSet, outputSet, n_mini_batch);
|
||||||
|
auto inputMiniBatches = std::get<0>(batches);
|
||||||
|
auto outputMiniBatches = std::get<1>(batches);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
for (int i = 0; i < n_mini_batch; i++) {
|
for (int i = 0; i < n_mini_batch; i++) {
|
||||||
@ -158,6 +153,18 @@ void MLPPSVC::save(std::string fileName) {
|
|||||||
util.saveParameters(fileName, weights, bias);
|
util.saveParameters(fileName, weights, bias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MLPPSVC::MLPPSVC(std::vector<std::vector<real_t>> p_inputSet, std::vector<real_t> p_outputSet, real_t p_C) {
|
||||||
|
inputSet = p_inputSet;
|
||||||
|
outputSet = p_outputSet;
|
||||||
|
n = inputSet.size();
|
||||||
|
k = inputSet[0].size();
|
||||||
|
C = p_C;
|
||||||
|
|
||||||
|
y_hat.resize(n);
|
||||||
|
weights = MLPPUtilities::weightInitialization(k);
|
||||||
|
bias = MLPPUtilities::biasInitialization();
|
||||||
|
}
|
||||||
|
|
||||||
real_t MLPPSVC::Cost(std::vector<real_t> z, std::vector<real_t> y, std::vector<real_t> weights, real_t C) {
|
real_t MLPPSVC::Cost(std::vector<real_t> z, std::vector<real_t> y, std::vector<real_t> weights, real_t C) {
|
||||||
class MLPPCost cost;
|
class MLPPCost cost;
|
||||||
return cost.HingeLoss(z, y, weights, C);
|
return cost.HingeLoss(z, y, weights, C);
|
||||||
@ -189,7 +196,6 @@ real_t MLPPSVC::propagate(std::vector<real_t> x) {
|
|||||||
|
|
||||||
// sign ( wTx + b )
|
// sign ( wTx + b )
|
||||||
void MLPPSVC::forwardPass() {
|
void MLPPSVC::forwardPass() {
|
||||||
MLPPLinAlg alg;
|
|
||||||
MLPPActivation avn;
|
MLPPActivation avn;
|
||||||
|
|
||||||
z = propagate(inputSet);
|
z = propagate(inputSet);
|
||||||
|
@ -16,11 +16,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MLPPSVC {
|
class MLPPSVC {
|
||||||
public:
|
public:
|
||||||
MLPPSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C);
|
|
||||||
std::vector<real_t> modelSetTest(std::vector<std::vector<real_t>> X);
|
std::vector<real_t> modelSetTest(std::vector<std::vector<real_t>> X);
|
||||||
real_t modelTest(std::vector<real_t> x);
|
real_t modelTest(std::vector<real_t> x);
|
||||||
void gradientDescent(real_t learning_rate, int max_epoch, bool UI = false);
|
void gradientDescent(real_t learning_rate, int max_epoch, bool UI = false);
|
||||||
@ -29,6 +26,8 @@ public:
|
|||||||
real_t score();
|
real_t score();
|
||||||
void save(std::string fileName);
|
void save(std::string fileName);
|
||||||
|
|
||||||
|
MLPPSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
real_t Cost(std::vector<real_t> y_hat, std::vector<real_t> y, std::vector<real_t> weights, real_t C);
|
real_t Cost(std::vector<real_t> y_hat, std::vector<real_t> y, std::vector<real_t> weights, real_t C);
|
||||||
|
|
||||||
@ -53,5 +52,4 @@ private:
|
|||||||
void UI(int epoch, real_t cost_prev);
|
void UI(int epoch, real_t cost_prev);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif /* SVC_hpp */
|
#endif /* SVC_hpp */
|
||||||
|
Loading…
Reference in New Issue
Block a user