如何从Python中调用torch7(Lua)函数?

2024-10-01 07:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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

有什么想法吗?在


Tags: 模型程序forwardluaconvnettoch7backpro
2条回答

正如Torch作者之一在torch7 maillist上建议的那样,您可以尝试pytorch。在

我想你现在有了一个更好的解决方案,那就是lutorpy。 与pytorch不同的是,pythorch中有一个lua引擎,因此可以更灵活地导入任何lua模块并用python编写代码,而且易于使用和灵活。对于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?

相关问题 更多 >