/
Unit.cpp
167 lines (134 loc) · 3.84 KB
/
Unit.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Copyright 2012 Eugen Hristev Licensed under the
Educational Community License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may
obtain a copy of the License at
http://www.osedu.org/licenses/ECL-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
#include "Unit.h"
#include "narx_util.h"
#include "stdlib.h"
#include "assert.h"
#include "Activation_functions.h"
double Unit::alfa = 0.2;
Unit::Unit(void)
{
input_area = new Unit * [MAX_INPUTS_PER_UNIT];
input_count = 0;
input_weights = new double[MAX_INPUTS_PER_UNIT];
for(int i = 0; i < MAX_INPUTS_PER_UNIT; i ++) {
input_weights[i] = (double) (rand() % 100) / 100;
}
bias = (double) (rand() % 100) / 100;
old_weights = new double[MAX_INPUTS_PER_UNIT];
for(int i = 0; i < MAX_INPUTS_PER_UNIT; i ++) {
old_weights[i] = input_weights[i];
}
output = 0;
}
Unit::~Unit(void)
{
delete [] input_area;
delete [] input_weights;
delete [] old_weights;
}
void Unit::set_activation_func( double (*f) (double arg))
{
activation_func = f;
}
void Unit::set_activation_func_derv( double (*f) (double arg))
{
activation_func_derv = f;
}
int Unit::add_input_unit (Unit *unit)
{
if (input_count >= MAX_INPUTS_PER_UNIT) {
return -1;
}
input_weights[input_count] = (double) (rand() % 100) / 100;
input_area[input_count] = unit;
//printf("%f\n", input_weights[input_count]);
//FLOG(QString("input count=%1").arg(input_count).toStdString().c_str());
return input_count++;
}
double Unit::pre_output()
{
double preoutput = 0;
for (int i = 0; i < input_count; i++) {
//FLOG(QString("input:%1\n").arg(input_area[i]->get_output()).toStdString().c_str());
preoutput += input_area[i]->get_output() * input_weights[i];
}
//if(activation_func == Activation_functions::aslog)
//FLOG(QString("unit preoutput:%1\n").arg(preoutput).toStdString().c_str());
// if (activation_func == Activation_functions::identity)
// FLOG(QString("output unit preoutput:%1\n").arg(preoutput).toStdString().c_str());
preoutput += bias;
//FLOG(QString("output unit preoutput:%1\n").arg(preoutput).toStdString().c_str());
return preoutput;
}
void Unit::compute_output()
{
output = activation_func(pre_output());
}
double Unit::get_output()
{
compute_output();
return output;
}
void Unit::compute_delta(double superior_layer_delta)
{
deltah = activation_func_derv(pre_output()) * superior_layer_delta ; // * get_output();
}
void Unit::adjust_weights()
{
for(int i = 0; i < input_count; i ++) {
input_weights[i] += Unit::alfa * deltah * input_area[i]->get_output();
//FLOG(QString("ok adjust=%1:%2\n").arg( pre_output() ) .arg(activation_func_derv(pre_output())).toStdString().c_str());
}
}
double *Unit::weights()
{
return input_weights;
}
int Unit::inputcount()
{
return input_count;
}
void Unit::copy(Unit *u)
{
for(int i = 0; i < input_count; i++) {
input_weights[i] = u->input_weights[i];
}
}
void Unit::sum(Unit *u)
{
for(int i = 0; i < input_count; i++) {
input_weights[i] += u->input_weights[i];
}
}
void Unit::divide(int len)
{
for(int i = 0; i < input_count; i++) {
input_weights[i] /= len;
}
}
double Unit::get_delta(Unit *u)
{
for (int i = 0; i < input_count; i++)
if (u == input_area[i]) {
return deltah * old_weights[i];
}
assert(false);
return 0;
}
void Unit::fix_weights()
{
for(int i = 0; i < input_count; i ++) {
old_weights[i] = input_weights[i];
}
}