Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 433
S.AMEEN Avatar asked Oct 19 '22 22:10

S.AMEEN


1 Answers

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?

like image 180
Wei Avatar answered Oct 22 '22 10:10

Wei