-
Notifications
You must be signed in to change notification settings - Fork 0
/
NNUtils.cpp
105 lines (86 loc) · 1.94 KB
/
NNUtils.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef NNUTILS_H
#define NNUTILS_H
#include "Vect.h"
#include "Mat.h"
#include <stdlib.h>
#include <time.h>
void initRand() {
time_t t;
srand((unsigned) time(&t));
rand(); rand(); rand();
}
void fillRandom(Mat* mat, float from, float to) {
float diff = to-from;
int maxR = mat->getRows();
int maxC = mat->getCols();
for (int r=0; r<maxR; r++) {
for (int c=0; c<maxC; c++) {
float v = ((float) (rand() % 1001))/1001.0f;
float vRange = from+diff*v;
mat->set(r, c, vRange);
}
}
}
void fillRandom(Vect* vect, float from, float to) {
float diff = to-from;
for (int i=0; i<vect->getLength(); i++) {
float v = ((float) (rand() % 1001))/1001.0f;
float vRange = from+diff*v;
vect->set(i, vRange);
}
}
void fillRandomNorm(Mat* mat, float from, float to) {
float diff = to-from;
int maxR = mat->getRows();
int maxC = mat->getCols();
for (int r=0; r<maxR; r++) {
for (int c=0; c<maxC; c++) {
float v = ((float) ((rand() % 1001)+(rand() % 1001)+(rand() % 1001)))/3003.0;
float vRange = from+diff*v;
mat->set(r, c, vRange);
}
}
}
void fillRandomNorm(Vect* vect, float from, float to) {
float diff = to-from;
for (int i=0; i<vect->getLength(); i++) {
float v = ((float) ((rand() % 1001)+(rand() % 1001)+(rand() % 1001)))/3003.0;
float vRange = from+diff*v;
vect->set(i, vRange);
}
}
void fill(Vect* vect, float v) {
for (int i=0; i<vect->getLength(); i++) {
vect->set(i, v);
}
}
void fill(Mat* mat, float v) {
for (int r=0; r<mat->getRows(); r++) {
for (int c=0; c<mat->getRows(); r++) {
mat->set(r,c, v);
}
}
}
float relu(float f) {
if (f<0.0f) {
return 0.0f;
}
return f;
}
void relu(Vect *vect) {
for (int i=0; i<vect->getLength(); i++) {
vect->set(i, relu(vect->get(i)));
}
}
float relu_prime(float f) {
if (f<0.0f) {
return 0.0f;
}
return 1.0f;
}
void relu_prime(Vect *vect) {
for (int i=0; i<vect->getLength(); i++) {
vect->set(i, relu_prime(vect->get(i)));
}
}
#endif // NNUTILS_H