-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathutils.c
More file actions
122 lines (100 loc) · 3.04 KB
/
Copy pathutils.c
File metadata and controls
122 lines (100 loc) · 3.04 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <math.h>
#include <float.h>
#include "utils.h"
float min(float a, float b) {
return (a < b) ? a : b;
}
float max(float a, float b) {
return (a > b) ? a : b;
}
float relu(float x) {
if (x < 0.0) return 0.0;
else return x;
}
float sigmoid(float x) {
return 1.0f / (1.0f + expf(-1.0f * x));
}
float tanhyperbolic(float x) {
float ex = expf(x);
float enx = expf(-1.0f * x);
return (ex - enx) / (ex + enx);
}
float quantTanh(float x) {
return max(min(x, 1.0f), -1.0f);
}
float quantSigmoid(float x) {
return max(min((x + 1.0f) / 2.0f, 1.0f), 0.0f);
}
void v_relu(const float* const vec, unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++) ret[i] = relu(vec[i]);
}
void v_sigmoid(const float* const vec, unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++) ret[i] = sigmoid(vec[i]);
}
void v_tanh(const float* const vec, unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++) ret[i] = tanhyperbolic(vec[i]);
}
void v_quantSigmoid(const float* const vec, unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++) ret[i] = sigmoid(vec[i]);
}
void v_quantTanh(const float* const vec, unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++) ret[i] = tanh(vec[i]);
}
void matVec(const float* const mat, const float* const vec,
unsigned nrows, unsigned ncols,
float alpha, float beta,
float* const ret) {
for (unsigned row = 0; row < nrows; row++) {
float sum = 0.0f;
float* mat_offset = (float*)mat + row * ncols;
for (unsigned col = 0; col < ncols; col++) {
sum += *mat_offset++ * vec[col];
}
ret[row] = alpha * ret[row] + beta * sum;
}
}
void v_add(float scalar1, const float* const vec1,
float scalar2, const float* const vec2,
unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++)
ret[i] = scalar1 * vec1[i] + scalar2 * vec2[i];
}
void v_mult(const float* const vec1, const float* const vec2,
unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++)
ret[i] = vec1[i] * vec2[i];
}
void v_div(const float* const vec1, const float* const vec2,
unsigned len, float* const ret) {
for (unsigned i = 0; i < len; i++)
ret[i] = vec2[i] / vec1[i];
}
float l2squared(const float* const vec1,
const float* const vec2, unsigned dim) {
float sum = 0.0f;
for (unsigned i = 0; i < dim; i++)
sum += (vec1[i] - vec2[i]) * (vec1[i] - vec2[i]);
return sum;
}
unsigned argmax(const float* const vec, unsigned len) {
unsigned maxId = 0;
float maxScore = FLT_MIN;
for (unsigned i = 0; i < len; i++) {
if (vec[i] > maxScore) {
maxScore = vec[i];
maxId = i;
}
}
return maxId;
}
void softmax(const float* const input, unsigned len, float* const ret) {
float m = input[argmax(input, len)];
float sum = 0.0f;
for (unsigned i = 0; i < len; i++)
sum += expf(input[i] - m);
float offset = m + logf(sum);
for (unsigned i = 0; i < len; i++)
ret[i] = expf(input[i] - offset);
}