https://github.com/jancervenka/net
Neural network classifier implemented in Eigen
https://github.com/jancervenka/net
cpp eigen neural-network
Last synced: 9 months ago
JSON representation
Neural network classifier implemented in Eigen
- Host: GitHub
- URL: https://github.com/jancervenka/net
- Owner: jancervenka
- Created: 2020-05-10T18:40:04.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-14T05:36:57.000Z (almost 6 years ago)
- Last Synced: 2025-04-12T19:19:25.102Z (about 1 year ago)
- Topics: cpp, eigen, neural-network
- Language: C++
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[0]: http://eigen.tuxfamily.org/index.php?title=Main_Page
[1]: https://cs231n.github.io/neural-networks-case-study/
# Net
Neural network classifier implemented in C++ and [Eigen numeric library][0].
The code is based on the [Python example][1] from Stanford CS231.
The classifer is using softmax and L2 regularization to compute the loss.
Training is done with vanilla gradient descent. The network architecture
consists of one hidden layer with RELU activations.
## Usage
The `Net` constructor requires number of features and classes in
the classification problem and size of the network hidden layer.
```cpp
Net(int n_features, int n_classes, int hidden_size);
```
Training a model requires a `MatrixXd` feature matrix, `VectorXi`
target labels and number of epochs.
```cpp
void fit(const Eigen::MatrixXd &X, const Eigen::VectorXi &y, int epochs);
```
Classifying new samples requires a `MatrixXd` feature matrix.
The output is a matrix with class probabilites for each sample.
```cpp
Eigen::MatrixXd predict(const Eigen::MatrixXd &X);
```
### Example
```cpp
int main() {
Eigen::MatrixXd X_train = load_X_train();
Eigen::VectorXi y_train = load_y_train();
Eigen::MatrixXd X = load_X();
int epochs = 100;
Net nn(2, 2, 64);
nn.fit(X_train, y_train, epochs);
Eigen::VectorXi y_pred = nn.predict(X);
}
```
A working example can be found in `example/main.cpp`. On OS X, it can be built by
running `make` (assuming `Eigen` is in `/usr/local/include`).