Example #1
0
        private void TrainWithType(TrainingType type)
        {
            Thread t = new Thread(() =>
            {
                double learningRate = 1;
                Invoke(new Action(() =>
                {
                    learningRate = (double)num_learningRate.Value;
                }));
                trainingFunc((int)num_iters.Value, type, learningRate, x =>
                {
                    error.Add(new cPoint() { x = error.Count, y = (float)x * 100 });
                    if (error.Count % 100 == 0)
                    {
                        UpdateGraph();
                    }
                });

                Invoke(new Action(() =>
                {
                    btn_batchtrain.Enabled = true;
                }));
            });
            btn_batchtrain.Enabled = false;
            t.IsBackground = true;
            t.Start();
        }
Example #2
0
        /// <summary>
        /// Initiates Training object
        /// Select amount of words from dictionary to learn
        /// </summary>
        /// <param name="dictionary">Dictionary with word cards</param>
        /// <param name="isSwitched">Switch words in card</param>
        /// <param name="amount">Amount of cards to learn</param>
        /// <param name="maxCounter">Max counter value</param>
        /// <param name="type">Training type</param>
        /// <param name="learnedBefore">If true, Select only words that were learned in previous training type</param>
        public Training(WordsDictionary dictionary, bool isSwitched, int amount, int maxCounter, TrainingType type, bool learnedBefore)
        {
            this.dictionary = dictionary;
            this.type = type;

            // switch all cards if needed
            if (isSwitched)
            {
                foreach (var card in dictionary)
                    card.Switched = isSwitched;
            }

            IEnumerable<WordCard> cardsList = dictionary;
            // get learned cards from prevois training type
            if (learnedBefore && type != 0)
            {
                cardsList =
                    from c in cardsList
                    where c.Counter1[type - 1] >= maxCounter
                    select c;
            }

            // select cards that have counter less than maxCounter for specific training type
            cardsList = (
                from c in cardsList
                where c.Counter1[type] < maxCounter
                orderby c.LastTrained
                select c)
                .Take(amount);
            cardsToLearn = cardsList.ToList();
        }
Example #3
0
        public static IEnumerable<WordCard> FilterByCounter(IEnumerable<WordCard> dictionary, CounterFilterType filterType, int filterValue, TrainingType trainingType)
        {
            IEnumerable<WordCard> result = dictionary;
            switch (filterType)
            {
                case CounterFilterType.Equals:
                    result =
                        from c in dictionary
                        where c.Counter1[trainingType] == filterValue || c.Counter2[trainingType] == filterValue
                        select c;
                    break;
                case CounterFilterType.More:
                    result =
                        from c in dictionary
                        where c.Counter1[trainingType] > filterValue || c.Counter2[trainingType] > filterValue
                        select c;
                    break;
                case CounterFilterType.Less:
                    result =
                        from c in dictionary
                        where c.Counter1[trainingType] < filterValue || c.Counter2[trainingType] < filterValue
                        select c;
                    break;
                default:
                    throw new ArgumentException("Unsupported filter type");
            }

            return result;
        }
 public void SetUp()
 {
     maxCards = random.Next(3, 100);
     dictionary = new WordsDictionary(language1, language2);
     for (int i = 0; i < maxCards; i++)
     {
         dictionary.Add(generator.GetCardExtra(5));
     }
     trainingType = trainingTypeList[random.Next(trainingTypeList.Count)];
 }
Example #5
0
        public void TestWrittingWHint()
        {
            maxCards = 1;
            type = TrainingType.WrittingWHint;

            Training training = new Training(dictionary, isSwitched, maxCards, maxCounter, type, learnedBefore);
            var currentCard = training.NextCard();
            currentCard.Word2 = "Test";
            string actual = training.GetHint();
            string expected = "First letter is 'T', Word length is 4";

            Assert.AreEqual(expected, actual, "Validating hint");
        }
Example #6
0
        public void TestChoose()
        {
            maxCounter = 1;
            type = TrainingType.Choose;

            Training training = new Training(dictionary, isSwitched, maxCards, maxCounter, type, learnedBefore);
            CheckTrainingList(training);

            // go trough all cards in training and choose cards
            var currentCard = training.NextCard();
            while (currentCard != null)
            {
                var chooseList = training.Choose();
                CheckChooseList(chooseList, currentCard);
                currentCard = training.NextCard();
            }
        }
Example #7
0
        public void TestChooseCards0Type()
        {
            maxCounter = 1;
            type = TrainingType.Choose;

            Training training = new Training(dictionary, isSwitched, maxCards, maxCounter, type, learnedBefore);
            CheckTrainingList(training);

            // all other cards have different type as currentCard
            foreach (var card in dictionary)
                card.Type = WordType.Noun;
            var currentCard = training.NextCard();
            currentCard.Type = WordType.Verb;
            var chooseList = training.Choose(cardsToChoose);
            CheckChooseList(chooseList, currentCard);
        }
Example #8
0
        public void TestChooseCardsLessThanType()
        {
            maxCounter = 1;
            type = TrainingType.Choose;

            Training training = new Training(dictionary, isSwitched, maxCards, maxCounter, type, learnedBefore);
            CheckTrainingList(training);

            // set word type as currentCard            
            foreach (var card in dictionary)
                card.Type = WordType.Noun;
            var currentCard = training.NextCard();
            currentCard.Type = WordType.Verb;
            for (int i = 0; i < dictionary.Count - 1; i++)
                dictionary[i].Type = currentCard.Type;
            var chooseList = training.Choose(cardsToChoose);
            CheckChooseList(chooseList, currentCard);
        }
Example #9
0
        public void TestChooseCardsMoreThanAvailableType()
        {
            maxCounter = 1;
            type = TrainingType.Choose;

            Training training = new Training(dictionary, isSwitched, maxCards, maxCounter, type, learnedBefore);
            CheckTrainingList(training);

            // set word type as currentCard
            var currentCard = training.NextCard();
            foreach (var card in dictionary)
                card.Type = currentCard.Type;
            var chooseList = training.Choose(cardsToChoose);
            CheckChooseList(chooseList, currentCard);
        }
Example #10
0
 public void SetUp()
 {
     card = new CardsGenerator().GetCardExtra(5);
     trainingType = trainingTypeList[random.Next(trainingTypeList.Count)];
 }
 protected TrainingTypeModel(TrainingType type)
 {
     Type = type;
 }
 private OneRightManyWrong(TrainingType type)
     : base(type)
 {
 }
 protected internal OneRight(TrainingType type)
     : base(type)
 {
 }
 private ManyRight(TrainingType type)
     : base(type)
 {
 }
Example #15
0
        public void TestWrittingWHintLearnedBeforeTrueOK()
        {
            type = TrainingType.WrittingWHint;
            learnedBefore = true;
            int cardsToLearn = maxCards / 3;

            // set counter for choose to maxCounter for cardsToLearn
            foreach (var card in dictionary)
                card.Counter1[TrainingType.Choose] = 0;
            for (int i = 0; i < cardsToLearn; i++)
                dictionary[i].Counter1[TrainingType.Choose] = maxCounter;

            Training training = new Training(dictionary, isSwitched, cardsToLearn + 1, maxCounter, type, learnedBefore);
            CheckTrainingList(training);
            Assert.AreEqual(cardsToLearn, training.TotalCards, "Validating number of cards to learn");
        }
Example #16
0
        public void Train(double[][] inputs, double[][] expected, TrainingType type, int iterations)
        {
            int i, j;

            switch (type)
            {
                case TrainingType.BackPropogation:

                    lock (this)
                    {

                        for (i = 0; i < iterations; i++)
                        {

                            InitializeLearning(); // set all weight changes to zero

                            for (j = 0; j < inputs.Length; j++)
                                BackPropogation_TrainingSession(this, inputs[j], expected[j]);

                            ApplyLearning(); // apply batch of cumlutive weight changes
                        }

                    }
                    //Console.WriteLine(  "");
                    break;
                default:
                    throw new ArgumentException("Unexpected TrainingType");
            }
        }
Example #17
0
        public void TestWrittingWHintLearnedBeforeTrueNOK()
        {
            type = TrainingType.WrittingWHint;
            learnedBefore = true;
            int cardsToLearn = maxCards / 3;

            // set counter for choose to maxCounter for all cards
            foreach (var card in dictionary)
                card.Counter1[TrainingType.Choose] = maxCounter;

            // select cards with counter maxCounter + 1
            Training training = new Training(dictionary, isSwitched, cardsToLearn, maxCounter + 1, type, learnedBefore);
            CheckTrainingList(training);
            Assert.AreEqual(0, training.TotalCards, "Validating number of cards to learn");
        }
Example #18
0
        private void TrainFor(int iterations, TrainingType type, double learningRate, Action<double> callbackWithError)
        {
            OneToManyMap<GraphMap<FeatureVector, WeightMatrix>.ILinkable, Pair<GraphMap<FeatureVector, WeightMatrix>.ILinkable, GraphMap<FeatureVector, WeightMatrix>.Link<WeightMatrix>>> backwards;
            Queue<GraphMap<FeatureVector, WeightMatrix>.ILinkable> inputVectors;
            Queue<GraphMap<FeatureVector, WeightMatrix>.ILinkable> outputVectors;
            GetGraphOrder(out backwards, out inputVectors, out outputVectors);

            List<Dictionary<string, FeatureVector>> trainingData = new List<Dictionary<string, FeatureVector>>();

            // Get training data
            foreach (DataGridViewRow row in data_training.Rows)
            {
                string config = row.Cells[2].Value.ToString();
                trainingData.Add(StateFromConfiguration(config).ToDictionary(x => x.name));
            }

            int iterationCounter = 0;

            double averager = 1.0 / trainingData.Count;
            double learningrate = learningRate;

            // begin training...
            while (true)
            {
                double squaredTrainingError = 0;
                int totalTrainingNeuronCount = 0;

                // Matrix weight gradients
                Dictionary<GraphMap<FeatureVector, WeightMatrix>.Link<WeightMatrix>, Matrix> dw = new Dictionary<GraphMap<FeatureVector, WeightMatrix>.Link<WeightMatrix>, Matrix>();

                foreach (var inputVector in inputVectors)
                {
                    foreach (var edge in inputVector.Edges)
                    {
                        // make space to store the weight gradients
                        dw[edge.Value] = new Matrix(edge.Value.Data.weights.RowCount, edge.Value.Data.weights.ColumnCount);
                    }
                }

                foreach (var trainingCase in trainingData)
                {
                    double perTrainingSquaredError = 0;
                    int perTrainingNeuronCount = 0;

                    // errors in all input vectors
                    Dictionary<string, Matrix> dy = new Dictionary<string, Matrix>();

                    // set all feature vectors to a training case
                    foreach (var feature in trainingCase)
                    {
                        if (vectors[feature.Key].Data.layer != LayerType.OUTPUT)
                        {
                            vectors[feature.Key].Data.state = feature.Value.state;
                        }
                    }

                    // forward prop
                    foreach (var outputVector in outputVectors)
                    {
                        var sources = backwards[outputVector];
                        outputVector.Data.state = new Matrix(outputVector.Data.state.RowCount, outputVector.Data.state.ColumnCount);
                        foreach (var source in sources)
                        {
                            var x = Training.AddBiasTerm(source.a.Data.state);
                            outputVector.Data.state += ((BaseMatrix)source.b.Data.weights * (BaseMatrix)x);
                        }
                        outputVector.Data.state = outputVector.Data.type.Func(outputVector.Data.state);
                    }

                    // Calculate errors
                    foreach (var output in outputs)
                    {
                        dy[output.Data.name] = -(trainingCase[output.Data.name].state - output.Data.state);
                        for (int i = 0; i < output.Data.state.RowCount; i++)
                        {
                            double error = dy[output.Data.name][i, 0];
                            perTrainingSquaredError += error * error;
                            perTrainingNeuronCount++;
                        }
                    }

                    squaredTrainingError += perTrainingSquaredError;
                    totalTrainingNeuronCount += perTrainingNeuronCount;

                    // Establish space for the input vectors
                    foreach (var inputVec in inputVectors)
                    {
                        dy[inputVec.Data.name] = new Matrix(inputVec.Data.state.RowCount, inputVec.Data.state.ColumnCount);
                    }

                    // backprop and add to weight gradients
                    foreach (var inputVec in inputVectors)
                    {
                        foreach (var edge in inputVec.Edges)
                        {
                            Matrix dHidden;
                            Matrix dWeights;
                            var x = Training.AddBiasTerm(inputVec.Data.state);
                            GraphMap<FeatureVector, WeightMatrix>.ILinkable asd;
                            asd = edge.Key;
                            FeatureVector data = asd.Data;
                            Training.BackpropLayer(dy[data.name], x, edge.Value.Data.weights, data.type, out dHidden, out dWeights);
                            dy[inputVec.Data.name] += Training.RemoveBiasTerm(dHidden);

                            if (type == TrainingType.Batch)
                            {
                                dw[edge.Value] -= dWeights;
                            }
                            else if (type == TrainingType.Online)
                            {
                                dw[edge.Value] = -dWeights;
                            }
                        }
                    }

                    // update weights
                    if (type == TrainingType.Online)
                    {
                        foreach (var inputVec in inputVectors)
                        {
                            foreach (var edge in inputVec.Edges)
                            {
                                edge.Value.Data.weights = (edge.Value.Data.weights + dw[edge.Value] * learningrate);
                            }
                        }
                    }
                }

                if (type == TrainingType.Batch)
                {
                    // update weights
                    foreach (var inputVec in inputVectors)
                    {
                        foreach (var edge in inputVec.Edges)
                        {
                            edge.Value.Data.weights = (edge.Value.Data.weights + dw[edge.Value] * averager * learningrate);
                        }
                    }
                }

                // calculate total error
                double totalError = Math.Sqrt(squaredTrainingError) / totalTrainingNeuronCount;
                callbackWithError(totalError);
                //Debug.WriteLine(totalError);

                iterationCounter++;

                // repeat until stopped
                if (iterationCounter == iterations)
                {
                    break;
                }
            }
        }