Tenncor
rbm.hpp
Go to the documentation of this file.
1 
9 #include "layr/dense.hpp"
10 #include "layr/ulayer.hpp"
11 
12 #ifndef LAYR_RBM_HPP
13 #define LAYR_RBM_HPP
14 
15 namespace layr
16 {
17 
19 const std::string hidden_key = "hidden";
20 
22 const std::string visible_key = "visible";
23 
25 struct RBMBuilder final : public iLayerBuilder
26 {
27  RBMBuilder (std::string label) : label_(label) {}
28 
30  void set_tensor (teq::TensptrT tens, std::string target) override {} // rbm has no tensor
31 
33  void set_sublayer (LayerptrT layer) override
34  {
35  layers_.push_back(layer);
36  }
37 
39  LayerptrT build (void) const override;
40 
41 private:
42  std::vector<LayerptrT> layers_;
43 
44  std::string label_;
45 };
46 
48 const std::string rbm_layer_key =
50 [](std::string label) -> LBuilderptrT
51 {
52  return std::make_shared<RBMBuilder>(label);
53 });
54 
57 struct RBM final : public iLayer
58 {
59  RBM (teq::DimT nhidden, teq::DimT nvisible,
60  UnaryptrT activation,
61  layr::InitF<PybindT> weight_init,
62  layr::InitF<PybindT> bias_init,
63  const std::string& label) :
64  label_(label),
65  hidden_(std::make_shared<Dense>(
66  nhidden, nvisible, weight_init, bias_init, hidden_key)),
67  activation_(activation)
68  {
69  auto hidden_contents = hidden_->get_contents();
70  auto weight = hidden_contents[0];
71  auto hbias = hidden_contents[1];
72  NodeptrT vbias = nullptr;
73 
74  if (bias_init)
75  {
76  vbias = bias_init(teq::Shape({nvisible}), dense_bias_key);
77  }
78  visible_ = std::make_shared<Dense>(tenncor::transpose(
80  tag_sublayers();
81  }
82 
83  RBM (DenseptrT hidden, DenseptrT visible,
84  UnaryptrT activation, std::string label) :
85  label_(label),
86  hidden_(hidden),
87  visible_(visible),
88  activation_(activation)
89  {
90  tag_sublayers();
91  }
92 
93  RBM (const RBM& other,
94  std::string label_prefix = "")
95  {
96  copy_helper(other, label_prefix);
97  }
98 
99  RBM& operator = (const RBM& other)
100  {
101  if (this != &other)
102  {
103  copy_helper(other);
104  }
105  return *this;
106  }
107 
108  RBM (RBM&& other) = default;
109 
110  RBM& operator = (RBM&& other) = default;
111 
113  RBM* clone (std::string label_prefix = "") const
114  {
115  return static_cast<RBM*>(this->clone_impl(label_prefix));
116  }
117 
119  size_t get_ninput (void) const override
120  {
121  return hidden_->get_ninput();
122  }
123 
125  size_t get_noutput (void) const override
126  {
127  return hidden_->get_noutput();
128  }
129 
131  std::string get_ltype (void) const override
132  {
133  return rbm_layer_key;
134  }
135 
137  std::string get_label (void) const override
138  {
139  return label_;
140  }
141 
143  teq::TensptrsT get_contents (void) const override
144  {
145  auto out = hidden_->get_contents();
146  auto vis_contents = visible_->get_contents();
147  auto act_contents = activation_->get_contents();
148  out.insert(out.end(), vis_contents.begin(), vis_contents.end());
149  out.insert(out.end(), act_contents.begin(), act_contents.end());
150  return out;
151  }
152 
154  NodeptrT connect (NodeptrT visible) const override
155  {
156  return activation_->connect(hidden_->connect(visible));
157  }
158 
161  {
162  return activation_->connect(visible_->connect(hidden));
163  }
164 
165 private:
166  iLayer* clone_impl (const std::string& label_prefix) const override
167  {
168  return new RBM(*this, label_prefix);
169  }
170 
171  void tag_sublayers (void)
172  {
173  auto hidden_subs = hidden_->get_contents();
174  for (auto& sub : hidden_subs)
175  {
176  if (sub)
177  {
178  tag(sub, LayerId(hidden_->get_ltype(),
179  hidden_->get_label(), 0));
180  }
181  }
182 
183  auto visible_subs = visible_->get_contents();
184  for (auto& sub : visible_subs)
185  {
186  if (sub)
187  {
188  tag(sub, LayerId(visible_->get_ltype(),
189  visible_->get_label(), 1));
190  }
191  }
192 
193  auto activation_subs = activation_->get_contents();
194  for (auto& sub : activation_subs)
195  {
196  tag(sub, LayerId(activation_->get_ltype(),
197  activation_->get_label(), 2));
198  }
199  }
200 
201  void copy_helper (const RBM& other, std::string label_prefix = "")
202  {
203  label_ = label_prefix + other.label_;
204  hidden_ = DenseptrT(other.hidden_->clone(label_prefix));
205  auto hidden_contents = hidden_->get_contents();
206  NodeptrT vbias_node = nullptr;
207  if (auto vbias = other.visible_->get_contents()[1])
208  {
210  vbias)->clone());
211  }
212  visible_ = std::make_shared<Dense>(tenncor::transpose(
213  eteq::NodeConverters<PybindT>::to_node(hidden_contents[0])),
214  vbias_node, label_prefix + visible_key);
215 
216  activation_ = UnaryptrT(other.activation_->clone(label_prefix));
217  tag_sublayers();
218  }
219 
220  std::string label_;
221 
223 
225 
227 };
228 
230 using RBMptrT = std::shared_ptr<RBM>;
231 
232 }
233 
234 #endif // LAYR_RBM_HPP
Builder implementation for restricted boltzmann layer.
Definition: rbm.hpp:25
size_t get_ninput(void) const override
Implementation of iLayer.
Definition: rbm.hpp:119
const std::string dense_bias_key
Fully connected bias label.
Definition: dense.hpp:24
Layer implementation to apply fully_connect functions to weight and optional bias.
Definition: dense.hpp:72
LayerRegistry & get_layer_reg(void)
Return global layer registry reference.
RBM * clone(std::string label_prefix="") const
Return deep copy of this layer with prefixed label.
Definition: rbm.hpp:113
LayerptrT build(void) const override
Implementation of iLayerBuilder.
Definition: rbm.cpp:8
std::shared_ptr< Dense > DenseptrT
Smart pointer of fully connected layer.
Definition: dense.hpp:201
std::string register_tagr(std::string key, LayerBuildF builder)
Definition: layer.hpp:198
std::vector< LayerptrT > layers_
Definition: rbm.hpp:42
Definition: shape.hpp:62
Definition: conv.hpp:16
size_t get_noutput(void) const override
Implementation of iLayer.
Definition: rbm.hpp:125
std::shared_ptr< iLayerBuilder > LBuilderptrT
Layer builder smart pointer.
Definition: layer.hpp:179
std::shared_ptr< ULayer > UnaryptrT
Smart pointer of unary layer.
Definition: ulayer.hpp:200
DenseptrT visible_
Definition: rbm.hpp:224
NodeptrT connect(NodeptrT visible) const override
Implementation of iLayer.
Definition: rbm.hpp:154
std::string get_label(void) const override
Implementation of iLayer.
Definition: rbm.hpp:137
const std::string layers_key_prefix
String prefixed to every layer key.
Definition: layer.hpp:25
std::function< eteq::VarptrT< T >(teq::Shape, std::string)> InitF
Function that produces a variable given the variable&#39;s shape and label.
Definition: init.hpp:20
Definition: layer.hpp:164
DenseptrT hidden_
Definition: rbm.hpp:222
teq::TensptrsT get_contents(void) const override
Implementation of iLayer.
Definition: rbm.hpp:143
EigenptrT< T > sub(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:679
std::string get_ltype(void) const override
Implementation of iLayer.
Definition: rbm.hpp:131
void tag(teq::TensptrT tensor, LayerId subs) const
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
RBM(const RBM &other, std::string label_prefix="")
Definition: rbm.hpp:93
RBM(teq::DimT nhidden, teq::DimT nvisible, UnaryptrT activation, layr::InitF< PybindT > weight_init, layr::InitF< PybindT > bias_init, const std::string &label)
Definition: rbm.hpp:59
void set_tensor(teq::TensptrT tens, std::string target) override
Implementation of iLayerBuilder.
Definition: rbm.hpp:30
std::vector< TensptrT > TensptrsT
Vector of tensor smart pointers.
Definition: itensor.hpp:60
Sublayer type, label, and index encapsulation.
Definition: layer.hpp:35
RBM(DenseptrT hidden, DenseptrT visible, UnaryptrT activation, std::string label)
Definition: rbm.hpp:83
uint16_t DimT
Type used for shape dimension.
Definition: shape.hpp:31
RBM & operator=(const RBM &other)
Definition: rbm.hpp:99
NodeptrT backward_connect(NodeptrT hidden) const
Return visible reconstruction from hidden.
Definition: rbm.hpp:160
void copy_helper(const RBM &other, std::string label_prefix="")
Definition: rbm.hpp:201
void tag_sublayers(void)
Definition: rbm.hpp:171
std::shared_ptr< iNode< T > > NodeptrT
Smart pointer of node.
Definition: inode.hpp:63
Definition: rbm.hpp:57
std::string label_
Definition: rbm.hpp:44
UnaryptrT activation_
Definition: rbm.hpp:226
void set_sublayer(LayerptrT layer) override
Implementation of iLayerBuilder.
Definition: rbm.hpp:33
RBMBuilder(std::string label)
Definition: rbm.hpp:27
std::shared_ptr< iLayer > LayerptrT
Smart pointer of layer.
Definition: layer.hpp:159
iLayer * clone_impl(const std::string &label_prefix) const override
Definition: rbm.hpp:166
const std::string hidden_key
Hidden fully connected layer label.
Definition: rbm.hpp:19
const std::string rbm_layer_key
Identifier for restricted boltzmann machine.
Definition: rbm.hpp:48
Definition: layer.hpp:121
Node registry of tensor types and tensor to node function.
Definition: inode.hpp:75
std::shared_ptr< RBM > RBMptrT
Smart pointer of RBM layer.
Definition: rbm.hpp:230
const std::string visible_key
Visible fully connected layer label.
Definition: rbm.hpp:22
std::string label_
Definition: rbm.hpp:220