Exemple #1
0
        /// <summary>
        /// The main applicaton entry point.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            // set up a machine learning context
            var context = new MLContext();

            // load training and test data
            Console.WriteLine("Loading data...");
            var data = context.Data.LoadFromTextFile <HeartData>(dataPath, hasHeader: false, separatorChar: ',');

            // split the data into a training and test partition
            var partitions = context.Data.TrainTestSplit(data, testFraction: 0.4);

            // load training and testing data
            var training = context.Data.CreateEnumerable <HeartData>(partitions.TrainSet, reuseRowObject: false);
            var testing  = context.Data.CreateEnumerable <HeartData>(partitions.TestSet, reuseRowObject: false);

            // set up data arrays
            var training_data   = training.Select(v => v.GetFeatures()).ToArray();
            var training_labels = training.Select(v => v.GetLabel()).ToArray();
            var testing_data    = testing.Select(v => v.GetFeatures()).ToArray();
            var testing_labels  = testing.Select(v => v.GetLabel()).ToArray();

            // build features and labels
            var features = NetUtil.Var(new int[] { 7 }, DataType.Float);
            var labels   = NetUtil.Var(new int[] { 1 }, DataType.Float);

            // build the network
            var network = features
                          .Dense(16, CNTKLib.ReLU)
                          .Dense(128, CNTKLib.ReLU)
                          .Dense(1, CNTKLib.Sigmoid)
                          .ToNetwork();

            Console.WriteLine("Model architecture:");
            Console.WriteLine(network.ToSummary());

            // set up the loss function and the classification error function
            var lossFunc  = CNTKLib.BinaryCrossEntropy(network.Output, labels);
            var errorFunc = NetUtil.BinaryClassificationError(network.Output, labels);

            // use the Adam learning algorithm
            var learner = network.GetAdamLearner(
                learningRateSchedule: (0.001, 1),
                momentumSchedule: (0.9, 1),
                unitGain: true);

            // set up a trainer and an evaluator
            var trainer   = network.GetTrainer(learner, lossFunc, errorFunc);
            var evaluator = network.GetEvaluator(errorFunc);

            // train the model
            Console.WriteLine("Epoch\tTrain\tTrain\tTest");
            Console.WriteLine("\tLoss\tError\tError");
            Console.WriteLine("-----------------------------");

            var maxEpochs     = 100;
            var batchSize     = 1;
            var loss          = new double[maxEpochs];
            var trainingError = new double[maxEpochs];
            var testingError  = new double[maxEpochs];
            var batchCount    = 0;

            for (int epoch = 0; epoch < maxEpochs; epoch++)
            {
                // train one epoch on batches
                loss[epoch]          = 0.0;
                trainingError[epoch] = 0.0;
                batchCount           = 0;
                training_data.Index().Shuffle().Batch(batchSize, (indices, begin, end) =>
                {
                    // get the current batch
                    var featureBatch = features.GetBatch(training_data, indices, begin, end);
                    var labelBatch   = labels.GetBatch(training_labels, indices, begin, end);

                    // train the network on the batch
                    var result = trainer.TrainBatch(
                        new[] {
                        (features, featureBatch),
                        (labels, labelBatch)
                    },
Exemple #2
0
        /// <summary>
        /// The main program entry point.
        /// </summary>
        /// <param name="args">The command line parameters.</param>
        static void Main(string[] args)
        {
            // check the compute device
            Console.WriteLine("Checking compute device...");
            Console.WriteLine($"  Using: {NetUtil.CurrentDevice.AsString()}");

            // unpack archive
            Console.WriteLine("Unpacking archive...");
            if (!File.Exists("x_train_imdb.bin"))
            {
                ZipFile.ExtractToDirectory("imdb_data.zip", ".");
            }

            // load training and test data
            Console.WriteLine("Loading data files...");
            var sequenceLength  = 500;
            var training_data   = DataUtil.LoadBinary <float>("x_train_imdb.bin", 25000, sequenceLength);
            var training_labels = DataUtil.LoadBinary <float>("y_train_imdb.bin", 25000);
            var testing_data    = DataUtil.LoadBinary <float>("x_test_imdb.bin", 25000, sequenceLength);
            var testing_labels  = DataUtil.LoadBinary <float>("y_test_imdb.bin", 25000);

            Console.WriteLine($"  Records for training: {training_data.Length}");
            Console.WriteLine($"  Records for testing:  {testing_data.Length}");

            // build features and labels
            var features = NetUtil.Var(new int[] { 1 }, CNTK.DataType.Float);
            var labels   = NetUtil.Var(new int[] { 1 }, CNTK.DataType.Float,
                                       dynamicAxes: new List <CNTK.Axis>()
            {
                CNTK.Axis.DefaultBatchAxis()
            });

            // build the network
            var lstmUnits = 32;
            var network   = features
                            .OneHotOp(10000, true)
                            .Embedding(32)
                            .LSTM(lstmUnits, lstmUnits)
                            .Dense(1, CNTKLib.Sigmoid)
                            .ToNetwork();

            Console.WriteLine("Model architecture:");
            Console.WriteLine(network.ToSummary());

            // set up the loss function and the classification error function
            var lossFunc  = CNTKLib.BinaryCrossEntropy(network.Output, labels);
            var errorFunc = NetUtil.BinaryClassificationError(network.Output, labels);

            // use the Adam learning algorithm
            var learner = network.GetAdamLearner(
                learningRateSchedule: (0.001, 1),
                momentumSchedule: (0.9, 1),
                unitGain: true);

            // set up a trainer and an evaluator
            var trainer   = network.GetTrainer(learner, lossFunc, errorFunc);
            var evaluator = network.GetEvaluator(errorFunc);

            // train the model
            Console.WriteLine("Epoch\tTrain\tTrain\tTest");
            Console.WriteLine("\tLoss\tError\tError");
            Console.WriteLine("-----------------------------");

            var maxEpochs     = 10;
            var batchSize     = 128;
            var loss          = new double[maxEpochs];
            var trainingError = new double[maxEpochs];
            var testingError  = new double[maxEpochs];
            var batchCount    = 0;

            for (int epoch = 0; epoch < maxEpochs; epoch++)
            {
                // train one epoch on batches
                loss[epoch]          = 0.0;
                trainingError[epoch] = 0.0;
                batchCount           = 0;
                training_data.Batch(batchSize, (data, begin, end) =>
                {
                    // get the current batch
                    var featureBatch = features.GetSequenceBatch(sequenceLength, training_data, begin, end);
                    var labelBatch   = labels.GetBatch(training_labels, begin, end);

                    // train the network on the batch
                    var result = trainer.TrainBatch(
                        new[] {
                        (features, featureBatch),
                        (labels, labelBatch)
                    },
Exemple #3
0
        /// <summary>
        /// The main program entry point.
        /// </summary>
        /// <param name="args">The command line parameters.</param>
        static void Main(string[] args)
        {
            // set up a machine learning context
            var context = new MLContext();

            // load the spam dataset in memory
            Console.WriteLine("Loading data...");
            var data = context.Data.LoadFromTextFile <SpamData>(
                path: dataPath,
                hasHeader: true,
                separatorChar: '\t');

            // use 80% for training and 20% for testing
            var partitions = context.Data.TrainTestSplit(data, testFraction: 0.3);

            // set up a pipeline to featurize the text
            Console.WriteLine("Featurizing text...");
            var pipeline = context.Transforms.Text.FeaturizeText(
                outputColumnName: "Features",
                inputColumnName: nameof(SpamData.Message));

            // create a model
            var model = pipeline.Fit(partitions.TrainSet);

            // create training and testing datasets
            var trainingData = model.Transform(partitions.TrainSet);
            var testingData  = model.Transform(partitions.TestSet);

            // create training and testing enumerations
            var training = context.Data.CreateEnumerable <ProcessedData>(trainingData, reuseRowObject: false);
            var testing  = context.Data.CreateEnumerable <ProcessedData>(testingData, reuseRowObject: false);

            // set up data arrays
            var training_data   = training.Select(v => v.GetFeatures()).ToArray();
            var training_labels = training.Select(v => v.GetLabel()).ToArray();
            var testing_data    = testing.Select(v => v.GetFeatures()).ToArray();
            var testing_labels  = testing.Select(v => v.GetLabel()).ToArray();

            // report shape of dataset
            var nodeCount = training_data.First().Length;

            Console.WriteLine($"  Embedded text data in {nodeCount} dimensions");

            // build features and labels
            var features = NetUtil.Var(new int[] { nodeCount }, DataType.Float);
            var labels   = NetUtil.Var(new int[] { 1 }, DataType.Float);

            // build the network
            var network = features
                          .Dense(16, CNTKLib.ReLU)
                          .Dense(16, CNTKLib.ReLU)
                          .Dense(1, CNTKLib.Sigmoid)
                          .ToNetwork();

            Console.WriteLine("Model architecture:");
            Console.WriteLine(network.ToSummary());

            // set up the loss function and the classification error function
            var lossFunc  = CNTKLib.BinaryCrossEntropy(network.Output, labels);
            var errorFunc = NetUtil.BinaryClassificationError(network.Output, labels);

            // use the Adam learning algorithm
            var learner = network.GetAdamLearner(
                learningRateSchedule: (0.001, 1),
                momentumSchedule: (0.9, 1),
                unitGain: true);

            // set up a trainer and an evaluator
            var trainer   = network.GetTrainer(learner, lossFunc, errorFunc);
            var evaluator = network.GetEvaluator(errorFunc);

            // train the model
            Console.WriteLine("Epoch\tTrain\tTrain\tTest");
            Console.WriteLine("\tLoss\tError\tError");
            Console.WriteLine("-----------------------------");

            var maxEpochs     = 10;
            var batchSize     = 64;
            var loss          = new double[maxEpochs];
            var trainingError = new double[maxEpochs];
            var testingError  = new double[maxEpochs];
            var batchCount    = 0;

            for (int epoch = 0; epoch < maxEpochs; epoch++)
            {
                // train one epoch on batches
                loss[epoch]          = 0.0;
                trainingError[epoch] = 0.0;
                batchCount           = 0;
                training_data.Index().Shuffle().Batch(batchSize, (indices, begin, end) =>
                {
                    // get the current batch
                    var featureBatch = features.GetBatch(training_data, indices, begin, end);
                    var labelBatch   = labels.GetBatch(training_labels, indices, begin, end);

                    // train the network on the batch
                    var result = trainer.TrainBatch(
                        new[] {
                        (features, featureBatch),
                        (labels, labelBatch)
                    },