Пример #1
0
        public void Play()
        {
            var iris   = AyxCsvReader.Instance.ReadCsvFileDataTable("Data/iris.csv");
            var labels = DataFormat.DataTableToList(iris, "Species").ToList();
            var codec  = new OneHotCodec(labels);
            var y      = codec.Encode(labels);
            var X      = DataFormat.DataTableToTensor(iris, 1, 2, 3, 4);

            var model = new NeuralNetwork()
                        .AddFullLayer(5)
                        .AddReLU()
                        .AddFullLayer(5)
                        .AddReLU()
                        .AddFullLayer(codec.Length)
                        .AddSoftmax()
                        .UseCrossEntropyLoss()
                        .UseGradientDescent(0.001);

            var trainer = new Trainer(model, 16, 200, true);

            trainer.StartTrain(X, y, null, null);

            var machine = new ClassificationMachine(model);

            machine.LabelCodec = codec;

            while (true)
            {
                Console.Write("input Sepal.Length:");
                var a1 = double.Parse(Console.ReadLine());
                Console.Write("input Sepal.Width:");
                var a2 = double.Parse(Console.ReadLine());
                Console.Write("input Petal.Length:");
                var a3 = double.Parse(Console.ReadLine());
                Console.Write("input Petal.Width:");
                var a4 = double.Parse(Console.ReadLine());

                var input = new TensorOld(new double[] { a1, a2, a3, a4 }, 1, 4);
                Console.WriteLine($"you input is {machine.Predict(input).First()},  Codec:{machine.LastCodecResult},  Raw:{machine.LastRawResult}");
            }
        }
Пример #2
0
        public void Play()
        {
            var trainX = MNISTReader.ReadImagesToTensor4("Data\\train-images.idx3-ubyte", 60000);
            var trainY = MNISTReader.ReadLabelsToMatrix("Data\\train-labels.idx1-ubyte", 60000);
            var testX  = MNISTReader.ReadImagesToTensor4("Data\\t10k-images.idx3-ubyte", 10000);
            var testY  = MNISTReader.ReadLabelsToMatrix("Data\\t10k-labels.idx1-ubyte", 10000);

            var nn = new NeuralNetwork()
                     .AddLayer(new ConvLayer(10, 5, 1))
                     .AddReLU()
                     .AddLayer(new MaxPooling(2))
                     .AddLayer(new ConvLayer(20, 5, 1))
                     .AddReLU()
                     .AddLayer(new MaxPooling(2))
                     .AddLayer(new FlattenLayer())
                     .AddFullLayer(100)
                     .AddSigmoid()
                     .AddFullLayer(50)
                     .AddSigmoid()
                     .AddFullLayer(10)
                     .AddSoftmaxWithCrossEntropyLoss()
                     .UseAdam();

            var cate = trainY
                       .GetRawValues()
                       .Distinct()
                       .OrderBy(a => a)
                       .Select(a => a.ToString())
                       .ToList();

            var codec = new OneHotCodec(cate);
            var norm  = new ZScoreNorm(trainX);

            var trainer = new Trainer(nn, 64, 10, true)
            {
                LabelCodec = codec,
                Normalizer = norm,
            };

            trainer.StartTrain(trainX, trainY, testX, testY);
        }
Пример #3
0
        public void OneHotTest()
        {
            var oh = new OneHotCodec(data);

            Assert.Equal(6, oh.Length);

            var test = new List <string> {
                "a", "c", "d", "a"
            };
            var expected = new TensorOld(new double[, ]
            {
                { 1, 0, 0, 0, 0, 0 },
                { 0, 0, 1, 0, 0, 0 },
                { 0, 0, 0, 1, 0, 0 },
                { 1, 0, 0, 0, 0, 0 },
            });
            var encode = oh.Encode(test);

            Assert.Equal(expected, encode);

            var decode = oh.Decode(encode);

            Assert.Equal(decode, test);
        }