-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cu
More file actions
158 lines (113 loc) · 5.01 KB
/
main.cu
File metadata and controls
158 lines (113 loc) · 5.01 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef RAND_MAX
#define RAND_MAX 32767
#endif
#include "neuralNetwork.h"
double init_weight(){ return ((double) rand())/ ((double)RAND_MAX);}
void shuffle(int* array, size_t n){
if(n > 1){
size_t i;
for(i = 0; i < n - 1; i++){
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
int main(int argc, char** argv){
if(argc < 2){
printf("add some params");
exit(0);
}
//int gridSize = 4;
//int blockSize = 1;
time_t t;
srand((unsigned)time(&t));
// Set the learning rate & epochs
int epochs = 10000;
double lr = 1.0f;
int numInputs = 2;
int numHiddenNodes = 4;
int numOutputs = 1;
double training_inputs[8] = {0.0f,0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f};
double training_outputs[4] = {0.0f, 1.0f, 1.0f, 0.0f};
int trainingSetOrder[] = {0,1,2,3};
int numTrainingSets = 4;
// Initialize all the arrays into memory
double* hiddenLayer = (double*) malloc(numHiddenNodes * sizeof(double));
double* outputLayer = (double*) malloc(numOutputs * sizeof(double));
double* hiddenLayerBias = (double*) malloc(numHiddenNodes * sizeof(double));
double* outputLayerBias = (double*) malloc(numOutputs * sizeof(double));
double* hiddenWeights = (double*)malloc(numInputs * numHiddenNodes* sizeof(double));
double* outputWeights = (double*)malloc(numHiddenNodes * numOutputs * sizeof(double));
//cuda
double* cuHiddenLayer;
double* cuOutputLayer;
double* cuHiddenLayerBias;
double* cuOutputLayerBias;
double* cuOutputWeights;
double* cuHiddenWeights;
double* cuTrainingInputs;
double* cuTrainingOutputs;
int* cuTrainingSetOrder;
cudaMalloc((void**)&cuHiddenLayer, numHiddenNodes * sizeof(double));
cudaMalloc((void**)&cuOutputLayer, numOutputs * sizeof(double));
cudaMalloc((void**)&cuHiddenLayerBias, numHiddenNodes * sizeof(double));
cudaMalloc((void**)&cuOutputLayerBias, numOutputs * sizeof(double));
cudaMalloc((void**)&cuHiddenWeights, numInputs * numHiddenNodes * sizeof(double));
cudaMalloc((void**)&cuTrainingInputs, 8 * sizeof(double));
cudaMalloc((void**)&cuTrainingOutputs, 4 * sizeof(double));
cudaMalloc((void**)&cuTrainingSetOrder, 4 * sizeof(int));
cudaMalloc((void**)&cuOutputWeights, numHiddenNodes * numOutputs * sizeof(double));
// Initialize All The Weights
for(int i = 0; i < numInputs; i++){
for(int j = 0; j < numHiddenNodes; j++){
hiddenWeights[(i * 2) + j] = init_weight();
}
}
for(int i=0;i<numHiddenNodes;i++){
hiddenLayerBias[i] = init_weight();
for(int j=0; j<numOutputs; j++){
outputWeights[(2 * i )+ j] = init_weight();
}
}
for(int i = 0; i<numOutputs; i++){
outputLayerBias[i] = init_weight();
}
cudaMemcpy(cuHiddenLayer, hiddenLayer, numHiddenNodes * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(cuOutputLayer, outputLayer, numOutputs * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(cuHiddenLayerBias, hiddenLayerBias, numHiddenNodes * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(cuOutputLayerBias, outputLayerBias, numOutputs * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(cuHiddenWeights, hiddenWeights, numInputs * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(cuTrainingInputs, training_inputs, 8 * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(cuTrainingOutputs, training_outputs, 4 * sizeof(double), cudaMemcpyHostToDevice);
cudaMemcpy(cuTrainingSetOrder, trainingSetOrder, 4 * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuOutputWeights, outputWeights, numHiddenNodes * sizeof(double), cudaMemcpyHostToDevice);
//------------------------------------------------------------------------------
//do epochs
for(int n = 0; n < epochs; n++){
shuffle(trainingSetOrder, numTrainingSets);
for(int x = 0; x < numTrainingSets; x++){
int i = trainingSetOrder[x];
forwardFeed<<<1, 1>>>(cuTrainingInputs, cuHiddenWeights, cuHiddenLayer, cuOutputLayer, cuOutputWeights, cuOutputLayerBias, cuHiddenLayerBias, numHiddenNodes, numInputs, numOutputs, i);
cudaDeviceSynchronize();
backpropogate<<<1, 1>>>(cuTrainingInputs, cuHiddenLayer, cuHiddenWeights, cuOutputLayer, cuOutputWeights, cuTrainingOutputs, cuHiddenLayerBias, cuOutputLayerBias, numHiddenNodes, numInputs, numOutputs, i, lr);
cudaDeviceSynchronize();
}
}
// Predict Function
// Create two pieces of test input
double test_input[2] ={atof(argv[1]), atof(argv[2])};
double* cuInputs;
cudaMalloc((void**)&cuInputs, 2 * sizeof(double));
cudaMemcpy(cuInputs, test_input, 2 * sizeof(double), cudaMemcpyHostToDevice);
forwardFeed<<<1, 1>>>(cuInputs, cuHiddenWeights, cuHiddenLayer, cuOutputLayer, cuOutputWeights, cuOutputLayerBias, cuHiddenLayerBias, numHiddenNodes, numInputs, numOutputs, 0);
cudaDeviceSynchronize();
// Transfer the memory off of the GPU to the CPU
cudaMemcpy(outputLayer, cuOutputLayer, numOutputs * sizeof(double), cudaMemcpyDeviceToHost);
printf("%f\n", outputLayer[0]);
}