图4-13 新数据集的神经网络模型The neural network modelfor this new dataset
每个图像就是一个矩阵,我们设置输入节点数量为5 x 5 = 25。
As each image is set on a matrix, we set 25input nodes.
此外,由于我们有五个数字来进行分类,网络包含五个输出节点。
In addition, as we have five digits toclassify, the network contains five output nodes.
使用softmax函数作为输出节点的激活函数。
The softmax function is used as theactivation function of the output node.
隐藏层有50个节点,sigmoid函数用作激活函数。
The hidden layer has 50 nodes and thesigmoid function is used as the activation function.
MultiClass函数利用SGD方法实现多元分类的学习规则。
The function MultiClass implements thelearning rule of multiclass classification using the SGD method.
它采用权重和训练数据作为输入参数,并返回训练后的权重。
It takes the input arguments of the weightsand training data and returns the trained weights.
[W1,W2] = MultiClass(W1, W2, X, D)
其中W1和W2分别为输入-隐藏层和隐藏-输出层之间的权重矩阵。
where W1 and W2 are the weight matrices ofthe input-hidden and hidden-output layers, respectively.
X和D分别是训练数据的输入和正确输出。
X and D are the input and correct output ofthe training data, respectively.
下面的列表显示了实现MultiClass函数的MultiClass.m文件。
The following listing shows theMultiClass.m file, which implements the function MultiClass.
function [W1, W2] = MultiClass(W1, W2, X,D)
alpha = 0.9;
N = 5;
fork = 1:N
x = reshape(X(:, :, k), 25, 1);
d = D(k, :)';
v1= W1*x;
y1= Sigmoid(v1);
v= W2*y1;
y= Softmax(v);
e= d - y;
delta= e;
e1= W2'*delta;
delta1= y1.*(1-y1).*e1;
dW1 = alpha*delta1*x';
W1 = W1 + dW1;
dW2 = alpha*delta*y1';
W2 = W2 + dW2;
end
end
该代码与第三章“交叉熵函数”部分中的示例代码具有相同的过程,通过对训练数据应用增量规则,计算权重更新dW1和dW2,并调整神经网络的权重。
This code follows the same procedure asthat of the example code in the “Cross Entropy Function” section in Chapter 3,which applies the delta rule to the training data, calculates the weight updates,dW1 and dW2, and adjusts the neural network’s weights.
然而,这里的代码又稍有不同,因为它使用函数softmax计算输出,并调用函数改变矩阵维数以实现训练数据的导入。
However, this code slightly differs in thatit uses the function softmax for the calculation of the output and calls thefunction reshape to import the inputs from the training data.
x =reshape(X(:, :, k), 25, 1);
输入变量X包含堆叠的二维图像数据。
The input argument X contains the stackedtwo-dimensional image data.
这意味着X是一个5 x 5 x 5的三维矩阵。
This means that X is a 5 x 5 x 5three-dimensional matrix.
因此,reshape函数中的第一个变量X(:, :, k),表示第个图像数据中的5 x 5矩阵。
Therefore, the first argument of thefunction reshape, X(:, :, k) indicates the 5 x 5 matrix that contains the -th image data.
由于该神经网络只能输入向量格式,因此二维矩阵应转换为25 x 1的向量。
As this neural network is compatible withonly the vector format inputs, the two-dimensional matrix should be transformedinto a 25 x 1 vector.
函数reshape实现的就是这种转换功能。
The function reshape performs thistransformation.
——本文译自Phil Kim所著的《Matlab Deep Learning》
更多精彩文章请关注微信号: