BP神经网络预测数据

        神经网络的其中一个功能为数据预测。指的是利用已知的数据模式和规律来对未知的数据进行推测和预测。神经网络通过学习大量的输入数据和对应的输出结果,能够发现数据之间的复杂关系,并且在接受到新的输入数据时,可以根据这些学习到的规律来做出相应的预测。

        MATLAB这个仿真工具可以很好地进行预测。其使用步骤如下

1.选择APP中的神经网络拟合

2.这里导入数据,这里我定义的了一个简单的测试数据,是模拟一个与x²相似的关系模型,x的值为1至11的数,而输出y为1、4.1、9.7、16.5、25.4、36.7、49.4、65、83、100、121。把这些数添加到工作区的变量中,或者也可以放在Excel表中以文件的方式导入。

这里的特征就是有多少个输入,观测值的就是测试数据的数目

3.确认后点击训练,就会生成一下数据表,都是些模型预测性能指标的说明,这里就不进行赘述

4.点击导出模型,选择导出用于MATLAB Coder的网咯函数

5.就会生成下列代码


function [y1] = myNeuralNetworkFunction(x1)
%MYNEURALNETWORKFUNCTION neural network simulation function.
%
% Auto-generated by MATLAB, 29-May-2024 12:47:32.
%
% [y1] = myNeuralNetworkFunction(x1) takes these arguments:
%   x = Qx1 matrix, input #1
% and returns:
%   y = Qx1 matrix, output #1
% where Q is the number of samples.

%#ok<*RPMT0>

% ===== NEURAL NETWORK CONSTANTS =====

% Input 1
x1_step1.xoffset = 1;
x1_step1.gain = 0.2;
x1_step1.ymin = -1;

% Layer 1
b1 = [-13.892357504987034034;-10.884130698028425144;7.971929314174220238;4.6666786207209467463;1.6803558095784869586;1.6029029445475988513;4.6938222383510685987;7.7668131141000209539;-10.889525577509822085;14.08532184265181364];
IW1_1 = [14.107644977625529492;14.002056525283192556;-13.883470058004155234;-13.999996366149302318;-13.963776146540370249;13.994387935156195724;13.988126338219638356;14.004391658682372679;-13.9993511941901847;13.914678133816293482];

% Layer 2
b2 = 0.22445677763093485613;
LW2_1 = [0.44754396359693610075 -0.18256713270233904112 -0.69018528128988199111 0.19912329312706408824 -0.12260209775282723821 0.10073931460703094976 0.077867951266448576231 0.047015002981135915749 -0.10653781102282021409 -0.08330633581371255092];

% Output 1
y1_step1.ymin = -1;
y1_step1.gain = 0.0166666666666667;
y1_step1.xoffset = 1;

% ===== SIMULATION ========

% Dimensions
Q = size(x1,1); % samples

% Input 1
x1 = x1';
xp1 = mapminmax_apply(x1,x1_step1);

% Layer 1
a1 = tansig_apply(repmat(b1,1,Q) + IW1_1*xp1);

% Layer 2
a2 = repmat(b2,1,Q) + LW2_1*a1;

% Output 1
y1 = mapminmax_reverse(a2,y1_step1);
y1 = y1';
end

% ===== MODULE FUNCTIONS ========

% Map Minimum and Maximum Input Processing Function
function y = mapminmax_apply(x,settings)
y = bsxfun(@minus,x,settings.xoffset);
y = bsxfun(@times,y,settings.gain);
y = bsxfun(@plus,y,settings.ymin);
end

% Sigmoid Symmetric Transfer Function
function a = tansig_apply(n,~)
a = 2 ./ (1 + exp(-2*n)) - 1;
end

% Map Minimum and Maximum Output Reverse-Processing Function
function x = mapminmax_reverse(y,settings)
x = bsxfun(@minus,y,settings.ymin);
x = bsxfun(@rdivide,x,settings.gain);
x = bsxfun(@plus,x,settings.xoffset);
end

6.保存代码到当前文件夹里,然后在命令行窗口里调用myNeuralNetworkFunction(x1)这个函数,x1改为你要预测的数值。就会输出预测的结果,当然我这里的数据不是很准,因为训练的数据很少,训练的数据越多会精准些。

7.选择生成代码

% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 29-May-2024 12:59:07
%
% This script assumes these variables are defined:
%
%   X - input data.
%   Y - target data.
%这里就是读取工作区的测试数据
x = X';
t = Y';

%这里的也可以读取Excel表格的数据,格式为:
x=xlread('文件名.xlsx');
t=xlread('文件名.xlsx');

% Choose a Training Function 选择训练函数
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.速度最快
% 'trainbr' takes longer but may be better for challenging problems.适用于噪声和小数据集
% 'trainscg' uses less memory. Suitable in low memory situations.适用于大型问题
trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.

% Create a Fitting Network,创建拟合的网络
hiddenLayerSize = [10];每一层的节点数,这里表示只有一层10结点的
net = fitnet(hiddenLayerSize,trainFcn);

% Choose Input and Output Pre/Post-Processing Functions输入和输出的数据预处理,例如常用的归一化
% For a list of all processing functions type: help nnprocess
net.input.processFcns = {'removeconstantrows','mapminmax'};
net.output.processFcns = {'removeconstantrows','mapminmax'};

% Setup Division of Data for Training, Validation, Testing切分数据集,划分训练集、验证集、测试集的数目。
% For a list of all data division functions type: help nndivision
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;

% Choose a Performance Function选择性能函数
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse';  % 均方误差

% 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,x,t);

% Test the Network结果输出
y = net(x);%预测结果
e = gsubtract(t,y);%真实值与预测值差值
performance = perform(net,t,y)%总体均方差

% Recalculate Training, Validation and Test Performance性能表现
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)

% View the Network
view(net)

% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)

% Deployment这里的就是导出模型的选择
% Change the (false) values to (true) to enable the following code blocks.
% See the help for each generation function for more information.
if (false)
    % Generate MATLAB function for neural network for application
    % deployment in MATLAB scripts or with MATLAB Compiler and Builder
    % tools, or simply to examine the calculations your trained neural
    % network performs.
    genFunction(net,'myNeuralNetworkFunction');
    y = myNeuralNetworkFunction(x);
end
if (false)
    % Generate a matrix-only MATLAB function for neural network code
    % generation with MATLAB Coder tools.
    genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
    y = myNeuralNetworkFunction(x);
end
if (false)
    % Generate a Simulink diagram for simulation or deployment with.
    % Simulink Coder tools.
    gensim(net);
end

代码的一些测试方法等可以进行修改,发挥更好的模型预测,

这个net.trainParam.参数。这个可以更好的对性能进一步优化,例如运行时间,迭代的次数等

if (false)
    % Generate a matrix-only MATLAB function for neural network code
    % generation with MATLAB Coder tools.
    genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
    y = myNeuralNetworkFunction(x);
end

这个代码改成true后生成myNeuralNetworkFunction(x1)这个函数用于后续输入新的数据进行预测

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值