Commit 55a8edfd authored by Martin Beseda's avatar Martin Beseda

Merge branch 'tmp' into 'master'

changes to facilitate simulator functionality

See merge request bes0030/lib4neuro!6
parents dbe52773 952dc13f
......@@ -102,6 +102,17 @@ namespace lib4neuro {
}
void DataSet::shift_outputs_to_zero() {
auto first_elem = this->data.at(0).second;
for(size_t j = 0; j < this->data.size(); ++j){
for(size_t i = 0; i < this->get_output_dim(); ++i){
this->data.at(j).second[i] -= first_elem[i];
}
}
}
void DataSet::add_data_pair(std::vector<double>& inputs,
std::vector<double>& outputs) {
if (this->n_elements == 0 && this->input_dim == 0 && this->output_dim == 0) {
......
......@@ -130,6 +130,11 @@ namespace lib4neuro {
unsigned int output_dim,
NormalizationStrategy* ns = nullptr);
/**
*
*/
LIB4NEURO_API void shift_outputs_to_zero();
/**
* Getter for number of elements
* @return Number of elements in the data set
......
......@@ -25,21 +25,8 @@ namespace lib4neuro {
}
NeuralNetwork::NeuralNetwork(std::string filepath) {
::std::ifstream ifs(filepath);
if (ifs.is_open()) {
try {
boost::archive::text_iarchive ia(ifs);
ia >> *this;
}
catch (boost::archive::archive_exception& e) {
THROW_RUNTIME_ERROR(
"Serialized archive error: '" + e.what() + "'! Please, check if your file is really "
"the serialized DataSet.");
}
ifs.close();
} else {
THROW_RUNTIME_ERROR("File '" + filepath + "' couldn't be open!");
}
this->init_from_file( filepath );
}
......@@ -131,8 +118,9 @@ namespace lib4neuro {
}
potential = this->neurons.at(si)->activate(this->neuron_potentials.at(si),
bias);
std::cout << " applying bias: " << bias << " to neuron potential: " << this->neuron_potentials.at(si)
<< " -> " << potential << std::endl;
std::cout << "Neuron" << si << " (" << this->neuron_potentials.at(si) << " - " << bias << ") -> (" << potential << ")" << std::endl;
// std::cout << " applying bias: " << bias << " to neuron potential: " << this->neuron_potentials.at(si)
// << " -> " << potential << std::endl;
for (auto c: *this->outward_adjacency.at(si)) {
size_t ti = c.first;
......@@ -140,10 +128,11 @@ namespace lib4neuro {
this->neuron_potentials.at(ti) +=
this->connection_list.at(ci)->eval(this->connection_weights) * potential;
std::cout << " EDGE(" << si << ", " << ti << ")" << this->connection_list.at(ci)->eval(this->connection_weights) << std::endl;
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;
}
}
}
......@@ -157,7 +146,7 @@ namespace lib4neuro {
}
output[i] = this->neurons.at(oi)->activate(this->neuron_potentials.at(oi),
bias);
std::cout << "setting the output[" << i << "] = " << output[i] << "(bias = " << bias << ")" << std::endl;
// std::cout << "setting the output[" << i << "] = " << output[i] << "(bias = " << bias << ")" << std::endl;
++i;
}
}
......@@ -203,7 +192,7 @@ namespace lib4neuro {
NeuralNetwork::add_connection_constant(size_t n1_idx,
size_t n2_idx,
double weight) {
std::shared_ptr<ConnectionFunctionConstant> cfc = std::make_shared<ConnectionFunctionConstant>(ConnectionFunctionConstant());
std::shared_ptr<ConnectionFunctionConstant> cfc = std::make_shared<ConnectionFunctionConstant>(ConnectionFunctionConstant(weight));
size_t conn_idx = this->add_new_connection_to_list(cfc);
......@@ -831,6 +820,25 @@ namespace lib4neuro {
this->layers_analyzed = true;
}
void NeuralNetwork::init_from_file(const std::string &filepath) {
::std::ifstream ifs(filepath);
if (ifs.is_open()) {
try {
boost::archive::text_iarchive ia(ifs);
ia >> *this;
}
catch (boost::archive::archive_exception& e) {
THROW_RUNTIME_ERROR(
"Serialized archive error: '" + e.what() + "'! Please, check if your file is really "
"the serialized DataSet.");
}
ifs.close();
} else {
THROW_RUNTIME_ERROR("File '" + filepath + "' couldn't be open!");
}
}
void NeuralNetwork::save_text(std::string filepath) {
::std::ofstream ofs(filepath);
{
......
......@@ -127,6 +127,13 @@ namespace lib4neuro {
*/
bool delete_biases = true;
/**
* helpful arrays to be used in derived classes which are not neccessarily part of the lib4neauro library
*/
std::vector<size_t> ___ind1, ___ind2;
std::vector<std::vector<size_t>> ___ind_m1, ___ind_m2;
std::vector<std::vector<double>> ___val_m1;
/**
*
*/
......@@ -166,6 +173,12 @@ namespace lib4neuro {
*/
void analyze_layer_structure();
/**
* for loading purposes outside the scope of this library
*/
virtual void init_from_file(const std::string &fn);
public:
/**
......
......@@ -44,6 +44,12 @@ namespace lib4neuro {
ar & nn.delete_weights;
ar & nn.delete_biases;
ar & nn.normalization_strategy;
ar & nn.___ind1;
ar & nn.___ind2;
ar & nn.___ind_m1;
ar & nn.___ind_m2;
ar & nn.___val_m1;
}
};
}
......
......@@ -35,7 +35,8 @@ double DoubleUnitStrategy::normalize(double n,
this->max_min_inp_val.at(1) = min;
}
return 2 * (n - min) / (max - min) - 1;
// return 2 * (n - min) / (max - min) - 1;
return n / (this->get_max_value() - this->get_min_value());
}
double DoubleUnitStrategy::de_normalize(double n) {
......@@ -43,6 +44,7 @@ double DoubleUnitStrategy::de_normalize(double n) {
THROW_RUNTIME_ERROR("Data were not normalized, so de-normalization cannot progress!");
}
return 0.5 * ((1 + n) * (this->get_max_value() - this->get_min_value())) + this->get_min_value();
//return 0.5 * ((1 + n) * (this->get_max_value() - this->get_min_value())) + this->get_min_value();
return n * (this->get_max_value() - this->get_min_value());
}
......@@ -8,3 +8,4 @@ namespace lib4neuro{
}
#endif //INC_4NEURO_CONSTANTS_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment