Ejemplo n.º 1
0
void train(Configuration *cfg)
{
	QString fileName(QDir::homePath() + "/" + QCoreApplication::applicationName() + ".ini");
	qDebug() << "using config file:" << fileName;
	QSettings settings(fileName, QSettings::IniFormat);
	const float learningRate = settings.value("learningRate", 0.8).toFloat();
	const unsigned int numLayers = settings.value("numLayers", 3).toInt();
	const unsigned int numInput = settings.value("numInput", 1024).toInt();
	const unsigned int numHidden = settings.value("numHidden", 32).toInt();
	const unsigned int numOutput = settings.value("numOutput", 1).toInt();
	const float desiredError = settings.value("desiredError", 0.0001f).toFloat();
	const unsigned int maxIterations = settings.value("maxIterations", 3000).toInt();
	const unsigned int iterationsBetweenReports = settings.value("iterationsBetweenReports", 100).toInt();

	FANN::neural_net net;
	net.create_standard(numLayers, numInput, numHidden, numOutput);
	net.set_learning_rate(learningRate);
	net.set_activation_steepness_hidden(0.5);
	net.set_activation_steepness_output(0.5);
	net.set_learning_momentum(0.6);
	net.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC);
	net.set_activation_function_output(FANN::SIGMOID_SYMMETRIC);
	net.set_training_algorithm(FANN::TRAIN_RPROP);
	net.print_parameters();

	FANN::training_data data;
	if (data.read_train_from_file(cfg->getDataSavePath().toStdString()))
	{
		qDebug() << "Wczytano dane";
		//inicjalizacja wag
		net.init_weights(data);
		data.shuffle_train_data();
		net.set_callback(printCallback, NULL);
		net.train_on_data(data, maxIterations,
			iterationsBetweenReports, desiredError);
		net.save(cfg->getNetSavePath().toStdString());
		qDebug() << "Nauczono i zapisano siec";
	}
}
Ejemplo n.º 2
0
void neuralNetworkTraining(std::string training_data_file)
{
    /*
     * Parameters for create_standard method.
     *
     * num_layers             : The total number of layers including the input and the output layer.
     * num_input_neurons      : The number of neurons in the input layer.
     * num_hidden_one_neurons : The number of neurons in the first hidden layer.
     * num_hidden_two_neurons : The number of neurons in the second hidden layer.
     * num_output_neurons     : The number of neurons in the output layer.
     */
    const unsigned int num_layers = 3;
    const unsigned int num_input_neurons = 8;
    const unsigned int num_hidden_neurons = 5;
    const unsigned int num_output_neurons = 1;

    /*
     * Parameters for train_on_data method.
     *
     * desired_errors         : The desired get_MSE or get_bit_fail, depending on which stop function is chosen by set_train_stop_function.
     * max_epochs             : The maximum number of epochs the training should continue.
     * epochs_between_reports : The number of epochs between printing a status report to stdout. A value of zero means no reports should be printed.
     */
    const float desired_error = DESIRED_ERROR;
    const unsigned int max_epochs = MAX_EPOCHS;
    const unsigned int epochs_between_reports = EPOCHS_BETWEEN_REPORTS;

    FANN::neural_net net;
    // Create a standard fully connected backpropagation neural network.
    net.create_standard(num_layers, num_input_neurons, num_hidden_neurons, num_output_neurons);

    net.set_activation_function_hidden(FANN::SIGMOID_SYMMETRIC_STEPWISE); // Set the activation function for all of the hidden layers.
    net.set_activation_function_output(FANN::SIGMOID_SYMMETRIC_STEPWISE); // Set the activation function for the output layer.
    net.set_training_algorithm(FANN::TRAIN_RPROP);                        // Set the training algorithm.
    net.randomize_weights(-INIT_EPSILON, INIT_EPSILON);                   // Give each connection a random weight between -INIT_EPSILON and INIT_EPSILON.

    std::cout << std::endl << "Network Type                         :  ";
    switch (net.get_network_type())
    {
    case FANN::LAYER:
        std::cout << "LAYER" << std::endl;
        break;
    case FANN::SHORTCUT:
        std::cout << "SHORTCUT" << std::endl;
        break;
    default:
        std::cout << "UNKNOWN" << std::endl;
        break;
    }
    net.print_parameters();

    std::cout << std::endl << "Training Network." << std::endl;
    FANN::training_data data;
    if (data.read_train_from_file(training_data_file))
    {
        std::cout << "Max Epochs: " << std::setw(8) << max_epochs << ". " << "Desired Error: " << std::left << desired_error << std::right << std::endl;

        net.set_callback(printCallback, NULL);                                      // Sets the callback function for use during training.
        net.train_on_data(data, max_epochs, epochs_between_reports, desired_error); // Trains on an entire dataset, for a period of time.

        std::cout << "Saving Network." << std::endl;
        net.save("neural_network_controller_float.net");
        unsigned int decimal_point = net.save_to_fixed("neural_network_controller_fixed.net");
        data.save_train_to_fixed("neural_network_controller_fixed.data", decimal_point);
    }
}