@devilogic
2016-08-02T06:10:08.000000Z
字数 3340
阅读 2312
matlab
神经网络是非常善于拟合函数的,事实上,有一个很精巧的证明显示神经网络能拟合任意实际的函数。
假设,举例说明你有一些房屋的信息。你想设计一个神经网络让它来预测一个房屋的价值,给定13个地理与真实的房产信息。你有506个这样13个特征的信息,并且有这506个样本关联的市场价格。
整理一个Q个输入向量以列为单位组合成一个矩阵。然后安排另外一个Q列目标向量(有效的输出向量是一一对应每个输入向量)组合成第二个矩阵(理解神经网络的数据结构一文中详细描述了数据格式如何适应静态数据与时间序列数据)。举个例子,你能定义一个布尔值的与门拟合问题,它有四个两元素输入向量与一个目标向量如下:
inputs = [0 1 0 1; 0 0 1 1];targets = [0 0 0 1];
% Solve an Input-Output Fitting problem with a Neural Network% Script generated by Neural Fitting app% Created 01-Aug-2016 15:03:58%% This script assumes these variables are defined:%% houseInputs - 输入数据.% houseTargets - 目标数据.x = houseInputs;t = houseTargets;% 选择一个训练函数% 列出所有的训练函数,键入:help nntrain% 'trainlm' 通常是最快的.% 'trainbr' 花费时间更长但是可以适用更有复杂的问题.% 'trainscg' 使用较少的内存.适合配置较低的机器.trainFcn = 'trainlm'; % Levenberg-Marquardt 反向传播.% 创建一个拟合网络hiddenLayerSize = 10; % 隐藏层为10net = fitnet(hiddenLayerSize,trainFcn);% 分离数据为:训练,验证,测试net.divideParam.trainRatio = 70/100; % 训练使用70%的数据net.divideParam.valRatio = 15/100; % 验证使用15%的数据net.divideParam.testRatio = 15/100; % 测试使用15%的数据% 训练网络[net,tr] = train(net,x,t);% 测试网络y = net(x);e = gsubtract(t,y);performance = perform(net,t,y)% 浏览网络view(net)% 画图% 反注释掉以下代码开启画图功能.%figure, plotperform(tr)%figure, plottrainstate(tr)%figure, ploterrhist(e)%figure, plotregression(t,y)%figure, plotfit(net,x,t)
% Solve an Input-Output Fitting problem with a Neural Network% Script generated by Neural Fitting app% Created 01-Aug-2016 15:04:12%% This script assumes these variables are defined:%% houseInputs - 输入数据.% houseTargets - 目标数据.x = houseInputs;t = houseTargets;% 选择一个训练函数% 列出所有的训练函数,键入:help nntrain% 'trainlm' 通常是最快的.% 'trainbr' 花费时间更长但是可以适用更有复杂的问题.% 'trainscg' 使用较少的内存.适合配置较低的机器.trainFcn = 'trainlm'; % Levenberg-Marquardt 反向传播.% 创建一个拟合网络hiddenLayerSize = 10; % 隐藏层为10net = fitnet(hiddenLayerSize,trainFcn);% 创建适用性网络hiddenLayerSize = 10;net = fitnet(hiddenLayerSize,trainFcn);% 选择输入与输出预处理处理函数.% 列出所有的数据预处理函数,键入:help nnprocessnet.input.processFcns = {'removeconstantrows','mapminmax'};net.output.processFcns = {'removeconstantrows','mapminmax'};% 设置分离数据为:训练,验证,测试% 列出所有数据分离函数,键入:help nndividenet.divideFcn = 'dividerand'; % 分离数据随机net.divideMode = 'sample'; % 分离每个样本net.divideParam.trainRatio = 70/100;net.divideParam.valRatio = 15/100;net.divideParam.testRatio = 15/100;% 选择一个性能评估函数% 列出所有性能函数,键入:help nnperformancenet.performFcn = 'mse'; % Mean Squared Error% 选择绘图函数% 列出所有绘图函数,键入:help nnplotnet.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...'plotregression', 'plotfit'};% 训练[net,tr] = train(net,x,t);% 测试网络y = net(x);e = gsubtract(t,y);performance = perform(net,t,y)% 重新计算 训练,验证和测试性能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(net)% 画图% 反注释掉以下代码开启画图功能.%figure, plotperform(tr)%figure, plottrainstate(tr)%figure, ploterrhist(e)%figure, plotregression(t,y)%figure, plotfit(net,x,t)% 部署% 改变 (false) 值到 (true) 来启用以下代码块.% 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);endif (false)% Generate a matrix-only MATLAB function for neural network code% generation with MATLAB Coder tools.genFunction(net,'myNeuralNetworkFunction','MatrixOnly','yes');y = myNeuralNetworkFunction(x);endif (false)% 为仿真或者部署产生一个仿真模型图gensim(net);end