Replace a layer on LSTM
显示 更早的评论
Dear all,
I am trying to create a weithed LSTM to Sequence-to-sequence classification. So first I created de LSTM.
numFeatures = 1;
numHiddenUnits = 200;
numClasses = 11;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer('Name','classoutput')];
Then I created the weighted layer as I found in:
classdef weightedClassificationLayer < nnet.layer.ClassificationLayer
properties
% (Optional) Layer properties.
ClassWeights
end
methods
function layer = weightedClassificationLayer(classWeights,name)
% layer = weightedClassificationLayer(classWeights) creates a
% weighted cross entropy loss layer. classWeights is a row
% vector of weights corresponding to the classes in the order
% that they appear in the training data.
%
% layer = weightedClassificationLayer(classWeights, name)
% additionally specifies the layer name.
% Set class weights
layer.ClassWeights = classWeights;
% Set layer name
if nargin == 2
layer.Name = name;
end
% Set layer description
layer.Description = 'Weighted cross entropy';
end
function loss = forwardLoss(layer, Y, T)
% loss = forwardLoss(layer, Y, T) returns the weighted cross
% entropy loss between the predictions Y and the training
% targets T.
% Find observation and sequence dimensions of Y
[~, N, S] = size(Y);
% Reshape ClassWeights to KxNxS
W = repmat(layer.ClassWeights(:), 1, N, S);
% Compute the loss
loss = -sum( W(:).*T(:).*log(Y(:)) )/N;
end
function dLdY = backwardLoss(layer, Y, T)
% dLdY = backwardLoss(layer, Y, T) returns the derivatives of
% the weighted cross entropy loss with respect to the
% predictions Y.
% Find observation and sequence dimensions of Y
[~, N, S] = size(Y);
% Reshape ClassWeights to KxNxS
W = repmat(layer.ClassWeights(:), 1, N, S);
% Compute the derivative
dLdY = -(W.*T./Y)/N;
end
end
end
And
classWeights =[0.05 0.05 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1];
wLayer = weightedClassificationLayer(classWeights);
But when I try to replace the classification layer following the CNN example:
layers = replaceLayer(layers,"classoutput",wLayer);
It appears the error:
Check for missing argument or incorrect argument data type in call to function 'replaceLayer'
Can anyone help me?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Visualization and Interpretability 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!