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
424990db
Commit
424990db
authored
6 years ago
by
Martin Beseda
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Several improvements made in Simulator example.
parent
979d46d1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/examples/simulator.cpp
+51
-24
51 additions, 24 deletions
src/examples/simulator.cpp
with
51 additions
and
24 deletions
src/examples/simulator.cpp
+
51
−
24
View file @
424990db
...
...
@@ -29,6 +29,13 @@ int main(int argc, char** argv){
l4n
::
CSVReader
reader
(
"/home/martin/Desktop/data_Heaviside.txt"
,
"
\t
"
,
true
);
// File, separator, skip 1st line
reader
.
read
();
// Read from the file
/* Open file for writing */
std
::
string
filename
=
"simulator_output.txt"
;
std
::
ofstream
output_file
(
filename
);
if
(
!
output_file
.
is_open
())
{
throw
std
::
runtime_error
(
"File '"
+
filename
+
"' can't be opened!"
);
}
/* Create data set for both the training and testing of the neural network */
std
::
vector
<
unsigned
int
>
inputs
=
{
3
};
// Possible multiple inputs, e.g. {0,3}
std
::
vector
<
unsigned
int
>
outputs
=
{
1
};
// Possible multiple outputs, e.g. {1,2}
...
...
@@ -83,7 +90,7 @@ int main(int argc, char** argv){
// 1) Threshold for the successful ending of the optimization - deviation from minima
// 2) Number of iterations to reset step size to tolerance/10.0
// 3) Maximal number of iterations - optimization will stop after that, even if not converged
l4n
::
GradientDescent
gs
(
1e-3
,
100
,
1
0
);
l4n
::
GradientDescent
gs
(
1e-3
,
100
,
20
0
);
// Weight and bias randomization in the network according to the uniform distribution
// Calling methods nn.randomize_weights() and nn.randomize_biases()
...
...
@@ -97,21 +104,37 @@ int main(int argc, char** argv){
/* Cross - validation */
l4n
::
CrossValidator
cv
(
&
gs
,
&
mse
);
// Parameters: 1) Number of data-set parts used for CV, 2) Number of tests performed
cv
.
run_k_fold_test
(
10
,
1
);
// Parameters:
// 1) Number of data-set parts used for CV
// 2) Number of tests performed
// git 3) File-path to the files with data from cross-validation (one CV run - one file)
cv
.
run_k_fold_test
(
10
,
3
,
&
output_file
);
/* Save network to the text file */
nn
.
save_text
(
"test_net.4n"
);
/* Check of the saved network */
/* Check of the saved network
- print to STDOUT
*/
std
::
cout
<<
std
::
endl
<<
"The original network info:"
<<
std
::
endl
;
nn
.
print_stats
();
nn
.
print_weights
();
nn
.
write_stats
();
nn
.
write_weights
();
nn
.
write_biases
();
l4n
::
NeuralNetwork
nn_loaded
(
"test_net.4n"
);
std
::
cout
<<
std
::
endl
<<
"The loaded network info:"
<<
std
::
endl
;
nn_loaded
.
print_stats
();
nn_loaded
.
print_weights
();
nn_loaded
.
write_stats
();
nn
.
write_weights
();
nn
.
write_biases
();
/* Check of the saved network - write to the file */
output_file
<<
std
::
endl
<<
"The original network info:"
<<
std
::
endl
;
nn
.
write_stats
(
&
output_file
);
nn
.
write_weights
(
&
output_file
);
nn
.
write_biases
(
&
output_file
);
output_file
<<
std
::
endl
<<
"The loaded network info:"
<<
std
::
endl
;
nn_loaded
.
write_stats
(
&
output_file
);
nn
.
write_weights
(
&
output_file
);
nn
.
write_biases
(
&
output_file
);
/* Example of evaluation of a single input, normalized input, de-normalized output */
std
::
vector
<
double
>
input_norm
(
ds
.
get_input_dim
()),
...
...
@@ -131,25 +154,29 @@ int main(int argc, char** argv){
ds
.
de_normalize_single
(
input_norm
,
input
);
ds
.
de_normalize_single
(
expected_output_norm
,
expected_output
);
std
::
cout
<<
std
::
endl
<<
"input: "
;
for
(
auto
el
:
input_norm
)
{
std
::
cout
<<
el
<<
", "
;
}
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"output: "
;
for
(
auto
el
:
output
)
{
std
::
cout
<<
el
<<
", "
;
}
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"expected output: "
;
for
(
auto
el
:
expected_output
)
{
std
::
cout
<<
el
<<
", "
;
}
std
::
cout
<<
std
::
endl
;
/* Evaluate network on an arbitrary data-set and save results into the file */
l4n
::
DataSet
ds2
;
std
::
vector
<
double
>
inp
,
out
;
for
(
double
i
=
0
;
i
<
5
;
i
++
)
{
inp
=
{
i
};
out
=
{
i
+
2
};
ds2
.
add_data_pair
(
inp
,
out
);
}
output_file
<<
std
::
endl
<<
"Evaluating network on the dataset: "
<<
std
::
endl
;
ds2
.
store_data_text
(
&
output_file
);
output_file
<<
"Output and the error:"
<<
std
::
endl
;
mse
.
eval_on_data_set
(
&
ds2
,
&
output_file
);
/* Close the output file for writing */
output_file
.
close
();
return
0
;
}
catch
(
const
std
::
runtime_error
&
e
)
{
std
::
cerr
<<
e
.
what
()
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
catch
(
const
std
::
out_of_range
&
e
)
{
std
::
cerr
<<
e
.
what
()
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
catch
(
const
std
::
invalid_argument
&
e
)
{
}
catch
(
const
std
::
exception
&
e
)
{
std
::
cerr
<<
e
.
what
()
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
...
...
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