栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch:chunks为0的报错

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch:chunks为0的报错

File D:/Codes/code/Python Project/group_reid-master/group_reid-master/main_group_gcn_siamese_part_half_fulltest_sink.py , line 348, in train_gcn
 loss.backward()
 File D:CodesAnaconda3envspytorch_gpulibsite-packagestorchtensor.py , line 185, in backward
 torch.autograd.backward(self, gradient, retain_graph, create_graph)
 File D:CodesAnaconda3envspytorch_gpulibsite-packagestorchautograd__init__.py , line 127, in backward
 allow_unreachable True) # allow_unreachable flag
RuntimeError: chunk expects chunks to be greater than 0, got: 0
Exception raised from chunk at ..atensrcATennativeTensorShape.cpp:496 (most recent call first):

如图 一直报错chunk为0 最开始百思不得其解 在网上找资料没有和我类似的情况 键入报错的地方发现在loss求导时报错 下图 我寻思loss是调用的函数 不可能会报这种错误啊。于是直接丢在服务器上debug试试 由于pytorch的版本不同 报错的内容也不同。在1.1版本的pytorch上终于找到出错的地方了。

 loss.backward()

原来是在切块的时候维度不匹配导致的

env11_junk1 env11.squeeze().unsqueeze(0).unsqueeze(0).repeat((5-x1_valid.shape[0]), parts, 1)
env22_junk2 env22.squeeze().unsqueeze(0).unsqueeze(0).repeat((5-x2_valid.shape[0]), parts, 1)
env11 env11.squeeze().unsqueeze(0).unsqueeze(0).repeat(x1_valid.shape[0], parts, 1)
env22 env22.squeeze().unsqueeze(0).unsqueeze(0).repeat(x2_valid.shape[0], parts, 1)
 # calculate within graph and inter graph message
h_k1 torch.cat((self.W_x(x1[i, :sample_size1, :]), self.W_neib(x_neib1), self.W_relative(mu1), self.W_env(env11)), 2).unsqueeze(0) 
h_k_junk1 torch.cat((self.W_x(x1[i, sample_size1:, :]), self.W_x(x1[i, sample_size1:, :]), self.W_x(x1[i, sample_size1:, :]),self.W_env(env11_junk1)), 2).unsqueeze(0)
h_k2 torch.cat((self.W_x(x2[i, :sample_size2, :]), self.W_neib(x_neib2), self.W_relative(mu2), self.W_env(env22)), 2).unsqueeze(0)
h_k_junk2 torch.cat((self.W_x(x2[i, sample_size2:, :]), self.W_x(x2[i, sample_size2:, :]), self.W_x(x2[i, sample_size2:, :]),self.W_env(env22_junk2)), 2).unsqueeze(0) 

在我的代码中 squeeze和unsqueeze用的多余了 直接premute就好了 我本意是想在第一维度上复制和X1相同的第一维度 但是在实际的数据集中X1的第一维度有可能是为0的 因此repeat的参数为0的话就会报错了 不能小于原始的dim。由于报的错误不明显 所以耽误了半天时间想这个问题 特此记录一下。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267954.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号