异常:IndexError: tensors used as indices must be long, byte or bool tensors

详细异常

Traceback (most recent call last):File "D:/PythonCode/pytorch131/UltraGCN-main/main.py", line 624, in <module>train(ultragcn, optimizer, train_loader, test_loader, mask, test_ground_truth_list, interacted_items, params)File "D:/PythonCode/pytorch131/UltraGCN-main/main.py", line 410, in trainloss = model(users, pos_items, neg_items)File "C:\anaconda3\envs\pytorch131\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__result = self.forward(*input, **kwargs)File "D:/PythonCode/pytorch131/UltraGCN-main/main.py", line 359, in forwardomega_weight = self.get_omegas(users, pos_items, neg_items)File "D:/PythonCode/pytorch131/UltraGCN-main/main.py", line 300, in get_omegaspos_weight = self.constraint_mat[users * self.item_num + pos_items].to(device)
IndexError: tensors used as indices must be long, byte or bool tensors
# 错误语句
pos_weight = self.constraint_mat[users * self.item_num + pos_items].to(device)

这个异常不是一个单一异常,后面所有用的 user, pos_items, neg_items 等等语句都可能报这个异常。

所以要找到根源的 user, pos_items, neg_items 变量。

说明,每个代码的异常变量都不一样,要测试好是那个变量的问题。可以通过逐一改变变量的类型(.long())测试。

我的根源语句是

users = users.to(device)
pos_items = pos_items.to(device)
neg_items = neg_items.to(device)

改正后

users = users.long().to(device)
pos_items = pos_items.long().to(device)
neg_items = neg_items.long().to(device)

看的其他的解答,pytorch框架 下long() 也可以换为.type(torch.long)

异常:IndexError: tensors used as indices must be long, byte or bool tensors相关推荐

  1. IndexError: tensors used as indices must be long, byte or bool tensors

    下面的程序会报错IndexError: tensors used as indices must be long, byte or bool tensors mask = torch.Tensor([ ...

  2. tensors used as indices must be long or byte tensors

    tensors used as indices must be long or byte tensors import torch import numpy as np a=torch.zeros(( ...

  3. 成功解决IndexError: arrays used as indices must be of integer (or boolean) type

    成功解决IndexError: arrays used as indices must be of integer (or boolean) type 目录 解决问题 解决思路 解决方法 解决问题 I ...

  4. 【pyradiomics】bugFix:GLCM特征时:IndexError: arrays used as indices must be of integer (or boolean) type

    GLCM特征时出现bug:IndexError: arrays used as indices must be of integer (or boolean) type 解决方法:https://gi ...

  5. 【Sketch2Pose项目环境搭建】win10+Anoconda+VScode

    最近我尝试在windows上运行Sketch2Pose这个项目,但前前后后花了很久,疯狂踩坑,嗯,很崩溃,遇到很多问题.于是写这篇博客,结合原仓库Readme内容,记录一下项目环境搭建的过程. 搭建环 ...

  6. 三元组损失tripletloss

    雷郭出品 先验知识 1.Triplet loss 2. Hard Triplets选择 先验知识 题外话(来自网络): (最近,learning to rank 的思想逐渐被应用到很多领域,比如goo ...

  7. python中slice 和 indices使用

    一般来说,内置的slice()函数会创建一个切片对象,可以用在任何允许进行切片操作的地方. 下面是slice的简介: # slice 两种用法 class slice(stop)class slice ...

  8. python进阶八_警告和异常

    心情有点纠结,怎么说呢,倒不是因为其他学习上的事情,反而是因为生活上狗血的剧情逼着人偏离,渐行渐远,人跟人之间有误会也是正常的,可能是因为交流不够,彼此不够了解吧,希望能尽快度过这一段纠结的日子,简单 ...

  9. Python基础(九)--异常

    Python基础(九)--异常 1 异常相关概念 1.1 什么是异常 异常是程序运行过程中产生的一种事件,该事件会打乱程序的正常流程.可以说,异常就是一种意外,指程序没有按照正常或期望的方式执行. 当 ...

最新文章

  1. Redis在Windows上编译
  2. gin 项目结构_Gin框架 - 项目目录
  3. python 安装包时出现红字_Python安装(Windows 7 8 10)
  4. OC__part11.2
  5. Runtime error 216 at xxx 故障解决一例
  6. 数据科学 IPython 笔记本 9.9 花式索引
  7. 基于文本和语音的双模态情感分析
  8. React Native Fetch封装那点事...
  9. gulp自己主动化任务脚本在HybridApp开发中的使用
  10. 自由缩放属性-resize(禁止textarea的自由缩放尺寸功能)
  11. 服务器端提交form
  12. 【Python】Json序列化和反序列化模块dumps、loads、dump、load函数介绍
  13. 鲁棒控制(Ⅰ)—LMI处理方法
  14. 阿里巴巴矢量字体库更改设置
  15. Git-删除文件后找回
  16. Shake Shack上海第7家门店开业
  17. android获取短信中心号
  18. 搜狐畅言评论系统使用
  19. 转速/线速度/角速度计算FC
  20. Linux环境下Font font = new Font(黑体, Font.BOLD, 18)[BufferedImage~Graphics~drawString]中文乱码

热门文章

  1. ubuntu安装pycuda软件
  2. 戴尔服务器安装Exsi Server
  3. 【LeetCode】IPO(使用贪心求解)
  4. 社会即将分层,你将会在第几层?
  5. 全国计算机等级陕西一级题库,全国计算机等级考试一级试题库
  6. Windows命令提示符大全
  7. 知识付费:割裂的圈子,模糊的未来
  8. Golang Gin embed static 静态文件嵌入
  9. 还不了解国际化视野与交叉学科思维?收下这份让你学业职业成长的秘籍!
  10. RBM(限制玻尔兹曼机)、DBN(深度信念网络)介绍