#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <floatfann.h>
//From http://www.richelbilderbeek.nl/CppFileExists.htm
bool FileExists(const std::string& filename)
{
std::fstream f;
f.open(filename.c_str(),std::ios::in);
return f.is_open();
}
//From http://www.richelbilderbeek.nl/CppFileToVector.htm
const std::vector<std::string> FileToVector(const std::string& fileName)
{
assert(FileExists(fileName)==true);
std::vector<std::string> myVector;
std::ifstream in(fileName.c_str());
std::string myString;
for (int i=0; !in.eof(); ++i)
{
std::getline(in,myString);
myVector.push_back(myString);
}
return myVector;
}
//From http://www.richelbilderbeek.nl/CppCoutContainer.htm
template <class Container>
void CoutContainer(const Container& c)
{
std::copy(c.begin(),c.end(),
std::ostream_iterator<typename Container::value_type>(std::cout,"\n"));
}
//From http://www.richelbilderbeek.nl/CppSaveContainer.htm
template <class Container>
void SaveContainer(const Container& c, const std::string& filename)
{
std::ofstream f(filename.c_str());
std::copy(c.begin(),c.end(),std::ostream_iterator<typename Container::value_type>(f,"\n"));
}
void CreateXorProblemTrainingFile(const std::string& filename)
{
std::vector<std::string> s;
//First line:
// - 4: number of training sets
// - 2: number of inputs
// - 1: number of outputs
s.push_back("4 2 1");
//Second line: if the two inputs are 1.0 and 1.0....
s.push_back("1.0 1.0");
//Third line: the XOR network should return a -1.0
s.push_back("-1.0");
//Fourth line: if the two inputs are -1.0 and -1.0....
s.push_back("-1.0 -1.0");
//Fifth line: the XOR network should return a -1.0
s.push_back("-1.0");
//And so one...
s.push_back("-1.0 1.0"); //In
s.push_back( "1.0" ); //Expected out
s.push_back("1.0 -1.0"); //In
s.push_back( "1.0" ); //Expected out
SaveContainer(s,filename);
}
int main()
{
const unsigned int num_input = 2;
const unsigned int num_hidden = 3;
const unsigned int num_output = 1;
std::vector<unsigned int> layer_sizes;
layer_sizes.push_back(num_input);
layer_sizes.push_back(num_hidden);
layer_sizes.push_back(num_output);
const std::string file_input = "XorProblemTrainingSet.txt";
const std::string file_output = "Output.txt";
const double desired_error = 0.00001;
const unsigned int max_epochs = 100000;
const unsigned int epochs_between_reports = 100;
const double learning_rate = 0.05;
fann * const n = fann_create_shortcut_array(learning_rate, layer_sizes.size(),&layer_sizes[0]);
fann_set_activation_function_hidden(n, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(n, FANN_SIGMOID_SYMMETRIC);
CreateXorProblemTrainingFile(file_input);
//FANN is not const-correct...
fann_train_on_file(n,const_cast<char*>(file_input.c_str()), max_epochs, epochs_between_reports, desired_error);
fann_save(n,file_output.c_str());
{
std::cout << "Showing output file: " << '\n';
const std::vector<std::string> v = FileToVector(file_output);
CoutContainer(v);
}
fann_destroy(n);
}
|