Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
lib4neuro
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MolDyn
lib4neuro
Commits
6d69861a
Commit
6d69861a
authored
5 years ago
by
Martin Beseda
Browse files
Options
Downloads
Patches
Plain Diff
WIP: Removing memory leaks
parent
5e978596
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Network/NeuralNetwork.cpp
+53
-38
53 additions, 38 deletions
src/Network/NeuralNetwork.cpp
src/Network/NeuralNetwork.h
+5
-2
5 additions, 2 deletions
src/Network/NeuralNetwork.h
with
58 additions
and
40 deletions
src/Network/NeuralNetwork.cpp
+
53
−
38
View file @
6d69861a
...
...
@@ -19,11 +19,10 @@ namespace lib4neuro {
NeuralNetwork
::
NeuralNetwork
()
{
this
->
neurons
=
new
::
std
::
vector
<
Neuron
*>
(
0
);
this
->
neuron_biases
=
new
::
std
::
vector
<
double
>
(
0
);
this
->
neuron_potentials
=
new
::
std
::
vector
<
double
>
(
0
);
this
->
neuron_bias_indices
=
new
::
std
::
vector
<
int
>
(
0
);
this
->
connection_weights
=
new
::
std
::
vector
<
double
>
(
0
);
this
->
connection_list
=
new
::
std
::
vector
<
ConnectionFunctionGeneral
*
>
(
0
);
this
->
connection_list
=
::
std
::
vector
<
std
::
shared_ptr
<
ConnectionFunctionGeneral
>
>
(
0
);
this
->
inward_adjacency
=
new
::
std
::
vector
<
std
::
vector
<
std
::
pair
<
size_t
,
size_t
>>
*>
(
0
);
this
->
outward_adjacency
=
new
::
std
::
vector
<
std
::
vector
<
std
::
pair
<
size_t
,
size_t
>>
*>
(
0
);
...
...
@@ -96,17 +95,27 @@ namespace lib4neuro {
this
->
neuron_biases
=
nullptr
;
}
if
(
this
->
connection_list
)
{
// if (this->connection_list) {
//
// if (this->delete_weights) {
// for (auto& c: *this->connection_list) {
// printf("%p\n", c);
// if(c) {
// printf("Deleting %p\n", c);
// puts("c");
//// delete c;
// puts("a");
//// c = nullptr;
// puts("b");
// }
// }
// }
// }
// this->connection_list.clear();
// delete this->connection_list;
// this->connection_list = nullptr;
if
(
this
->
delete_weights
)
{
for
(
auto
c
:
*
this
->
connection_list
)
{
delete
c
;
c
=
nullptr
;
}
}
}
delete
this
->
connection_list
;
this
->
connection_list
=
nullptr
;
// puts("*********** 1");
if
(
this
->
inward_adjacency
)
{
for
(
auto
e
:
*
this
->
inward_adjacency
)
{
...
...
@@ -127,10 +136,8 @@ namespace lib4neuro {
e
=
nullptr
;
}
}
delete
this
->
outward_adjacency
;
this
->
outward_adjacency
=
nullptr
;
delete
this
->
outward_adjacency
;
this
->
outward_adjacency
=
nullptr
;
}
if
(
this
->
neuron_layers_feedforward
)
{
...
...
@@ -455,9 +462,9 @@ namespace lib4neuro {
size_t
ci
=
c
.
second
;
this
->
neuron_potentials
->
at
(
ti
)
+=
this
->
connection_list
->
at
(
ci
)
->
eval
(
*
this
->
connection_weights
)
*
potential
;
this
->
connection_list
.
at
(
ci
)
->
eval
(
*
this
->
connection_weights
)
*
potential
;
std
::
cout
<<
" adding input to neuron "
<<
ti
<<
" += "
<<
this
->
connection_list
->
at
(
ci
)
->
eval
(
*
this
->
connection_weights
)
<<
"*"
<<
potential
<<
std
::
endl
;
std
::
cout
<<
" adding input to neuron "
<<
ti
<<
" += "
<<
this
->
connection_list
.
at
(
ci
)
->
eval
(
*
this
->
connection_weights
)
<<
"*"
<<
potential
<<
std
::
endl
;
}
}
}
...
...
@@ -480,9 +487,11 @@ namespace lib4neuro {
NeuralNetwork
::
add_connection_simple
(
size_t
n1_idx
,
size_t
n2_idx
,
SIMPLE_CONNECTION_TYPE
sct
,
size_t
weight_idx
)
{
ConnectionFunctionIdentity
*
con_weight_u1u2
;
std
::
shared_ptr
<
ConnectionFunctionIdentity
>
con_weight_u1u2
;
// ConnectionFunctionIdentity* con_weight_u1u2;
if
(
sct
==
SIMPLE_CONNECTION_TYPE
::
UNITARY_WEIGHT
)
{
con_weight_u1u2
=
new
ConnectionFunctionIdentity
();
con_weight_u1u2
=
std
::
make_shared
<
ConnectionFunctionIdentity
>
(
ConnectionFunctionIdentity
());
// con_weight_u1u2 = new ConnectionFunctionIdentity();
}
else
{
if
(
sct
==
SIMPLE_CONNECTION_TYPE
::
NEXT_WEIGHT
)
{
weight_idx
=
this
->
connection_weights
->
size
();
...
...
@@ -493,25 +502,26 @@ namespace lib4neuro {
}
}
con_weight_u1u2
=
new
ConnectionFunctionIdentity
(
weight_idx
);
con_weight_u1u2
=
std
::
make_shared
<
ConnectionFunctionIdentity
>
(
ConnectionFunctionIdentity
(
weight_idx
));
// con_weight_u1u2 = new ConnectionFunctionIdentity(weight_idx);
}
size_t
conn_idx
=
this
->
add_new_connection_to_list
(
con_weight_u1u2
);
// size_t conn_idx = this->add_new_connection_to_list(con_weight_u1u2);
this
->
add_outward_connection
(
n1_idx
,
n2_idx
,
conn_idx
);
this
->
add_inward_connection
(
n2_idx
,
n1_idx
,
conn_idx
);
this
->
layers_analyzed
=
false
;
return
this
->
connection_list
->
size
()
-
1
;
return
this
->
connection_list
.
size
()
-
1
;
}
size_t
NeuralNetwork
::
add_connection_constant
(
size_t
n1_idx
,
size_t
n2_idx
,
double
weight
)
{
std
::
shared_ptr
<
ConnectionFunctionConstant
>
cfc
=
std
::
make_shared
<
ConnectionFunctionConstant
>
(
ConnectionFunctionConstant
());
ConnectionFunctionConstant
*
con_weight_u1u2
=
new
ConnectionFunctionConstant
(
weight
);
size_t
conn_idx
=
this
->
add_new_connection_to_list
(
con_weight_u1u2
);
size_t
conn_idx
=
this
->
add_new_connection_to_list
(
cfc
);
this
->
add_outward_connection
(
n1_idx
,
n2_idx
,
conn_idx
);
this
->
add_inward_connection
(
n2_idx
,
n1_idx
,
conn_idx
);
...
...
@@ -524,7 +534,7 @@ namespace lib4neuro {
void
NeuralNetwork
::
add_existing_connection
(
size_t
n1_idx
,
size_t
n2_idx
,
size_t
connection_idx
,
NeuralNetwork
&
parent_network
)
{
size_t
conn_idx
=
this
->
add_new_connection_to_list
(
parent_network
.
connection_list
->
at
(
connection_idx
));
size_t
conn_idx
=
this
->
add_new_connection_to_list
(
parent_network
.
connection_list
.
at
(
connection_idx
));
this
->
add_outward_connection
(
n1_idx
,
n2_idx
,
conn_idx
);
this
->
add_inward_connection
(
n2_idx
,
n1_idx
,
conn_idx
);
...
...
@@ -610,7 +620,7 @@ namespace lib4neuro {
size_t
ci
=
c
.
second
;
this
->
neuron_potentials
->
at
(
ti
)
+=
this
->
connection_list
->
at
(
ci
)
->
eval
(
*
this
->
connection_weights
)
*
potential
;
this
->
connection_list
.
at
(
ci
)
->
eval
(
*
this
->
connection_weights
)
*
potential
;
}
}
}
...
...
@@ -679,9 +689,9 @@ namespace lib4neuro {
size_t
ci
=
c
.
second
;
neuron_potential_t
=
this
->
neurons
->
at
(
ti
)
->
get_last_activation_value
(
);
connection_weight
=
this
->
connection_list
->
at
(
ci
)
->
eval
(
*
this
->
connection_weights
);
connection_weight
=
this
->
connection_list
.
at
(
ci
)
->
eval
(
*
this
->
connection_weights
);
this
->
connection_list
->
at
(
ci
)
->
eval_partial_derivative
(
*
this
->
get_parameter_ptr_weights
(),
this
->
connection_list
.
at
(
ci
)
->
eval_partial_derivative
(
*
this
->
get_parameter_ptr_weights
(),
gradient
,
neuron_potential_t
*
scaling_backprog
[
neuron_idx
]);
...
...
@@ -756,11 +766,11 @@ namespace lib4neuro {
size_t
ci
=
c
.
second
;
neuron_activation_t
=
this
->
neurons
->
at
(
ti
)
->
get_last_activation_value
(
);
connection_weight
=
this
->
connection_list
->
at
(
ci
)
->
eval
(
*
this
->
connection_weights
);
connection_weight
=
this
->
connection_list
.
at
(
ci
)
->
eval
(
*
this
->
connection_weights
);
std
::
cout
<<
" [backpropagation] value ("
<<
ti
<<
"): "
<<
neuron_activation_t
<<
", scaling: "
<<
scaling_backprog
[
neuron_idx
]
<<
std
::
endl
;
this
->
connection_list
->
at
(
ci
)
->
eval_partial_derivative
(
*
this
->
get_parameter_ptr_weights
(),
this
->
connection_list
.
at
(
ci
)
->
eval_partial_derivative
(
*
this
->
get_parameter_ptr_weights
(),
gradient
,
neuron_activation_t
*
scaling_backprog
[
neuron_idx
]);
...
...
@@ -946,7 +956,7 @@ namespace lib4neuro {
void
NeuralNetwork
::
write_stats
()
{
::
std
::
cout
<<
std
::
flush
<<
"Number of neurons: "
<<
this
->
neurons
->
size
()
<<
::
std
::
endl
<<
"Number of connections: "
<<
this
->
connection_list
->
size
()
<<
::
std
::
endl
<<
"Number of connections: "
<<
this
->
connection_list
.
size
()
<<
::
std
::
endl
<<
"Number of active weights: "
<<
this
->
connection_weights
->
size
()
<<
::
std
::
endl
<<
"Number of active biases: "
<<
this
->
neuron_biases
->
size
()
<<
::
std
::
endl
;
...
...
@@ -968,7 +978,7 @@ namespace lib4neuro {
}
ofs
<<
"Number of neurons: "
<<
this
->
neurons
->
size
()
<<
::
std
::
endl
<<
"Number of connections: "
<<
this
->
connection_list
->
size
()
<<
::
std
::
endl
<<
"Number of connections: "
<<
this
->
connection_list
.
size
()
<<
::
std
::
endl
<<
"Number of active weights: "
<<
this
->
connection_weights
->
size
()
<<
::
std
::
endl
<<
"Number of active biases: "
<<
this
->
neuron_biases
->
size
()
<<
::
std
::
endl
;
...
...
@@ -985,7 +995,7 @@ namespace lib4neuro {
void
NeuralNetwork
::
write_stats
(
std
::
ofstream
*
file_path
)
{
*
file_path
<<
"Number of neurons: "
<<
this
->
neurons
->
size
()
<<
::
std
::
endl
<<
"Number of connections: "
<<
this
->
connection_list
->
size
()
<<
::
std
::
endl
<<
"Number of connections: "
<<
this
->
connection_list
.
size
()
<<
::
std
::
endl
<<
"Number of active weights: "
<<
this
->
connection_weights
->
size
()
<<
::
std
::
endl
<<
"Number of active biases: "
<<
this
->
neuron_biases
->
size
()
<<
::
std
::
endl
;
...
...
@@ -1006,9 +1016,14 @@ namespace lib4neuro {
return
this
->
connection_weights
;
}
size_t
NeuralNetwork
::
add_new_connection_to_list
(
ConnectionFunctionGeneral
*
con
)
{
this
->
connection_list
->
push_back
(
con
);
return
this
->
connection_list
->
size
()
-
1
;
// size_t NeuralNetwork::add_new_connection_to_list(ConnectionFunctionGeneral *con) {
// this->connection_list.push_back(std::make_shared<ConnectionFunctionGeneral>(*con));
// return this->connection_list.size() - 1;
// }
size_t
NeuralNetwork
::
add_new_connection_to_list
(
std
::
shared_ptr
<
ConnectionFunctionGeneral
>
con
)
{
this
->
connection_list
.
push_back
(
con
);
return
this
->
connection_list
.
size
()
-
1
;
}
void
NeuralNetwork
::
add_inward_connection
(
size_t
s
,
size_t
t
,
size_t
con_idx
)
{
...
...
@@ -1222,7 +1237,7 @@ namespace lib4neuro {
this
->
neuron_bias_indices
=
new
::
std
::
vector
<
int
>
(
0
);
this
->
connection_weights
=
new
::
std
::
vector
<
double
>
(
0
);
this
->
connection_list
=
new
::
std
::
vector
<
ConnectionFunctionGeneral
*
>
(
0
);
this
->
connection_list
=
::
std
::
vector
<
std
::
shared_ptr
<
ConnectionFunctionGeneral
>
>
(
0
);
this
->
inward_adjacency
=
new
::
std
::
vector
<
std
::
vector
<
std
::
pair
<
size_t
,
size_t
>>
*>
(
0
);
this
->
outward_adjacency
=
new
::
std
::
vector
<
std
::
vector
<
std
::
pair
<
size_t
,
size_t
>>
*>
(
0
);
...
...
This diff is collapsed.
Click to expand it.
src/Network/NeuralNetwork.h
+
5
−
2
View file @
6d69861a
...
...
@@ -17,6 +17,7 @@
#include
<algorithm>
#include
<utility>
#include
<fstream>
#include
<memory>
#include
"../settings.h"
#include
"../Neuron/Neuron.h"
...
...
@@ -89,7 +90,7 @@ namespace lib4neuro {
/**
*
*/
std
::
vector
<
ConnectionFunctionGeneral
*
>
*
connection_list
=
nullptr
;
std
::
vector
<
std
::
shared_ptr
<
ConnectionFunctionGeneral
>
>
connection_list
;
//
= nullptr;
/**
*
...
...
@@ -136,7 +137,9 @@ namespace lib4neuro {
* @param con Connection object to be added
* @return Returns the index of the added connection among all the connections
*/
size_t
add_new_connection_to_list
(
ConnectionFunctionGeneral
*
con
);
// size_t add_new_connection_to_list(ConnectionFunctionGeneral *con);
size_t
add_new_connection_to_list
(
std
::
shared_ptr
<
ConnectionFunctionGeneral
>
con
);
/**
* Adds a new entry (oriented edge s -> t) to the adjacency list of this network
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment