⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glacdbk.m

📁 用Cross validation的方法建立人工神经网络的模型!
💻 M
字号:
%
% function  [region, cdbk, iteration, distortion] = 
% GLAcdbk(data, n_columns, nwds,threshold): generate code book of size 
%                                              N=nwds for given data.
% 
% Init the code book to contain the centroids of small tubes of size blocksize^2 
% (eg. i=4, i*i=4*4), then iteratively tune the code book to be optimal
%
% Input:     data -- the feature matrix used to generate code book, each
%                    row represents an image
%       	 nwds -- size of code book, or number of codewords
%       threshold -- the threshold to determine when to stop the iteration
%       n_columns -- number of columns for each compressed image 
%
% Output: cdbk -- cell containing nwds*2 matrix, i.e. the vector of all code words
%    iteration -- the iterations tooked to generate the optimal cdbk
%       region -- nwds*1 cell, represents the partitions based on optimal cdbk
%   distortion -- iteration*1 array, contains all distortion history
% 

function [region, cdbk, iteration, distortion] = GLAcdbk(data, n_columns, nwds, threshold)

fprintf('GLA generate cdbk begin...\n');

% Init the first code word
[no_image no_features] = size(data);

% Init the codebook with random codewords
temp_cdbk = zeros(0,0);
min_f = min(data);
max_f = max(data);
f = zeros(nwds,no_features);
for i = 1: no_features
    f(:,i) = min_f(i) + (max_f(i)-min_f(i)) * rand(nwds,1);
end

% Check whether there are cdwds at same position, if yes, adjust the
% positions
% for i = 1: nwds
%    while(length(find(x==x(i)) & find(y==y(i))) > 1)
%        x(i) = 1 + (max_x-1)*rand(1);
%        y(i) = min_y + (max_y-min_y) * rand(1);
%    end
%end
temp_cdbk = f;


% Use GLA algorithm iteratively optimize the cdbk, stop when the distortion
% is smaller than some threshold
% D: contains consecutive overall average distortions,
iteration = 0;
region = cell(nwds, 1);

% iteratively tune the code book into optimal
while (iteration == 0) |(iteration == 1) | ((D(iteration-1)-D(iteration))/D(iteration) > threshold)
    
    % compute the average distortion
    iteration = iteration + 1;

    % partition the regions
    region = partition(data, temp_cdbk);
    
    % update the new code word to be centroid of the region
    for i = 1: nwds
        if size(region{i},1) > 0
            [temp_cdbk(i,:)] = mean(region{i}); %centroid(region{i});
        else
            f = zeros(1,no_features);
            for j = 1: no_features
                f(j) = min_f(j) + (max_f(j)-min_f(j)) * rand();;
            end
            temp_cdbk(i,:) = f;
        end
    end

    % D - the average distortion on current cdbk
    D(iteration) = avg_distortion(region, temp_cdbk);
    cdbk{iteration} = temp_cdbk;
end

distortion = D;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -