-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivation.cpp
More file actions
67 lines (63 loc) · 1.66 KB
/
Copy pathactivation.cpp
File metadata and controls
67 lines (63 loc) · 1.66 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
#include "tt_nn.h"
#ifndef SYNTHESIS
#include <iostream>
using namespace std;
#endif
void relu_inplace(
TYPE_DATA* data,
int offset,
int shape
){
#ifndef SYNTHESIS
cout << "relu_inplace(array, ";
cout << offset << ", ";
cout << shape << ");" << endl;
#endif
for (int i = 0; i < shape; i++) {
data[offset + i] = data[offset + i] > TYPE_DATA(0) ? data[offset + i] : TYPE_DATA(0);
}
}
void relu_backward_inplace(
TYPE_DATA* data,
int data_offset,
int grad_offset,
int shape
){
#ifndef SYNTHESIS
cout << "relu_backward_inplace(array, ";
cout << data_offset << ", ";
cout << grad_offset << ", ";
cout << shape << ");" << endl;
#endif
for (int i = 0; i < shape; i++) {
data[grad_offset + i] = data[data_offset + i] > TYPE_DATA(0) ? data[grad_offset + i] : TYPE_DATA(0);
}
}
void softmax_ce_grad(
TYPE_DATA* data,
unsigned char label,
int out_offset,
int grad_offset,
unsigned char num_class
){
#ifndef SYNTHESIS
cout << "softmax_ce_grad(array, ";
cout << "label" << ", ";
cout << out_offset << ", ";
cout << grad_offset << ", ";
cout << "num_class" << ");" << endl;
#endif
TYPE_DATA max_val = data[out_offset];
for (int i = 1; i < num_class; i++) {
max_val = max_val > data[out_offset + i] ? max_val : data[out_offset + i];
}
TYPE_DATA sum = 0;
for (int i = 0; i < num_class; i++) {
data[grad_offset + i] = exp(data[out_offset + i] - max_val);
sum += data[grad_offset + i];
}
for (int i = 0; i < num_class; i++) {
data[grad_offset + i] /= sum;
}
data[grad_offset + label] -= 1;
}