MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tensor_test.cpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include <iomanip>
25 
26 #include <sys/time.h>
27 
28 #include <types/MatrixTypes.hpp>
29 #include <types/TensorTypes.hpp>
30 
31 #include <fstream>
32 // Include headers that implement a archive in simple text format
33 #include <boost/archive/text_iarchive.hpp>
34 #include <boost/archive/text_oarchive.hpp>
35 
36 #include <boost/archive/xml_iarchive.hpp>
37 #include <boost/archive/xml_oarchive.hpp>
38 
39 
40 using namespace Eigen;
41 using namespace std;
42 
43 
44 /*
45 Eigen Concatenation
46 Horizontally:
47 
48 MatrixXd C(A.rows(), A.cols()+B.cols());
49 C << A, B;
50 
51 Vertically:
52 
53 MatrixXd D(A.rows()+B.rows(), A.cols());
54 D << A,
55  B;
56  */
57 
65 int main(int argc, char* argv[]) {
66  // Default sizes of matrices.
67  const size_t N = 2;
68  const size_t M = 3;
69  const size_t K = 4;
70 
71  // Create new tensor.
72  mic::types::TensorXd t0({N, M});
73  t0.enumerate();
74  std::cout << "t0 = " << t0 << std::endl;
75  for(size_t row=0; row<N; row++) {
76  for(size_t col=0; col<M; col++)
77  std::cout << " t0(" << row << "," << col << ") = " << t0({row,col});
78  std::cout << std::endl;
79  }//: for
80 
81 
82  // Create new tensor.
83  mic::types::TensorXd t1({N*M*K});
84  t1.enumerate();
85  // Different methods of setting the new value of data.
86  t1({2}) = 33.1;
87  t1(4) = 55;
88  double* data = t1.data();
89  data[10] = 1000.1;
90  std::cout << "t1 = " << t1 << std::endl;
91 
92  // Conservative resize - with keeping the old values. Number of elements must remained unchanged!
93  t1.conservativeResize({N, M, K});
94  t1({2,2,2}) = 222.1;
95  t1(5) = 666;
96  std::cout << "resized t1 = " << t1 << std::endl;
97 
98  // Get access to elements one by one.
99  std::cout << "Printing elements one by one (getIndex): ";
100  for(size_t k=0; k<K; k++)
101  for(size_t m=0; m<M; m++)
102  for(size_t n=0; n<N; n++) {
103  double d = t1({n,m,k});
104  std::cout << d << ", ";
105  }//: for
106  std::cout << std::endl;
107 
108  std::cout << "Printing elements one by one: ";
109  for(size_t k=0; k<t1.size(); k++){
110  double d = t1(k);
111  std::cout << d << ", ";
112  }//: for
113  std::cout << std::endl;
114 
115 
116  // Resize and change the total number of elements.
117  t1.resize({4,4,4,4});
118  t1.enumerate();
119  std::cout << "resized t1 = " << t1 << std::endl;
120 
121  // Get a subtensor.
122  mic::types::TensorXd t2 = t1.block({{0,3},{0},{0},{0,1}});
123  std::cout << "subtensor t2 = " << t2 << std::endl;
124 
125  // Flatten both tensors.
126  t1.flatten();
127  std::cout << "flattened t1 = " << t1 << std::endl;
128  t2.flatten();
129  std::cout << "flattened t2 = " << t2 << std::endl;
130 
131  // Concatenate tensors - add t2 two times.
132  t1.concatenate({t2,t2});
133  std::cout << " t1 concatenated with {t2,t2} = " << t1 << std::endl;
134  t1.resize({3,2});
135  std::cout << " t1 resized to {3, 2} = " << t1 << std::endl;
136 
137  // Map 2D tensor to matrix.
139  std::cout << "matrix from tensor = \n" << mat << std::endl;
140  mat *= 2;
141  std::cout << "matrix *2 = \n" << mat << std::endl;
142 
143  // Map matrix to 2D tensor.
144  mic::types::TensorXd t4 = mat;
145  std::cout << "tensor from matrix = " << t4 << std::endl;
146 
147 
148  // Tensor pointer.
149  mic::types::TensorXfPtr ten_ptr (new mic::types::TensorXf({N, M, K}));
150  ten_ptr->enumerate();
151  std::cout << "Pointer to tensor = " << *ten_ptr << std::endl;
152 
153 } //: main
154 
std::shared_ptr< mic::types::TensorXf > TensorXfPtr
Shared pointer to tensor of single precision floats (of dynamic size).
Contains definition of basic matrix datatypes derived from Eigen.
void concatenate(const Tensor &obj_)
Definition: Tensor.hpp:617
Tensor< T > block(std::vector< std::vector< size_t > > ranges_)
Definition: Tensor.hpp:550
int main(int argc, char *argv[])
Program for testing tensors/Eigen map.
Definition: tensor_test.cpp:65
Template class representing an nD (n-Dimensional) tensor. Tensor is row-major, i.e. first dimension is height (rows), second is width (cols), third is depth (channels) etc.
Definition: Matrix.hpp:49
Template-typed Matrix of dynamic size. Uses OpenBLAS if found by CMAKE - overloaded, specializations of * operator for types: float, double.
Definition: Matrix.hpp:64
Contains declaration of tensor types.