Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
old_macaon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Franck Dary
old_macaon
Commits
7d98b2f2
Commit
7d98b2f2
authored
6 years ago
by
Franck Dary
Browse files
Options
Downloads
Patches
Plain Diff
Genetic Algorithm now only saves the best MLP
parent
bf22fa5a
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
neural_network/include/GeneticAlgorithm.hpp
+3
-1
3 additions, 1 deletion
neural_network/include/GeneticAlgorithm.hpp
neural_network/src/GeneticAlgorithm.cpp
+13
-51
13 additions, 51 deletions
neural_network/src/GeneticAlgorithm.cpp
with
16 additions
and
52 deletions
neural_network/include/GeneticAlgorithm.hpp
+
3
−
1
View file @
7d98b2f2
...
...
@@ -43,7 +43,9 @@ class GeneticAlgorithm : public NeuralNetwork
/// @param nbOutputs The size of the mlp output layer..
Individual
(
dynet
::
ParameterCollection
&
model
,
int
nbInputs
,
const
std
::
string
&
topology
,
int
nbOutputs
);
/// @brief Create a blank individual, so that it will be loaded from a saved model.
Individual
();
///
/// @param bestId The id of the individual that was saved in the file.
Individual
(
unsigned
int
bestId
);
void
becomeChildOf
(
Individual
*
other
);
void
becomeChildOf
(
Individual
*
mom
,
Individual
*
dad
);
void
mutate
(
float
probability
);
...
...
This diff is collapsed.
Click to expand it.
neural_network/src/GeneticAlgorithm.cpp
+
13
−
51
View file @
7d98b2f2
...
...
@@ -39,19 +39,7 @@ void GeneticAlgorithm::init(int nbInputs, const std::string & topology, int nbOu
std
::
vector
<
float
>
GeneticAlgorithm
::
predict
(
FeatureModel
::
FeatureDescription
&
fd
)
{
std
::
vector
<
float
>
prediction
=
generation
[
0
]
->
mlp
.
predict
(
fd
);
for
(
unsigned
int
i
=
1
;
i
<
generation
.
size
();
i
++
)
{
auto
otherPred
=
generation
[
i
]
->
mlp
.
predict
(
fd
);
for
(
unsigned
int
j
=
0
;
j
<
prediction
.
size
();
j
++
)
prediction
[
j
]
+=
otherPred
[
j
];
}
for
(
unsigned
int
j
=
0
;
j
<
prediction
.
size
();
j
++
)
prediction
[
j
]
/=
generation
.
size
();
return
prediction
;
return
generation
[
0
]
->
mlp
.
predict
(
fd
);
}
float
GeneticAlgorithm
::
update
(
FeatureModel
::
FeatureDescription
&
fd
,
int
gold
)
...
...
@@ -128,16 +116,10 @@ float GeneticAlgorithm::update(FeatureModel::FeatureDescription & fd, int gold)
void
GeneticAlgorithm
::
save
(
const
std
::
string
&
filename
)
{
File
*
file
=
new
File
(
filename
,
"w"
);
fprintf
(
file
->
getDescriptor
(),
"%
d %d %s
\n
"
,
nbInputs
,
nbOutputs
,
topology
.
c_str
()
);
fprintf
(
file
->
getDescriptor
(),
"%
u
\n
"
,
generation
[
0
]
->
id
);
delete
file
;
unsigned
int
quarter
=
generation
.
size
()
/
4
;
for
(
unsigned
int
i
=
0
;
i
<
quarter
;
i
++
)
{
generation
[
i
]
->
mlp
.
saveStruct
(
filename
);
generation
[
i
]
->
mlp
.
saveParameters
(
filename
);
}
generation
[
0
]
->
mlp
.
saveStruct
(
filename
);
generation
[
0
]
->
mlp
.
saveParameters
(
filename
);
}
void
GeneticAlgorithm
::
printTopology
(
FILE
*
output
)
...
...
@@ -156,37 +138,17 @@ void GeneticAlgorithm::printTopology(FILE * output)
void
GeneticAlgorithm
::
load
(
const
std
::
string
&
filename
)
{
File
*
file
=
new
File
(
filename
,
"r"
);
int
i
,
o
;
char
buffer
[
1024
];
if
(
fscanf
(
file
->
getDescriptor
(),
"%d %d %[^
\n
]
\n
"
,
&
i
,
&
o
,
buffer
)
!=
3
)
unsigned
int
bestId
;
if
(
fscanf
(
file
->
getDescriptor
(),
"%u
\n
"
,
&
bestId
)
!=
1
)
{
fprintf
(
stderr
,
"ERROR (%s) : file
\'
%s
\'
bad format
. Aborting.
\n
"
,
ERRINFO
,
filename
.
c_str
());
fprintf
(
stderr
,
"ERROR (%s) :
expected best id when reading
file
\'
%s
\'
. Aborting.
\n
"
,
ERRINFO
,
filename
.
c_str
());
exit
(
1
);
}
delete
file
;
generation
.
emplace_back
(
new
Individual
(
bestId
));
this
->
nbInputs
=
i
;
this
->
nbOutputs
=
o
;
this
->
topology
=
buffer
;
auto
splited
=
split
(
topology
,
' '
);
if
(
splited
.
size
()
!=
2
||
!
isNum
(
splited
[
0
]))
{
fprintf
(
stderr
,
"ERROR (%s) : wrong topology
\'
%s
\'
. Aborting.
\n
"
,
ERRINFO
,
topology
.
c_str
());
exit
(
1
);
}
int
nbElems
=
std
::
stoi
(
splited
[
0
]);
for
(
int
i
=
0
;
i
<
nbElems
;
i
++
)
generation
.
emplace_back
(
new
Individual
());
for
(
int
i
=
0
;
i
<
nbElems
;
i
++
)
{
generation
[
i
]
->
mlp
.
loadStruct
(
model
,
filename
,
0
);
generation
[
i
]
->
mlp
.
loadParameters
(
model
,
filename
);
}
generation
[
0
]
->
mlp
.
loadStruct
(
model
,
filename
,
0
);
generation
[
0
]
->
mlp
.
loadParameters
(
model
,
filename
);
}
GeneticAlgorithm
::
Individual
::
Individual
(
dynet
::
ParameterCollection
&
model
,
int
nbInputs
,
const
std
::
string
&
topology
,
int
nbOutputs
)
:
mlp
(
"MLP_"
+
std
::
to_string
(
idCount
))
...
...
@@ -199,10 +161,10 @@ GeneticAlgorithm::Individual::Individual(dynet::ParameterCollection & model, int
mutate
(
1.0
);
}
GeneticAlgorithm
::
Individual
::
Individual
()
:
mlp
(
"MLP_"
+
std
::
to_string
(
idCount
))
GeneticAlgorithm
::
Individual
::
Individual
(
unsigned
int
bestId
)
:
mlp
(
"MLP_"
+
std
::
to_string
(
bestId
))
{
this
->
id
=
idCount
++
;
this
->
id
=
bestId
;
this
->
loss
=
0.0
;
this
->
value
=
0.0
;
}
...
...
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