Running a Python Script with Tensorflow From Matlab
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to run a python script (implementing a deep neural network using Tensorflow Keras) as part of a larger algorithm within Matlab. I have managed to change my environment to the following:
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.6"
Executable: "C:\Users\dylwh\anaconda3\envs\tensorflow_env\python.exe"
Library: "C:\Users\dylwh\anaconda3\envs\tensorflow_env\python36.dll"
Home: "C:\Users\dylwh\anaconda3\envs\tensorflow_env"
Status: NotLoaded
ExecutionMode: InProcess
I am also able to load in and use python libraries such as numpy and sklearn, by starting the Matlab application from the Anaconda command line with the proper environment activated. As a test, I am trying to run a python script containing a simple function that I have found within these forums, with a line added to import tensorflow:
import sys
import tensorflow as tf
def squared(a,b):
c = (b/2)-a
return c
if __name__ == '__main__':
x = float(sys.argv[1])
y = float(sys.argv[2])
sys.stdout.write(str(squared(x,y)))
To run the function, I am using the line shown below, which produces the corresponding error:
>> !C:\Users\dylwh\anaconda3\envs\tensorflow_env\python.exe test.py 1 5
Traceback (most recent call last):
File "test.py", line 2, in <module>
import tensorflow as tf
File "C:\Users\dylwh\anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow\__init__.py", line 101, in <module>
from tensorflow_core import *
File "C:\Users\dylwh\anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
from tensorflow.python.tools import module_util as _module_util
ModuleNotFoundError: No module named 'tensorflow.python.tools'; 'tensorflow.python' is not a package
Importing Tensorflow works just fine in the Spyder IDE, but something is going wrong when I try this method. I'm having trouble understanding why this is the case when the environment clearly contains the Tensorflow library. Any help would be appreciated.
0 commentaires
Réponses (1)
Sivylla Paraskevopoulou
le 28 Mar 2022
The recently posted example Image Classification in MATLAB Using TensorFlow could help you with your workflow. To see the example, go to GitHub using this link:
You could also consider importing your TensorFlow model into MATLAB by using the importTensorFlowNetwork function. For more information, see this recent blog post:
and the documentation for importTensorFlowNetwork:
0 commentaires
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!