当前位置:首页 » 操作系统 » 神经网络源码

神经网络源码

发布时间: 2022-10-25 01:40:09

Ⅰ 用c语言编写RBF神经网络程序

RBF网络能够逼近任意的非线性函数,可以处理系统内的难以解析的规律性,具有良好的泛化能力,并有很快的学习收敛速度,已成功应用于非线性函数逼近、时间序列分析、数据分类、模式识别、信息处理、图像处理、系统建模、控制和故障诊断等。

简单说明一下为什么RBF网络学习收敛得比较快。当网络的一个或多个可调参数(权值或阈值)对任何一个输出都有影响时,这样的网络称为全局逼近网络。由于对于每次输入,网络上的每一个权值都要调整,从而导致全局逼近网络的学习速度很慢。BP网络就是一个典型的例子。

如果对于输入空间的某个局部区域只有少数几个连接权值影响输出,则该网络称为局部逼近网络。常见的局部逼近网络有RBF网络、小脑模型(CMAC)网络、B样条网络等。


附件是RBF神经网络的C++源码

Ⅱ 复杂神经网络模型用什么软件

bp神经网络能用MATLAB,
理论上编程语言都可以,比如VB,C语言,过程也都是建模、量化、运算及结果输出(图、表),但是matlab发展到现在,集成了很多的工具箱,所以用的最为广泛,用其他的就得是要从源码开发入手了。
bp神经网络是一种算法,只要是算法就可以用任何软件工具,只要编译器或者解释器支持,c,c++,python,来进行实现,只是实现时的复杂程度有区别而已

Ⅲ 如何用9行Python代码编写一个简易神经网络

学习人工智能时,我给自己定了一个目标--用Python写一个简单的神经网络。为了确保真得理解它,我要求自己不使用任何神经网络库,从头写起。多亏了Andrew Trask写得一篇精彩的博客,我做到了!下面贴出那九行代码:在这篇文章中,我将解释我是如何做得,以便你可以写出你自己的。我将会提供一个长点的但是更完美的源代码。

首先,神经网络是什么?人脑由几千亿由突触相互连接的细胞(神经元)组成。突触传入足够的兴奋就会引起神经元的兴奋。这个过程被称为“思考”。我们可以在计算机上写一个神经网络来模拟这个过程。不需要在生物分子水平模拟人脑,只需模拟更高层级的规则。我们使用矩阵(二维数据表格)这一数学工具,并且为了简单明了,只模拟一个有3个输入和一个输出的神经元。

我们将训练神经元解决下面的问题。前四个例子被称作训练集。你发现规律了吗?‘?’是0还是1?你可能发现了,输出总是等于输入中最左列的值。所以‘?’应该是1。

训练过程

但是如何使我们的神经元回答正确呢?赋予每个输入一个权重,可以是一个正的或负的数字。拥有较大正(或负)权重的输入将决定神经元的输出。首先设置每个权重的初始值为一个随机数字,然后开始训练过程:

取一个训练样本的输入,使用权重调整它们,通过一个特殊的公式计算神经元的输出。

计算误差,即神经元的输出与训练样本中的期待输出之间的差值。

根据误差略微地调整权重。

重复这个过程1万次。最终权重将会变为符合训练集的一个最优解。如果使用神经元考虑这种规律的一个新情形,它将会给出一个很棒的预测。

这个过程就是back propagation。

计算神经元输出的公式

你可能会想,计算神经元输出的公式是什么?首先,计算神经元输入的加权和,即接着使之规范化,结果在0,1之间。为此使用一个数学函数--Sigmoid函数:Sigmoid函数的图形是一条“S”状的曲线。把第一个方程代入第二个,计算神经元输出的最终公式为:你可能注意到了,为了简单,我们没有引入最低兴奋阈值。

调整权重的公式

我们在训练时不断调整权重。但是怎么调整呢?可以使用“Error Weighted Derivative”公式:为什么使用这个公式?首先,我们想使调整和误差的大小成比例。其次,乘以输入(0或1),如果输入是0,权重就不会调整。最后,乘以Sigmoid曲线的斜率(图4)。为了理解最后一条,考虑这些:

我们使用Sigmoid曲线计算神经元的输出

如果输出是一个大的正(或负)数,这意味着神经元采用这种(或另一种)方式

从图四可以看出,在较大数值处,Sigmoid曲线斜率小

如果神经元认为当前权重是正确的,就不会对它进行很大调整。乘以Sigmoid曲线斜率便可以实现这一点

Sigmoid曲线的斜率可以通过求导得到:把第二个等式代入第一个等式里,得到调整权重的最终公式:当然有其他公式,它们可以使神经元学习得更快,但是这个公式的优点是非常简单。

构造Python代码

虽然我们没有使用神经网络库,但是将导入Python数学库numpy里的4个方法。分别是:

exp--自然指数

array--创建矩阵

dot--进行矩阵乘法

random--产生随机数

比如, 我们可以使用array()方法表示前面展示的训练集:“.T”方法用于矩阵转置(行变列)。所以,计算机这样存储数字:我觉得我们可以开始构建更优美的源代码了。给出这个源代码后,我会做一个总结。

我对每一行源代码都添加了注释来解释所有内容。注意在每次迭代时,我们同时处理所有训练集数据。所以变量都是矩阵(二维数据表格)。下面是一个用Python写地完整的示例代码。

我们做到了!我们用Python构建了一个简单的神经网络!

首先神经网络对自己赋予随机权重,然后使用训练集训练自己。接着,它考虑一种新的情形[1, 0, 0]并且预测了0.99993704。正确答案是1。非常接近!

传统计算机程序通常不会学习。而神经网络却能自己学习,适应并对新情形做出反应,这是多么神奇,就像人类一样。

Ⅳ matlab BP神经网络的训练算法中训练函数(traingdm 、trainlm、trainbr)的实现过程及相应的VC源代码

VC源代码?你很搞笑嘛。。
给你trainlm的m码

function [out1,out2] = trainlm(varargin)
%TRAINLM Levenberg-Marquardt backpropagation.
%
% <a href="matlab:doc trainlm">trainlm</a> is a network training function that updates weight and
% bias states according to Levenberg-Marquardt optimization.
%
% <a href="matlab:doc trainlm">trainlm</a> is often the fastest backpropagation algorithm in the toolbox,
% and is highly recommended as a first choice supervised algorithm,
% although it does require more memory than other algorithms.
%
% [NET,TR] = <a href="matlab:doc trainlm">trainlm</a>(NET,X,T) takes a network NET, input data X
% and target data T and returns the network after training it, and a
% a training record TR.
%
% [NET,TR] = <a href="matlab:doc trainlm">trainlm</a>(NET,X,T,Xi,Ai,EW) takes additional optional
% arguments suitable for training dynamic networks and training with
% error weights. Xi and Ai are the initial input and layer delays states
% respectively and EW defines error weights used to indicate
% the relative importance of each target value.
%
% Training occurs according to training parameters, with default values.
% Any or all of these can be overridden with parameter name/value argument
% pairs appended to the input argument list, or by appending a structure
% argument with fields having one or more of these names.
% show 25 Epochs between displays
% showCommandLine 0 generate command line output
% showWindow 1 show training GUI
% epochs 100 Maximum number of epochs to train
% goal 0 Performance goal
% max_fail 5 Maximum validation failures
% min_grad 1e-10 Minimum performance gradient
% mu 0.001 Initial Mu
% mu_dec 0.1 Mu decrease factor
% mu_inc 10 Mu increase factor
% mu_max 1e10 Maximum Mu
% time inf Maximum time to train in seconds
%
% To make this the default training function for a network, and view
% and/or change parameter settings, use these two properties:
%
% net.<a href="matlab:doc nnproperty.net_trainFcn">trainFcn</a> = 'trainlm';
% net.<a href="matlab:doc nnproperty.net_trainParam">trainParam</a>
%
% See also trainscg, feedforwardnet, narxnet.

% Mark Beale, 11-31-97, ODJ 11/20/98
% Updated by Orlando De Jes鹥, Martin Hagan, Dynamic Training 7-20-05
% Copyright 1992-2010 The MathWorks, Inc.
% $Revision: 1.1.6.11.2.2 $ $Date: 2010/07/23 15:40:16 $

%% =======================================================
% BOILERPLATE_START
% This code is the same for all Training Functions.

persistent INFO;
if isempty(INFO), INFO = get_info; end
nnassert.minargs(nargin,1);
in1 = varargin{1};
if ischar(in1)
switch (in1)
case 'info'
out1 = INFO;
case 'check_param'
nnassert.minargs(nargin,2);
param = varargin{2};
err = nntest.param(INFO.parameters,param);
if isempty(err)
err = check_param(param);
end
if nargout > 0
out1 = err;
elseif ~isempty(err)
nnerr.throw('Type',err);
end
otherwise,
try
out1 = eval(['INFO.' in1]);
catch me, nnerr.throw(['Unrecognized first argument: ''' in1 ''''])
end
end
return
end
nnassert.minargs(nargin,2);
net = nn.hints(nntype.network('format',in1,'NET'));
oldTrainFcn = net.trainFcn;
oldTrainParam = net.trainParam;
if ~strcmp(net.trainFcn,mfilename)
net.trainFcn = mfilename;
net.trainParam = INFO.defaultParam;
end
[args,param] = nnparam.extract_param(varargin(2:end),net.trainParam);
err = nntest.param(INFO.parameters,param);
if ~isempty(err), nnerr.throw(nnerr.value(err,'NET.trainParam')); end
if INFO.isSupervised && isempty(net.performFcn) % TODO - fill in MSE
nnerr.throw('Training function is supervised but NET.performFcn is undefined.');
end
if INFO.usesGradient && isempty(net.derivFcn) % TODO - fill in
nnerr.throw('Training function uses derivatives but NET.derivFcn is undefined.');
end
if net.hint.zeroDelay, nnerr.throw('NET contains a zero-delay loop.'); end
[X,T,Xi,Ai,EW] = nnmisc.defaults(args,{},{},{},{},{1});
X = nntype.data('format',X,'Inputs X');
T = nntype.data('format',T,'Targets T');
Xi = nntype.data('format',Xi,'Input states Xi');
Ai = nntype.data('format',Ai,'Layer states Ai');
EW = nntype.nndata_pos('format',EW,'Error weights EW');
% Prepare Data
[net,data,tr,~,err] = nntraining.setup(net,mfilename,X,Xi,Ai,T,EW);
if ~isempty(err), nnerr.throw('Args',err), end
% Train
net = struct(net);
fcns = nn.subfcns(net);
[net,tr] = train_network(net,tr,data,fcns,param);
tr = nntraining.tr_clip(tr);
if isfield(tr,'perf')
tr.best_perf = tr.perf(tr.best_epoch+1);
end
if isfield(tr,'vperf')
tr.best_vperf = tr.vperf(tr.best_epoch+1);
end
if isfield(tr,'tperf')
tr.best_tperf = tr.tperf(tr.best_epoch+1);
end
net.trainFcn = oldTrainFcn;
net.trainParam = oldTrainParam;
out1 = network(net);
out2 = tr;
end

% BOILERPLATE_END
%% =======================================================

% TODO - MU => MU_START
% TODO - alternate parameter names (i.e. MU for MU_START)

function info = get_info()
info = nnfcnTraining(mfilename,'Levenberg-Marquardt',7.0,true,true,...
[ ...
nnetParamInfo('showWindow','Show Training Window Feedback','nntype.bool_scalar',true,...
'Display training window ring training.'), ...
nnetParamInfo('showCommandLine','Show Command Line Feedback','nntype.bool_scalar',false,...
'Generate command line output ring training.'), ...
nnetParamInfo('show','Command Line Frequency','nntype.strict_pos_int_inf_scalar',25,...
'Frequency to update command line.'), ...
...
nnetParamInfo('epochs','Maximum Epochs','nntype.pos_int_scalar',1000,...
'Maximum number of training iterations before training is stopped.'), ...
nnetParamInfo('time','Maximum Training Time','nntype.pos_inf_scalar',inf,...
'Maximum time in seconds before training is stopped.'), ...
...
nnetParamInfo('goal','Performance Goal','nntype.pos_scalar',0,...
'Performance goal.'), ...
nnetParamInfo('min_grad','Minimum Gradient','nntype.pos_scalar',1e-5,...
'Minimum performance gradient before training is stopped.'), ...
nnetParamInfo('max_fail','Maximum Validation Checks','nntype.strict_pos_int_scalar',6,...
'Maximum number of validation checks before training is stopped.'), ...
...
nnetParamInfo('mu','Mu','nntype.pos_scalar',0.001,...
'Mu.'), ...
nnetParamInfo('mu_dec','Mu Decrease Ratio','nntype.real_0_to_1',0.1,...
'Ratio to decrease mu.'), ...
nnetParamInfo('mu_inc','Mu Increase Ratio','nntype.over1',10,...
'Ratio to increase mu.'), ...
nnetParamInfo('mu_max','Maximum mu','nntype.strict_pos_scalar',1e10,...
'Maximum mu before training is stopped.'), ...
], ...
[ ...
nntraining.state_info('gradient','Gradient','continuous','log') ...
nntraining.state_info('mu','Mu','continuous','log') ...
nntraining.state_info('val_fail','Validation Checks','discrete','linear') ...
]);
end

function err = check_param(param)
err = '';
end

function [net,tr] = train_network(net,tr,data,fcns,param)

% Checks
if isempty(net.performFcn)
warning('nnet:trainlm:Performance',nnwarning.empty_performfcn_corrected);
net.performFcn = 'mse';
net.performParam = mse('defaultParam');
tr.performFcn = net.performFcn;
tr.performParam = net.performParam;
end
if isempty(strmatch(net.performFcn,{'sse','mse'},'exact'))
warning('nnet:trainlm:Performance',nnwarning.nonjacobian_performfcn_replaced);
net.performFcn = 'mse';
net.performParam = mse('defaultParam');
tr.performFcn = net.performFcn;
tr.performParam = net.performParam;
end

% Initialize
startTime = clock;
original_net = net;
[perf,vperf,tperf,je,jj,gradient] = nntraining.perfs_jejj(net,data,fcns);
[best,val_fail] = nntraining.validation_start(net,perf,vperf);
WB = getwb(net);
lengthWB = length(WB);
ii = sparse(1:lengthWB,1:lengthWB,ones(1,lengthWB));
mu = param.mu;

% Training Record
tr.best_epoch = 0;
tr.goal = param.goal;
tr.states = {'epoch','time','perf','vperf','tperf','mu','gradient','val_fail'};

% Status
status = ...
[ ...
nntraining.status('Epoch','iterations','linear','discrete',0,param.epochs,0), ...
nntraining.status('Time','seconds','linear','discrete',0,param.time,0), ...
nntraining.status('Performance','','log','continuous',perf,param.goal,perf) ...
nntraining.status('Gradient','','log','continuous',gradient,param.min_grad,gradient) ...
nntraining.status('Mu','','log','continuous',mu,param.mu_max,mu) ...
nntraining.status('Validation Checks','','linear','discrete',0,param.max_fail,0) ...
];
nn_train_feedback('start',net,status);

% Train
for epoch = 0:param.epochs

% Stopping Criteria
current_time = etime(clock,startTime);
[userStop,userCancel] = nntraintool('check');
if userStop, tr.stop = 'User stop.'; net = best.net;
elseif userCancel, tr.stop = 'User cancel.'; net = original_net;
elseif (perf <= param.goal), tr.stop = 'Performance goal met.'; net = best.net;
elseif (epoch == param.epochs), tr.stop = 'Maximum epoch reached.'; net = best.net;
elseif (current_time >= param.time), tr.stop = 'Maximum time elapsed.'; net = best.net;
elseif (gradient <= param.min_grad), tr.stop = 'Minimum gradient reached.'; net = best.net;
elseif (mu >= param.mu_max), tr.stop = 'Maximum MU reached.'; net = best.net;
elseif (val_fail >= param.max_fail), tr.stop = 'Validation stop.'; net = best.net;
end

% Feedback
tr = nntraining.tr_update(tr,[epoch current_time perf vperf tperf mu gradient val_fail]);
nn_train_feedback('update',net,status,tr,data, ...
[epoch,current_time,best.perf,gradient,mu,val_fail]);

% Stop
if ~isempty(tr.stop), break, end

% Levenberg Marquardt
while (mu <= param.mu_max)
% CHECK FOR SINGULAR MATRIX
[msgstr,msgid] = lastwarn;
lastwarn('MATLAB:nothing','MATLAB:nothing')
warnstate = warning('off','all');
dWB = -(jj+ii*mu) \ je;
[~,msgid1] = lastwarn;
flag_inv = isequal(msgid1,'MATLAB:nothing');
if flag_inv, lastwarn(msgstr,msgid); end;
warning(warnstate)
WB2 = WB + dWB;
net2 = setwb(net,WB2);
perf2 = nntraining.train_perf(net2,data,fcns);

% TODO - possible speed enhancement
% - retain intermediate variables for Memory Rection = 1

if (perf2 < perf) && flag_inv
WB = WB2; net = net2;
mu = max(mu*param.mu_dec,1e-20);
break
end
mu = mu * param.mu_inc;
end

% Validation
[perf,vperf,tperf,je,jj,gradient] = nntraining.perfs_jejj(net,data,fcns);
[best,tr,val_fail] = nntraining.validation(best,tr,val_fail,net,perf,vperf,epoch);
end
end

Ⅳ BP神经网络的训练集需要大样本吗一般样本个数为多少

BP神经网络的训练集需要大样本吗?一般样本个数为多少?
BP神经网络样本数有什么影响
学习神经网络这段时间,有一个疑问,BP神经网络中训练的次数指的网络的迭代次数,如果有a个样本,每个样本训练次数n,则网络一共迭代an次,在n>>a 情况下 , 网络在不停的调整权值,减小误差,跟样本数似乎关系不大。而且,a大了的话训练时间必然会变长。
换一种说法,将你的数据集看成一个固定值, 那么样本集与测试集 也可以按照某种规格确定下来如7:3 所以如何看待 样本集的多少与训练结果呢? 或者说怎么使你的网络更加稳定,更加符合你的所需 。

我尝试从之前的一个例子中看下区别

如何用70行java代码实现深度神经网络算法

作者其实是实现了一个BP神经网络 ,不多说,看最后的例子

一个运用神经网络的例子
最后我们找个简单例子来看看神经网络神奇的效果。为了方便观察数据分布,我们选用一个二维坐标的数据,下面共有4个数据,方块代表数据的类型为1,三角代表数据的类型为0,可以看到属于方块类型的数据有(1,2)和(2,1),属于三角类型的数据有(1,1),(2,2),现在问题是需要在平面上将4个数据分成1和0两类,并以此来预测新的数据的类型。


图片描述

我们可以运用逻辑回归算法来解决上面的分类问题,但是逻辑回归得到一个线性的直线做为分界线,可以看到上面的红线无论怎么摆放,总是有一个样本被错误地划分到不同类型中,所以对于上面的数据,仅仅一条直线不能很正确地划分他们的分类,如果我们运用神经网络算法,可以得到下图的分类效果,相当于多条直线求并集来划分空间,这样准确性更高。

图片描述

简单粗暴,用作者的代码运行后 训练5000次 。根据训练结果来预测一条新数据的分类(3,1)



预测值 (3,1)的结果跟(1,2)(2,1)属于一类 属于正方形

这时如果我们去掉 2个样本,则样本输入变成如下

//设置样本数据,对应上面的4个二维坐标数据
double[][] data = new double[][]{{1,2},{2,2}};
//设置目标数据,对应4个坐标数据的分类
double[][] target = new double[][]{{1,0},{0,1}};
1
2
3
4
1
2
3
4




则(3,1)结果变成了三角形,

如果你选前两个点 你会发现直接一条中间线就可以区分 这时候的你的结果跟之前4个点时有区别 so 你得增加样本 直到这些样本按照你所想要的方式分类 ,所以样本的多少 重要性体现在,样本得能反映所有的特征值(也就是输入值) ,样本多少或者特征(本例子指点的位置特征)决定的你的网络的训练结果,!!!这是 我们反推出来的结果 。这里距离深度学习好像近了一步。

另外,这个70行代码的神经网络没有保存你训练的网络 ,所以你每次运行都是重新训练的网络。其实,在你训练过后 权值已经确定了下来,我们确定网络也就是根据权值,so只要把训练后的权值保存下来,将需要分类的数据按照这种权值带入网络,即可得到输出值,也就是一旦网络确定, 权值也就确定,一个输入对应一个固定的输出,不会再次改变!个人见解。

最后附上作者的源码,作者的文章见开头链接
下面的实现程序BpDeep.java可以直接拿去使用,

import java.util.Random;
public class BpDeep{
public double[][] layer;//神经网络各层节点
public double[][] layerErr;//神经网络各节点误差
public double[][][] layer_weight;//各层节点权重
public double[][][] layer_weight_delta;//各层节点权重动量
public double mobp;//动量系数
public double rate;//学习系数

public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;l<layernum.length;l++){
layer[l]=new double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1<layernum.length){
layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;j<layernum[l]+1;j++)
for(int i=0;i<layernum[l+1];i++)
layer_weight[l][j][i]=random.nextDouble();//随机初始化权重
}
}
}
//逐层向前计算输出
public double[] computeOut(double[] in){
for(int l=1;l<layer.length;l++){
for(int j=0;j<layer[l].length;j++){
double z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;i<layer[l-1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐层反向计算误差并修改权重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;j<layerErr[l].length;j++)
layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);

while(l-->0){
for(int j=0;j<layerErr[l].length;j++){
double z = 0.0;
for(int i=0;i<layerErr[l+1].length;i++){
z=z+l>0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隐含层动量调整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距动量调整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距权重调整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//记录误差
}
}
}

public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
下面是这个测试程序BpDeepTest.java的源码:

import java.util.Arrays;
public class BpDeepTest{
public static void main(String[] args){
//初始化神经网络的基本配置
//第一个参数是一个整型数组,表示神经网络的层数和每层节点数,比如{3,10,10,10,10,2}表示输入层是3个节点,输出层是2个节点,中间有4层隐含层,每层10个节点
//第二个参数是学习步长,第三个参数是动量系数
BpDeep bp = new BpDeep(new int[]{2,10,2}, 0.15, 0.8);

//设置样本数据,对应上面的4个二维坐标数据
double[][] data = new double[][]{{1,2},{2,2},{1,1},{2,1}};
//设置目标数据,对应4个坐标数据的分类
double[][] target = new double[][]{{1,0},{0,1},{0,1},{1,0}};

//迭代训练5000次
for(int n=0;n<5000;n++)
for(int i=0;i<data.length;i++)
bp.train(data[i], target[i]);

//根据训练结果来检验样本数据
for(int j=0;j<data.length;j++){
double[] result = bp.computeOut(data[j]);
System.out.println(Arrays.toString(data[j])+":"+Arrays.toString(result));
}

//根据训练结果来预测一条新数据的分类
double[] x = new double[]{3,1};
double[] result = bp.computeOut(x);
System.out.println(Arrays.toString(x)+":"+Arrays.toString(result));
}
}

Ⅵ 神经网络研究与应用这块用python好还是matlab

两者或许无所谓好与坏。只要自己喜欢用,那就是好的,但是目前代码数量来看,可以学习的源代码MATLAB有非常多的源码。最重要的是,MATLAB里有神经网络工具箱,有可视化界面更容易调整参数。若果你是需要使用神经网络去完成某些数据分析,而你的数据又不是很多,那么建议你使用matlab,里面有已经搭建好的工具箱,非常齐全。

若果你对神经网络已经熟悉是,是打算投入应用,而且你的数据很大,那么根据你所需要的神经网络,用C或其他你认为性能好的语言,针对你的问题重新编一个算法,也不会花很大功夫。这样既省了自己的时间,又让自己轻松学习。总结来说,不论你学什么,用什么路径去学总是会达到想要的目的,但是重要的是在于学习的过程。

Ⅶ BP神经网络算法的C++源代码可否尽快发一份,[email protected]

#pragma hdrstop
#include <stdio.h>
#include <iostream.h>
const A=30.0;
const B=10.0;
const MAX=500; //最大训练次数
const COEF=0.0035; //网络的学习效率
const BCOEF=0.001;//网络的阀值调整效率
const ERROR=0.002 ; // 网络训练中的允许误差
const ACCURACY=0.0005;//网络要求精度
double sample[41][4]={{0,0,0,0},{5,1,4,19.020},{5,3,3,14.150},
{5,5,2,14.360},{5,3,3,14.150},{5,3,2,15.390},
{5,3,2,15.390},{5,5,1,19.680},{5,1,2,21.060},
{5,3,3,14.150},{5,5,4,12.680},{5,5,2,14.360},
{5,1,3,19.610},{5,3,4,13.650},{5,5,5,12.430},
{5,1,4,19.020},{5,1,4,19.020},{5,3,5,13.390},
{5,5,4,12.680},{5,1,3,19.610},{5,3,2,15.390},
{1,3,1,11.110},{1,5,2,6.521},{1,1,3,10.190},
{1,3,4,6.043},{1,5,5,5.242},{1,5,3,5.724},
{1,1,4,9.766},{1,3,5,5.870},{1,5,4,5.406},
{1,1,3,10.190},{1,1,5,9.545},{1,3,4,6.043},
{1,5,3,5.724},{1,1,2,11.250},{1,3,1,11.110},
{1,3,3,6.380},{1,5,2,6.521},{1,1,1,16.000},
{1,3,2,7.219},{1,5,3,5.724}};
double w[4][10][10],wc[4][10][10],b[4][10],bc[4][10];
double o[4][10],netin[4][10],d[4][10],differ;//单个样本的误差
double is; //全体样本均方差
int count,a;
void netout(int m, int n);//计算网络隐含层和输出层的输出
void calculd(int m,int n); //计算网络的反向传播误差
void calcalwc(int m,int n);//计算网络权值的调整量
void calcaulbc(int m,int n); //计算网络阀值的调整量
void changew(int m,int n); //调整网络权值
void changeb(int m,int n);//调整网络阀值
void clearwc(int m,int n);//清除网络权值变化量wc
void clearbc(int m,int n);//清除网络阀值变化量bc
void initialw(void);//初始化NN网络权值W
void initialb(void); //初始化NN网络阀值
void calculdiffer(void);//计算NN网络单个样本误差
void calculis(void);//计算NN网络全体样本误差
void trainNN(void);//训练NN网络
/*计算NN网络隐含层和输出层的输出 */
void netout(int m,int n)
{
int i,j,k;
//隐含层各节点的的输出
for (j=1,i=2;j<=m;j++) //m为隐含层节点个数
{
netin[i][j]=0.0;
for(k=1;k<=3;k++)//隐含层的每个节点均有三个输入变量
netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j];
netin[i][j]=netin[i][j]-b[i][j];
o[i][j]=A/(1+exp(-netin[i][j]/B));
}
//输出层各节点的输出
for (j=1,i=3;j<=n;j++)
{
netin[i][j]=0.0;
for (k=1;k<=m;k++)
netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j];
netin[i][j]=netin[i][j]-b[i][j];
o[i][j]=A/(1+exp(-netin[i][j]/B)) ;
}
}
/*计算NN网络的反向传播误差*/
void calculd(int m,int n)
{
int i,j,k;
double t;
a=count-1;
d[3][1]=(o[3][1]-sample[a][3])*(A/B)*exp(-netin[3][1]/B)/pow(1+exp(-netin[3][1]/B),2);
//隐含层的误差
for (j=1,i=2;j<=m;j++)
{
t=0.00;
for (k=1;k<=n;k++)
t=t+w[i+1][j][k]*d[i+1][k];
d[i][j]=t*(A/B)*exp(-netin[i][j]/B)/pow(1+exp(-netin[i][j]/B),2);
}
}
/*计算网络权值W的调整量*/
void calculwc(int m,int n)
{
int i,j,k;
// 输出层(第三层)与隐含层(第二层)之间的连接权值的调整
for (i=1,k=3;i<=m;i++)
{
for (j=1;j<=n;j++)
{
wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j];
}
// printf("\n");
}
//隐含层与输入层之间的连接权值的调整
for (i=1,k=2;i<=m;i++)
{
for (j=1;j<=m;j++)
{
wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j];
}
// printf("\n");
}
}
/*计算网络阀值的调整量*/
void calculbc(int m,int n)
{
int j;
for (j=1;j<=m;j++)
{
bc[2][j]=BCOEF*d[2][j];
}
for (j=1;j<=n;j++)
{
bc[3][j]=BCOEF*d[3][j];
}
}
/*调整网络权值*/
void changw(int m,int n)
{
int i,j;
for (i=1;i<=3;i++)
for (j=1;j<=m;j++)
{
w[2][i][j]=0.9*w[2][i][j]+wc[2][i][j];
//为了保证系统有较好的鲁棒性,计算权值时乘惯性系数0.9
printf("w[2][%d][%d]=%f\n",i,j,w[2][i][j]);
}
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
{
w[3][i][j]=0.9*w[3][i][j]+wc[3][i][j];
printf("w[3][%d][%d]=%f\n",i,j,w[3][i][j]);
}
}
/*调整网络阀值*/
void changb(int m,int n)
{
int j;
for (j=1;j<=m;j++)
b[2][j]=b[2][j]+bc[2][j];
for (j=1;j<=n;j++)
b[3][j]=b[3][j]+bc[3][j];
}
/*清除网络权值变化量wc*/
void clearwc(void)
{
for (int i=0;i<4;i++)
for (int j=0;j<10;j++)
for (int k=0;k<10;k++)
wc[i][j][k]=0.00;
}
/*清除网络阀值变化量*/
void clearbc(void)
{
for (int i=0;i<4;i++)
for (int j=0;j<10;j++)
bc[i][j]=0.00;
}
/*初始化网络权值W*/
void initialw(void)
{
int i,j,k,x;
double weight;
for (i=0;i<4;i++)
for (j=0;j<10;j++)
for (k=0;k<10;k++)
{
randomize();
x=100+random(400);
weight=(double)x/5000.00;
w[i][j][k]=weight;
}
}
/*初始化网络阀值*/
void initialb(void)
{
int i,j,x;
double fa;
for (i=0;i<4;i++)
for (j=0;j<10;j++)
{
randomize();
for (int k=0;k<12;k++)
{
x=100+random(400);
}
fa=(double)x/50000.00;
b[i][j]=fa;
}
}
/*计算网络单个样本误差*/
void calculdiffer(void)
{
a=count-1;
differ=0.5*(o[3][1]-sample[a][3])*(o[3][1]-sample[a][3]);
}
void calculis(void)
{
int i;
is=0.0;
for (i=0;i<=19;i++)
{
o[1][1]=sample[i][0];
o[1][2]=sample[i][1];
o[1][3]=sample[i][2];
netout(8,1);
is=is+(o[3][1]-sample[i][3])*(o[3][1]-sample[i][3]);
}
is=is/20;
}
/*训练网络*/
void trainNN(void)
{
long int time;
int i,x[4];
initialw();
initialb();
for (time=1;time<=MAX;time++)
{
count=0;
while(count<=40)
{
o[1][1]=sample[count][0];
o[1][2]=sample[count][1];
o[1][3]=sample[count][2];
count=count+1;
clearwc();
clearbc();
netout(8,1);
calculdiffer();
while(differ>ERROR)
{
calculd(8,1);
calculwc(8,1);
calculbc(8,1);
changw(8,1);
changb(8,1);
netout(8,1);
calculdiffer();
}
}
printf("This is %d times training NN...\n",time);
calculis();
printf("is==%f\n",is);
if (is<ACCURACY) break;
}
}
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
double result;
int m,test[4];
char ch='y';
cout<<"Please wait for the train of NN:"<<endl;
trainNN();
cout<<"Now,this molar network can work for you."<<endl;
while(ch=='y' || ch=='Y')
{
cout<<"Please input data to be tested."<<endl;
for (m=1;m<=3;m++)
cin>>test[m];
ch=getchar();
o[1][1]=test[1];
o[1][2]=test[2];
o[1][3]=test[3];
netout(8,1);
result=o[3][1];
printf("Final result is %f.\n",result);
printf("Still test?[Yes] or [No]\n");
ch=getchar();
}
return 0;
}

Ⅷ 求BP神经网络算法的C++源代码

// AnnBP.cpp: implementation of the CAnnBP class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "AnnBP.h"
#include "math.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAnnBP::CAnnBP()
{
eta1=0.3;
momentum1=0.3;

}

CAnnBP::~CAnnBP()
{

}

double CAnnBP::drnd()
{
return ((double) rand() / (double) BIGRND);
}

/*** 返回-1.0到1.0之间的双精度随机数 ***/
double CAnnBP::dpn1()
{
return (double) (rand())/(32767/2)-1;
}

/*** 作用函数,目前是S型函数 ***/
double CAnnBP::squash(double x)
{
return (1.0 / (1.0 + exp(-x)));
}

/*** 申请1维双精度实数数组 ***/
double* CAnnBP::alloc_1d_dbl(int n)
{
double *new1;

new1 = (double *) malloc ((unsigned) (n * sizeof (double)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_1D_DBL: Couldn't allocate array of doubles\n");
return (NULL);
}
return (new1);
}

/*** 申请2维双精度实数数组 ***/
double** CAnnBP::alloc_2d_dbl(int m, int n)
{
int i;
double **new1;

new1 = (double **) malloc ((unsigned) (m * sizeof (double *)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_2D_DBL: Couldn't allocate array of dbl ptrs\n");
return (NULL);
}

for (i = 0; i < m; i++) {
new1[i] = alloc_1d_dbl(n);
}

return (new1);
}

/*** 随机初始化权值 ***/
void CAnnBP::bpnn_randomize_weights(double **w, int m, int n)
{
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = dpn1();
}
}

}

/*** 0初始化权值 ***/
void CAnnBP::bpnn_zero_weights(double **w, int m, int n)
{
int i, j;

for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = 0.0;
}
}

}

/*** 设置随机数种子 ***/
void CAnnBP::bpnn_initialize(int seed)
{
CString msg,s;
msg="Random number generator seed:";
s.Format("%d",seed);
AfxMessageBox(msg+s);
srand(seed);
}

/*** 创建BP网络 ***/
BPNN* CAnnBP::bpnn_internal_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = (BPNN *) malloc (sizeof (BPNN));
if (newnet == NULL) {
printf("BPNN_CREATE: Couldn't allocate neural network\n");
return (NULL);
}

newnet->input_n = n_in;
newnet->hidden_n = n_hidden;
newnet->output_n = n_out;
newnet->input_units = alloc_1d_dbl(n_in + 1);
newnet->hidden_units = alloc_1d_dbl(n_hidden + 1);
newnet->output_units = alloc_1d_dbl(n_out + 1);

newnet->hidden_delta = alloc_1d_dbl(n_hidden + 1);
newnet->output_delta = alloc_1d_dbl(n_out + 1);
newnet->target = alloc_1d_dbl(n_out + 1);

newnet->input_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

newnet->input_prev_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_prev_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

return (newnet);

}

/* 释放BP网络所占地内存空间 */
void CAnnBP::bpnn_free(BPNN *net)
{
int n1, n2, i;

n1 = net->input_n;
n2 = net->hidden_n;

free((char *) net->input_units);
free((char *) net->hidden_units);
free((char *) net->output_units);

free((char *) net->hidden_delta);
free((char *) net->output_delta);
free((char *) net->target);

for (i = 0; i <= n1; i++) {
free((char *) net->input_weights[i]);
free((char *) net->input_prev_weights[i]);
}
free((char *) net->input_weights);
free((char *) net->input_prev_weights);

for (i = 0; i <= n2; i++) {
free((char *) net->hidden_weights[i]);
free((char *) net->hidden_prev_weights[i]);
}
free((char *) net->hidden_weights);
free((char *) net->hidden_prev_weights);

free((char *) net);
}

/*** 创建一个BP网络,并初始化权值***/
BPNN* CAnnBP::bpnn_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = bpnn_internal_create(n_in, n_hidden, n_out);

#ifdef INITZERO
bpnn_zero_weights(newnet->input_weights, n_in, n_hidden);
#else
bpnn_randomize_weights(newnet->input_weights, n_in, n_hidden);
#endif
bpnn_randomize_weights(newnet->hidden_weights, n_hidden, n_out);
bpnn_zero_weights(newnet->input_prev_weights, n_in, n_hidden);
bpnn_zero_weights(newnet->hidden_prev_weights, n_hidden, n_out);

return (newnet);

}

void CAnnBP::bpnn_layerforward(double *l1, double *l2, double **conn, int n1, int n2)
{
double sum;
int j, k;

/*** 设置阈值 ***/
l1[0] = 1.0;

/*** 对于第二层的每个神经元 ***/
for (j = 1; j <= n2; j++) {

/*** 计算输入的加权总和 ***/
sum = 0.0;
for (k = 0; k <= n1; k++) {
sum += conn[k][j] * l1[k];
}
l2[j] = squash(sum);
}
}

/* 输出误差 */
void CAnnBP::bpnn_output_error(double *delta, double *target, double *output, int nj, double *err)
{
int j;
double o, t, errsum;

errsum = 0.0;
for (j = 1; j <= nj; j++) {
o = output[j];
t = target[j];
delta[j] = o * (1.0 - o) * (t - o);
errsum += ABS(delta[j]);
}
*err = errsum;

}

/* 隐含层误差 */
void CAnnBP::bpnn_hidden_error(double *delta_h, int nh, double *delta_o, int no, double **who, double *hidden, double *err)
{
int j, k;
double h, sum, errsum;

errsum = 0.0;
for (j = 1; j <= nh; j++) {
h = hidden[j];
sum = 0.0;
for (k = 1; k <= no; k++) {
sum += delta_o[k] * who[j][k];
}
delta_h[j] = h * (1.0 - h) * sum;
errsum += ABS(delta_h[j]);
}
*err = errsum;
}

/* 调整权值 */
void CAnnBP::bpnn_adjust_weights(double *delta, int ndelta, double *ly, int nly, double **w, double **oldw, double eta, double momentum)
{
double new_dw;
int k, j;

ly[0] = 1.0;
for (j = 1; j <= ndelta; j++) {
for (k = 0; k <= nly; k++) {
new_dw = ((eta * delta[j] * ly[k]) + (momentum * oldw[k][j]));
w[k][j] += new_dw;
oldw[k][j] = new_dw;
}
}

}

/* 进行前向运算 */
void CAnnBP::bpnn_feedforward(BPNN *net)
{
int in, hid, out;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** Feed forward input activations. ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

}

/* 训练BP网络 */
void CAnnBP::bpnn_train(BPNN *net, double eta, double momentum, double *eo, double *eh)
{
int in, hid, out;
double out_err, hid_err;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** 前向输入激活 ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

/*** 计算隐含层和输出层误差 ***/
bpnn_output_error(net->output_delta, net->target, net->output_units,
out, &out_err);
bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out,
net->hidden_weights, net->hidden_units, &hid_err);
*eo = out_err;
*eh = hid_err;

/*** 调整输入层和隐含层权值 ***/
bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid,
net->hidden_weights, net->hidden_prev_weights, eta, momentum);
bpnn_adjust_weights(net->hidden_delta, hid, net->input_units, in,
net->input_weights, net->input_prev_weights, eta, momentum);
}

/* 保存BP网络 */
void CAnnBP::bpnn_save(BPNN *net, char *filename)
{
CFile file;
char *mem;
int n1, n2, n3, i, j, memcnt;
double dvalue, **w;
n1 = net->input_n; n2 = net->hidden_n; n3 = net->output_n;
printf("Saving %dx%dx%d network to '%s'\n", n1, n2, n3, filename);
try
{
file.Open(filename,CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

file.Write(&n1,sizeof(int));
file.Write(&n2,sizeof(int));
file.Write(&n3,sizeof(int));

memcnt = 0;
w = net->input_weights;
mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(double)));
// mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
dvalue = w[i][j];
//fast(&mem[memcnt], &dvalue, sizeof(double));
fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);

}
}

file.Write(mem,sizeof(double)*(n1+1)*(n2+1));
free(mem);

memcnt = 0;
w = net->hidden_weights;
mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(double)));
// mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));
for (i = 0; i <= n2; i++) {
for (j = 0; j <= n3; j++) {
dvalue = w[i][j];
fast(&mem[memcnt], &dvalue, sizeof(double));
// fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);
}
}

file.Write(mem, (n2+1) * (n3+1) * sizeof(double));
// free(mem);

file.Close();
return;
}

/* 从文件中读取BP网络 */
BPNN* CAnnBP::bpnn_read(char *filename)
{
char *mem;
BPNN *new1;
int n1, n2, n3, i, j, memcnt;
CFile file;

try
{
file.Open(filename,CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

// printf("Reading '%s'\n", filename);// fflush(stdout);

file.Read(&n1, sizeof(int));
file.Read(&n2, sizeof(int));
file.Read(&n3, sizeof(int));

new1 = bpnn_internal_create(n1, n2, n3);

// printf("'%s' contains a %dx%dx%d network\n", filename, n1, n2, n3);
// printf("Reading input weights..."); // fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));

file.Read(mem, ((n1+1)*(n2+1))*sizeof(double));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
//fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);
}
}
free(mem);

// printf("Done\nReading hidden weights..."); //fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));

file.Read(mem, (n2+1) * (n3+1) * sizeof(double));
for (i = 0; i <= n2; i++) {

for (j = 0; j <= n3; j++) {
//fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);

}
}
free(mem);
file.Close();

printf("Done\n"); //fflush(stdout);

bpnn_zero_weights(new1->input_prev_weights, n1, n2);
bpnn_zero_weights(new1->hidden_prev_weights, n2, n3);

return (new1);
}

void CAnnBP::CreateBP(int n_in, int n_hidden, int n_out)
{
net=bpnn_create(n_in,n_hidden,n_out);
}

void CAnnBP::FreeBP()
{
bpnn_free(net);

}

void CAnnBP::Train(double *input_unit,int input_num, double *target,int target_num, double *eo, double *eh)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}

for(int j=1;j<=target_num;j++)
{
net->target[j]=target[j-1];
}
bpnn_train(net,eta1,momentum1,eo,eh);

}

void CAnnBP::Identify(double *input_unit,int input_num,double *target,int target_num)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}
bpnn_feedforward(net);
for(int j=1;j<=target_num;j++)
{
target[j-1]=net->output_units[j];
}
}

void CAnnBP::Save(char *filename)
{
bpnn_save(net,filename);

}

void CAnnBP::Read(char *filename)
{
net=bpnn_read(filename);
}

void CAnnBP::SetBParm(double eta, double momentum)
{
eta1=eta;
momentum1=momentum;

}

void CAnnBP::Initialize(int seed)
{
bpnn_initialize(seed);

}

Ⅸ 怎样用python构建一个卷积神经网络模型

上周末利用python简单实现了一个卷积神经网络,只包含一个卷积层和一个maxpooling层,pooling层后面的多层神经网络采用了softmax形式的输出。实验输入仍然采用MNIST图像使用10个feature map时,卷积和pooling的结果分别如下所示。


部分源码如下:

[python]view plain

  • #coding=utf-8

  • '''''

  • Createdon2014年11月30日

  • @author:Wangliaofan

  • '''

  • importnumpy

  • importstruct

  • importmatplotlib.pyplotasplt

  • importmath

  • importrandom

  • import

  • #test

  • defsigmoid(inX):

  • if1.0+numpy.exp(-inX)==0.0:

  • return999999999.999999999

  • return1.0/(1.0+numpy.exp(-inX))

  • defdifsigmoid(inX):

  • returnsigmoid(inX)*(1.0-sigmoid(inX))

  • deftangenth(inX):

  • return(1.0*math.exp(inX)-1.0*math.exp(-inX))/(1.0*math.exp(inX)+1.0*math.exp(-inX))

  • defcnn_conv(in_image,filter_map,B,type_func='sigmoid'):

  • #in_image[num,featuremap,row,col]=>in_image[Irow,Icol]

  • #featuresmap[kfilter,row,col]

  • #type_func['sigmoid','tangenth']

  • #out_feature[kfilter,Irow-row+1,Icol-col+1]

  • shape_image=numpy.shape(in_image)#[row,col]

  • #print"shape_image",shape_image

  • shape_filter=numpy.shape(filter_map)#[kfilter,row,col]

  • ifshape_filter[1]>shape_image[0]orshape_filter[2]>shape_image[1]:

  • raiseException

  • shape_out=(shape_filter[0],shape_image[0]-shape_filter[1]+1,shape_image[1]-shape_filter[2]+1)

  • out_feature=numpy.zeros(shape_out)

  • k,m,n=numpy.shape(out_feature)

  • fork_idxinrange(0,k):

  • #rotate180tocalculateconv

  • c_filter=numpy.rot90(filter_map[k_idx,:,:],2)

  • forr_idxinrange(0,m):

  • forc_idxinrange(0,n):

  • #conv_temp=numpy.zeros((shape_filter[1],shape_filter[2]))

  • conv_temp=numpy.dot(in_image[r_idx:r_idx+shape_filter[1],c_idx:c_idx+shape_filter[2]],c_filter)

  • sum_temp=numpy.sum(conv_temp)

  • iftype_func=='sigmoid':

  • out_feature[k_idx,r_idx,c_idx]=sigmoid(sum_temp+B[k_idx])

  • eliftype_func=='tangenth':

  • out_feature[k_idx,r_idx,c_idx]=tangenth(sum_temp+B[k_idx])

  • else:

  • raiseException

  • returnout_feature

  • defcnn_maxpooling(out_feature,pooling_size=2,type_pooling="max"):

  • k,row,col=numpy.shape(out_feature)

  • max_index_Matirx=numpy.zeros((k,row,col))

  • out_row=int(numpy.floor(row/pooling_size))

  • out_col=int(numpy.floor(col/pooling_size))

  • out_pooling=numpy.zeros((k,out_row,out_col))

  • fork_idxinrange(0,k):

  • forr_idxinrange(0,out_row):

  • forc_idxinrange(0,out_col):

  • temp_matrix=out_feature[k_idx,pooling_size*r_idx:pooling_size*r_idx+pooling_size,pooling_size*c_idx:pooling_size*c_idx+pooling_size]

  • out_pooling[k_idx,r_idx,c_idx]=numpy.amax(temp_matrix)

  • max_index=numpy.argmax(temp_matrix)

  • #printmax_index

  • #printmax_index/pooling_size,max_index%pooling_size

  • max_index_Matirx[k_idx,pooling_size*r_idx+max_index/pooling_size,pooling_size*c_idx+max_index%pooling_size]=1

  • returnout_pooling,max_index_Matirx

  • defpoolwithfunc(in_pooling,W,B,type_func='sigmoid'):

  • k,row,col=numpy.shape(in_pooling)

  • out_pooling=numpy.zeros((k,row,col))

  • fork_idxinrange(0,k):

  • forr_idxinrange(0,row):

  • forc_idxinrange(0,col):

  • out_pooling[k_idx,r_idx,c_idx]=sigmoid(W[k_idx]*in_pooling[k_idx,r_idx,c_idx]+B[k_idx])

  • returnout_pooling

  • #out_featureistheoutputofconv

  • defbackErrorfromPoolToConv(theta,max_index_Matirx,out_feature,pooling_size=2):

  • k1,row,col=numpy.shape(out_feature)

  • error_conv=numpy.zeros((k1,row,col))

  • k2,theta_row,theta_col=numpy.shape(theta)

  • ifk1!=k2:

  • raiseException

  • foridx_kinrange(0,k1):

  • foridx_rowinrange(0,row):

  • foridx_colinrange(0,col):

  • error_conv[idx_k,idx_row,idx_col]=

  • max_index_Matirx[idx_k,idx_row,idx_col]*

  • float(theta[idx_k,idx_row/pooling_size,idx_col/pooling_size])*

  • difsigmoid(out_feature[idx_k,idx_row,idx_col])

  • returnerror_conv

  • defbackErrorfromConvToInput(theta,inputImage):

  • k1,row,col=numpy.shape(theta)

  • #print"theta",k1,row,col

  • i_row,i_col=numpy.shape(inputImage)

  • ifrow>i_roworcol>i_col:

  • raiseException

  • filter_row=i_row-row+1

  • filter_col=i_col-col+1

  • detaW=numpy.zeros((k1,filter_row,filter_col))

  • #thesamewithconvvalidinmatlab

  • fork_idxinrange(0,k1):

  • foridx_rowinrange(0,filter_row):

  • foridx_colinrange(0,filter_col):

  • subInputMatrix=inputImage[idx_row:idx_row+row,idx_col:idx_col+col]

  • #print"subInputMatrix",numpy.shape(subInputMatrix)

  • #rotatetheta180

  • #printnumpy.shape(theta)

  • theta_rotate=numpy.rot90(theta[k_idx,:,:],2)

  • #print"theta_rotate",theta_rotate

  • dotMatrix=numpy.dot(subInputMatrix,theta_rotate)

  • detaW[k_idx,idx_row,idx_col]=numpy.sum(dotMatrix)

  • detaB=numpy.zeros((k1,1))

  • fork_idxinrange(0,k1):

  • detaB[k_idx]=numpy.sum(theta[k_idx,:,:])

  • returndetaW,detaB

  • defloadMNISTimage(absFilePathandName,datanum=60000):

  • images=open(absFilePathandName,'rb')

  • buf=images.read()

  • index=0

  • magic,numImages,numRows,numColumns=struct.unpack_from('>IIII',buf,index)

  • printmagic,numImages,numRows,numColumns

  • index+=struct.calcsize('>IIII')

  • ifmagic!=2051:

  • raiseException

  • datasize=int(784*datanum)

  • datablock=">"+str(datasize)+"B"

  • #nextmatrix=struct.unpack_from('>47040000B',buf,index)

  • nextmatrix=struct.unpack_from(datablock,buf,index)

  • nextmatrix=numpy.array(nextmatrix)/255.0

  • #nextmatrix=nextmatrix.reshape(numImages,numRows,numColumns)

  • #nextmatrix=nextmatrix.reshape(datanum,1,numRows*numColumns)

  • nextmatrix=nextmatrix.reshape(datanum,1,numRows,numColumns)

  • returnnextmatrix,numImages

  • defloadMNISTlabels(absFilePathandName,datanum=60000):

  • labels=open(absFilePathandName,'rb')

  • buf=labels.read()

  • index=0

  • magic,numLabels=struct.unpack_from('>II',buf,index)

  • printmagic,numLabels

  • index+=struct.calcsize('>II')

  • ifmagic!=2049:

  • raiseException

  • datablock=">"+str(datanum)+"B"

  • #nextmatrix=struct.unpack_from('>60000B',buf,index)

  • nextmatrix=struct.unpack_from(datablock,buf,index)

  • nextmatrix=numpy.array(nextmatrix)

  • returnnextmatrix,numLabels

  • defsimpleCNN(numofFilter,filter_size,pooling_size=2,maxIter=1000,imageNum=500):

  • decayRate=0.01

  • MNISTimage,num1=loadMNISTimage("F:\train-images-idx3-ubyte",imageNum)

  • printnum1

  • row,col=numpy.shape(MNISTimage[0,0,:,:])

  • out_Di=numofFilter*((row-filter_size+1)/pooling_size)*((col-filter_size+1)/pooling_size)

  • MLP=BMNN2.MuiltilayerANN(1,[128],out_Di,10,maxIter)

  • MLP.setTrainDataNum(imageNum)

  • MLP.loadtrainlabel("F:\train-labels-idx1-ubyte")

  • MLP.initialweights()

  • #MLP.printWeightMatrix()

  • rng=numpy.random.RandomState(23455)

  • W_shp=(numofFilter,filter_size,filter_size)

  • W_bound=numpy.sqrt(numofFilter*filter_size*filter_size)

  • W_k=rng.uniform(low=-1.0/W_bound,high=1.0/W_bound,size=W_shp)

  • B_shp=(numofFilter,)

  • B=numpy.asarray(rng.uniform(low=-.5,high=.5,size=B_shp))

  • cIter=0

  • whilecIter<maxIter:

  • cIter+=1

  • ImageNum=random.randint(0,imageNum-1)

  • conv_out_map=cnn_conv(MNISTimage[ImageNum,0,:,:],W_k,B,"sigmoid")

  • out_pooling,max_index_Matrix=cnn_maxpooling(conv_out_map,2,"max")

  • pool_shape=numpy.shape(out_pooling)

  • MLP_input=out_pooling.reshape(1,1,out_Di)

  • #printnumpy.shape(MLP_input)

  • DetaW,DetaB,temperror=MLP.backwardPropogation(MLP_input,ImageNum)

  • ifcIter%50==0:

  • printcIter,"Temperror:",temperror

  • #printnumpy.shape(MLP.Theta[MLP.Nl-2])

  • #printnumpy.shape(MLP.Ztemp[0])

  • #printnumpy.shape(MLP.weightMatrix[0])

  • theta_pool=MLP.Theta[MLP.Nl-2]*MLP.weightMatrix[0].transpose()

  • #printnumpy.shape(theta_pool)

  • #print"theta_pool",theta_pool

  • temp=numpy.zeros((1,1,out_Di))

  • temp[0,:,:]=theta_pool

  • back_theta_pool=temp.reshape(pool_shape)

  • #print"back_theta_pool",numpy.shape(back_theta_pool)

  • #print"back_theta_pool",back_theta_pool

  • error_conv=backErrorfromPoolToConv(back_theta_pool,max_index_Matrix,conv_out_map,2)

  • #print"error_conv",numpy.shape(error_conv)

  • #printerror_conv

  • conv_DetaW,conv_DetaB=backErrorfromConvToInput(error_conv,MNISTimage[ImageNum,0,:,:])

  • #print"W_k",W_k

  • #print"conv_DetaW",conv_DetaW

热点内容
为什么安卓打吃鸡感觉有延迟 发布:2025-07-08 02:09:32 浏览:167
课题组服务器如何使用 发布:2025-07-08 02:09:27 浏览:43
sql语句长度限制 发布:2025-07-08 02:07:04 浏览:769
完美跑图服务器是什么 发布:2025-07-08 01:52:31 浏览:668
连王者荣耀服务器失败是什么原因 发布:2025-07-08 01:48:34 浏览:594
安卓设置横向竖向在哪里 发布:2025-07-08 01:45:33 浏览:882
安卓的语音助手叫什么 发布:2025-07-08 01:45:22 浏览:639
如何运行android项目 发布:2025-07-08 01:44:47 浏览:264
脚本韩信 发布:2025-07-08 01:30:59 浏览:949
lua脚本linux 发布:2025-07-08 01:20:57 浏览:220