ImportError: cannot import name 'as_tensor'

I imported fastai library in my local machine. I also pip install fast ai. I imported these

import json
import cv2
from PIL import ImageDraw, ImageFont
from matplotlib import patches, patheffects
from fastai.conv_learner import *
from fastai.dataset import *
from pathlib import Path

I get this error,
ImportError Traceback (most recent call last)
in
3 from PIL import ImageDraw, ImageFont
4 from matplotlib import patches, patheffects
----> 5 from fastai.conv_learner import *
6 from fastai.dataset import *
7 from pathlib import Path

~\Anaconda3\lib\site-packages\fastai\__init__.py in <module>
----> 1 from .basic_train import *
      2 from .callback import *
      3 #from .callbacks import *
      4 from .core import *
      5 from .basic_data import *

~\Anaconda3\lib\site-packages\fastai\basic_train.py in <module>
      1 "Provides basic training and validation with `Learner`"
----> 2 from .torch_core import *
      3 from .basic_data import *
      4 from .callback import *
      5 

~\Anaconda3\lib\site-packages\fastai\torch_core.py in <module>
      1 "Utility functions to help deal with tensors"
----> 2 from .imports.torch import *
      3 from .core import *
      4 
      5 AffineMatrix = Tensor

~\Anaconda3\lib\site-packages\fastai\imports\__init__.py in <module>
      1 from .core import *
----> 2 from .torch import *

~\Anaconda3\lib\site-packages\fastai\imports\torch.py in <module>
      1 import torch, torch.nn.functional as F
      2 from torch import ByteTensor, DoubleTensor, FloatTensor, HalfTensor, LongTensor, ShortTensor, Tensor
----> 3 from torch import nn, optim, as_tensor
      4 from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, TensorDataset

ImportError: cannot import name 'as_tensor'

Iam using pytorch 0.4. Can anyone help me with this one

1 Like

Iam using pytorch 0.4. Can anyone help me with this one

Not yet an expert, but I believe fastai v1 (which you are using if you’ve installed fastai w/ pip) depends on pytorch 1.x

I only see pytorch version 0.4.1 as latest in pytorch page. How to install pytorch 1.0??

I’m using google colab and this works for me:

from os.path import exists
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\.\([0-9]*\)\.\([0-9]*\)$/cu\1\2/'
accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'
!pip install torch_nightly -f https://download.pytorch.org/whl/nightly/{accelerator}/torch_nightly.html
!pip install fastai
import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.backends.cudnn.enabled)

Which I shamelessly borrowed from someone else on the net.

It just downloads and installs the most recently (nightly build) of pytorch (after doing some checks of your environment to get the right package).

The last few lines check if everything is working properly.

Probably will need tweaking for your use case but hopefully it points you in the right direction

EDIT::: This is not going to work as a normal python script, you’ll have to make the commands make sense in your environment

Patrick1, thank you for sharing what worked for you in Google’s Colab.

I was having the same error on my Windows 10 machine, and was able to solve it like this:
conda install pytorch -c pytorch

3 Likes

Soved it for me too. Windows 10, python3.6