Created
January 20, 2015 16:06
-
-
Save denizyuret/b937f380be8270026cd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function partest2(model, corpus, batchsize) | |
batches = ceil(numel(corpus)/batchsize); | |
for batch=1:batches | |
% Initialize a batch of sentences and parsers: | |
bstart = (batch-1)*batchsize+1; | |
bend = min(batch*batchsize, numel(corpus)); | |
sentences = corpus(bstart:bend); | |
nsentences = numel(sentences); | |
parsers = cell(1, nsentences); | |
for i=1:nsentences | |
parsers{i} = initparser(model, sentences{i}); | |
end | |
% Initialize dimensions and pre-allocate arrays: | |
nmove = numel(validmoves(parsers{1})); | |
ndims = numel(getfeatures(parsers{1}, sentences{1}, model)); | |
valid = false(nmove, nsentences); | |
x = zeros(ndims, nsentences); | |
% Initialize the number and indices of unfinished sentences: | |
nvalid = nsentences; | |
ivalid = 1:nsentences; | |
while 1 | |
% Update valid moves and unfinished sentences: | |
for j=1:nvalid | |
i = ivalid(j); | |
valid(:,i) = validmoves(parsers{i}); | |
end | |
ivalid = find(any(valid)); | |
nvalid = numel(ivalid); | |
if nvalid == 0 break; end | |
% Calculate features for unfinished sentences: | |
for j=1:nvalid | |
i = ivalid(j); | |
x(:,j) = getfeatures(parsers{i}, sentences{i}, model); | |
end | |
% Calculate scores and execute moves: | |
scores = predict(model, x(:,1:nvalid)); | |
scores(~valid(:,ivalid)) = -inf; | |
[~,moves] = max(scores); | |
for j=1:nvalid | |
i = ivalid(j); | |
transition(parsers{i}, moves(j)); | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment