mirror of
https://github.com/Relintai/MLPP.git
synced 2025-02-20 16:54:22 +01:00
"Vectorized" sigmoid
This commit is contained in:
parent
db8283212d
commit
558e138948
@ -38,33 +38,15 @@ namespace MLPP{
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<double> Activation::sigmoid(std::vector<double> z, bool deriv){
|
std::vector<double> Activation::sigmoid(std::vector<double> z, bool deriv){
|
||||||
if(deriv) {
|
LinAlg alg;
|
||||||
LinAlg alg;
|
if(deriv) { return alg.subtraction(sigmoid(z), alg.hadamard_product(sigmoid(z), sigmoid(z))); }
|
||||||
return alg.subtraction(sigmoid(z), alg.hadamard_product(sigmoid(z), sigmoid(z)));
|
return alg.elementWiseDivision(alg.onevec(z.size()), alg.addition(alg.onevec(z.size()), alg.exp(alg.scalarMultiply(-1, z))));
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<double> a;
|
|
||||||
a.resize(z.size());
|
|
||||||
|
|
||||||
for(int i = 0; i < z.size(); i++){
|
|
||||||
a[i] = sigmoid(z[i]);
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::vector<double>> Activation::sigmoid(std::vector<std::vector<double>> z, bool deriv){
|
std::vector<std::vector<double>> Activation::sigmoid(std::vector<std::vector<double>> z, bool deriv){
|
||||||
if(deriv) {
|
LinAlg alg;
|
||||||
LinAlg alg;
|
if(deriv) { return alg.subtraction(sigmoid(z), alg.hadamard_product(sigmoid(z), sigmoid(z))); }
|
||||||
return alg.subtraction(sigmoid(z), alg.hadamard_product(sigmoid(z), sigmoid(z)));
|
return alg.elementWiseDivision(alg.onemat(z.size(), z[0].size()), alg.addition(alg.onemat(z.size(), z[0].size()), alg.exp(alg.scalarMultiply(-1, z))));
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::vector<double>> a;
|
|
||||||
a.resize(z.size());
|
|
||||||
|
|
||||||
for(int i = 0; i < z.size(); i++){
|
|
||||||
a[i] = sigmoid(z[i]);
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<double> Activation::softmax(std::vector<double> z){
|
std::vector<double> Activation::softmax(std::vector<double> z){
|
||||||
|
23
main.cpp
23
main.cpp
@ -231,7 +231,7 @@ int main() {
|
|||||||
// ANN ann(alg.transpose(inputSet), outputSet);
|
// ANN ann(alg.transpose(inputSet), outputSet);
|
||||||
// ann.addLayer(10, "RELU", "Default", "Ridge", 0.0001);
|
// ann.addLayer(10, "RELU", "Default", "Ridge", 0.0001);
|
||||||
// ann.addLayer(10, "Sigmoid", "Default");
|
// ann.addLayer(10, "Sigmoid", "Default");
|
||||||
// ann.addOutputLayer("Softplus", "LogLoss", "XavierNormal");
|
// ann.addOutputLayer("Sigmoid", "LogLoss", "XavierNormal");
|
||||||
// ann.gradientDescent(0.1, 80000, 0);
|
// ann.gradientDescent(0.1, 80000, 0);
|
||||||
// alg.printVector(ann.modelSetTest(alg.transpose(inputSet)));
|
// alg.printVector(ann.modelSetTest(alg.transpose(inputSet)));
|
||||||
// std::cout << "ACCURACY: " << 100 * ann.score() << "%" << std::endl;
|
// std::cout << "ACCURACY: " << 100 * ann.score() << "%" << std::endl;
|
||||||
@ -348,18 +348,19 @@ int main() {
|
|||||||
// OutlierFinder outlierFinder(2); // Any datapoint outside of 2 stds from the mean is marked as an outlier.
|
// OutlierFinder outlierFinder(2); // Any datapoint outside of 2 stds from the mean is marked as an outlier.
|
||||||
// alg.printVector(outlierFinder.modelTest(inputSet));
|
// alg.printVector(outlierFinder.modelTest(inputSet));
|
||||||
|
|
||||||
// Testing for new Functions
|
// // Testing for new Functions
|
||||||
double z_s = 4;
|
// double z_s = 4;
|
||||||
std::cout << avn.sinh(z_s) << std::endl;
|
// std::cout << avn.sigmoid(z_s) << std::endl;
|
||||||
std::cout << avn.sinh(z_s, 1) << std::endl;
|
// std::cout << avn.sigmoid(z_s, 1) << std::endl;
|
||||||
|
|
||||||
std::vector<double> z_v = {4, 5};
|
// std::vector<double> z_v = {4, 5};
|
||||||
alg.printVector(avn.sinh(z_v));
|
// alg.printVector(avn.sigmoid(z_v));
|
||||||
alg.printVector(avn.sinh(z_v, 1));
|
// alg.printVector(avn.sigmoid(z_v, 1));
|
||||||
|
|
||||||
|
// std::vector<std::vector<double>> Z_m = {{4, 5}};
|
||||||
|
// alg.printMatrix(avn.sigmoid(Z_m));
|
||||||
|
// alg.printMatrix(avn.sigmoid(Z_m, 1));
|
||||||
|
|
||||||
std::vector<std::vector<double>> Z_m = {{4, 5}};
|
|
||||||
alg.printMatrix(avn.sinh(Z_m));
|
|
||||||
alg.printMatrix(avn.sinh(Z_m, 1));
|
|
||||||
// alg.printMatrix(alg.pinverse({{1,2}, {3,4}}));
|
// alg.printMatrix(alg.pinverse({{1,2}, {3,4}}));
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user