HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Neural net in C++

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
neuralnetstackoverflow

Problem

I wrote a Matrix struct and a neural net that uses it. Why is this slow? Gprof blames Matrix::operator()(int, int) which I suppose is my constructor, and Matrix::operator*(Matrix) and vector::operator[].

```
#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

#define debug(x) cout _rng(0, 1);

auto rng = [](){return _rng(generator);};
auto logistic = [](float x){return 1/(1+exp(-x));};
auto square = [](float x){return x*x;};

struct Matrix{
int rows, cols, size;
int label=-1;
vector data;

Matrix(int r=1, int c=1){ //column major
rows = r;
cols = c;
size = r*c;
data.resize(size);
}

Matrix one_to_many(){
Matrix m(10);
m.data[label] = 1;
return m;
}
//
// Matrix(const Matrix &m){
// rows = m.rows;
// cols = m.cols;
// size = m.size;
// data.resize(size);
// copy(m.data.begin(), m.data.end(), data.begin());
// }

void randomize(){
for (int i=0; i
void apply(Func f){
//#pragma omp parallel for
for (int i=0; i weights;

Net(vector sizes){
for (int i=0; i X, vector T){
float total=0;
for (int i=0; i X, vector T){
float total=0;
for (int i=0; i read_data(int images=1000){
ifstream in("mnist_train.csv");
vector data;
for (int image=0; image> datum.label;
for(int i=0; i> datum.data[i];
data.push_back(datum);
}
return data;
}

int main(){
vector images = read_data(10);
vector labels;
for (Matrix image: images){
labels.push_back(image.one_to_many());
assert(labels[labels.size()-1].abs_sum()==1);
// image.square_print();
//labels[labels.size()-1].print();
}
printf("data read\n");
Net brain({28*28, 3, 10});
printf("brain created!\n");
//
int epochs=3;
debug(brain.difference_squared(images, labels

Solution

You are using a naive matrix product, this is slow. There are faster ways to perform a matrix product. See Wikipedia:Matrix Multiplication.

In general you should not write your own math primitives. I would recommend that you use for example Eigen or Armadillo or any of the umpteen linear algebra libraries.

Context

StackExchange Code Review Q#133432, answer score: 4

Revisions (0)

No revisions yet.