-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensor.cpp
73 lines (61 loc) · 1.3 KB
/
tensor.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
#include <iostream>
#include <random>
#include <cmath>
#include "tensor.hpp"
void init_random(Tensor &a) {
float sigma = sqrt(2. / a.h);
float mean = 0.0;
int32_t datanum = a.d;
int32_t channel = a.c;
int32_t height = a.h;
int32_t width = a.w;
//random_device seed_gen;
//default_random_engine engine(seed_gen());
mt19937 engine(2);
normal_distribution<> dist(mean, sigma);
int32_t idx = 0;
for (int32_t d=0; d < datanum; d++) {
for (int32_t c=0; c < channel; c++) {
for (int32_t h=0; h < height; h++) {
for (int32_t w=0; w < width; w++) {
a[idx] = dist(engine);
idx++;
}
}
}
}
}
void init_zero(Tensor &a) {
int32_t datanum = a.d;
int32_t channel = a.c;
int32_t height = a.h;
int32_t width = a.w;
int32_t idx = 0;
for (int32_t d=0; d < datanum; d++) {
for (int32_t c=0; c < channel; c++) {
for (int32_t h=0; h < height; h++) {
for (int32_t w=0; w < width; w++) {
a[idx] = 0.;
idx++;
}
}
}
}
}
void init_zeroint(IntTensor &a) {
int32_t datanum = a.d;
int32_t channel = a.c;
int32_t height = a.h;
int32_t width = a.w;
int32_t idx = 0;
for (int32_t d=0; d < datanum; d++) {
for (int32_t c=0; c < channel; c++) {
for (int32_t h=0; h < height; h++) {
for (int32_t w=0; w < width; w++) {
a[idx] = 0;
idx++;
}
}
}
}
}