0

我想使用具有反向传播算法的多层神经网络来解决 3 个类的分类问题。我正在使用matlab 2012a。我在使用 newff 功能时遇到了麻烦。我想建立一个具有一个隐藏层的网络,输出层将有 3 个神经元,每个类一个。请举个例子告诉我。

这是我的代码

clc

%parameters
nodesInHL=7;
nodesInOutput=3;
iteration=1000;
HLtranfer='tansig';
outputTranser='tansig';
trainFunc='traingd';
learnRate=0.05;
performanceFunc='mse';


%rand('seed',0);
%randn('seed',0);
rng('shuffle');

net=newff(trainX,trainY,[nodesInHL],{HLtranfer,outputTranser},trainFunc,'learngd',performanceFunc);
net=init(net);

%setting parameters
net.trainParam.epochs=iteration;
net.trainParam.lr=learnRate;

%training
[net,tr]=train(net,trainX,trainY);

谢谢。

4

1 回答 1

1

newff功能已过时。推荐的功能是feedforwardnet,或者在您的情况下(分类),使用patternnet

您还可以使用 的 GUI nprtool,它提供了一个类似向导的工具,可指导您逐步构建网络。它甚至允许在实验结束时生成代码。

这是一个例子:

%# load sample dataset
%#   simpleclassInputs: 2x1000 matrix (1000 points of 2-dimensions)
%#   simpleclassTargets: 4x1000 matrix (4 possible classes)
load simpleclass_dataset

%# create ANN of one hidden layer with 7 nodes
net = patternnet(7);

%# set params
net.trainFcn = 'traingd';            %# training function
net.trainParam.epochs = 1000;        %# max number of iterations
net.trainParam.lr = 0.05;            %# learning rate
net.performFcn = 'mse';              %# mean-squared error function
net.divideFcn = 'dividerand';        %# how to divide data
net.divideParam.trainRatio = 70/100; %# training set
net.divideParam.valRatio = 15/100;   %# validation set
net.divideParam.testRatio = 15/100;  %# testing set

%# training
net = init(net);
[net,tr] = train(net, simpleclassInputs, simpleclassTargets);

%# testing
y_hat = net(simpleclassInputs);
perf = perform(net, simpleclassTargets, y_hat);
err = gsubtract(simpleclassTargets, y_hat);

view(net)

注意NN会自动设置输出层的节点数(根据目标类矩阵大小)

截屏

于 2012-07-30T18:40:14.337 回答