20

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)
2
  • I use ipython a lot, but not the notebooks. But if I recall other SO correctly you can't pass script specific commandline arguments to a notebook. You can just use the arguments that ipython uses to start the server. What commandline are you using (that includes the -f flag)? stackoverflow.com/questions/43057529/… Commented Aug 22, 2017 at 18:39
  • What was the command line that gave this error? Commented Sep 20, 2017 at 22:04

4 Answers 4

50

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

Sign up to request clarification or add additional context in comments.

Comments

11

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

1 Comment

Can I pass the required argument values from the other notebook?
9

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()

1 Comment

Solution of @nbro is easier and doesn't require modifying existing parser
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.