'NoneType' object has no attribute

When I implement the following code:

It gives me the following error
File “make_sample_data.py”, line 149, in
main(args)
File “make_sample_data.py”, line 132, in main
if args.load_pretrained:
AttributeError: ‘NoneType’ object has no attribute ‘load_pretrained’

Could you check, which attributes args contains?
I just tried to argparse code and it’s working correctly and returns True as the default for args.load_pretrained.

1 Like

I am sorry, I am not getting. it is still showing same error.

Add a print statement after line 147 as print(args) and check, if args.load_pretrained is defined and if so which value it’s using.

it is not printing anything and showing same error
Traceback (most recent call last):
File “make_sample_data.py”, line 150, in
main(args)
File “make_sample_data.py”, line 131, in main
if args.load_pretrained:
AttributeError: ‘NoneType’ object has no attribute ‘load_pretrained’

In that case the get_args method returns None and I don’t know why this would be the case.
Are you running your script in a Jupyter notebook? If so, could you use a terminal and rerun the script?

1 Like

No, I am not running on Jupyter, I am running on Visual studio and I also run on terminal. It is still giving same error.

Sorry, in that case I don’t know what might be causing this issue, as this code snippet successfully returns the default arguments:

def get_args():
    parser = argparse.ArgumentParser("parameters")

    parser.add_argument('--p', type=int, default=10, help='P being a parameter accounting for the fact that we expect only a few number of relevant classes to occur in each image. (default: 10)')
    parser.add_argument('--k', type=int, default=1000, help=', there is an important trade-off on K that strongly depends on the ratio K/M. (default: 1000)')
    parser.add_argument('--batch-size', type=int, default=100, help='batch size, (default: 100)')
    parser.add_argument('--input-size', type=tuple, default=(32, 32), help='input data size, (default: (32, 32))')
    parser.add_argument('--load-pretrained', type=bool, default=True)

    args = parser.parse_args()

    return args

args = get_args()
print(args)
> Namespace(batch_size=100, input_size=(32, 32), k=1000, load_pretrained=True, p=10)
print(args.load_pretrained)
> True

It seems that your current environment isn’t able to parse the arguments and based on this I would recommend to post the issue (without the PyTorch code) on e.g. StackOverflow.

1 Like