简体   繁体   中英

Call torch7 (Lua) function from python?

I have a program is written in python and I have model ConvNet trained using Toch7. I would like to call forward and backpro to the model from python program as I find difficult and hard to write it again in lua.

Any idea please?

I think you now have a much better solution, which is lutorpy . Different from pytorch, you have a lua engine in python, so it's more flexible to import any lua module and code in python, and it's easy to use and flexible. For pytorch you only have very few ported module which you can directly use in python.

With lutorpy, you can convert between numpy and torch tensor easily and very fast.

For you case, you can write your code in python like this:

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)

A brief comparison between different library: How can I load and use torch deep learning models from python?

正如火炬手的一位火炬手作者所建议的那样 ,你可以试试pytorch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM