如何修复ipykernel_launcher.py:错误:jupyter中无法识别的参数? [英] How to fix ipykernel_launcher.py: error: unrecognized arguments in jupyter?
问题描述
设置环境两天后,我正在遵循此tensorflow 教程,我终于可以运行premade_estimator.py
使用cmd
I am following this tensorflow tutorial after two days setting up the environment I finally could run premade_estimator.py
using cmd
但是当我尝试在jupyter笔记本中运行相同的代码时,出现此错误:
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
发生了异常,请使用%tb查看完整的追溯.
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!
推荐答案
我明白了!我们收到错误的原因是因为此代码使用 argparse
和该模块用于编写用户友好的命令行界面,因此似乎与Jupyter Notebook冲突.
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:
我们要做的是:
删除或注释这些行:
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=100, type=int, help='batch size')
parser.add_argument('--train_steps', default=1000, type=int,
help='number of training steps')
并替换args
args = parser.parse_args(argv[1:])
对于使用库> easydict
这样的字典:
for a dictionary using the library easydict
in this way:
args = easydict.EasyDict({
"batch_size": 100,
"train_steps": 1000
})
使用easydict
,我们可以将dict值作为参数的属性来访问.
With easydict
we can access dict values as attributes for the arguments.
更新
今年以来,在深入研究python之后,我发现此问题的解决方案更加简单(我们不需要使用任何外部库或方法). argparse
只是从终端将参数传递给python中的脚本的众多方法之一.当我尝试在Jupyter笔记本中进行操作时,显然那是行不通的.我们可以直接在函数中替换参数,例如:
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:
funtion(batch_size=100, train_steps=1000)
现在,如果我们的函数参数列表很长,则可以使用*args
或**kargs
.
Now, if we have a long list of parameters for our function, we can use *args
or **kargs
.
*args
将元组作为函数中的参数传递,在这种情况下,尤其是:
*args
pass a tuple as parameters in the function, for this case, in particular, it will be:
args = (100, 1000)
function(*args)
**kargs
将字典作为参数传递给我们的函数:
**kargs
pass a dictionary as arguments to our function:
args = {"batch_size": 100,
"train_steps": 1000}
function(**args)
只需在Google上对其进行搜索,您会发现一个很好的解释,说明如何同时使用它们,这里
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.
这篇关于如何修复ipykernel_launcher.py:错误:jupyter中无法识别的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!