当前位置:   article > 正文

Pytorch迁移学习之猫狗分类;create_feature_extractor提取模型向量_attributeerror: 'vgg' object has no attribute 'fc

attributeerror: 'vgg' object has no attribute 'fc

1、Pytorch迁移学习之猫狗分类

1.AttributeError: ‘VGG’ object has no attribute 'fc’错误
解决:fc换成classifier[6],并且带【索引】,索引值可以直接打印模型看到结构层
model= models.vgg16(pretrained=True) for param in model.parameters(): #params have requires_grad=True by default param.requires_grad = False num_ftrs = model.fc.in_features

2.element 0 of tensors does not require grad and does not have a grad_fn 错误
解决:这是数据出现错误,需要设置批数据的requires_grad值 data=Variable(data,requires_grad=True),标签值y则不用

        for i,(data,y) in enumerate(train_loader):
            data=Variable(data,requires_grad=True)
            # y=Variable(y,requires_grad=True)
            print(data.requires_grad)
  • 1
  • 2
  • 3
  • 4

数据结构,在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/858728
推荐阅读
相关标签
  

闽ICP备14008679号