MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TensorTests.cpp
Go to the documentation of this file.
1 
25 #include <gtest/gtest.h>
26 
27 #include <fstream>
28 // Include headers that implement a archive in simple text format
29 #include <boost/archive/text_iarchive.hpp>
30 #include <boost/archive/text_oarchive.hpp>
31 
32 // Redefine word "public" so every class field/method will be accessible for tests.
33 #define private public
34 #include <types/Tensor.hpp>
35 #include <types/Matrix.hpp>
36 
40 TEST(Tensor, Dimensions2x5) {
41  // Default sizes of matrices.
42  const size_t N = 2;
43  const size_t M = 5;
44  const size_t K = 13;
45 
46  mic::types::Tensor<float> nm({N, M, K});
47 
48  ASSERT_EQ(nm.dim(0), N);
49  ASSERT_EQ(nm.dim(1), M);
50  ASSERT_EQ(nm.dim(2), K);
51 }
52 
53 
57 TEST(Tensor, Enumeration2x3) {
58  // Default sizes of matrices.
59  const size_t N = 2;
60  const size_t M = 3;
61 
62  mic::types::Tensor<float> nm({N, M});
63  nm.enumerate();
64 
65 // std::cout << nm << std::endl;
66 
67  for (size_t i =0; i< N*M; i++)
68  ASSERT_EQ(nm(i), i);
69 
70 /* for(size_t row=0; row<N; row++) {
71  for(size_t col=0; col<M; col++)
72  std::cout << " nm(" << row << "," << col << ") = " << nm({row,col});
73  std::cout << std::endl;
74  }//: for*/
75 
76  ASSERT_EQ(nm({0,0}), 0);
77  ASSERT_EQ(nm({1,0}), 1);
78  ASSERT_EQ(nm({0,1}), 2);
79  ASSERT_EQ(nm({1,1}), 3);
80  ASSERT_EQ(nm({0,2}), 4);
81  ASSERT_EQ(nm({1,2}), 5);
82 
83 }
84 
85 
89 TEST(Tensor, Serialization) {
90  // Default sizes of matrices.
91  const size_t N = 2;
92  const size_t M = 5;
93  const size_t K = 11;
94 
95  mic::types::Tensor<float> nm({N, M, K});
96  nm.randn();
97 
98  const char* fileName = "saved.txt";
99  // Save data
100  {
101  // Create an output archive
102  std::ofstream ofs(fileName);
103  boost::archive::text_oarchive ar(ofs);
104  // Write data
105  ar & nm;
106  //std::cout << "Saved matrix = " << nm << std::endl;
107  }
108 
109  // Restore data
110  mic::types::Tensor<float> restored_tensor;
111  restored_tensor.randn();
112 
113  {
114  // Create and input archive
115  std::ifstream ifs(fileName);
116  boost::archive::text_iarchive ar(ifs);
117  // Load data
118  ar & restored_tensor;
119  //std::cout << "Restored tensor = " << restored_tensor << std::endl;
120  }
121 
122  // Check dimensions.
123  ASSERT_EQ(nm.elements, restored_tensor.elements);
124  ASSERT_EQ(nm.dimensions.size(), restored_tensor.dimensions.size());
125  for (size_t i =0; i< (size_t)nm.dimensions.size(); i++)
126  ASSERT_EQ(nm.dimensions[i], restored_tensor.dimensions[i]);
127 
128  // Check values.
129  float eps = 1e-8;
130  for (size_t i =0; i< (size_t)nm.size(); i++)
131  EXPECT_LE(fabs(nm(i) - restored_tensor(i)), eps);
132 
133 }
134 
138 TEST(Tensor, Im2Col2x3) {
139  // Default sizes of matrices.
140  const size_t N = 2;
141  const size_t M = 3;
142 
143  mic::types::Tensor<float> nm({N, M});
144  nm.enumerate();
145 
146 
147 }
148 
149 int main(int argc, char **argv) {
150  testing::InitGoogleTest(&argc, argv);
151  return RUN_ALL_TESTS();
152 }
153 
154 
int main(int argc, char **argv)
std::vector< size_t > dimensions
Definition: Tensor.hpp:692
void randn(T mean=0, T stddev=1)
Definition: Tensor.hpp:368
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
TEST(Tensor, Dimensions2x5)
Definition: TensorTests.cpp:40
File contaning a template tensor class.