GLU(门控线性激活单元)
具体算法是将输入的矩阵X切割成等长的两部分,其中一部分经过sigmoid处理后与另一部分相乘。举个例子,一个size为(4,2)的array,先将其切为两个(4,1)的左右矩阵,右矩阵经过sigmoid处理后乘回左矩阵,在size上体现为input是(4,2)的矩阵经过GLU后变成(4,1)。
我在matlab2022a中自定义了GLU层,以下是代码:
classdef GLULayer < nnet.layer.Layer & nnet.layer.Acceleratable
% & nnet.layer.Formattable ... % (Optional)
% (Optional)
properties
% (Optional) Layer properties.
% Declare layer properties here.
end
properties (Learnable)
% (Optional) Layer learnable parameters.
% Declare learnable parameters here.
end
properties (State)
% (Optional) Layer state parameters.
% Declare state parameters here.
end
properties (Learnable, State)
% (Optional) Nested dlnetwork objects with both learnable
% parameters and state parameters.
% Declare nested networks with learnable and state parameters here.
end
methods
function layer = GLULayer()
% (Optional) Create a myLayer.
% This function must have the same name as the class.
layer.Name = "GLU(Gate linear units 线性门控单元)";%层的名称和介绍
layer.Description = "GLU Gate linear units 线性门控单元";
% Define layer constructor function here.
end
function [Z]= predict(layer,X)
% Forward input data through the layer at prediction time and
% output the result and updated state.
%
% Inputs:
% layer - Layer to forward propagate through
% X - Input data
% Outputs:
% Z - Output of layer forward function
% state - (Optional) Updated layer state
%
% - For layers with multiple inputs, replace X with X1,...,XN,
% where N is the number of inputs.
% - For layers with multiple outputs, replace Z with
% Z1,...,ZM, where M is the number of outputs.
% - For layers with multiple state parameters, replace state
% with state1,...,stateK, where K is the number of state
% parameters.
nc = size(X,2);%输入矩阵第二维的长度
assert(mod(size(X,2),2) == 0,'不能被2整除,请检查维数');
nc = int8(nc/2);
Z = X(:,1:nc).*sigmoid(X(:,nc+1:nc*2));
% Define layer predict function here.
end
end
需要注意的是matlab自定义层数时允许多态,如果只是想要z = GLUlayer(layer,x)这一种使用方式,需要删掉其他的多输入多输出函数方法,不然在checklayer时会不通过。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删