forked from joshuaeckroth/cse3521-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron-template.cpp
84 lines (60 loc) · 1.93 KB
/
perceptron-template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
if(argc != 2) {
cout << "Error, no input file specified." << endl;
return -1;
}
srand(time(NULL));
ifstream data(argv[1]);
if(!data.is_open()) {
cout << "Error opening data file." << endl;
return -1;
}
double alpha = 0.0000001;
int example_count;
int input_count, output_count;
data >> example_count >> input_count >> output_count;
// take first 90% of the examples
int train_count = (int)(0.9*example_count);
double** example_inputs = new double*[example_count];
bool** example_outputs = new bool*[example_count];
for(int e = 0; e < example_count; e++) {
example_inputs[e] = new double[input_count+1];
example_outputs[e] = new bool[output_count];
for(int i = 0; i < input_count; i++) {
data >> example_inputs[e][i];
}
// set fixed bias input
example_inputs[e][input_count] = 1.0;
for(int i = 0; i < output_count; i++) {
data >> example_outputs[e][i];
}
}
bool *pred_outputs = new bool[output_count];
// network is a matrix of weights; rows represent input values + 1
// bias term, columns represent output nodes; so reading down the
// matrix, for some column, gives the weights for the perceptron
// associated with the column's output node
............
// optional: print network (as a matrix)
for(int i = 0; i < (input_count + 1); i++) {
for(int j = 0; j < output_count; j++) {
cout << network[i][j] << "\t";
}
cout << endl;
}
cout << endl << endl;
// try some predictions
for(int e = train_count; e < example_count; e++) {
// compute prediction
............
// compute loss
............
}
// report total loss
return 0;
}