function out1 = rands(in1,in2,in3,in4,in5,in6)
% BOILERPLATE_START
% This code is the same for all Weight/Bias Initialization Functions.
%定义INFO并给INFO赋值,分为依据网络字符输入与自定义数值输入
persistent INFO;
if isempty(INFO)
if nnet.test.flags('initWeightInfoTestFcns')
INFO = nnet.test.initWeightInfoTestFcns;
else
INFO = get_info;
end
end
if (nargin < 1), error(message('nnet:Args:NotEnough')); end
%若in1为字符数组
%将字符变为小写,判断是"info"还是"configure"还是"initialize"
% 其中"initialize"中又有"InputWeight"的初始化、"layerweight"的初始化与"Bias"的初始化
% 各初始化的方法定义在后面,大小依据网络尺寸来定
%否则比较"INFO."与输入"in1"是否相等,不然输出"输入信息不可识别!"
if ischar(in1)
switch lower(in1)
case 'info', out1 = INFO;
case 'configure'
out1 = configure_weight(in2);
case 'initialize'
switch(upper(in3))
case {'IW'}
if INFO.initInputWeight
if in2.inputConnect(in4,in5)
out1 = initialize_input_weight(in2,in4,in5,in6);
else
out1 = [];
end
else
nnerr.throw([upper(mfilename) ' does not initialize input weights.']);
end
case {'LW'}
if INFO.initLayerWeight
if in2.layerConnect(in4,in5)
out1 = initialize_layer_weight(in2,in4,in5,in6);
else
out1 = [];
end
else
nnerr.throw([upper(mfilename) ' does not initialize layer weights.']);
end
case {'B'}
if INFO.initBias
if in2.biasConnect(in4)
out1 = initialize_bias(in2,in4);
else
out1 = [];
end
else
nnerr.throw([upper(mfilename) ' does not initialize biases.']);
end
otherwise,
error(message('nnet:Args:UnrecValue'));
end
otherwise
try
out1 = eval(['INFO.' in1]);
catch me,
nnerr.throw(['Unrecognized first argument: ''' in1 ''''])
end
end
else
%若不为字符数组
% 若输入只有1个元素,输出in1-by-1的[-1 1]的均匀分布随机矩阵
% 若输入有两个元素
% 当in2数组中只有1个元素,输出in1-by-in2的[-1 1]的均匀分布随机矩阵
% 当in2数组有2列,输出in1-by-size(minmax(in2),1)尺寸的[-1 1]的均匀分布随机矩阵
% 若in2数组不止2列,输出in1-by-size(minmax(in2),1)尺寸的[-1 1]的均匀分布随机矩阵
if (nargin == 1)
if INFO.initFromRows
out1 = new_value_from_rows(in1);
else
nnerr.throw([upper(mfilename) ' cannot initialize from rows.']);
end
elseif (nargin == 2)
if numel(in2) == 1
if INFO.initFromRowsCols
out1 = new_value_from_rows_cols(in1,in2);
else
nnerr.throw([upper(mfilename) ' cannot initialize from rows and columns.']);
end
elseif size(in2,2) == 2
if INFO.initFromRowsRange
out1 = new_value_from_rows_range(in1,minmax(in2));
%minmax为每个行向量的最小最大值
else
nnerr.throw([upper(mfilename) ' cannot initialize from rows and ranges.']);
end
elseif size(in2,2) > 2
if INFO.initFromRowsInput
out1 = new_value_from_rows_inputs(in1,minmax(in2));
else
nnerr.throw([upper(mfilename) ' cannot initialize from rows and inputs.']);
end
else
error(message('nnet:rands:SecondArgNotScalarOr2Col'));
end
else
error(message('nnet:Args:TooManyInputArgs'));
end
end
end
% BOILERPLATE_END
%%------------------------------------------------
function info = get_info
info = nnfcnWeightInit(mfilename,'Random Symmetric',7.0,...
true,true,true, true,true,true,true, true);
end
function settings = configure_weight(inputs)
settings = struct;
end
function w = initialize_input_weight(net,i,j,config)
w = 2*rand(net.inputWeights{i,j}.size)-1;
end
function w = initialize_layer_weight(net,i,j,config)
w = 2*rand(net.layerWeights{i,j}.size)-1;
end
function b = initialize_bias(net,i)
b = 2*rand(net.layers{i}.size,1)-1;
end
function x = new_value_from_rows(rows)
x = 2*rand(rows,1)-1;
end
function x = new_value_from_rows_cols(rows,cols)
x = 2*rand(rows,cols)-1;
end
function x = new_value_from_rows_range(rows,range)
x = new_value_from_rows_cols(rows,size(range,1));
end
function x = new_value_from_rows_inputs(rows,inputs)
x = new_value_from_rows_cols(rows,size(inputs,1));
end
MATLAB rands.m函数部分注释
最新推荐文章于 2023-09-08 14:19:26 发布