程式實作:感知器 (採用 C# 實作)
類神經網路
前言
感知器
LMS 學習法
反傳遞網路
反傳遞學習法
自組織網路
自組織學習法
相關書籍
人工智慧
自然語言
機器翻譯
邏輯推論
計算理論
相關資源
相關網站
相關書籍
參考文獻
統計資訊
最新修改
網頁標記
訊息
相關網站
參考文獻
最新修改
簡體版
English
[フレーム]
專案程式下載:Perceptron.zip
簡介
Rosenblatt 於 1958 年提出第一個神經網路模型,稱為感知器,這個模型是基於 1943 年 McCulloch 與 Pitts 所提出的神經元模型,該模型的數學公式如下。
(1)\begin{align} Y = sign \biggl[ \sum_{i=1}^n x_i w_i - \theta \biggr] \end{align}
感知器的訓練方式
1. 初始化
設定權重 $w_1, w_2, ... w_n$ 和臨界值 $\theta $$ 的初值之範圍為 [-0.5, 0.5]。
2. 激勵
用輸入 $x_1(p), x_2(p), ...., x_n(p)$ , $w_1, w_2, ... w_n$ 與 $\theta $$ 計算感知器的輸出值 Y(p)。
\begin{align} Y(p) = step \biggl[ \sum_{i=1}^n x_i(p) w_i(p) - \theta \biggr] \end{align}
3. 權重訓練
(3)\begin{align} w_i(p+1) = w_i(p) + \Delta w_i(p) \end{align}
(4)
\begin{align} \Delta w_i(p) = \alpha * x_i(p) * e(p) \end{align}
(5)
\begin{equation} e(p) = Y_d(p)-Y(p) \end{equation}
4. 一代一代增加 p, 重複 2-3 步驟。
感知器實作
在本節中,我們將使用 C# 程式實作出感知器,其程式碼如下。
using System;
class Perceptron
{
double[] x, w;
double theta = 0.5, alpha = 0.01;
public static double[,] andTable = { { 0, 0, 0 }, { 0, 1, 0 }, { 1, 0, 0 }, { 1, 1, 1 } };
public static double[,] orTable = { { 0, 0, 0 }, { 0, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 } };
public static double[,] xorTable = { { 0, 0, 0 }, { 0, 1, 1 }, { 1, 0, 1 }, { 1, 1, 0 } };
static void Main(string[] args)
{
learn("and", andTable);
learn("or", orTable);
// learn("xor", xorTable);
}
public Perceptron()
{
x = new double[2];
w = new double[2];
}
bool training(double[,] truthTable)
{
for (int loop=0; loop<100; loop++)
{
double eSum = 0.0;
Console.WriteLine("============== loop : {0} ================", loop);
for (int i = 0; i < truthTable.GetLength(0); i++)
{
double[] x = { truthTable[i, 0], truthTable[i, 1] };
double yd = truthTable[i, 2];
double y = step(x, w, theta);
double e = yd - y;
eSum += e;
double[] dw = new double[2];
dw[0] = alpha * x[0] * e; w[0] += dw[0];
dw[1] = alpha * x[1] * e; w[1] += dw[1];
Console.WriteLine("x=({0:F0},{1:F0}) w=({2:F4},{3:F4}) y={4:F4} yd={5:F0} e={6:F4}",
x[0], x[1], w[0], w[1], y, yd, e);
}
if (eSum == 0.0) return true;
}
return false;
}
static double step(double[] x, double[] w, double theta) {
double result = w[0] * x[0] + w[1] * x[1] - theta;
if (result >= 0)
return 1;
else
return 0;
}
public static double[] learn(string tableName, double[,] truthTable)
{
Console.WriteLine("================== 學習 {0} 函數 ====================", tableName);
Perceptron p = new Perceptron();
if (p.training(truthTable))
Console.WriteLine("學習成功 !");
else
Console.WriteLine("學習失敗 !");
return p.w;
}
}
結語
感知器是最簡單的類神經網路,想要學習類神經網路的最簡單作法,正是從感知器下手。在本文中,我們以 C# 實作了感知器的程式,以便使用者理解其運作原理。
參考文獻
[フレーム]
本網頁的作者、授權與引用方式
- 作者
- 陳鍾誠,於金門大學資訊工程系,電子郵件:wt.ude.uqn|ccc#wt.ude.uqn|ccc,網站:http://ccckmit.wikidot.com。
- 授權
- 本文採用創作共用 (Creative Common) 3.0 版的 姓名標示─非商業性─相同方式分享 授權條款,歡迎轉載或修改使用,但若做為商業使用時必須取得授權,引用本文時請參考下列格式。
- 中文版 (APA格式)
- 陳鍾誠 (09 Nov 2009 07:08),(網頁標題) 程式實作:感知器 (採用 C# 實作),(網站標題) 陳鍾誠的網站,取自 http://ccckmit.wikidot.com/nn:perceptroncsharp ,網頁修改第 17 版。
- 英文版 (APA格式)
- Chung-Chen Chen (09 Nov 2009 07:08), Retrieved from http://ccckmit.wikidot.com/nn:perceptroncsharp , Page Revision 17.
page revision: 17, last edited: 11 Sep 2010 00:11