How do I operate fitnet function of Matlab by Python?

18 views (last 30 days)
Hi. I'm a Japanese university student.
I'm researching AI and will be doing neural network analysis using MATLAB and Python.
Previously, We had been manually analyzing hundreds of data one by one using MATLAB's Neural Net Fitting APP (nftool), but now I would like to automate the process of starting MATLAB and analyzing the data using Python.
I would like to execute the following code in Python.
net = fitnet(10,'trainlm');
net = train(net, input, target);
output = net(input);
R = corrcoef(output, target);
R = (1,2)
I've written this in Python like below
import matlab.engine
eng = matlab.engine.start_matlab('-desktop')
eng.workspace['net'] = eng.fitnet(10.)
eng.net = eng.train(eng.net,input,target)
eng.workspace['output'] = eng.net(input)
R = eng.corrcoef(eng.output,eng,target)
Necessary data are loaded at workspace in Matlab by using another Python code.
At Running it, this error occurred.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [78], in <cell line: 3>()
1 import matlab.engine
2 eng = matlab.engine.start_matlab('-desktop')
----> 3 eng.workspace['net'] = eng.fitnet(10.)
File ~\anaconda3\envs\python_ex\lib\site-packages\matlabengineforpython-r2021a-py3.8.egg\matlab\engine\matlabengine.py:70, in MatlabFunc.__call__(self, *args, **kwargs)
68 return FutureResult(self._engine(), future, nargs, _stdout, _stderr, feval=True)
69 else:
---> 70 return FutureResult(self._engine(), future, nargs, _stdout,
71 _stderr, feval=True).result()
File ~\anaconda3\envs\python_ex\lib\site-packages\matlabengineforpython-r2021a-py3.8.egg\matlab\engine\futureresult.py:67, in FutureResult.result(self, timeout)
64 if timeout < 0:
65 raise TypeError(pythonengine.getMessage('TimeoutCannotBeNegative'))
---> 67 return self.__future.result(timeout)
File ~\anaconda3\envs\python_ex\lib\site-packages\matlabengineforpython-r2021a-py3.8.egg\matlab\engine\fevalfuture.py:82, in FevalFuture.result(self, timeout)
79 if not result_ready:
80 raise TimeoutError(pythonengine.getMessage('MatlabFunctionTimeout'))
---> 82 self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
83 self._retrieved = True
84 return self._result
ValueError: MATLAB can return only 1-by-N and N-by-1 cell arrays.
This's the similar situation as this questioner.
According to this thread, fitnet returns a variable that Python cannot read, so it cannot process it and is causing this error, and I think so.
My Matlab's varsion is R2021a.
How should I code it to make it work correctly?

Accepted Answer

David Willingham
David Willingham on 24 Jun 2022
Hi,
Here is code in python that will call the neural network training:
import matlab.engine
eng = matlab.engine.start_matlab('-desktop')
data = eng.simplefit_dataset(nargout=2)
x = data[0][0]
t = data[1][0]
eng.workspace["x"] = x
eng.workspace["t"] = t
eng.evalc("net = fitnet(10.0);")
eng.evalc("net = train(net,x,t);")
eng.evalc("out = net(x);")
out = eng.workspace["out"]
R = eng.corrcoef(out,t)
If you want to test it from MATLAB you can run (where the code above is saved as 'mypythonscript.py':
pyrunfile('mypythonscript.py')
  1 Comment
David Willingham
David Willingham on 24 Jun 2022
Another option is to create functions in MATLAB (attached) which will minimise calls to evalc and then use the following python code to call them:
import matlab.engine
eng = matlab.engine.start_matlab('-desktop')
data = eng.simplefit_dataset(nargout=2)
x = data[0][0]
t = data[1][0]
hiddenSizes = 10.0
modelFilename = eng.myNNTrain(hiddenSizes, x, t)
out = eng.myNNPredict(x)
R = eng.corrcoef(out,t)

Sign in to comment.

More Answers (1)

David Willingham
David Willingham on 22 Jun 2022
Try changing this line in python:
eng.workspace['net'] = eng.fitnet(10.)
to
a = matlab.double(10)
eng.workspace['net'] = eng.fitnet(a)
Does this solve your error?
  2 Comments

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!