01 安装 Visual studio 2017.
不具备安装这个的话,也可安装, Microsoft Visual Studio Express (or equivalent )02 创建 C# 的 控制台程序
03 添加 Accord 库
03 让机器学习『异或』的逻辑
不需要在代码里写出来异或的程序逻辑,告诉机器 异或的输入和输出(其实就是一个监督学习,训练的过程),机器就自己学习会了异或逻辑。
上代码:
04 运行搞定
- using System;
- using Accord.Controls;
- using Accord.MachineLearning.VectorMachines;
- using Accord.MachineLearning.VectorMachines.Learning;
- using Accord.Math;
- using Accord.Statistics.Kernels;
- using Accord.Math.Optimization.Losses;
- using Accord.Statistics;
- namespace SampleApplication1
- {
- class Program
- {
- [MTAThread]
- static void Main(string[] args)
- {
- double[][] inputs =
- {
- /* 1.*/ new double[] { 0, 0 },
- /* 2.*/ new double[] { 1, 0 },
- /* 3.*/ new double[] { 0, 1 },
- /* 4.*/ new double[] { 1, 1 },
- };
- int[] outputs =
- {
- /* 1. 0 xor 0 = 0: */ 0,
- /* 2. 1 xor 0 = 1: */ 1,
- /* 3. 0 xor 1 = 1: */ 1,
- /* 4. 1 xor 1 = 0: */ 0,
- };
- // Create the learning algorithm with the chosen kernel
- var smo = new SequentialMinimalOptimization<Gaussian>()
- {
- Complexity = 100 // Create a hard-margin SVM
- };
- // Use the algorithm to learn the svm
- var svm = smo.Learn(inputs, outputs);
- // Compute the machine's answers for the given inputs
- bool[] prediction = svm.Decide(inputs);
- // Compute the classification error between the expected
- // values and the values actually predicted by the machine:
- double error = new AccuracyLoss(outputs).Loss(prediction);
- Console.WriteLine("Error: " + error);
- // Show results on screen
- ScatterplotBox.Show("Training data", inputs, outputs);
- ScatterplotBox.Show("SVM results", inputs, prediction.ToZeroOne());
- Console.ReadKey();
- }
- }
- }
本文的源代码:
https://pan.baidu.com/s/1gfrQyPX
密码: 关注微信, 输入关键字: 异或
来源: https://www.cnblogs.com/zhixingheyi/p/8111439.html