コード例 #1
0
 /// <summary>
 /// Invokes BeginSampleEvent
 /// </summary>
 /// <param name="currentIteration">
 /// Current training iteration
 /// </param>
 /// <param name="currentSample">
 /// Current sample which got trained successfully
 /// </param>
 protected virtual void OnEndSample(int currentIteration, TrainingSample currentSample)
 {
     if (EndSampleEvent != null)
     {
         EndSampleEvent(this, new TrainingSampleEventArgs(currentIteration, currentSample));
     }
 }
コード例 #2
0
        /// <summary>
        /// Trains the neural network for the given training set (Batch Training)
        /// </summary>
        /// <param name="trainingSet">
        /// The training set to use
        /// </param>
        /// <param name="trainingEpochs">
        /// Number of training epochs. (All samples are trained in some random order, in every
        /// training epoch)
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// if <c>trainingSet</c> is <c>null</c>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// if <c>trainingEpochs</c> is zero or negative
        /// </exception>
        public virtual void Learn(TrainingSet trainingSet, int trainingEpochs)
        {
            // Validate
            Helper.ValidateNotNull(trainingSet, "trainingSet");
            Helper.ValidatePositive(trainingEpochs, "trainingEpochs");
            if ((trainingSet.InputVectorLength != inputLayer.NeuronCount) ||
                (trainingMethod == TrainingMethod.Supervised && trainingSet.OutputVectorLength != outputLayer.NeuronCount) ||
                (trainingMethod == TrainingMethod.Unsupervised && trainingSet.OutputVectorLength != 0))
            {
                throw new ArgumentException("Invalid training set");
            }

            // Reset isStopping
            isStopping = false;

            // Re-Initialize the network
            Initialize();
            for (int currentIteration = 0; currentIteration < trainingEpochs; currentIteration++)
            {
                int[] randomOrder = Helper.GetRandomOrder(trainingSet.TrainingSampleCount);
                // Beginning a new training epoch
                OnBeginEpoch(currentIteration, trainingSet);

                // Check for Jitter Epoch
                if (jitterEpoch > 0 && currentIteration % jitterEpoch == 0)
                {
                    for (int i = 0; i < connectors.Count; i++)
                    {
                        connectors[i].Jitter(jitterNoiseLimit);
                    }
                }
                for (int index = 0; index < trainingSet.TrainingSampleCount; index++)
                {
                    TrainingSample randomSample = trainingSet[randomOrder[index]];

                    // Learn a random training sample
                    OnBeginSample(currentIteration, randomSample);
                    LearnSample(trainingSet[randomOrder[index]], currentIteration, trainingEpochs);
                    OnEndSample(currentIteration, randomSample);

                    // Check if we need to stop
                    if (isStopping)
                    {
                        isStopping = false; return;
                    }
                }
                // Training Epoch successfully complete
                OnEndEpoch(currentIteration, trainingSet);

                // Check if we need to stop
                if (isStopping)
                {
                    isStopping = false; return;
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Trains the network for the given training sample (Online training mode). Note that this
        /// method trains the sample only once, irrespective of what current epoch is. The arguments
        /// are just used to find out training progress and adjust parameters depending on it.
        /// </summary>
        /// <param name="trainingSample">
        /// Training sample to use
        /// </param>
        /// <param name="currentIteration">
        /// Current training iteration
        /// </param>
        /// <param name="trainingEpochs">
        /// Number of training epochs
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If <c>trainingSample</c> is <c>null</c>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <c>trainingEpochs</c> is not positive
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <c>currentIteration</c> is negative or, if it is not less than <c>trainingEpochs</c>
        /// </exception>
        public virtual void Learn(TrainingSample trainingSample, int currentIteration, int trainingEpochs)
        {
            Helper.ValidateNotNull(trainingSample, "trainingSample");
            Helper.ValidatePositive(trainingEpochs, "trainingEpochs");
            Helper.ValidateWithinRange(currentIteration, 0, trainingEpochs - 1, "currentIteration");

            OnBeginSample(currentIteration, trainingSample);
            LearnSample(trainingSample, currentIteration, trainingEpochs);
            OnEndSample(currentIteration, trainingSample);
        }
コード例 #4
0
        /// <summary>
        /// Adds a new supervised training sample to the training set. If already exists, it will
        /// be replaced.
        /// </summary>
        /// <param name="sample">
        /// The sample to add
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If <c>sample</c> is <c>null</c>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If sizes of input vector or output vector are different from their expected sizes
        /// </exception>
        public void Add(TrainingSample sample)
        {
            // Validation
            Helper.ValidateNotNull(sample, "sample");
            if (sample.InputVector.Length != inputVectorLength)
            {
                throw new ArgumentException
                          ("Input vector must be of size " + inputVectorLength, "sample");
            }
            if (sample.OutputVector.Length != outputVectorLength)
            {
                throw new ArgumentException
                          ("Output vector must be of size " + outputVectorLength, "sample");
            }

            // Note that the reference is being added. (Sample is immutable)
            trainingSamples.Add(sample);
        }
コード例 #5
0
 /// <summary>
 /// Determine whether the given object is equal to this instance
 /// </summary>
 /// <param name="obj">
 /// The object to compare with this instance
 /// </param>
 /// <returns>
 /// <c>true</c> if the given object is equal to this instance, <c>false</c> otherwise
 /// </returns>
 public override bool Equals(object obj)
 {
     if (obj is TrainingSample)
     {
         TrainingSample sample = (TrainingSample)obj;
         int            size;
         if ((size = sample.inputVector.Length) == inputVector.Length)
         {
             for (int i = 0; i < size; i++)
             {
                 if (inputVector[i] != sample.inputVector[i])
                 {
                     return(false);
                 }
             }
             return(true);
         }
     }
     return(false);
 }
コード例 #6
0
 /// <summary>
 /// Determines whether the training sample is present in the set
 /// </summary>
 /// <param name="sample">
 /// The sample to locate
 /// </param>
 /// <returns>
 /// <c>true</c> if present, <c>false</c> otherwise
 /// </returns>
 public bool Contains(TrainingSample sample)
 {
     return trainingSamples.Contains(sample);
 }
コード例 #7
0
        private double[] doPrediction(double[] inputValue4Predicts, int numDayPredict,int modelType)
        {
            double[] result = null;
            string strModelFile = null;
            string strTestFile = null;

            if (modelType == 0)//DT-ANN
            {
                strModelFile = _trainModel + "DT-ANN\\" + cmbStockID.SelectedItem.ToString()+ "_" + numDayPredict.ToString() + "_model.txt";
                // Load model lên
                BackpropagationNetwork bpNetwork;
                Stream stream = File.Open(strModelFile, FileMode.Open);
                BinaryFormatter bformatter = new BinaryFormatter();
                bpNetwork = (BackpropagationNetwork)bformatter.Deserialize(stream);
                stream.Close();

                // Tạo tập mẫu để test
                TrainingSample testSample = new TrainingSample(inputValue4Predicts);

                // Thực hiện test
                double[] dblTemp = bpNetwork.Run(testSample.InputVector);
                result = new double[1];

                result[0] = ConverterBUS.Convert2Trend(dblTemp);
            }
            else
            {
                //strModelFile = _trainModel + "K-SVMeans\\"; //+cmbStockID.SelectedItem.ToString() + "_" + numDayPredict.ToString() + "_model.txt";
                string strMutualPath = _trainModel + "K-SVMeans\\" +cmbStockID.SelectedItem.ToString() + "_" + numDayPredict.ToString();
                string strClusterModelFile = strMutualPath + "__clusterModel.txt";

                // Thực hiện cluster
                SampleDataBUS samDataBUS = new SampleDataBUS();
                samDataBUS.Samples = new double[1][];
                samDataBUS.Samples[0] = inputValue4Predicts;

                //samDataBUS.Read(strTestFile);
                Clustering clustering = new Clustering(samDataBUS.Samples, DistanceType.Manhattan);
                clustering.Run(strClusterModelFile, true);

                int clusterIndex = clustering.SampleData.ClusterIndices[0];

                string strSVMModelFiles = strMutualPath + "_model" + (++clusterIndex).ToString() + ".txt";

                Model model = Model.Read(strSVMModelFiles);

                Node[] nodes = new Node[10];

                for (int i = 0; i < 10; i++)
                {
                    nodes[i] = new Node(i, inputValue4Predicts[i]);
                }

                double[] predictProbability = Prediction.PredictProbability(model, nodes);

                //khoi tao label 3 pt
                int[] labels = new int[3];
                Procedures.svm_get_labels(model, labels);

                result = new double[3];

                for (int j = 0; j < labels.Length; j++)
                {
                    if (labels[j] == 1)
                    {
                        result[0] = predictProbability[j];
                    }
                    else if (labels[j] == 0)
                    {
                        result[1] = predictProbability[j];
                    }
                    else
                    {
                        result[2] = predictProbability[j];
                    }
                }
            }
            return result;
        }
コード例 #8
0
        /// <summary>
        /// Adds a new supervised training sample to the training set. If already exists, it will
        /// be replaced.
        /// </summary>
        /// <param name="sample">
        /// The sample to add
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If <c>sample</c> is <c>null</c>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If sizes of input vector or output vector are different from their expected sizes
        /// </exception>
        public void Add(TrainingSample sample)
        {
            // Validation
            Helper.ValidateNotNull(sample, "sample");
            if (sample.InputVector.Length != inputVectorLength)
            {
                throw new ArgumentException
                    ("Input vector must be of size " + inputVectorLength, "sample");
            }
            if (sample.OutputVector.Length != outputVectorLength)
            {
                throw new ArgumentException
                    ("Output vector must be of size " + outputVectorLength, "sample");
            }

            // Note that the reference is being added. (Sample is immutable)
            trainingSamples.Add(sample);
        }
コード例 #9
0
 /// <summary>
 /// Determines whether the training sample is present in the set
 /// </summary>
 /// <param name="sample">
 /// The sample to locate
 /// </param>
 /// <returns>
 /// <c>true</c> if present, <c>false</c> otherwise
 /// </returns>
 public bool Contains(TrainingSample sample)
 {
     return(trainingSamples.Contains(sample));
 }
コード例 #10
0
 /// <summary>
 /// Creates a new instance of this class
 /// </summary>
 /// <param name="trainingIteration">
 /// Current training iteration
 /// </param>
 /// <param name="trainingSample">
 /// The training sample associated with the event
 /// </param>
 public TrainingSampleEventArgs(int trainingIteration, TrainingSample trainingSample)
 {
     this.trainingIteration = trainingIteration;
     this.trainingSample = trainingSample;
 }
コード例 #11
0
 /// <summary>
 /// A protected helper function used to train single learning sample
 /// </summary>
 /// <param name="trainingSample">
 /// Training sample to use
 /// </param>
 /// <param name="currentIteration">
 /// Current training epoch (Assumed to be positive and less than <c>trainingEpochs</c>)
 /// </param>
 /// <param name="trainingEpochs">
 /// Number of training epochs (Assumed to be positive)
 /// </param>
 protected abstract void LearnSample(TrainingSample trainingSample, int currentIteration, int trainingEpochs);
コード例 #12
0
 /// <summary>
 /// Removes the given training sample
 /// </summary>
 /// <param name="sample">
 /// The sample to remove
 /// </param>
 /// <returns>
 /// <c>true</c> if successful, <c>false</c> otherwise
 /// </returns>
 public bool Remove(TrainingSample sample)
 {
     return(trainingSamples.Remove(sample));
 }
コード例 #13
0
        /// <summary>
        /// Trains the network for the given training sample (Online training mode). Note that this
        /// method trains the sample only once, irrespective of what current epoch is. The arguments
        /// are just used to find out training progress and adjust parameters depending on it.
        /// </summary>
        /// <param name="trainingSample">
        /// Training sample to use
        /// </param>
        /// <param name="currentIteration">
        /// Current training iteration
        /// </param>
        /// <param name="trainingEpochs">
        /// Number of training epochs
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If <c>trainingSample</c> is <c>null</c>
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <c>trainingEpochs</c> is not positive
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <c>currentIteration</c> is negative or, if it is not less than <c>trainingEpochs</c>
        /// </exception>
        public virtual void Learn(TrainingSample trainingSample, int currentIteration, int trainingEpochs)
        {
            Helper.ValidateNotNull(trainingSample, "trainingSample");
            Helper.ValidatePositive(trainingEpochs, "trainingEpochs");
            Helper.ValidateWithinRange(currentIteration, 0, trainingEpochs - 1, "currentIteration");

            OnBeginSample(currentIteration, trainingSample);
            LearnSample(trainingSample, currentIteration, trainingEpochs);
            OnEndSample(currentIteration, trainingSample);
        }
コード例 #14
0
        /// <summary>
        /// Hàm tạo tập dữ liệu học cho từ tập tin học đã có
        /// </summary>
        /// <param name="trainingFile">
        /// Đường dẫn đến tập tin
        /// </param>
        public void CreateTrainingSet(string trainingFile)
        {
            StreamReader reader = null;

            try
            {
                double[] dblOutputs = new double[OutputVectorLength];
                reader = new StreamReader(trainingFile);
                string strTemps = reader.ReadToEnd();
                reader.Close();

                string[] strLines = Regex.Split(strTemps, "\r\n");

                string[] strTemp = strLines[0].Split(' ');
                for (int i = 1; i < strTemp.Length; i++)
                {
                    if (strTemp[i] != "")
                    {
                        this.inputVectorLength++;
                    }
                }

                double[] dblInputs = new double[InputVectorLength];
                int numPattern = strLines.Length - 1;
                for (int i = 0; i < numPattern; i++)
                {
                    string[] strValue = strLines[i].Split(' ');

                    int iTempActual = int.Parse(strValue[0]);

                    switch (iTempActual)
                    {
                        case -1:// Xu hướng đi xuống
                            dblOutputs[0] = 1;
                            dblOutputs[1] = 0;
                            dblOutputs[2] = 0;
                            break;
                        case 0:// Không xác định được xu hướng
                            dblOutputs[0] = 0;
                            dblOutputs[1] = 1;
                            dblOutputs[2] = 0;
                            break;
                        case 1:// Xu hướng đi lên
                            dblOutputs[0] = 0;
                            dblOutputs[1] = 0;
                            dblOutputs[2] = 1;
                            break;
                    }

                    for (int j = 0; j < InputVectorLength; j++)
                    {
                        dblInputs[j] = double.Parse(strValue[j + 1].Split(':')[1]);
                    }
                    TrainingSample sample = new TrainingSample(dblInputs, dblOutputs);
                    this.Add(sample);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #15
0
 /// <summary>
 /// Creates a new instance of this class
 /// </summary>
 /// <param name="trainingIteration">
 /// Current training iteration
 /// </param>
 /// <param name="trainingSample">
 /// The training sample associated with the event
 /// </param>
 public TrainingSampleEventArgs(int trainingIteration, TrainingSample trainingSample)
 {
     this.trainingIteration = trainingIteration;
     this.trainingSample    = trainingSample;
 }
コード例 #16
0
 /// <summary>
 /// Invokes BeginSampleEvent
 /// </summary>
 /// <param name="currentIteration">
 /// Current training iteration
 /// </param>
 /// <param name="currentSample">
 /// Current sample which got trained successfully
 /// </param>
 protected virtual void OnEndSample(int currentIteration, TrainingSample currentSample)
 {
     if (EndSampleEvent != null)
     {
         EndSampleEvent(this, new TrainingSampleEventArgs(currentIteration, currentSample));
     }
 }
コード例 #17
0
 /// <summary>
 /// A protected helper function used to train single learning sample
 /// </summary>
 /// <param name="trainingSample">
 /// Training sample to use
 /// </param>
 /// <param name="currentIteration">
 /// Current training epoch (Assumed to be positive and less than <c>trainingEpochs</c>)
 /// </param>
 /// <param name="trainingEpochs">
 /// Number of training epochs (Assumed to be positive)
 /// </param>
 protected abstract void LearnSample(TrainingSample trainingSample, int currentIteration, int trainingEpochs);
コード例 #18
0
 /// <summary>
 /// Removes the given training sample
 /// </summary>
 /// <param name="sample">
 /// The sample to remove
 /// </param>
 /// <returns>
 /// <c>true</c> if successful, <c>false</c> otherwise
 /// </returns>
 public bool Remove(TrainingSample sample)
 {
     return trainingSamples.Remove(sample);
 }
コード例 #19
0
        /// <summary>
        /// Hàm tạo tập dữ liệu học cho từ tập tin học đã có
        /// </summary>
        /// <param name="trainingFile">
        /// Đường dẫn đến tập tin
        /// </param>
        public void CreateTrainingSet(string trainingFile)
        {
            StreamReader reader = null;

            try
            {
                double[] dblOutputs = new double[OutputVectorLength];
                reader = new StreamReader(trainingFile);
                string strTemps = reader.ReadToEnd();
                reader.Close();

                string[] strLines = Regex.Split(strTemps, "\r\n");


                string[] strTemp = strLines[0].Split(' ');
                for (int i = 1; i < strTemp.Length; i++)
                {
                    if (strTemp[i] != "")
                    {
                        this.inputVectorLength++;
                    }
                }

                double[] dblInputs  = new double[InputVectorLength];
                int      numPattern = strLines.Length - 1;
                for (int i = 0; i < numPattern; i++)
                {
                    string[] strValue = strLines[i].Split(' ');

                    int iTempActual = int.Parse(strValue[0]);

                    switch (iTempActual)
                    {
                    case -1:    // Xu hướng đi xuống
                        dblOutputs[0] = 1;
                        dblOutputs[1] = 0;
                        dblOutputs[2] = 0;
                        break;

                    case 0:    // Không xác định được xu hướng
                        dblOutputs[0] = 0;
                        dblOutputs[1] = 1;
                        dblOutputs[2] = 0;
                        break;

                    case 1:    // Xu hướng đi lên
                        dblOutputs[0] = 0;
                        dblOutputs[1] = 0;
                        dblOutputs[2] = 1;
                        break;
                    }

                    for (int j = 0; j < InputVectorLength; j++)
                    {
                        dblInputs[j] = double.Parse(strValue[j + 1].Split(':')[1]);
                    }
                    TrainingSample sample = new TrainingSample(dblInputs, dblOutputs);
                    this.Add(sample);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }