vico
vico

Reputation: 18191

No module named 'torch.nn.functional'

I have python file with lines:

import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable

It generates errors:

  File "C:\gdrive\python\a.py", line 5, in <module>
import torch.nn.functional as F
ModuleNotFoundError: No module named 'torch.nn.functional'

How to fix that error?

I have installed pytorch by using command:

conda install pytorch-cpu torchvision-cpu -c pytorch

Upvotes: 2

Views: 19438

Answers (2)

Shanika Perera
Shanika Perera

Reputation: 25

Try this. from torch.nn import functional as F

Upvotes: 3

Michael Jungo
Michael Jungo

Reputation: 32992

It looks like you have an outdated version of PyTorch. Conda - pytorch-cpu was last published over a year ago and its latest version of PyTorch is 1.1.0, whereas PyTorch is currently at version 1.5.0. That packages has been abandoned.

You should install PyTorch with the official instructions given on PyTorch - Get Started locally, by selecting the version you want. In your case that would be Conda with CUDA None (to get the CPU only version).

The resulting command is:

conda install pytorch torchvision cpuonly -c pytorch

Upvotes: 5

Related Questions