-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.c
More file actions
146 lines (131 loc) · 3.84 KB
/
tensor.c
File metadata and controls
146 lines (131 loc) · 3.84 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "privateTensorOps.c"
//ORGANIZE THIS by tensor gen functions, tensor op functions, and activation functions
int findIndex(tensor* t, int* indices){
int index = 0;
int len = t->ndim;
int count = len - 1;
for(int i = 0;i < len; i++ ){
int base = indices[i];
for(int j = 0; j < count; j++){
base *= t->shape[len - 1 - j];
}
index += base;
count--;
}
return index;
}
coord* findCoords(tensor* t, int index){
//don't leak this
coord* coords = malloc(sizeof(coords));
coords->coords = malloc(sizeof(int) * t->ndim);
coords->ndim = t->ndim;
for(int i = t->ndim - 1; i >= 0; i--){
coords->coords[i] = index % t->shape[i];
index /= t->shape[i];
}
return coords;
}
void deleteCoords(coord* coords){
//please use
free(coords->coords);
free(coords);
}
void showShape(tensor* t){
printf("%i dims: { ", t->ndim);
for(int i = 0; i < t->ndim; i++){
printf("%i", t->shape[i]);
if(i != t->ndim -1) printf(", ");
}
printf(" }\n");
}
void showFlatArray(tensor* t){
printf("{ ");
for(int i = 0;i < t->length; i++){
printf(" %f, ", t->data[i]);
}
printf("}\n");
}
void repr(tensor* t){
int index = 0;
showShape(t);
_reprRecurse(t, 0, &index);
}
tensor* createTensor(int* shape, int ndim){
tensor* t = malloc(sizeof(tensor));
t->ndim = ndim;
t->shape = malloc(sizeof(int)* ndim);
memcpy(t->shape, shape, sizeof(int) * ndim);
int multiplier = 1;
for(int i = 0; i < ndim; i++) multiplier *= shape[i];
t->data = malloc(sizeof(float) * multiplier);
t->length = multiplier;
return t;
}
tensor* pad(tensor* t, int* pads, int padDimCount){
//pads must be even numbers
//add checks for this
int newShape[t->ndim]; //this may effect the compilation on barebones c11+ compilers
memcpy(newShape, t->shape, sizeof(int) * t->ndim);
for(int i = 1; i < padDimCount + 1; i++) newShape[t->ndim - i] = (pads[padDimCount - i]) + t->shape[t->ndim - i];
int multiplier = 1;
for(int i = 0; i < t->ndim; i++) multiplier *= newShape[i];
tensor* nt = createTensor(newShape, t->ndim);
float* newData = calloc(multiplier, sizeof(float));
int i = 0; int j = 0;
while(i < t->length && j < multiplier){
coord* coords = findCoords(nt, j);
int inPad = 0;
//this doesnt work
for(int k = 1; k < padDimCount + 1; k++){
if(coords->coords[nt->ndim - k] < (pads[padDimCount - k]/2) || coords->coords[nt->ndim - k] > nt->shape[nt->ndim - k] - (pads[padDimCount - k]/2)-1){
inPad = 1;
break;
}
}
if(inPad){j++; continue;}
newData[j] = t->data[i];
j++;
i++;
deleteCoords(coords);
}
nt->data = newData;
nt->length = multiplier;
return nt;
}
//TENSOR OPS
void reshape(tensor* t, int* shape, int ndim){
int size = 1;
for(int i = 0; i < ndim; i++) size *= shape[i];
if(size != t->length){ printf("invalid reshape"); return; }
memcpy(t->shape, shape, sizeof(int) * ndim);
t->ndim = ndim;
}
void freeze(tensor* t){ t->grad = 0; }
void unfreeze(tensor* t){ t->grad = 1; }
void randomize(tensor* t){
for(int i = 0; i < t->length; i++){
float scale = rand() / (float)RAND_MAX;
t->data[i] = -1.0f + scale * (1.0f - -1.0f);
}
}
void squeeze(tensor* t){
int c = 0;
int* newShape = malloc(sizeof(int) * t->ndim);
for(int i= 0; i < t->ndim; i++){
if(t->shape[i] > 1){
newShape[c] = t->shape[i];
c++;
}
}
t->ndim = c;
t->shape = realloc(newShape, sizeof(int) * c);
}
/*ACTIVATION FUNCTIONS*/
void relu(tensor* t){
for(int i = 0; i < t->length; i++){
t->data[i] = (t->data[i] < 0)? 0 : t->data[i];
}
}
void sigmoid(tensor* t){
;
}