본문 바로가기

DL & ML/Graph

[DGL] RelGraphConv에서 IndexError: index out of range in self 문제!

728x90
반응형

RelGraphConv layer로 계산을 하는 중 다음과 같은 에러를 만났다.

 

IndexError: index out of range in self

 

도저히 왜 나는지 이해가 안가서 dgl 라이브러리의 코드를 직접 뜯어서 해당하는 위치에 코드를 확인해보았다.

 

에러가 나는 부분은 다음 위치였다.

 

File ~/anaconda3/envs/dgl/lib/python3.8/site-packages/dgl/nn/pytorch/conv/relgraphconv.py:245, in RelGraphConv.basis_message_func(self, edges, etypes)
--> 245     weight = weight.index_select(0, etypes)
    246     msg = th.bmm(h.unsqueeze(1), weight).squeeze(1)
    248 if 'norm' in edges.data:

 

dgl 문서에서 아주 간단한 relgraphconv layer를 돌리는 예제에서의 weight과 etypes가 어떻게 나타나는지를 비교해보고 문제점을 알 수 있었다!

 

 

NN Modules (PyTorch) — DGL 0.6.1 documentation

node_feats (torch.Tensor or pair of torch.Tensor) – The input node features. If a torch.Tensor is given, it represents the input node feature of shape \((N, D_{in})\) where \(D_{in}\) is size of input feature, \(N\) is the number of nodes. If a pair of t

docs.dgl.ai

 

바로! relgraphconv에서는 각 노드 별 연결된 엣지의 관계를 각각 다른 weight을 사용하게 된다.

만약 edge의 관계가 총 5가지가 있다면 5개의 서로 다른 weight을 사용한다는 것!

이러한 weight은 torch.tensor 타입으로 저장이 된다.

따라서 첫 번재 weight에 접근하기 위해서는 인덱스 0, 두 번째 weight에 접근하기 위해서는 인덱스 1로 접근을 하게 된다.

 

따라서 엣지 관계 데이터를 항상 0부터 시작하도록 설정해주어야 인덱스를 접근하는 데에서 오류 없이 접근가능하다!

 

나같은 경우 Movielens 데이터셋에 이를 적용하였는데, 평점이 1 ~ 5점 그대로 edata로 넣어주었더니 인덱스 에러가 나는 것이었다.

따라서 1 ~ 5 rating을 0 ~ 4로 맵핑해준 후 진행하였더니 에러 없이 해결되었다!

728x90
반응형