Bug encountered When running Google's Deep Q Network Code - lua

Google's Deep Q Network for Atari Games is here.
https://github.com/rahular/deepmind-dqn
When I run it with GPU setting
./run_gpu <game name>
I had this error
../torch/bin/luajit: ./convnet.lua:22: attempt to call local 'convLayer' (a nil value)
stack traceback:
./convnet.lua:22: in function 'network'
./NeuralQLearner.lua:89: in function '__init'
...einforcement_Learning/torch/share/lua/5.1/torch/init.lua:51: in function <...einforcement_Learning/torch/share/lua/5.1/torch/init.lua:47>
[C]: at 0x7f419423d380
./initenv.lua:133: in function 'setup'
train_agent.lua:52: in main chunk
[C]: at 0x00406230
The code that caused this issue is in this file https://github.com/rahular/deepmind-dqn/blob/master/dqn/convnet.lua
and it is in this function
function create_network(args)
local net = nn.Sequential()
net:add(nn.Reshape(unpack(args.input_dims)))
--- first convolutional layer
local convLayer = nn.SpatialConvolution
if args.gpu >= 0 then
net:add(nn.Transpose({1,2},{2,3},{3,4}))
convLayer = nn.SpatialConvolutionCUDA
end
net:add(convLayer(args.hist_len*args.ncols, args.n_units[1],
args.filter_size[1], args.filter_size[1],
args.filter_stride[1], args.filter_stride[1],1))
net:add(args.nl())
The net:add(convLayer( is 22th line.
I used gpu setting so it seems
convLayer = nn.SpatialConvolutionCUDA
caused convLayer to be nil.
Does anyone know why nn.SpatialConvolutionCUDA returns a nil ?

Did the code originally come with GPU support, or did you add it yourself?
You should replaced the depreceated layers, i.e. replace:
net:add(nn.Transpose({1,2},{2,3},{3,4}))
convLayer = nn.SpatialConvolutionCUDA
with
convLayer = nn.SpatialConvolution
Check the documentation for the layers.
Edit: Use this branch, I fixed it for GPU support.

Found the solution.
using this github branch
https://github.com/soumith/deepmind-atari
After cloning this branch, then install cutorch and cunn using luarocks.
Now you can run the code.

Related

Mediawiki scribunto lua module do not know builtin functions

I am having a problem with calling Lua built-in functions using Scribunto.
I created basic module Module:Item
local p = {};
function p.test(frame)
print("Hello World!")
end
return p
Which I call in different page as {{#invoke: Item | test}}
and I receive a following error:
Lua error in Module:Item at line 3: attempt to call global 'print' (a nil value).
Backtrace:
1. (tail call): ?
2. Module:Item:3: in function "chunk"
3. mw.lua:511: ?
4. (tail call): ?
5. [C]: in function "xpcall"
6. MWServer.lua:99: in function "handleCall"
7. MWServer.lua:313: in function "dispatch"
8. MWServer.lua:52: in function "execute"
9. mw_main.lua:7: in main chunk
10. [C]: ?
Since print is Lua built-in function I have the feeling the problem will be somewhere in setting on the pc.
However, when I imported wiki Infoboxes, they are working OK.
Versions:
Linux Mint Tara - Cinnamon based on ubuntu 18
MediaWiki 1.31.7
Scribunto (106fbf4) 17:24, 15 May 2018
Lua 5.1.5
Any help pointing where the problem can be is highly appreciated.
Scribunto intentionally doesn't include print. The "Removed functions and packages" section in its manual says this about it:
This was discussed on wikitech-l and it was decided that it should be omitted in favour of return values, to improve code quality. If necessary, mw.log() may be used to output information to the debug console.

SimilarityMeasure is an invalid module name

This is a follow-up to my earlier question Lua: Semantic Similarity using Neural Networks.
For Semantic similarity I've execute the following code,
include('Conv.lua')
modelTrained = torch.load("download_local_location/modelSTS.trained.th", 'ascii')
modelTrained.convModel:evaluate()
modelTrained.softMaxC:evaluate()
local linputs = torch.zeros(rigth_sentence_length, emd_dimension)
linpus = XassignEmbeddingValuesX
local rinputs = torch.zeros(left_sentence_length, emd_dimension)
rinpus = XassignEmbeddingValuesX
local part2 = modelTrained.convModel:forward({linputs, rinputs})
local output = modelTrained.softMaxC:forward(part2)
local val = torch.range(0, 5, 1):dot(output:exp())
return val/5
And Execute it using the following command in terminal,
> th similarity.lua
But the error it displays is,
while creating metatable similarityMeasure.Conv: bad argument #1 (similarityMeasure is an invalid module name)
stack traceback:
[C]: in function 'newmetatable'
/torch/install/share/lua/5.2/torch/init.lua:102: in function 'class'
.../textSimilarityConvNet-master/Conv.lua:1: in main chunk
[C]: in function 'dofile'
/torch/install/share/lua/5.2/paths/init.lua:84: in function 'dofile'
/torch/install/share/lua/5.2/torch/init.lua:49: in function 'include'
similarity.lua:1: in main chunk
[C]: in function 'dofile'
.../torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:150: in main chunk
[C]: in ?
First few lines in Conv.lua is,
local Conv = torch.class('similarityMeasure.Conv')
function Conv:__init(config)
self.mem_dim = config.mem_dim or 150
self.learning_rate = config.learning_rate or 0.01
self.batch_size = config.batch_size or 1 --25
self.num_layers = config.num_layers or 1
self.reg = config.reg or 1e-4
self.structure = config.structure or 'lstm' -- {lstm, bilstm}
self.sim_nhidden = config.sim_nhidden or 150
self.task = config.task or 'sic' -- or 'vid'
-- word embedding
self.emb_vecs = config.emb_vecs
self.emb_dim = config.emb_vecs:size(2)
Please guide me to solve this.
You are missing the similarityMeasure module (and likely other modules as well). I'm guessing that the things you are missing are dependencies that are not included with the default Torch installation. The installation instructions in the Github respository's readme file say the following:
Please install Torch deep learning library. We recommend this local
installation which includes all required packages our tool needs,
simply follow the instructions here: https://github.com/torch/distro
If you installed Torch some other way, try doing it using the linked distro instead and see if that fixes the problem.
It should work. Also the repo has been recently updated.
https://github.com/castorini/MP-CNN-Torch
Once you installed the torch library properly, then you can check and see the newly added/provided testDeployment.lua file, as an example to see how to use the trained model properly.

Lib graphviz cannot be loaded when using itorch

I'm learning the torch following the tutorials provided by torch team. When I typed lines below as provided by 'NNGraph Tutorial', sth unexpected happened:
-- it is common style to mark inputs with identity nodes for clarity.
input = nn.Identity()()
-- each hidden layer is achieved by connecting the previous one
-- here we define a single hidden layer network
h1 = nn.Tanh()(nn.Linear(20, 10)(input))
output = nn.Linear(10, 1)(h1)
mlp = nn.gModule({input}, {output})
x = torch.rand(20)
dx = torch.rand(1)
mlp:updateOutput(x)
mlp:updateGradInput(x, dx)
mlp:accGradParameters(x, dx)
-- draw graph (the forward graph, '.fg')
-- this will produce an SVG in the runtime directory
graph.dot(mlp.fg, 'MLP', 'MLP')
itorch.image('MLP.svg')
Error information:
...s/noahcao/torch/install/share/lua/5.2/graph/graphviz.lua:157: graphviz library could not be loaded.
stack traceback:
/Users/noahcao/torch/install/share/lua/5.2/itorch/main.lua:167: in function </Users/noahcao/torch/install/share/lua/5.2/itorch/main.lua:160>
[C]: in function 'error'
...s/noahcao/torch/install/share/lua/5.2/graph/graphviz.lua:157: in function 'graphvizFile'
...s/noahcao/torch/install/share/lua/5.2/graph/graphviz.lua:197: in function 'dot'
[string "-- it is common style to mark inputs with ide..."]:18: in main chunk
[C]: in function 'xpcall'
/Users/noahcao/torch/install/share/lua/5.2/itorch/main.lua:210: in function </Users/noahcao/torch/install/share/lua/5.2/itorch/main.lua:174>
(...tail calls...)
/Users/noahcao/torch/install/share/lua/5.2/lzmq/poller.lua:75: in function 'poll'
...s/noahcao/torch/install/share/lua/5.2/lzmq/impl/loop.lua:307: in function 'poll'
...s/noahcao/torch/install/share/lua/5.2/lzmq/impl/loop.lua:325: in function 'sleep_ex'
...s/noahcao/torch/install/share/lua/5.2/lzmq/impl/loop.lua:370: in function 'start'
/Users/noahcao/torch/install/share/lua/5.2/itorch/main.lua:389: in main chunk
[C]: in function 'require'
(command line):1: in main chunk
[C]: in ?
But I checked the graphviz.lua file, that seems OK. I couldn't understand what the information wants to tell me.
I have also faced the same issue. This solved my issue:
# Mac users
brew install graphviz
# Debian/Ubuntu users
sudo apt-get install graphviz -y
Hope this might help someone.
Please check this link for Graph package of Torch.

Issue with neuralnetwork_turial.lua with data preprocessing

I have installed the torch deep learning module by first git clone-ing and later using luarocks make and the installation was succussful. The require 'dp' works well in the torch prompt.
But when I try to execute the neuralnetwork_tutorial.lua(th neuralnetwork_tutorial.lua), it throws the following errors.
Tanny #neuralnetwork_tutorial.lua: About to initiate: datasource = dp.Mnist{input_preprocess = dp.Standardize()}
Tanny #/home/ubuntu/binaries/torches/torch/install/share/lua/5.1/dp/preprocess/standardize.lua: Marked presence!!!
Tanny #/home/ubuntu/binaries/torches/torch/install/share/lua/5.1/dp/torch/File.lua says: #177 typeidx= 3
Tanny #/home/ubuntu/binaries/torches/torch/install/share/lua/5.1/dp/torch/File.lua says: #177 typeidx= 1
Tanny #/home/ubuntu/binaries/torches/torch/install/share/lua/5.1/dp/torch/File.lua says: #177 typeidx= 4
Tanny #/home/ubuntu/binaries/torches/torch/install/share/lua/5.1/dp/torch/File.lua says: #177 typeidx= 0
Tanny #/home/ubuntu/binaries/torches/torch/install/share/lua/5.1/dp/torch/File.lua says: #177 typeidx= 28
Tanny #/home/ubuntu/binaries/torches/torch/install/share/lua/5.1/dp/torch/File.lua says: #259 typeidx= 28
/home/ubuntu/binaries/torches/torch/install/bin/luajit: ...aries/torches/torch/install/share/lua/5.1/torch/File.lua:260: unknown object
stack traceback:
[C]: in function 'error'
...aries/torches/torch/install/share/lua/5.1/torch/File.lua:260: in function 'readObject'
...aries/torches/torch/install/share/lua/5.1/torch/File.lua:252: in function 'readObject'
...aries/torches/torch/install/share/lua/5.1/torch/File.lua:277: in function 'loadData'
...es/torches/torch/install/share/lua/5.1/dp/data/mnist.lua:74: in function 'loadTrainValid'
...es/torches/torch/install/share/lua/5.1/dp/data/mnist.lua:61: in function '__init'
...aries/torches/torch/install/share/lua/5.1/torch/init.lua:50: in function <...aries/torches/torch/install/share/lua/5.1/torch/init.lua:46>
[C]: in function 'Mnist'
neuralnetwork_tutorial.lua:16: in main chunk
[C]: in function 'dofile'
...ches/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:131: in main chunk
[C]: at 0x0804d650
I put some print statements in those scripts to understand the flow. I happen to notice that in File.lua the first step after getting the object is to determine the type of the object; of which 8 have been declared. The types have been declared through 0 to 7, 0 being TYPE_NIL. However the code fails, as it detects a type 28(??).
Kindly any help where I am going wrong? Or where to look into to find the issue?
P.S.: The script downloads the data on its own, however due to certain standard corporate proxy setting issues, it could not download. Therefore, I personally downloaded the data MNIST and stored it in the specific data directory. If this could be a clue??
Okay, so it was a bug in the code (serialized MNIST wasn't cross-platform). Fixed by serializing dataset using ascii format instead of binary.

Lua: How to call error without stack trace

I'm using Lua to parse scripts written in some language (let's call it L) and create Lua-code that can be run by e.g. LuaJIT. But to simplify debugging for the users, I want to map the run time errors given by Lua/LuaJIT to the correct line in the L-files. I do this by xpcalling the created Lua-code, translating the error message and stacktrace and then calling error with this message. Unfortunately this gives me two stack traces, one created by me and one tracing back to the function that called error. Is it possible to get rid of this stack trace, or is there some better way of doing this?
local status, err = xpcall(loadedCode, debug.traceback)
if not status then
error(createANewErrorMessageWithPrettyTraceback(err),0)
end
Output:
luajit: ./my/file.name:5: Some error message
stack traceback:
my pretty traceback
stack traceback:
[C]: in function 'error'
./my/file/calling/error.lua:44: in function <./my/file/calling/error.lua:26>
./my-main:16: in main chunk
[C]: at 0x00404180
I know that e.g. Moonscript does something similar to this, but as far as I can see they just write the new error message to stderr and then continues as normal, instead of stopping the program which is what I want to do.
There is a possibility of doing this and then calling error with no arguments, which will make the program fail (actually I think it's error that fails), but this feels like quite an ugly solution, so I'll rather keep the stupid second trace than doing that.
PS: I assume what the title asks actually doesn't work (as error only takes two arguments), so what I'm actually asking is more how something like this can be achieved. (Are there other functions that do similar things perhaps, or where I should look to figure out how to write that function myself.)
Edit: Is it perhaps possible to edit the function that error's using to get its traceback, as it is with debug.traceback?
I wanted to do something similar (only from Lua directly) and I ended up overwriting debug.traceback function itself to change the stack trace to suit my needs. My code is below; see if this method works for you as well:
local dtraceback = debug.traceback
debug.traceback = function (...)
if select('#', ...) >= 1 then
local err, lvl = ...
if err and type(err) ~= 'thread' then
local trace = dtraceback(err, (lvl or 2)+1)
if genv.print == iobase.print then -- no remote redirect
return trace
else
genv.print(trace) -- report the error remotely
return -- don't report locally to avoid double reporting
end
end
end
-- direct call to debug.traceback: return the original.
-- debug.traceback(nil, level) doesn't work in Lua 5.1
-- (http://lua-users.org/lists/lua-l/2011-06/msg00574.html), so
-- simply remove first frame from the stack trace
return (dtraceback(...):gsub("(stack traceback:\n)[^\n]*\n", "%1"))
end
You could simply display the modified traceback that you want and exit.
local function errh(err)
print(createANewErrorMessageWithPrettyTraceback(debug.traceback(err, 2)))
os.exit(-1) -- error code
end
local status, result = xpcall(loadedCode, errh)
-- The script will never reach this point if there is an error.
print(result)

Resources