Archive
Improving ML.NET accuracy by connecting 2 neural networks #AI #ML

In this case, I am using the Issue Classification scenario in ML.NET, where I initially trained a network to predict an output on 24 inputs. The result was a very poor 10%, but I had an idea, to create a second training set, based on the inital inputs, plus the outputs (The SCORE array) of the network (31 elements).
By creating a new table, with the initial 24 inputs, the training output, and the scrore array, i.e. another 31 floats, I then created a table that was 56 columns wide, and I used that again to train on ML.NET.
Using this second-level neural network, I effectively created a neural network with twice the number of layers, and the accuracy went from 10% to 30%.
I am hoping to repeat this process another number of times to gradually increase the accuracy up to a usable level of 90%.
No code examples yet; but if the repeated process actually increases this right up to 90%+, then I’ll share!
Dealing with #Overfitting in ML.NET (#AI / #ML)

One of the issues with ML.NET, is that it will tend to be over-eager to provide a prediction, even when there is not enough information to go on, in order to make any sort of accurate prediction. This is where you should also look at the Score Property, and take the Max Value of this array in order to determine the level of confidence.
If you then plot confidence against correct predictions and incorrect predictions, and hopefully you should see that incorrect predictions should lie primarily on the lower end of the confidence scale, and correct predictions should lie on the higher end of the confidence scale.
Ideally, you should see a clearer delimination between correct and incorrect, to allow you say that if confidence is less than .3 (or whatever), then the prediction is unknown, not the wild guess that ML.NET has suggested.
To generate this graph, here is the SQL I used;
create table PercentileGraph
(
id int identity(0,1),
[Start] as cast(id as float)/100,
[End] as cast(id as float)/100 + 0.01,
Correct int,
Incorrect int,
)while 1=1
begin
insert into PercentileGraph default values
end— Run for a second, then stop
delete from PercentileGraph where id>100
select * from PercentileGraph
update PercentileGraph set correct =
(
select count(*) from Evaluation where
Model=Predicted
and ModelConfidence>=PercentileGraph.[start]
and ModelConfidence<=PercentileGraph.[end]
)update PercentileGraph set incorrect =
(
select count(*) from Evaluation where
Model<>Predicted
and ModelConfidence>=PercentileGraph.[start]
and ModelConfidence<=PercentileGraph.[end]
)select * from PercentileGraph
#DES Encryption using #BouncyCastle in C#

DES or the Data Encryption Standard, is a classic symetric encryption algorithm that is largely been superceeded by AES and 3DES nowadays, but you may have a legacy format that still uses DES, so you will need to interoperate with it.
This code example uses the BouncyCastle library in C# to perform DES encryption and decryption with an 8 byte (64 bit) key, and is available here; https://github.com/infiniteloopltd/DES
It is a 8 byte block cypher, like Blowfish, so your ciphertext will be bloated by a maximum of 8 bytes – So it’s economicical with memory on short plaintexts.
I’ve used byte arrays here as inputs and outputs for flexibility;
private static byte[] DES_Encrypt(byte[] input, byte[] key)
{
var engine = new DesEngine();
var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
cipher.Init(true, new KeyParameter(key));
var cipherText = new byte[cipher.GetOutputSize(input.Length)];
var outputLen = cipher.ProcessBytes(input, 0, input.Length, cipherText, 0);
cipher.DoFinal(cipherText, outputLen);
return cipherText;
}
And the decyption is as follows;
private static byte[] DES_Decrypt(byte[] key, byte[] cipherText)
{
var engine = new DesEngine();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine));
cipher.Init(false, new KeyParameter(key));
var plainText = new byte[cipher.GetOutputSize(cipherText.Length)];
var outputLen = cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0);
cipher.DoFinal(plainText, outputLen);
return plainText;
}
“Hello World” in #GeneticAlgorithms in C#

Genetic algoritms are a computational representation of what happens in nature, how evolution solves problems, so it applies a “survival of the fittest” approach to solving a problem.
Like Machine Learning, instead of prescribing the solution to a problem you provide problem examples and corresponding solutions, and let the GA (Genetic Algorithm) try to figure out how to solve that problem.
Like in nature, (If we ignore religion for a moment!), nobody designed a cheetah to run faster than a gazelle, but when presented with a problem of a fast gazelle, the solution was to run faster than it. Other solutions, like running backwards, were not effective, and so were not selected for.
So, TL;DR; here is the github repo, which as code in C# using GeneticSharp to solve a very simple problem; https://github.com/infiniteloopltd/geneticAlgorithm_helloWorld
Here, the problem posed is simple addition;
// 1 + 1 = 2
// 2 + 2 = 4
// 3 + 2 = 5
However, you can substitute the parameters with more complex equations, and the GA will still try to solve it, it just will take longer.
The “Genes” of the GA are addition, substration, multiplation, division, and constant numbers (1,2,3). So, it will probably struggle to solve Sin(), Tan(), Cos() etc. Like in the same way that DNA is limited to making protiens, so you can’t make Petrol using DNA.
So, to wrap up this post, here is the code to run a simple equation solver using Genetic Algorithms;
static void Main(string[] args)
{
// Problem: Simple addition
// 1 + 1 = 2
// 2 + 2 = 4
// 3 + 2 = 5
var myInputs = new List<FunctionBuilderInput>
{
new FunctionBuilderInput(new List<double> {1, 1}, 2),
new FunctionBuilderInput(new List<double> {2, 2}, 4),
new FunctionBuilderInput(new List<double> {3, 2}, 5)
};
var myFitness = new FunctionBuilderFitness(myInputs.ToArray());
var myChromosome = new FunctionBuilderChromosome(myFitness.AvailableOperations, 5);
var selection = new EliteSelection();
var crossover = new ThreeParentCrossover();
var mutation = new UniformMutation(true);
var fitness = myFitness;
var chromosome = myChromosome;
var population = new Population(100, 200, chromosome)
{
GenerationStrategy = new PerformanceGenerationStrategy()
};
var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
{
Termination = new FitnessThresholdTermination(0)
};
ga.GenerationRan += delegate
{
Console.Clear();
var bestChromosome = ga.Population.BestChromosome;
Console.WriteLine(“Generations: {0}”, ga.Population.GenerationsNumber);
Console.WriteLine(“Fitness: {0,10}”, bestChromosome.Fitness);
Console.WriteLine(“Time: {0}”, ga.TimeEvolving);
Console.WriteLine(“Speed (gen/sec): {0:0.0000}”, ga.Population.GenerationsNumber / ga.TimeEvolving.TotalSeconds);
var best = bestChromosome as FunctionBuilderChromosome;
Console.WriteLine(“Function: {0}”, best.BuildFunction());
};
ga.Start();
Console.WriteLine(“Evolved.”);
Console.ReadKey();
}
Cryptanalysis with #ML.NET (#AI / #ML)

Disclaimer: This approach works only when you have a limited-length plaintext, and plenty (millions) of examples of cyphertext / plaintext pairs. I achieved 90%+ accuracy with this approach, it’s not perfect, and requires several days of training (8 hours per character of plaintext)
So, imagine you have a database held in SQL server that holds a list of cyphertext and corresponding plaintext examples – even if you don’t even know the cypher used, you can still use ML.NET as a means of Cryptanalysis. Without knowing the cypher used, you can’t use brute-force as an approach.
What I did – I broke the cyphertext into individual columns – So one column represented one character of the cyphertext, and I did the same with the plaintext, breaking up the plaintext into one column per character.
Then I ran a classification model for 8 hours on 1 million rows of examples, using the ML.NET model builder, providing each column of the cyphertext as the input, and one column at a time of the plaintext as the label (output).
After 8 days, I had 8 different ML.NET models that could decode the cyphertext.
WITHOUT – even knowing what cypher was being used.
RESULT
Restoring a cancelled ML.NET trained network #AI #MachineLearning #ML.NET

If you’re using the ML.NET VS Add-in to train your models, you may find it really frustrating if the training suddenly cancels itself, or you accidentally cancel it, and if you’re doing an 8 hour train, then this can drive you nuts.
Thankfully, I found the temporary files, that it generates, and you can still use in your code, although it generates about 10 different models per algorithm, so you’ll need to experiment to find the one that works best (typically it’ll be the 9th or 10th)
So, where are the files?
<user>\AppData\Local\Temp\Microsoft.ML.AutoML
Phew!
And how to load them in?, here’s some sample code;
private static ModelOutput Predict(ModelInput input)
{if (predictionEngine == null)
{
// Create new MLContext
MLContext mlContext = new MLContext();ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
predictionEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
}// Use model to make prediction on input data
var result = predictionEngine.Predict(input);
return result;
}
Where you statically define the following variables;
private static PredictionEngine<ModelInput, ModelOutput> predictionEngine = null;
private static string modelPath = @””;
modelpath being the ZIP file of the model.
Super-simple way to deal with non-www #CNAME in #DNS

If you ever needed to point your domain at a external hosting provider, Blogger.com is a good example of this, and you suddenly find that the non-www version of your domain doesn’t work?
Back-up for a second, What is non-www?
non-www is your domain name without the “www” at the start. So “http://MyWebsite.com” is the non-www and “http://www.MyWebsite.com” is the with-www
If you use Blogger.com to host your blog, then you are told to use a CNAME DNS record to point your “www” domain to ghs.google.com , but you can’t set a CNAME on the “@” record (non-www”), and if you try guessing the IP, then you’ll end up breaking it.
TL DR; Want the solution already?
Just add an A record for “@” and point it to IP address 176.105.255.48 , this server will automatically redirect anything that lands there to the “www” version of the same domain.
If you want to take this further, and would like to explore more about hosting websites on your own domain, then take a look at domaindeflect.com
And for people who are techie, or paranoid, the tiny script used to do this is open source on github, here https://github.com/infiniteloopltd/nonwww
Using #MachineLearning to predict the age of a #VIN
TLDR; Source code for this is available on github here; https://github.com/infiniteloopltd/VinAge
This is a first working example of something useful that I’ve created with Microsoft’s ML.NET framework.
I’ve used the “Issue Classification” scenario, training the model on 10,000 known VIN’s and known ages, and then tested it against a VIN that wasn’t in the training data, and it successfully got the age.
It didn’t work so well on a US VIN, but I guess there needs to be more variation in the training data. Also, it took about 1 second per 100 rows to train, so I can imagine some overnight training sessions!
Antipatterns:
I had also tried this on a “Price prediction” scenario, but it gave quite innacurate results, even though the training only took 30 mins over 6 million rows.
Norwegian #VIN number #API (open source)

This website https://www.understellsnummer.com/(or in english here; https://www.understellsnummer.com/en.html) allows you to enter the VIN number of a norwegian vehicle, and search for details about it.
It’s not a VIN decoder, it looks up the exact vehicle in a database of over 10 million vehicles registered in Norway, which is what is used behind the norwegian version of our vehicle registration number lookup here; http://www.registreringsnummerapi.co.no/
The data comes from official governement sources in Norway, and is updated regularly.
Newly launched #Udemy course for Microsoft #SQL server.
This is my first Udemy course that I’ve published. It teaches you pretty much all I know about SQL server in 6 hours of videos, right from absolute beginner, to expert.
If you’re interested in finding out more about the course, please click the link below;
https://www.udemy.com/course/sql_server/?referralCode=8FFD23693EFA04445F99
If you ask nicely, I have a limted number of coupons available, if you contact me personally.
Here are the course contents;
Microsoft SQL server is the most popular database platform for Windows, and with this course, we will take you from absolute beginner to expert in just under six hours of hands-on experience.
We begin by teaching you how to install your own free copy of SQL server on your own machine, or if you prefer, to host SQL server on the cloud with Amazon Web Services or Microsoft Azure.
Following on, we teach you how to create tables, insert rows, update and delete them, and read them back. You’ll quickly learn how to write queries to filter and sort data, to group and summarize data, to run statistical queries on the data.
Once you’ve mastered the basics, we show you how to create database triggers, to manage transactions, and how to wrap up complex code as simpler code using stored procedures and user defined functions.
But, it’s not all about database development, we also teach you about database adminstration, how to backup and restore your database, and more importantly, transfer your backups to the cloud, like AWS S3 or Azure Blob storage.
We also teach you about database tuning, how to make your queries run faster, and make your system more performant, and about database security, how to protect your data, and comply with data protection laws like GDPR and CCPA.
And to wrap up, we teach you how to connect to SQL server from four different programming languages, Microsoft C# .NET Core, Node JS, Python and Java.
What you’ll learn
- Students will learn how to use the Microsoft SQL server database platform
Are there any course requirements or prerequisites?
- Students will require a Windows PC that meets the minimum specifications for installing Microsoft SQL server
Who this course is for:
- Students interested in learning about the Microsoft SQL server database platform