I was using colab: I want to run a .py file having argparse function to train a model

70 Views Asked by At
if __name__ == '__main__':
    torch.cuda.empty_cache()
    
    """Parameters"""
    parser  = argparse.ArgumentParser(description = "Emotion Classifier" )
    parser.add_argument( "--batch", type=int, help = "batch_size", default = 1)
    
    parser.add_argument( "--epoch", type=int, help = 'training epohcs', default = 10) # 12 for iemocap
    parser.add_argument( "--norm", type=int, help = "max_grad_norm", default = 10)
    parser.add_argument( "--lr", type=float, help = "learning rate", default = 1e-6) # 1e-5
    parser.add_argument( "--sample", type=float, help = "sampling trainign dataset", default = 1.0) # 

    parser.add_argument( "--dataset", help = 'MELD or EMORY or iemocap or dailydialog', default = 'MELD')
    
    parser.add_argument( "--pretrained", help = 'roberta-large or bert-large-uncased or gpt2 or gpt2-large or gpt2-medium', default = 'roberta-large')    
    parser.add_argument( "--initial", help = 'pretrained or scratch', default = 'pretrained')
    parser.add_argument('-dya', '--dyadic', action='store_true', help='dyadic conversation')
    parser.add_argument('-fr', '--freeze', action='store_true', help='freezing PM')
    parser.add_argument( "--cls", help = 'emotion or sentiment', default = 'emotion')
        
    args = parser.parse_args()
    
    logger = logging.getLogger(__name__)
    streamHandler = logging.StreamHandler()
    
    # Add the line below to fix the issue.
    sys.exit(main())

here's the error colab is showing

usage: colab_kernel_launcher.py [-h] [--batch BATCH] [--epoch EPOCH] [--norm NORM] [--lr LR]
                                [--sample SAMPLE] [--dataset DATASET] [--pretrained PRETRAINED]
                                [--initial INITIAL] [-dya] [-fr] [--cls CLS]
colab_kernel_launcher.py: error: unrecognized arguments: /root/.local/share/jupyter/runtime/kernel-1fba85a1-85fd-4471-a790-9c103c9dcac9.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2
/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py:3561: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

I have a train.py file, that I want to run on my colab.

1

There are 1 best solutions below

0
furas On

When you run parse_args() then this gets values from sys.argv.
I can't test it but when server runs colab then it may puts in sys.argv some information for colab and this makes problem.

But you can put your values directly in parse_args() like

args = parser.parse_args( ["--sample", "your_filename"] )

or with empty list to use default values

args = parser.parse_args( [] )

BTW:

I don't see if your code uses args in any place - so I don't know if you really needs ArgumentParser and rest. Maybe you should remove it.

If you really use args in main() then it would be more readable to send it as argument like

def main(args):       # get `args` in function 
    # ... code ...

# ... 

sys.exit(main(args))  # send `args` to function