如何在pytorch中实现parse_args()?

发布于 2025-02-12 11:07:46 字数 2168 浏览 0 评论 0原文

我正在尝试为我的深度学习模型编码一个模块。我希望使用ArgeParse存储这些论点。 args = parser.parse_args()中存在一些问题!另外,使用ArgParse库有什么好处?

import numpy as np
import argparse
    
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type=str,
                        help='Dataset')
    parser.add_argument('--epoch', type=int, default=40,
                        help='Training Epochs')
    parser.add_argument('--node_dim', type=int, default=64,
                        help='Node dimension')
    parser.add_argument('--num_channels', type=int, default=2,
                        help='number of channels')
    parser.add_argument('--lr', type=float, default=0.005,
                        help='learning rate')
    parser.add_argument('--weight_decay', type=float, default=0.001,
                        help='l2 reg')
    parser.add_argument('--num_layers', type=int, default=2,
                        help='number of layer')
    parser.add_argument('--norm', type=str, default='true',
                        help='normalization')
    parser.add_argument('--adaptive_lr', type=str, default='false',
                        help='adaptive learning rate')

    args = parser.parse_args()
    
    print(args)

上面的代码是完整代码的一部分,如下面的链接

错误:

usage: ipykernel_launcher.py [-h] [--dataset DATASET] [--epoch EPOCH]
                             [--node_dim NODE_DIM]
                             [--num_channels NUM_CHANNELS] [--lr LR]
                             [--weight_decay WEIGHT_DECAY]
                             [--num_layers NUM_LAYERS] [--norm NORM]
                             [--adaptive_lr ADAPTIVE_LR]
ipykernel_launcher.py: error: unrecognized arguments: -f /Users/anshumansinha/Library/Jupyter/runtime/kernel-1e4c0f41-a4d7-4388-be24-640dd3411f56.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

上述代码是代码的一部分,可以在以下github链接:链接

I am trying to code a module for my deep learning model. I wish to store these arguments using argeparse. There is some problem occuring in args = parser.parse_args()! Also What are the benefits of using the argparse library?

import numpy as np
import argparse
    
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type=str,
                        help='Dataset')
    parser.add_argument('--epoch', type=int, default=40,
                        help='Training Epochs')
    parser.add_argument('--node_dim', type=int, default=64,
                        help='Node dimension')
    parser.add_argument('--num_channels', type=int, default=2,
                        help='number of channels')
    parser.add_argument('--lr', type=float, default=0.005,
                        help='learning rate')
    parser.add_argument('--weight_decay', type=float, default=0.001,
                        help='l2 reg')
    parser.add_argument('--num_layers', type=int, default=2,
                        help='number of layer')
    parser.add_argument('--norm', type=str, default='true',
                        help='normalization')
    parser.add_argument('--adaptive_lr', type=str, default='false',
                        help='adaptive learning rate')

    args = parser.parse_args()
    
    print(args)

The above code is a part of full code, as given in the link below

Error:

usage: ipykernel_launcher.py [-h] [--dataset DATASET] [--epoch EPOCH]
                             [--node_dim NODE_DIM]
                             [--num_channels NUM_CHANNELS] [--lr LR]
                             [--weight_decay WEIGHT_DECAY]
                             [--num_layers NUM_LAYERS] [--norm NORM]
                             [--adaptive_lr ADAPTIVE_LR]
ipykernel_launcher.py: error: unrecognized arguments: -f /Users/anshumansinha/Library/Jupyter/runtime/kernel-1e4c0f41-a4d7-4388-be24-640dd3411f56.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

The above code is a part of a code, the full code can be seen on the following github link : link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2025-02-19 11:07:46

您使用参数-f称为ipykernel_launcher.py。但是-f不在您的参数列表中。
-f是参数,您要用于输入文件吗?然后,您应该将类​​似的内容添加到您的代码中:

parser.add_argument('--file', '-f', type=str)

ArgParse的好处是,您不必手工编写代码来解析参数。您可以尝试一下。它比通常想象的更多的代码。特别是如果您有位置论点。

You called ipykernel_launcher.py with the argument -f. But -f is not in your list of arguments.
Is -f the argument, which you want to use for the input file? Then you should add something like this to your code:

parser.add_argument('--file', '-f', type=str)

The benefit of argparse is that you don't have to write by hand the code for parsing the arguments. You can try this. It is more code than one normally thinks. Especially if you have positional arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文