diff --git a/mlpp/wgan/wgan.cpp b/mlpp/wgan/wgan.cpp index 968e414..d37a290 100644 --- a/mlpp/wgan/wgan.cpp +++ b/mlpp/wgan/wgan.cpp @@ -15,20 +15,20 @@ #include -MLPPWGAN::MLPPWGAN(real_t k, std::vector> outputSet) : +MLPPWGANOld::MLPPWGANOld(real_t k, std::vector> outputSet) : outputSet(outputSet), n(outputSet.size()), k(k) { } -MLPPWGAN::~MLPPWGAN() { +MLPPWGANOld::~MLPPWGANOld() { delete outputLayer; } -std::vector> MLPPWGAN::generateExample(int n) { +std::vector> MLPPWGANOld::generateExample(int n) { MLPPLinAlg alg; return modelSetTestGenerator(alg.gaussianNoise(n, k)); } -void MLPPWGAN::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { +void MLPPWGANOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { class MLPPCost cost; MLPPLinAlg alg; real_t cost_prev = 0; @@ -50,7 +50,7 @@ void MLPPWGAN::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { for (int i = 0; i < CRITIC_INTERATIONS; i++) { generatorInputSet = alg.gaussianNoise(n, k); discriminatorInputSet = modelSetTestGenerator(generatorInputSet); - discriminatorInputSet.insert(discriminatorInputSet.end(), MLPPWGAN::outputSet.begin(), MLPPWGAN::outputSet.end()); // Fake + real inputs. + discriminatorInputSet.insert(discriminatorInputSet.end(), MLPPWGANOld::outputSet.begin(), MLPPWGANOld::outputSet.end()); // Fake + real inputs. y_hat = modelSetTestDiscriminator(discriminatorInputSet); outputSet = alg.scalarMultiply(-1, alg.onevec(n)); // WGAN changes y_i = 1 and y_i = 0 to y_i = 1 and y_i = -1 @@ -75,7 +75,7 @@ void MLPPWGAN::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { forwardPass(); if (UI) { - MLPPWGAN::UI(epoch, cost_prev, MLPPWGAN::y_hat, alg.onevec(n)); + MLPPWGANOld::UI(epoch, cost_prev, MLPPWGANOld::y_hat, alg.onevec(n)); } epoch++; @@ -85,14 +85,14 @@ void MLPPWGAN::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { } } -real_t MLPPWGAN::score() { +real_t MLPPWGANOld::score() { MLPPLinAlg alg; MLPPUtilities util; forwardPass(); return util.performance(y_hat, alg.onevec(n)); } -void MLPPWGAN::save(std::string fileName) { +void MLPPWGANOld::save(std::string fileName) { MLPPUtilities util; if (!network.empty()) { util.saveParameters(fileName, network[0].weights, network[0].bias, 0, 1); @@ -105,7 +105,7 @@ void MLPPWGAN::save(std::string fileName) { } } -void MLPPWGAN::addLayer(int n_hidden, std::string activation, std::string weightInit, std::string reg, real_t lambda, real_t alpha) { +void MLPPWGANOld::addLayer(int n_hidden, std::string activation, std::string weightInit, std::string reg, real_t lambda, real_t alpha) { MLPPLinAlg alg; if (network.empty()) { network.push_back(MLPPOldHiddenLayer(n_hidden, activation, alg.gaussianNoise(n, k), weightInit, reg, lambda, alpha)); @@ -116,7 +116,7 @@ void MLPPWGAN::addLayer(int n_hidden, std::string activation, std::string weight } } -void MLPPWGAN::addOutputLayer(std::string weightInit, std::string reg, real_t lambda, real_t alpha) { +void MLPPWGANOld::addOutputLayer(std::string weightInit, std::string reg, real_t lambda, real_t alpha) { MLPPLinAlg alg; if (!network.empty()) { outputLayer = new MLPPOldOutputLayer(network[network.size() - 1].n_hidden, "Linear", "WassersteinLoss", network[network.size() - 1].a, weightInit, "WeightClipping", -0.01, 0.01); @@ -125,7 +125,7 @@ void MLPPWGAN::addOutputLayer(std::string weightInit, std::string reg, real_t la } } -std::vector> MLPPWGAN::modelSetTestGenerator(std::vector> X) { +std::vector> MLPPWGANOld::modelSetTestGenerator(std::vector> X) { if (!network.empty()) { network[0].input = X; network[0].forwardPass(); @@ -138,7 +138,7 @@ std::vector> MLPPWGAN::modelSetTestGenerator(std::vector MLPPWGAN::modelSetTestDiscriminator(std::vector> X) { +std::vector MLPPWGANOld::modelSetTestDiscriminator(std::vector> X) { if (!network.empty()) { for (int i = network.size() / 2 + 1; i < network.size(); i++) { if (i == network.size() / 2 + 1) { @@ -154,7 +154,7 @@ std::vector MLPPWGAN::modelSetTestDiscriminator(std::vectora; } -real_t MLPPWGAN::Cost(std::vector y_hat, std::vector y) { +real_t MLPPWGANOld::Cost(std::vector y_hat, std::vector y) { MLPPReg regularization; class MLPPCost cost; real_t totalRegTerm = 0; @@ -168,7 +168,7 @@ real_t MLPPWGAN::Cost(std::vector y_hat, std::vector y) { return (cost.*cost_function)(y_hat, y) + totalRegTerm + regularization.regTerm(outputLayer->weights, outputLayer->lambda, outputLayer->alpha, outputLayer->reg); } -void MLPPWGAN::forwardPass() { +void MLPPWGANOld::forwardPass() { MLPPLinAlg alg; if (!network.empty()) { network[0].input = alg.gaussianNoise(n, k); @@ -186,7 +186,7 @@ void MLPPWGAN::forwardPass() { y_hat = outputLayer->a; } -void MLPPWGAN::updateDiscriminatorParameters(std::vector>> hiddenLayerUpdations, std::vector outputLayerUpdation, real_t learning_rate) { +void MLPPWGANOld::updateDiscriminatorParameters(std::vector>> hiddenLayerUpdations, std::vector outputLayerUpdation, real_t learning_rate) { MLPPLinAlg alg; outputLayer->weights = alg.subtraction(outputLayer->weights, outputLayerUpdation); @@ -203,7 +203,7 @@ void MLPPWGAN::updateDiscriminatorParameters(std::vector>> hiddenLayerUpdations, real_t learning_rate) { +void MLPPWGANOld::updateGeneratorParameters(std::vector>> hiddenLayerUpdations, real_t learning_rate) { MLPPLinAlg alg; if (!network.empty()) { @@ -216,7 +216,7 @@ void MLPPWGAN::updateGeneratorParameters(std::vector>>, std::vector> MLPPWGAN::computeDiscriminatorGradients(std::vector y_hat, std::vector outputSet) { +std::tuple>>, std::vector> MLPPWGANOld::computeDiscriminatorGradients(std::vector y_hat, std::vector outputSet) { class MLPPCost cost; MLPPActivation avn; MLPPLinAlg alg; @@ -252,7 +252,7 @@ std::tuple>>, std::vector> M return { cumulativeHiddenLayerWGrad, outputWGrad }; } -std::vector>> MLPPWGAN::computeGeneratorGradients(std::vector y_hat, std::vector outputSet) { +std::vector>> MLPPWGANOld::computeGeneratorGradients(std::vector y_hat, std::vector outputSet) { class MLPPCost cost; MLPPActivation avn; MLPPLinAlg alg; @@ -281,7 +281,7 @@ std::vector>> MLPPWGAN::computeGeneratorGradient return cumulativeHiddenLayerWGrad; } -void MLPPWGAN::UI(int epoch, real_t cost_prev, std::vector y_hat, std::vector outputSet) { +void MLPPWGANOld::UI(int epoch, real_t cost_prev, std::vector y_hat, std::vector outputSet) { MLPPUtilities::CostInfo(epoch, cost_prev, Cost(y_hat, outputSet)); std::cout << "Layer " << network.size() + 1 << ": " << std::endl; MLPPUtilities::UI(outputLayer->weights, outputLayer->bias); diff --git a/mlpp/wgan/wgan.h b/mlpp/wgan/wgan.h index 994306f..98bc093 100644 --- a/mlpp/wgan/wgan.h +++ b/mlpp/wgan/wgan.h @@ -17,10 +17,10 @@ -class MLPPWGAN { +class MLPPWGANOld { public: - MLPPWGAN(real_t k, std::vector> outputSet); - ~MLPPWGAN(); + MLPPWGANOld(real_t k, std::vector> outputSet); + ~MLPPWGANOld(); std::vector> generateExample(int n); void gradientDescent(real_t learning_rate, int max_epoch, bool UI = false); real_t score(); diff --git a/test/mlpp_tests.cpp b/test/mlpp_tests.cpp index 5680656..c2275a9 100644 --- a/test/mlpp_tests.cpp +++ b/test/mlpp_tests.cpp @@ -488,7 +488,7 @@ void MLPPTests::test_wgan(bool ui) { { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40 } }; - MLPPWGAN gan(2, alg.transpose(outputSet)); // our gan is a wasserstein gan (wgan) + MLPPWGANOld gan(2, alg.transpose(outputSet)); // our gan is a wasserstein gan (wgan) gan.addLayer(5, "Sigmoid"); gan.addLayer(2, "RELU"); gan.addLayer(5, "Sigmoid");