SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
mnist.hpp
Go to the documentation of this file.
1 /*
2  * SNABSuite -- Spiking Neural Architecture Benchmark Suite
3  * Copyright (C) 2019 Christoph Ostrau
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifndef SNABSUITE_SNABS_MNIST_HPP
22 #define SNABSUITE_SNABS_MNIST_HPP
23 
24 #include <cypress/cypress.hpp>
25 
26 #include "common/snab_base.hpp"
27 #include "mnist_mlp.hpp"
28 
29 namespace SNAB {
30 using namespace cypress;
31 
36 class MNIST_BASE : public SNABBase {
37 public:
38  MNIST_BASE(const std::string backend, size_t bench_index);
39  virtual cypress::Network &build_netw(cypress::Network &netw) override;
40  virtual void run_netw(cypress::Network &netw) override;
41  virtual std::vector<std::array<cypress::Real, 4>> evaluate() override;
42  /*virtual std::shared_ptr<SNABBase> clone() override
43  {
44  return std::make_shared<MNIST_BASE>(m_backend, m_bench_index);
45  }*/
46 
47 protected:
48  NeuronParameter m_neuro_params; // Neuron Parameters
49  std::string m_neuron_type_str; // String containing the neuron type
50  size_t m_images, m_batchsize; // Number of images in general and per batch
51  cypress::Real m_duration, m_max_freq,
52  m_pause; // Spike data: Sample duration, Sample max freq, and pause
53  // time between two images
54  bool m_poisson,
55  m_train_data; // Use poisson or regular spiking, Use train or test data
56  cypress::Real m_max_weight; // Max weight to be scaled to
57  // std::vector<cypress::Real> m_conv_max_weight;
58  cypress::Real m_max_pool_weight;
59  cypress::Real m_pool_inhib_weight;
60  cypress::Real m_pool_delay;
61 
62  std::vector<
63  std::pair<std::vector<std::vector<Real>>, std::vector<uint16_t>>>
64  m_batch_data; // Spiking data for the network
65 
66  bool m_batch_parallel = true; // Run batches parallel (in one network)
67 
68  std::vector<cypress::Network>
69  m_networks; // Used for batches not run in parallel
70 
71  std::vector<cypress::PopulationBase> m_label_pops; // vector of label pops
72 
73  std::string m_dnn_file = "";
74  bool m_scaled_image = false;
75  std::shared_ptr<MNIST::MLPBase> m_mlp;
76 
77  cypress::Real m_weights_scale_factor = 0.0;
78  std::vector<cypress::Real> m_conv_weights_scale_factors;
79 
80  bool m_count_spikes = false;
81  std::vector<cypress::PopulationBase> m_all_pops;
82 
91  size_t create_deep_network(cypress::Network &netw, Real max_weight = 0.0,
92  Real max_pool_weight = 0.0,
93  Real pool_inhib_weight = 0.0);
94 
102  MNIST_BASE(const std::string backend, size_t bench_index, std::string name);
103 
108  void read_config();
109 
116  cypress::Network &build_netw_int(cypress::Network &netw);
117 };
118 
124 class MnistSpikey : public MNIST_BASE {
125 public:
126  MnistSpikey(const std::string backend, size_t bench_index)
127  : MNIST_BASE(backend, bench_index, __func__)
128  {
129  }
130 
131  std::shared_ptr<SNABBase> clone() override
132  {
133  return std::make_shared<MnistSpikey>(m_backend, m_bench_index);
134  }
135 };
136 
142 class MnistNAS63 : public MNIST_BASE {
143 public:
144  MnistNAS63(const std::string backend, size_t bench_index)
145  : MNIST_BASE(backend, bench_index, __func__)
146  {
147  }
148 
149  std::shared_ptr<SNABBase> clone() override
150  {
151  return std::make_shared<MnistNAS63>(m_backend, m_bench_index);
152  }
153 };
154 
160 class MnistNAS129 : public MNIST_BASE {
161 public:
162  MnistNAS129(const std::string backend, size_t bench_index)
163  : MNIST_BASE(backend, bench_index, __func__)
164  {
165  }
166 
167  std::shared_ptr<SNABBase> clone() override
168  {
169  return std::make_shared<MnistNAS129>(m_backend, m_bench_index);
170  }
171 };
172 
178 class MnistNAStop : public MNIST_BASE {
179 public:
180  MnistNAStop(const std::string backend, size_t bench_index)
181  : MNIST_BASE(backend, bench_index, __func__)
182  {
183  }
184 
185  std::shared_ptr<SNABBase> clone() override
186  {
187  return std::make_shared<MnistNAStop>(m_backend, m_bench_index);
188  }
189 };
190 
198 class MnistDiehl : public MNIST_BASE {
199 public:
200  MnistDiehl(const std::string backend, size_t bench_index)
201  : MNIST_BASE(backend, bench_index, __func__)
202  {
203  }
204 
205  std::shared_ptr<SNABBase> clone() override
206  {
207  return std::make_shared<MnistDiehl>(m_backend, m_bench_index);
208  }
209 };
210 
216 public:
217  MnistITLLastLayer(const std::string backend, size_t bench_index)
218  : MNIST_BASE(backend, bench_index, __func__)
219  {
220  }
221 
222  virtual cypress::Network &build_netw(cypress::Network &netw) override;
223  virtual void run_netw(cypress::Network &netw) override;
224  virtual std::vector<std::array<cypress::Real, 4>> evaluate() override;
225 
226  virtual std::shared_ptr<SNABBase> clone() override
227  {
228  return std::make_shared<MnistITLLastLayer>(m_backend, m_bench_index);
229  }
230 
231 protected:
232  MnistITLLastLayer(const std::string backend, size_t bench_index,
233  std::string name)
234  : MNIST_BASE(backend, bench_index, name)
235  {
236  }
237  std::pair<std::vector<std::vector<std::vector<Real>>>,
238  std::vector<uint16_t>>
240 
241  bool m_positive = false;
242  cypress::Real m_norm_rate_hidden = 1.0;
243  cypress::Real m_norm_rate_last = 1.0;
244  size_t m_global_correct = 0;
245  size_t m_num_images = 0;
246  cypress::Real m_sim_time = 0;
247  std::string m_loss_function = "CatHinge";
248  bool m_last_layer_only = true;
249  size_t m_num_test_images = 10000;
250  size_t m_test_batchsize = 0;
251 };
252 
257 class MnistITL : public MnistITLLastLayer {
258 private:
259 public:
260  MnistITL(const std::string backend, size_t bench_index)
261  : MnistITLLastLayer(backend, bench_index, __func__)
262  {
263  m_last_layer_only = false;
264  }
265  std::shared_ptr<SNABBase> clone() override
266  {
267  return std::make_shared<MnistITL>(m_backend, m_bench_index);
268  }
269 };
270 
279 class MnistDoubleCNN : public MNIST_BASE {
280 public:
281  MnistDoubleCNN(const std::string backend, size_t bench_index)
282  : MNIST_BASE(backend, bench_index, __func__)
283  {
284  }
285 
286  std::shared_ptr<SNABBase> clone() override
287  {
288  return std::make_shared<MnistDoubleCNN>(m_backend, m_bench_index);
289  }
290 };
291 
301 class MnistCNNPool : public MNIST_BASE {
302 public:
303  MnistCNNPool(const std::string backend, size_t bench_index)
304  : MNIST_BASE(backend, bench_index, __func__)
305  {
306  }
307 
308  std::shared_ptr<SNABBase> clone() override
309  {
310  return std::make_shared<MnistCNNPool>(m_backend, m_bench_index);
311  }
312 };
313 
314 } // namespace SNAB
315 #endif
MnistNAS129(const std::string backend, size_t bench_index)
Definition: mnist.hpp:162
MnistDoubleCNN(const std::string backend, size_t bench_index)
Definition: mnist.hpp:281
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:205
std::shared_ptr< MNIST::MLPBase > m_mlp
Definition: mnist.hpp:75
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:167
cypress::Real m_pause
Definition: mnist.hpp:51
MnistITL(const std::string backend, size_t bench_index)
Definition: mnist.hpp:260
std::vector< cypress::Network > m_networks
Definition: mnist.hpp:69
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:308
MnistCNNPool(const std::string backend, size_t bench_index)
Definition: mnist.hpp:303
MnistSpikey(const std::string backend, size_t bench_index)
Definition: mnist.hpp:126
std::vector< cypress::PopulationBase > m_label_pops
Definition: mnist.hpp:71
MnistNAS63(const std::string backend, size_t bench_index)
Definition: mnist.hpp:144
cypress::Real m_pool_inhib_weight
Definition: mnist.hpp:59
cypress::Real m_max_weight
Definition: mnist.hpp:56
cypress::Json read_config(const std::string &name, const std::string &backend)
Virtual Base class for SNABs(Benchmarks). All SNABs should have seperate building of networks...
Definition: snab_base.hpp:33
MnistDiehl(const std::string backend, size_t bench_index)
Definition: mnist.hpp:200
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:265
std::vector< cypress::PopulationBase > m_all_pops
Definition: mnist.hpp:81
std::vector< cypress::Real > m_conv_weights_scale_factors
Definition: mnist.hpp:78
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:286
MnistITLLastLayer(const std::string backend, size_t bench_index)
Definition: mnist.hpp:217
virtual std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:226
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:149
size_t m_images
Definition: mnist.hpp:50
Real max_weight(const T &json)
Calculate the max weight, ignore negative values.
cypress::Real m_max_pool_weight
Definition: mnist.hpp:58
std::string m_neuron_type_str
Definition: mnist.hpp:49
std::pair< std::vector< std::vector< std::vector< Real > > >, std::vector< uint16_t > > m_spmnist
Definition: mnist.hpp:239
cypress::Real m_pool_delay
Definition: mnist.hpp:60
MnistITLLastLayer(const std::string backend, size_t bench_index, std::string name)
Definition: mnist.hpp:232
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:131
std::vector< std::pair< std::vector< std::vector< Real > >, std::vector< uint16_t > > > m_batch_data
Definition: mnist.hpp:64
bool m_train_data
Definition: mnist.hpp:54
MnistNAStop(const std::string backend, size_t bench_index)
Definition: mnist.hpp:180
NeuronParameter m_neuro_params
Definition: mnist.hpp:48
std::shared_ptr< SNABBase > clone() override
Virtual method cloning the SNAB without knowing which SNAB it is.
Definition: mnist.hpp:185