Beam Search Function Error

function [words,attentionScores] = beamSearch(X,beamIndex,parametersEncoder,parametersDecoder, ...
enc,maxNumWords)
% Model dimensions
numFeatures = size(X,1);
numHiddenUnits = size(parametersDecoder.attention.Weights1,1);
% Extract features
features = modelEncoder(X,parametersEncoder);
% Initialize state
state = struct;
state.gru.HiddenState = dlarray(zeros([numHiddenUnits 1],"like",X));
% Initialize candidates
candidates = struct;
candidates.State = state;
candidates.Words = "<start>";
candidates.Score = 0;
candidates.AttentionScores = dlarray(zeros([numFeatures maxNumWords],"like",X));
candidates.StopFlag = false;
t = 0;
% Loop over words
while t < maxNumWords
t = t + 1;
candidatesNew = [];
% Loop over candidates
for i = 1:numel(candidates)
% Stop generating when stop token is predicted
if candidates(i).StopFlag
continue
end
% Candidate details
state = candidates(i).State;
words = candidates(i).Words;
score = candidates(i).Score;
attentionScores = candidates(i).AttentionScores;
% Predict next token
decoderInput = word2ind(enc,words(end));
[YPred,state,attentionScores(:,t)] = modelDecoder(decoderInput,parametersDecoder,features,state);
YPred = softmax(YPred,DataFormat="CB");
[scoresTop,idxTop] = maxk(extractdata(YPred),beamIndex);
idxTop = gather(idxTop);
% Loop over top predictions
for j = 1:beamIndex
candidate = struct;
candidateWord = ind2word(enc,idxTop(j));
candidateScore = scoresTop(j);
if candidateWord == "<stop>"
candidate.StopFlag = true;
attentionScores(:,t+1:end) = [];
else
candidate.StopFlag = false;
end
candidate.State = state;
candidate.Words = [words candidateWord];
candidate.Score = score + log(candidateScore);
candidate.AttentionScores = attentionScores;
candidatesNew = [candidatesNew candidate]; % Error in this line "The variable 'candidateNew' appears to change size on every loop iteration. Consider preallocating for speed."
end
end
% Get top candidates
[~,idx] = maxk([candidatesNew.Score],beamIndex);
candidates = candidatesNew(idx);
% Stop predicting when all candidates have stop token
if all([candidates.StopFlag])
return
end
end
% Get top candidate
words = candidates(1).Words(2:end-1); % Error in this line "Function definitions in a script must appear at the end of the file. Move statements to before the function definitons"
attentionScores = candidates(1).AttentionScores;
end

回答(1 个)

Image Analyst
Image Analyst 2022-11-5

0 个投票

You have an extra "end" in there that finishes off the function. Then the code after that it believes is a script (not the function). So it thinks you have a function (closed with an "end" statement) followed by script. If you have scripts and functions in the same m-file, you must have the script first, then all the functions afterwards, each finished off with an "end" statement.
Type control-a (to select all) then control-i (to properly indent everything. Then examine the code to see if you have too many end statements in there.

类别

帮助中心File Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by