📄 filt_package.m
字号:
function packed_f = filt_package (packed_f, f1, f2, shift, tag)% FUNCTION packed_f = filt_package (packed_f, f1, f2, shift)%% This function reshapes and packages the filters. If f2=0, we% have only one filter f1. Otherwise, we have two filters to be% jointly reshaped. Note that f1 and f2 should be of the same size.%% The filters are, in essence, 4-D matrices, but% since these are normally sparse, we package the filters in a% sparse format. The reshaping and sparsifying procedures are% explained below:%% Input arguements f1 and f2 are given by x by y by old features by new% features, and since many elements are zero-valued, the 4-D matrix% is formated into a more light, sprase (only nonzero values are% stored) format. Because all the main operations are dot-products% (weighted sum), zero-values do not contribute to the results anyway.% The output packed_f is saved as a structure with nonzero% values/weights, three subscripts pointing to those nonzero values (x% by y by old features), and the size of input f1 and f2 (this size needs% to be known in order to compute an index (aux_sub2ind) to compute% the weighted sum). Those structure elements are given for each% new feature.%% The output, packed_f will be a structure consisting of: % f1, f2: values of the synaptic weights% i1, i2, i3: index/subscripts of above weights% size: size of the original filters% shift: shift value for convolution.% and these fields are suffixed by "tag", indicating the scale of% this filter. The "tag" will be in the format of "s?" (?=scale).num_feat = size(f1,4);siz = [size(f1,1) size(f1,2) size(f1,3) num_feat];if f2 == 0 % somewhat wasteful, but more easy to code, since we can consider % f1 and f2 in the same way. f2 = repmat(0, size(f1)); flag_f2_equal_0 = 1;else flag_f2_equal_0 = 0;endf1_r = reshape(f1, [prod(size(f1))/num_feat num_feat]);f2_r = reshape(f2, [prod(size(f1))/num_feat num_feat]);% maximum number of nonzero afferents.% Take the maximum of f1 and f2.max_nonzero_aff = max([max(sum(f1_r~=0)), max(sum(f2_r~=0))]);% f_i? has one extra component to make sure there is at least one% zero element: avoid infinite loop % We are using aux_sub2ind to make subscript (0,1,1) -> index 0 .f_i1 = zeros(max_nonzero_aff+1, num_feat);f_i2 = ones(max_nonzero_aff+1, num_feat);f_i3 = ones(max_nonzero_aff+1, num_feat);f1_value = zeros(max_nonzero_aff, num_feat);f2_value = zeros(max_nonzero_aff, num_feat);% Loop thru number of new featuresfor i = 1:num_feat % Find index of nonzero weights nonzero_index = find((abs(f1_r(:,i))~=0) | (abs(f2_r(:,i))~=0)); if length(nonzero_index) > 0 [i1, i2, i3] = ind2sub(siz(1:3), nonzero_index); f_i1(1:length(nonzero_index),i) = i1; f_i2(1:length(nonzero_index),i) = i2; f_i3(1:length(nonzero_index),i) = i3; f1_value(1:length(nonzero_index),i) = f1_r(nonzero_index,i); f2_value(1:length(nonzero_index),i) = f2_r(nonzero_index,i); endendif flag_f2_equal_0 f2_value = 0;end% Now set all the fields of the filter.packed_f = setfield(packed_f, ['f1_' tag], f1_value);packed_f = setfield(packed_f, ['f2_' tag], f2_value);packed_f = setfield(packed_f, ['i1_' tag], f_i1); packed_f = setfield(packed_f, ['i2_' tag], f_i2); packed_f = setfield(packed_f, ['i3_' tag], f_i3); packed_f = setfield(packed_f, ['size_' tag], siz); packed_f = setfield(packed_f, ['shift_' tag], shift);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -