Search code examples
pythonpytorchgoogle-colaboratorymnisttorchvision

NameError: name 'transforms' is not defined, while using google colab


train = datasets.MNIST("", train=True, download=True,transform = transforms.Compose([transforms.ToTensor()]))
test = datasets.MNIST("", train=False, download=True,transform = transforms.Compose([transforms.ToTensor()]))`

After executing this on colab notebook, I am getting this error:

Traceback (most recent call last)
<ipython-input-6-b81aa6cf1cbe> in <module>()
----> 1 train = datasets.MNIST("", train=True, download=True,transform = transforms.Compose([transforms.ToTensor()]))
      2 test = datasets.MNIST("", train=False, download=True,transform = transforms.Compose([transforms.ToTensor()]))

NameError: name 'transforms' is not defined*

Solution

  • I'm guessing from the context that you're using Pytorch, in which case you need to make sure you have:

    from torchvision import transforms
    

    In your imports. By the looks of things you have already imported datasets from the same library.