How to run Python (Pytorch) Code in MATLAB

15 ビュー (過去 30 日間)
SH
SH 2023 年 3 月 16 日
コメント済み: Frederick 2025 年 4 月 11 日
Hello, I hope you are doing well. I have the following Python(pytorch) code which is running in an Anaconda environment. I want it to run it on MATLAB
I have tried the following but it does not work Can any one help me with that.?
pyenv("Version","C:\Users\DELL\anaconda3\envs\myenv\python.exe")
ans =
PythonEnvironment with properties:
Version: "3.8"
Executable: "C:\Users\DELL\anaconda3\envs\myenv\python.exe"
Library: "C:\Users\DELL\anaconda3\envs\myenv\python38.dll"
Home: "C:\Users\DELL\anaconda3\envs\myenv"
Status: NotLoaded
ExecutionMode: InProcess
Here is the following code (PythonScript.py)
import torch
import numpy as np
import matplotlib.pyplot as plt
print('Check GPU Availble=',torch.cuda.is_available())
print('How many GPU Availble=',torch.cuda.device_count())
print('Index of Current GPU=',torch.cuda.current_device())
print('Name of the Current GPU Used=',torch.cuda.get_device_name(0))
array = np.loadtxt('Dataset1.csv', delimiter=',')
twofeatures = array[:, [1, 3]]
#print(twofeatures)
torchfeatures = torch.from_numpy(twofeatures)
gpufeature = torchfeatures.cuda()
gpufeature = gpufeature.to(torch.float)
from fast_pytorch_kmeans import KMeans
kmeans = KMeans(n_clusters=30, mode='euclidean', verbose=1)
labels = kmeans.fit_predict(gpufeature)
#print(labels)
index = labels.cpu().data.numpy()
#print(index)
plt.scatter(array[:, [0]], array[:, [1]], c=index, s=10, cmap="jet")
plt.show()
pyrun(PythonScript.py);
Getting the following error
Error using __init__><module>
Python Error: OSError: [WinError 127] The specified procedure could not be found. Error
loading "C:\Users\DELL\anaconda3\envs\myenv\lib\site-packages\torch\lib\shm.dll" or one of
its dependencies.
Error in <string>><module> (line 1)
Can anybody help me with that
  1 件のコメント
Frederick
Frederick 2025 年 4 月 11 日
I got this same error with Matlab R2022b, Python 3.10, and torch 2.6.0, and pyenv(..., ExecutionMode="InProcess"). I get the error when I use torch, but not if I use numpy. I did not get the error after switching to ExecutionMode="OutOfProcess". It works, but for my use case ExecutionMode="OutOfProcess" is much slower. I switched to Matlab R2023a, and ExecutionMode="InProcess" works! I am still investigating, but it seems there is some sort of DLL incompatibility between Matlab R2022b and pytorch 2.6.0. In my experience the error messages for such DLL problems are never very specific - the real incompatibility is usually deeper in than the error message indicates. The potential for this problem is hinted at here https://www.mathworks.com/help/matlab/ref/pyenv.html

サインインしてコメントする。

回答 (1 件)

Sivylla Paraskevopoulou
Sivylla Paraskevopoulou 2023 年 3 月 21 日
Can you try the following command to run your Python file?
pyrunfile("PythonScript.py")
  1 件のコメント
Narayan
Narayan 2024 年 6 月 30 日
編集済み: Narayan 2024 年 6 月 30 日
I have used the same command to run the python file to call the pytorch model. But it was very slow during iteratively calling the pyrunfile(" PythonScript.py") command during simulations. Is there any better way in matlab ?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2022a

Translated by