传递参数
python[test.py]
import sys
a=sys.argv[1]
b=sts.argv[2]
print a,b
python test.py abc 1
torch[test.lua]
a=arg[1]
b=arg[2]
th test.lua acd 1
bash [test.sh]
#!/bin/bash
a=$1
b=$2
./test.sh 123 string
传递参数2
python [test.py]
import argparse
def main(params):
jfile = json.load(open(params['input_json'], 'r'))
names_train=jfile["unique_img_train"]
names_test =jfile["uniuqe_img_test"]
with h5py.File(params['input_h5'],'r') as hf:
data1 = hf.get('attenmaps')
attenmaps = np.array(data1)
data2 = hf.get('imgidx')
imgidx = np.array(data2)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input_h5', default='/home/c-nrong/VQA/HieCoAttenVQA2/data/vqa_data_prepro.h5', help='hdf5 file for attenmaps.')
parser.add_argument('--body', default='abc\n')
parser.add_argument('--ymd', nargs='+', default=['20190630', '201905avg'],help='') #列表型
args = parser.parse_args()
params = vars(args) # convert to ordinary dict
print(params)
print 'parsed input parameters:'
print json.dumps(params, indent = 2)
main(params)
调用的时候
python main.py --input_h5 "test" --ymd 20190530 201905avg 20190601
python test.py --input_json data/temp.json --input_h5 prep/data.h5
torch
http://blog.csdn.net/jiejinquanil/article/details/49659159
cmd = torch.CmdLine()
cmd:text()
cmd:text()
cmd:text(‘Training a simple network’)
cmd:text()
cmd:text(‘Options’)
cmd:option(‘-seed’,123,’initial random seed’)
cmd:option(‘-booloption’,false,’boolean option’)
cmd:option(‘-stroption’,’mystring’,’string option’)
cmd:text()
local opt = cmd:parse(arg)
print(opt)
local seed = opt.seed
th myscript.lua -seed 456 -stroption mycustomstring
可变参数
lua
http://www.runoob.com/lua/lua-functions.html
Lua函数可以接受可变数目的参数,和C语言类似在函数参数列表中使用三点(…) 表示函数有可变的参数。
Lua将函数的参数放在一个叫arg的表中,#arg 表示传入参数的个数。
例如,我们计算几个数的平均值:
function average(...)
result = 0
local arg={...}
for i,v in ipairs(arg) do
result = result + v
end
print("总共传入 " .. #arg .. " 个数")
return result/#arg
end
print("平均值为",average(10,5,3,4,5,6))
output
总共传入 6 个数
平均值为 5.5
python
str="abc"
dd=123
print("this is string %s, value %d" %(str,dd))
torch
pi_name="abc"
pi=3.2
print(string.format("The value of %s is %d, isn't that cool?",pi_name, pi))
bash
a="abc"
echo "now print $a"
echo $a
exit
python
import sys
sys.exit()
torch
require 'os'
os.exit()
bash
exit 1
exit -1
子函数
python [need a main function]