How do I operate fitnet function of Matlab by Python?

조회 수: 7 (최근 30일)
威吹 塚本
威吹 塚本 2022년 6월 22일
댓글: 威吹 塚本 2022년 6월 24일
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?

채택된 답변

David Willingham
David Willingham 2022년 6월 24일
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
David Willingham
David Willingham 2022년 6월 24일
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)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Willingham
David Willingham 2022년 6월 22일
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
威吹 塚本
威吹 塚本 2022년 6월 22일
Thank you for your response.
I tried the method you provided and the following error occurred.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-4731bf8d7b8a> in <module>
----> 1 a = matlab.double(10)
2 eng.workspace['net'] = eng.fitnet(a)
~\anaconda3\Lib\site-packages\matlab\mlarray.py in __init__(self, initializer, size, is_complex)
49 super(double, self).__init__('d', initializer, size, is_complex)
50 except Exception as ex:
---> 51 raise ex
52
53
~\anaconda3\Lib\site-packages\matlab\mlarray.py in __init__(self, initializer, size, is_complex)
47 """
48 try:
---> 49 super(double, self).__init__('d', initializer, size, is_complex)
50 except Exception as ex:
51 raise ex
~\anaconda3\Lib\site-packages\matlab\_internal\mlarray_sequence.py in __init__(self, typecode, initializer, size, is_complex)
39 self._python_type = python_type[typecode]
40 if initializer is not None:
---> 41 init_dims = _get_size(initializer)
42 try:
43 self._size = _normalize_size(size, init_dims)
~\anaconda3\Lib\site-packages\matlab\_internal\mlarray_utils.py in _get_size(initializer)
74 is_rect, depth = _is_rectangular(initializer)
75 if not is_rect:
---> 76 raise ValueError("initializer must be a rectangular nested sequence")
77
78 dims = []
ValueError: initializer must be a rectangular nested sequence
威吹 塚本
威吹 塚本 2022년 6월 24일
it works correctly! Thank you for your help.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by