stagadish / nnplusplus Goto Github PK
View Code? Open in Web Editor NEWA small and easy to use neural net implementation for C++. Just download and #include!
License: MIT License
A small and easy to use neural net implementation for C++. Just download and #include!
License: MIT License
Hey,
Today I have got to know about your library thru your post in G+. I am also trying to write a similar library/toolbox to be used in optimization.
At first look at your library, I can say that it looks neat. However, it would be great if you considered using templates in your data structures. Or, if you would like to go for inheritance for some reason, then you can abstract some functionality in your base classes.
What I mean by the above comment is that, for example, you could use an activation function
abstraction and keep a pointer inside your NeuralNet
class to support different activation functions, such as:
#include <iostream>
#include <vector>
template <typename T>
class IFunction {
public:
virtual T operator()(T variable) const = 0;
virtual ~IFunction() = default;
};
template <typename T>
class PReLU : public IFunction<T> {
private:
T alpha_;
public:
PReLU(T alpha = T { 0 }) : alpha_ {alpha} { }
T operator()(T variable) const override {
T zero { 0 };
return variable < zero ? alpha_*variable : variable;
}
};
template <typename T>
class Identity : public IFunction<T> {
public:
T operator()(T variable) const override {
return variable;
}
};
int main (int argc, char* argv[]) {
Identity <double> identity_function { };
PReLU <double> relu_function { };
PReLU <double> absolute_value { -1. };
IFunction <double> *generic_func_ptr { nullptr };
size_t numelems { 100ul };
double xmin { -5. }, xmax { 5. }, dx {(xmax - xmin)/(numelems-1)};
std::vector <double> xvalues ( numelems );
for (size_t idx = 0; idx < xvalues.size(); idx++)
xvalues[idx] = xmin + idx*dx;
// either directly call as if your instances were functions (actually, they
// are functors right now, with their operator()'s overloaded)
for (auto x : xvalues)
std::cout << "x: " << x
<< ", identity_function(x): " << identity_function(x)
<< ", relu_function(x): " << relu_function(x)
<< std::endl;
// or, use polymorphism
generic_func_ptr = &identity_function;
for (auto x : xvalues)
std::cout << "x: " << x
<< ", generic_func_ptr->operator()(x): "
<< generic_func_ptr->operator()(x)
<< std::endl;
// or, both
generic_func_ptr = &relu_function;
for (auto x : xvalues)
std::cout << "x: " << x
<< ", (*generic_func_ptr)(x): " << (*generic_func_ptr)(x)
<< std::endl;
// absolute value function
for (auto x : xvalues)
std::cout << "x: " << x
<< ", absolute_value(x): " << absolute_value(x)
<< std::endl;
return 0;
}
I hope this helps :) Good luck with your code!...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.