-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron_row.cpp
86 lines (77 loc) · 2.22 KB
/
neuron_row.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Copyright (c) 2013 Michael LeSane
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
//initializes a neuron row given a vector of neurons.
neuron_row::neuron_row(std::vector<neuron> init,int out)
{
this->contents = init;
this->out = out;
}
//initializes a neuron row given various parameters.
neuron_row::neuron_row(int neurons, int out, int bias, int count_activations, int random)
{
std::vector<neuron> init;
for(int i = 0; i < neurons; i++){
init.push_back(neuron(0,count_activations,random));
init[i].out = out;
}
if(bias) init.push_back(neuron(1,count_activations,random));
this->contents = init;
this->out = out;
}
//forward pass
std::vector<neuron> run_new_contents
(
hpx::lcos::future<std::vector<neuron>> prev_row,
std::vector<neuron> current_row,
int serial
)
{
return hpx::lcos::local::dataflow
(
hpx::util::unwrapped
( [] (std::vector<neuron> roots, std::vector<neuron> current, int serial)
{
for(int i = 0; i < (int)current.size(); i++)
current[i].run(roots,serial);
return current;
}
),prev_row,hpx::lcos::make_ready_future(current_row),hpx::lcos::make_ready_future((int)serial)
).get();
}
void neuron_row::finalize_run()
{
this->contents = this->new_contents.get();
}
void neuron_row::run(neuron_row prev,int serial)
{
//New Implementation
/*if(!serial)
{
this->new_contents = hpx::async(&run_new_contents,prev.new_contents,this->contents,serial);
//this->new_contents = hpx::lcos::make_ready_future(this->contents);
return;
}/**/
//Old Implementation
std::vector<neuron> roots = prev.contents;
for(int i = 0; i < (int)this->contents.size(); i++)
this->contents[i].run(roots,serial);
}
//adds a neuron to the row. weights of next row not affected, please eventually fix this.
void neuron_row::add(neuron x)
{
contents.push_back(x);
}
//returns the number of neurons in the row.
int neuron_row::size()
{
return this->contents.size();
}
//backpropagation
void neuron_row::correct(std::vector<float> v, float m, float n,neuron_row prev,neuron_row next,int serial)
{
for(int j = 0; j < (int)this->size(); j++)
this->contents[j].correct((this->out)?v[j]:0,j,m,n,prev,next,serial);
}