1

I am trying to use the matlab neural network toolbox from matlab python engine.

Suppose for example I want to emulate the following example http://uk.mathworks.com/help/nnet/ref/fitnet.html

net = fitnet(10);
net = train(net,x,t);
view(net)
y = net(x);
perf = perform(net,y,t)

Then I have the following python code:

import numpy as np
import sys
sys.path.insert(0, '/home/donbeo/myApp/matlab_python_engine/installdir/lib/python3.4/site-packages/')




    n = 100
    x = np.linspace(-10, 10, n)
    y = x**3 + x*2 + x + np.random.normal(0, .1, n)
    plt.plot(x, y)
    plt.show()

    import matlab.engine
    eng = matlab.engine.start_matlab()
    eng.net = eng.fitnet(10.);
    eng.net = eng.train(eng.net,x,t);
    y_est = eng.net(x);

    eng.quit()

When I run the line eng.net = eng.fitnet(10.);

I get the following error:

In [24]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-cc577dcdc93c> in <module>()
----> 1 eng.net = eng.fitnet(10.);

/home/donbeo/myApp/matlab_python_engine/installdir/lib/python3.4/site-packages/matlab/engine/matlabengine.py in __call__(self, *args, **kwargs)
     77         else:
     78             return FutureResult(self._engine(), future, nargs, _stdout,
---> 79                                 _stderr).result()
     80 
     81     def __validate_engine(self):

/home/donbeo/myApp/matlab_python_engine/installdir/lib/python3.4/site-packages/matlab/engine/futureresult.py in result(self, timeout)
    105 
    106             self._result = pythonengine.getFEvalResult(
--> 107                 self._future,self._nargout, None, out=self._out, err=self._err)
    108             self._retrieved = True
    109             return self._result

TypeError: Unsupported datatype returned from MATLAB.

In [25]: 

How can I solve?

EDIT:

The command eng.net = eng.fitnet(10);

returns :

Error using network/subsasgn>network_subsasgn (line 553)
"layers{1}.size" must be a positive integer.

Error in network/subsasgn (line 13)
net = network_subsasgn(net,subscripts,v,netname);

Error in feedforwardnet>create_network (line 116)
      net.layers{i}.size = param.hiddenSizes(i);

Error in feedforwardnet (line 69)
    net = create_network(param);

Error in fitnet>create_network (line 98)
  net = feedforwardnet(param.hiddenSizes,param.trainFcn);

Error in fitnet (line 70)
    net = create_network(param);

---------------------------------------------------------------------------
MatlabExecutionError                      Traceback (most recent call last)
<ipython-input-29-480a6805679e> in <module>()
----> 1 eng.net = eng.fitnet(10);

/home/donbeo/myApp/matlab_python_engine/installdir/lib/python3.4/site-packages/matlab/engine/matlabengine.py in __call__(self, *args, **kwargs)
     77         else:
     78             return FutureResult(self._engine(), future, nargs, _stdout,
---> 79                                 _stderr).result()
     80 
     81     def __validate_engine(self):

/home/donbeo/myApp/matlab_python_engine/installdir/lib/python3.4/site-packages/matlab/engine/futureresult.py in result(self, timeout)
    105 
    106             self._result = pythonengine.getFEvalResult(
--> 107                 self._future,self._nargout, None, out=self._out, err=self._err)
    108             self._retrieved = True
    109             return self._result

MatlabExecutionError: 
  File /home/donbeo/myApp/MatlabR2015a/toolbox/nnet/nnet/@network/subsasgn.p, line 553, in network_subsasgn

  File /home/donbeo/myApp/MatlabR2015a/toolbox/nnet/nnet/@network/subsasgn.p, line 13, in subsasgn

  File /home/donbeo/myApp/MatlabR2015a/toolbox/nnet/nnet/nnnetwork/feedforwardnet.m, line 116, in create_network

  File /home/donbeo/myApp/MatlabR2015a/toolbox/nnet/nnet/nnnetwork/feedforwardnet.m, line 69, in feedforwardnet

  File /home/donbeo/myApp/MatlabR2015a/toolbox/nnet/nnet/nnnetwork/fitnet.m, line 98, in create_network

  File /home/donbeo/myApp/MatlabR2015a/toolbox/nnet/nnet/nnnetwork/fitnet.m, line 70, in fitnet
"layers{1}.size" must be a positive integer.


In [30]: 
Donbeo
  • 17,067
  • 37
  • 114
  • 188
  • Try passing an integer (10) instead of float (10.) – Daniel Jul 07 '15 at 18:49
  • I get a different kind of error. I have tried various combinations but I think that the problem is that fitnet returns a variable that can not be read by python and I am not sure of how this should be handled – Donbeo Jul 07 '15 at 18:53
  • Is there any reference for the left side of the assignment? Assigning a workspace variable is done this way: http://de.mathworks.com/help/matlab/matlab_external/use-the-matlab-engine-workspace-in-python.html – Daniel Jul 07 '15 at 18:54
  • even in this way it is not working. Is there another option? – Donbeo Jul 07 '15 at 22:58

1 Answers1

0

The best thing to do in this circumstances is to create a matlab function that can perform your required actions and then return to python variables, that it can understand. With matlab objects that python cant understand it is best to control the objects in matlab, Load them and save them in matlab every time you call matlab engine to operate. Other solution is to use handle objects but that's not a perfect solution, each solution has its ups and downs.

Adam Jensen
  • 49
  • 1
  • 5
  • I hope you found your solution, Didn't see that you asked me to post the code. if you still want i will post it. – Adam Jensen Feb 19 '16 at 22:43