forked from Sentdex/NNfSiX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p002-Basic-Neuron-Layer.cu
57 lines (46 loc) · 1.66 KB
/
p002-Basic-Neuron-Layer.cu
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
#include <iostream>
#include <math.h>
#define NUM_INPUTS 4
#define NUM_OUTPUTS 3
int main() {
float *inputs, *weights1, *bias1, *weights2, *bias2, *weights3, *bias3, *output;
cudaMallocManaged(&inputs, NUM_INPUTS*sizeof(float));
cudaMallocManaged(&weights1, NUM_INPUTS*sizeof(float));
cudaMallocManaged(&bias1, sizeof(float));
cudaMallocManaged(&weights2, NUM_INPUTS*sizeof(float));
cudaMallocManaged(&bias2, sizeof(float));
cudaMallocManaged(&weights3, NUM_INPUTS*sizeof(float));
cudaMallocManaged(&bias3, sizeof(float));
cudaMallocManaged(&output, NUM_OUTPUTS*sizeof(float));
inputs[0] = 1.0f;
inputs[1] = 2.0f;
inputs[2] = 3.0f;
inputs[3] = 2.5f;
weights1[0] = 0.2f;
weights1[1] = 0.8f;
weights1[2] = -0.5f;
weights1[3] = 1.0f;
weights2[0] = 0.5f;
weights2[1] = -0.91f;
weights2[2] = 0.26f;
weights2[3] = -0.5f;
weights3[0] = -0.26f;
weights3[1] = -0.27f;
weights3[2] = 0.17f;
weights3[3] = 0.87f;
bias1[0] = 2.0f;
bias2[0] = 3.0f;
bias3[0] = 0.5f;
output[0] = inputs[0]*weights1[0] + inputs[1]*weights1[1] + inputs[2]*weights1[2] + inputs[3]*weights1[3] + bias1[0];
output[1] = inputs[0]*weights2[0] + inputs[1]*weights2[1] + inputs[2]*weights2[2] + inputs[3]*weights2[3] + bias2[0];
output[2] = inputs[0]*weights3[0] + inputs[1]*weights3[1] + inputs[2]*weights3[2] + inputs[3]*weights3[3] + bias3[0];
std::cout << "[" << output[0] << ", " << output[1] << ", " << output[2] << "]" << std::endl;
cudaFree(inputs);
cudaFree(weights1);
cudaFree(bias1);
cudaFree(weights2);
cudaFree(bias2);
cudaFree(weights3);
cudaFree(bias3);
cudaFree(output);
}