【问题标题】:Convert Torch Lua into Numpy Python将 Torch Lua 转换为 Numpy Python
【发布时间】:2016-09-05 15:03:17
【问题描述】:

我希望将一些 Lua Torch 代码转换为 numpy Python。 我用谷歌搜索了一些文档,但仍然不清楚。

我找到了这个。 https://nn.readthedocs.io/en/rtd/index.html

想知道 Lua Torch 函数和 numpy 函数之间是否有任何映射?

谢谢

【问题讨论】:

    标签: python numpy lua torch


    【解决方案1】:

    您可以尝试Lutorpy,这是一个用于将 lua/torch 与 python/numpy 连接起来的 python 库。

    这是一个转换的例子:

    -- 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()
    

    更多信息,您可以前往lutorpy的github页面。

    【讨论】:

    • 成熟到可以认真使用了吗?
    猜你喜欢
    • 2016-02-04
    • 2014-10-04
    • 2016-06-09
    • 2021-02-02
    • 1970-01-01
    • 2016-12-24
    • 2020-03-16
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多