public static void Run() { //Logistic Regression is one of the first models newcomers to Deep Learning are implementing. //The focus of this tutorial is to show how to do logistic regression using Gluon API. var ctx = mx.Cpu(); int train_data_size = 1000; int val_data_size = 100; var(train_x, train_ground_truth_class) = GetRandomData(train_data_size, ctx); var train_dataset = new ArrayDataset((train_x, train_ground_truth_class)); train_dataloader = new DataLoader(train_dataset, batch_size: batch_size, shuffle: true); var(val_x, val_ground_truth_class) = GetRandomData(val_data_size, ctx); var val_dataset = new ArrayDataset((val_x, val_ground_truth_class)); val_dataloader = new DataLoader(val_dataset, batch_size: batch_size, shuffle: true); net = new HybridSequential(); net.Add(new Dense(units: 10, activation: ActivationType.Relu)); net.Add(new Dense(units: 10, activation: ActivationType.Relu)); net.Add(new Dense(units: 10, activation: ActivationType.Relu)); net.Add(new Dense(units: 1)); net.Initialize(new Xavier()); loss = new SigmoidBinaryCrossEntropyLoss(); trainer = new Trainer(net.CollectParams(), new SGD(learning_rate: 0.1f)); accuracy = new Accuracy(); f1 = new F1(); int epochs = 10; float threshold = 0.5f; foreach (var e in Enumerable.Range(0, epochs)) { var avg_train_loss = TrainModel() / train_data_size; var avg_val_loss = ValidateModel(threshold) / val_data_size; Console.WriteLine($"Epoch: {e}, Training loss: {avg_train_loss}, Validation loss: {avg_val_loss}, Validation accuracy: {accuracy.Get().Item2}, F1 score: {f1.Get().Item2}"); accuracy.Reset(); } }
public static void Run() { //Logistic Regression is one of the first models newcomers to Deep Learning are implementing. //The focus of this tutorial is to show how to do logistic regression using Gluon API. var ctx = mx.Cpu(); int train_data_size = 1000; int val_data_size = 100; int batch_size = 10; var(train_x, train_ground_truth_class) = GetRandomState(train_data_size, ctx); var train_dataset = new ArrayDataset(train_x, train_ground_truth_class); var train_dataloader = new DataLoader(train_dataset, batch_size: batch_size, shuffle: true); var(val_x, val_ground_truth_class) = GetRandomState(val_data_size, ctx); var val_dataset = new ArrayDataset(val_x, val_ground_truth_class); var val_dataloader = new DataLoader(val_dataset, batch_size: batch_size, shuffle: true); var net = new HybridSequential(); net.Add(new Dense(units: 10, activation: ActivationType.Relu)); net.Add(new Dense(units: 10, activation: ActivationType.Relu)); net.Add(new Dense(units: 10, activation: ActivationType.Relu)); net.Add(new Dense(units: 1)); net.Initialize(new Xavier()); var loss = new SigmoidBinaryCrossEntropyLoss(); var trainer = new Trainer(net.CollectParams(), new SGD(learning_rate: 0.1f)); var accuracy = new Accuracy(); var f1 = new F1(); float cumulative_train_loss = 0; int i = 0; foreach (var item in train_dataloader) { var data = item[0]; var label = item[1]; } }