SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
Classes | Typedefs | Enumerations | Functions
mnist_helper Namespace Reference

Classes

struct  CONVOLUTION_LAYER
 
struct  POOLING_LAYER
 

Typedefs

typedef std::pair< std::vector< std::vector< Real > >, std::vector< uint16_t > > MNIST_DATA
 
typedef std::pair< std::vector< std::vector< std::vector< Real > > >, std::vector< uint16_t > > SPIKING_MNIST
 
typedef std::vector< std::vector< std::vector< std::vector< Real > > > > CONVOLUTION_FILTER
 
typedef struct CONVOLUTION_LAYER CONVOLUTION_LAYER
 
typedef struct POOLING_LAYER POOLING_LAYER
 

Enumerations

Functions

MNIST_DATA loadMnistData (const size_t num_data, const std::string path)
 Read in MNIST data from files. More...
 
void print_image (const std::vector< Real > &img, size_t wrap)
 Prints image to std::cout. More...
 
std::vector< std::vector< std::vector< Real > > > image_to_rate (const std::vector< std::vector< Real >> &images, const Real duration, const Real max_freq, size_t num_images, bool poisson=true)
 Converts a vector of images to a rate based representation. More...
 
SPIKING_MNIST mnist_to_spike (const MNIST_DATA &mnist_data, const Real duration, const Real max_freq, size_t num_images, bool poisson=true)
 Converts the full MNIST dataset to a spiking MNIST dataset. More...
 
std::vector< MNIST_DATAcreate_batches (const SPIKING_MNIST &mnist_data, const size_t batch_size, Real duration, Real pause, const bool shuffle=false, unsigned seed=0)
 Creates batches of spikes representing the MNIST data. More...
 
cypress::Population< SpikeSourceArray > create_spike_source (Network &netw, const MNIST_DATA &spikes)
 Creates Spike sources in network from spikes. More...
 
cypress::Population< SpikeSourceArray > & update_spike_source (cypress::Population< SpikeSourceArray > &source, const MNIST_DATA &spikes)
 Update Spike sources in network from spikes. More...
 
Json read_network (std::string path, bool msgpack=true)
 Read in the network file from json of msgpack. The Repo provides a script which creates compatible files. More...
 
template<typename T >
Real max_weight (const T &json)
 Calculate the max weight, ignore negative values. More...
 
template<typename T >
Real max_weight (const Matrix< T > &mat)
 Calculate the max weight, ignore negative values. More...
 
template<typename T >
Real min_weight (const T &json)
 Calculate the min weight. More...
 
template<typename T >
Real min_weight (const Matrix< T > &json)
 Calculate the min weight. More...
 
template<typename T >
Real max_weight_abs (const T &json)
 Calculate the max absolute weight. More...
 
template<typename T >
Real max_weight_abs (const Matrix< T > &json)
 Calculate the max absolute weight. More...
 
std::vector< LocalConnection > dense_weights_to_conn (const Matrix< Real > &mat, Real scale, Real delay)
 Convert a dense layer to list of Local Connections. More...
 
std::vector< LocalConnection > conv_weights_to_conn (const mnist_helper::CONVOLUTION_LAYER &layer, Real scale, Real delay)
 Converts a conv layer to list of Local Connections. More...
 
std::vector< std::vector< LocalConnection > > pool_to_conn (const mnist_helper::POOLING_LAYER &layer, Real max_pool_weight, Real pool_inhib_weight, Real delay, Real pool_delay)
 
std::vector< uint16_t > spikes_to_labels (const PopulationBase &pop, Real duration, Real pause, size_t batch_size)
 Converts the simulation results into label data. More...
 
void conv_spikes_per_kernel (const std::string &filename, const PopulationBase &pop, Real duration, Real pause, size_t batch_size, Real norm=0.0)
 Saves the spikes of a convolution layer in a file. More...
 
std::vector< std::vector< Real > > spikes_to_rates (const PopulationBase pop, Real duration, Real pause, size_t batch_size, Real norm=0.0)
 Converts the simulation results into values between 0 and 1. More...
 
size_t compare_labels (std::vector< uint16_t > &label, std::vector< uint16_t > &res)
 Compare original labels with simulation labels, return number of correct labels. More...
 
std::vector< Real > av_pooling_image (std::vector< Real > &image, size_t height, size_t width, size_t pooling_size)
 Downscale an image by average pooling. More...
 
MNIST_DATA scale_mnist (MNIST_DATA &data, size_t pooling_size=3)
 downscale the complete MNIST dataset More...
 
SPIKING_MNIST read_data_to_spike (const size_t num_images, bool train_data, const Real duration, const Real max_freq, bool poisson=true, bool scale_down=false)
 Reads in MNIST test or train data. More...
 
std::vector< LocalConnection > conns_from_mat (const cypress::Matrix< Real > &weights, Real delay, Real scale_factor=0.0)
 Generate connection from given weight matrix. More...
 
void update_conns_from_mat (const std::vector< cypress::Matrix< Real >> &weights, Network &netw, Real delay=1.0, Real scale_factor=0.0)
 Updates the connector is a given network with the weights provided. More...
 

Class Documentation

struct mnist_helper::CONVOLUTION_LAYER

Definition at line 38 of file helper_functions.hpp.

Class Members
CONVOLUTION_FILTER filter
vector< size_t > input_sizes
vector< size_t > output_sizes
size_t padding
size_t stride
struct mnist_helper::POOLING_LAYER

Definition at line 46 of file helper_functions.hpp.

Class Members
vector< size_t > input_sizes
vector< size_t > output_sizes
vector< size_t > size
size_t stride

Typedef Documentation

typedef std::vector<std::vector<std::vector<std::vector<Real> > > > mnist_helper::CONVOLUTION_FILTER

Definition at line 37 of file helper_functions.hpp.

Definition at line 45 of file helper_functions.hpp.

typedef std::pair<std::vector<std::vector<Real> >, std::vector<uint16_t> > mnist_helper::MNIST_DATA

Definition at line 32 of file helper_functions.hpp.

Definition at line 52 of file helper_functions.hpp.

typedef std::pair<std::vector<std::vector<std::vector<Real> > >, std::vector<uint16_t> > mnist_helper::SPIKING_MNIST

Definition at line 35 of file helper_functions.hpp.

Enumeration Type Documentation

Enumerator
Dense 
Conv 
Pooling 

Definition at line 53 of file helper_functions.hpp.

Function Documentation

std::vector<Real> mnist_helper::av_pooling_image ( std::vector< Real > &  image,
size_t  height,
size_t  width,
size_t  pooling_size 
)

Downscale an image by average pooling.

Parameters
imagethe image itself
heightheight of the image
widthwidth of the image
pooling_sizesize of the pooling window (e.g. 2)
Returns
the downscaled image
size_t mnist_helper::compare_labels ( std::vector< uint16_t > &  label,
std::vector< uint16_t > &  res 
)

Compare original labels with simulation labels, return number of correct labels.

Parameters
labeldata label
reslabel from simulation
Returns
number of correct labels
std::vector<LocalConnection> mnist_helper::conns_from_mat ( const cypress::Matrix< Real > &  weights,
Real  delay,
Real  scale_factor = 0.0 
)

Generate connection from given weight matrix.

Parameters
weightsthe weight matrix, type cypress::Matrix<double>
delaysynaptic delay, use 1.0 is unsure
scale_factorscale all weights, do not scale if set to zero
Returns
a list of connections
void mnist_helper::conv_spikes_per_kernel ( const std::string &  filename,
const PopulationBase &  pop,
Real  duration,
Real  pause,
size_t  batch_size,
Real  norm = 0.0 
)

Saves the spikes of a convolution layer in a file.

Parameters
filenamethe file to save to
popthe neuron population to count spikes from
durationpresentation time of a sample
pausepause time in between samples
batch_sizenumber of samples interpreted by these neurons (batch size)
normdivide the number of spikes in bin by this value, ignore if it is set to zero
std::vector<LocalConnection> mnist_helper::conv_weights_to_conn ( const mnist_helper::CONVOLUTION_LAYER layer,
Real  scale,
Real  delay 
)

Converts a conv layer to list of Local Connections.

Parameters
layerstruct of convolution layer information
scalescale factor for weights
delaysynaptic delay
Returns
vector of connections
std::vector<MNIST_DATA> mnist_helper::create_batches ( const SPIKING_MNIST mnist_data,
const size_t  batch_size,
Real  duration,
Real  pause,
const bool  shuffle = false,
unsigned  seed = 0 
)

Creates batches of spikes representing the MNIST data.

Parameters
mnist_dataSpiking MNIST data
batch_sizenumber of images per batch
durationduration of every image
pausetime in between images
shuffleTrue for shuffling images. Defaults to false.
seedSeed for shuffling images Defaults to 0.
Returns
A vector of spike batches, every vector entry gives a container. std::get<0> give spikes for every pixel representing all images in one batch, std::get<1> returns labels
cypress::Population<SpikeSourceArray> mnist_helper::create_spike_source ( Network &  netw,
const MNIST_DATA spikes 
)

Creates Spike sources in network from spikes.

Parameters
netwa cypress network
spikesOne batch from the return value of "create_batch"
Returns
SpikeSourceArray Population
std::vector<LocalConnection> mnist_helper::dense_weights_to_conn ( const Matrix< Real > &  mat,
Real  scale,
Real  delay 
)

Convert a dense layer to list of Local Connections.

Parameters
matcypress matrix of weights
scalescale factor for weights
delaysynaptic delay
Returns
vector of connections
std::vector<std::vector<std::vector<Real> > > mnist_helper::image_to_rate ( const std::vector< std::vector< Real >> &  images,
const Real  duration,
const Real  max_freq,
size_t  num_images,
bool  poisson = true 
)

Converts a vector of images to a rate based representation.

Parameters
imagesvector of images
durationduration of the rate
max_freqmaximal rate/frequency
num_imagesnumber of images to read in
poissonFalse: regular spiking. True: poisson rates. Defaults to true.
Returns
vector (images) of vector (pixel) of spikes
MNIST_DATA mnist_helper::loadMnistData ( const size_t  num_data,
const std::string  path 
)

Read in MNIST data from files.

Parameters
num_dataNumber of images
pathpath to file, without end, e.g. /path/to/data/train for training data, /path/to/data/t10k for test data
Returns
pair, std::get<0> is a vector of images, std::get<1> a vector of labels
template<typename T >
Real mnist_helper::max_weight ( const T &  json)

Calculate the max weight, ignore negative values.

Parameters
jsonjson array containing the weights
Returns
max weight

Definition at line 157 of file helper_functions.hpp.

158 {
159  Real max = 0.0;
160  for (size_t i = 0; i < json.size(); i++) {
161  for (size_t j = 0; j < json[i].size(); j++) {
162  Real tmp = Real(json[i][j]);
163  if (tmp > max) {
164  max = tmp;
165  }
166  }
167  }
168  return max;
169 }
template<typename T >
Real mnist_helper::max_weight ( const Matrix< T > &  mat)

Calculate the max weight, ignore negative values.

Parameters
matcypress Matrix containing the weights
Returns
max weight

Definition at line 178 of file helper_functions.hpp.

179 {
180  Real max = 0.0;
181  for (size_t i = 0; i < mat.size(); i++) {
182  Real tmp = Real(mat[i]);
183  if (tmp > max) {
184  max = tmp;
185  }
186  }
187  return max;
188 }
template<typename T >
Real mnist_helper::max_weight_abs ( const T &  json)

Calculate the max absolute weight.

Parameters
jsonjson array containing the weights
Returns
max weight

Definition at line 237 of file helper_functions.hpp.

238 {
239  Real max = 0.0;
240  for (size_t i = 0; i < json.size(); i++) {
241  for (size_t j = 0; j < json[i].size(); j++) {
242  Real tmp = fabs(Real(json[i][j]));
243  if (tmp > max) {
244  max = tmp;
245  }
246  }
247  }
248  return max;
249 }
template<typename T >
Real mnist_helper::max_weight_abs ( const Matrix< T > &  json)

Calculate the max absolute weight.

Parameters
matcypress Matrix containing the weights
Returns
max weight

Definition at line 258 of file helper_functions.hpp.

259 {
260  Real max = 0.0;
261  for (size_t i = 0; i < json.size(); i++) {
262  Real tmp = fabs(Real(json[i]));
263  if (tmp > max) {
264  max = tmp;
265  }
266  }
267  return max;
268 }
template<typename T >
Real mnist_helper::min_weight ( const T &  json)

Calculate the min weight.

Parameters
jsonjson array containing the weights
Returns
min weight

Definition at line 197 of file helper_functions.hpp.

198 {
199  Real min = 0.0;
200  for (size_t i = 0; i < json.size(); i++) {
201  for (size_t j = 0; j < json[i].size(); j++) {
202  Real tmp = Real(json[i][j]);
203  if (tmp < min) {
204  min = tmp;
205  }
206  }
207  }
208  return min;
209 }
template<typename T >
Real mnist_helper::min_weight ( const Matrix< T > &  json)

Calculate the min weight.

Parameters
matcypress Matrix containing the weights
Returns
min weight

Definition at line 218 of file helper_functions.hpp.

219 {
220  Real min = 0.0;
221  for (size_t i = 0; i < json.size(); i++) {
222  Real tmp = Real(json[i]);
223  if (tmp < min) {
224  min = tmp;
225  }
226  }
227  return min;
228 }
SPIKING_MNIST mnist_helper::mnist_to_spike ( const MNIST_DATA mnist_data,
const Real  duration,
const Real  max_freq,
size_t  num_images,
bool  poisson = true 
)

Converts the full MNIST dataset to a spiking MNIST dataset.

Parameters
mnist_datadata container from loadMnistData
durationduration of spikes per image
max_freqMaximal rate (e.g. for px = 1)
num_imagesnumber of images to read in
poissonFalse: regular spiking. True: poisson rates. Defaults to true.
Returns
pair, std::get<0> is a vector of spiking images, std::get<1> a vector of labels
std::vector<std::vector<LocalConnection> > mnist_helper::pool_to_conn ( const mnist_helper::POOLING_LAYER layer,
Real  max_pool_weight,
Real  pool_inhib_weight,
Real  delay,
Real  pool_delay 
)
Parameters
layerstruct of pooling layer information
max_pool_weightscale factor for pooling weights
pool_inhib_weightscale factor for cross inhibitory weights
delaysynaptic delay
Returns
vector of connections. First the inhibitory connections in the previous layer, then the connections to the pooling layer
void mnist_helper::print_image ( const std::vector< Real > &  img,
size_t  wrap 
)

Prints image to std::cout.

Parameters
imgthe image
wrapline wrap -> width of the image
SPIKING_MNIST mnist_helper::read_data_to_spike ( const size_t  num_images,
bool  train_data,
const Real  duration,
const Real  max_freq,
bool  poisson = true,
bool  scale_down = false 
)

Reads in MNIST test or train data.

Parameters
num_imagesnumber of images to read in
train_datatrue for training data, false for test
durationduration of spikes per image
max_freqMaximal rate (e.g. px = 1)
poissonFalse: regular spiking. True: poisson rates. Defaults to true.
scale_downScales down the image by a factor of 3 in each dim
Returns
pair, std::get<0> is a vector of spiking images, std::get<1> a vector of labels
Json mnist_helper::read_network ( std::string  path,
bool  msgpack = true 
)

Read in the network file from json of msgpack. The Repo provides a script which creates compatible files.

Parameters
pathfull path to file
msgpackTrue: Compressed msgpack. False: plain Json. Defaults to true.
Returns
The json containing the json from file
MNIST_DATA mnist_helper::scale_mnist ( MNIST_DATA data,
size_t  pooling_size = 3 
)

downscale the complete MNIST dataset

Parameters
dataThe MNIST dataset in a container
Returns
downscaled MNIST dataset in a container
std::vector<uint16_t> mnist_helper::spikes_to_labels ( const PopulationBase &  pop,
Real  duration,
Real  pause,
size_t  batch_size 
)

Converts the simulation results into label data.

Parameters
popthe label population
durationpresentation time of a sample
pausepause time in between samples
batch_sizenumber of samples interpreted by these neurons (batch size)
Returns
a vector of labels, for one hot coded neurons, if two neurons had the same activation or there was no activation at all, std::numeric_limits<uint16_t>::max() is returned
std::vector<std::vector<Real> > mnist_helper::spikes_to_rates ( const PopulationBase  pop,
Real  duration,
Real  pause,
size_t  batch_size,
Real  norm = 0.0 
)

Converts the simulation results into values between 0 and 1.

Parameters
popthe population to be converted to rates
durationpresentation time of a sample
pausepause time in between samples
batch_sizenumber of samples interpreted by these neurons (batch size) norm divide the number of spikes in bin by this value, ignore if it is set to zero
Returns
for every sample a vector of rates for every neuron (vec[sample][neuron])
void mnist_helper::update_conns_from_mat ( const std::vector< cypress::Matrix< Real >> &  weights,
Network &  netw,
Real  delay = 1.0,
Real  scale_factor = 0.0 
)

Updates the connector is a given network with the weights provided.

Parameters
weightsthe new weight in weights[layer](input, output)
netwthe network to alter
delaysynaptic delay Defaults to 1.0.
scale_factorScales the weights during conversion, no scale if set to zero
cypress::Population<SpikeSourceArray>& mnist_helper::update_spike_source ( cypress::Population< SpikeSourceArray > &  source,
const MNIST_DATA spikes 
)

Update Spike sources in network from spikes.

Parameters
sourcea cypress source array
spikesOne batch from the return value of "create_batch"
Returns
SpikeSourceArray Population