- ignore *.asv
git-svn-id: http://moon:8086/svn/matlab/trunk@96 801c6759-fa7c-4059-a304-17956f83a07c
This commit is contained in:
@@ -1,20 +0,0 @@
|
|||||||
function [xpca xzca] = PCAwhite(x)
|
|
||||||
size(x)
|
|
||||||
x = x';
|
|
||||||
k= 10;
|
|
||||||
epsilon=1e-42;
|
|
||||||
avg = mean(x, 1)
|
|
||||||
|
|
||||||
x = x - repmat(avg, size(x, 1), 1);
|
|
||||||
|
|
||||||
sigma = x * x' / size(x, 2);
|
|
||||||
size(sigma)
|
|
||||||
[U,S,V] = svd(sigma);
|
|
||||||
%xRot = U' * x; % rotated version of the data.
|
|
||||||
%xTilde = U(:,1:k)' * x; % reduced dimension representation of the data,
|
|
||||||
% where k is the number of eigenvectors to keep
|
|
||||||
|
|
||||||
xpca = diag(1./sqrt(diag(S) + epsilon)) * U' * x;
|
|
||||||
xzca = U * diag(1./sqrt(diag(S) + epsilon)) * U' * x;
|
|
||||||
|
|
||||||
plot(x(
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
function jbatch2()
|
|
||||||
numLabels = 10;
|
|
||||||
|
|
||||||
maxNumSamples = -1;
|
|
||||||
di
|
|
||||||
for d=1:numLabels,
|
|
||||||
Dall(d) = load(['digit' num2str(d-1)]);
|
|
||||||
fprintf('%5d Digits of class %d\n',size(Dall(d).D,1),d-1);
|
|
||||||
maxNumSamples = max(maxNumSamples, size(Dall(d).D,1));
|
|
||||||
jconv(Dall(d).D/256, d-1);
|
|
||||||
% jimage(Dall{d}/256, d-1, 28, 28);
|
|
||||||
end;
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
function jconv(data, label)
|
|
||||||
[numTrain, numPixel] = size(data);
|
|
||||||
|
|
||||||
s = ['mnist_' num2str(label) '.trainingStates.dat']
|
|
||||||
fid = fopen(s, 'w');
|
|
||||||
fprintf(fid, '%d\n', numTrain);
|
|
||||||
|
|
||||||
for m=1:numTrain,
|
|
||||||
jwrite(fid, data(m,:));
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
fclose(fid);
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
function jimage(data, label, nx, ny)
|
|
||||||
[numTrain, numPixel] = size(data);
|
|
||||||
|
|
||||||
for m=1:numTrain,
|
|
||||||
im0 = reshape(data(m,:), nx, ny)';
|
|
||||||
imwrite(im0, ['mnist_' num2str(label) '_' num2str(m) '.tif']);
|
|
||||||
[im_on, im_off] = retina(im0, 7, 0.3);
|
|
||||||
end
|
|
||||||
|
|
||||||
%imwrite(im_on, [dst_dir 'images\' filename '.on.tif']);
|
|
||||||
%imwrite(im_off, [dst_dir 'images\' filename '.off.tif']);
|
|
||||||
%imwrite(0.5*(im_on - im_off) + 0.5, [dst_dir 'images\' filename '.on_off.tif']);
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
function jwrite_retina(fid, data, nx, ny)
|
|
||||||
|
|
||||||
numPixel = length(data);
|
|
||||||
|
|
||||||
fprintf(fid, '%d\n', numPixel);
|
|
||||||
im0 = reshape(data(m,:), nx, ny)';
|
|
||||||
imwrite(im0, ['mnist_' num2str(label) '_' num2str(m) '.tif']);
|
|
||||||
[im_on, im_off] = retina(im0, 7, 0.3);
|
|
||||||
imwrite(0.5*(im_on - im_off) + 0.5, ['mnist_retina_' num2str(label) '_' num2str(m) '.tif']);
|
|
||||||
|
|
||||||
for n=1:numPixel,
|
|
||||||
fprintf(fid, '%f\n', data(n));
|
|
||||||
end
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
function norbload()
|
|
||||||
|
|
||||||
fid=fopen('smallnorb-5x46789x9x18x6x2x96x96-training-dat.mat','r');
|
|
||||||
fread(fid,4,'uchar'); % result = [85 76 61 30], byte matrix(in base 16: [55 4C 3D 1E])
|
|
||||||
fread(fid,4,'uchar'); % result = [4 0 0 0], ndim = 4
|
|
||||||
numImages = fread(fid,4,'uchar'); % result = [236 94 0 0], dim0 = 24300 (=94*256+236)
|
|
||||||
fread(fid,4,'uchar'); % result = [2 0 0 0], dim1 = 2
|
|
||||||
fread(fid,4,'uchar'); % result = [96 0 0 0], dim2 = 96
|
|
||||||
fread(fid,4,'uchar'); % result = [96 0 0 0], dim3 = 96
|
|
||||||
|
|
||||||
numImages = numImages
|
|
||||||
for n=1:200,
|
|
||||||
im = transpose(reshape(fread(fid,96*96),96,96))/256;
|
|
||||||
if (mod(n, 2) == 0)
|
|
||||||
size(im)
|
|
||||||
imshow(im); % show the first image
|
|
||||||
pause (0.1);
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
function pca_eval()
|
|
||||||
|
|
||||||
close all;
|
|
||||||
|
|
||||||
Ntrain = 100;
|
|
||||||
Ndim = 2;
|
|
||||||
K = 1;
|
|
||||||
epsilon = 0.1;
|
|
||||||
|
|
||||||
x = 2*(rand(Ndim, Ntrain)-0.5);
|
|
||||||
x = 0.3*x + repmat(linspace(-0.7,0.7, Ntrain), Ndim, 1);
|
|
||||||
plot(x(1,:), x(2,:), '+'); axis([-1 1 -1 1]); grid; title('Raw data'); xlabel('x_1'); ylabel('x_2');
|
|
||||||
|
|
||||||
sigma = x * x' / size(x, 2)
|
|
||||||
[U,S,V] = svd(sigma)
|
|
||||||
xrot = U' * x;
|
|
||||||
figure;
|
|
||||||
plot(xrot(1,:), xrot(2,:), '+'); axis([-1 1 -1 1]); grid; title('Rotated data'); xlabel('x_{rot,1}'); ylabel('x_{rot,2}');
|
|
||||||
|
|
||||||
xpca = pcdiag(sqrt(1./(diag(S) + epsilon))) * xrot;
|
|
||||||
figure;
|
|
||||||
plot(xpca(1,:), xpca(2,:), '+'); axis([-1 1 -1 1]); grid; title('Whitened data dim=2'); xlabel('x_{pca,1}'); ylabel('x_{pca,2}');
|
|
||||||
|
|
||||||
|
|
||||||
xtilde = zeros(size(xrot));
|
|
||||||
xtilde(1:K, :) = xrot(1:K, :);
|
|
||||||
|
|
||||||
lambda = diag(S);
|
|
||||||
retained_variance = sum(lambda(1:K))./sum(lambda)
|
|
||||||
|
|
||||||
figure;
|
|
||||||
plot(xtilde(1,:), xtilde(2,:), '+'); axis([-1 1 -1 1]); grid; title('Rotated data reduced to dim = 1'); xlabel('x_{tilde,1}'); ylabel('x_{tilde,2}');
|
|
||||||
|
|
||||||
size (U)
|
|
||||||
size(xrot)
|
|
||||||
xhat = U(:, 1:K) * xrot(1:K, :);
|
|
||||||
|
|
||||||
figure;
|
|
||||||
plot(xhat(1,:), xhat(2,:), '+'); axis([-1 1 -1 1]); grid; title('Data reduced to dim = 1'); xlabel('x_{hat,1}'); ylabel('x_{hat,2}');
|
|
||||||
|
|
||||||
xpca1 = diag(sqrt(1./(diag(S)))) * xhat;
|
|
||||||
figure;
|
|
||||||
plot(xpca1(1,:), xpca1(2,:), '+'); axis([-1 1 -1 1]); grid; title('Whitened data dim=1'); xlabel('x_{pca,1}'); ylabel('x_{pca,2}');
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
function rbm_eval(nh, batch, numEpochs)
|
|
||||||
|
|
||||||
[nv batchsize] = size(batch);
|
|
||||||
|
|
||||||
numGibbs = 1;
|
|
||||||
muW = 0.1;
|
|
||||||
muBV = 0.1;
|
|
||||||
muBH = 0.1;
|
|
||||||
momentum = 0.5;
|
|
||||||
momentum_final = 0.9;
|
|
||||||
weightDecay = 0.000;
|
|
||||||
|
|
||||||
w = 0.1*randn(nv, nh);
|
|
||||||
bv = zeros(nv, 1);
|
|
||||||
bh = zeros(1, nh);
|
|
||||||
|
|
||||||
v = zeros(batchsize, nv);
|
|
||||||
h = zeros(batchsize, nh);
|
|
||||||
|
|
||||||
diffW = zeros(nv, nh);
|
|
||||||
diffBV = zeros(1, nv);
|
|
||||||
diffBH = zeros(1, nh);
|
|
||||||
diffErr = zeros(nv, batchsize);
|
|
||||||
|
|
||||||
dW = zeros(nv, nh);
|
|
||||||
dBV = zeros(nv, 1);
|
|
||||||
dBH = zeros(nh, 1);
|
|
||||||
|
|
||||||
for k=1:numEpochs
|
|
||||||
errsum = 0;
|
|
||||||
% Positive phase
|
|
||||||
v = batch;
|
|
||||||
h = 1./(1 + exp(-(v' * w + repmat(bh, batchsize, 1))));
|
|
||||||
|
|
||||||
% Update difference
|
|
||||||
diffW = v * h;
|
|
||||||
diffBV = sum(v, 2);
|
|
||||||
diffBH = sum(h')';
|
|
||||||
diffErr = v;
|
|
||||||
% Negative phase
|
|
||||||
for n=1:numGibbs
|
|
||||||
% h is sampled
|
|
||||||
h = h > rand(batchsize, nh);
|
|
||||||
|
|
||||||
% reconstruct
|
|
||||||
v = 1./(1 + exp(-(h *w'+ repmat(bv, batchsize, 1))));
|
|
||||||
|
|
||||||
% Get new h probabilities from reconstruction
|
|
||||||
h = 1./(1 + exp(-(v' * w + repmat(bh, batchsize, 1))))';
|
|
||||||
end
|
|
||||||
% Update difference
|
|
||||||
diffW = diffW - v * h';
|
|
||||||
diffBV = diffBV - sum(v, 2);
|
|
||||||
diffBH = diffBH - sum(h');
|
|
||||||
diffErr = diffErr - v;
|
|
||||||
|
|
||||||
err = sum(sum((diffErr).^2));
|
|
||||||
errsum = errsum + err;
|
|
||||||
|
|
||||||
if k > 5,
|
|
||||||
momentum = momentum_final;
|
|
||||||
end;
|
|
||||||
|
|
||||||
% Update parameter gradients
|
|
||||||
dW = momentum*dW + muW*(diffW/batchsize - weightDecay*w);
|
|
||||||
dBV = momentum*dBV + muBV*diffBV/batchsize;
|
|
||||||
dBH = momentum*dBH + muBH*diffBH/batchsize;
|
|
||||||
|
|
||||||
% Update parameters
|
|
||||||
w = w + dW;
|
|
||||||
bv = bv + dBV;
|
|
||||||
bh = bh + dBH;
|
|
||||||
end
|
|
||||||
v = v
|
|
||||||
err = err
|
|
||||||
errsum = errsum
|
|
||||||
Reference in New Issue
Block a user