-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from nicholas-leonard/treemax
Treemax
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
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,6 @@ | ||
require 'torch' | ||
|
||
torchx = {} | ||
|
||
torch.include('torchx', 'treemax.lua') | ||
torch.include('torchx', 'test.lua') |
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,33 @@ | ||
local treeMaxBuffer = {} | ||
function torch.treemax(tensor, treeSize) | ||
assert(torch.type(treeSize) == 'table') | ||
assert(tensor:dim() == 1) | ||
local tmb = treeMaxBuffer[torch.type(tensor)] -- upvalue | ||
if not tmb then | ||
tmb = { | ||
mean = tensor.new(), | ||
max = tensor.new(), | ||
idx = torch.LongTensor() | ||
} | ||
treeMaxBuffer[torch.type(tensor)] = tmb | ||
end | ||
|
||
local lvl = tensor | ||
local maxIdx, maxVal = 1, 0 | ||
for i=1,#treeSize do | ||
lvl = lvl:view(treeSize[i], -1) | ||
local lvlStride = lvl:size(2) | ||
if i < #treeSize then | ||
tmb.mean:mean(lvl, 2) | ||
tmb.max:max(tmb.idx, tmb.mean:select(2,1), 1) | ||
else | ||
tmb.max:max(tmb.idx, lvl:select(2,1), 1) | ||
end | ||
|
||
local lvlMax, lvlIdx = tmb.max[1], tmb.idx[1] | ||
lvl = lvl[lvlIdx] | ||
maxIdx = maxIdx + (lvlIdx-1)*lvlStride | ||
maxVal = lvlMax | ||
end | ||
return maxVal, maxIdx | ||
end |