📄 小波程序.txt
字号:
actualWithPredicted(:, 2) = predicted(1: pointsAhead);
plot(actualWithPredicted);
graph = ['Mean Absolute Error = ', num2str(mean(AbsError))];
title(graph);
legend('Actual', 'Forecasted');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%File name : nretrain.m
%Description : This file loads the existing NNs and trains them again.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
%Prompt for the starting point for training
disp('Program will now RETRAIN the Neural Networks ')
disp('with the SAME intial data series again...');
disp(' ');
disp('To capture the pattern of the signal, the model is ')
disp('set to accept dataPoints x 2 sets of training examples; ');
disp('1 set of demand + 1 sets of price. ');
disp(' ');
disp('The normalised demand data <point 2>, is to be taken as the ')
disp('output value for the first iteration of training examples. ');
disp(' ');
msg = ['Data points to be used for reTraining the NNs is from 1 to ',...
num2str(dataPoints)];
disp(msg);
disp(' ');
disp('Press ENTER key to continue...');
pause;
%Clear command screen
clc;
%Prompt for no. of training cycles
%For current program, 1 cycle = user set no. of iterations (ie: dataPoints)
cycle = input('Input number of cycles to retrain the NNs: ');
numOfTimes = resolution + 1;
%Loading existing NNs for training
for x = 1: numOfTimes
%Re-initialising variables
clear targetDemand;
clear inputs;
clear output;
clc;
%Loading NN for the respective demand and temperature coefficient signals
filename = ['nn', num2str(x)];
clear nn
load(filename);
%Getting the size of NN
numOfInputs = nn.nin;
numOfHiddens = nn.nhidden;
numOfOutput = 1;
%Setting length of reTraining examples and target outputs
reTrainLength = dataPoints;
targetLength = reTrainLength;
targetStartAt = 2;
%Set target outputs of the training examples
if(x == 1)
targetDemand = normDemand(targetStartAt: 1 + targetLength);
else
targetDemand = normDemandDetail(x - 1, targetStartAt: 1 + targetLength);
end
%Preparing training examples
%Setting training i/ps to be 2 with user set no. of iterations (dataPoints)
y = 0;
while y < reTrainLength
if(x == 1)
inputs(1, y + 1) = normDemand(y + 1);
inputs(2, y + 1) = normPrice(y + 1);
else
inputs(1, y + 1) = normDemandDetail(x - 1, y + 1);
inputs(2, y + 1) = normPriceDetail(x - 1, y + 1);
end
output(y + 1, :) = targetDemand(y + 1);
y = y + 1;
end
inputs = (inputs');
%Setting no. of training cycles
[ni, np] = size(targetDemand); % <== [ni, np] tells the NN how long is 1 cycle;
size(targetDemand) %With reference to line 106
%NN options
options = zeros(1, 18);
options(1) = 1; %Provides display of error values
options(14) = cycle * ni * np;
%Training the neural network
%netopt(net, options, x, t, alg);
nn = netopt(nn, options, inputs, output, 'scg');
%Save the neural network
filename = ['nn', num2str(x)];
save(filename, 'nn');
disp(' ');
msg = ['Neural network => ', filename, ' <= successfully RETRAINED and saved!! '];
disp(msg);
if(x < 3)
disp(' ');
disp('Press ENTER key to continue training the next NN...');
else
disp(' ');
disp('Model is now ready to forecast again!!');
disp(' ');
disp('Press ENTER key to continue...');
end
pause;
end
小波神经网络程序
gaoli 发表于 2007-8-2 16:37:00
clc
clear
%step 1=========================
%定义输入样本;
t=0:0.01:1.5;
x=-sin(2*pi*t);
targ=[0 0 1 1 0 0 ];
eta=0.02;aerfa=0.935;
%初始化连接权wjh(输出层和隐层的连接权);whi(隐层和输出层的连接权);
%假设小波函数节点数为:H个;样本数为P;
%输出节点数为:J个;输入节点数为:I个;
H=15;P=2;
I=length(t);
J=length(targ);
%初始化小波参数
b=rand(H,1);
a=rand(H,1);
%初始化权系数;
whi=rand(I,H);
wjh=rand(H,J);
%阈值初始化;
b1=rand(H,1);
b2=rand(J,1);
p=0;
%保存的误差;
Err_NetOut=[];
flag=1;count=0;
while flag>0
flag=0;
count=count+1;
%step 2=================================
xhp1=0;
for h=1:H
for i=1:I
xhp1=xhp1+whi(i,h)*x(i);
end
ixhp(h)=xhp1+b1(h);
xhp1=0;
end
for h=1:H
oxhp(h)=fai((ixhp(h)-b(h))/a(h));
end
%step 3====================================
ixjp1=0;
for j=1:J
for h=1:H
ixjp1=ixjp1+wjh(h,j)*oxhp(h);
end
ixjp(j)=ixjp1+b2(j);
ixjp1=0;
end
for i=1:J
oxjp(i)=fnn(ixjp(i));
end
%step 6==保存每次误差=====
wuchayy=1/2*sumsqr(oxjp-targ);
%E_x=1/2*sumsqr(x);
Err_NetOut=[Err_NetOut wuchayy];%保存每次的误差;
%Err_rate=Err_NetOut/E_x;
%Err_rate
%oxjp
%求detaj ,detab2==================================
for j=1:J
detaj(j)=-(oxjp(j)-targ(j))*oxjp(j)*(1-oxjp(j));
end
for j=1:J
for h=1:H
detawjh(h,j)=eta*detaj(j)*oxhp(h);
end
end
detab2=eta*detaj;
%求detah, detawhi detab1 detab detaa;========================
sum=0;
for h=1:H
for j=1:J
sum=detaj(j)*wjh(h,j)*diffai((ixhp(h)-b(h))/a(h))/a(h)+sum;
end
detah(h)=sum;
sum=0;
end
for h=1:H
for i=1:I
detawhi(i,h)=eta*detah(h)*x(i);
end
end
detab1=eta*detah;
detab=-eta*detah;
for h=1:H
detaa(h)=-eta*detah(h)*((ixhp(h)-b(h))/a(h));
end
%引入动量因子aerfa,修正各个系数==========================================
wjh=wjh+(1+aerfa)*detawjh;
whi=whi+(1+aerfa)*detawhi;
a=a+(1+aerfa)*detaa';
b=b+(1+aerfa)*detab';
b1=b1+(1+aerfa)*detab1';
b2=b2+(1+aerfa)*detab2';
%======================================================
%引入修正算法!!
%判断所有的样本是否计算完==================================
p=p+1;
if p~=P
flag=flag+1;
else
if Err_NetOut(end)>0.05
flag=flag+1;
else
figure;
plot(Err_NetOut);
title('误差曲线');
disp('目标达到');
%disp(oxjp);
end
end
if count>2000
figure;
plot(Err_NetOut);
title('误差曲线');
disp('目标未达到');
disp(oxjp);
break;
end
end
程序代码 diffai.m
function y3=diffai(x);
y3=-0.75*sin(1.75*x)*exp(-x.^2/2)-cos(1.75*x)*exp(-x.^2/2)*x;
程序代码 fai.m
function yl=fai(x)
yl=cos(0.75.*x)+exp(-x.^2/2);
程序代码 fnn.m
function y2=fnn(x)
y2=1/(1+exp(-x));
用小波函数构建神经网络的源程序
gaoli 发表于 2007-8-2 16:42:00
该程序是用小波函数构建神经网络的源程序。用以分析心电信号、脑电信号等等。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
load data
M1=20;
epo=15;
A=4;
B=18;
B2=B/2+1
N=500;
M=(A+1)*(B+1);
for a0=1:A+1;
for b0=1:B+1;
i=(B+1)*(a0-1)+b0;
b_init(i)=((b0-B2)/10)/(2^(-A)); a_init(i)=1/(2^(-A));
c_init(i)=(20-A)/2;
end
end
w0=ones(1,M);
for i=1:N
for j=1:M
t=x(i);
t= a_init(j)*t-b_init(j);
%P0(i,j)= (cos(1.75*t)*exp(-t*t/2))/2^c_init(j);
P0(i,j)= ((1-t*t)*exp(-t*t/2))/2^c_init(j);
end
end
%calculation of output of network
for i=1:N
u=0;
for j=1:M
u=u+w0(j)*P0(i,j);%w0?aè¨?μ
end
y0(i)= u;% y(p)= u=??W(j)*phi(p,j)= ??W(j)* |μj(t)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:M
W(:,k)=P0(:,k);
end
for k=2:M
u=0;
for i=1:k-1
aik(i)=(P0(:,k)'*W(:,i))/(W(:,i)'*W(:,i));
u=u+aik(i) *W(:,i);
end
W(:,k)=P0(:,k)-u;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:M
g(i)= (W(:,i)'*d')/( W(:,i)'* W(:,i));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u=0;
for i=1:M
u=u+g(i)*(W(:,i)'*W(:,i));
end
DD=u;
for i=1:M
Erro(i)=(g(i)^2)*(W(:,i)'*W(:,i))/DD;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
k=1;
while(k<=M1)
u=1;
for i=2:M
if abs(Erro(u))<abs(Erro(i));
u=i
else u=u
end
end
I(k)=u;
Erro(u)=0
k=k+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:M1
u=I(k);
a(k)=a_init(u);
b(k)=b_init(u);
c(k)=c_init(u);
w1(k)=w0(u);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
epoch=1;
error=0.1;
err=0.0001;
lin=0.5;
while (error>=err & epoch<=epo)
for i=1:N
for j=1:M1
t=x(i);
t= a(j)*t-b(j);
%P1(i,j)= (cos(1.75*t)*exp(-t*t/2))/2^c(j);
P1(i,j)= ((1-t*t)*exp(-t*t/2))/2^c(j);
end
end
%calculation of output of network
for i=1:N
u=0;
for j=1:M1
u=u+w1(j)*P1(i,j);
end
y1(i)= u;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u=0;
for i=1:N
u=u+(d(i)-y1(i))^2;
end
u=u/2;%u=1/2??(d-p)^2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if u>error
lin=lin*0.8;
else
lin=lin*1.2;
end
error=u; %error=u=1/2??(d-p)^2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:M1
u=0;
for i=1:N
u=u+(d(i)-y1(i))*P1(i,j);
end
EW(j)=-u;
end
if epoch==1
SW=-EW;
w1_=w1;
else
SW=-EW+((EW*EW')*SW_)/(EW_*EW_');
end
EW_=EW;
SW_=SW;
w1=w1_+SW*lin;
w1_=w1;
%number of epoch increase by 1
epoch=epoch+1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(2,1,1);plot(x,d);
subplot(2,1,2);plot(x,y1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -