Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named 'torch.nn.functional'

Tags:

python

pytorch

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
like image 481
vico Avatar asked Jan 18 '26 21:01

vico


1 Answers

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
like image 89
Michael Jungo Avatar answered Jan 20 '26 11:01

Michael Jungo