无法导入名称' mselossflat'从fastai.layers'
我正在运行代码 https://github.com/thiagodma/pytorch_exs/blob/master/master/multitasklearning/multitask_age_gender_ethnicity_ethnicity_resnet34.ipynb colab上。
当我写的时候:
from fastai import *
from fastai.vision import *
from fastai.layers import MSELossFlat, CrossEntropyFlat
from torchvision import transforms
import warnings
warnings.filterwarnings("ignore")
files_train = get_image_files("utkface_aligned_cropped/UTKFace")
files_valid = get_image_files("utkface_aligned_cropped/crop_part1")
我得到错误:
ImportError Traceback (most recent call last)
<ipython-input-4-07ceae9afcad> in <module>()
1 from fastai import *
2 from fastai.vision import *
----> 3 from fastai.layers import MSELossFlat, CrossEntropyFlat
4 from torchvision import transforms
5 import warnings
ImportError: cannot import name 'MSELossFlat' from 'fastai.layers' (/usr/local/lib/python3.7/dist-packages/fastai/layers.py)
I am running the code https://github.com/thiagodma/Pytorch_exs/blob/master/MultiTaskLearning/multitask_age_gender_ethnicity_resnet34.ipynb on Colab.
As I write:
from fastai import *
from fastai.vision import *
from fastai.layers import MSELossFlat, CrossEntropyFlat
from torchvision import transforms
import warnings
warnings.filterwarnings("ignore")
files_train = get_image_files("utkface_aligned_cropped/UTKFace")
files_valid = get_image_files("utkface_aligned_cropped/crop_part1")
I get the error:
ImportError Traceback (most recent call last)
<ipython-input-4-07ceae9afcad> in <module>()
1 from fastai import *
2 from fastai.vision import *
----> 3 from fastai.layers import MSELossFlat, CrossEntropyFlat
4 from torchvision import transforms
5 import warnings
ImportError: cannot import name 'MSELossFlat' from 'fastai.layers' (/usr/local/lib/python3.7/dist-packages/fastai/layers.py)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为您指的是使用
fastai
v2库的代码段。在最新的fastai
中,mselossflat
函数可以在fastai.losses
上导入。但是,由于您正在尝试一个旧的代码段,因此最好重现该代码的执行环境以获得预期的结果。我建议尝试在您的环境上安装
fastai
v2并再次执行代码。This is probably because you are referring to a code snippet that used the
fastai
v2 library. In the newestfastai
, theMSELossFlat
function can be imported atfastai.losses
.But since you are trying out an old code snippet, it's best to reproduce the execution enviroment of that code to get an expected result. I would recommend trying to install
fastai
v2 on your environment and execute the code again.