在编写SVM中的Hinge loss函数的时候报错“'int' object has no attribute 'backward'”

for epoch in range(50):for batch in dataloader:opt.zero_grad()output=hinge_loss(svm(batch[0],w,b),batch[1]) output.backward()opt.step()draw_margin(w, b, camera)

报错的原因是output,也就是损失函数这里输出了int值。但是在实验过程中,梯度确实是下下降了。只是总是在下降过程中出现了这种报错。

经过排错,发现了hinge_loss函数中出现了问题

def hinge_loss(y_pred,y_true):return (0 if 0>(1-y_pred*y_true).mean() else (1-y_pred*y_true).mean()) 

注意,此时如果 0>(1-y_pred*y_true).mean() 这个条件成立,函数会返回0。0本身是个int类型的数。这就是为什么有时候梯度可以下降。有时候不可以。

将原函数修改为:

def hinge_loss(y_pred,y_true):#这里注意,返回0的时候,0的类型需要是tensor类型,并且需要梯度。return (torch.tensor(0.0,requires_grad=True) if 0>(1-y_pred*y_true).mean() else (1-y_pred*y_true).mean())  

'int' object has no attribute 'backward'报错 使用Pytorch编写 Hinge loss函数相关推荐

  1. AttributeError: 'NoneType' object has no attribute 'grid'报错解决方案

    1问题描述: 当我们在使用tkinter时经常遇到AttributeError: 'NoneType' object has no attribute 'grid'的报错 2.原因分析: import ...

  2. AttributeError: ‘Request‘ object has no attribute ‘is_xhr‘ 报错

    在用 Flask+VUE 做前后端分离的数据可视化项目时,遇到了这个报错:AttributeError: 'Request' object has no attribute 'is_xhr' 是 We ...

  3. AttributeError: 'Request' object has no attribute 'is_xhr' 报错的解决办法

    在用 Flask+VUE 做前后端分离的数据可视化项目时,遇到了这个报错:AttributeError: 'Request' object has no attribute 'is_xhr' 是 We ...

  4. AttributeError: 'NoneType' object has no attribute 'split' 报错处理

    报错场景 social_django 组件对原生 django 的支持较好, 但是因为 在此DRF进行的验证为 JWT 方式 和 django 的验证存在区别, 因此需要进行更改自行支持 JWT 方式 ...

  5. 【Django】‘function‘ object has no attribute ‘subjects‘报错的解决

    项目场景 创建了一个新文件夹 myapp ,用于保存新功能模块下实现功能的代码文件 views.py . 问题描述 运行项目时,报错 'function' object has no attribut ...

  6. AttributeError: ‘PosixPath‘ object has no attribute ‘rstrip‘ 报错问题记录

    环境 python3.6.5 django 3.1.5 问题定位及解决 最近准备开发一个在线教育的网站,各个环境都搭好了,并且测试demo 也能跑的好好的,为了以后 调试接口方便,所以我打算把swag ...

  7. ‘StandardScaler‘ object has no attribute ‘fit_tranform‘报错原因

    原代码: from sklearn.preprocessing import StandardScaler sc = StandardScaler()x_train=sc.fit_tranform(i ...

  8. 解决AttributeError: 'str' object has no attribute 'decode'报错问题

    顺着报错文件点进去,找到query = query.decode(errors='replace') 将decode修改为encode即可

  9. AttributeError: 'function' object has no attribute 'Document'报错解决

    原因是函数名称和系统中的某个名称相同的,替换另外的名字就行了 报错代码: #读取docx中的文本代码示例 import docx def docx():file=docx.Document(filen ...

最新文章

  1. 单片机怎么做定时器矩阵,彻底解决各种定时问题?
  2. idea ssh连接mysql数据库_mysql命令行客户端如何通过ssh服务器连接数据库啊?
  3. Effective Java之考虑自定义的序列化模式(七十五)
  4. c ++查找字符串_C ++数组| 查找输出程序| 套装5
  5. if else的使用以及如何从键盘获取数值
  6. python中生成器的两段代码
  7. pxe无人值守智能安装linux
  8. 售前更需要关注客户业务
  9. WPS简历模板的图标怎么修改_新媒体运营-简历模板范文,【工作经历+项目经验+自我评价】怎么写?...
  10. 从零玩转第三方登录之QQ登录
  11. JAVA ECXCEL 考勤导入查询
  12. JavaScript获取CSS内联样式和外联样式
  13. 元素偏移offset的常用属性
  14. 游戏热更新:游戏客户端热更新那点事
  15. strut2框架搭建
  16. ardupilot软件仿真及调试(vscode版)
  17. 视频损坏解决办法(个人记录)
  18. java 汉字转拼音工具_java汉字转拼音工具类
  19. 进入U盘PE系统无法识别硬盘分区的原因分析和解决方法
  20. mysql粗粒度_粗粒度(Coarse-grained)vs细粒度(fine-grained)

热门文章

  1. jQuery -gt; end方法的使用方法
  2. c++ 状态模式(state)
  3. 精悍的Python代码段-转
  4. 输入控件控制输入限制
  5. c#不可忍受之慢——谁是罪魁祸首
  6. 22-1图的遍历的源代码
  7. Java面向对象编程 第一章 面向对象开发方法概述
  8. ubuntu14.10环境下Apeach的安装配置
  9. Oracle 常用符号CHR
  10. 类模板 - C++快速入门45