Home > @ahrbf > private > rbfnet.m

rbfnet

PURPOSE ^

RBFNET Constructs an adaptive hyperplane radial basis

SYNOPSIS ^

function net = rbfnet(varargin)

DESCRIPTION ^

 RBFNET Constructs an adaptive hyperplane radial basis
   function network
   RBFNET(nin,nhid,nout,learnp,irange);
       nin     - number of inputs
       nhid    - number of bases per input dimension
       nout    - number of outputs
       learnp  - learning rate
       irange  - input range
       vers    - version of getcenter to use [1,2,3]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function net = rbfnet(varargin)
0002 % RBFNET Constructs an adaptive hyperplane radial basis
0003 %   function network
0004 %   RBFNET(nin,nhid,nout,learnp,irange);
0005 %       nin     - number of inputs
0006 %       nhid    - number of bases per input dimension
0007 %       nout    - number of outputs
0008 %       learnp  - learning rate
0009 %       irange  - input range
0010 %       vers    - version of getcenter to use [1,2,3]
0011 
0012 % Satrajit Ghosh, SpeechLab, Boston University. (c)2001
0013 % $Header: /mnt/localhd/cvsdir/MODELLING/NEWDIVA/@ahrbf/private/rbfnet.m,v 1.1.1.1 2006/10/06 18:20:23 brumberg Exp $
0014 
0015 % $NoKeywords: $
0016 
0017 global in_data out_data;
0018 
0019 % Determine the total number of hidden bases
0020 m = varargin{1};    % Input dimension
0021 k = varargin{2};    % Hidden layer description
0022 
0023 % If only one value is assigned then each dimension has
0024 % that many units
0025 if (prod(size(k)) == 1),
0026     k  = k*ones(1,m);
0027 end;
0028 % Otherwise check to ensure that the length of the descriptor has
0029 % one entry for each input dimension
0030 if (prod(size(k)) ~= m),
0031     error('rbfnet: no. of bases per dimension not specified');
0032 end;
0033 k = k(:)';
0034 
0035 % Create the net structure
0036 net.nin     = m;            % Input dimension
0037 net.hiddist = k;            % Hidden units per dimension
0038 net.nhid    = prod(k);      % Number of hidden nodes
0039 net.nout    = varargin{3};  % Output dimension
0040 lrate       = varargin{4};  % learning rate
0041 irange      = varargin{5};  % input range across dimensions
0042 vers        = varargin{6};  % version of getcenter
0043 
0044 % Set the learning rates for the network
0045 net.alpha   = lrate(1);
0046 if length(lrate)>1,
0047     net.beta    = lrate(2);
0048 else,
0049     net.beta = 0;
0050 end;
0051 
0052 % Initialize values of different net related matrices
0053 net.c = zeros(net.nin,net.nhid);            % distance matrix of input to hidden neurons
0054 net.h = zeros(net.nhid,1);                  % Output of hidden neurons
0055 %net.v = zeros(net.nhid,net.nout);           % Output layer weights
0056 net.v = 0.001.*rand(net.nout,net.nhid);     % Majid's initialization value
0057 net.w = zeros(net.nin,net.nhid,net.nout);   % weights from input to output layer
0058 
0059 % Set net range
0060 net.min = min(irange);
0061 net.max = max(irange);
0062 net.range = net.max-net.min; % change to the possibility of diff ranges
0063 
0064 % Initialize inputs and outputs
0065 net.out = zeros(net.nout,1);
0066 net.in = zeros(net.nin,1);
0067 
0068 % Determine the centers and variances of the networks
0069 net.mu = zeros(net.nin,net.nhid);
0070 net.sg = zeros(net.nin,net.nhid);
0071 
0072 % The following piece of code has been subsumed into a more general form
0073 % rf = 1; % range multiplication factor
0074 % for i=1:net.nin,
0075 %     if (k(i)>1),
0076 %         sg(i,1) = (rf*net.range/(k(i)-1))/2;
0077 %     else,
0078 %         sg(i,1) = rf*net.range/2;
0079 %     end;
0080 % end;
0081 % net.sg = repmat(sg,1,net.nhid);
0082 
0083 % a and b are temporary storages for net centers
0084 % sg is a temporary storage for variances
0085 % The following loop determines the centers and variances
0086 % for each input dimension
0087 
0088 a = cell(net.nin,1);
0089 for i=1:net.nin,
0090     % a{i} = getcenter1(rf*net.min,rf*net.max,k(i),i);
0091     
0092     % change the function call different ways of initializing the
0093     % centers and variances
0094     switch vers,
0095     case 1,
0096         [a{i} sg{i}]= getcenter1(net.min,net.max,k(i),i);
0097     case 2,
0098         [a{i} sg{i}]= getcenter2(net.min,net.max,k(i),i);
0099     case 3,
0100         [a{i} sg{i}]= getcenter3(net.min,net.max,k(i),i);
0101     case 4,
0102         disp('Using k-means to determine center');
0103     otherwise,
0104         error('Incorrect version');
0105     end;
0106 end;
0107 
0108 % The following segment of code extends the centers and
0109 % variances for each dimension to a grid across the nin-dimensional
0110 % space
0111 if vers<4,
0112 b = cell(net.nin,1);
0113 c = cell(net.nin,1);
0114 if net.nin == 1,
0115     b{1} = a{1};
0116     c{1} = sg{1};
0117 else,
0118     [b{:}] = ndgrid(a{:});
0119     [c{:}] = ndgrid(sg{:});
0120 end;
0121 
0122 for i=1:net.nin,
0123     net.mu(i,:) = [b{i}(:)]';
0124     net.sg(i,:) = [c{i}(:)]';
0125 end;
0126 else,
0127     [idx,C] = kmeans(in_data',net.nhid);
0128     net.mu = C';
0129     for i=1:net.nhid,
0130 %        net.mu(:,i) = mean(in_data(:,idx==i)');
0131         net.sg(:,i) = var(in_data(:,idx==i)');
0132     end;
0133 end
0134 % The three different forms of the getcenter function
0135 
0136 % FORM 1: Determines the centers and variances based on the range of the
0137 %   the input dimensions and the number of bases for that dimension
0138 function [pos, sg] = getcenter1(netmin,netmax,num_hid,dim_num)
0139 %   netmin: minimum value for the dimension
0140 %   netmax: maximum value for the dimension
0141 %   num_hid: number of hidden neurons for that dimension
0142 %   dim_num: which dimension?
0143 
0144 if (num_hid == 1),
0145     pos = (netmin+netmax)/2;
0146     sg = (netmax-netmin)/2;
0147 else    
0148     %pos = linspace(min,max,k); % include the edge in center prediction
0149     
0150     % Centers predicted based on number of bins in that dimension
0151     % Centers are the middle of the bins
0152     pos = linspace(netmin,netmax,2*num_hid+1);
0153     pos = pos(2:2:end);
0154     sg = ((netmax-netmin)/(num_hid-1))/2*ones(num_hid,1);
0155 end;
0156 
0157 % FORM 2: Determines the centers and variances based on the range of the
0158 %   the inputs in training data and the number of bases for that dimension
0159 function [pos, sg]= getcenter2(netmin,netmax,num_hid,dim_num)
0160 
0161 global in_data out_data
0162 
0163 if (num_hid == 1),
0164     pos = mean(in_data(dim_num,:));
0165     sg=std(in_data(dim_num,:));
0166 else
0167     % determine position using bin centers
0168     pos_i = linspace(netmin,netmax,2*num_hid+1);
0169     pos_i = pos_i(2:2:end);
0170     
0171     for ii=1:num_hid
0172         kk=find(in_data(dim_num,:)>=netmin+(ii-1)*(netmax-netmin)/num_hid & in_data(dim_num,:)<=netmin+(ii)*(netmax-netmin)/num_hid);
0173         
0174         % TODO: alter for the case when no data is present on the bin
0175         pos(ii)=mean(in_data(dim_num,kk)); 
0176         
0177         % 2 seems like an appropriate multiplication factor
0178         % given uniform sampling
0179         sg(ii)=2*std(in_data(dim_num,kk));
0180         kk=[];
0181     end
0182 end
0183 
0184 % Replace the variance of empty bins with an arbitrary large number
0185 idx =find(isnan(sg));
0186 if ~isempty(idx),
0187     sg(idx) = 10;
0188 end;
0189 
0190 % Replace the means of empty bins with centers of bins
0191 idx =find(isnan(pos));
0192 if ~isempty(idx),
0193     pos(idx) = pos_i(idx);
0194 end;
0195 
0196 % FORM 3: Determines the centers and variances based on the range of the
0197 %   the inputs in training data and the number of bases for that dimension
0198 %   using an expectation maximization algorithm
0199 
0200 % In development
0201 function [pos, sg]= getcenter3(netmin,netmax,num_hid,dim_num)
0202 
0203 global in_data out_data
0204 
0205 Nb = num_hid;
0206 [h,x] = hist(in_data(dim_num,:),512);
0207 N = length(h);
0208 f = convn(h,hamming(14),'same');
0209 beta0 = [ones(Nb,1)/Nb,linspace(netmin,netmax,Nb)',4*ones(Nb,1)];
0210 [beta] = gmem(x,f,beta0);
0211 pos = beta(:,2);
0212 %sg = beta(:,3);
0213 if (num_hid == 1),
0214     sg = (netmax-netmin)/2;
0215 else    
0216     %pos = linspace(min,max,k); % include the edge in center prediction
0217     
0218     % Centers predicted based on number of bins in that dimension
0219     % Centers are the middle of the bins
0220     sg = ((netmax-netmin)/(num_hid-1))/2*ones(num_hid,1);
0221 end;

Generated on Tue 27-Mar-2007 12:06:24 by m2html © 2003