【问题标题】:Call torch7 (Lua) function from python?从python调用torch7(Lua)函数?
【发布时间】:2016-02-16 21:25:55
【问题描述】:

我有一个用 python 编写的程序,我有一个使用 Toch7 训练的模型 ConvNet。我想从 python 程序中调用模型的前向和后退,因为我发现在 lua 中再次编写它很困难。

有什么想法吗?

【问题讨论】:

    标签: python python-2.7 lua torch


    【解决方案1】:

    我认为您现在有一个更好的解决方案,即lutorpy。 与pytorch不同的是,你在python中有一个lua引擎,因此在python中导入任何lua模块和代码更加灵活,并且易于使用和灵活。对于 pytorch,您只有很少的可直接在 python 中使用的移植模块。

    使用 lutorpy,您可以轻松快速地在 numpy 和 torch 张量之间进行转换。

    对于您的情况,您可以像这样在 python 中编写代码:

    import numpy as np
    import lutorpy as lua
    
    model = torch.load('PATH TO YOUR MODEL FILE')
    
    # generate your input data with numpy
    arr = np.random.randn(100)
    
    # convert your numpy array into torch tensor
    x = torch.fromNumpyArray(arr)
    
    # apply model forward method with "._" syntax(which is equivalent to ":" in lua)
    y = model._forward(x)
    

    不同库之间的简要比较: How can I load and use torch deep learning models from python?

    【讨论】:

    【解决方案2】:

    正如一位 Torch 作者在torch7 maillist 上所建议的,您可以尝试pytorch

    【讨论】:

      猜你喜欢
      • 2013-12-22
      • 2012-06-30
      • 2014-09-01
      • 2011-03-26
      • 2014-09-02
      • 2016-09-23
      • 2013-02-19
      • 2015-12-30
      相关资源
      最近更新 更多