-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivations.cpp
190 lines (157 loc) · 4.03 KB
/
activations.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <math.h>
#include <cstring>
#include "Tensor.h"
#include "activations.h"
float* relu(float input[], int size, bool inplace) {
float* result;
if(!inplace){
result = new float[size];
}
for (int i = 0; i < size; ++i) {
if (inplace) {
input[i] = std::max(input[i], 0.0f);
} else {
result[i] = std::max(input[i], 0.0f);
}
}
if (inplace) {
return input;
} else {
return result;
}
}
float* relu(float *input, int rows, int cols, bool inplace) {
float* result;
int size = rows*cols;
if(inplace){
result = input;
}
else{
result = new float[size];
}
for (int i = 0; i < size; ++i) {
result[i] = std::max(input[i], 0.0f);
}
return result;
}
Tensor relu(Tensor &mat, bool inplace) {
Tensor *result;
if(inplace){
result = &mat;
}
else{
result = new Tensor(mat.rows, mat.cols);
std::memcpy(result->data, mat.data, sizeof(float) * mat.size);
}
for (int i = 0; i < result->size; ++i) {
result->data[i] = std::max(result->data[i], 0.0f);
}
return *result;
}
void _softmax(float *input, int start_index, int end_index){
float max_val=input[start_index], sum_val=0;
#pragma omp parallel for reduction(max:max_val)
for(int i=start_index+1; i<end_index; i++){
max_val = std::max(max_val, input[i]);
}
#pragma omp parallel for reduction(+:sum_val)
for(int i=start_index; i<end_index; i++){
input[i] = exp(input[i] - max_val);
sum_val += input[i];
}
#pragma simd
for(int i=start_index; i<end_index; i++){
input[i] = input[i]/sum_val;
}
}
float* softmax(float *input, int rows, int cols, bool inplace){
float sum_val;
float *result;
if (inplace){
result = input;
}
else{
result = new float[rows*cols];
std::memcpy(result, input, rows*cols*sizeof *input);
}
int i, j;
for(i=0; i<rows; i++){
_softmax(result, i*cols, (i+1)*cols);
}
return result;
}
Tensor* softmax(Tensor &mat, bool inplace){
Tensor *result;
if (inplace){
result = &mat;
}
else{
result = new Tensor(mat.rows, mat.cols);
std::memcpy(result->data, mat.data, sizeof(float) * mat.size);
}
int i, j;
for(i=0; i<result->rows; i++){
_softmax(result->data, i*result->cols, (i+1)*result->cols);
}
return result;
}
Tensor* softmax(Tensor *mat, bool inplace){
Tensor *result;
if (inplace){
result = mat;
}
else{
result = new Tensor(mat->rows, mat->cols);
std::memcpy(result->data, mat->data, sizeof(float) * mat->size);
}
int i, j;
#pragma omp for
for(i=0; i<result->rows; i++){
_softmax(result->data, i*result->cols, (i+1)*result->cols);
}
return result;
}
float* scale(float *input, int rows, int cols, bool inplace){
float *result;
if (!inplace){
result = new float[rows*cols];
}
else{
result = input;
}
float scale = sqrt(cols);
for(int i=0; i<rows*cols; i++){
result[i] = result[i]/scale;
}
return result;
}
Tensor* scale(Tensor &mat, bool inplace) {
Tensor *result;
if (inplace) {
result = &mat;
} else {
result = new Tensor(mat.rows, mat.cols);
std::memcpy(result->data, mat.data, sizeof(float) * mat.size);
}
float scale = std::sqrt(result->cols);
for (int i = 0; i < result->size; ++i) {
result->data[i] = result->data[i]/scale;
}
return result;
}
Tensor* scale(Tensor *mat, bool inplace) {
Tensor *result;
if (inplace) {
result = mat;
} else {
result = new Tensor(mat->rows, mat->cols);
std::memcpy(result->data, mat->data, sizeof(float) * mat->size);
}
float scale = std::sqrt(result->cols);
#pragma simd
for (int i = 0; i < result->size; ++i) {
result->data[i] = result->data[i]/scale;
}
return result;
}