Esempio n. 1
0
        public QLearner(List <Action> actions, int numHidden, int numInputs, int seed)
        {
            this.actions = actions;
            network      = new BackPropNeuralNet(numInputs, numHidden, actions.Count);
            int           numWeights = (numInputs * numHidden) + (numHidden * actions.Count) + (numHidden + actions.Count);
            List <double> weights    = new List <double>();

            for (int i = 0; i < numWeights; i++)
            {
                weights.Add(Random.Range(-1.0f, 1.0f));
            }
            network.SetWeights(weights.ToArray());
        }
Esempio n. 2
0
        public Detect(BackPropNeuralNet bnn)
        {
            InitializeComponent();

            DoubleBuffered = true;
            Dock           = DockStyle.Fill;

            blobFinder = new BlobFinder();
            this.bnn   = bnn;


            this.panel1.Paint     += new PaintEventHandler(this.panel1_Paint);
            this.panel1.MouseDown += new MouseEventHandler(this.panel1_MouseDown);
            this.panel1.MouseMove += new MouseEventHandler(this.panel1_MouseMove);
            this.panel1.MouseUp   += new MouseEventHandler(this.panel1_MouseUp);
        }
        public void Setup(IEnumerable <IFunctor> functors, StockHistorySet stockHistorySet)
        {
            Fns = functors.ToArray();
            m_StockHistorySet = stockHistorySet;

            if (this.Fns == null)
            {
                throw new ApplicationException();
            }
            if (!Fns.Any())
            {
                throw new ApplicationException();
            }

            int numInput   = Fns.Length;
            int numHidden  = 8;
            int numOutput  = 1;
            int numWeights = (numInput * numHidden) + (numHidden * numOutput) + (numHidden + numOutput);

            DebugWrite("Creating a " + numInput + "-input, " + numHidden + "-hidden, " + numOutput + "-output neural network");
            DebugWrite("Using hard-coded tanh function for hidden layer activation");
            DebugWrite("Using hard-coded log-sigmoid function for output layer activation");

            m_Bnn = new BackPropNeuralNet(numInput, numHidden, numOutput);

            DebugWrite("\nGenerating random initial weights and bias values");
            double[] initWeights = new double[numWeights];
            for (int i = 0; i < initWeights.Length; ++i)
            {
                initWeights[i] = (rnd.NextDouble() - 0.5d) * 1.0d;
            }
            DebugWrite("Loading neural network initial weights and biases into neural network");
            m_Bnn.SetWeights(initWeights);

            double learnRate = 0.2; // learning rate - controls the maginitude of the increase in the change in weights.
            double momentum  = 0.1; // momentum - to discourage oscillation.

            DebugWrite("Setting learning rate = " + learnRate.ToString("F2") + " and momentum = " + momentum.ToString("F2"));

            int    maxEpochs   = 8000000;
            double errorThresh = 0.01;

            DebugWrite("\nSetting max epochs = " + maxEpochs + " and error threshold = " + errorThresh.ToString("F6"));

            // Train

            int    epoch = 0;
            double error = double.MaxValue;

            DebugWrite("\nBeginning training using back-propagation\n");

            int stocksCount = stockHistorySet.AllStockHistories.Count();;

            while (epoch < maxEpochs) // train
            {
                int stockNum = rnd.Next(0, stocksCount);
                var stock    = stockHistorySet.AllStockHistories[stockNum];

                double realValue   = stock.Closes[Today + DaysInFuture] / stock.Closes[Today];
                double updateValue = Math.Tanh(realValue);

                double predictedOutput = m_Bnn.ComputeOutputs(ComputeInputs(stock))[0];
                double predictedValue  = ATanh(predictedOutput) * stock.Closes[Today];

                m_Bnn.UpdateWeights(new[] { updateValue }, learnRate, momentum);
                ++epoch;

                if (epoch % 20000 == 0)
                {
                    error = GetAverageError(true);

                    if (error < errorThresh)
                    {
                        DebugWrite("Found weights and bias values that meet the error criterion at epoch " + epoch);
                        break;
                    }
                    DebugWrite("epoch = " + epoch);
                    DebugWrite(" error = " + error + "\n");
                }
            } // train loop

            double[] finalWeights = m_Bnn.GetWeights();
            DebugWrite("");
            DebugWrite("Final neural network weights and bias values are:");
            Helpers.ShowVector(finalWeights, 5, 8, true);
        }
Esempio n. 4
0
 public State()
 {
     NeuralNetwork = new BackPropNeuralNet(NumberOfInputs, NumberOfHidden, NumberOfOutputs);
 }
Esempio n. 5
0
 public QLearner(int eValue, double alpha, double gamma, double eta, List <Action> actions, List <int> hiddenSizes, int numInputs, int seed)
 {
     this.actions = actions;
     //hiddenSizes.Add(1);
     network = new BackPropNeuralNet(numInputs + actions.Count, hiddenSizes[0], 1);
 }
        public void Setup(IEnumerable<IFunctor> functors, StockHistorySet stockHistorySet)
        {
            Fns = functors.ToArray();
            m_StockHistorySet = stockHistorySet;

            if (this.Fns == null) throw new ApplicationException();
            if (!Fns.Any()) throw new ApplicationException();

            int numInput = Fns.Length;
            int numHidden = 8;
            int numOutput = 1;
            int numWeights = (numInput * numHidden) + (numHidden * numOutput) + (numHidden + numOutput);

            DebugWrite("Creating a " + numInput + "-input, " + numHidden + "-hidden, " + numOutput + "-output neural network");
            DebugWrite("Using hard-coded tanh function for hidden layer activation");
            DebugWrite("Using hard-coded log-sigmoid function for output layer activation");

            m_Bnn = new BackPropNeuralNet(numInput, numHidden, numOutput);

            DebugWrite("\nGenerating random initial weights and bias values");
            double[] initWeights = new double[numWeights];
            for (int i = 0; i < initWeights.Length; ++i)
                initWeights[i] = (rnd.NextDouble() - 0.5d) * 1.0d;
            DebugWrite("Loading neural network initial weights and biases into neural network");
            m_Bnn.SetWeights(initWeights);

            double learnRate = 0.2;  // learning rate - controls the maginitude of the increase in the change in weights.
            double momentum = 0.1; // momentum - to discourage oscillation.
            DebugWrite("Setting learning rate = " + learnRate.ToString("F2") + " and momentum = " + momentum.ToString("F2"));

            int maxEpochs = 8000000;
            double errorThresh = 0.01;
            DebugWrite("\nSetting max epochs = " + maxEpochs + " and error threshold = " + errorThresh.ToString("F6"));

            // Train

            int epoch = 0;
            double error = double.MaxValue;
            DebugWrite("\nBeginning training using back-propagation\n");

            int stocksCount = stockHistorySet.AllStockHistories.Count(); ;

            while (epoch < maxEpochs) // train
            {
                int stockNum = rnd.Next(0, stocksCount);
                var stock = stockHistorySet.AllStockHistories[stockNum];

                double realValue = stock.Closes[Today + DaysInFuture] / stock.Closes[Today];
                double updateValue = Math.Tanh(realValue);

                double predictedOutput = m_Bnn.ComputeOutputs(ComputeInputs(stock))[0];
                double predictedValue = ATanh(predictedOutput) * stock.Closes[Today];

                m_Bnn.UpdateWeights(new[] { updateValue }, learnRate, momentum);
                ++epoch;

                if (epoch % 20000 == 0)
                {
                    error = GetAverageError(true);

                    if (error < errorThresh)
                    {
                        DebugWrite("Found weights and bias values that meet the error criterion at epoch " + epoch);
                        break;
                    }
                    DebugWrite("epoch = " + epoch);
                    DebugWrite(" error = " + error + "\n");
                }
            } // train loop

            double[] finalWeights = m_Bnn.GetWeights();
            DebugWrite("");
            DebugWrite("Final neural network weights and bias values are:");
            Helpers.ShowVector(finalWeights, 5, 8, true);
        }