0

I have trained a neural DQN in Matlab and exported the resulting parameters of the NN to an excel file. I'm using these data to control a robot in real time. the code must be written in python since it is on a raspberry pi. I rebuild the model in python and imported the weights from excel through pandas. but loading these parameters every time I need to run the python code takes a very long time. is there a way to save these parameters directly in the python script in a way that I haven't to load it every time I run the code? when I first have run this code on jupyter notebook T haven't this problem since I saved the loaded data just one time and the next cells could identify the names of the parameters without problems. any help would be appreciated...

  • I think by, "when I first have run this code on jupyter notebook T haven't this problem since I saved the loaded data just one time and the next cells could identify the names of the parameters without problems. " you are talking about reading in the values to the current namespace in your notebook. For controlling your robot with Python, you now want something similar. Jupyter notebooks evolved from IPython notebooks several years ago by adding ability to use other language kernels. IPython notebooks evolved from IPython by adding the notebook concept and GUI. IPython evolved from Python ... – Wayne Mar 05 '23 at 19:18
  • by adding magics and convenience concepts beyond the simple interprete console you get when you type 'python' (or similar) on your command line. Python also allows you to run scripts like it sounds how you are trying to control your robot by running a script. That's not a good way because of what you are hitting where once the script ends, it doesn't recall what you just read in. I recounted several other options because you probably want to run IPython on your raspberry Pi. You could use the python interpreted console too but it isn't as convenient. I suspect IPython is already... – Wayne Mar 05 '23 at 19:24
  • on your Raspberry pi if Python is there and it is a recent machine. (Maybe not but I bet it is easy to add.) You can check by running `ipython` on the command line. The interpreter console should come up. The IPython interpreter console is essentially an IPython notebook without the GUI. IPython as convenience abilities for pasting in multi-line code so you can just paste in what your need. Or, **probably better in your case** since you have a script now, you can initiate running the script you've already made in the console namespace by `%run -i myscript.py`. Read about the ... – Wayne Mar 05 '23 at 19:28
  • magic run command. You'll see the `-i` flag allows it to read in scripts into the current namespace. (Or you can paste the blocks of code in the IPython console if you prefer and not need the `-i` flag because that means it will run in the same namespace as the console.) Once you are working in the console namespace, you'll read in values will continue to be defined and known in the namespace and you can either enter lines of code to control your robot or have the commands in scripts and use `%run -i my_commands.py` to execute them. (Think you can even get away with no `.py`.) – Wayne Mar 05 '23 at 19:32
  • Thanks for your detailed answer. but I would make my question more clear,. I could run my code successfully on raspberry pi . the code is a trained DQN model which takes a state as an input and outputs an action. The problem is; importing the parameters from the excel files, (which happens every 0.5 seconds,) makes it impossible to control the robot in real time .because the it took a relatively long time (about 3 sec) to import the parameters and run the model. – Issa Fares Mar 06 '23 at 05:19
  • The parameters are 11 excel files, with big dimensions like 100x2 x32 and 100x200 and 2x2x40 ..... – Issa Fares Mar 06 '23 at 05:23
  • I thought in Jupyter you were importing once and it kept them in the namespace, right? So I'm trying to help you see how you can read them in once on the raspberry pi and keep working with them. You should only need to load them in once at the start and keep using the ipython console that already has them. If you did want to make it faster, you can of course pickle the array or arrays you make from Excel and load that in each time your start up things. Depending on what you are picking exactly, you may wand [dill](https://stackoverflow.com/a/46325381/8508004). Then load once per session. – Wayne Mar 06 '23 at 05:31

0 Answers0