private ILoss <int[]> CreateLoss() { ILoss <int[]> loss = null; switch (this.TaskParameters.Loss.Name) { case "LogLikelihood": default: loss = new LogLikelihoodLoss(); break; case "CTC": loss = new CTCLoss(); break; } JsonSerializer jsonSerializer = new JsonSerializer(); using (JTokenReader jtokenReader = new JTokenReader(this.TaskParameters.Loss.Parameters)) { jsonSerializer.Populate(jtokenReader, loss); } return(loss); }
public void learn_test() { #region doc_learn // Ensure that results are reproducible Accord.Math.Random.Generator.Seed = 0; // Generate some data to be learned double[][] inputs = { new double[] { +1.0312479734420776 }, new double[] { +0.99444115161895752 }, new double[] { +0.21835240721702576 }, new double[] { +0.47197291254997253 }, new double[] { +0.68701112270355225 }, new double[] { -0.58556461334228516 }, new double[] { -0.64154046773910522 }, new double[] { -0.66485315561294556 }, new double[] { +0.37940266728401184 }, new double[] { -0.61046308279037476 } }; // Create a new One-class SVM learning algorithm var teacher = new OneclassSupportVectorLearning <Linear>() { Kernel = new Linear(), // or, for example, 'new Gaussian(0.9)' Nu = 0.1 }; // Learn a support vector machine var svm = teacher.Learn(inputs); // Test the machine double[] prediction = svm.Score(inputs); // Compute the log-likelihood of the answer double ll = new LogLikelihoodLoss().Loss(prediction); #endregion Assert.AreEqual(-1.6653345369377348E-16, ll, 1e-10); Assert.AreEqual(2, svm.Weights.Length); Assert.AreEqual(0.39198910030993617, svm.Weights[0], 1e-10); Assert.AreEqual(0.60801089969006383, svm.Weights[1], 1e-10); Assert.AreEqual(inputs[0][0], svm.SupportVectors[0][0], 1e-10); Assert.AreEqual(inputs[7][0], svm.SupportVectors[1][0], 1e-10); }
public void TinyShakespeareTest1() { const int letterSize = 5; Assembly assembly = Assembly.GetExecutingAssembly(); string path = Path.Combine(Path.GetDirectoryName(assembly.Location), @"..\TestData\tinyshakespeare.txt"); string alltext = File.ReadAllText(path).ToUpperInvariant(); // calculate alphabet size HashSet <char> chars = new HashSet <char>(); for (int i = 0, ii = alltext.Length; i < ii; i++) { chars.Add(alltext[i]); } Dictionary <char, int> alphabet = chars.OrderBy(x => x) .Select((x, i) => Tuple.Create(x, i)) .ToDictionary(x => x.Item1, x => x.Item2); // create character vectors Tensor letters = new Tensor(null, new[] { alphabet.Count + 1, 1, 1, letterSize }); letters.Randomize(); // create network string architechture = string.Format( CultureInfo.InvariantCulture, "1x1x{0}~20-{1}LSTM~TH~SM", alphabet.Count, alphabet.Count + 1); Network network = Network.FromArchitecture(architechture); ////string a = network.Architecture; // learn network /*ILoss<int[]> loss = new CTCLoss() * { * BlankLabelIndex = alphabet.Count * };*/ ILoss <int[]> loss = new LogLikelihoodLoss(); ITrainingAlgorithm algorithm = new SGD() { LearningRate = 0.01f }; Trainer <int[]> trainer = new Trainer <int[]>() { BatchSize = 1, ClipValue = 10.0f }; ////string[] words = alltext.Split(new char[] { ' ', '\n', '.', ',', ';', ':', '?', '!' }, StringSplitOptions.RemoveEmptyEntries); string[] words = Enumerable.Repeat("FIRST", 1000).ToArray(); trainer.RunEpoch( network, words.Select(w => { /*Tensor x = new Tensor(new Layout(network.InputLayout, w.Length)); * for (int i = 0, ii = w.Length; i < ii; i++) * { * Tensor.Copy(letterSize, letters, alphabet[w[i]] * letterSize, x, i * letterSize); * }*/ Tensor x = new Session().Concat( w.Select(ch => Tensor.OneHot(null, network.InputShape, 0, 0, 0, alphabet[ch])).ToArray(), (int)Axis.B); int[] y = new int[w.Length]; for (int i = 0, ii = y.Length; i < ii; i++) { y[i] = alphabet[i == ii - 1 ? ' ' : w[i + 1]]; } return(x, y); }), 0, algorithm, loss, CancellationToken.None); // test network foreach (string w in words.Where(x => x.Length > 1)) { string w1 = w.Substring(0, w.Length - 1); Tensor x = new Tensor(null, network.InputShape.Reshape(Axis.B, w1.Length)); for (int i = 0, ii = w1.Length; i < ii; i++) { Vectors.Copy(letterSize, letters.Weights, alphabet[w1[i]] * letterSize, x.Weights, i * letterSize); } /* Tensor x = Tensor.OneHot( * new Layout(network.InputLayout, w1.Length), * w1.Select(ch => alphabet[ch]).ToList());*/ Tensor y = network.Forward(null, x); int start = y.Strides[0] * (y.Shape.GetAxis(Axis.B) - 1); int argmax = Vectors.ArgMax(y.Strides[0], y.Weights, start); int classIndex = argmax - start; char res = alphabet.ElementAt(classIndex).Key; Console.WriteLine("{0} -> {1} + {2} {3} {4}", w, w1, classIndex, res, y.Weights[argmax]); } /*foreach (string w in new string[] { "AAAA", "AA" }) * { * Tensor x = Tensor.OneHot( * new Layout(network.InputLayout, w.Length), * w.Select(ch => alphabet[ch]).ToList()); * * Tensor y = network.Forward(x, true); * network.Backward(x, y, y); * }*/ }
public void XorTest1() { const int AlphabetSize = 16; const int VectorSize = 4; const int BatchSize = 3000; const int Epochs = 200; const int TestBatchSize = 3000; Random random = new Random(0); string[] classes = Enumerable.Range(0, AlphabetSize).Select(v => v.ToString(CultureInfo.InvariantCulture)).ToArray(); ClassificationNetwork network = ClassificationNetwork.FromArchitecture("1x1x4~80-80-80-16LSTM", classes); float[] vectors = new RandomGeneratorF().Generate(AlphabetSize * VectorSize); (Tensor, int[]) createSample(int size) { Tensor input = new Tensor(null, new[] { size, 1, 1, VectorSize }); int[] truth = new int[size]; int v = 0; for (int i = 0; i < size; i++) { v ^= random.Next(0, AlphabetSize); Vectors.Copy(VectorSize, vectors, v * VectorSize, input.Weights, i * VectorSize); if (i > 0) { truth[i - 1] = v; } } return(input, truth); } // train the network Trainer <int[]> trainer = new Trainer <int[]>() { ClipValue = 2.0f }; SGD sgd = new SGD(); ILoss <int[]> loss = new LogLikelihoodLoss(); for (int epoch = 0; epoch < Epochs; epoch++) { (Tensor, int[])sample = createSample(BatchSize); TrainingResult result = trainer.RunEpoch( network, Enumerable.Repeat(sample, 1), epoch, sgd, loss, CancellationToken.None); Console.WriteLine(result.CostLoss); } // test the network (Tensor x, int[] expected) = createSample(TestBatchSize); Tensor y = network.Forward(null, x); ////y.Reshape(testBatchSize - 1); ////expected.Reshape(testBatchSize - 1); float error = loss.Loss(y, expected, false); Console.WriteLine(y); Console.WriteLine(expected); ////Console.WriteLine(y.Axes[1]); Console.WriteLine(error); ////Assert.IsTrue(errorL1 < 0.01, errorL1.ToString(CultureInfo.InvariantCulture)); }