RuntimeError: The size of tensor a (7375) must match the size of tensor b (6776) at non-singleton dimension 0

报错的代码位置:

1.File "F:\recommend experience\doing\modeldemo.py"中的: 

 self.user_dgi_feat = self.dgi.encoder(self.user_feat_sp_tensor).detach()

  

2. File "F:\recommend experience\doing\DGI\dgi.py",
self.encoder = Encoder(g, in_feats, n_hidden, activation)
3.
 File "F:\recommend experience\doing\DGI\dgi.py",
 
class Encoder(nn.Module):
def __init__(self, g, in_feats, n_hidden, activation):
super(Encoder, self).__init__()
self.g = g
self.conv = GCN(g, in_feats, n_hidden, activation)

def forward(self, features, corrupt=False):
features = self.conv(features)
return features
4.
class GraphConv(nn.Module):

def forward(self, graph, feat, weight=None):
graph = graph.local_var()
# 在局部作用域下复制图
degs = graph.out_degrees().to(feat.device).float().clamp(min=1)
# clamp就是把最小值设置为1
# torch.clamp(input ,min,max,out=None)
# torch.clamp()的作用把input的数据,夹逼到[min,max]之间
# mport torch as t
# a=t.arange(8).view(2,4)
# a
#
# #tensor([[[0, 1, 2, 3]],
#
#     [[4, 5, 6, 7]]])
#
# t.clamp(a,min=3)
#
# 结果为:
#
# tensor([[3, 3, 3, 3],
# [4, 5, 6, 7]])
norm = th.pow(degs, -0.5)
shp = norm.shape + (1,) * (feat.dim() - 1)
norm = th.reshape(norm, shp)

weight = self.weight

feat = th.matmul(feat, self.weight)
# th.matmul是矩阵乘法函数
feat = feat * norm
graph.srcdata['h'] = feat
graph.update_all(fn.copy_u(u='h',out='m'),
fn.sum(msg='m', out='h'))

# graph.update_all(fn.copy_src(src='h', out='m'),
# fn.sum(msg='m', out='h'))
rst = graph.dstdata['h']
rst = rst * norm

if self.bias is not None:
rst = rst + self.bias

if self._activation is not None:
rst = self._activation(rst)

return rst
 
 
posted @ 2023-05-21 22:18  化繁为简,弃快从慢  阅读(597)  评论(0编辑  收藏  举报