当前位置: 首页 > news >正文

【报错】view size is not compatible with input tensor‘s size and stride

完整报错

Traceback (most recent call last): File "D:\360MoveData\Users\HONOR\whu\TwoStageTraining.py", line 590, in <module> criterion=seg_criterion, save_dir='./models', writer=writer_first_stage) File "D:\360MoveData\Users\HONOR\whu\TwoStageTraining.py", line 317, in train_segmentation_only iou = calculate_iou(seg_preds, masks, num_classes=2, ignore_index=0) File "D:\360MoveData\Users\HONOR\whu\TwoStageTraining.py", line 229, in calculate_iou targets = targets.view(-1) RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead. Process finished with exit code 1

报错原因

PyTorch 的 .view() 方法要求张量在内存中是连续存储的。如果张量经过某些操作(如 transpose, permute)后不再是连续存储,.view() 就会报错。

应该使用 .reshape() 来代替 .view(),因为 .reshape() 在必要时会自动复制数据以保证形状正确,即使张量不连续也能工作。

解决办法

将.view()改为.reshape()

比如修改前:

preds = preds.view(-1)
targets = targets.view(-1)

则修改后:

preds = preds.reshape(-1)
targets = targets.reshape(-1)

http://www.dtcms.com/a/179525.html

相关文章:

  • 编译原理头歌实验:词法分析程序设计与实现(C语言版)
  • Oracle OCP认证考试考点详解083系列12
  • SpringBoot中使用MCP和通义千问来处理和分析数据-连接本地数据库并生成实体类
  • 15前端项目----用户信息/导航守卫
  • SNMP 协议介绍、开发方法及示例
  • 【造包工具】【Xcap】精讲Xcap构造分片包(IPv4、ipv6、4G\5G等pcap均可),图解超赞超详细!!!
  • 投资逻辑与未来风险:高端PCB的黄金周期能持续多久?
  • 【Linux网络】网络命令
  • mongodb升级、改单节点模式
  • 矢量网络分析仪测驻波比:从原理到实战操作全解析
  • Nacos源码—6.Nacos升级gRPC分析一
  • 【redis】分片方案
  • 数据结构(二)——线性表的链式表示和实现
  • 关键字where
  • STM32智能刷卡消费系统(uC/OS-III)
  • LeetCode 11.盛最多水的容器 (Java)
  • CBO和HBO区别及介绍
  • 【软件测试学习day7】Junit5
  • 抛物线法(二次插值法)
  • C++卡特兰数讲解
  • 避免数据丢失:在存储测试数据之前,要做好Redis持久化
  • 牙科CBCT性能检测模体的革新价值
  • springmvc的入门案例
  • 链表的面试题4之合并有序链表
  • JDBC:java与数据库连接,Maven,MyBatis
  • C++漫步结构与平衡的殿堂:AVL树
  • 基于卫星遥感数据进行农作物长势监测原理简述
  • Spring普通配置类 vs 自动配置类-笔记
  • 2.5 特征值与特征向量
  • 适配国产化,私有化部署的局域网即时通讯工具-BeeWorks