惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - waemz

数学之美 系列十八 - 矩阵运算和文本处理中的分类问题 文本分类(二)特征权重量化器(文档转向量表示) 文本分类(一)封装分词器 转嘉士伯的Java小屋写的关于编码的文章(三)网页文件的编码 转嘉士伯的Java小屋写的关于编码的文章(二)GB2312,GBK与中文网页 转嘉士伯的Java小屋写的关于编码的文章(一)编码字符集与字符集编码的区别 SVM入门(三)线性分类器Part 2 SVM入门(一)SVM的八股简介 SVM入门(二)线性分类器Part 1 人工神经网络框架AForge学习(三):后向传播学习算法 人工神经网络框架AForge学习(二):Sigmoid激活函数 基于朴素贝叶斯分类器的文本分类算法C#版(二) 基于朴素贝叶斯分类器的文本分类算法C#版(一) AderTemplate模版引擎使用分析(二) AderTemplate模版引擎使用分析 ASP.NET纯代码实现伪静态地址(URL重写) 深入浅出工厂模式 在Web应用程序中执行计划任务(多线程) 您未必知道的Js技巧
人工神经网络框架AForge学习(一)
waemz · 2009-03-02 · via 博客园 - waemz

本示例演示了单层人工神经网络的学习过程.

示例中采用了16条样本数据,3个分类,在经过7-20次的迭代后,误差值与期望最小误差(示例中为0)的差距变为最小.

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.IO;
  5
  6using AForge;
  7using AForge.Neuro;
  8using AForge.Neuro.Learning;
  9using AForge.Controls;
 10
 11namespace AspxOn.AForgeNeuroTest
 12{
 13    class Program
 14    {
 15        static void Main(string[] args)
 16        {
 17            double[,] data = null;
 18            int[] classes = null;
 19
 20            double[,] tempData = new double[2002];
 21            int[] tempClass = new int[200];
 22
 23            double learnintRate = 0.1;
 24
 25            //最大和最小的X值
 26            double minX = double.MaxValue;
 27            double maxX = double.MinValue;
 28
 29            //样本数目统计
 30            int samples = 0;
 31            //分类数目统计
 32            int classCount = 0;
 33            int[] samplesPerClass = new int[10];
 34
 35            try
 36            {
 37                //准备训练样本数据
 38                string s = "0.1,0.1,0;0.2,0.3,0;0.3,0.4,0;0.1,0.3,0;0.2,0.5,0;0.1,1,1;0.2,1.1,1;0.3,0.9,1;";
 39                s+="0.4,0.8,1;0.2,0.9,1;1,0.4,2;0.9,0.5,2;0.8,0.6,2;0.9,0.4,2;1,0.5,2;1,0.3,2";
 40                string[] ss = s.Split(';');
 41
 42                for (int i = 0; i < ss.Length; i++)
 43                {
 44                    string str = ss[i];
 45                    string[] strs = str.Split(',');
 46
 47                    //检查数组长度
 48                    if (strs.Length != 3)
 49                        throw new Exception("length error!");
 50
 51                    //生成单词
 52                    tempData[samples, 0= double.Parse(strs[0]);
 53                    tempData[samples, 1= double.Parse(strs[1]);
 54                    //生成分类
 55                    tempClass[samples] = int.Parse(strs[2]);
 56
 57                    //跳过分类如果分类值大于等于10
 58                    if (tempClass[samples] >= 10)
 59                    {
 60                        continue;
 61                    }

 62
 63                    //计算不同分类的总数
 64                    if (tempClass[samples] >= classCount)
 65                        classCount += tempClass[samples] + 1;
 66
 67                    //统计每个分类的样本数目
 68                    samplesPerClass[tempClass[samples]]++;
 69
 70                    //寻找最小值
 71                    if (tempData[samples, 0< minX)
 72                        minX = tempData[samples, 0];
 73                    //寻找最大值
 74                    if (tempData[samples, 0> maxX)
 75                        maxX = tempData[samples, 0];
 76
 77                    //样本数目累加
 78                    samples++;
 79
 80                }

 81
 82                data = new double[samples, 2];
 83                Array.Copy(tempData, 0, data, 0, samples * 2);
 84                classes = new int[samples];
 85                Array.Copy(tempClass, 0, classes, 0, samples);
 86
 87
 88            }

 89            catch
 90            {
 91                throw new Exception("failed loading data!");
 92            }

 93
 94            /*=============================开始训练=================================*/
 95
 96            //准备学习样本数据
 97            double[][] input = new double[samples][];
 98            double[][] output = new double[samples][];
 99
100            for (int i = 0; i < samples; i++)
101            {
102                input[i] = new double[2];
103                output[i] = new double[classCount];
104
105                //设置输入值
106                input[i][0= data[i, 0];
107                input[i][1= data[i, 1];
108                //设置输出值
109                output[i][classes[i]] = 1;
110
111            }

112
113            //神经网络初始化,创建激活神经网络
114            ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 2, classCount);
115            ActivationLayer layer = network[0];
116            //创建导师
117            PerceptronLearning teacher = new PerceptronLearning(network);
118            //设置学习比率
119            teacher.LearningRate = learnintRate;
120
121            //迭代次数
122            int iteration = 1;
123            //停止迭代标志
124            bool neetToStop = false;
125            //是否输出每次权重
126            bool showWeight = true;
127
128            try
129            {
130                //误差值
131                List<double> errors = new List<double>();
132
133                while (!neetToStop)
134                {
135                    输出权重
148
149                    //将本次误差添加到误差列表
150                    double error = teacher.RunEpoch(input, output);
151                    errors.Add(error);
152                    //输出本次训练误差
153                    Console.WriteLine("误差:" + error.ToString());
154                    //输出当前迭代次数
155                    Console.WriteLine("迭代次数:" + iteration);
156                    Console.WriteLine("---------------------------------------------");
157
158                    //如果误差等于0,停止训练
159                    //此处可预设一个最小值,不一定非是0.当误差小于此最小值时,结束训练。
160                    if (error == 0)
161                        break;
162
163
164                    iteration++;
165                }

166            }

167            catch
168            {
169                throw new Exception("error!");
170            }

171
172            /*=============================结束训练=================================*/
173            
174            Console.ReadKey();
175        }

176
177    }

178}

输出结果如下: