Last active
August 6, 2018 14:42
-
-
Save Halfish/58984390f65b785fac17 to your computer and use it in GitHub Desktop.
Lua常见语法总结备忘
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- create a CmdLine object | |
cmd = torch.CmdLine() | |
-- print help text | |
cmd:text() | |
cmd:text("Options:") | |
-- define input varians and default value | |
cmd:option('-threads', 2, 'number of threads to use') | |
cmd:option('-save', 'model.t7', 'name of model to save') | |
cmd:text() | |
-- parse arguments to a table | |
opt = cmd:parse(arg or {}) -- opt is a table like { threads:2, save:"model.t7" } | |
-- we can use `th cmdline.lua -h` to see help information |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 如何定义一个模块 | |
local ModelAB = {} | |
function ModelAB.create(argv) | |
... | |
return results | |
end | |
return ModelAB | |
-- 别人在用这个文件的时候,就可以调用 | |
-- ModelAB = requre 'ModelAB' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 下面的例子展示了怎么使用字典 | |
-- 用花括号{}可以表示字典,即key,value键值对 | |
info = {name = 'Bruce', last_name = 'Zhang', age = 24} | |
-- 键值查找 | |
info.name --输出'Bruce' | |
info.age --输出24 | |
info['name'] --输出'Bruce' | |
info['age'] --输出24 | |
-- 遍历 | |
for key, value in pairs(info) do | |
print(key, value) | |
end | |
--- 输出结果如下: | |
--- name Bruce | |
--- age 24 | |
--- last_name Zhang | |
-- 切割表 table.splice | |
t = {1, 3, 8, 9, 22, 76} | |
spliced, reminder = table.splice(t, 2, 3) -- 切割t中起始为2,长度为3的元素 | |
spliced -- {1, 22, 76} | |
reminder -- {3, 8, 9} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- torch.Tensor是torch7中实现的矩阵运算库 | |
-- 随机数矩阵,返回的是torch.DoubleTensor of size 5x5 | |
t = torch.rand(5, 6) | |
-- 1. 可以直接通过下表访问改变数值 | |
t[3][3] = 666 | |
-- 2. 切片,取出第3行到第5行的数据 | |
t[{{3, 5}}] | |
-- 3. 维度 | |
t:size() -- 5; 6 [torch.LongStorage of size 2] | |
t:size(1) -- 得到行数 | |
t:size(2) -- 得到列数 | |
-- 4. 求和 | |
t:sum() -- 矩阵内所有数字的和 | |
t:sum(1) -- 矩阵内所有行的和,得到 size 1x6 | |
t:sum(2) -- 矩阵内所有列的和,得到 size 5x1 | |
-- 5. 最大值 | |
t:max() -- 矩阵内所有数字的最大值 | |
y, i = t:max(1) -- 矩阵内每行的最大值和他们的索引,得到 size 1x6 | |
y, i =t:max(2) -- 矩阵内每列的最大值和他们的索引,得到 size 5x1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment