Beam Search Function Error
Show older comments
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
Answers (1)
Image Analyst
on 5 Nov 2022
0 votes
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.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!