Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick test implementation of 'First Past' lattice.
- Loading branch information
Sam Bowman
committed
May 12, 2015
1 parent
1340534
commit 17f09b0
Showing
12 changed files
with
121 additions
and
42 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function [ weights, correctWeight, loss ] = ComputeFirstPast(scores, threshold, labels, multipliers, hardMax) | ||
|
||
% Sigmoid to get a bounded range. | ||
scores = Sigmoid(scores); | ||
|
||
% Lower the threshold if nothing passes it. | ||
effectiveThreshold = min([repmat(threshold, size(scores, 2)); max(scores, [], 1)]); | ||
|
||
% Choose the first entry to pass the threshold. | ||
[ ~, bestIndex ] = max(scores >= repmat(effectiveThreshold, size(scores, 1), 1), [], 1); | ||
|
||
loss = zeros(size(scores, 2), 1, 'like', scores); | ||
paddedScores = [zeros(1, size(scores, 2)); scores]; | ||
for b = 1:size(scores, 2) | ||
loss(b) = max(0, threshold - paddedScores(labels(b) + 1, b)); | ||
|
||
if labels(b) > 1 | ||
competingScores = scores(1:labels(b) - 1, b); | ||
loss(b) = loss(b) + ... | ||
sum(competingScores(competingScores >= repmat(threshold, labels(b) - 1, 1)) - threshold); | ||
end | ||
end | ||
|
||
weights = zeros(size(scores), 'like', scores); | ||
if hardMax | ||
weights(sub2ind(size(scores), bestIndex, 1:size(scores, 2))) = 1; | ||
else | ||
weights(sub2ind(size(scores), bestIndex, 1:size(scores, 2))) = scores(sub2ind(size(scores), bestIndex, 1:size(scores, 2))); | ||
end | ||
|
||
paddedWeights = [zeros(1, size(weights, 2)); weights]; | ||
correctWeight = paddedWeights(sub2ind(size(paddedWeights), labels' + 1, 1:length(labels))); | ||
|
||
loss = loss .* multipliers; | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function [ deltas ] = ComputeFirstPastGradient(scores, threshold, weights, deltasIn, labels, multipliers, hardMax) | ||
|
||
% TODO: Store this. | ||
scores = Sigmoid(scores); | ||
|
||
deltas = zeros(size(deltasIn), 'like', deltasIn); | ||
|
||
% Looping for now during development. | ||
for b = 1:size(scores, 2) | ||
if labels(b) > 0 | ||
deltas(labels(b), b) = deltas(labels(b), b) - (scores(labels(b), b) < threshold); | ||
deltas(1:labels(b) - 1, b) = deltas(1:labels(b) - 1, b) + (scores(1:labels(b) - 1, b) >= repmat(threshold, labels(b) - 1, 1)); | ||
end | ||
end | ||
|
||
deltas = bsxfun(@times, deltas, multipliers'); | ||
|
||
if ~hardMax | ||
deltas = deltas + deltasIn .* (weights > 0); | ||
end | ||
|
||
deltas = deltas .* SigmoidDeriv([], scores); | ||
|
||
end |
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
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