如何通过构建的神经网络工具箱计算数据?

时间:2014-10-15 03:51:35

标签: matlab neural-network

我有一系列x和y数据。例如:

x=[1 2 4 5 7 8 9 18 29]
y=[4 7 11 18 35 42 67 100 110]

我使用了Matlab的神经网络工具箱并制作了一个神经网络模型。(我已将我的代码放在问题的末尾) 但我想计算低于x的相应值。换句话说,如果:

x=[60 80 98 120]

然后,我想在Matlab中计算出这些点的相应y?(我知道我可以通过简单的回归来计算这个。但我坚持用神经网络做这个)

任何人都可以帮助我吗?

x=[1 2 4 5 7 8 9 18 29]
y=[4 7 11 18 35 42 67 100 110]

%// Solve an Input-Output Fitting problem with a Neural Network
%// Script generated by NFTOOL
%// Created Wed Oct 15 00:18:47 PDT 2014
%//
%// This script assumes these variables are defined:
%//
%//   x - input data.
%//   y - target data.

inputs = x;
targets = y;

%// Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize);

%// Choose Input and Output Pre/Post-Processing Functions
%// For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};


%// Setup Division of Data for Training, Validation, Testing
%// For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand';  %// Divide data randomly
net.divideMode = 'sample';  %// Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

%// For help on training function 'trainlm' type: help trainlm
%// For a list of all training functions type: help nntrain
net.trainFcn = 'trainlm';  %// Levenberg-Marquardt

%// Choose a Performance Function
    %// For a list of all performance functions type: help nnperformance
net.performFcn = 'mse';  %// Mean squared error

%// Choose Plot Functions
%// For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
  'plotregression', 'plotfit'};


%// Train the Network
[net,tr] = train(net,inputs,targets);

%// Test the Network
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs)

%// Recalculate Training, Validation and Test Performance
trainTargets = targets .* tr.trainMask{1};
valTargets = targets  .* tr.valMask{1};
testTargets = targets  .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,outputs)
valPerformance = perform(net,valTargets,outputs)
testPerformance = perform(net,testTargets,outputs)

%// View the Network
view(net)

%// Plots
%// Uncomment these lines to enable various plots.
%//figure, plotperform(tr)
%//figure, plottrainstate(tr)
%//figure, plotfit(net,inputs,targets)
%//figure, plotregression(targets,outputs)
%//figure, ploterrhist(errors)

3 个答案:

答案 0 :(得分:2)

matlab中有一个特定的函数来模拟经过训练的NN:sim

它很简单:

    sim(net,x);

ans =

  102.6437  102.6437  102.6437  102.6437

它会在给定输入的情况下模拟网络。

答案 1 :(得分:1)

@Ander Biguri的答案是正确的。我只想坚持你的方法有两个问题(这可能是一个评论和blabla,但我不能包括一张图片)

如果你看到你的预测点(红色十字架),它们看起来很好。

enter image description here

但是,如果您绘制所有x轴的预测点,您可以看到NN实际从您的数据中学到了什么:

enter image description here

x_ = 0:0.1:120;
y_ = sim(net,x_);
figure;
hold on;
plot(x_,y_,'r.');
plot(x,y,'bo');
axis([0 100 0 300]);

您可以发现两个问题:

  1. 由于缺乏数据,NN过度拟合。此外,NN的非线性超平面变得疯狂。
  2. NN不能推断。所有预测值都大于x=29无意义。

答案 2 :(得分:0)

请注意,测试范围 x = [60 80 98 120] 远远超出训练范围 x = [1 2 4 5 7 8 9 18 29] 。所以我们在这里要做的是:使用ANN& amp;解决外推问题。不使用ANN作为分类的目的。

简单来说,用于推断使用曲线拟合,如回归而不是使用ann与自定义神经元。如果ann是使用整​​个数据范围来训练 x = [1 2 4 5 7 8 9 18 29 60 80 98 120] 网络。