示例#1
0
        public string recursivePrediction(int numStepRecursPrediction, float[] amplituda, float[] offset)
        {
            int length  = signal.GetLength(0);
            int length2 = signal.GetLength(1);

            float[,] predictSignal = new float[numStepRecursPrediction + delayInput, length2];
            float[,] desiredSignal = new float[numStepRecursPrediction, length2];
            float[,] testSignal    = new float[numStepRecursPrediction + delayInput, length2 + delayInput];
            for (int i = 0; i < numStepRecursPrediction; i++)
            {
                for (int k = 0; k < length2 + delayInput; k++)
                {
                    testSignal[i, k] = signal[i + length - numStepRecursPrediction - delayInput + k, 0];
                }
            }
            MlpTrainResult resTrain = train(1000, length - numStepRecursPrediction, amplituda, offset);

            float[] val = new float[length2 + delayInput];

            for (int k = 0; k < length2 + delayInput; k++)
            {
                predictSignal[k, 0] = testSignal[0, k];
            }

            predictSignal[delayInput, 0]     = resTrain.checkSignal[delayInput];
            desiredSignal[0, 0]              = resTrain.checkSignal[delayInput];
            predictSignal[delayInput + 1, 0] = resTrain.predictSignal[0] * amplituda[0] + offset[0];
            desiredSignal[1, 0]              = resTrain.realSignal[0] * amplituda[0] + offset[0];
            predictSignal[delayInput + 2, 0] = resTrain.predictSignal2[0] * amplituda[0] + offset[0];
            desiredSignal[2, 0]              = resTrain.realSignal2[0] * amplituda[0] + offset[0];

            for (int j = 0; j < length2 + delayInput; j++)
            {
                val[j] = testSignal[2, j];
            }

            float[] newVal;
            for (int i = 3; i < numStepRecursPrediction - 1; i++)
            {
                newVal = _computeNet(val);
                for (int j = 0; j < length2; j++)
                {
                    predictSignal[i + delayInput, j] = newVal[j];
                    desiredSignal[i, j] = testSignal[i, delayInput];
                }
                for (int j = 0; j < length2 + delayInput; j++)
                {
                    val[j] = predictSignal[i + j, 0];
                }
            }
            float[, ,] testOut = new float[numStepRecursPrediction, length2, 3];
            for (int i = 0; i < numStepRecursPrediction; i++)
            {
                for (int k = 0; k < length2; k++)
                {
                    testOut[i, k, 0] = (predictSignal[i + delayInput, k] - offset[k]) / amplituda[k];
                    testOut[i, k, 1] = (desiredSignal[i, k] - offset[k]) / amplituda[k];
                    testOut[i, k, 2] = (testOut[i, k, 1] - testOut[i, k, 0]) / testOut[i, k, 1] * 100;
                }
            }
            DataProcess.ExportArray3(testOut, "_test.csv");
            return(resTrain.report);
        }
示例#2
0
        public MlpTrainResult train(int numEpochs, int trainLength, float[] amplituda, float[] offset)
        {
            int epochCount = 0;

            float[] errors            = new float[numEpochs];
            int     numInputSignals   = signal.GetLength(1) + delayInput;
            int     numDesiredSignals = desiredSignal.GetLength(1);

            trainLength = (trainLength == 0) ? signal.GetLength(0) : trainLength;
            float[][][] bestWeights = this.Weights;

            float[] checkSignal  = new float[numInputSignals];
            float[] checkSignal2 = new float[numInputSignals];

            float[] checkPredict = new float[numDesiredSignals];
            float[] realSignal   = new float[numDesiredSignals];
            float[] realSignal2  = new float[numDesiredSignals];

            float[] checkPredict2  = new float[numDesiredSignals];
            float[] checkPredict1B = new float[numInputSignals];
            float[] checkPredict2B = new float[numInputSignals];

            for (int j = 0; j < numInputSignals; j++)
            {
                checkSignal[j]  = signal[trainLength + j - delayInput, 0];
                checkSignal2[j] = signal[trainLength + j - delayInput + 1, 0];
            }
            realSignal[0]  = desiredSignal[trainLength + 1, 0];
            realSignal2[0] = desiredSignal[trainLength + 2, 0];
            realSignal[0]  = (realSignal[0] - offset[0]) / amplituda[0];
            realSignal2[0] = (realSignal2[0] - offset[0]) / amplituda[0];

            float minErr = 1000;
            float procentErr, procentErr1, procentErr2;
            float procentErrB1 = 0;
            float procentErrB2 = 0;
            float y1, y2, k, b;

            k = 0;
            b = 0;
            int            numberEpochCount = 0;
            MlpTrainResult res = new MlpTrainResult();

            while (epochCount < numEpochs)
            {
                errors[epochCount] = _trainEpoch(numInputSignals, numDesiredSignals, trainLength);
                checkPredict       = _computeNet(checkSignal);
                checkSignal2[numInputSignals - 1] = checkPredict[0];
                checkPredict2    = _computeNet(checkSignal2);
                checkPredict[0]  = (checkPredict[0] - offset[0]) / amplituda[0];
                procentErr1      = _getProcentDifference(realSignal, checkPredict);
                checkPredict2[0] = (checkPredict2[0] - offset[0]) / amplituda[0];
                procentErr2      = _getProcentDifference(realSignal2, checkPredict2);
                procentErr       = (procentErr1 + procentErr2) / 2;
                if (procentErr1 < minErr)
                {
                    minErr           = procentErr1;
                    bestWeights      = this.Weights;
                    numberEpochCount = epochCount;
                    procentErrB1     = procentErr1;
                    procentErrB2     = procentErr2;

                    res.checkSignal    = checkSignal;
                    res.realSignal     = realSignal;
                    res.predictSignal  = checkPredict;
                    res.realSignal2    = realSignal2;
                    res.predictSignal2 = checkPredict2;
                }

                /*
                 * if (errors[epochCount] < minError)
                 * {
                 *  bestWeights = this.Weights;
                 *  numberBestEpoch = epochCount;
                 *  minError = errors[epochCount];
                 * }
                 * else
                 * {
                 *  //numStop++;
                 * }
                 */
                //if (numStop > 10) break;
                //DataProcess.shuffleArrays(ref signal, ref desiredSignal, trainLength);
                epochCount++;
            }
            this.Weights = bestWeights;

            /*
             *
             * for (int i = 0; i < 10; i++)
             * {
             *  _trainEpoch(numInputSignals, numDesiredSignals, trainLength + 2);
             * }
             */

            DataProcess.ExportArray(errors, "_mlp_err.csv");
            string report = "";

            report += "\n minErr = " + minErr.ToString();
            report += "\n numberEpochCount = " + numberEpochCount.ToString();
            report += "\n procentErrB1 = " + procentErrB1.ToString();
            report += "\n procentErrB2 = " + procentErrB2.ToString();

            res.report = report;
            return(res);
        }