配置并在matlab上对其进行训练后,如何使用神经网络运行预测?

如何解决配置并在matlab上对其进行训练后,如何使用神经网络运行预测?

我使用nnstart命令,得到了用于配置和训练网络的Matlab应用。导入数据并对网络进行培训后,就无法再选择实际运行时间序列预测了。我能做的最好的事情就是可以生成一个脚本。但是该脚本似乎并未包含实际进行预测的程序。这是代码。如何运行预测?另外,如何选择激活函数g(x)?

% Solve an Input-Output Time-Series Problem with a Time Delay Neural Network
% Script generated by Neural Time Series app.
% Created 03-Nov-2020 23:33:27
%
% This script assumes these variables are defined:
 %
 %   data - input time series.
 %   data_1 - target time series.

 X = tonndata(data,false,false);
  T = tonndata(data_1,false);

  % 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 Time Delay Network
     inputDelays = 1:2;
     hiddenLayerSize = 10;
     net = timedelaynet(inputDelays,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'};

     % Prepare the Data for Training and Simulation
     % The function PREPARETS prepares timeseries data for a particular network,% shifting time by the minimum amount to fill input states and layer
     % states. Using PREPARETS allows you to keep your original time series data
       % unchanged,while easily customizing it for networks with differing
     % numbers of delays,with open loop or closed loop feedback modes.
    [x,xi,ai,t] = preparets(net,X,T);

% 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 = 'time';  % 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';  % Mean Squared Error

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

  % Train the Network
   [net,tr] = train(net,x,t,ai);

  % Test the Network
  y = net(x,ai);
  e = gsubtract(t,y);
  performance = perform(net,y)

   % Recalculate Training,Validation and Test Performance
   trainTargets = gmultiply(t,tr.trainMask);
   valTargets = gmultiply(t,tr.valMask);
   testTargets = gmultiply(t,tr.testMask);
   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,plotresponse(t,ploterrcorr(e)
%figure,plotinerrcorr(x,e)

 % Step-Ahead Prediction Network
 % For some applications it helps to get the prediction a timestep early.
 % The original network returns predicted y(t+1) at the same time it is
 % given x(t+1). For some applications such as decision making,it would
 % help to have predicted y(t+1) once x(t) is available,but before the
 % actual y(t+1) occurs. The network can be made to return its output a
 % timestep early by removing one delay so that its minimal tap delay is now
  % 0 instead of 1. The new network returns the same outputs as the original
 % network,but outputs are shifted left one timestep.
 nets = removedelay(net);
 nets.name = [net.name ' - Predict One Step Ahead'];
 view(nets)
 [xs,xis,ais,ts] = preparets(nets,T);
  ys = nets(xs,ais);
  stepAheadPerformance = perform(nets,ts,ys)

  % 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,ai);
end
if (false)
% Generate a matrix-only MATLAB function for neural network code
% generation with MATLAB Coder tools.
genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');
x1 = cell2mat(x(1,:));
xi1 = cell2mat(xi(1,:));
y = myNeuralNetworkFunction(x1,xi1);
end
if (false)
% Generate a Simulink diagram for simulation or deployment with.
% Simulink Coder tools.
gensim(net);
end
 % Solve an Input-Output Time-Series Problem with a Time Delay Neural Network
% Script generated by Neural Time Series app.
% Created 03-Nov-2020 23:33:27
%
% This script assumes these variables are defined:
%
%   data - input time series.
%   data_1 - target time series.

X = tonndata(data,false);
T = tonndata(data_1,false);

% 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 Time Delay Network
 inputDelays = 1:2;
 hiddenLayerSize = 10;
net = timedelaynet(inputDelays,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'};

 % Prepare the Data for Training and Simulation
 % The function PREPARETS prepares timeseries data for a particular network,% shifting time by the minimum amount to fill input states and layer
 % states. Using PREPARETS allows you to keep your original time series data
 % unchanged,while easily customizing it for networks with differing
 % numbers of delays,with open loop or closed loop feedback modes.
 [x,T);
  
 % Setup Division of Data for Training,Testing
 % For a list of all data division functions type: help nndivision
 net.divideFcn = 'dividerand';  % Divide data randomly
 net.divideMode = 'time';  % 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';  % Mean Squared Error

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

 % Train the Network 
[net,ai);

% Test the Network
y = net(x,ai);
e = gsubtract(t,y);
performance = perform(net,y)

% Recalculate Training,Validation and Test Performance
trainTargets = gmultiply(t,tr.trainMask);
valTargets = gmultiply(t,tr.valMask);
testTargets = gmultiply(t,tr.testMask);
trainPerformance = perform(net,y)
valPerformance = perform(net,y)

% View the Network
view(net)

% Plots
%Uncomment these lines to enable various plots.
%figure,e)

% Step-Ahead Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is
% given x(t+1). For some applications such as decision making,it would
% help to have predicted y(t+1) once x(t) is available,but before the 
% actual y(t+1) occurs. The network can be made to return its output a
% timestep early by removing one delay so that its minimal tap delay is now
% 0 instead of 1. The new network returns the same outputs as the original
% network,but outputs are shifted left one timestep.
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,T);
ys = nets(xs,ais);
stepAheadPerformance = perform(nets,ys)

% 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,xi1);
end
if (false)
% Generate a Simulink diagram for simulation or deployment with.
% Simulink Coder tools.
gensim(net);
end

解决方法

对于分类模型,请在模型对象Y = predict(Mdl,X)上使用predict

对于回归模型,请在模型对象Y = sim(Mdl,X)上使用sim

与其他语言不同,MATLAB并不将所有方法都包装到一个类中,而是具有一个适合所有模型的命令(实际上,有两个命令:一个用于分类数据,另一个用于连续预测)。因此,您也可以在SVM(fitcsvm / fitrsvm)或kNN(fitcknn)上使用它们。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-