Skip to content

Commit

Permalink
Removed some cruft; iterates until absolute error is <= 0.05
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaeckroth committed Feb 11, 2010
1 parent fbdf692 commit 6c3bf2b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
13 changes: 0 additions & 13 deletions ffnetwork.cpp
Expand Up @@ -5,10 +5,6 @@
#include <cmath>
using namespace std;

#include <QDebug>
#include <QFile>
#include <QTextStream>

#include "ffnetwork.h"

/**
Expand Down Expand Up @@ -46,9 +42,6 @@ FFNetwork::FFNetwork(QObject *parent, vector<unsigned int> _layers) :

delta[i-1] = new double[layers[i]];
}
file = new QFile("weights.csv");
file->open(QIODevice::WriteOnly | QIODevice::Text);
row = 0;
}

void FFNetwork::fillRandomWeights()
Expand All @@ -75,10 +68,7 @@ vector<double> FFNetwork::processInput(vector<double> input)
neuronVals[0][i] = input[i];
}

QTextStream out(file);

double sum;
//out << row++ << ",";
for(unsigned int i = 1; i < layers.size(); i++)
{
// for each neuron in layer
Expand All @@ -89,19 +79,16 @@ vector<double> FFNetwork::processInput(vector<double> input)
// find sum with weights (ignore bias when counting with w)
for(unsigned int w = 0; w < layers[i-1]; w++)
{
//out << weights[i-1][j*(layers[i-1]+1)+w] << ",";
sum += neuronVals[i-1][w] * weights[i-1][j*(layers[i-1]+1)+w];
}

// add bias
sum += weights[i-1][j*(layers[i-1]+1) + layers[i-1]];
//out << weights[i-1][j*(layers[i-1]+1) + layers[i-1]] << ",";

// set neuron's final value
neuronVals[i][j] = sigmoid(sum);
}
}
//out << "\n";

// determine final output
vector<double> output(layers[layers.size()-1]);
Expand Down
4 changes: 0 additions & 4 deletions ffnetwork.h
Expand Up @@ -4,7 +4,6 @@
#include <vector>

#include <QObject>
#include <QFile>

class FFNetwork : public QObject
{
Expand All @@ -24,11 +23,8 @@ public slots:
double **weights;
double **neuronVals;
double **delta;
QFile *file;
int row;

double sigmoid(double x);
int activationFunc(double x);
};

#endif // FFNETWORK_H
20 changes: 12 additions & 8 deletions main.cpp
@@ -1,6 +1,7 @@
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>

#include <QDebug>
#include <QtGui/QApplication>
Expand Down Expand Up @@ -44,15 +45,19 @@ int main(int argc, char *argv[])
}

std::vector<double> output;
int total = 0;
int correct = 0;

unsigned int *ordering = new unsigned int[inputs.size()];
unsigned int ordered = 0;
unsigned int ordered;
unsigned int index;
unsigned int epoch = 0;
double error;
bool seen;
qsrand(std::time(NULL));
for(unsigned int epoch = 0; epoch < 100000; epoch++)
while(true)
{
epoch++;
error = 0.0;
ordered = 0;
while(ordered < inputs.size())
{
index = qrand() % inputs.size();
Expand All @@ -71,16 +76,15 @@ int main(int argc, char *argv[])
{
ordering[ordered++] = index;
output = network.processInput(inputs[index]);
if(activationFunc(output[0]) == (int)(expected[index][0])) correct++;
error += std::fabs(output[0] - expected[index][0]);
network.backprop(output, expected[index]);
total++;
}
}
ordered = 0;
if(error/double(inputs.size()) <= 0.05) break;
}
delete[] ordering;

qDebug() << QString("%1/%2 = %3").arg(correct).arg(total).arg(double(correct)/double(total), 5, 'f', 3);
qDebug() << QString("# of epochs: %1").arg(epoch);

//QApplication app(argc, argv);
//MainWindow w;
Expand Down

0 comments on commit 6c3bf2b

Please sign in to comment.