徑向基底函數網路訓練

James McCaffrey

程式的總體結構

``````using System;
{
{
static void Main(string[] args)
{
Console.WriteLine("Begin radial basis function (RBF) network training demo");
double[][] allData = new double[30][];
allData[0] = new double[] { -0.784, 1.255, -1.332, -1.306, 0, 0, 1 };
allData[1] = new double[] { -0.995, -0.109, -1.332, -1.306, 0, 0, 1 };
// Etc.
allData[28] = new double[] { 0.904, -1.473, 1.047, 0.756, 1, 0, 0 };
allData[29] = new double[] { 1.431, 1.528, 1.209, 1.659, 1, 0, 0 };
Console.WriteLine("First four and last line of normalized, encoded input data:");
Helpers.ShowMatrix(allData, 4, 3, true, true);
double[][] trainData = null;
double[][] testData = null;
int seed = 8; // Gives a good demo
GetTrainTest(allData, seed, out trainData, out testData);
Helpers.ShowMatrix(trainData, trainData.Length, 3, true, false);
Helpers.ShowMatrix(testData, testData.Length, 3, true, false);
int numInput = 4;
int numHidden = 5;
int numOutput = 3;
Console.WriteLine("Beginning RBF training");
int maxIterations = 100; // Max for PSO
double[] bestWeights = rn.Train(trainData, maxIterations);
Console.WriteLine("Evaluating RBF accuracy on the test data");
rn.SetWeights(bestWeights);
double acc = rn.Accuracy(testData);
Console.WriteLine("Classification accuracy = " + acc.ToString("F4"));
Console.WriteLine("End RBF network training demo");
}
static void GetTrainTest(double[][] allData, int seed,
out double[][] trainData, out double[][] testData) { .
.
}
}
{
private static Random rnd = null;
private int numInput;
private int numHidden;
private int numOutput;
private double[] inputs;
private double[][] centroids;
private double[] widths;
private double[][] hoWeights;
private double[] oBiases;
private double[] outputs;
public RadialNetwork(int numInput, int numHidden, int numOutput) { .
.
}
private static double[][] MakeMatrix(int rows, int cols) { .
.
}
public void SetWeights(double[] weights) { .
.
}
public double[] GetWeights() { .
.
}
private double MeanSquaredError(double[][] trainData,
double[] weights) { .
.
}
public double Accuracy(double[][] testData) { .
.
}
private static int MaxIndex(double[] vector) { .
.
}
public double[] ComputeOutputs(double[] xValues) { .
.
}
private static double[] Softmax(double[] rawOutputs) { .
.
}
public double[] Train(double[][] trainData, int maxIterations) { .
.
}
private void DoCentroids(double[][] trainData) { .
.
}
private static double AvgAbsDist(double[] v1, double[] v2,
int numTerms) { .
.
}
private int[] DistinctIndices(int n, int range) { .
.
}
private void DoWidths(double[][] centroids) { .
.
}
private double[] DoWeights(double[][] trainData, int maxIterations) { .
.
}
private static double EuclideanDist(double[] v1, double[] v2,
int numTerms) { .
.
}
private static void Shuffle(int[] sequence) { .
.
}
}
public class Particle
{
// Implementation here
}
public class Helpers
{
// Implementation here
}
}
``````

``````double[][] allData = new double[30][];
allData[0] = new double[] { -0.784, 1.255, -1.332, -1.306, 0, 0, 1 };
allData[1] = new double[] { -0.995, -0.109, -1.332, -1.306, 0, 0, 1 };
// Etc.
allData[28] = new double[] { 0.904, -1.473, 1.047, 0.756, 1, 0, 0 };
allData[29] = new double[] { 1.431, 1.528, 1.209, 1.659, 1, 0, 0 };
``````

``````double[][] trainData = null;
double[][] testData = null;
int seed = 8;
GetTrainTest(allData, seed, out trainData, out testData);
``````

Rbf 神經網路進行具現化：

``````int numInput = 4;
int numHidden = 5;
int numOutput = 3;
numHidden, numOutput);
``````

``````int maxIterations = 100;
double[] bestWeights = rn.Train(trainData, maxIterations);
``````

``````rn.SetWeights(bestWeights);
double acc = rn.Accuracy(testData);
Console.WriteLine("Classification accuracy = " +
acc.ToString("F4"));
``````

BestWeights 陣列保存的 RBF 權重和偏置值所確定的培訓方法。方法 SetWeights 載入這些權重和偏置值。你不需要有郵編和顯式載入，因為由火車方法設置這些值的寬度。

確定 RBF 網路郵編

``````public double[] Train(double[][] trainData, int maxIterations)
{
DoCentroids(trainData);
DoWidths(this.centroids);
double[] bestWeights = DoWeights(trainData, maxIterations);
return bestWeights;
}
``````

``````initialize maxDistance
intialize bestIndices
loop
select numHidden random indices into train data
compute an estimated distance between selected data items
if estimated distance > maxDistance then
set maxDistance = curr distance
set bestIndices = curr indices
end if
end loop
fetch the x-values in train data at bestIndices
store x-values into RBF centroids
``````

``````0: ( 1.537, -0.382,  1.317,  0.756)
1: (-0.468,  2.346, -1.170, -1.048)
2: ( 1.115,  0.164,  0.560,  0.370)
3: ( 1.220,  0.436,  0.452,  0.241)
``````

``````d = abs(1.537 - (-0.468)) + .
.
.
+ abs(0.756 - (-1.048)) / 4
= 2.256
``````

``````private int[] DistinctIndices(int n, int range)
{
// Reservoir sampling.
assumes rnd exists
int[] result = new int[n];
for (int i = 0; i < n; ++i)
result[i] = i;
for (int t = n; t < range; ++t) {
int m = rnd.Next(0, t + 1);
if (m < n) result[m] = t;
}
return result;
}
``````

確定 RBF 網路寬度

RBF 網路輸入過程輸出機制為隱藏的每個節點需要一個寬度值。 有很多可能性確定的寬度值。 最簡單的方法和使用的演示程式，是計算一個共同的寬度，所有隱藏的處理節點可以使用。 在這一領域的研究往往是朦朧和結論有時是相互矛盾。 該演示程式作為郵編對所有可能的組合之間的歐幾裡德距離平均計算常見的寬度。 在偽代碼：

``````sumOfDists = 0.0
for each pair of centroids
accumulate Euclidean distance between curr pair
end loop
return accumulated sumOfDists / number of pairs
``````

確定 RBF 網路權重和偏見

``````set number of particles
set maxIterations
initialize all particles to random positions
loop maxIterations times
for each particle
update curr particle's velocity
use new velocity to compute new position
compute error for new position
check if new particle best position
check if new best position for all particles
end for
end loop
return best position found by any particle
``````

PSO 是一個迷人的話題在它自己的權利。你可以瞭解更多關於它通過閱讀我 2011 年 8 月的文章，"粒子群優化演算法"(msdn.microsoft.com/magazine/hh335067)。PSO 要求的幾個自由參數規格­eters，包括控制粒子的當前位置的相對影響的重量常量的最佳的歷史地位和最佳全球的歷史地位。PSO 還需要指定粒子的數量、 反覆運算和 （可選），一種用於早期演算法退出錯誤閾值的最大數目。您可以嘗試使用演示代碼這些因素。

總結

Dr。James McCaffre 為微軟在華盛頓州雷德蒙德的研究工作 他曾在幾個 Microsoft 產品，包括互聯網資源管理器和 Bing。他可以在達成 jammc@microsoft.com