ChatGPT hayatımıza girdiğinden bu yana yazılım öğrenmek ve öğretmek daha kolay oldu. Mesela .NET’te BotSharp gibi hiçbir üçüncü parti kütüphane kullanmadan ChatBot nasıl geliştirebilirim diye merak ettiniz mi? Microsoft’un geliştirdiği ML.NET ile bu mümkün.

ML.NET Microsoft’un makine öğrenmesi kütüphanesi çıkmadan önce Makine Öğrenmesi’nde C#’ta AForge.NET kullanılıyordu. Fakat o çok eskide kaldı ve onun yerini daha modern olan ML.NET aldı. ML.NET, Python kütüphaneleri ile uyumlu olsa da Python kütüphanelerinin .NET’e aktarımı işi halen sürüyor.
Mesela Microsoft Tensorflow, NLTK gibi kütüphanelerin .NET alternatiflerini hazırlayacak diye beklemekteyiz. CognitiveToolkit projesi kapatıldığından bu yana C#’ın Deep Learning çözümlerinde halen zayıf olduğunu söyleyebilirim.
Neyse ki ML.NET var ve ML.NET ile C# dilini kullanarak verilerimiz üzerinde makine öğrenmesi algoritmalarını çalıştırabiliyoruz. Google’ın Dialogflow’u ile, Python’un ChatterBot’u ile rekabet etmek nispeten zor da olsa basit bir soru-cevap verisinden sohbet botu üretmek mümkün.
İşte en ilkel haliyle bir ML.NET ChatBotu:
using System;
using Microsoft.ML;
using Microsoft.ML.Data;
namespace Chatbot
{
class Program
{
static void Main(string[] args)
{
// Set up the ML context
var mlContext = new MLContext();
// Load the data
var data = mlContext.Data.LoadFromTextFile<MessageData>(
path: "conversations.csv",
hasHeader: true,
separatorChar: ','
);
// Split the data into training and test sets
var trainTestData = mlContext.Data.TrainTestSplit(data, testFraction: 0.2);
var trainingData = trainTestData.TrainSet;
var testData = trainTestData.TestSet;
// Set up the pipeline
var pipeline = mlContext.Transforms.Text.FeaturizeText(
"Features",
"Message"
).Append(mlContext.Regression.Trainers.FastTree(
labelColumn: "Label",
featureColumn: "Features"
));
// Train the model
var model = pipeline.Fit(trainingData);
// Evaluate the model on the test data
var testMetrics = mlContext.Regression.Evaluate(
model.Transform(testData),
labelColumn: "Label"
);
// Print the evaluation metrics
Console.WriteLine("R-Squared: " + testMetrics.RSquared);
Console.WriteLine("Mean Absolute Error: " + testMetrics.MeanAbsoluteError);
// Use the model to predict the response to a new message
var predictionEngine = mlContext.Model.CreatePredictionEngine<MessageData, MessagePrediction>(model);
while (true)
{
Console.WriteLine("Enter a message:");
var message = Console.ReadLine();
if (message == "exit")
{
break;
}
var response = predictionEngine.Predict(new MessageData
{
Message = message
});
Console.WriteLine("Response: " + response.Response);
}
}
}
// The input data for the model.
// This defines the shape of the data that the model expects.
public class MessageData
{
[LoadColumn(0)]
public string Message { get; set; }
[LoadColumn(1)]
public float Label { get; set; }
}
// The output of the model.
// This defines the shape of the data that the model produces.
public class MessagePrediction
{
[ColumnName("Prediction")]
public float Response { get; set; }
}
}
Bir başka makine öğrenmesi ise spam filtrelemesi için. Diyelim ki Twitter benzeri bir sosyal medya sitesi hazırlıyorsunuz ya da bir e-posta istemcisi ya da sunucu hazırlıyorsunuz ve makine öğrenmesi ile gelen tweetler üzerinde ya da gelen mesajlar üzerinde spam olanları tespit etmek istiyorsunuz. Veya web siteniz yorumlar bölümünde spam yorumları tespit etmek istiyorsunuz. Bunun için ML.NET ile yazabileceğiniz makine öğrenmesi kodu şöyle:
using System;
using Microsoft.ML;
using Microsoft.ML.Data;
namespace SpamFilter
{
class Program
{
static void Main(string[] args)
{
// Set up the ML context
var mlContext = new MLContext();
// Load the data
var data = mlContext.Data.LoadFromTextFile<MessageData>(
path: "messages.csv",
hasHeader: true,
separatorChar: ','
);
// Split the data into training and test sets
var trainTestData = mlContext.Data.TrainTestSplit(data, testFraction: 0.2);
var trainingData = trainTestData.TrainSet;
var testData = trainTestData.TestSet;
// Set up the pipeline
var pipeline = mlContext.Transforms.Text.FeaturizeText(
"Features",
"Message"
).Append(mlContext.BinaryClassification.Trainers.FastTree(
labelColumn: "Label",
featureColumn: "Features"
));
// Train the model
var model = pipeline.Fit(trainingData);
// Evaluate the model on the test data
var testMetrics = mlContext.BinaryClassification.Evaluate(
model.Transform(testData),
labelColumn: "Label"
);
// Print the evaluation metrics
Console.WriteLine("Accuracy: " + testMetrics.Accuracy);
Console.WriteLine("AUC: " + testMetrics.AreaUnderRocCurve);
// Use the model to predict whether a new message is spam or not
var predictionEngine = mlContext.Model.CreatePredictionEngine<MessageData, MessagePrediction>(model);
var message = new MessageData
{
Message = "You have won a prize! Claim it now!"
};
var prediction = predictionEngine.Predict(message);
Console.WriteLine("Prediction: " + prediction.Prediction);
}
}
// The input data for the model.
// This defines the shape of the data that the model expects.
public class MessageData
{
[LoadColumn(0)]
public bool Label { get; set; }
[LoadColumn(1)]
public string Message { get; set; }
}
// The output of the model.
// This defines the shape of the data that the model produces.
public class MessagePrediction
{
[ColumnName("Prediction")]
public bool Prediction { get; set; }
}
}
ML.NET kütüphanesi C# eğitimlerinde mutlaka gösterilmelidir. Yapay Zeka, Makine Öğrenmesi, Derin Öğrenme için illa ki Python dilini kullanmak gerekmez, elbette ki yapay zeka projeleri için Python’da çok daha fazla paket bulunmaktadır ama temel makine öğrenmesi algoritmaları için .NET’in de sağladığı imkanları öğrenip öğretmekte fayda var.
Mutlu kodlamalar 🙂