-
Notifications
You must be signed in to change notification settings - Fork 967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initialized buffer/gradInputs memory and added tests for dice #913
Open
robotsorcerer
wants to merge
4
commits into
torch:master
Choose a base branch
from
robotsorcerer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1431ee3
initialized buffer/gradInputs memory and added tests for dice
robotsorcerer 292153d
removed accgradparams; added dice to init.lua)
robotsorcerer 6c7020b
Merge branch 'master' of https://github.com/torch/nn
robotsorcerer 731e2d1
Merge branch 'master' of https://github.com/torch/nn
robotsorcerer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
--[[ | ||
Computes the Sorensen-dice coefficient of similarity given two samples. | ||
The quotient of similarity is defined as: | ||
|
||
Q = 2 * (X n Y) | ||
------------------- | ||
sum_i(X) + sum_i(Y) | ||
where X and Y are the two samples; | ||
(X n Y) denote the intersection where the elements of X and Y are equal. | ||
|
||
Author: Olalekan Ogunmolu, July 2016 | ||
[email protected] | ||
]] | ||
|
||
local DICECriterion, parent = torch.class('nn.DICECriterion', 'nn.Criterion') | ||
|
||
local eps = 1 | ||
|
||
function DICECriterion:_init(weights) | ||
parent._init(self) | ||
|
||
if weights then | ||
assert(weights:dim() == 1, "weights input should be 1-D Tensor") | ||
self.weights = weights | ||
end | ||
|
||
end | ||
|
||
function DICECriterion:updateOutput(input, target) | ||
|
||
assert(input:nElement() == target:nElement(), "input and target size mismatch") | ||
|
||
local weights = self.weights | ||
|
||
local numerator, denom, common, output | ||
|
||
if weights ~= nil and target:dim() ~= 1 then | ||
weights = self.weights:view(1, target:size(2)):expandAs(target) | ||
end | ||
|
||
-- compute 2 * (X intersection Y) | ||
common = torch.eq(input, target) --find logical equivalence between both | ||
numerator = torch.sum(common) | ||
numerator = numerator * 2 | ||
|
||
-- compute denominator: sum_i(X) + sum_i(Y) | ||
denom = input:nElement() + target:nElement() + eps | ||
|
||
output = numerator/denom | ||
|
||
self.output = -output | ||
|
||
return self.output | ||
end | ||
|
||
function DICECriterion:updateGradInput(input, target) | ||
--[[ | ||
2 * sum_i(X) * sum_i(Y) | ||
Gradient = --------------------------------- | ||
sum_i(X)*(sum_i(X) + sum_i(Y))^2 | ||
]] | ||
|
||
assert(input:nElement() == target:nElement(), "inputs and target size mismatch") | ||
self.buffer = self.buffer or input.new() | ||
|
||
local buffer = self.buffer | ||
local weights = self.weights | ||
local gradInput = self.gradInput | ||
|
||
if weights ~= nil and target:dim() ~= 1 then | ||
weights = self.weights:view(1, target:size(2)):expandAs(target) | ||
end | ||
|
||
buffer:resizeAs(input) | ||
buffer:zero() | ||
|
||
-- compute sum_i(X) + sum_i(Y) + eps | ||
buffer:add(input:nElement()):add(target:nElement()):add(eps) | ||
-- compute (sum_i(X) + sum_i(Y) + eps )^2 + eps | ||
buffer:cmul(buffer):add(eps) | ||
-- compute sum_i(X)*(sum_i(X) + sum_i(Y) + eps )^2 + eps | ||
buffer:mul(input:nElement()) | ||
|
||
gradInput:resizeAs(input) | ||
gradInput:zero() | ||
|
||
-- compute 2 * sum_i(X) * sum_i(Y) | ||
gradInput:add(input:nElement()):mul(target:nElement()):mul(2) | ||
|
||
-- compute quotient | ||
gradInput:cdiv(buffer) | ||
|
||
if weights ~= nil then | ||
gradInput:cmul(weights) | ||
end | ||
|
||
if self.sizeAverage then | ||
gradInput:div(target:nElement()) | ||
end | ||
|
||
return gradInput | ||
end | ||
|
||
function DICECriterion:accGradParameters(input, gradOutput) | ||
end | ||
|
||
function DICECriterion:reset() | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not needed, as this is a Criterion. Criterions dont have accGradParameters