Interactive between Matlab and Pytorch

Hey, I’m running pytorch in Matlab. In specific, I import Pytorch lib into the matlab and so I can call functions from PyTorch. I would like to know how to convert matlab variables such as double, complex double to the pytorch tensor? In Python I can simply do A = torch.tensor(B) that B is a variable assigned before. However, I cannot do that in Matlab.

I cannot find PyTorch in MATLAB’s supported frameworks interop so I guess you are exporting to ONNX first?
In any case, I would expect the MATLAB docs should mention how to use 3rd party frameworks but again I couldn’t find any supported use case so far.

Emm…I’m not sure what ONNX is…what I did is config environment in Conda first, and then start Matlab from Conda, then I could use following code to import torch in matlab

torch = py.importlib.import_module("torch");

This allows me to call PyTorch function such as

p2 = py.numpy.array(b);
b_Ten = py.torch.from_numpy(p2);

so I can convert matlab variable ‘b’ to a tensor ‘b_Ten’. Actually I just figured out I can do it in this way.However, it is still unclear to me that how to convert tensor back to matlab variable. I tried

b_Ten.numpy(); 

but it makes error…

Oh, interesting. My MATLAB usage dates a few years back and was mostly targeting signal processing so I wasn’t aware of a Python API and thus don’t know what its current support is, sorry.

Yea, Im using MATLAB for signal processing as well, but I need Pytorch to compute a gradient, which I cannot get the explicit expression easily…