Commit 2ae164de authored by K-H-Ismail's avatar K-H-Ismail Committed by Gauthier Quesnel
Browse files

auditory: add second layer

parent eb505273
......@@ -125,4 +125,5 @@ if (NOT BUILD_SHARED_LIBS)
irritator_add_test(test-api test/public-api.cpp)
irritator_add_test(simulations test/simulations.cpp)
irritator_add_test(auditory test/auditory.cpp)
irritator_add_test(benchmark benchmark/benchmark.cpp)
endif ()
......@@ -12,6 +12,8 @@
#include <chrono>
#include <hayai/hayai.hpp>
static void
dot_graph_save(const irt::simulation& sim, std::FILE* os)
{
......@@ -208,7 +210,7 @@ make_neuron(irt::simulation* sim, long unsigned int i) noexcept
}
struct synapse make_synapse(irt::simulation* sim, long unsigned int source, long unsigned int target,
irt::output_port_id presynaptic,irt::output_port_id postsynaptic)
irt::output_port_id presynaptic,irt::output_port_id postsynaptic,double quantum)
{
using namespace boost::ut;
double taupre = 20.0*1;
......@@ -240,7 +242,7 @@ struct synapse make_synapse(irt::simulation* sim, long unsigned int source, long
quant_pre.default_adapt_state =
irt::quantifier::adapt_state::possible;
quant_pre.default_zero_init_offset = true;
quant_pre.default_step_size = 1e-5;
quant_pre.default_step_size = quantum;
quant_pre.default_past_length = 3;
sum_pre.default_input_coeffs[0] = 1.0;
sum_pre.default_input_coeffs[1] = dApre;
......@@ -252,7 +254,7 @@ struct synapse make_synapse(irt::simulation* sim, long unsigned int source, long
quant_post.default_adapt_state =
irt::quantifier::adapt_state::possible;
quant_post.default_zero_init_offset = true;
quant_post.default_step_size = 1e-5;
quant_post.default_step_size = quantum;
quant_post.default_past_length = 3;
sum_post.default_input_coeffs[0] = 1.0;
sum_post.default_input_coeffs[1] = dApost;
......@@ -384,124 +386,96 @@ struct synapse make_synapse(irt::simulation* sim, long unsigned int source, long
return synapse_model;
}
int
main()
void network(long unsigned int N, double simulation_duration, double quantum)
{
using namespace boost::ut;
using namespace boost::ut;
"song_1_simulation"_test = [] {
irt::simulation sim;
// Neuron constants
long unsigned int N = 10;
/*double F = 15.0;
double Eex = 0.0;
double Ein = -70*0.001;
double tauex = 5*0.001;
double tauin = tauex;*/
// Synapse constants
irt::simulation sim;
// Neuron constants
//long unsigned int N = 10;
//double ginbar = 0.05;
expect(irt::is_success(sim.init(100000000lu, 10000000lu)));
expect(irt::is_success(sim.init(100000000lu, 10000000lu)));
expect(sim.generator_models.can_alloc(N));
expect(sim.integrator_models.can_alloc(2*N*N));
expect(sim.quantifier_models.can_alloc(2*N*N));
expect(sim.adder_2_models.can_alloc(4*N*N));
expect(sim.constant_models.can_alloc(N));
expect(sim.cross_models.can_alloc(2*N*N));
//struct neuron neuron_model0 = make_neuron(&sim,0lu);
printf(">> Allocating neurones ... ");
auto start = std::chrono::steady_clock::now();
// Neurons
std::vector<irt::dynamics_id> generators;
for (long unsigned int i = 0 ; i < N; i++) {
auto& gen = sim.generator_models.alloc();
gen.default_value = 3.0;
gen.default_offset = i+1;
gen.default_period = N+1;
//struct neuron neuron_model0 = make_neuron(&sim,0lu);
!expect(irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
printf(">> Allocating neurones ... ");
auto start = std::chrono::steady_clock::now();
// Neurons
std::vector<irt::dynamics_id> generators;
for (long unsigned int i = 0 ; i < N; i++) {
auto& gen = sim.generator_models.alloc();
gen.default_value = 3.0;
gen.default_offset = i+1;
gen.default_period = N+1;
!expect(irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
generators.emplace_back(sim.generator_models.get_id(gen));
}
auto end = std::chrono::steady_clock::now();
printf(" [%f] ms.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()));
printf(">> Allocating synapses ... ");
start = std::chrono::steady_clock::now();
std::vector<struct synapse> synapses;
for (long unsigned int i = 0 ; i < N; i++) {
for (long unsigned int j = 0 ; j < N; j++) {
struct synapse synapse_model = make_synapse(&sim,i,j,
sim.generator_models.get(generators[i]).y[0],
sim.generator_models.get(generators[j]).y[0]);
synapses.emplace_back(synapse_model);
}
generators.emplace_back(sim.generator_models.get_id(gen));
}
auto end = std::chrono::steady_clock::now();
printf(" [%f] ms.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()));
printf(">> Allocating synapses ... ");
start = std::chrono::steady_clock::now();
std::vector<struct synapse> synapses;
synapses.reserve(256*N);
for (long unsigned int i = 0 ; i < N; i++) {
for (long unsigned int j = 0 ; j < N; j++) {
struct synapse synapse_model = make_synapse(&sim,i,j,
sim.generator_models.get(generators[i]).y[0],
sim.generator_models.get(generators[j]).y[0],quantum);
synapses.emplace_back(synapse_model);
}
end = std::chrono::steady_clock::now();
printf(" [%f] s.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::seconds>(end - start).count()));
//dot_graph_save(sim, stdout);
}
end = std::chrono::steady_clock::now();
printf(" [%f] s.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::seconds>(end - start).count()));
printf(">> synapses size %ld \n",synapses.capacity());
irt::time t = 0.0;
/*std::FILE* os = std::fopen("output_song.csv", "w");
!expect(os != nullptr);
std::string s = "t,";
for (long unsigned int i = 0; i < N*N; i++)
{
s = s + "Apre" + std::to_string(i)
+ ",Apost" + std::to_string(i)
+ ",";
}
for (long unsigned int i = 0; i < N*N; i++)
s = s + "W" + std::to_string(i) + ",";
fmt::print(os, s + "\n");*/
printf(">> Initializing simulation ... \n");
start = std::chrono::steady_clock::now();
expect(irt::status::success == sim.initialize(t));
end = std::chrono::steady_clock::now();
printf(">> Simulation initialized in : %f ms.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()));
printf(">> Start running ... \n");
start = std::chrono::steady_clock::now();
do {
irt::status st = sim.run(t);
expect(st == irt::status::success);
/*std::string s = std::to_string(t)+",";
for (long unsigned int i = 0; i < N*N; i++)
{
s = s + std::to_string(sim.integrator_models.get(synapses[i].integrator_pre).last_output_value)
+ ","
+ std::to_string(sim.integrator_models.get(synapses[i].integrator_post).last_output_value)
+ ",";
}
for (long unsigned int i = 0; i < N*N; i++)
s = s + std::to_string(sim.accumulator_2_models.get(synapses[i].accumulator_syn).number) + ",";
fmt::print(os, s + "\n");*/
irt::time t = 0.0;
} while (t < N);
end = std::chrono::steady_clock::now();
printf(">> Simulation done in : %f s.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::seconds>(end - start).count()));
//std::fclose(os);
};
printf(">> Initializing simulation ... \n");
start = std::chrono::steady_clock::now();
expect(irt::status::success == sim.initialize(t));
end = std::chrono::steady_clock::now();
printf(">> Simulation initialized in : %f ms.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()));
printf(">> Start running ... \n");
start = std::chrono::steady_clock::now();
do {
irt::status st = sim.run(t);
expect(st == irt::status::success);
} while (t < simulation_duration);
end = std::chrono::steady_clock::now();
printf(">> Simulation done in : %f s.\n" ,static_cast<double>(std::chrono::duration_cast<std::chrono::seconds>(end - start).count()));
}
BENCHMARK_P(Network, N, 10, 1,(long unsigned int N, double simulation_duration, double quantum))
{
network(N,simulation_duration,quantum);
}
BENCHMARK_P_INSTANCE(Network, N, (10,30,1e-5));
BENCHMARK_P_INSTANCE(Network, N, (100,30,1e-5));
BENCHMARK_P_INSTANCE(Network, N, (500,30,1e-5));
int
main()
{
hayai::ConsoleOutputter consoleOutputter;
hayai::Benchmarker::AddOutputter(consoleOutputter);
hayai::Benchmarker::RunAllTests();
}
......@@ -155,14 +155,12 @@ struct neuron {
struct neuron_adaptive {
irt::dynamics_id sum1;
irt::dynamics_id sum2;
irt::dynamics_id sum3;
irt::dynamics_id integrator1;
irt::dynamics_id integrator2;
irt::dynamics_id quantifier1;
irt::dynamics_id quantifier2;
irt::dynamics_id constant;
irt::dynamics_id cross1;
irt::dynamics_id cross2;
irt::dynamics_id constant_cross;
};
......@@ -268,11 +266,11 @@ struct neuron_adaptive
make_neuron_adaptive(irt::simulation* sim, long unsigned int i) noexcept
{
using namespace boost::ut;
double tau_lif = 10*0.001;
double tau_lif = 10;
double Vr_lif = 0.0;
double Vt_lif = 10.0;
double tau_threshold = 15*0.001;
double tau_threshold = 15;
auto& sum_lif = sim->adder_2_models.alloc();
......@@ -281,12 +279,10 @@ make_neuron_adaptive(irt::simulation* sim, long unsigned int i) noexcept
auto& constant_cross_lif = sim->constant_models.alloc();
auto& cross_lif = sim->cross_models.alloc();
auto& sum_threshold = sim->adder_2_models.alloc();
auto& sum_threshold = sim->adder_3_models.alloc();
auto& integrator_threshold = sim->integrator_models.alloc();
auto& quantifier_threshold = sim->quantifier_models.alloc();
auto& cross_threshold = sim->cross_models.alloc();
auto& sum_reset = sim->adder_2_models.alloc();
auto& constant = sim->constant_models.alloc();
//LIF
......@@ -309,20 +305,16 @@ make_neuron_adaptive(irt::simulation* sim, long unsigned int i) noexcept
//Threshold
sum_threshold.default_input_coeffs[0] = -1.0/tau_threshold;
sum_threshold.default_input_coeffs[1] = 10.0/tau_threshold;
sum_threshold.default_input_coeffs[2] = 1.0/tau_threshold;
integrator_threshold.default_current_value = Vt_lif;
integrator_threshold.default_current_value = Vr_lif;
quantifier_threshold.default_adapt_state =
irt::quantifier::adapt_state::possible;
quantifier_threshold.default_zero_init_offset = true;
quantifier_threshold.default_step_size = 0.1;
quantifier_threshold.default_past_length = 3;
cross_threshold.default_threshold = Vt_lif;
sum_reset.default_input_coeffs[0] = 1.0;
sum_reset.default_input_coeffs[1] = 3.0;
constant.default_value = 1.0;
......@@ -332,24 +324,20 @@ make_neuron_adaptive(irt::simulation* sim, long unsigned int i) noexcept
sim->alloc(cross_lif, sim->cross_models.get_id(cross_lif));
sim->alloc(constant_cross_lif, sim->constant_models.get_id(constant_cross_lif));
sim->alloc(sum_threshold, sim->adder_2_models.get_id(sum_threshold));
sim->alloc(sum_threshold, sim->adder_3_models.get_id(sum_threshold));
sim->alloc(integrator_threshold, sim->integrator_models.get_id(integrator_threshold));
sim->alloc(quantifier_threshold, sim->quantifier_models.get_id(quantifier_threshold));
sim->alloc(cross_threshold, sim->cross_models.get_id(cross_threshold));
sim->alloc(sum_reset, sim->adder_2_models.get_id(sum_reset));
sim->alloc(constant, sim->constant_models.get_id(constant));
struct neuron_adaptive neuron_model = {sim->adder_2_models.get_id(sum_lif),
sim->adder_2_models.get_id(sum_threshold),
sim->adder_2_models.get_id(sum_reset),
sim->adder_3_models.get_id(sum_threshold),
sim->integrator_models.get_id(integrator_lif),
sim->integrator_models.get_id(integrator_threshold),
sim->quantifier_models.get_id(quantifier_lif),
sim->quantifier_models.get_id(quantifier_threshold),
sim->constant_models.get_id(constant),
sim->cross_models.get_id(cross_lif),
sim->cross_models.get_id(cross_threshold),
sim->cross_models.get_id(cross_lif),
sim->constant_models.get_id(constant_cross_lif),
};
......@@ -374,34 +362,23 @@ make_neuron_adaptive(irt::simulation* sim, long unsigned int i) noexcept
expect(sim->connect(quantifier_threshold.y[0], integrator_threshold.x[0]) ==
irt::status::success);
expect(sim->connect(cross_threshold.y[0], integrator_threshold.x[2]) ==
irt::status::success);
expect(sim->connect(cross_threshold.y[0], quantifier_threshold.x[0]) ==
irt::status::success);
expect(sim->connect(cross_threshold.y[0], sum_threshold.x[0]) ==
expect(sim->connect(integrator_threshold.y[0], quantifier_threshold.x[0]) ==
irt::status::success);
expect(sim->connect(integrator_lif.y[0],cross_threshold.x[0]) ==
irt::status::success);
expect(sim->connect(integrator_threshold.y[0],cross_threshold.x[2]) ==
expect(sim->connect(integrator_threshold.y[0], sum_threshold.x[0]) ==
irt::status::success);
expect(sim->connect(sum_reset.y[0],cross_threshold.x[1]) ==
irt::status::success);
expect(sim->connect(integrator_threshold.y[0],sum_reset.x[0]) ==
irt::status::success);
expect(sim->connect(constant.y[0],sum_reset.x[1]) ==
irt::status::success);
expect(sim->connect(sum_threshold.y[0],integrator_threshold.x[1]) ==
irt::status::success);
expect(sim->connect(constant.y[0],sum_lif.x[1]) ==
irt::status::success);
expect(sim->connect(constant.y[0],sum_threshold.x[1]) ==
irt::status::success);
irt::status::success);
expect(sim->connect(integrator_lif.y[0],sum_threshold.x[2]) ==
irt::status::success);
expect(sim->connect(integrator_threshold.y[0],cross_lif.x[3]) ==
irt::status::success);
expect(sim->connect(integrator_threshold.y[0],cross_threshold.x[3]) ==
irt::status::success);
return neuron_model;
}
int
......@@ -415,6 +392,7 @@ main()
// Neuron constants
long unsigned int N = 1;
expect(irt::is_success(sim.init(2048lu, 800lu)));
/*long unsigned int N = sound_data.size() - 1;
......@@ -448,6 +426,8 @@ main()
for (long unsigned int i = 0; i < N; i++)
{
s = s + "Neuron" + std::to_string(i)
+ ","
+ "Neuront" + std::to_string(i)
+ ",";
}
fmt::print(os, s + "\n");
......@@ -455,6 +435,8 @@ main()
expect(irt::status::success == sim.initialize(t));
do {
//printf("%f\n",t);
irt::status st = sim.run(t);
expect(st == irt::status::success);
......@@ -464,13 +446,15 @@ main()
{
//s = s + std::to_string(sim.cross_models.get(first_layer_neurons[i].cross).event)
s = s + std::to_string(sim.integrator_models.get(second_layer_neurons[i].integrator1).last_output_value)
+ ",";
+ ","
+ std::to_string(sim.integrator_models.get(second_layer_neurons[i].integrator2).last_output_value)
+ ",";
}
fmt::print(os, s + "\n");
} while (t < 200);
} while (t < 100);
//} while (t < sound_data[0].size()/samplerate);
std::fclose(os);
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment