-
May 7th, 2024, 05:13 AM
#1
SharpLearning neural network classification with probabilities
Hi,
I am trying to classify arrays of integers of size 202 (which are physical curves) into either 'Bad' or 'Good' labels.
My observations consist of 1608 pre-labelled columns.
I have read the data from CSV file that I prepared in another application into Data-matrix (1608 over 202 2D array), and the labels into Labels_matrix (1608 1D array).
I have trained the net using Learn method, and even successfully predicted the observations of Test_matrix. Here is the working code:
Code:
using SharpLearning.Containers.Matrices;
using SharpLearning.InputOutput.Csv;
using SharpLearning.Neural;
using SharpLearning.Neural.Activations;
using SharpLearning.Neural.Layers;
using SharpLearning.Neural.Learners;
using SharpLearning.Neural.Loss;
using System;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using SharpLearning.Metrics.Classification;
using SharpLearning.Neural.Models;
using SharpLearning.Neural.Optimizers;
using System.Collections.Generic;
using SharpLearning.Neural.TargetEncoders;
string csvDataFilePath = @"C:\git\GalTesting\dsd\DATA_for_classification_learner.csv";
string csvLabelsFilePath = @"C:\git\GalTesting\dsd\labels_numbers_for_classification_learner.csv";
string csvTestFilePath = @"C:\git\GalTesting\dsd\Test_for_classification_learner.csv";
double[][] Data_matrix;
double[] Labels_matrix;
double[][] Test_matrix;
var lines = File.ReadAllLines(csvDataFilePath);
int numRows = lines.Length;
int numCols = lines[0].Split(',').Length;
Data_matrix = new double[numCols][];
for (int i = 0; i < numCols; i++)
{
Data_matrix[i] = new double[numRows];
for (int j = 0; j < numRows; j++)
{
var values1 = lines[j].Split(',');
if (double.TryParse(values1[i], out double parsedValue))
Data_matrix[i][j] = parsedValue;
else
Data_matrix[i][j] = 0; // Default value if parsing fails
}
}
lines = File.ReadAllLines(csvLabelsFilePath);
numRows = 1;
numCols = lines[0].Split(',').Length;
Labels_matrix = new double[numCols];
var values = lines[0].Split(',');
for (int j = 0; j < numCols; j++)
{
if (double.TryParse(values[j], out double parsedValue))
Labels_matrix[j] = parsedValue;
else
Labels_matrix[j] = 0; // Default value if parsing fails
}
lines = File.ReadAllLines(csvTestFilePath);
numRows = lines.Length;
numCols = lines[0].Split(',').Length;
Test_matrix = new double[numCols][];
for (int i = 0; i < numCols; i++)
{
Test_matrix[i] = new double[numRows];
for (int j = 0; j < numRows; j++)
{
var values1 = lines[j].Split(',');
if (double.TryParse(values1[i], out double parsedValue))
Test_matrix[i][j] = parsedValue;
else
Test_matrix[i][j] = 0; // Default value if parsing fails
}
}
var net = new NeuralNet();
net.Add(new InputLayer(202));
net.Add(new DenseLayer(25, Activation.Relu));
//net.Add(new DenseLayer(32, Activation.Relu));
net.Add(new SoftMaxLayer(2));
net.Initialize(1, new Random());
ClassificationNeuralNetLearner learner = new ClassificationNeuralNetLearner(net, new LogLoss());
ClassificationNeuralNetModel model = learner.Learn(Data_matrix, Labels_matrix);
double[] final_predictions = new double[Test_matrix[0].Length];
final_predictions = model.Predict(Test_matrix);
Now I need not only to classify the data into the labels, but also to know the probability that each observation was given to tha label.
How can I do that?
Thanks,
Gal
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|