01 安装 Visual studio 2017.
不具备安装这个的话,也可安装,Microsoft Visual Studio Express (or equivalent)
02 创建 C# 的 控制台程序
03 添加 Accord 库
03 让机器学习『异或』的逻辑
不需要在代码里写出来异或的程序逻辑,告诉机器 异或的输入和输出(其实就是一个监督学习,训练的过程),机器就自己学习会了异或逻辑。
上代码:
1 using System; 2 using Accord.Controls; 3 using Accord.MachineLearning.VectorMachines; 4 using Accord.MachineLearning.VectorMachines.Learning; 5 using Accord.Math; 6 using Accord.Statistics.Kernels; 7 using Accord.Math.Optimization.Losses; 8 using Accord.Statistics; 9 10 namespace SampleApplication1 11 { 12 class Program 13 { 14 [MTAThread] 15 static void Main(string[] args) 16 { 17 double[][] inputs = 18 { 19 /* 1.*/ new double[] { 0, 0 }, 20 /* 2.*/ new double[] { 1, 0 }, 21 /* 3.*/ new double[] { 0, 1 }, 22 /* 4.*/ new double[] { 1, 1 }, 23 }; 24 25 int[] outputs = 26 { 27 /* 1. 0 xor 0 = 0: */ 0, 28 /* 2. 1 xor 0 = 1: */ 1, 29 /* 3. 0 xor 1 = 1: */ 1, 30 /* 4. 1 xor 1 = 0: */ 0, 31 }; 32 33 // Create the learning algorithm with the chosen kernel 34 var smo = new SequentialMinimalOptimization<Gaussian>() 35 { 36 Complexity = 100 // Create a hard-margin SVM 37 }; 38 39 // Use the algorithm to learn the svm 40 var svm = smo.Learn(inputs, outputs); 41 42 // Compute the machine‘s answers for the given inputs 43 bool[] prediction = svm.Decide(inputs); 44 45 // Compute the classification error between the expected 46 // values and the values actually predicted by the machine: 47 double error = new AccuracyLoss(outputs).Loss(prediction); 48 49 Console.WriteLine("Error: " + error); 50 51 // Show results on screen 52 ScatterplotBox.Show("Training data", inputs, outputs); 53 ScatterplotBox.Show("SVM results", inputs, prediction.ToZeroOne()); 54 55 Console.ReadKey(); 56 } 57 } 58 }
04 运行搞定
本文的源代码:
链接:https://pan.baidu.com/s/1gfrQyPX
密码: 关注微信输入关键字: 异或