-1

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

Anshuman Sinha
  • 157
  • 1
  • 9
  • 1
    How do you run this program? – MisterMiyagi Jul 03 '22 at 19:31
  • @MisterMiyagi I did not follow your question. Do you wish to know what inputs I give while running this program? It is a part of the main.py module , the overall code is shown here [link](https://github.com/seongjunyun/Graph_Transformer_Networks/blob/master/main.py) – Anshuman Sinha Jul 03 '22 at 19:34
  • Literally what do you do to run this? Do you type something on the shell? Do you use a GUI? Do you use Jupyter somehow? – MisterMiyagi Jul 03 '22 at 19:36
  • Jupyter notebook only! – Anshuman Sinha Jul 03 '22 at 19:38
  • Note that external code may as well not exist, unless it is purely for trivia. Questions should be self-contained and work even when links rot, become inaccessible, or simply for a search view. See the [MRE] help page for details how to best help us help you. – MisterMiyagi Jul 03 '22 at 19:39
  • Jupyter notebook only *how*? `! ` magic, subprocess, terminal, ... , how exactly do you run this program? – MisterMiyagi Jul 03 '22 at 19:40
  • 2
    `argparse` helps when specifying values from a OS shell call. You are using jupter (notebook). Any commandline values get used by the server. What you are seeing is a message from the server to your notebook. – hpaulj Jul 03 '22 at 19:42
  • @MisterMiyagi Currently, I am trying to understand this program, so at the moment I am running this code snippet on jupyter notebook so that I can understand what all is happening within the code! Kindly provide help in that line, on what shall I do better or provide some correct direction to where I can look further! – Anshuman Sinha Jul 03 '22 at 19:46
  • 1
    Did you paste the code into a notebook cell and execute that by any chance? – MisterMiyagi Jul 03 '22 at 20:12
  • Yes, I just called a part of the code in order to understand the working of the code! I wanted to know what is being stored in args from the line args = parser.parse_args(). That's why I printed it, and got the error. I wish to convert this whole pytorch code into tensorflow that's why I am doing all this study! – Anshuman Sinha Jul 03 '22 at 20:13
  • @MisterMiyagi Are you able to see my full code? That's the code which I am trying to convert into tensorflow! But the Pytorch uses different kind of structure/ pipeline, where they use computational graphs and calculate loss and optimise it! – Anshuman Sinha Jul 03 '22 at 20:15
  • I am following this tutorial of custom training in tensorflow for help , but seems very difficult when no one is proving any direction! [link](https://www.tensorflow.org/tutorials/customization/custom_training_walkthrough) – Anshuman Sinha Jul 03 '22 at 20:16
  • 2
    When run as intended, not in a notebook, `args` will be an object with values like `args.dataset` and `args.epoch`. It can be simulated, but for understand only it may not be worth the effort. – hpaulj Jul 03 '22 at 20:19
  • 1
    Do you want to your jupyter cell for using cmd line arg? Because here you tried to run jupyter cell with code which expects cmd line arg. – GodWin1100 Jul 03 '22 at 20:27
  • I try to call the program with arguments from the terminal with the code: python untitled0.py --dataset 'dataset' --epoch 4 --node_dim 1 --num_channels 5 --num_layers 5 --norm 'norm' --adaptive_lr '0.005'. There is no error in your code. But how did you try to call it? From a jupyter notebook? – Hüma Jul 03 '22 at 20:06
  • I just called a part of the code in order to understand the working of the code! I wanted to know what is being stored in `args` from the line `args = parser.parse_args()`. That's why I printed it, and got the error. I wish to convert this whole pytorch code into tensorflow that's why I am doing all this study! – Anshuman Sinha Jul 03 '22 at 20:11
  • Do you know any way in which I can break the codes into usefull pieces and convert them into tensorflow! Any information will be much help for me! – Anshuman Sinha Jul 03 '22 at 20:12
  • @GodWin Yes I would run a cmd line , `$ python main.py --dataset ACM --num_layers 2 --adaptive_lr true` . This is the line which I'll use to start my code. The dataset is a graph dataset containing nodes and edges – Anshuman Sinha Jul 04 '22 at 08:36

1 Answers1

0

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.

habrewning
  • 735
  • 3
  • 12
  • Yes in the following lines of the code I used to load my dataset as f; the data set contains `node_features` , `edges` and `labels` . I have also attached the full code as an edit! `with open('data/'+args.dataset+'/node_features.pkl','rb') as f: node_features = pickle.load(f) with open('data/'+args.dataset+'/edges.pkl','rb') as f: edges = pickle.load(f) with open('data/'+args.dataset+'/labels.pkl','rb') as f: labels = pickle.load(f)` – Anshuman Sinha Jul 03 '22 at 19:43
  • what would have been my code structure if I was not using argparse? You have mentioned that I would've need to write by hand! How would that have panned out? Thanks for the answer! – Anshuman Sinha Jul 04 '22 at 08:34
  • 1
    You would need something like this: [link](https://stackoverflow.com/a/20069/19446851) – habrewning Jul 04 '22 at 08:48
  • Hey, also I don't see that there is any defined values for these arguments in the code! Then what's the use of these arguments? Or do we define them on the very first line like: `$ python main.py --dataset DBLP --num_layers 3` ? But in this line they only define 2 arguments `dataset` and `--num_layers` , then why have they given so many arguments in the main function? Is it to make the code more generalisable? – Anshuman Sinha Jul 04 '22 at 09:00
  • 1
    Yes, that is how the main function can be called. These other arguments have some purpose in case someone wants to do very specific things. But they are optional, so in the normal case not used. Someone who is more familiar with Jupyter may know the purpose of all these arguments. As Jupyter is also developed in Python, your question is valid. Why do we need such command argument mechanism? Jupyter in principle does not have to use the command line interface. Maybe this can be configured somewhere in Jupyter. But as I am not familiar with Jupyter, I cannot tell you this. – habrewning Jul 04 '22 at 09:12