Skip to content

Commit 9b1870f

Browse files
committed
Moved generators: KasperskyLab#83
1 parent 667def6 commit 9b1870f

4 files changed

Lines changed: 204 additions & 149 deletions

File tree

examples/mnist-learn/construct_network.cpp

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
#include <knp/neuron-traits/all_traits.h>
2828
#include <knp/synapse-traits/all_traits.h>
2929

30-
#include "generators.h"
31-
3230
// A list of short type names to make reading easier.
31+
using DeltaSynapseData = knp::synapse_traits::synapse_parameters<knp::synapse_traits::DeltaSynapse>;
32+
using DeltaProjection = knp::core::Projection<knp::synapse_traits::DeltaSynapse>;
33+
using ResourceSynapse = knp::synapse_traits::SynapticResourceSTDPDeltaSynapse;
34+
using ResourceDeltaProjection = knp::core::Projection<knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>;
35+
using ResourceSynapseData = ResourceDeltaProjection::Synapse;
36+
using ResourceSynapseParams = knp::synapse_traits::synapse_parameters<ResourceSynapse>;
3337
using BlifatPopulation = knp::core::Population<knp::neuron_traits::BLIFATNeuron>;
3438
using ResourceBlifatPopulation = knp::core::Population<knp::neuron_traits::SynapticResourceSTDPBLIFATNeuron>;
3539
using ResourceNeuron = knp::neuron_traits::SynapticResourceSTDPBLIFATNeuron;
@@ -168,90 +172,118 @@ AnnotatedNetwork create_example_network(int num_compound_networks)
168172
afferent_synapse.rule_.w_min_ = min_synaptic_weight;
169173
afferent_synapse.rule_.w_max_ = max_synaptic_weight;
170174

175+
171176
// 1. Trainable input projection.
172177
ResourceDeltaProjection input_projection{
173-
knp::core::UID{false}, population_uids[INPUT], make_dense_generator(input_size, afferent_synapse),
178+
knp::core::UID{false}, population_uids[INPUT],
179+
knp::framework::projection::synapse_generators::all_to_all<ResourceSynapse>(
180+
input_size, num_input_neurons, [&](size_t, size_t) { return afferent_synapse; }),
174181
input_projection_size};
175182
result.data_.projections_from_raster_.push_back(input_projection.get_uid());
176183
input_projection.unlock_weights(); // Trainable
177184
result.network_.add_projection(input_projection);
178185
result.data_.inference_internal_projection_.insert(input_projection.get_uid());
179186

180-
default_synapse.weight_ = 9;
181187

182188
// 2. Activating projection. It sends signals from labels to dopamine population.
183189
const DeltaSynapseData default_activating_synapse{1, 1, knp::synapse_traits::OutputType::BLOCKING};
190+
auto projection_2_generator_functor =
191+
knp::framework::projection::synapse_generators::Aligned<knp::synapse_traits::DeltaSynapse>(
192+
pop_data[INPUT].pd_.size_, pop_data[DOPAMINE].pd_.size_,
193+
[&](size_t, size_t) { return default_activating_synapse; });
184194
DeltaProjection projection_2{
185-
knp::core::UID{false}, population_uids[DOPAMINE],
186-
make_aligned_generator(pop_data[INPUT].pd_.size_, pop_data[DOPAMINE].pd_.size_, default_activating_synapse),
187-
pop_data[INPUT].pd_.size_};
195+
knp::core::UID{false}, population_uids[DOPAMINE], projection_2_generator_functor,
196+
projection_2_generator_functor.suggested_synapses_amount()};
188197
result.network_.add_projection(projection_2);
189198
result.data_.wta_data_[i].second.push_back(projection_2.get_uid());
190199

200+
191201
// 3. Dopamine projection, it goes from dopamine population to input population.
192202
const DeltaSynapseData default_dopamine_synapse{dopamine_value, 1, knp::synapse_traits::OutputType::DOPAMINE};
203+
auto projection_3_generator_functor =
204+
knp::framework::projection::synapse_generators::Aligned<knp::synapse_traits::DeltaSynapse>(
205+
pop_data[DOPAMINE].pd_.size_, pop_data[INPUT].pd_.size_,
206+
[&](size_t, size_t) { return default_dopamine_synapse; });
193207
DeltaProjection projection_3{
194-
population_uids[DOPAMINE], population_uids[INPUT],
195-
make_aligned_generator(pop_data[DOPAMINE].pd_.size_, pop_data[INPUT].pd_.size_, default_dopamine_synapse),
196-
pop_data[INPUT].pd_.size_};
197-
208+
population_uids[DOPAMINE], population_uids[INPUT], projection_3_generator_functor,
209+
projection_3_generator_functor.suggested_synapses_amount()};
198210
result.network_.add_projection(projection_3);
199211
result.data_.inference_internal_projection_.insert(projection_3.get_uid());
200212

213+
201214
// 4. Strong excitatory projection going to output neurons.
215+
default_synapse.weight_ = 9;
216+
auto projection_4_generator_functor =
217+
knp::framework::projection::synapse_generators::Aligned<knp::synapse_traits::DeltaSynapse>(
218+
pop_data[INPUT].pd_.size_, pop_data[OUTPUT].pd_.size_, [&](size_t, size_t) { return default_synapse; });
202219
DeltaProjection projection_4{
203-
knp::core::UID{false}, population_uids[OUTPUT],
204-
make_aligned_generator(pop_data[INPUT].pd_.size_, pop_data[OUTPUT].pd_.size_, default_synapse),
205-
pop_data[INPUT].pd_.size_};
220+
knp::core::UID{false}, population_uids[OUTPUT], projection_4_generator_functor,
221+
projection_4_generator_functor.suggested_synapses_amount()};
206222
result.data_.wta_data_[i].second.push_back(projection_4.get_uid());
207-
208223
result.network_.add_projection(projection_4);
209224
result.data_.inference_internal_projection_.insert(projection_4.get_uid());
210225

226+
211227
// 5. Blocking projection.
212228
const DeltaSynapseData default_blocking_synapse{-20, 1, knp::synapse_traits::OutputType::BLOCKING};
229+
auto projection_5_generator_functor =
230+
knp::framework::projection::synapse_generators::Aligned<knp::synapse_traits::DeltaSynapse>(
231+
pop_data[OUTPUT].pd_.size_, pop_data[GATE].pd_.size_,
232+
[&](size_t, size_t) { return default_blocking_synapse; });
213233
DeltaProjection projection_5{
214-
population_uids[OUTPUT], population_uids[GATE],
215-
make_aligned_generator(pop_data[OUTPUT].pd_.size_, pop_data[GATE].pd_.size_, default_blocking_synapse),
216-
num_possible_labels};
234+
population_uids[OUTPUT], population_uids[GATE], projection_5_generator_functor,
235+
projection_5_generator_functor.suggested_synapses_amount()};
217236
result.network_.add_projection(projection_5);
218237
result.data_.inference_internal_projection_.insert(projection_5.get_uid());
219238

239+
220240
// 6. Strong excitatory projection going from ground truth classes.
241+
auto projection_6_generator_functor =
242+
knp::framework::projection::synapse_generators::Aligned<knp::synapse_traits::DeltaSynapse>(
243+
num_possible_labels, pop_data[DOPAMINE].pd_.size_, [&](size_t, size_t) { return default_synapse; });
221244
DeltaProjection projection_6{
222-
knp::core::UID{false}, population_uids[DOPAMINE],
223-
make_aligned_generator(num_possible_labels, pop_data[DOPAMINE].pd_.size_, default_synapse),
224-
pop_data[DOPAMINE].pd_.size_};
245+
knp::core::UID{false}, population_uids[DOPAMINE], projection_6_generator_functor,
246+
projection_6_generator_functor.suggested_synapses_amount()};
225247
result.network_.add_projection(projection_6);
226248
result.data_.projections_from_classes_.push_back(projection_6.get_uid());
227249

250+
228251
// 7. Strong slow excitatory projection going from ground truth classes.
229252
auto slow_synapse = default_synapse;
230253
slow_synapse.delay_ = 10;
254+
auto projection_7_generator_functor =
255+
knp::framework::projection::synapse_generators::Aligned<knp::synapse_traits::DeltaSynapse>(
256+
num_possible_labels, pop_data[GATE].pd_.size_, [&](size_t, size_t) { return slow_synapse; });
231257
DeltaProjection projection_7{
232-
knp::core::UID{false}, population_uids[GATE],
233-
make_aligned_generator(num_possible_labels, pop_data[GATE].pd_.size_, slow_synapse),
234-
pop_data[GATE].pd_.size_};
258+
knp::core::UID{false}, population_uids[GATE], projection_7_generator_functor,
259+
projection_7_generator_functor.suggested_synapses_amount()};
235260
result.network_.add_projection(projection_7);
236261
result.data_.projections_from_classes_.push_back(projection_7.get_uid());
237262

263+
238264
// 8. Strong inhibitory projection from ground truth input.
239265
auto inhibitory_synapse = default_synapse;
240266
inhibitory_synapse.weight_ = -30;
267+
auto projection_8_generator_functor =
268+
knp::framework::projection::synapse_generators::Exclusive<knp::synapse_traits::DeltaSynapse>(
269+
num_possible_labels, [&](size_t, size_t) { return inhibitory_synapse; });
241270
DeltaProjection projection_8{
242-
knp::core::UID{false}, population_uids[GATE],
243-
make_exclusive_generator(num_possible_labels, inhibitory_synapse),
244-
num_possible_labels * (pop_data[GATE].pd_.size_ - 1)};
271+
knp::core::UID{false}, population_uids[GATE], projection_8_generator_functor,
272+
projection_8_generator_functor.suggested_synapses_amount()};
245273
result.data_.projections_from_classes_.push_back(projection_8.get_uid());
246274
result.network_.add_projection(projection_8);
247275

276+
248277
// 9. Weak excitatory projection.
249278
auto weak_excitatory_synapse = default_synapse;
250279
weak_excitatory_synapse.weight_ = 3;
280+
auto projection_9_generator_functor =
281+
knp::framework::projection::synapse_generators::Aligned<knp::synapse_traits::DeltaSynapse>(
282+
pop_data[GATE].pd_.size_, pop_data[INPUT].pd_.size_,
283+
[&](size_t, size_t) { return weak_excitatory_synapse; });
251284
DeltaProjection projection_9{
252-
population_uids[GATE], population_uids[INPUT],
253-
make_aligned_generator(pop_data[GATE].pd_.size_, pop_data[INPUT].pd_.size_, weak_excitatory_synapse),
254-
pop_data[INPUT].pd_.size_};
285+
population_uids[GATE], population_uids[INPUT], projection_9_generator_functor,
286+
projection_9_generator_functor.suggested_synapses_amount()};
255287
result.network_.add_projection(projection_9);
256288
result.data_.inference_internal_projection_.insert(projection_9.get_uid());
257289
}

examples/mnist-learn/generators.h

Lines changed: 0 additions & 90 deletions
This file was deleted.

examples/mnist-learn/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ int main(int argc, char** argv)
6565
steps_per_image, active_steps, image_size, state_increment_factor,
6666
std::vector<float>(image_size, 0.f)));
6767

68+
dataset.steps_required_for_training_ = 10000;
69+
dataset.steps_required_for_inference_ = 1000;
70+
6871
std::cout << "Processed dataset, training will last " << dataset.steps_required_for_training_
6972
<< " steps, inference " << dataset.steps_required_for_inference_ << " steps" << std::endl;
7073

0 commit comments

Comments
 (0)