作为一个很新的新人,今天尝试跑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