Torch, how to execute a script with "dofile" and input parameters?

I run a torch script from my linux shell using the th command. This torch script accepts two input parameters:

th torch_script.lua input_parameter1 input_parameter2

Now I want to run this script through the Torch shell. To do this, I need to use the dofile command. But in this case, I do not know how to pass the input parameters input_parameter1 and input_parameter2 .

In Torch, how do I pass some input parameters to the dofile run dofile ?


EDIT : Here is the code I'm trying to run. I can't run it correctly, maybe you can tell me why

Contents of external_command.lua:

 local arg = arg or {...} input_parameter = arg[1] print("input_parameter ".. input_parameter); 

On the shell:

 $th th> tempFunc = load "external_command.lua" th> tempFunc("try") [string "_RESULT={tempFunc("try")}"]:1: attempt to call global 'tempFunc' (a nil value) stack traceback: [string "_RESULT={tempFunc("try")}"]:1: in main chunk [C]: in function 'xpcall' /home/davide/torch/install/share/lua/5.1/trepl/init.lua:630: in function 'repl' ...vide/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk [C]: at 0x004064d0 

EDIT 2 : I tried the solution sent by TonyHsu, but that still doesn't work. That's what I'm doing.

I define a runfile() function in a script called runfile.lua that contains the following code:

 function runfile(scriptName, input) opt = nil arg = input dofile(scriptName) end 

Then I use an external_command.lua script, which I previously defined as an input parameter to scriptName for this function:

 th> scriptName = "external_command.lua" th> require './runfile.lua' th> runfile(scriptName, "Hello world"); 

Unfortunately, also in this case I get an error message:

 external_command.lua:4: attempt to concatenate global 'input_parameter' (a nil value) stack traceback: external_command.lua:4: in main chunk [C]: in function 'dofile' /home/davide/torch/temp/runfile.lua:4: in function 'runfile' [string "runfile(scriptName, "Hello world");"]:1: in main chunk [C]: in function 'xpcall' /home/davide/torch/install/share/lua/5.1/trepl/init.lua:648: in function 'repl' ...vide/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk [C]: at 0x004064d0 
+8
shell lua torch
source share
3 answers

I think the trick is to pass parameters in the global variable args. Let's say I have the following content in external_command.lua .

 -- args has been set by the caller if not args or #args == 0 then print('no input_parameter') else print('#args = ' .. #args, 'input_parameter: ' .. args[1]) end 

Then define runfile () as follows.

 function runfile(f, ...) local tmp = args -- save the original global args args = table.pack(...) dofile(f) args = tmp -- restore args end 

I tested it in th. It looks like this.

 th> runfile('ext_command.lua', 10) #args = 1 input_parameter: 10 [0.0002s] th> runfile('ext_command.lua', 'a', 'b', 'c') #args = 3 input_parameter: a [0.0002s] 
+1
source share

You are using loadfile :

 local TempFunc = loadfile "torch_script.lua" TempFunc(input_parameter1, input_parameter2) 
0
source share

Perhaps you can try to define a function first:

 function runfile(<scriptName>, ...) opt = nil arg = {...} dofile(<scriptName>) end 

then run

 runfile(<scriptName>,...) 
0
source share

All Articles