1 报错:AttributeError: module ‘torchtext.data’ has no attribute ‘Field’

原文代码如下

import math
import time
import numpy as np
import torch
import torch.nn.functional as F
from torchtext import data

# 两个Field对象定义字段的处理方法(文本字段、标签字段)
TEXT = data.Field(tokenize=lambda x: x.split(), lower=True)
LABEL = data.LabelField(dtype=torch.float)

更改成如下引用即可

import math
import time
import numpy as np
import torch
import torchtext
import torch.nn.functional as F
from torchtext.legacy import data
from torchtext.legacy.data import Field, TabularDataset, Iterator, BucketIterator

2 报错:TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

问题描述
如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到numpy格式。 numpy不能读取CUDA tensor 需要将它转化为 CPU tensor

self.cpu().numpy()
torch.tensor(self, device='cpu')

3 报错在导入torch的时候报错

报错内容:TqdmWarning: IProgress not found. Please update jupyter and ipywidgets.

解决办法:

  1. 卸载jupyter:pip uninstall jupyter
  2. 卸载ipywidgets:pip uninstall ipywidgets
  3. 安装jupyter:pip install jupyter
  4. 安装ipywidgets:pip install ipywidgets
  5. 关联:jupyter nbextension enable –py widgetsnbextension