close all
clear all
clc
load x.txt;

load y.txt;

inputs = x';
targets = y;

% 创建一个模式识别网络(两层BP网络),同时给出中间层神经元的个数,这里使用20
hiddenLayerSize = 20;
net = patternnet(hiddenLayerSize);

% 对数据进行预处理,这里使用了归一化函数(一般不用修改)
% For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};

% 把训练数据分成三部分,训练网络、验证网络、测试网络
% 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 a list of all training functions type: help nntrain
net.trainFcn = 'trainlm';  % Levenberg-Marquardt

% 使用均方误差来评估网络
% For a list of all performance functions type: help nnperformance
net.performFcn = 'mse';  % Mean squared error

% 画图函数
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
  'plotregression', 'plotfit'};

% 开始训练网络(包含了训练和验证的过程)
[net,tr] = train(net,inputs,targets);

% 测试网络
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs)

% 获得训练、验证和测试的结果
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(net)

% 根据画图的结果,决定是否满意
% Uncomment these lines to enable various plots.
figure, plotperform(tr)
figure, plottrainstate(tr)
figure, plotconfusion(targets,outputs)
figure, ploterrhist(errors)
% Test the Network
load z.txt;

testinputs= z';
testoutputs = net(testinputs);

BP神经网络代码的更多相关文章

  1. 感知机算法及BP神经网络

    简介:感知机在1957年就已经提出,可以说是最为古老的分类方法之一了.是很多算法的鼻祖,比如说BP神经网络.虽然在今天看来它的分类模型在很多数时候泛化能力不强,但是它的原理却值得好好研究.先学好感知机 ...

  2. 三.BP神经网络

    BP神经网络是包含多个隐含层的网络,具备处理线性不可分问题的能力.以往主要是没有适合多层神经网络的学习算法,,所以神经网络的研究一直处于低迷期. 20世纪80年代中期,Rumelhart,McClel ...

  3. BP神经网络算法推导及代码实现笔记zz

    一. 前言: 作为AI入门小白,参考了一些文章,想记点笔记加深印象,发出来是给有需求的童鞋学习共勉,大神轻拍! [毒鸡汤]:算法这东西,读完之后的状态多半是 --> “我是谁,我在哪?” 没事的 ...

  4. python构建bp神经网络_曲线拟合(一个隐藏层)__2.代码实现

    IDE:jupyter 抽象程度可能不是那么高,以后再优化. 理论和代码实现的差距还是挺大的 数据集请查看 python构建bp神经网络(一个隐藏层)__1.数据可视化 部分代码预览 git上传.ip ...

  5. 用Python实现BP神经网络(附代码)

    用Python实现出来的机器学习算法都是什么样子呢? 前两期线性回归及逻辑回归项目已发布(见文末链接),今天来讲讲BP神经网络. BP神经网络 全部代码 https://github.com/lawl ...

  6. 简洁的BP及RBF神经网络代码

    BP神经网络 function [W,err]=BPTrain(data,label,hiddenlayers,nodes,type) %Train the bp artial nueral net ...

  7. Matlab实现BP神经网络预测(附实例数据及代码)

    BP神经网络介绍 神经网络是机器学习中一种常见的数学模型,通过构建类似于大脑神经突触联接的结构,来进行信息处理.在应用神经网络的过程中,处理信息的单元一般分为三类:输入单元.输出单元和隐含单元. 顾名 ...

  8. BP神经网络原理及python实现

    [废话外传]:终于要讲神经网络了,这个让我踏进机器学习大门,让我读研,改变我人生命运的四个字!话说那么一天,我在乱点百度,看到了这样的内容: 看到这么高大上,这么牛逼的定义,怎么能不让我这个技术宅男心 ...

  9. 数据挖掘系列(9)——BP神经网络算法与实践

    神经网络曾经很火,有过一段低迷期,现在因为深度学习的原因继续火起来了.神经网络有很多种:前向传输网络.反向传输网络.递归神经网络.卷积神经网络等.本文介绍基本的反向传输神经网络(Backpropaga ...

随机推荐

  1. iOS 调用私有函数安装app 卸载 app

    1.环境 1.OS X EI Caption 10.11.1 & Xcode 7 2.Xcode安装Command Line Tools 3.iPhone 安装AppSync 2.Mobile ...

  2. HUD 1171 Big Event in HDU(01背包)

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  3. A* 寻路算法学习

    代码小记 #include <iostream> #include <list> struct POINT { int X; int Y; }; // G: 起点到当前点的成本 ...

  4. ajax写法

    $.ajax({ type:'post', url:'<%=path%>/login', cache:false, dataType:'json', success:function(da ...

  5. Linux 文件服务---------- nfs Server

    Linux 文件服务nfs (Network file system)#网络文件系统 ---> 远程文件调用samba #文件共享(unix /linux /windows ) ,只能适用于局域 ...

  6. hibernate增删改查

    -----------增加--------- public void insertUsers(String userName,String userPwd) { Users u=new Users() ...

  7. redirect-action

    功能: 点击login , redirect 到hello.jsp 显示 "hello" 点击redirect, redirect 到 error.jsp 显示 "err ...

  8. ubuntu 10.4非法关机后上不了网

    用的好好的ubuntu 10.4,非法关机后居然上不了网,右上角的网络图标也不见了,还以为是网卡问题,进入xp,发现一切正常,心里不断地诅咒ubuntu,该死的ubuntu,我windows还天天非法 ...

  9. if和switch的原理

    在C语言中,if和switch是条件分支的重要组成部分.if的功能是计算判断条件的值,根据返回的值的不同来决定跳转到哪个部分.值为真则跳转到if语句块中,否则跳过if语句块.下面来分析一个简单的if实 ...

  10. java中的词汇

    java中的词汇: 空白符:空格,制表符,换行符.他们的存在使得代码变得很美观. 标识符:由大小写字母,数字,下划线,美元符号组成.且数字不能用于标识符第一个字符. 字面值:变量的值通常使用表示常量的 ...