A simple neural network implementation in C++. This project is a learning exercise to improve C++ skills and understand neural network basics.
Tensor
class for matrix operationstorch.nn
like modules likeLinear
,ReLU
- Forward and backward pass using optimizer
-
Clone the repository:
git clone github.com/gladuz/nn-cpp.git
-
Compile:
make
-
Run:
./build/nn_project
Tensor data({1, 2, 3, 4, 5, 6}, 2, 3); // 2x3 matrix
Tensor target({2, -2}, 2, 1);
nn::Sequential seq;
seq.add(std::make_shared<nn::Linear>(3, 5));
seq.add(std::make_shared<nn::ReLU>());
seq.add(std::make_shared<nn::Linear>(5, 7));
seq.add(std::make_shared<nn::ReLU>());
seq.add(std::make_shared<nn::Linear>(7, 1));
auto optimizer = optim::SGD(seq.parameters(), 0.01);
for(int i=0; i<100; i++){
auto out = seq.forward(data);
auto [loss, loss_grad] = nn::functional::mse_loss(out, target);
cout<<"Loss at epoch: "<<i<<" is: "<<loss<<endl;
seq.backward(loss_grad);
optimizer.step();
}
- Add dataloader example with MNIST
- Implement additional layer types