Jupyter notebook 上无法使用argparse.parse_args()

作为一个很新的新人,今天尝试跑python代码,但是pycharm一键跑完,无法了解每一个函数功能,由此,想用jupyter notebook来看一下:

当用argparse生成参数模块的时候报错:

usage: ipykernel_launcher.py [-h] [-t TITLE] [-ppi PPI] [-omic OMIC]
                             [-cancer CANCER] [-e EPOCHS] [-p PATIENCE]
                             [-dp DP] [-lr LR] [-wd WD] [-hs1 HS1] [-hs2 HS2]
                             [-thr_go THR_GO] [-thr_seq THR_SEQ]
                             [-thr_exp THR_EXP] [-thr_path THR_PATH]
                             [-seed SEED]
ipykernel_launcher.py: error: unrecognized arguments: -f /home/kkjz/.local/share/jupyter/runtime/kernel-82db4994-aaf3-48c0-8abe-38c249a68ada.json

跑的代码是这样的:

import argparse

def parse_args():
    parser = argparse.ArgumentParser(
        description='Train MODIG with cross-validation and save model to file')
    parser.add_argument('-t', '--title', help='the name of running experiment',
                        dest='title',
                        default=None,
                        type=str
                        )
    parser.add_argument('-ppi', '--ppi', help='the chosen type of PPI',
                        dest='ppi',
                        default='CPDB',
                        type=str
                        )
    parser.add_argument('-omic', '--omic', help='the chosen node attribute [multiomic, snv, cnv, mrna, dm]',
                        dest='omic',
                        default='multiomic',
                        type=str
                        )
    parser.add_argument('-cancer', '--cancer', help='the model on pancan or specific cancer type',
                        dest='cancer',
                        default='pancan',
                        type=str
                        )
    parser.add_argument('-e', '--epochs', help='maximum number of epochs (default: 1000)',
                        dest='epochs',
                        default=1000,
                        type=int
                        )
    parser.add_argument('-p', '--patience', help='patience (default: 20)',
                        dest='patience',
                        default=20,
                        type=int
                        )
    parser.add_argument('-dp', '--dropout', help='the dropout rate (default: 0.25)',
                        dest='dp',
                        default=0.25,
                        type=float
                        )
    parser.add_argument('-lr', '--learningrate', help='the learning rate (default: 0.001)',
                        dest='lr',
                        default=0.001,
                        type=float
                        )
    parser.add_argument('-wd', '--weightdecay', help='the weight decay (default: 0.0005)',
                        dest='wd',
                        default=0.0005,
                        type=float
                        )
    parser.add_argument('-hs1', '--hiddensize1', help='the hidden size of first convolution layer (default: 300)',
                        dest='hs1',
                        default=300,
                        type=int
                        )
    parser.add_argument('-hs2', '--hiddensize2', help='the hidden size of second convolution layer (default: 100)',
                        dest='hs2',
                        default=100,
                        type=int
                        )
    parser.add_argument('-thr_go', '--thr_go', help='the threshold for GO semantic similarity (default: 0.8)',
                        dest='thr_go',
                        default=0.8,
                        type=float
                        )
    parser.add_argument('-thr_seq', '--thr_seq', help='the threshold for gene sequence similarity (default: 0.8)',
                        dest='thr_seq',
                        default=0.8,
                        type=float
                        )
    parser.add_argument('-thr_exp', '--thr_exp', help='the threshold for tissue co-expression pattern (default: 0.8)',
                        dest='thr_exp',
                        default=0.8,
                        type=float
                        )
    parser.add_argument('-thr_path', '--thr_path', help='the threshold of gene pathway co-occurrence (default: 0.6)',
                        dest='thr_path',
                        default=0.6,
                        type=float
                        )
    parser.add_argument('-seed', '--seed', help='the random seed (default: 42)',
                        dest='seed',
                        default=42,
                        type=int
                        )
    args = parser.parse_args()
    return args

if __name__ == '__main__':

    args = parse_args()

别看长,其实就三步,导入包,定义参数,运行,具体 argparse的介绍,请看别人的。这里只讲出现这种错误的解决办法:

args = parser.parse_args(args=[])
    return args

只需要在 parser.parse_args()中加入args=[]参数就可以了!

解决:

具体原理传参数问题!!!

还可以更改参数,只要不定义为=【】空就好:

def parse_args(args):
“”“”“
“”“”“””
    args = parser.parse_args(args)
    return args

 

 

参考链接:Jupyter notebook 上无法使用argparse.parse_args()-易采站长站

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值