如何修复 ipykernel_launcher.py: error: unrecognized arguments in jupyter? [英] How to fix ipykernel_launcher.py: error: unrecognized arguments in jupyter?
问题描述
我正在关注这个 tensorflow
但是当我尝试在 jupyter notebook 中运行相同的代码时,我收到此错误:
<块引用>用法:ipykernel_launcher.py [-h] [--batch_size BATCH_SIZE][--train_steps TRAIN_STEPS]ipykernel_launcher.py:错误:无法识别的参数:-f C:UsersdavidAppDataRoamingjupyter
untimekernel-4faecb24-6e87-40b4-bf15-5d24520d7130.json
发生异常,使用 %tb 查看完整的回溯.
SystemExit: 2C:Anaconda3envspython3xlibsite-packagesIPythoncoreinteractiveshell.py:2918:用户警告:要退出:使用退出"、退出"或 Ctrl-D.警告(退出:使用'退出','退出'或Ctrl-D.",stacklevel=1)
我尝试使用以下方法修复它但没有成功:
pip install --ignore-installed --upgrade jupyterpip 安装 ipykernelpython -m ipykernel 安装conda 安装笔记本 ipykernelipython kernelspec install-self
任何想法将不胜感激!谢谢!
我明白了!我们得到错误的原因是因为这段代码使用了 argparse
而这个模块是用来写用户友好的命令行界面的,所以看起来,它和 Jupyter Notebook 有冲突.
我在这个页面中找到了解决方案:
我们要做的是:
删除或注释这些行:
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='训练步骤数')
并替换args
args = parser.parse_args(argv[1:])
对于使用库 easydict
的字典方式:
args = easydict.EasyDict({batch_size":100,train_steps":1000})
使用 easydict
我们可以访问 dict 值作为参数的属性.
更新
在今年深入研究 python 之后,我发现这个问题的解决方案更简单(我们不需要使用任何外部库或方法).argparse
只是从终端将参数传递给 python 脚本的众多方法之一.当我尝试在 jupyter 笔记本中执行此操作时,这显然行不通.我们可以直接在函数中替换参数,如:
函数(batch_size=100,train_steps=1000)
现在,如果我们的函数有很长的参数列表,我们可以使用 *args
或 **kargs
.
*args
在函数中传递一个元组作为参数,对于这种情况,特别是:
args = (100, 1000)函数(*参数)
**kargs
将字典作为参数传递给我们的函数:
kargs = {batch_size":100,train_steps":1000}功能(**kargs)
只需谷歌一下,你就会找到一个关于如何使用它们的非常好的解释,这里有一个 我用来研究这个的文档.
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:UsersdavidAppDataRoamingjupyter untimekernel-4faecb24-6e87-40b4-bf15-5d24520d7130.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2 C:Anaconda3envspython3xlibsite-packagesIPythoncoreinteractiveshell.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!
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:
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')
and replace args
args = parser.parse_args(argv[1:])
for a dictionary using the library easydict
in this way:
args = easydict.EasyDict({
"batch_size": 100,
"train_steps": 1000
})
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:
funtion(batch_size=100, train_steps=1000)
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:
args = (100, 1000)
function(*args)
**kargs
pass a dictionary as arguments to our function:
kargs = {"batch_size": 100,
"train_steps": 1000}
function(**kargs)
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: error: unrecognized arguments in jupyter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!