-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor.cpp
950 lines (827 loc) · 23.6 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#include <iostream>
#include <string>
#include <random>
#include <math.h>
#include <fstream>
#include "dais_exc.h"
#include "tensor.h"
#define PI 3.141592654
#define FLT_MAX 3.402823466e+38F /* max value */
#define FLT_MIN 1.175494351e-38F /* min positive value */
using namespace std;
/**
* struct implementation usato per nascondere l'implementazione dei tensori
*
*/
struct Tensor::Impl {
float* data;
float** cols_p;
float*** matrix_p;
};
/**
* Class constructor
*
* Parameter-less class constructor
*/
Tensor::Tensor() {
pimpl = nullptr;
this->col = 0;
this->row = 0;
this->dep = 0;
}
/**
* metodo privato usato per allocare il tensore così da evitare ridondanza del codice
* l'allocazione prevede che la matrice sia flattened così da permettere una maggiore località spaziale
* e in'oltre l'accesso avviene tramite due altri array di pointer che puntano alla matrice permettendo di accedervi tramite
* parentesi quadre e senza dover ogni volta utilizzare la formula di accesso a matrice flattened
*
* @param row
* @param col
* @param dep
*/
void Tensor::allocate_matrix(int row, int col, int dep) {
if (row == 0 || col == 0 || dep == 0)
throw(unknown_exception());
//alloco l'array contenente i dati del tensore
pimpl->data = new float[row * col * dep];
//alloco i due array utilizzati per accedere al tensore
pimpl->cols_p = new float*[row * col];
pimpl->matrix_p = new float**[row];
//sistemo gli array per far si che si possa dereferenziare matrix_p con le parentesi quadre
for (int i = 0; i < row; i++) {
pimpl->matrix_p[i] = &(pimpl->cols_p[i * col]);
for (int j = 0; j < col; j++)
pimpl->matrix_p[i][j] = pimpl->data + i * col * dep + j * dep;
}
}
/**
* Copy constructor
*
* This constructor copies the data from another Tensor
*
* @return the new Tensor
*/
Tensor::Tensor(const Tensor& that) {
pimpl = new Impl;
row = that.row;
col = that.col;
dep = that.dep;
allocate_matrix(row, col, dep);
for (int i = 0; i < row * col * dep; i++) {
pimpl->data[i] = that.pimpl->data[i];
}
}
/**
* Class constructor
*
* Creates a new tensor of size r*c*d initialized at value v
*
* @param r
* @param c
* @param d
* @param v
* @return new Tensor
*/
Tensor::Tensor(int r, int c, int d, float v) {
init(r, c, d, v);
}
/**
* Class distructor
*
* Cleanup the data when deallocated
*/
Tensor::~Tensor() {
if (pimpl) {
delete[] pimpl->data;
delete[] pimpl->cols_p;
delete[] pimpl->matrix_p;
delete pimpl;
pimpl = nullptr;
}
}
/**
* Constant Initialization
*
* Perform the initialization of the tensor to a value v
*
* @param r The number of rows
* @param c The number of columns
* @param d The depth
* @param v The initialization value
*/
void Tensor::init(int r, int c, int d, float v) {
if (r < 0 || c < 0 || d < 0)
throw(unknown_exception());
pimpl = new Impl;
allocate_matrix(r, c, d);
row = r;
col = c;
dep = d;
for (int i = 0; i < row * col * dep; i++) {
pimpl->data[i] = v;
}
}
/**
* Get minimum
*
* Compute the minimum value considering a particular index in the third dimension
*
* @return the minimum of data( , , k)
*/
float Tensor::getMin(int k) const {
if (!pimpl)
throw(tensor_not_initialized());
float min = FLT_MAX;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (pimpl->matrix_p[i][j][k] < min) min = pimpl->matrix_p[i][j][k];
}
}
return min;
}
/**
* Get maximum
*
* Compute the maximum value considering a particular index in the third dimension
*
* @return the maximum of data( , , k)
*/
float Tensor::getMax(int k) const {
if (!pimpl)
throw(tensor_not_initialized());
float max = FLT_MIN;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (pimpl->matrix_p[i][j][k] > max) max = pimpl->matrix_p[i][j][k];
}
}
return max;
}
/**
* Operator overloading ==
*
* It performs the point-wise equality check between two Tensors.
*
* The equality check between floating points cannot be simply performed using the
* operator == but it should take care on their approximation.
*
* This approximation is known as rounding (do you remember "Architettura degli Elaboratori"?)
*
* For example, given a=0.1232 and b=0.1233 they are
* - the same, if we consider a rounding with 1, 2 and 3 decimals
* - different when considering 4 decimal points. In this case b>a
*
* So, given two floating point numbers "a" and "b", how can we check their equivalence?
* through this formula:
*
* a ?= b if and only if |a-b|<EPSILON
*
* where EPSILON is fixed constant (defined at the beginning of this header file)
*
* Two tensors A and B are the same if:
* A[i][j][k] == B[i][j][k] for all i,j,k
* where == is the above formula.
*
* The two tensors must have the same size otherwise throw a dimension_mismatch()
*
* @return returns true if all their entries are "floating" equal
*/
bool Tensor::operator==(const Tensor& rhs) const {
if (!pimpl)
throw(tensor_not_initialized());
if (row != rhs.row || col != rhs.col || dep != rhs.dep)
throw(dimension_mismatch());
bool equals = true;
for (size_t i = 0; i < (size_t)row * col * dep && equals; i++) {
if (fabs(pimpl->data[i] - rhs.pimpl->data[i]) >= EPSILON) {
equals = false;
}
}
return equals;
}
/**
* Operator overloading ()
*
* if indexes are out of bound throw index_out_of_bound() exception
*
* @return the value at location [i][j][k]
*/
float Tensor::operator()(int i, int j, int k) const {
if (!pimpl)
throw(tensor_not_initialized());
if (i < 0 || i >= row || j < 0 || j >= col || k < 0 || k >= dep)
throw(index_out_of_bound());
return pimpl->matrix_p[i][j][k];
}
/**
* Operator overloading ()
*
* Return the pointer to the location [i][j][k] such that the operator (i,j,k) can be used to
* modify tensor data.
*
* If indexes are out of bound throw index_out_of_bound() exception
*
* @return the pointer to the location [i][j][k]
*/
float& Tensor::operator()(int i, int j, int k) {
if (!pimpl)
throw(tensor_not_initialized());
if (i < 0 || i >= row || j < 0 || j >= col || k < 0 || k >= dep)
throw(index_out_of_bound());
return pimpl->matrix_p[i][j][k];
}
/**
* Operator overloading <<
*
* Use the overaloading of << to show the content of the tensor.
*
* You are free to chose the output format, btw we suggest you to show the tensor by layer.
*
* [..., ..., 0]
* [..., ..., 1]
* ...
* [..., ..., k]
*/
ostream& operator<<(ostream& stream, const Tensor& obj) {
if (!obj.pimpl)
throw(tensor_not_initialized());
for (int i = 0; i < obj.row; i++) {
for (int j = 0; j < obj.col; j++) {
stream << "[";
for (int k = 0; k < obj.dep - 1; k++) {
stream << obj.pimpl->matrix_p[i][j][k] << ",";
}
stream << obj.pimpl->matrix_p[i][j][obj.dep - 1] << "] ";
}
stream << "\n";
}
stream << "\n";
return stream;
}
/**
* Tensor Clamp
*
* Clamp the tensor such that the lower value becomes low and the higher one become high.
*
* @param low Lower value
* @param high Higher value
*/
void Tensor::clamp(float low, float high) {
if (!pimpl)
throw(tensor_not_initialized());
if (low > high)
throw(unknown_exception());
for (int i = 0; i < row * col * dep; i++) {
if (pimpl->data[i] < low)
pimpl->data[i] = low;
else if (pimpl->data[i] > high)
pimpl->data[i] = high;
}
}
/**
* Tensor Rescaling
*
* Rescale the value of the tensor following this rule:
*
* newvalue(i,j,k) = ((data(i,j,k)-min(k))/(max(k)-min(k)))*new_max
*
* where max(k) and min(k) are the maximum and minimum value in the k-th channel.
*
* new_max is the new value for the maximum
*
* @param new_max New maximum vale
*/
void Tensor::rescale(float new_max) {
if (!pimpl)
throw(tensor_not_initialized());
for (int k = 0; k < dep; k++) {
float min = getMin(k);
float max = getMax(k);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
pimpl->matrix_p[i][j][k] = ((pimpl->matrix_p[i][j][k] - min) / (max - min)) * new_max;
}
}
}
}
/**
* Tensor padding
*
* Zero pad a tensor in height and width, the new tensor will have the following dimensions:
*
* (rows+2*pad_h) x (cols+2*pad_w) x (depth)
*
* @param pad_h the height padding
* @param pad_w the width padding
* @return the padded tensor
*/
Tensor Tensor::padding(int pad_h, int pad_w) const {
if (!pimpl)
throw(tensor_not_initialized());
Tensor new_t{row + pad_h * 2, col + pad_w * 2, dep};
for (int k = 0; k < new_t.dep; k++) {
for (int i = 0; i < new_t.row; i++) {
for (int j = 0; j < new_t.col; j++) {
if (i >= pad_h && i < new_t.row - pad_h && j >= pad_w && j < new_t.col - pad_w)
new_t(i, j, k) = pimpl->matrix_p[i - pad_h][j - pad_w][k];
else
new_t(i, j, k) = 0;
}
}
}
return new_t;
}
/**
* Subset a tensor
*
* retuns a part of the tensor having the following indices:
* row_start <= i < row_end
* col_start <= j < col_end
* depth_start <= k < depth_end
*
* The right extrema is NOT included
*
* @param row_start
* @param row_end
* @param col_start
* @param col_end
* @param depth_start
* @param depth_end
* @return the subset of the original tensor
*/
Tensor Tensor::subset(unsigned int row_start, unsigned int row_end, unsigned int col_start, unsigned int col_end, unsigned int depth_start, unsigned int depth_end) const {
if (!pimpl)
throw(tensor_not_initialized());
if (row_start < 0 || (int)row_start > row + 1 || row_end < 0 || (int)row_end > row + 1 ||
col_start < 0 || (int)col_start > col + 1 || col_end < 0 || (int)col_end > col + 1 ||
depth_start < 0 || (int)depth_start > dep + 1 || depth_end < 0 || (int)depth_end > dep + 1)
throw(index_out_of_bound());
if (row_end <= row_start || col_end <= col_start || depth_end <= depth_start)
throw(unknown_exception());
Tensor new_t{(int)(row_end - row_start), (int)(col_end - col_start), (int)(depth_end - depth_start)};
for (unsigned int k = depth_start; k < depth_end; k++) {
for (unsigned int i = row_start; i < row_end; i++) {
for (unsigned int j = col_start; j < col_end; j++) {
new_t(i - row_start, j - col_start, k - depth_start) = pimpl->matrix_p[i][j][k];
}
}
}
return new_t;
}
/**
* Concatenate
*
* The function concatenates two tensors along a give axis
*
* Example: this is of size 10x5x6 and rhs is of 25x5x6
*
* if concat on axis 0 (row) the result will be a new Tensor of size 35x5x6
*
* if concat on axis 1 (columns) the operation will fail because the number
* of rows are different (10 and 25).
*
* In order to perform the concatenation is mandatory that all the dimensions
* different from the axis should be equal, other wise throw concat_wrong_dimension().
*
* @param rhs The tensor to concatenate with
* @param axis The axis along which perform the concatenation
* @return a new Tensor containing the result of the concatenation
*/
Tensor Tensor::concat(const Tensor& rhs, int axis) const {
if (!pimpl || !rhs.pimpl)
throw(tensor_not_initialized());
Tensor new_t;
switch (axis) {
case 0:
if (col != rhs.col || dep != rhs.dep)
throw(concat_wrong_dimension());
new_t.init(row + rhs.row, col, dep);
for (int k = 0; k < dep; k++) {
for (int i = 0; i < row + rhs.row; i++) {
for (int j = 0; j < col; j++) {
if (i < row)
new_t(i, j, k) = pimpl->matrix_p[i][j][k];
else
new_t(i, j, k) = rhs.pimpl->matrix_p[i - row][j][k];
}
}
}
break;
case 1:
if (row != rhs.row || dep != rhs.dep)
throw(concat_wrong_dimension());
new_t.init(row, col + rhs.col, dep);
for (int k = 0; k < dep; k++) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col + rhs.col; j++) {
if (j < col)
new_t(i, j, k) = pimpl->matrix_p[i][j][k];
else
new_t(i, j, k) = rhs.pimpl->matrix_p[i][j - col][k];
}
}
}
break;
case 2:
if (col != rhs.col || row != rhs.row)
throw(concat_wrong_dimension());
new_t.init(row, col, dep + rhs.dep);
for (int k = 0; k < dep + rhs.dep; k++) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (k < dep)
new_t(i, j, k) = pimpl->matrix_p[i][j][k];
else
new_t(i, j, k) = rhs.pimpl->matrix_p[i][j][k - dep];
}
}
}
break;
default:
throw(unknown_exception());
}
return new_t;
}
/**
* Convolution
*
* This function performs the convolution of the Tensor with a filter.
*
* The filter f must have odd dimensions and same depth.
*
* Remeber to apply the padding before running the convolution
*
* @param f The filter
* @return a new Tensor containing the result of the convolution
*/
Tensor Tensor::convolve(const Tensor& f) const {
if (!pimpl || !f.pimpl)
throw(tensor_not_initialized());
if (f.col % 2 == 0 || f.row % 2 == 0) throw(filter_odd_dimensions());
if (dep != f.dep) throw dimension_mismatch();
int pad_h = (f.row - 1) / 2;
int pad_w = (f.col - 1) / 2;
Tensor conv = {row, col, dep};
Tensor padded = padding(pad_h, pad_w);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k < dep; k++) {
float result = 0;
for (int i_filter = 0; i_filter < f.row; i_filter++) {
for (int j_filter = 0; j_filter < f.col; j_filter++) {
result += padded(i + i_filter, j + j_filter, k) * f(i_filter, j_filter, k);
}
}
conv(i, j, k) = result;
}
}
}
return conv;
}
/**
* Operator overloading = (assignment)
*
* Perform the assignment between this object and another
*
* @return a reference to the receiver object
*/
Tensor& Tensor::operator=(const Tensor& other) {
if (this != &other) {
if (pimpl) {
delete[] pimpl->data;
delete[] pimpl->cols_p;
delete[] pimpl->matrix_p;
delete pimpl;
pimpl = nullptr;
}
if (!other.pimpl) {
pimpl = nullptr;
col = 0;
row = 0;
dep = 0;
} else {
init(other.row, other.col, other.dep);
for (int i = 0; i < row * col * dep; i++) {
pimpl->data[i] = other.pimpl->data[i];
}
}
}
return (*this);
}
/**
* Operator overloading -
*
* It performs the point-wise difference between two Tensors.
*
* result(i,j,k)=this(i,j,k)-rhs(i,j,k)
*
* The two tensors must have the same size otherwise throw a dimension_mismatch()
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator-(const Tensor& rhs) const {
if (!pimpl || !rhs.pimpl)
throw(tensor_not_initialized());
if (row != rhs.row || col != rhs.col || dep != rhs.dep) {
throw(dimension_mismatch());
}
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] = pimpl->data[i] - rhs.pimpl->data[i];
}
return newTensor;
}
/**
* Operator overloading +
*
* It performs the point-wise sum between two Tensors.
*
* result(i,j,k)=this(i,j,k)+rhs(i,j,k)
*
* The two tensors must have the same size otherwise throw a dimension_mismatch()
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator+(const Tensor& rhs) const {
if (!pimpl || !rhs.pimpl)
throw(tensor_not_initialized());
if (row != rhs.row || col != rhs.col || dep != rhs.dep) {
throw(dimension_mismatch());
}
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] = pimpl->data[i] + rhs.pimpl->data[i];
}
return newTensor;
}
/**
* Operator overloading *
*
* It performs the point-wise product between two Tensors.
*
* result(i,j,k)=this(i,j,k)*rhs(i,j,k)
*
* The two tensors must have the same size otherwise throw a dimension_mismatch()
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator*(const Tensor& rhs) const {
if (!pimpl || !rhs.pimpl)
throw(tensor_not_initialized());
if (row != rhs.row || col != rhs.col || dep != rhs.dep) {
throw(dimension_mismatch());
}
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] = pimpl->data[i] * rhs.pimpl->data[i];
}
return newTensor;
}
/**
* Operator overloading /
*
* It performs the point-wise division between two Tensors.
*
* result(i,j,k)=this(i,j,k)/rhs(i,j,k)
*
* The two tensors must have the same size otherwise throw a dimension_mismatch()
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator/(const Tensor& rhs) const {
if (!pimpl || !rhs.pimpl)
throw(tensor_not_initialized());
if (row != rhs.row || col != rhs.col || dep != rhs.dep) {
throw(dimension_mismatch());
}
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] = pimpl->data[i] / rhs.pimpl->data[i];
}
return newTensor;
}
/**
* Operator overloading +
*
* It performs the point-wise sum between a Tensor and a constant
*
* result(i,j,k)=this(i,j,k)+rhs
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator+(const float& rhs) const {
if (!pimpl)
throw(tensor_not_initialized());
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] += rhs;
}
return newTensor;
}
/**
* Operator overloading -
*
* It performs the point-wise difference between a Tensor and a constant
*
* result(i,j,k)=this(i,j,k)-rhs
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator-(const float& rhs) const {
if (!pimpl)
throw(tensor_not_initialized());
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] -= rhs;
}
return newTensor;
}
/**
* Operator overloading *
*
* It performs the point-wise product between a Tensor and a constant
*
* result(i,j,k)=this(i,j,k)*rhs
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator*(const float& rhs) const {
if (!pimpl)
throw(tensor_not_initialized());
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] *= rhs;
}
return newTensor;
}
/**
* Operator overloading / between a Tensor and a constant
*
* It performs the point-wise division between a Tensor and a constant
*
* result(i,j,k)=this(i,j,k)/rhs
*
* @return returns a new Tensor containing the result of the operation
*/
Tensor Tensor::operator/(const float& rhs) const {
if (!pimpl)
throw(tensor_not_initialized());
Tensor newTensor{*this};
for (int i = 0; i < row * col * dep; i++) {
newTensor.pimpl->data[i] /= rhs;
}
return newTensor;
}
/**
* Rows
*
* @return the number of rows in the tensor
*/
int Tensor::rows() const {
return row;
}
/**
* Cols
*
* @return the number of columns in the tensor
*/
int Tensor::cols() const {
return col;
}
/**
* Depth
*
* @return the depth of the tensor
*/
int Tensor::depth() const {
return dep;
}
/**
* Random Initialization
*
* Perform a random initialization of the tensor
*
* @param mean The mean
* @param std Standard deviation
*/
void Tensor::init_random(float mean, float std) {
if (pimpl) {
std::default_random_engine generator;
std::normal_distribution<float> distribution(mean, std);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k < dep; k++) {
this->operator()(i, j, k) = distribution(generator);
}
}
}
} else {
throw(tensor_not_initialized());
}
}
/**
* showSize
*
* shows the dimensions of the tensor on the standard output.
*
* The format is the following:
* rows" x "colums" x "depth
*
*/
void Tensor::showSize() const {
cout << this->rows() << " " << this->cols() << " " << this->depth() << endl;
}
/**
* Reading from file
*
* Load the content of a tensor from a textual file.
*
* The file should have this structure: the first three lines provide the dimensions while
* the following lines contains the actual data by channel.
*
* For example, a tensor of size 4x3x2 will have the following structure:
* 4
* 3
* 2
* data(0,0,0)
* data(0,1,0)
* data(0,2,0)
* data(1,0,0)
* data(1,1,0)
* .
* .
* .
* data(3,1,1)
* data(3,2,1)
*
* if the file is not reachable throw unable_to_read_file()
*
* @param filename the filename where the tensor is stored
*/
void Tensor::read_file(string filename) {
ifstream ifs{filename};
if (!ifs) throw(unable_to_read_file());
ifs >> row;
ifs >> col;
ifs >> dep;
init(row, col, dep);
for (int k = 0; k < dep; k++) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
ifs >> pimpl->matrix_p[i][j][k];
}
}
}
}
/**
* Write the tensor to a file
*
* Write the content of a tensor to a textual file.
*
* The file should have this structure: the first three lines provide the dimensions while
* the following lines contains the actual data by channel.
*
* For example, a tensor of size 4x3x2 will have the following structure:
* 4
* 3
* 2
* data(0,0,0)
* data(0,1,0)
* data(0,2,0)
* data(1,0,0)
* data(1,1,0)
* .
* .
* .
* data(3,1,1)
* data(3,2,1)
*
*
* @param filename the filename where the tensor is stored
*/
void Tensor::write_file(string filename) const {
ofstream ost{filename};
ost << row << "\n"
<< col << "\n"
<< dep << "\n";
for (int i = 0; i < dep; i++)
for (int j = 0; j < row; j++)
for (int k = 0; k < col; k++)
ost << pimpl->matrix_p[j][k][i] << "\n";
}
/**
* funzione usata per inizializzare un tensore filtro data una matrice di float
* usato in daisgram per i filtri per la convolve
*
* @param f
* @param row
* @param col
*/
void Tensor::init_filter(float* f, int row, int col) {
init(row, col, 1);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
pimpl->matrix_p[i][j][0] = f[i * col + j];
}
}
}