Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise 'IndexError: index out of range in self' when I use HGTConv with my own dataset. #39

Open
rickyqiao opened this issue May 3, 2022 · 1 comment

Comments

@rickyqiao
Copy link

In my work, I build the Hetergouenous graph firstly, then I apply the HGTConv as the tutorial shown.

The custom KG is shown as:
HeteroData(
symptom={ x=[39, 128] },
component={ x=[19, 128] },
reason={ x=[17, 128] },
solution={ x=[18, 128] },
(symptom, take_place, component)={ edge_index=[2, 38] },
(symptom, cause_by, reason)={ edge_index=[2, 33] },
(symptom, how_to_fit, solution)={ edge_index=[2, 33] },
(component, component_parallel, component)={ edge_index=[2, 17] }
)

And my code as follow:


class HGT(torch.nn.Module):
def init(self, hidden_channels, out_channels, num_heads, num_layers):
super().init()

    self.lin_dict = torch.nn.ModuleDict()
    for node_type in data_IKG.node_types:
        self.lin_dict[node_type] = Linear(-1, hidden_channels)

    self.convs = torch.nn.ModuleList()
    for _ in range(num_layers):
        conv = HGTConv(hidden_channels, hidden_channels, data_IKG.metadata(),
                       num_heads, group='sum',cached=False)
    self.convs.append(conv)

    self.lin = Linear(hidden_channels, out_channels)

def forward(self, x_dict, edge_index_dict):
    for node_type, x in x_dict.items():
        x_dict[node_type] = self.lin_dict[node_type](x).relu_()

    for conv in self.convs:
        print(1)
        x_dict = conv(x_dict, edge_index_dict)
        print(2)
    return self.lin(x_dict['symptom'])

model_IKG = HGT(hidden_channels=64, out_channels=5,
num_heads=1, num_layers=1)

with torch.no_grad(): # Initialize lazy modules.
out = model_IKG(data_IKG.x_dict, data_IKG.edge_index_dict)

Then it raises:
Input In [324], in HGT.forward(self, x_dict, edge_index_dict)
29 for conv in self.convs:
30 print(1)
---> 31 x_dict = conv(x_dict, edge_index_dict)
32 print(2)
33 return self.lin(x_dict['symptom'])


Then the error message is:


File ~/.virtualenvs/xlq/lib/python3.8/site-packages/torch/nn/modules/module.py:1102, in Module._call_impl(self, *input, **kwargs)
1098 # If we don't have any hooks, we want to skip the rest of the logic in
1099 # this function, and just call forward.
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []

File ~/.virtualenvs/xlq/lib/python3.8/site-packages/torch_geometric/nn/conv/hgt_conv.py:159, in HGTConv.forward(self, x_dict, edge_index_dict)
156 v = (v_dict[src_type].transpose(0, 1) @ m_rel).transpose(1, 0)
158 # propagate_type: (k: Tensor, q: Tensor, v: Tensor, rel: Tensor)
--> 159 out = self.propagate(edge_index, k=k, q=q_dict[dst_type], v=v,
160 rel=self.p_rel[edge_type], size=None)
161 out_dict[dst_type].append(out)
163 # Iterate over node-types:

File ~/.virtualenvs/xlq/lib/python3.8/site-packages/torch_geometric/nn/conv/message_passing.py:309, in MessagePassing.propagate(self, edge_index, size, **kwargs)
306 for arg in decomp_args:
307 kwargs[arg] = decomp_kwargs[arg][i]
--> 309 coll_dict = self.collect(self.user_args, edge_index,
310 size, kwargs)
312 msg_kwargs = self.inspector.distribute('message', coll_dict)
313 for hook in self._message_forward_pre_hooks.values():

File ~/.virtualenvs/xlq/lib/python3.8/site-packages/torch_geometric/nn/conv/message_passing.py:202, in MessagePassing.collect(self, args, edge_index, size, kwargs)
200 if isinstance(data, Tensor):
201 self.set_size(size, dim, data)
--> 202 data = self.lift(data, edge_index, dim)
204 out[arg] = data
206 if isinstance(edge_index, Tensor):

File ~/.virtualenvs/xlq/lib/python3.8/site-packages/torch_geometric/nn/conv/message_passing.py:172, in MessagePassing.lift(self, src, edge_index, dim)
170 if isinstance(edge_index, Tensor):
171 index = edge_index[dim]
--> 172 return src.index_select(self.node_dim, index)
173 elif isinstance(edge_index, SparseTensor):
174 if dim == 1:

IndexError: index out of range in self


Please help me to fix the problem, much appreciate.

@rickyqiao rickyqiao changed the title Raise 'TypeError: __init__() got an unexpected keyword argument 'add_self_loops' when I use HGTConv with my own dataset. Raise 'IndexError: index out of range in self' when I use HGTConv with my own dataset. May 3, 2022
@cold-rivers
Copy link

Did you solve the problem later? If you use the pyg data format, how should the
fine-turn code be modified?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants