[英]Convert Torch Lua into Numpy Python
Am looking to convert some Lua Torch code into numpy Python. 我希望将一些Lua Torch代码转换为numpy Python。 I google for find some documentations, but it still unclear. 我在google上找到了一些文档,但仍不清楚。
I found this. 我找到了这个。 https://nn.readthedocs.io/en/rtd/index.html https://nn.readthedocs.io/en/rtd/index.html
Wondering if there is any mapping between Lua Torch functions and numpy functions ? 想知道Lua Torch函数和numpy函数之间是否存在任何映射?
Thanks 谢谢
You could try Lutorpy , a python library for bridging lua/torch with python/numpy. 您可以尝试Lutorpy ,这是用于将lua / torch与python / numpy桥接的python库。
Here is an example of conversion: 这是一个转换示例:
-- lua code # python code (with lutorpy)
-- import lutorpy as lua
require "nn" ===> require("nn")
model = nn.Sequential() ===> model = nn.Sequential()
-- use ":" to access add ===> # use "._" to access add
model:add(nn.Linear(10, 3)) ===> model._add(nn.Linear(10, 3))
-- import numpy as np
x = torch.Tensor(10):zero() ===> arr = np.zeros(10)
-- torch style(painful?) ===> # numpy style(elegent?)
x:narrow(1, 2, 6):fill(1) ===> arr[1:7] = 1
-- # convert numpy array to a torch tensor
-- x = torch.fromNumpyArray(arr)
-- # or you can still use torch style
x:narrow(1, 7, 2):fill(2) ===> x._narrow(1, 7, 2)._fill(2)
-- 1-based index ===> # 0-based index
x[10] = 3 ===> x[9] = 3
y = model:forward(x) ===> y = model._forward(x)
-- # you can convert y to a numpy array
-- yArr = y.asNumpyArray()
For more information, you could goto the github page of lutorpy . 有关更多信息,您可以转到lutorpy的github页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.