ESN,MATLAB中错误使用 vertcat要串联的数组的维度不一致,程序如下

% load data
data = load('MackeyGlass_t17.txt');
initLen = 100;%前100项用来初始化储备池
trainLen = 2000;
testLen = 2000;

% plot MackeyGlass: 1:1000
figure(10);
plot(data(1:1000));
title('A sample of data');

% generate ESN
inSize = 1;
outSize = 1;
resSize = 1000;
a = 0.93;
rand( 'seed' ,40);%把随机数保存了一样,下次用的时候再拿出来。
Win = (rand(resSize,inSize+1)-0.5) .* 1;
W = rand(resSize,resSize)-0.5;

%normalizing and setting spectral radius
disp 'Computing spectral radius'
opt.disp = 0;
rhoW = abs(eigs(W,1,'lm',opt));
disp 'done'
W = W .* (1.25 / rhoW);

% collect state matrix
X = zeros(1+inSize+resSize,trainLen-initLen);
% target matrix
Yt = data(initLen+2:trainLen+1)';

% collect X by run the reservoir
x = zeros(resSize,1);
for t = 1:trainLen
    u = data(t);
    x = (1-a)*x + a*(eye(1000) ./ (1 + exp(-Win*[1;u] - W*x )));
    %x = (1-a)*x + a*tanh( Win*[1;u] + W*x);
    if t > initLen
        X(:,t-initLen) = [1;u;x];
    end
end


% train output
reg = 1e-8;
Wout = ((X*X' + reg*eye(1+resSize+inSize)) \ (X*Yt'))';

% by test data collect Y
Y = zeros(outSize,testLen);
u = data(trainLen+1);
for t = 1:testLen
    x = (1-a)*x + a*(eye(1000) / (1 + exp(-Win*[1;u] - W*x )));
    %x = (1-a)*x + a*tanh( Win*[1;u] + W*x );%1./(1 + exp())
    y = Wout*[1;u;x];
    Y(:,t) = y;
    u = y;
end

% compute error
errorLen = 500;
mse = sum((data(trainLen+2:trainLen+errorLen+1)'-Y(1,1:errorLen)).^2)./errorLen;
disp( ['MSE = ' ,num2str( mse )]);

% plot signals
figure(1);
plot( data(trainLen+2:trainLen+testLen+1),'color', [0,0.75,0] );
hold on;
plot(Y','b');
hold off;
axis tight;
title('From n=0 target and generate signals');
legend('target signals','free-running predicted signals');

figure(2);
plot(X(1:20,1:200)');
title('reservoir x(n)');

figure(3);
bar(Wout');
title('output weight Wout');


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值