Skip to content

Instantly share code, notes, and snippets.

@ndronen
Last active August 29, 2015 14:22
Show Gist options
  • Save ndronen/a8e13e5799b8118f7cc0 to your computer and use it in GitHub Desktop.
Save ndronen/a8e13e5799b8118f7cc0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env th
local cmd = torch.CmdLine()
cmd:text('Demonstration of incompatibility between nn and fbcunn temporal convolutions.')
cmd:text()
cmd:text('Running with -fbconv causes a tensor dimension mismatch error in TemporalConvolutionFB_updateOutput.')
cmd:text()
cmd:text('I think the bug is somewhere in ConvolutionBias.cu.')
cmd:text()
cmd:text('Options:')
cmd:option('-fblookup', false, 'whether to use Facebook lookup table')
cmd:option('-fbconv', false, 'whether to use Facebook extensions')
local opt = cmd:parse(arg)
local torch = torch
if opt.fblookup or opt.fbconv then
require('fbcunn')
torch = require('fbtorch')
else
require('nn')
end
torch.manualSeed(1)
local sentLength = 5
local nWords = 1000
local wordDims = 10
local filterWidth = 2
local stride = 1
local nFilters = 5
local nClasses = 2
local target = torch.Tensor{1}
local input = torch.rand(sentLength):mul(3):add(1):round()
local model = nn.Sequential()
local criterion = nn.ClassNLLCriterion()
if opt.fblookup then
model:add(nn.LookupTableGPU(nWords, wordDims))
else
model:add(nn.LookupTable(nWords, wordDims))
end
if opt.fbconv then
model:add(nn.TemporalConvolutionFB(wordDims, nFilters, filterWidth, stride))
else
model:add(nn.TemporalConvolution(wordDims, nFilters, filterWidth, stride))
end
model:add(nn.TemporalMaxPooling(sentLength - filterWidth + 1))
model:add(nn.Tanh())
model:add(nn.Linear(nFilters, nClasses))
model:add(nn.LogSoftMax())
if opt.fblookup or opt.fbconv then
model:cuda(); criterion:cuda(); input = input:cuda(); target = target:cuda()
end
local output = model:forward(input)
local err = criterion:forward(output, target)
local df_do = criterion:backward(output, target)
model:backward(input, df_do)
print('OK')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment