vmmlib  1.7.0
 All Classes Namespaces Functions Pages
tucker4_tensor.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef __VMML__TUCKER4_TENSOR__HPP__
34 #define __VMML__TUCKER4_TENSOR__HPP__
35 
36 #include <vmmlib/t4_hooi.hpp>
37 
38 namespace vmml {
39 
40  // T is the input type for the tensor
41  template< size_t R1, size_t R2, size_t R3, size_t R4, size_t I1, size_t I2, size_t I3, size_t I4, typename T = float>
43  public:
44  typedef double T_internal;
45 
48 
55 
57  ~tucker4_tensor();
58  size_t nnz_core() const;
59  size_t size_core() const;
60  void reconstruct(t4_type& data_);
61 
62  template< typename T_init>
63  tensor_stats decompose(const t4_type& data_, T_init init, const size_t max_iterations = 10, const float tolerance = 10);
64 
65  template< typename T_init>
66  tensor_stats tucker_als(const t4_type& data_, T_init init, const size_t max_iterations = 10, const float tolerance = 1e-04);
67 
68  private:
69  //t3_core_type* _core ;
70  u1_type* _u1;
71  u2_type* _u2;
72  u3_type* _u3;
73  u4_type* _u4;
74  t4_core_type _core;
75 
76  }; // class tucker3_tensor
77 
78 
79 
80 #define VMML_TEMPLATE_STRING template< size_t R1, size_t R2, size_t R3, size_t R4, size_t I1, size_t I2, size_t I3, size_t I4, typename T >
81 #define VMML_TEMPLATE_CLASSNAME tucker4_tensor< R1, R2, R3, R4, I1, I2, I3, I4, T >
82 
83  VMML_TEMPLATE_STRING
84  VMML_TEMPLATE_CLASSNAME::tucker4_tensor() {
85  _core.zero();
86  _u1 = new u1_type();
87  _u1->zero();
88  _u2 = new u2_type();
89  _u2->zero();
90  _u3 = new u3_type();
91  _u3->zero();
92  _u4 = new u4_type();
93  _u4->zero();
94  }
95 
96  VMML_TEMPLATE_STRING
97  size_t
98  VMML_TEMPLATE_CLASSNAME::nnz_core() const {
99  return _core.nnz();
100  }
101 
102  VMML_TEMPLATE_STRING
103  size_t
104  VMML_TEMPLATE_CLASSNAME::size_core() const {
105  return _core.size();
106  }
107 
108  VMML_TEMPLATE_STRING
109  VMML_TEMPLATE_CLASSNAME::~tucker4_tensor() {
110  delete _u1;
111  delete _u2;
112  delete _u3;
113  delete _u4;
114  }
115 
116  VMML_TEMPLATE_STRING
117  void
118  VMML_TEMPLATE_CLASSNAME::reconstruct(t4_type& data_) {
119  // TODO (maybe try with the reordered summation formula)
120  t4_ttm::full_tensor4_matrix_multiplication(_core, *_u1, *_u2, *_u3, *_u4, data_);
121  }
122 
123  VMML_TEMPLATE_STRING
124  template< typename T_init>
125  tensor_stats
126  VMML_TEMPLATE_CLASSNAME::decompose(const t4_type& data_, T_init init, const size_t max_iterations, const float tolerance) {
127  return tucker_als(data_, init, max_iterations, tolerance);
128  }
129 
130  VMML_TEMPLATE_STRING
131  template< typename T_init>
132  tensor_stats
133  VMML_TEMPLATE_CLASSNAME::tucker_als(const t4_type& data_, T_init init, const size_t max_iterations, const float tolerance) {
134  tensor_stats result;
135 
136  typedef t4_hooi< R1, R2, R3, R4, I1, I2, I3, I4, T_internal > hooi_type;
137  result += hooi_type::als(data_, *_u1, *_u2, *_u3, *_u4, _core, init, 0, max_iterations, tolerance);
138 
139  return result;
140  }
141 
142 #undef VMML_TEMPLATE_STRING
143 #undef VMML_TEMPLATE_CLASSNAME
144 
145 } // namespace vmml
146 
147 #endif /* TUCKER4_TENSOR_HPP */