Argparse in iPython notebook: unrecognized arguments: -f - python

I am trying to pass a .py file to ipython notebook environment.
I have never had to deal directly with argparse before. How do I rewrite the main() function?
I tried to delete the line of def main(): and keep the rest of the code.
But args = parser.parse_args()" returned an error:
ipykernel_launcher.py: error: unrecognized arguments: -f.
And when I run . %tb:
showing this
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='data/tinyshakespeare',
help='data directory containing input.txt')
parser.add_argument('--input_encoding', type=str, default=None,
help='character encoding of input.txt, from https://docs.python.org/3/library/codecs.html#standard-encodings')
parser.add_argument('--log_dir', type=str, default='logs',
help='directory containing tensorboard logs')
parser.add_argument('--save_dir', type=str, default='save',
help='directory to store checkpointed models')
parser.add_argument('--rnn_size', type=int, default=256,
help='size of RNN hidden state')
parser.add_argument('--num_layers', type=int, default=2,
help='number of layers in the RNN')
parser.add_argument('--model', type=str, default='lstm',
help='rnn, gru, or lstm')
parser.add_argument('--batch_size', type=int, default=50,
help='minibatch size')
parser.add_argument('--seq_length', type=int, default=25,
help='RNN sequence length')
parser.add_argument('--num_epochs', type=int, default=50,
help='number of epochs')
parser.add_argument('--save_every', type=int, default=1000,
help='save frequency')
parser.add_argument('--grad_clip', type=float, default=5.,
help='clip gradients at this value')
parser.add_argument('--learning_rate', type=float, default=0.002,
help='learning rate')
parser.add_argument('--decay_rate', type=float, default=0.97,
help='decay rate for rmsprop')
parser.add_argument('--gpu_mem', type=float, default=0.666,
help='%% of gpu memory to be allocated to this process. Default is 66.6%%')
parser.add_argument('--init_from', type=str, default=None,
help="""continue training from saved model at this path. Path must contain files saved by previous training process:
'config.pkl' : configuration;
'words_vocab.pkl' : vocabulary definitions;
'checkpoint' : paths to model file(s) (created by tf).
Note: this file contains absolute paths, be careful when moving files around;
'model.ckpt-*' : file(s) with model definition (created by tf)
""")
args = parser.parse_args()
train(args)

You can try args = parser.parse_args(args=[]).

It's better to use #nbro 's answer for Jupyter execution.
args = parser.parse_args(args=[])
If you want to manage parameters as class format, you can try this.
class Args:
data = './data/penn'
model = 'LSTM'
emsize = 200
nhid = 200
args=Args()

As #nbro suggested, the following command should work:
args = parser.parse_args(args=[])
In addition, if you have required arguments in your parser, set them inside the list:
args = parser.parse_args(args=['--req_1', '10', '--req_2', '10'])
Where you previously used:
import argparse
parser = argparse.ArgumentParser(description="Dummy parser")
parser.add_argument("--req_1", type=int, required=True, help="required int 1")
parser.add_argument("--req_2", type=int, required=True, help="required int 2")
You can also see from the notebook all params:
print("see all args:", args)
print("use one arg:", args.req_1)
You can find more information in the docs: Parsing arguments

An example is:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('echo')
args = parser.parse_args(['aa']) # actually you don't have to write (args=['aa'])
print(args.echo)
the output should be
>>> aa

Related

Arguments in top level parser come before subparsers if it has subparsers

I have this MRE:
import argparse
parser = argparse.ArgumentParser(description='Bundle a Python application')
parser.add_argument(
'-o', '--output', metavar='OUTPUT FILE', dest='file_name', type=str,
default=None)
parser.add_argument(
'--extensions', '--ext', action='store_const', metavar='EXTENSIONS',
dest='extensions', const=True, default=False,
help='Whether to allow the importing of C extensions (not needed if C extensions are optional')
if 0:
actions_parser = parser.add_subparsers(
dest='action', metavar='ACTION', help='Action mod should take')
actions_parser.required = True
build_parser = actions_parser.add_parser("build")
build_parser.add_argument(
dest='root', metavar='PROJECT', type=str, help='Project path',
nargs='?', default='.')
get_parser = actions_parser.add_parser("get")
get_parser.add_argument(
dest='module', metavar='MODULE', type=str, help='Module to download')
args = parser.parse_args()
If you run this with python test.py --ext, this works as expected.
However, if you change the 0 to a 1, then python test.py foo --ext fails, even though it should work. Why?
As it turns out, all the arguments attached to the main parser must be before any subaction, e.g. python test.py --ext foo.

How to implement parse_args() in pytorch?

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
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.

Issue with Parser

I am training an AI but I am getting this error and cant figure out why.
usage: car.py [-h] [--dataset /path/to/car/dataset/] --weights /path/to/weights.h5 [--logs /path/to/logs/] [--image path or URL to image] [--video path or URL to video]
car.py: error: the following arguments are required: , --weights
this is my parser
if __name__ == '__main__':
import argparse
# Parse command line arguments
parser = argparse.ArgumentParser(
description='Train Mask R-CNN to detect cars.')
parser.add_argument("command",
metavar="<command>",
help="'train' or 'splash'")
parser.add_argument('--dataset', required=False,
metavar="/path/to/car/dataset/",
help='Directory of the Car dataset')
parser.add_argument('--weights', required=True,
metavar="/path/to/weights.h5",
help="Path to weights .h5 file or 'coco'")
parser.add_argument('--logs', required=False,
default=DEFAULT_LOGS_DIR,
metavar="/path/to/logs/",
help='Logs and checkpoints directory (default=logs/)')
parser.add_argument('--image', required=False,
metavar="path or URL to image",
help='Image to apply the color splash effect on')
parser.add_argument('--video', required=False,
metavar="path or URL to video",
help='Video to apply the color splash effect on')
args = parser.parse_args()
The error says that your weights argument is required, and you yourself have written so in your code. It won't run until you provide the argument --weights in your call

conditional args parse in python

I would like to have a code that get the first argument as tool and after that expects different options depending on the tool
myapp.py [TOOL] [TOOLPARAMETERS]
python myapp.py tool0 -i 'path/to/my_image.jpg' -o 'path/to/my_output_path_folder/' -r 0.7 -m 13
python myapp.py tool1 -m 3 -r 'some_string_now' -m 13
As you can see in this case each tool could have it's own required arguments and in each case they could even expect different types
I have been messing around with add_subparsers with some success.
import argparse
parser = argparse.ArgumentParser(prog='myapp', usage='%(prog)s [tool] [options]')
parser.add_argument('-a', required=True, help='required globally argument for all tools')
subparsers = parser.add_subparsers(help='tool help')
# subparser for tool0
parser_tool0 = subparsers.add_parser('tool0', help='tool 0 help')
# ADD tool0 ARGUMENTS
parser_tool0.add_argument('-i' , required=True, help='path to the input folder')
parser_tool0.add_argument('-o' , required=True, help='path to the output folder')
parser_tool0.add_argument('-r' , default=0.7, type=float, help='some float')
parser_tool0.add_argument('-g' , default=10, type=int, help='some integrer')
parser_tool0.add_argument('-d' , default=False, help='debug')
# subparser for tool1
parser_tool0 = subparsers.add_parser('tool1', help='tool 0 help')
# ADD tool0 ARGUMENTS
parser_tool0.add_argument('-r' , help='r not required string')
parser_tool0.add_argument('-f' , required=True, help='f not required string')
parser_tool0.add_argument('-d' , default=0.7, type=float, help='some float')
parser_tool0.add_argument('-i' , default=10, type=int, help='some integrer')
parser_tool0.add_argument('-b' , default=False, help='boolean')
parser_tool0.add_argument('opts',
help='Modify config options using the command-line',
default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
print(args)
So I would like to know what should be the best practices for that case.
Thanks in advance
Per the docs and this so answer subparsers are the way to go.

python error :the following arguments are required

i am new to work with python and i want to run this code , but get this error.
code:
import argparse
import os
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(BASE_DIR)
sys.path.append(BASE_DIR)
from model import *
import indoor3d_util
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=0, help='GPU to use [default: GPU 0]')
parser.add_argument('--batch_size', type=int, default=1, help='Batch Size during training [default: 1]')
parser.add_argument('--num_point', type=int, default=4096, help='Point number [default: 4096]')
parser.add_argument('--model_path', required=True, help='model checkpoint file path')
parser.add_argument('--dump_dir', required=True, help='dump folder path')
parser.add_argument('--output_filelist', required=True, help='TXT filename, filelist, each line is an output for a room')
parser.add_argument('--room_data_filelist', required=True, help='TXT filename, filelist, each line is a test room data label file.')
parser.add_argument('--no_clutter', action='store_true', help='If true, donot count the clutter class')
parser.add_argument('--visu', action='store_true', help='Whether to output OBJ file for prediction visualization.')
FLAGS = parser.parse_args()
what should i do?
error :
batch_test.py: error: the following arguments are required: --model_path, --dump_dir, --output_filelist, --room_data_filelist
Those arguments are defined as required in your code required=True but don't have a default specified. You can either specify them at runtime or you can add a default value or you can make them not required with required=False
If you need additional help with any of those options, let me know.
You should see something like:
usage: batch_test.py [-h] [--gpu GPU] [--batch_size BATCH_SIZE]
[--num_point NUM_POINT] --model_path MODEL_PATH
--dump_dir DUMP_DIR --output_filelist OUTPUT_FILELIST
--room_data_filelist ROOM_DATA_FILELIST [--no_clutter]
[--visu]
And just append the arguments from command line, e.g.
python batch_test.py --model_path PATH_TO_YOUR_MODEL, --dump_dir YOUR_DUMP_DIR, --output_filelist OUT_LIST, --room_data_filelist ROOM_DATA_LIST
Or turn off the required from your source code.
When running the python script you showed, you have to include the arguments that are required (which has required=True) in the following format:
python batch_test.py <argument 1> <argument 1 value> <argument 2> <argument 2 value> ...
In the code you showed, there are several arguments that are required, which are:--model-path, --dump_dir, --output_filelist, --room_data_filelist.
You can actually see the instruction on how to fill each argument by entering the command:
python batch_test.py -h

Categories

Resources