python,lua,bash

这篇博客探讨了Python, Lua和Bash在传递参数、子函数、可变参数以及文件操作如txt, csv, hdf5和json等方面的应用。在Lua中,函数可以接受可变参数,参数存放在arg表中。Python的文件操作包括从numpy数组到列表的转换。 Torch在处理hdf5文件方面也有涉及。" 107480503,9664351,使用Visual Studio 2019开发Python环境配置,"['Python', 'Visual Studio', '开发工具', 'IDE']

传递参数

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

print

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]

def getIdx(imgidxs,itoimg):
  idxs = {}
  for i, imgidx in enumerate(imgidxs):
     img=itoimg.get(str(int(imgidx)))
     img = img.encode('punycode') #unicode -> ascii
     imgid=img.split('/')[1].split('.')[0]
     if imgid not in idxs:
         idxs[imgid] = []
     idxs[imgid].append(i)
  return idxs
if __name__ == "__main__":
  imgidx=
  itoimg=
  a=getIdx(imgidxs,itoimg)

torch [no main function]

for i = 4,1,-1 do 
     print(i) 
end 
 
function load(fname) 
    self.im=image.load(fname) 
end 

path="abc/im.png"
load(path)

bash [test.sh] [no main function]

#!/bin/bash
function sub() {
 a=$1
 b=$2
 echo $a,$b

input1=$1
input2=$2
sub($input1,$input2)
./test.sh 1 2

for and if

python

for i in range(10):
	print(i)

if(score>=90) and (score<=100):  
    print "A"  
elif(score>=80 and score<90):  
    print "B"  
elif(score>=60 and score<80):  
    print "C"  
else:  
    print "D"

torch

for i = 4,1,-1 do 
     print(i) 
end

--https://www.lua.org/pil/4.3.1.html
if op == "+" then
      r = a + b
elseif op == "-" then
      r = a - b
elseif op == "*" then
      r = a*b
elseif op == "/" then
      r = a/b
else
      error("invalid operation")
end

bash

for((i=1;i<10;i++))do
 echo $i
done

if [ ! -r /work/an/a.test ];then
	touch /work/an/a.test 
elif
	...
elif
	...
else
	...
fi

writefile

python

1.convert numpy array to list

a=np.ones((1,4))
b=a.tolist()

2.convert list to numpy array

a = [1, 2]
np.asarray(a)

txt

with open("test.txt","w") as f:
	f.write("abc")
import numpy as np
with open(outfile, 'w') as writer:
     writer.truncate()
     feat = net.blobs[label_name].data[0].reshape(1,-1) #numpy array
     np.savetxt(writer, feat, fmt='%.8g')

csv

import numpy as np
data = np.loadtxt("foo.csv",delimiter=",")

#ヘッダ行を飛ばす。skiprowsを指定。
data = np.loadtxt("foo.csv",delimiter=",", skiprows=1)

#tsvの読み込み。
data = np.loadtxt("foo.tsv",delimiter="\t")

#列を指定して読み込み。usecols指定。下記は1,2,4列目を読む。
data = np.loadtxt("bar.csv",delimiter="\t", usecols=(0,1,3))
data = np.loadtxt(file_in,delimiter=",")
df = pd.DataFrame(data=data, columns=['mesh500mid', 'season', 'period', 'dayflag', 'pop'])
import pandas as pd
df = pd.read_csv('D:\\PDA\\4.18\\data.csv',
encoding='utf8')
df.columns = ['mesh500mid', 'season', 'period', 'dayflag', 'pop']
print df.head()

hdf5

#numpy array
with h5py.File(outfile, 'w') as hf:
	hf.create_dataset('areaprobs', data=final)

json

#type(data) => dict or list?
import json
infile="abc.json"
data = {
    'name' : 'ACME',
    'shares' : 100,
    'price' : 542.23
} #dict
data = [
        {
        'name' : 'ACME',
        'shares' : 100,
        'price' : 542.23
        },
        {
        'name' : 'ACME',
        'shares' : 100,
        'price' : 542.23
        }
] #list
json.dump(data, open(infile, 'w'))

torch

hdf5

infile="abc.h5"
local train_h5_file = hdf5.open(infile, 'w')
train_h5_file:write('/images_train', data)   #data: torch.Tensor
train_h5_file:close()
local scorefile=string.format("%s/%s_score_epoch%d.h5",outpath,intype,mei)
local myFile = hdf5.open(scorefile, 'w')
myFile:write('score', scores)
myFile:close()

###json
#readfile
https://docs.python.org/2/tutorial/inputoutput.html
http://blog.csdn.net/apsvvfb/article/details/52067999
txt,hdf5,json

##python
###txt

f = open("test.txt", 'r')
f_lines = f.readlines()
f.close()
    
for ix, line in enumerate(f_lines):
	print ix
	print line
f = open('workfile', 'w')
f.read()
#'This is the entire file.\n'
f.read()
#''
f.close()
f.readline()
#'This is the first line of the file.\n'
f.readline()
#'Second line of the file\n'
f.readline()
#''
f.close()
for line in f:
        print line,
f.close()
with open('workfile', 'r') as f:
    read_data = f.read()
#don't need "f.close()"

###hdf5

import numpy as np
import h5py
infile="abc.h5"
with h5py.File(infile,'r') as hf:
	data1 = hf.get('attenmaps')
	attenmaps = np.array(data1)
	data2 = hf.get('imgidx')
	imgidx = np.array(data2)

###json

import json
infile="abc.json"
jfile = json.load(open(infile, 'r'))
names_train=jfile["unique_img_train"]
print(#names_train)

##torch
###txt

for line in io.lines(inputfile) do

end
io.input():close()

###hdf5

require 'hdf5'
local myFile = hdf5.open('temp.h5', 'r')
local data = myFile:read('feature'):all()
myFile:close()
--the type of data is "torch.DoubleTensor"

###json

local cjson = require 'cjson'
filename="/home/c-nrong/VQA/HieCoAttenVQA2/data/vqa_data_prepro.json"
local file = io.open(filename, 'r') 
local text = file:read() 
file:close() 
local json_file = cjson.decode(text) 
word = json_file.ix_to_img_test
print(word)

##bash

for w in `cat test.txt`;do
	echo $w
done
while read w
do
	echo $w
done < test.txt

#rm,mkdir
python

import os
if os.path.exists(filename):
    os.remove(filename)
else:
    print("Sorry, I can not remove %s file." % filename)

if os.path.isdir('E:\\book\\temp'):
	os.rmdir(path) 
else:
	os.mkdir('E:\\book\\temp')

#array
##python

y=[(x+1)*5 for x in range(6)]
#5,10,15,25,30

##lua

for mei=5,30,5 do
	print(mei)
	--5,10,15,20,25,30
end

###table to tensor
https://groups.google.com/forum/#!topic/torch7/bdY_AveKn2k

require 'nn'
function TableToTensor(table)
  local tensorSize = table[1]:size()
  local tensorSizeTable = {-1}
  for i=1,tensorSize:size(1) do
    tensorSizeTable[i+1] = tensorSize[i]
  end
  merge=nn.Sequential()
    :add(nn.JoinTable(1))
    :add(nn.View(unpack(tensorSizeTable)))

  return merge:forward(table)
end

#array initialization
##python
numpy array: start from 0
http://blog.csdn.net/sunny2038/article/details/9002531
NumPy数组的维数称为秩(rank),一维数组的秩为1,二维数组的秩为2,以此类推。在NumPy中,每一个线性的数组称为是一个轴(axes),秩其实是描述轴的数量。比如说,二维数组相当于是两个一维数组,其中第一个一维数组中每个元素又是一个一维数组。所以一维数组就是NumPy中的轴(axes),第一个轴相当于是底层数组,第二个轴是底层数组里的数组。而轴的数量——秩,就是数组的维数。

NumPy的数组中比较重要ndarray对象属性有:

ndarray.ndim:数组的维数(即数组轴的个数),等于秩。最常见的为二维数组(矩阵)。

ndarray.shape:数组的维度。为一个表示数组在每个维度上大小的整数元组。例如二维数组中,表示数组的“行数”和“列数”。ndarray.shape返回一个元组,这个元组的长度就是维度的数目,即ndim属性。

ndarray.size:数组元素的总个数,等于shape属性中元组元素的乘积。

ndarray.dtype:表示数组中元素类型的对象,可使用标准的Python类型创建或指定dtype。另外也可使用前一篇文章中介绍的NumPy提供的数据类型。

ndarray.itemsize:数组中每个元素的字节大小。例如,一个元素类型为float64的数组itemsiz属性值为8(float64占用64个bits,每个字节长度为8,所以64/8,占用8个字节),又如,一个元素类型为complex32的数组item属性为4(32/8)。
ndarray.data:包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。
import numpy as np
a=np.zeros((3,4))
a=np.zeros(3)
b=np.ones((3,4))
b=np.ones(4)

a = np.array( [2,3,4] )
print(a[0])
#2
b = np.array( [ (1.5,2,3), (4,5,6) ] )   
print(b[0][0])
#1.5

np.arange(10, 30, 5)  
#array([10, 15, 20, 25])  

range(5)
#0,1,2,3,4
np.arange(5)
#0,1,2,3,4

np.linspace(-1, 0, 5)  
# array([-1.  , -0.75, -0.5 , -0.25,  0.  ])

>>> b = np.array([(1.5,2,3), (4,5,6) ,(7,8,9)])
>>> b
array([[ 1.5,  2. ,  3. ],
       [ 4. ,  5. ,  6. ],
       [ 7. ,  8. ,  9. ]])
>>> b[0:1]
array([[ 1.5,  2. ,  3. ]])
>>> b[0:2]
array([[ 1.5,  2. ,  3. ],
       [ 4. ,  5. ,  6. ]])
>>> b[1:2]
array([[ 4.,  5.,  6.]])
>>> b[2]
array([ 7.,  8.,  9.])
>>> b[0,0:2]
array([ 1.5,  2. ])
>>> b[0:1,0:2]
array([[ 1.5,  2. ]])

##lua
torch.tensor
start from 1

a=torch.Tensor(5):zero()
b=torch.Tensor(2,5):fill(3.14)
print(a[1])
type(a[1])

th> a = torch.Tensor({{1,2,3,4}, {5,6,7,8}})
th> a
 1  2  3  4
 5  6  7  8
[torch.DoubleTensor of size 2x4]
th> a[1]
 1
 2
 3
 4
[torch.DoubleTensor of size 4]

x = torch.Tensor(5, 6):zero()
> x
 0 0 0 0 0 0
 0 0 0 0 0 0
 0 0 0 0 0 0
 0 0 0 0 0 0
 0 0 0 0 0 0
[torch.DoubleTensor of dimension 5x6]

x[{ 1,3 }] = 1 -- sets element at (i=1,j=3) to 1
-- same to "x[1][3]=1" 
> x
 0  0  1  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
[torch.DoubleTensor of dimension 5x6]

x[{ 2,{2,4} }] = 2  -- sets a slice of 3 elements to 2
> x
 0  0  1  0  0  0
 0  2  2  2  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
 0  0  0  0  0  0
[torch.DoubleTensor of dimension 5x6]

x[{ {},4 }] = -1 -- sets the full 4th column to -1
> x
 0  0  1 -1  0  0
 0  2  2 -1  0  0
 0  0  0 -1  0  0
 0  0  0 -1  0  0
 0  0  0 -1  0  0
[torch.DoubleTensor of dimension 5x6]

x[{ {},2 }] = torch.range(1,5) -- copy a 1D tensor to a slice of x
> x

 0  1  1 -1  0  0
 0  2  2 -1  0  0
 0  3  0 -1  0  0
 0  4  0 -1  0  0
 0  5  0 -1  0  0
[torch.DoubleTensor of dimension 5x6]

> a = torch.range(1, 5)
> a
 1
 2
 3
 4
 5
[torch.DoubleTensor of size 5]
x:size()

c++ : ?

torch

local batchEnd = (batchStart + batchSize - 1 > trainNum) and (trainNum) or (batchStart + batchSize - 1)

c++

local batchEnd = (batchStart + batchSize - 1 > trainNum) ? trainNum : (batchStart + batchSize - 1)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值