0 Views
0 Downloads
0 Favorites
NerveTest
//+------------------------------------------------------------------+
//|                                                    NerveTest.mq5 |
//|                                      Copyright 2020, Anonymous3. |
//+------------------------------------------------------------------+
#include <Nerve\NerveMatrix.mqh>
#include <Graphics\Graphic.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() {
//--- Neural network object
    CNerveMatrix m_nerve();
//--- create network topology [2,2,1]
    int configuration[] = {2, 2, 1};
//--- define hidden and output layer activation functions
    Activations activation_functions[] = {Sigmoid, Sigmoid};
//--- initialize the network
    m_nerve.initialize(configuration, activation_functions);
//--- you can change learning parameters (not a must)
    double learning_rate = 0.15, learning_momentum = 0.2, error_buffer_average_period = 100.0;
    m_nerve.setParameters(learning_rate, learning_momentum, error_buffer_average_period);
//--- ready. train and get outputs
    double input1[] = {0.0, 1.0};
    double input2[] = {1.0, 1.0};
    double input3[] = {0.0, 0.0};
    double control1[] = {0.0};
    double control2[] = {1.0};
    double control3[] = {0.0};
//--- to be collected after feedforward
    double network_prediction[];
//---
    int epoch = 10000;  // how many times it should train. ++epoch == ++accuracy
    while(epoch) {
        --epoch;
        //--- train
        m_nerve.feedForward(input1);
        //--- make the network learn
        m_nerve.backPropagate(control1);
        //--- train on different other training sets
        m_nerve.feedForward(input2);
        m_nerve.backPropagate(input2);
        m_nerve.feedForward(input3);
        m_nerve.backPropagate(input3);
    }
//--- make a prediction
    double predict[] = {1.0, 0.0};
    m_nerve.feedForward(predict);
//--- collect prediction results
    m_nerve.getResults(network_prediction);
//--- print results
    Print("Trained network prediction : ");
    ArrayPrint(network_prediction);
//--- you can save the network configuration with trained weights
    string nerve = m_nerve.Save();
//--- you can save the string above to store trained configuration
//--- to load pass the string to a created network
//--- let's create a new network
    CNerveMatrix new_network();
    new_network.Load(nerve);
//--- it is ready to make predictions
    new_network.feedForward(input1);
    new_network.getResults(network_prediction);
//--- chec results
    Print("Loaded network results: ");
    ArrayPrint(network_prediction);
    Print("Expected  results : ");
    ArrayPrint(control1);
    Print("********");
//--- you can print or plot the error buffer using the standard graphic library
//--- you can get the errors that occured during training
    double errors[];
    m_nerve.getErrorBuffer(errors);
//---
    int width = (int)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS);
    int height = (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);
    CGraphic graphic;
    graphic.Create(0, "Graphic", 0, 0, 0, width, height);
    CCurve *network_errors = graphic.CurveAdd(errors, clrRed, CURVE_LINES, "high_price");
//---
    graphic.CurvePlotAll();
    graphic.Update();
    Sleep(10000);   // or DebugBreak();
    graphic.Destroy();
}
//+------------------------------------------------------------------+

Comments