-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRL_Tensor.hpp
829 lines (730 loc) · 31.2 KB
/
RRL_Tensor.hpp
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
#ifndef RRL_RRL_TENSOR_HPP
#define RRL_RRL_TENSOR_HPP
#include "vector"
#include "string"
#include <tensorflow/core/public/session.h>
#include "Eigen/Core"
#include <algorithm>
#include "glog/logging.h"
#include <boost/utility/enable_if.hpp>
#include <Eigen/StdVector>
namespace rrl
{
template <typename Dtype, int NDim>
class TensorBase
{
typedef Eigen::Map<Eigen::Matrix<Dtype, -1, -1>> EigenMat;
typedef Eigen::TensorMap<Eigen::Tensor<Dtype, NDim>> EigenTensor;
public:
// empty constructor. Resize has to be called before use
TensorBase(const std::string name = "")
{
namedTensor_.first = name;
setDataType(); //??
}
// empty data constructor
TensorBase(const std::vector<int> dim, const std::string name = "")
{
init(dim, name);
}
// constant constructor
TensorBase(const std::vector<int> dim, const Dtype constant, const std::string name = "")
{
init(dim, name);
eTensor().setConstant(constant);
}
// copy constructor
TensorBase(const TensorBase<Dtype, NDim> ©)
{
if (copy.size() != -1)
{
init(copy.dim(), copy.getName());
memcpy(data(), copy.data(), size() * sizeof(Dtype));
}
else
{
setName(copy.getName());
setDataType();
}
}
/// Eigen Tensor constructor is abigous with std::vector<int> constructor ...
// // copy constructor from Eigen Tensor
// Tensor(const Eigen::Tensor<Dtype, NDim> &etensor, const std::string name = "") {
// auto dims = etensor.dimensions();
// std::vector<int> dim(dims.size());
// for (int i = 0; i < dims.size(); i++)
// dim[i] = dims[i];
// Tensor(dim, name);
// std::memcpy(data_->flat().data(), etensor.data(), sizeof(Dtype) * etensor.size());
// }
// copy construct from Eigen Matrix
template <int Rows, int Cols>
TensorBase(const Eigen::Matrix<Dtype, Rows, Cols> &emat, const std::string name = "")
{
//LOG_IF(FATAL, NDim != 2) << "Specify the shape";
std::vector<int> dim = {emat.rows(), emat.cols()};
init(dim, name);
std::memcpy(namedTensor_.second.flat<Dtype>().data(), emat.data(), sizeof(Dtype) * emat.size());
}
// this constructor is used when the resulting tensor dim is not 2D
template <int Rows, int Cols>
TensorBase(const Eigen::Matrix<Dtype, Rows, Cols> &emat, std::vector<int> dim, const std::string name = "")
{
init(dim, name);
//LOG_IF(FATAL, emat.size() != size_) << "size mismatch";
std::memcpy(namedTensor_.second.flat<Dtype>().data(), emat.data(), sizeof(Dtype) * emat.size());
}
virtual ~TensorBase(){};
////////////////////////////
/////// casting methods ////
////////////////////////////
operator std::pair<std::string, tensorflow::Tensor>()
{
return namedTensor_;
};
operator tensorflow::Tensor()
{
return namedTensor_.second;
};
template <int Rows, int Cols>
operator Eigen::Matrix<Dtype, Rows, Cols>()
{
//LOG_IF(FATAL, dim_.size() > 2) << "This method works upto 2D Tensor";
// LOG_IF(FATAL, Rows != dim_[0] || Cols != dim_[1]) << "dimension mismatch";
EigenMat mat(namedTensor_.second.flat<Dtype>().data(), dim_[0], dim_[1]);
return mat;
};
operator EigenTensor()
{
EigenTensor mat(namedTensor_.second.flat<Dtype>().data(), esizes_);
return mat;
}
//////////////////////////
/// Eigen Tensor mirror///
//////////////////////////
EigenTensor eTensor()
{
return EigenTensor(namedTensor_.second.flat<Dtype>().data(), esizes_);
}
void setConstant(const Dtype constant)
{
eTensor().setConstant(constant);
}
void setZero()
{
eTensor().setZero();
}
void setRandom()
{
eTensor().setRandom();
}
Dtype *data()
{
return namedTensor_.second.flat<Dtype>().data();
}
const Dtype *data() const
{
return namedTensor_.second.flat<Dtype>().data();
}
////////////////////////////////
/// tensorflow tensor mirror ///
////////////////////////////////
tensorflow::TensorShape tfShape()
{
return namedTensor_.second.shape();
}
const tensorflow::Tensor &tfTensor() const
{
return namedTensor_.second;
}
std::vector<tensorflow::Tensor> &output()
{
return vecTens;
}
///////////////////////////////
////////// operators //////////
///////////////////////////////
TensorBase<Dtype, NDim> &operator=(const TensorBase<Dtype, NDim> &rTensor)
{
/// copy everything except for the name
dim_ = rTensor.dim_;
dim_inv_ = rTensor.dim_inv_;
size_ = rTensor.size_;
esizes_ = rTensor.esizes_;
vecTens = rTensor.vecTens;
std::memcpy(namedTensor_.second.flat<Dtype>().data(), rTensor.namedTensor_.second.template flat<Dtype>().data(), sizeof(Dtype) * size_);
return *this;
}
TensorBase<Dtype, NDim> &operator=(const tensorflow::Tensor &tfTensor)
{
//LOG_IF(FATAL, dim_inv_ != tfTensor.shape()) << "Tensor shape mismatch";
std::memcpy(namedTensor_.second.flat<Dtype>().data(), tfTensor.flat<Dtype>().data(), sizeof(Dtype) * size_);
return *this;
}
TensorBase<Dtype, NDim> &operator=(const std::string name)
{
setName(name);
return *this;
}
TensorBase<Dtype, NDim> &operator=(const Eigen::Tensor<Dtype, NDim> &eTensor)
{
for (int i = 0; i < NDim; i++)
//LOG_IF(FATAL, dim_[i] != eTensor.dimension(i))
// << "Tensor size mismatch: " << i << "th Dim " << dim_[i] << "vs" << eTensor.dimension(i);
std::memcpy(namedTensor_.second.flat<Dtype>().data(), eTensor.data(), sizeof(Dtype) * size_);
return *this;
}
TensorBase<Dtype, NDim> &operator-=(const TensorBase<Dtype, NDim> &other)
{
//LOG_IF(FATAL, size() != other.size()) << "size mismatch";
for (int i = 0; i < size(); i++)
data()[i] -= other.data()[i];
return *this;
}
TensorBase<Dtype, NDim> &operator+=(const TensorBase<Dtype, NDim> &other)
{
//LOG_IF(FATAL, size() != other.size()) << "size mismatch";
for (int i = 0; i < size(); i++)
data()[i] += other.data()[i];
return *this;
}
friend TensorBase<Dtype, NDim> operator-(const TensorBase<Dtype, NDim> &lhs, const TensorBase<Dtype, NDim> &rhs)
{
TensorBase<Dtype, NDim> result(lhs);
result -= rhs;
return result;
}
friend TensorBase<Dtype, NDim> &&operator-(TensorBase<Dtype, NDim> &&lhs, const TensorBase<Dtype, NDim> &rhs)
{
return std::move(lhs -= rhs);
}
friend TensorBase<Dtype, NDim> &&operator-(const TensorBase<Dtype, NDim> &lhs, TensorBase<Dtype, NDim> &&rhs)
{
return std::move(rhs -= lhs);
}
friend TensorBase<Dtype, NDim> &&operator-(TensorBase<Dtype, NDim> &&lhs, TensorBase<Dtype, NDim> &&rhs)
{
return std::move(rhs -= lhs);
}
friend TensorBase<Dtype, NDim> operator+(const TensorBase<Dtype, NDim> &lhs, const TensorBase<Dtype, NDim> &rhs)
{
TensorBase<Dtype, NDim> result(lhs);
result += rhs;
return result;
}
friend TensorBase<Dtype, NDim> &&operator+(TensorBase<Dtype, NDim> &&lhs, const TensorBase<Dtype, NDim> &rhs)
{
return std::move(lhs += rhs);
}
friend TensorBase<Dtype, NDim> &&operator+(const TensorBase<Dtype, NDim> &lhs, TensorBase<Dtype, NDim> &&rhs)
{
return std::move(rhs += lhs);
}
friend TensorBase<Dtype, NDim> &&operator+(TensorBase<Dtype, NDim> &&lhs, TensorBase<Dtype, NDim> &&rhs)
{
return std::move(lhs += rhs);
}
// TODO: Matmul, *
/// scalar operators
rrl::TensorBase<Dtype, NDim> &operator+=(const Dtype rhs)
{
for (int i = 0; i < size(); i++)
data()[i] += rhs;
return *this;
}
TensorBase<Dtype, NDim> &operator-=(const Dtype rhs)
{
for (int i = 0; i < size(); i++)
data()[i] -= rhs;
return *this;
}
TensorBase<Dtype, NDim> &operator*=(const Dtype rhs)
{
for (int i = 0; i < size(); i++)
data()[i] *= rhs;
return *this;
}
TensorBase<Dtype, NDim> &operator/=(const Dtype rhs)
{
for (int i = 0; i < size(); i++)
data()[i] /= rhs;
return *this;
}
friend TensorBase<Dtype, NDim> operator+(TensorBase<Dtype, NDim> &lhs, Dtype rhs)
{
TensorBase<Dtype, NDim> result(lhs);
result += rhs;
return result;
}
friend TensorBase<Dtype, NDim> operator+(Dtype lhs, TensorBase<Dtype, NDim> &rhs)
{
return operator+(rhs, lhs);
}
friend TensorBase<Dtype, NDim> operator-(TensorBase<Dtype, NDim> &lhs, Dtype rhs)
{
TensorBase<Dtype, NDim> result(lhs);
result -= rhs;
return result;
}
friend TensorBase<Dtype, NDim> operator-(Dtype lhs, TensorBase<Dtype, NDim> &rhs)
{
return operator-(rhs, lhs);
}
friend TensorBase<Dtype, NDim> operator*(TensorBase<Dtype, NDim> &lhs, Dtype rhs)
{
TensorBase<Dtype, NDim> result(lhs);
result *= rhs;
return result;
}
friend TensorBase<Dtype, NDim> operator*(Dtype lhs, TensorBase<Dtype, NDim> &rhs)
{
return operator*(rhs, lhs);
}
friend TensorBase<Dtype, NDim> operator/(TensorBase<Dtype, NDim> &lhs, Dtype rhs)
{
TensorBase<Dtype, NDim> result(lhs);
result /= rhs;
return result;
}
template <int rows, int cols>
void copyDataFrom(const Eigen::Matrix<Dtype, rows, cols> &eMat)
{
//LOG_IF(FATAL, size_ != eMat.rows() * eMat.cols())
// << "Data size mismatch: " << size_ << "vs" << eMat.rows() * eMat.cols();
std::memcpy(namedTensor_.second.flat<Dtype>().data(), eMat.data(), sizeof(Dtype) * size_);
}
void copyDataFrom(const tensorflow::Tensor &tfTensor)
{
//LOG_IF(FATAL, size_ != tfTensor.flat<Dtype>().size())
// << "Data size mismatch: " << size_ << "vs" << tfTensor.flat<Dtype>().size();
std::memcpy(namedTensor_.second.flat<Dtype>().data(), tfTensor.flat<Dtype>().data(), sizeof(Dtype) * size_);
}
// Dtype *operator[](int x) {
// return vecTens[x].flat<Dtype>().data();
// };
Dtype &operator[](int i)
{
return namedTensor_.second.flat<Dtype>().data()[i];
}
Dtype &at(int i)
{
return namedTensor_.second.flat<Dtype>().data()[i];
}
const Dtype &operator[](int i) const
{
return namedTensor_.second.flat<Dtype>().data()[i];
}
const Dtype &at(int i) const
{
return namedTensor_.second.flat<Dtype>().data()[i];
}
///////////////////////////////
/////////// generic ///////////
///////////////////////////////
const std::string &getName() const { return namedTensor_.first; }
void setName(const std::string &name) { namedTensor_.first = name; }
const std::vector<int> &dim() const { return dim_; }
const int dim(int idx) const { return dim_[idx]; }
int rows() { return dim_[0]; }
int cols() { return dim_[1]; }
int batches() { return dim_[2]; }
const int size() const { return size_; }
/// you lose all data calling resize
void resize(const std::vector<int> dim)
{
//LOG_IF(FATAL, NDim != dim.size()) << "tensor rank mismatch";
dim_inv_.Clear();
size_ = 1;
dim_ = dim;
for (int i = dim.size() - 1; i > -1; i--)
{
dim_inv_.AddDim(dim[i]);
size_ *= dim[i];
}
for (int d = 0; d < NDim; d++)
{
esizes_[d] = dim_[d];
}
setDataType();
namedTensor_.second = tensorflow::Tensor(dtype_, dim_inv_);
}
protected:
void init(const std::vector<int> dim, const std::string name = "")
{
//LOG_IF(FATAL, dim.size() != NDim)
// << "specified dimension differs from the Dimension in the template parameter";
namedTensor_.first = name;
setDataType();
size_ = 1;
dim_ = dim;
for (int i = dim_.size() - 1; i > -1; i--)
{
dim_inv_.AddDim(dim_[i]);
size_ *= dim_[i];
}
namedTensor_ = {name, tensorflow::Tensor(dtype_, dim_inv_)};
for (int d = 0; d < NDim; d++)
esizes_[d] = dim_[d];
}
void setDataType()
{
if (typeid(Dtype) == typeid(float))
dtype_ = tensorflow::DataType::DT_FLOAT;
else if (typeid(Dtype) == typeid(double))
dtype_ = tensorflow::DataType::DT_DOUBLE;
else if (typeid(Dtype) == typeid(int))
dtype_ = tensorflow::DataType::DT_INT32;
}
tensorflow::DataType dtype_;
std::pair<std::string, tensorflow::Tensor> namedTensor_;
std::vector<int> dim_;
tensorflow::TensorShape dim_inv_; /// tensorflow dimension
long int size_ = -1;
Eigen::DSizes<Eigen::DenseIndex, NDim> esizes_;
std::vector<tensorflow::Tensor> vecTens;
};
/// Tensor methods
template <typename Dtype, int NDim>
class Tensor : public rrl::TensorBase<Dtype, NDim>
{
typedef rrl::TensorBase<Dtype, NDim> TensorBase;
public:
using TensorBase::eTensor;
using TensorBase::TensorBase;
using TensorBase::operator=;
using TensorBase::operator[];
using TensorBase::operator std::pair<std::string, tensorflow::Tensor>;
using TensorBase::operator tensorflow::Tensor;
};
/// 1D method
template <typename Dtype>
class Tensor<Dtype, 1> : public rrl::TensorBase<Dtype, 1>
{
typedef Eigen::Map<Eigen::Matrix<Dtype, -1, 1>> EigenMat;
typedef Eigen::TensorMap<Eigen::Tensor<Dtype, 1>, Eigen::Aligned> EigenTensor;
typedef rrl::TensorBase<Dtype, 1> TensorBase;
using TensorBase::dim_;
using TensorBase::dim_inv_;
using TensorBase::dtype_;
using TensorBase::namedTensor_;
public:
using TensorBase::eTensor;
using TensorBase::resize;
using TensorBase::TensorBase;
using TensorBase::operator=;
using TensorBase::operator[];
///////////////////////////////
////////// operators //////////
///////////////////////////////
template <int rows, int cols>
void operator=(const Eigen::Matrix<Dtype, rows, cols> &eMat)
{
//LOG_IF(FATAL, dim_[0] != eMat.rows())
// << "matrix size mismatch: " << dim_[0] << "X1"
// << "vs" << eMat.rows() << "X1";
std::memcpy(namedTensor_.second.template flat<Dtype>().data(), eMat.data(), sizeof(Dtype) * this->size_);
}
void operator=(const Eigen::Matrix<Dtype, -1, -1> &eMat)
{
//LOG_IF(FATAL, dim_[0] != eMat.rows())
// << "matrix size mismatch: " << dim_[0] << "X1"
// << "vs" << eMat.rows() << "X1";
std::memcpy(namedTensor_.second.template flat<Dtype>().data(), eMat.data(), sizeof(Dtype) * this->size_);
}
////////////////////////////
/// Eigen Methods mirror ///
////////////////////////////
EigenMat eMat()
{
EigenMat mat(namedTensor_.second.template flat<Dtype>().data(), TensorBase::dim_[0], 1);
return mat;
}
EigenMat block(int startIdx, int size)
{
LOG_IF(FATAL, startIdx + size > dim_[0]) << "requested segment exceeds Tensor dimension (startIdx+size v.s lastIdx = " << startIdx + size - 1 << " v.s. " << dim_[0] - 1;
EigenMat mat(namedTensor_.second.template flat<Dtype>().data() + startIdx, size, 1);
return mat;
}
/// you lose all data calling resize
void resize(int n)
{
std::vector<int> dim = {n};
resize(dim);
}
};
/// 2D method
template <typename Dtype>
class Tensor<Dtype, 2> : public rrl::TensorBase<Dtype, 2>
{
typedef Eigen::Map<Eigen::Matrix<Dtype, -1, -1>> EigenMat;
typedef Eigen::Map<Eigen::Matrix<Dtype, -1, -1>, 0, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>> EigenMatStride;
typedef Eigen::TensorMap<Eigen::Tensor<Dtype, 2>, Eigen::Aligned> EigenTensor;
typedef rrl::TensorBase<Dtype, 2> TensorBase;
using TensorBase::dim_;
using TensorBase::dim_inv_;
using TensorBase::dtype_;
using TensorBase::namedTensor_;
public:
using TensorBase::eTensor;
using TensorBase::resize;
using TensorBase::TensorBase;
using TensorBase::operator=;
using TensorBase::operator[];
///////////////////////////////
////////// operators //////////
///////////////////////////////
template <int rows, int cols>
void operator=(const Eigen::Matrix<Dtype, rows, cols> &eMat)
{
//LOG_IF(FATAL, dim_[0] != eMat.rows() || dim_[1] != eMat.cols())
// << "matrix size mismatch: " << dim_[0] << "X" << dim_[1] << "vs" << eMat.rows() << "X"
// << eMat.cols();
std::memcpy(namedTensor_.second.template flat<Dtype>().data(), eMat.data(), sizeof(Dtype) * this->size_);
}
void operator=(const Eigen::Matrix<Dtype, -1, -1> &eMat)
{
//LOG_IF(FATAL, dim_[0] != eMat.rows() || dim_[1] != eMat.cols())
// << "matrix size mismatch: " << dim_[0] << "X" << dim_[1] << "vs" << eMat.rows() << "X"
// << eMat.cols();
std::memcpy(namedTensor_.second.template flat<Dtype>().data(), eMat.data(), sizeof(Dtype) * this->size_);
}
////////////////////////////
/// Eigen Methods mirror ///
////////////////////////////
typename EigenMat::ColXpr col(int colId)
{
EigenMat mat(namedTensor_.second.template flat<Dtype>().data(), dim_[0], dim_[1]);
return mat.col(colId);
}
typename EigenMat::RowXpr row(int rowId)
{
EigenMat mat(namedTensor_.second.template flat<Dtype>().data(), dim_[0], dim_[1]);
return mat.row(rowId);
}
EigenMatStride block(int rowStart, int colStart, int rowDim, int colDim)
{
EigenMatStride mat(namedTensor_.second.template flat<Dtype>().data() + rowStart + dim_[0] * colStart, rowDim, colDim, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0], 1));
return mat;
}
EigenMat eMat()
{
EigenMat mat(namedTensor_.second.template flat<Dtype>().data(), dim_[0], dim_[1]);
return mat;
}
/// you lose all data calling resize
void resize(int rows, int cols)
{
std::vector<int> dim = {rows, cols};
resize(dim);
}
void conservativeResize(int rows, int cols)
{
if (dim_.size() == 0)
resize({rows, cols});
else if (dim_[0] == rows && dim_[1] == cols)
return;
Eigen::Matrix<Dtype, -1, -1> Temp(dim_[0], dim_[1]);
Temp = eMat();
resize({rows, cols});
Temp.conservativeResize(rows, cols);
eMat() = Temp;
}
void removeCol(int colID)
{
tensorflow::Tensor Temp(dtype_, dim_inv_);
memcpy(Temp.flat<Dtype>().data(),
namedTensor_.second.template flat<Dtype>().data(),
sizeof(Dtype) * namedTensor_.second.template flat<Dtype>().size());
resize(dim_[0], dim_[1] - 1);
memcpy(namedTensor_.second.template flat<Dtype>().data(),
Temp.flat<Dtype>().data(),
sizeof(Dtype) * dim_[0] * colID);
if (colID < dim_[1] - 1)
{
memcpy(namedTensor_.second.template flat<Dtype>().data() + colID * dim_[0],
Temp.flat<Dtype>().data() + (colID + 1) * dim_[0],
sizeof(Dtype) * dim_[0] * (dim_[1] - colID - 1));
}
}
};
/// 3D method
template <typename Dtype>
class Tensor<Dtype, 3> : public rrl::TensorBase<Dtype, 3>
{
typedef Eigen::Map<Eigen::Matrix<Dtype, -1, -1>> EigenMat;
typedef Eigen::Map<Eigen::Matrix<Dtype, -1, -1>, 0, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>> EigenMatStride;
typedef Eigen::TensorMap<Eigen::Tensor<Dtype, 3>, Eigen::Aligned> EigenTensor;
typedef rrl::TensorBase<Dtype, 3> TensorBase;
using TensorBase::dim_;
using TensorBase::dim_inv_;
using TensorBase::dtype_;
using TensorBase::namedTensor_;
public:
using TensorBase::eTensor;
using TensorBase::resize;
using TensorBase::TensorBase;
using TensorBase::operator=;
using TensorBase::operator[];
/// you lose all data calling resize
void resize(int rows, int cols, int batches)
{
std::vector<int> dim = {rows, cols, batches};
resize(dim);
}
void conservativeResize(int rows, int cols, int batches)
{
if (dim_.size() == 0)
resize({rows, cols, batches});
else if (dim_[0] == rows && dim_[1] == cols && dim_[2] == batches)
return;
std::vector<int> dim = {rows, cols, batches};
if (dim_[1] == cols && dim_[2] == batches)
{
Eigen::Matrix<Dtype, -1, -1> Temp(rows, cols * batches);
int commonRow = std::min(dim_[0], rows);
Temp.block(0, 0, commonRow, cols * batches) = EigenMatStride(namedTensor_.second.template flat<Dtype>().data(), commonRow, cols * batches, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0], 1));
resize(dim);
memcpy(namedTensor_.second.template flat<Dtype>().data(), Temp.data(),
sizeof(Dtype) * Temp.size());
}
else if (dim_[0] == rows && dim_[2] == batches)
{
Eigen::Matrix<Dtype, -1, -1> Temp(rows * cols, batches);
int commonCol = std::min(dim_[1], cols);
Temp.block(0, 0, rows * commonCol, batches) = EigenMatStride(namedTensor_.second.template flat<Dtype>().data(), rows * commonCol, batches, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0] * dim_[1], 1));
resize(dim);
memcpy(namedTensor_.second.template flat<Dtype>().data(), Temp.data(),
sizeof(Dtype) * Temp.size());
}
else if (dim_[0] == rows && dim_[1] == cols)
{
Eigen::Matrix<Dtype, -1, -1> Temp(rows * cols, batches);
int commonBat = std::min(dim_[2], batches);
Temp.block(0, 0, rows * cols, commonBat) = EigenMatStride(namedTensor_.second.template flat<Dtype>().data(), rows * cols, commonBat, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0] * dim_[1], 1));
resize(dim);
memcpy(namedTensor_.second.template flat<Dtype>().data(), Temp.data(),
sizeof(Dtype) * Temp.size());
}
else if (dim_[0] == rows)
{
Eigen::Matrix<Dtype, -1, -1> Temp(rows * cols, batches);
int commonCol = std::min(dim_[1], cols);
int commonBat = std::min(dim_[2], batches);
Temp.block(0, 0, rows * commonCol, commonBat) = EigenMatStride(namedTensor_.second.template flat<Dtype>().data(), rows * commonCol, commonBat, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0] * dim_[1], 1));
resize(dim);
memcpy(namedTensor_.second.template flat<Dtype>().data(), Temp.data(),
sizeof(Dtype) * Temp.size());
}
else if (dim_[1] == cols)
{
Eigen::Matrix<Dtype, -1, -1> Temp(rows, cols * batches);
int commonRow = std::min(dim_[0], rows);
int commonBat = std::min(dim_[2], batches);
Temp.block(0, 0, commonRow, cols * commonBat) = EigenMatStride(namedTensor_.second.template flat<Dtype>().data(), commonRow, cols * commonBat, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0], 1));
resize(dim);
memcpy(namedTensor_.second.template flat<Dtype>().data(), Temp.data(),
sizeof(Dtype) * Temp.size());
}
else if (dim_[2] == batches)
{
int commonRow = std::min(dim_[0], rows);
int commonCol = std::min(dim_[1], cols);
Eigen::Matrix<Dtype, -1, -1> Temp(dim_[0] * commonCol, batches);
Temp = EigenMatStride(namedTensor_.second.template flat<Dtype>().data(), dim_[0] * commonCol, batches, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0] * dim_[1], 1));
Eigen::Matrix<Dtype, -1, -1> Temp2(rows, cols * batches);
Temp2.block(0, 0, commonRow, commonCol * batches) = EigenMatStride(Temp.data(), commonRow, commonCol * batches, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0], 1));
resize(dim);
memcpy(namedTensor_.second.template flat<Dtype>().data(), Temp2.data(),
sizeof(Dtype) * Temp2.size());
}
else
{
int commonRow = std::min(dim_[0], rows);
int commonCol = std::min(dim_[1], cols);
int commonBat = std::min(dim_[2], batches);
Eigen::Matrix<Dtype, -1, -1> Temp(dim_[0] * commonCol, commonBat);
Temp = EigenMatStride(namedTensor_.second.template flat<Dtype>().data(), dim_[0] * commonCol, commonBat, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0] * dim_[1], 1));
Eigen::Matrix<Dtype, -1, -1> Temp2(rows, cols * batches);
Temp2.block(0, 0, commonRow, commonCol * commonBat) = EigenMatStride(Temp.data(), commonRow, commonCol * commonBat, Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0], 1));
resize(dim);
memcpy(namedTensor_.second.template flat<Dtype>().data(), Temp2.data(),
sizeof(Dtype) * Temp2.size());
}
}
void removeBatch(int batchId)
{
tensorflow::Tensor Temp(dtype_, dim_inv_);
memcpy(Temp.flat<Dtype>().data(),
namedTensor_.second.template flat<Dtype>().data(),
sizeof(Dtype) * namedTensor_.second.template flat<Dtype>().size());
resize(dim_[0], dim_[1], dim_[2] - 1);
memcpy(namedTensor_.second.template flat<Dtype>().data(),
Temp.flat<Dtype>().data(),
sizeof(Dtype) * dim_[0] * dim_[1] * batchId);
if (batchId < dim_[2] - 1)
{
memcpy(namedTensor_.second.template flat<Dtype>().data() + batchId * dim_[0] * dim_[1],
Temp.flat<Dtype>().data() + (batchId + 1) * dim_[0] * dim_[1],
sizeof(Dtype) * dim_[0] * dim_[1] * (dim_[2] - batchId - 1));
}
}
typename EigenMat::ColXpr col(int batchId, int colId)
{
EigenMat mat(namedTensor_.second.template flat<Dtype>().data() + batchId * dim_[0] * dim_[1], dim_[0], dim_[1]);
return mat.col(colId);
}
typename EigenMat::RowXpr row(int batchId, int rowId)
{
EigenMat mat(namedTensor_.second.template flat<Dtype>().data() + batchId * dim_[0] * dim_[1], dim_[0], dim_[1]);
return mat.row(rowId);
}
/// row(rowId) of all the batches)
/// shape = dim[1] * dim[2]
/// [ row(0,rowId).transpose(), row(1,rowId).transpose(), ... ]
EigenMatStride row(int rowId)
{
EigenMatStride mat(namedTensor_.second.template flat<Dtype>().data() + rowId, dim_[1], dim_[2], Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0] * dim_[1], dim_[0]));
return mat;
}
/// col(colId) of all the batches)
/// shape = dim[0] * dim[2]
/// [ col(0,colId), col(1,colId), ... ]
EigenMatStride col(int colId)
{
EigenMatStride mat(namedTensor_.second.template flat<Dtype>().data() + colId * dim_[0], dim_[0], dim_[2], Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(dim_[0] * dim_[1], 1));
return mat;
}
EigenMat batch(int batchId)
{
EigenMat mat(namedTensor_.second.template flat<Dtype>().data() + batchId * dim_[0] * dim_[1], dim_[0], dim_[1]);
return mat;
}
EigenTensor batchBlock(int startBatchID, int size)
{
//LOG_IF(FATAL, startBatchID + size > dim_[2]) << "endBatchID exceeds last batch ID: " << startBatchID + size - 1 << "vs" << dim_[2] - 1;
EigenTensor ten(namedTensor_.second.template flat<Dtype>().data() + startBatchID * dim_[0] * dim_[1], dim_[0], dim_[1], size);
return ten;
}
template <int rows, int cols>
void partiallyFillBatch(int batchId, Eigen::Matrix<Dtype, rows, cols> &eMat)
{
//LOG_IF(FATAL, dim_[0] != rows) << "Column size mismatch ";
std::memcpy(namedTensor_.second.template flat<Dtype>().data() + batchId * dim_[0] * dim_[1],
eMat.data(), sizeof(Dtype) * eMat.size());
}
template <int rows>
void partiallyFillBatch(int batchId, std::vector<Eigen::Matrix<Dtype, rows, 1>> &eMatVec, int ignoreLastN = 0)
{
//LOG_IF(FATAL, dim_[0] != eMatVec[0].rows()) << "Column size mismatch " << dim_[0] << "vs." << eMatVec[0].rows();
for (int colId = 0; colId < eMatVec.size() - ignoreLastN; colId++)
batch(batchId).col(colId) = eMatVec[colId];
}
};
template <typename Dtype, int NDim>
std::ostream &operator<<(std::ostream &os, TensorBase<Dtype, NDim> &m)
{
os << m.eTensor();
return os;
}
};
#endif