Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

torch.optim - AttributeError: partially initialized module 'torch' has no attribute '_jit_internal' (most likely due to a circular import)

Currently there is some issue about torch in my computer.

When I initiate the below code,

import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torch

It works but when I add "from torch.optim import Adam,AdamW"

import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torch

from torch.optim import Adam,AdamW

And initiate, then it gives me

AttributeError: partially initialized module 'torch' has no attribute '_jit_internal' (most likely due to a circular import)

This error. Can someone help me what is the problem?

Know what is the actual problem

like image 909
user25118365 Avatar asked Sep 07 '25 19:09

user25118365


1 Answers

I have had this multiple times before and from experience can tell you that this error can occur if you have a file named torch.py or another conflicting module name in the same directory as your script. Python may mistakenly try to import your script rather than the actual PyTorch module. If you have a file named torch.py rename it to something else and check your directory to ensure there are no conflicting filenames. Another reason this can happen is a circular import (where a module tries to import itself). Try checking any imports you use if they import themselves. I hope this helps you!

like image 200
Maximillion Avatar answered Sep 09 '25 15:09

Maximillion