I am following this tensorflow tutorial after two days setting up the environment I finally could run premade_estimator.py
using cmd
but when I try to run the same code in a jupyter notebook I am getting this error:
usage: ipykernel_launcher.py [-h] [--batch_size BATCH_SIZE] [--train_steps TRAIN_STEPS] ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\david\AppData\Roaming\jupyter\runtime\kernel-4faecb24-6e87-40b4-bf15-5d24520d7130.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2 C:\Anaconda3\envs\python3x\lib\site-packages\IPython\core\interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
I have tried to fix it without success using:
pip install --ignore-installed --upgrade jupyter
pip install ipykernel
python -m ipykernel install
conda install notebook ipykernel
ipython kernelspec install-self
Any idea will be appreciate! Thanks!
Have you tried :
A more elegant solution would be:
instead of
I got it! the reason why we get the error is because this code is using
argparse
and this module is used to write user-friendly command-line interfaces, so it seems, it has a conflict with Jupyter Notebook.I found the solution in this page:
What we have to do is:
Delete or comment these lines:
and replace
args
for a dictionary using the library
easydict
in this way:With
easydict
we can access dict values as attributes for the arguments.Update
After all this year diving deeper in python, I found the solution for this question is way more simple (We don't need to use any external library or method).
argparse
is just one of the many ways to pass arguments to a script in python from the terminal. When I tried to do it in a jupyter notebook obviously that wasn't going to work. We can just replace in the function directly the parameters like:Now, if we have a long list of parameters for our function, we can use
*args
or**kargs
.*args
pass a tuple as parameters in the function, for this case, in particular, it will be:**kargs
pass a dictionary as arguments to our function:just google it and you will find a really good explanation on how to use them both, here one documentation that I used to study this.