添加更多的选项来gruLayer GateActivationFunction

4视图(30天)
哈姆萨
哈姆萨 2023年2月28日
回答: 2023年3月13日
问候,
我想火车作为回归格勒乌RNN网络估计一个时间序列信号。我想看到的效果改变GateActivationFunction有限但我有两个选项“乙状结肠,hard-sigmoid”。我试着添加更多选项”双曲正切,radbasn”以下文件“GRULayer、gruForwardGeneral GRULayer”,下面附修改后的文件。当我试图用“gruLayer运行代码(Hiddenlayers1,‘名’,‘gru1’,‘OutputMode’,‘序列’,‘StateActivationFunction’,‘双曲正切’,‘GateActivationFunction’,‘双曲正切”)。我将看到一个错误告诉我,我与两个选项有限。我的问题是,添加更多的选项呢?如果是的我大多数的其他文件编辑是什么?。
谢谢,
Hamza Al Kouzbary
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
gruLayer % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
函数层= gruLayer(变长度输入宗量)
% gruLayer封闭的复发性单元层
%
% = gruLayer层(numHiddenUnits)创建一个封闭的复发性单元层。
% numHiddenUnits层是隐藏单位的数量,指定为
%一个正整数。
%
%层= gruLayer (VAL1 numHiddenUnits,‘PARAM1’,‘PARAM2’, VAL2,…)
%指定可选的参数名称/值对来创建层:
%
%为层,“名字”——名称指定
%作为特征向量或
%的字符串。默认值是
%”。
%’InputWeights”——输入权重,由指定的
% 3 * numHiddenUnits-by-D矩阵或
% [],D的数量
%的特征输入数据。的
%默认是[]。
%的RecurrentWeights经常性的权重,指定为
% 3 * numHiddenUnits-by -
% numHiddenUnits
%矩阵或[]。默认值是
% []。
%“偏见”——层偏见,指定为一个
% 3 * numHiddenUnits-by-1向量,一个
% 6 * numHiddenUnits-by-1向量,
%或[]。缺省值是[]。
%的HiddenState最初隐藏状态,指定
%作为numHiddenUnits-by-1向量
%或[]。缺省值是[]。
%的OutputMode的输出的格式
%层。选项有:
% - - - - - -“序列”,输出一个
%完整的序列。
%——最后的输出
%只有最后一个元素。
%的默认值
%的“序列”。
%的StateActivationFunction激活函数来更新
%隐藏状态。
%的选项是:
%——“双曲正切
% - - - - - -“softsign”
%的默认值是“双曲正切”。
%的GateActivationFunction激活函数适用于
%的大门。选项有:
% - - - - - -“乙状结肠”
%——“双曲正切
% - - - - - -“radbasn”
% - - - - - -“hard-sigmoid”
%的默认值是“乙状结肠”。
%的InputWeightsLearnRateFactor乘数的学习
%的速度输入权重,
%指定为一个标量或
%三元素向量。的
%的默认值是1。
%的RecurrentWeightsLearnRateFactor乘数的学习
%的复发性权重,
%指定为一个标量或
%三元素向量。的
%的默认值是1。
%的BiasLearnRateFactor乘数的学习
%的速度偏差,指定为
%一个标量或三元素
%的向量。默认值是1。
%的InputWeightsL2Factor L2的乘数
%规范的输入
%重量,指定为一个标量
%或三元素向量。的
%的默认值是1。
%的RecurrentWeightsL2Factor L2的乘数
%经常性的调整
%重量,指定为一个标量
%或三元素向量。的
%的默认值是1。
%的BiasL2Factor L2的乘数
%规范的偏差,
%指定为一个标量或
%三元素向量。的
%的默认值是0。
%的InputWeightsInitializer函数来初始化
%的输入权值,指定为
%’glorot’,‘他’,“正交”,
% ' narrow-normal ', ' 0 ',
%的或一个函数处理。
%默认是“glorot”。
%的RecurrentWeightsInitializer函数来初始化
%的权重,指定为
%’glorot’,‘他’,“正交”,
% ' narrow-normal ', ' 0 ',
%的或一个函数处理。
%默认是“正交”。
%的BiasInitializer函数来初始化
%的偏见,指定为“0”,
%“narrow-normal”,或“的”
%函数处理。默认值是
%的“0”。
%的ResetGateMode重置门模式下,指定为
%以下之一:
% - - - - - -“after-multiplication”,
%申请重置门后
%矩阵乘法。这
使用cuDNN %选项
%上运行时库
% GPU。
% - - - - - -“before-multiplication”,
%申请重置大门之前
%矩阵乘法。
% - - - - - -“recurrent-bias-after-multiplication”,
%申请重置门后
%矩阵乘法和
%使用复发性偏见。
%的默认值
%的“after-multiplication”。
%
%示例1:
% %与100年创建一个格勒乌层隐藏的单位。
%
% = gruLayer层(100);
%
%示例2:
% %创建一个格勒乌和50个隐藏层单元返回最后一个
% %输出元素的序列。手动初始化复发
% %重量从标准差的高斯分布
% % 0.01。
%
% numHiddenUnits = 50;
% = gruLayer层(numHiddenUnits OutputMode,“去年”,…
% RecurrentWeights, randn ([3 * numHiddenUnits numHiddenUnits)) * 0.01);
%
%参见nnet.cnn.layer.GRULayer
%
% < a href = " matlab: helpview (“deeplearning”、“list_of_layers”)" >的深度学习层< / >
% 2019 - 2020版权MathWorks公司。
%解析输入参数。
变长度输入宗量= nnet.internal.cnn.layer.util.gatherParametersToCPU(变长度输入宗量);
args = nnet.cnn.layer.GRULayer.parseInputArguments(变长度输入宗量{:});
%创建一个层的内部表示。
internalLayer = nnet.internal.cnn.layer.GRU (args.Name,
args.InputSize,
args.NumHiddenUnits,
真的,
iGetReturnSequence (args.OutputMode),
args.StateActivationFunction,
args.GateActivationFunction,
args.ResetGateMode);
%使用内部一层一层构建一个用户可见。
层= nnet.cnn.layer.GRULayer (internalLayer);
%设置可学的参数,学习速度,L2因素和初始值设定项。
层。InputWeights = args.InputWeights;
层。InputWeightsL2Factor = args.InputWeightsL2Factor;
层。InputWeightsLearnRateFactor = args.InputWeightsLearnRateFactor;
层。InputWeightsInitializer = args.InputWeightsInitializer;
层。RecurrentWeights = args.RecurrentWeights;
层。RecurrentWeightsL2Factor = args.RecurrentWeightsL2Factor;
层。RecurrentWeightsLearnRateFactor = args.RecurrentWeightsLearnRateFactor;
层。RecurrentWeightsInitializer = args.RecurrentWeightsInitializer;
层。偏见= args.Bias;
层。偏见L2Factor = args.BiasL2Factor;
层。偏见LearnRateFactor = args.BiasLearnRateFactor;
层。偏见初始值设定项=args.BiasInitializer;
%设置隐藏状态的状态。
层。HiddenState = args.HiddenState;
结束
函数tf = iGetReturnSequence(模式)
tf = true;
如果比较字符串(模式,“最后一次”)
tf = false;
结束
结束
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
gruForwardGeneral % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
函数[h, h] = gruForwardGeneral (X,可学的状态,选项)
% gruForwardGeneral实现格勒乌向前,明白了
% https://arxiv.org/abs/1406.1078v1
% 2019年版权MathWorks公司。
W = learnable.W;
R = learnable.R;
b = learnable.b;
h0 = state.h0;
%确定尺寸
numHidden =大小(R, 2);
%确定尺寸
(~ N T) = (X)大小;
%索引助手
(皮、zInd后)= nnet.internal.cnn.util.gruGateIndices (numHidden);
%输入重量
Wrz = W((皮,zInd):);
:Wh = W(后);
%的权重
Rrz = R((皮,zInd):);
Rh = R(后,:);
%的偏见
青铜= b((皮,zInd):);
bh = b(后,:);
%预先分配隐藏状态
h = 0 (numHidden N T,“喜欢”,X);
如果isstring (options.StateActivationFunction) | | ischar (options.StateActivationFunction)
stateActivationFunction = iGetStateActivation(选项。StateActivationFunction);
elseifisa (options.StateActivationFunction“function_handle”)
stateActivationFunction = options.StateActivationFunction;
结束
如果isstring (options.GateActivationFunction) | | ischar (options.GateActivationFunction)
gateActivationFunction = iGetGateActivation(选项。GateActivationFunction);
elseifisa (options.GateActivationFunction“function_handle”)
gateActivationFunction = options.GateActivationFunction;
结束
%的第一次迭代循环
%更新r和z盖茨
rz = gateActivationFunction (Wrz * X (:,: 1) + Rrz * h0 +青铜);
:r = rz(皮);
z = rz (zInd:);
%计算候选国家海关
hs = stateActivationFunction (Wh * X (:,: 1) + r。* (Rh * h0) + bh);
%更新隐藏状态h
h (:: 1) = (1 - z)。* h + z。* h0;
%主发展循环
tt = 2: T
hIdx = h (:,:, tt-1);
%更新r和z盖茨
rz = gateActivationFunction (Wrz * X (:,: tt) + Rrz * hIdx +青铜);
:r = rz(皮);
z = rz (zInd:);
%计算候选国家海关
hs = stateActivationFunction (Wh * X (:,: tt) + r。* (Rh * hIdx) + bh);
%更新隐藏状态h
h (:,:, tt) = (1 - z)。* h + z。* hIdx;
结束
如果options.ReturnLast
H = H;
h = h(:,:,结束);
其他的
H = H(:,:,结束);
结束
结束
% %辅助函数
函数行动= iGetStateActivation(激活)
开关激活
情况下的双曲正切
行动= @nnet.internal.cnnhost.tanhForward;
情况下“softsign”
行动= @iSoftSign;
结束
结束
函数行动= iGetGateActivation(激活)
开关激活
情况下“乙状结肠”
行动= @nnet.internal.cnnhost.sigmoidForward;
情况下“hard-sigmoid”
行动= @nnet.internal.cnnhost.hardSigmoidForward;
情况下的双曲正切
行动= @nnet.internal.cnnhost.tanhForward;
情况下“radbasn”
行动= @nnet.internal.cnnhost.hardSigmoidForward;
结束
结束
% %激活函数
函数y = iSoftSign (x)
y = x。/ (1 + abs (x));
结束
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
GRULayer % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
classdefGRULayer < nnet.cnn.layer.Layer & nnet.internal.cnn.layer.Externalizable
% GRULayer封闭的复发性单元(格勒乌)层
%
%创建格勒乌层,使用gruLayer。
%
% GRULayer属性:
%的名字——层的名称
% InputSize:输入层的大小
% NumHiddenUnits——隐藏单位的层数
% OutputMode——输出序列或最后
% StateActivationFunction——激活函数
%更新隐藏状态
% GateActivationFunction——激活函数
%申请盖茨
% ResetGateMode - ResetGateMode重置门
%模式。申请重置门
%之前或之后的矩阵
%的乘法,或
%没有复发的偏见。
% NumInputs——输入的数量
%的层。
% InputNames——输入的名称
%的层。
% NumOutputs——输出的数量
%的层。
% OutputNames——输出的名称
%的层。
%
%属性可学的参数:
% InputWeights——输入权重
% InputWeightsInitializer——函数
%初始化输入
%的重量。
% InputWeightsLearnRateFactor——学习速率的乘数
输入重量%
% InputWeightsL2Factor - L2的乘数
%输入重量
%
% RecurrentWeights:复发性权重
% RecurrentWeightsInitializer——函数
%初始化的
%的重量。
% RecurrentWeightsLearnRateFactor——学习速率的乘数
%的复发性权重
% RecurrentWeightsL2Factor - L2的乘数
%的权重
%
%偏差,偏差向量
% BiasInitializer——函数
%初始化倾向。
% BiasLearnRateFactor——学习速率的乘数
%的偏见
% BiasL2Factor - L2乘数的偏见
%
%的状态参数:
% HiddenState——隐藏的状态向量
%
%的例子:
%创建一个封闭的复发性单元层。
%
% = gruLayer层(10)
%
%参见gruLayer
% 2019 - 2020版权MathWorks公司。
属性(依赖)
层的名称%的名字
%层的名称。如果这是设置为”,那么一个名字
%在训练时间自动设置。
的名字
结束
属性(SetAccess =私人,依赖)
% InputSize输入大小层。如果这个设置
% '汽车',然后输入大小期间将自动设置
%的培训
InputSize
% NumHiddenUnits隐藏单位的层数
NumHiddenUnits
% OutputMode层的输出格式。如果“序列”,
%的输出是一个序列。如果“最后”,是最后一个元素的输出
%在序列
OutputMode
% StateActivationFunction激活函数来更新
%隐藏状态。有效的选项是“双曲正切”或“softsign”。默认的
%的双曲正切”。
StateActivationFunction
% GateActivationFunction激活函数适用于
%盖茨。有效的选项是“乙状结肠”或“hard-sigmoid”。默认的
%的乙状结肠。
GateActivationFunction
% ResetGateMode重置门模式下,指定的
%:
%的after-multiplication应用重置门后的矩阵
%的乘法。采用这一选项偏差大小
% 3 * numHiddenUnits-by-1。这个选项是cuDNN兼容。
%的before-multiplication应用重置门之前矩阵
%的乘法。采用这一选项偏差大小
% 3 * numHiddenUnits-by-1。
%的recurrent-bias-after-multiplication申请重置
%矩阵乘法和使用后复发性偏见。与
%偏差尺寸6 * numHiddenUnits-by-1这个选项。这
%选择是cuDNN兼容。
ResetGateMode
结束
属性(依赖)
% InputWeights输入层的权重
%的输入权重矩阵格勒乌层。输入体重
%矩阵是一个垂直连接的三个“门”的输入
%权重矩阵的传球前进格勒乌。这些个人
%按照以下顺序矩阵连接:更新门,
%重置门,门输出。这个矩阵的大小
% 3 * NumHiddenUnits-by-InputSize。
InputWeights
% InputWeightsInitializer初始化的函数
输入重量%。
InputWeightsInitializer
% InputWeightsLearnRateFactor的学习速率的因素
%输入重量
%的学习速率因子输入权重。这个因素是
%乘以全球学习速率来确定
%的输入权值学习速率这一层。例如,
%如果设置为2,然后输入权值的学习速率
%在这一层将当前全球学习速率的两倍。
%的值来控制学习的三个人
% InputWeights矩阵,可以分配一个1×3向量。
InputWeightsLearnRateFactor(:1){mustBeNumeric, iCheckFactorDimensions}
% InputWeightsL2Factor L2正则化因子的输入
%的重量
%的L2正则化因子输入权重。这个因素
%是乘以全球L2正则化设置
%确定输入的L2正规化设置权重
%在这一层。例如,如果它被设置为2,那么L2
%正规化的输入权重这一层
%两次全球L2正则化设置。控制
%的L2价值因素三个个人矩阵
% InputWeights, 1×3向量可以被指定。
InputWeightsL2Factor(:1){mustBeNumeric, iCheckFactorDimensions}
%的复发性权重RecurrentWeights层
%的复发性权重矩阵格勒乌层。的复发
%重量矩阵是一个垂直连接的三个“门”
%的权重矩阵的传球前进格勒乌。那些
%个人按照以下顺序矩阵连接:
%更新门,重置大门,门输出。这个矩阵将会
% 3 * NumHiddenUnits-by-NumHiddenUnits大小。
RecurrentWeights
% RecurrentWeightsInitializer初始化的函数
%的权重。
RecurrentWeightsInitializer
% RecurrentWeightsLearnRateFactor学习速率的因素
%的复发性权重
%经常性的学习速率因子权重。这个因素
%是乘以全球学习速率来确定
%的复发性权重学习速率这一层。为
%的例子中,如果它被设置为2,那么学习的
%的重量在这一层将是目前的两倍
%的全球学习速率。控制学习的价值率
%三个体RecurrentWeights矩阵,
% 1×3向量可以被指定。
RecurrentWeightsLearnRateFactor(:1){mustBeNumeric, iCheckFactorDimensions}
% RecurrentWeightsL2Factor的L2正则化因子
%的权重
%复发的L2正则化因子权重。这
%的因素是乘以全球L2正则化设置
%确定L2正规化的复发
%重量在这一层。例如,如果它被设置为2,那么
% L2正规化的复发性权重这一层
%是全球L2正规化设置的两倍。控制
%的L2价值因素三个个人矩阵
% RecurrentWeights, 1×3向量可以被指定。
RecurrentWeightsL2Factor(:1){mustBeNumeric, iCheckFactorDimensions}
%的偏见的偏见层
%的偏差向量格勒乌层。向量是一种偏见
%连接的三个“门”偏差向量在前进
%通过格勒乌。这些个人向量是连接
%以下顺序:更新门,重置大门,门输出。这
% 3 * NumHiddenUnits-by-1向量将大小。
偏见
% BiasInitializer函数初始化倾向
BiasInitializer
%的学习速率因子BiasLearnRateFactor偏见
%的学习速率因子的偏见。这个因素是
%乘以全球学习速率来确定
%学习速率的偏见在这一层。例如,如果它是
%设置为2,那么这一层的学习速率的偏见
%将当前全球学习速率的两倍。控制
% 3个人的学习速率值向量
%的偏见,一个1×3向量可以分配。
BiasLearnRateFactor(:1){mustBeNumeric, iCheckFactorDimensions}
% BiasL2Factor L2正则化因子的偏见
%的L2正则化因子偏见。这个因素是
%乘以全球L2正则化设置
%确定L2正规化的偏见
%层。例如,如果它被设置为2,那么L2
%正规化的偏见这一层的两倍
%全球L2正则化设置。控制的价值
%的L2因素三个个体向量偏见,a
% 1×3向量可以被指定。
BiasL2Factor(:1){mustBeNumeric, iCheckFactorDimensions}
结束
属性(依赖)
% HiddenState隐藏状态的初始值。
%的初始值隐藏状态。这个向量会
% NumHiddenUnits-by-1大小。设置这个值设置默认
%值的隐藏状态重置时调用
% resetState SeriesNetwork的方法。
HiddenState
结束
属性(SetAccess =私人、隐藏的依赖)
% OutputSize隐藏单位的层数。看到
% NumHiddenUnits。
OutputSize
% OutputState隐藏状态的层。看到HiddenState。
OutputState
结束
方法
函数这= GRULayer (privateLayer)
这一点。PrivateLayer = PrivateLayer;
结束
函数val = get.Name(这)
val = this.PrivateLayer.Name;
结束
函数这= set.Name(这个,val)
iAssertValidLayerName (val);
this.PrivateLayer。Name = char (val);
结束
函数val = get.InputSize(这)
val = this.PrivateLayer.InputSize;
如果isempty (val)
val =“汽车”;
结束
结束
函数val = get.NumHiddenUnits(这)
val = this.PrivateLayer.HiddenSize;
结束
函数val = get.OutputMode(这)
val = iGetOutputMode (this.PrivateLayer。ReturnSequence);
结束
函数val = get.StateActivationFunction(这)
val = this.PrivateLayer.Activation;
结束
函数val = get.GateActivationFunction(这)
val = this.PrivateLayer.RecurrentActivation;
结束
函数val = get.InputWeights(这)
val = this.PrivateLayer.InputWeights.HostValue;
如果isa (val,“dlarray”)
val = extractdata (val);
结束
结束
函数val = get.ResetGateMode(这)
val = this.PrivateLayer.ResetGateMode;
结束
函数这= set.InputWeights(这个值)
如果isequal (this.InputSize“汽车”)
expectedInputSize =南;
其他的
expectedInputSize = this.InputSize;
结束
属性= {“大小”(3 *。NumHiddenUnits expectedInputSize),
“真实”的,“nonsparse”};
值= iGatherAndValidateParameter(价值、属性);
如果~ isempty(值)
这一点。PrivateLayer = this.PrivateLayer.configureForInputs (
{iMakeSizeOnlyArray([大小(价值,2)南南),“认知行为治疗”)});
结束
this.PrivateLayer.InputWeights。值=价值;
结束
函数val = get.InputWeightsInitializer(这)
如果iIsCustomInitializer (this.PrivateLayer.InputWeights.Initializer)
val = this.PrivateLayer.InputWeights.Initializer.Fcn;
其他的
val = this.PrivateLayer.InputWeights.Initializer.Name;
结束
结束
函数这= set.InputWeightsInitializer(这个值)
价值= iAssertValidWeightsInitializer(价值,“InputWeightsInitializer”);
%创建的初始化和指数的权重
%大小:3 * NumHiddenUnits-by-InputSize
this.PrivateLayer.InputWeights。初始值设定项=
iInitializerFactory(价值2 1);
结束
函数val = get.RecurrentWeights(这)
val = this.PrivateLayer.RecurrentWeights.HostValue;
如果isa (val,“dlarray”)
val = extractdata (val);
结束
结束
函数这= set.RecurrentWeights(这个值)
属性= {“大小”(3 *。NumHiddenUnits this.NumHiddenUnits),
“真实”的,“nonsparse”};
值= iGatherAndValidateParameter(价值、属性);
this.PrivateLayer.RecurrentWeights。值=价值;
结束
函数val = get.RecurrentWeightsInitializer(这)
如果iIsCustomInitializer (this.PrivateLayer.RecurrentWeights.Initializer)
val = this.PrivateLayer.RecurrentWeights.Initializer.Fcn;
其他的
val = this.PrivateLayer.RecurrentWeights.Initializer.Name;
结束
结束
函数这= set.RecurrentWeightsInitializer(这个值)
价值= iAssertValidWeightsInitializer(价值,“RecurrentWeightsInitializer”);
%创建的初始化和指数的权重
%大小:3 * NumHiddenUnits-by-NumHiddenUnits
this.PrivateLayer.RecurrentWeights。初始值设定项=
iInitializerFactory(价值2 1);
结束
函数val = get.Bias(这)
val = this.PrivateLayer.Bias.HostValue;
如果isa (val,“dlarray”)
val = extractdata (val);
结束
结束
函数这= set.Bias(这个值)
biasnrowfactor = 1 +双(isequal (this.ResetGateMode,
“recurrent-bias-after-multiplication”));
属性= {“列”,“真实”的,“nonsparse”};
值= iGatherAndValidateParameter(价值、属性);
expectedSize = 3 * biasnrowfactor * this.NumHiddenUnits;
%有效输入值是空的或者有大小
% 3 * NumHiddenUnits,如果“ResetGateMode”
%’after-multiplication’或‘before-multiplication’,或
% 6 * NumHiddenUnits,如果“ResetGateMode”
%的“recurrent-bias-after-multiplication”。
如果长度(值)~ = expectedSize & & ~ isequal(价值,[])
错误消息(“nnet_cnn:层:GRULayer: BiasSize,
3 * biasnrowfactor this.ResetGateMode));
结束
this.PrivateLayer.Bias。值=价值;
结束
函数val = get.BiasInitializer(这)
如果iIsCustomInitializer (this.PrivateLayer.Bias.Initializer)
val = this.PrivateLayer.Bias.Initializer.Fcn;
其他的
val = this.PrivateLayer.Bias.Initializer.Name;
结束
结束
函数这= set.BiasInitializer(这个值)
值= iAssertValidBiasInitializer(价值);
%偏差初始化需要知道哪些复发类型
this.PrivateLayer.Bias。初始值设定项=iInitializerFactory(value,
“格勒乌”);
结束
函数val = get.HiddenState(这)
val =收集(this.PrivateLayer.HiddenState.Value);
结束
函数这= set.HiddenState(这个值)
价值= iGatherAndValidateParameter(价值,“默认”,(这一点。NumHiddenUnits 1]);
this.PrivateLayer。InitialHiddenState =价值;
this.PrivateLayer.HiddenState。值=价值;
结束
函数val = get.InputWeightsLearnRateFactor(这)
val = this.getFactor (this.PrivateLayer.InputWeights.LearnRateFactor);
结束
函数这= set.InputWeightsLearnRateFactor(这个,val)
val =收集(val);
iAssertValidFactor (val)
this.PrivateLayer.InputWeights。LearnRateFactor = this.setFactor (val);
结束
函数val = get.InputWeightsL2Factor(这)
val = this.getFactor (this.PrivateLayer.InputWeights.L2Factor);
结束
函数这= set.InputWeightsL2Factor(这个,val)
val =收集(val);
iAssertValidFactor (val)
this.PrivateLayer.InputWeights。L2Factor = this.setFactor (val);
结束
函数val = get.RecurrentWeightsLearnRateFactor(这)
val = this.getFactor (this.PrivateLayer.RecurrentWeights.LearnRateFactor);
结束
函数这= set.RecurrentWeightsLearnRateFactor(这个,val)
val =收集(val);
iAssertValidFactor (val)
this.PrivateLayer.RecurrentWeights。LearnRateFactor = this.setFactor (val);
结束
函数val = get.RecurrentWeightsL2Factor(这)
val = this.getFactor (this.PrivateLayer.RecurrentWeights.L2Factor);
结束
函数这= set.RecurrentWeightsL2Factor(这个,val)
val =收集(val);
iAssertValidFactor (val)
this.PrivateLayer.RecurrentWeights。L2Factor = this.setFactor (val);
结束
函数val = get.BiasLearnRateFactor(这)
val = this.getFactor (this.PrivateLayer.Bias.LearnRateFactor);
结束
函数这= set.BiasLearnRateFactor(这个,val)
val =收集(val);
iAssertValidFactor (val)
this.PrivateLayer.Bias。LearnRateFactor = this.setFactor (val);
结束
函数val = get.BiasL2Factor(这)
val = this.getFactor (this.PrivateLayer.Bias.L2Factor);
结束
函数这= set.BiasL2Factor(这个,val)
val =收集(val);
iAssertValidFactor (val)
this.PrivateLayer.Bias。L2Factor = this.setFactor (val);
结束
函数val = get.OutputSize(这)
val = this.NumHiddenUnits;
结束
函数val = get.OutputState(这)
val = this.HiddenState;
结束
函数= saveobj(这)
privateLayer = this.PrivateLayer;
出去了。Version = 1.0;
出去了。的名字= privateLayer.Name;
出去了。InputSize = privateLayer.InputSize;
出去了。NumHiddenUnits = privateLayer.HiddenSize;
出去了。ReturnSequence = privateLayer.ReturnSequence;
出去了。ResetGateMode = privateLayer.ResetGateMode;
出去了。StateActivationFunction = privateLayer.Activation;
出去了。GateActivationFunction = privateLayer.RecurrentActivation;
出去了。InputWeights = toStruct (privateLayer.InputWeights);
出去了。RecurrentWeights = toStruct (privateLayer.RecurrentWeights);
出去了。偏见= toStruct(privateLayer.Bias);
出去了。HiddenState = toStruct (privateLayer.HiddenState);
出去了。InitialHiddenState =收集(privateLayer.InitialHiddenState);
结束
结束
方法(静态)
函数inputArguments = parseInputArguments(变长度输入宗量)
解析器= iCreateParser ();
parser.parse(变长度输入宗量{:});
inputArguments = iConvertToCanonicalForm(解析器);
inputArguments。InputSize = [];
结束
函数这= loadobj(中)
internalLayer = nnet.internal.cnn.layer.GRU (in.Name,
in.InputSize,
in.NumHiddenUnits,
真的,
in.ReturnSequence,
in.StateActivationFunction,
in.GateActivationFunction,
在。ResetGateMode);
internalLayer。InputWeights= nnet.internal.cnn.layer.learnable.PredictionLearnableParameter.fromStruct(in.InputWeights);
internalLayer。RecurrentWeights = nnet.internal.cnn.layer.learnable.PredictionLearnableParameter.fromStruct (in.RecurrentWeights);
internalLayer。偏见= nnet.internal.cnn.layer.learnable.PredictionLearnableParameter.fromStruct(in.Bias);
internalLayer。HiddenState= nnet.internal.cnn.layer.dynamic.TrainingDynamicParameter.fromStruct(in.HiddenState);
internalLayer。InitialHiddenState = in.InitialHiddenState;
这= nnet.cnn.layer.GRULayer (internalLayer);
结束
结束
方法(隐藏访问=保护)
函数(描述、类型)= getOneLineDisplay (obj)
描述= iGetMessageString (
“nnet_cnn:层:GRULayer: oneLineDisplay,
num2str (obj.NumHiddenUnits));
类型= iGetMessageString (“nnet_cnn:层:GRULayer:类型”);
结束
函数组= getPropertyGroups(这)
generalParameters = {“名字”};
hyperParameters = {“InputSize”,
“NumHiddenUnits”,
“OutputMode”,
“StateActivationFunction”,
“GateActivationFunction”,
“ResetGateMode”};
learnableParameters = {“InputWeights”,
“RecurrentWeights”,
“偏见”};
stateParameters = {“HiddenState”};
组= [
这一点。propertyGroupGeneral (generalParameters)
这一点。propertyGroupHyperparameters (hyperParameters)
这一点。propertyGroupLearnableParameters (learnableParameters)
这一点。propertyGroupDynamicParameters (stateParameters)
];
结束
函数页脚= getFooter(这)
variableName = inputname (1);
页脚=。createShowAllPropertiesFooter (variableName);
结束
函数val = getFactor (val)
如果isscalar (val)
%不需要操作
elseif元素个数(val) = = (3 * this.NumHiddenUnits)
val = val (1: this.NumHiddenUnits:结束);
val =瓦尔(:)”;
其他的
%的错误——因素不正确的大小
结束
结束
函数val = setFactor (val)
如果isscalar (val)
%不需要操作
elseif元素个数(val) = = 3
%三元素向量扩展成3 * NumHiddenUnits-by-1
%列向量
expandedValues = repelem (val,这。NumHiddenUnits);
val = expandedValues (:);
其他的
%的错误——因素不正确的大小
结束
结束
结束
结束
函数messageString = iGetMessageString(变长度输入宗量)
messageString = getString(消息(变长度输入宗量{:}));
结束
函数p = iCreateParser ()
p = inputParser;
defaultName =;
defaultOutputMode =“序列”;
defaultStateActivationFunction =的双曲正切;
defaultGateActivationFunction =“乙状结肠”;
defaultWeightLearnRateFactor = 1;
defaultBiasLearnRateFactor = 1;
defaultWeightL2Factor = 1;
defaultBiasL2Factor = 0;
defaultInputWeightsInitializer =“glorot”;
defaultRecurrentWeightsInitializer =“正交”;
defaultBiasInitializer =“零”;
defaultLearnable = [];
defaultState = [];
defaultResetGateMode =“after-multiplication”;
p.addRequired (“NumHiddenUnits”@ (x) validateattributes (x, {“数字”},{“标量”,“积极”,“整数”}));
p.addParameter (“名字”、defaultName @nnet.internal.cnn.layer.paramvalidation.validateLayerName);
p.addParameter (“OutputMode”、defaultOutputMode @ (x)任何(iAssertAndReturnValidOutputMode (x)));
p.addParameter (“StateActivationFunction”、defaultStateActivationFunction @ (x)任何(iAssertAndReturnValidStateActivation (x)));
p.addParameter (“GateActivationFunction”、defaultGateActivationFunction @ (x)任何(iAssertAndReturnValidGateActivation (x)));
p.addParameter (“InputWeightsLearnRateFactor”defaultWeightLearnRateFactor, @ (x) iAssertValidFactor (x));
p.addParameter (“RecurrentWeightsLearnRateFactor”defaultWeightLearnRateFactor, @ (x) iAssertValidFactor (x));
p.addParameter (“BiasLearnRateFactor”defaultBiasLearnRateFactor, @ (x) iAssertValidFactor (x));
p.addParameter (“InputWeightsL2Factor”defaultWeightL2Factor, @ (x) iAssertValidFactor (x));
p.addParameter (“RecurrentWeightsL2Factor”defaultWeightL2Factor, @ (x) iAssertValidFactor (x));
p.addParameter (“BiasL2Factor”defaultBiasL2Factor, @ (x) iAssertValidFactor (x));
p.addParameter (“InputWeightsInitializer”,defaultInputWeightsInitializer);
p.addParameter (“RecurrentWeightsInitializer”,defaultRecurrentWeightsInitializer);
p.addParameter (“BiasInitializer”,defaultBiasInitializer);
p.addParameter (“InputWeights”,defaultLearnable);
p.addParameter (“RecurrentWeights”,defaultLearnable);
p.addParameter (“偏见”,defaultLearnable);
p.addParameter (“HiddenState”,defaultState);
p.addParameter (“ResetGateMode”、defaultResetGateMode @ (x)任何(iAssertAndReturnValidResetGateMode (x)));
结束
函数inputArguments = iConvertToCanonicalForm(解析)
结果= parser.Results;
inputArguments =结构;
inputArguments。NumHiddenUnits= double( results.NumHiddenUnits );
inputArguments。的名字= convertStringsToChars(results.Name);
inputArguments。OutputMode= iAssertAndReturnValidOutputMode(results.OutputMode);
inputArguments。StateActivationFunction = iAssertAndReturnValidStateActivation (convertStringsToChars (results.StateActivationFunction));
inputArguments。GateActivationFunction = iAssertAndReturnValidGateActivation (convertStringsToChars (results.GateActivationFunction));
inputArguments。InputWeightsLearnRateFactor = results.InputWeightsLearnRateFactor;
inputArguments。RecurrentWeightsLearnRateFactor = results.RecurrentWeightsLearnRateFactor;
inputArguments。偏见LearnRateFactor = results.BiasLearnRateFactor;
inputArguments。InputWeightsL2Factor = results.InputWeightsL2Factor;
inputArguments。RecurrentWeightsL2Factor = results.RecurrentWeightsL2Factor;
inputArguments。BiasL2Factor= results.BiasL2Factor;
inputArguments。InputWeights初始值设定项=results.InputWeightsInitializer;
inputArguments。RecurrentWeightsInitializer = results.RecurrentWeightsInitializer;
inputArguments。BiasInitializer= results.BiasInitializer;
inputArguments。InputWeights= results.InputWeights;
inputArguments。RecurrentWeights = results.RecurrentWeights;
inputArguments。偏见= results.Bias;
inputArguments。HiddenState= results.HiddenState;
inputArguments。ResetGateMode= iAssertAndReturnValidResetGateMode(results.ResetGateMode);
结束
函数模式= iGetOutputMode (tf)
如果特遣部队
模式=“序列”;
其他的
模式=“最后一次”;
结束
结束
函数iCheckFactorDimensions(值)
昏暗的=元素个数(价值);
如果~(昏暗的= = 1 | |昏暗的= = 3)
异常= MException(消息(“nnet_cnn:层:GRULayer: InvalidFactor));
throwAsCaller(异常);
结束
结束
函数validString = iAssertAndReturnValidOutputMode(值)
validString = validatestring(价值,{“序列”,“最后一次”});
结束
函数validString = iAssertAndReturnValidStateActivation(值)
validString = validatestring(价值,{的双曲正切,“softsign”});
结束
函数validString = iAssertAndReturnValidGateActivation(值)
validString = validatestring(价值,{“乙状结肠”,的双曲正切,“hard-sigmoid”,“radbasn”});
结束
函数iAssertValidFactor(值)
validateattributes(价值,{“数字”},{“向量”,“真实”的,非负的,“有限”});
结束
函数值= iAssertValidWeightsInitializer(价值,名称)
validateattributes(价值,{“function_handle”,“字符”,“字符串”},{});
如果(ischar(价值)| | isstring(值))
价值= validatestring(价值,{“narrow-normal”,
“glorot”,
“他”,
“正交”,
“零”,
“的”},、名称);
结束
结束
函数值= iAssertValidBiasInitializer(值)
validateattributes(价值,{“function_handle”,“字符”,“字符串”},{});
如果(ischar(价值)| | isstring(值))
价值= validatestring(价值,{“零”,
“narrow-normal”,
“的”});
结束
结束
函数初始值设定项= iInitializerFactory(变长度输入宗量)
初始值设定项= nnet.internal.cnn.layer.learnable.initializer
.initializerFactory(变长度输入宗量{:});
结束
函数tf = iIsCustomInitializer (init)
tf = isa(初始化,“nnet.internal.cnn.layer.learnable.initializer.Custom”);
结束
函数iAssertValidLayerName(名字)
iEvalAndThrow (@ ()
nnet.internal.cnn.layer.paramvalidation.validateLayerName(名称));
结束
函数iEvalAndThrow(函数)
%省略堆栈包含内部函数将调用者
试一试
func ();
异常
throwAsCaller(异常)
结束
结束
函数值= iGatherAndValidateParameter(变长度输入宗量)
试一试
值= nnet.internal.cnn.layer.paramvalidation
.gatherAndValidateNumericParameter(变长度输入宗量{:});
异常
throwAsCaller(异常)
结束
结束
函数值= iAssertAndReturnValidResetGateMode(值)
价值= validatestring(价值,{“after-multiplication”,“before-multiplication”,“recurrent-bias-after-multiplication”});
结束
函数dlX = iMakeSizeOnlyArray(变长度输入宗量)
dlX = deep.internal.PlaceholderArray(变长度输入宗量{:});
结束

答案(1)

本
2023年3月13日
我建议实施这一扩展格勒乌层作为一个自定义层下面这个例子:
你可以跟随你发现的代码 gruForwardGeneral 来做到这一点。
不建议您尝试直接修改源代码。

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!