codezonediitj / BNN

Deep Learning Framework with a specialisation aimed for Binarized Neural Networks.
Other
10 stars 6 forks source link

Model API #18

Open czgdp1807 opened 4 years ago

czgdp1807 commented 4 years ago

Description of the problem

APIs for models(train, test) need to be discussed in the issue. A simple use case for starting, Image classification on MNIST dataset with 784-200-200-10.

Example of the problem

References/Other comments

czgdp1807 commented 4 years ago
#include <bnn/nn/models.hpp>
#include <bnn/nn/layers.hpp>
#include <bnn/metrics/losses.hpp>
#include <bnn/nn/optimisers.hpp>
#include <bnn/nn/activations.hpp>
#include <bnn/data/read.hpp>
#include <bnn/core/tensor.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace bnn;

int main()
{

    TensorCPU<float>* mnist = load_data("mnist_train_images");
    TensorCPU<float>* labels = load_data("mnist_train_labels");

    vector<unsigned> shape_1 = {784};
    vector<unsigned> shape_2 = {200};
    vector<unsigned> shape_3 = {10};
    Dense<data_type>* Dense_1 = new Dense<data_type>(input_shape=shape_1, output_shape=shape_2, activation=relu);
    Dense<data_type>* Dense_2 = new Dense<data_type>(shape_2, shape_2, activation=relu);
    Dense<data_type>* Output = new Dense<data_type>(shape_2, shape_3, activation=softmax);

    Model* mnist_nn = new Model(Dense_1, Dense_2, Output);
    mnist_nn->train(input_data=mnist, targets=labels, optimiser=adam, loss=cross_entropy, batch_size=128, epochs=600);

    TensorCPU<float>* mnist_test = load_data("mnist_test_images", flatten=true, normalise=true);
    TensorCPU<float>* labels_test = load_data("mnist_test_labels", one_hot=true);
    float test_error = mnist_nn->test(test_input=mnist_test, targets=labels_test, eval_metric=error_rate);
    std::cout<<test_error<<std::endl;

}
czgdp1807 commented 4 years ago

The above is a sample code for training and testing a classification model. It highlights some things that need to be implemented before we can think of working on model APIs.

czgdp1807 commented 4 years ago

A few functions are to be added in tensor_ops.hpp,

  1. normalise(TensorCPU<data_type>* t, const string& type, unsigned axis=-1) - This will normalise the specified axis of the tensor.
  2. flatten(TensorCPU<data_type>* t) - This will flatten the complete tensor in place.