RuntimeError: index 2512 is out of bounds for dimension 0 with size 2512

Hi

I am getting the following error:
RuntimeError: index 2512 is out of bounds for dimension 0 with size 2512

Here is the full troubleshooting:

Here is what I have in data:

I would appreciate your help. Thank you!

The index error is raised, since an index of 2512 is invalid for a shape of 2512, which would accept indices in [0, 2511], so you would have to check how this index is created and if you could clip it to the valid range.

1 Like

Thank you for your reply. I am still getting this error and not sure how to fix it, here is my code:

if name == “main”:
#process & create the dataset files
parser = argparse.ArgumentParser()

# system
parser.add_argument("--feature", type=str, default="glove", help="glove | all")
#no use of user_type for now
parser.add_argument("--user_type", type=str, default="hate", help="hate | suspend")
parser.add_argument("--model_type", type=str, default="sage", help="sage | gat")
parser.add_argument("--epoch", type=int, default=201)
args = parser.parse_args()
assert(args.feature in ['glove', 'all'])
assert(args.user_type in ['hate', 'suspend'])
assert(args.model_type in ['sage', 'gat'])
print("====information of experiment====")
print("FEATURE: ", args.feature, "classification_type:", args.user_type, "MODEL:", args.model_type)
print("====end information of experiment====")
dataset = construct_dataset(args.feature)
model_type = args.model_type
index0, index1, index2,index3,index4,index5,index6,index7,index8,index9,index10,index11,index12,index13,index14,index15,index16,index17,index18,index19,index20,index21,index22,index23,index24,index25,index26,index27,index28 = get_labeled_index(feature_type=args.feature)
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=123)
#y_all = [2] * len(hate_index)
y_all = [0] * len(index0)

#y_normal = [0] * len(normal_index)
y_1 = [1] * len(index1)
y_2 = [2] * len(index2)
y_3 = [3] * len(index3)
y_4 = [4] * len(index4)
y_5 = [5] * len(index5)
y_6 = [6] * len(index6)
y_7 = [7] * len(index7)
y_8 = [8] * len(index8)
y_9 = [9] * len(index9)
y_10 = [10] * len(index10)
y_11 = [11] * len(index11)
y_12 = [12] * len(index12)
y_13 = [13] * len(index13)
y_14 = [14] * len(index14)
y_15 = [15] * len(index15)
y_16 = [16] * len(index16)
y_17 = [17] * len(index17)
y_18 = [18] * len(index18)
y_19 = [19] * len(index19)
y_20 = [20] * len(index20)
y_21 = [21] * len(index21)
y_22 = [22] * len(index22)
y_23 = [23] * len(index23)
y_24 = [24] * len(index24)
y_25 = [25] * len(index25)
y_26 = [26] * len(index26)
y_27 = [27] * len(index27)
y_28 = [28] * len(index28)


#y_all.extend(y_normal)
y_all.extend(y_1)
y_all.extend(y_2)
y_all.extend(y_3)
y_all.extend(y_4)
y_all.extend(y_5)
y_all.extend(y_6)
y_all.extend(y_7)
y_all.extend(y_8)
y_all.extend(y_9)
y_all.extend(y_10)
y_all.extend(y_11)
y_all.extend(y_12)
y_all.extend(y_13)
y_all.extend(y_14)
y_all.extend(y_15)
y_all.extend(y_16)
y_all.extend(y_17)
y_all.extend(y_18)
y_all.extend(y_19)
y_all.extend(y_20)
y_all.extend(y_21)
y_all.extend(y_22)
y_all.extend(y_23)
y_all.extend(y_24)
y_all.extend(y_25)
y_all.extend(y_26)
y_all.extend(y_27)
y_all.extend(y_28)


all_index = []

#all_index.extend(hate_index)
all_index.extend(index0)
all_index.extend(index1)
all_index.extend(index2)
all_index.extend(index3)
all_index.extend(index4)
all_index.extend(index5)
all_index.extend(index6)
all_index.extend(index7)
all_index.extend(index8)
all_index.extend(index9)
all_index.extend(index10)
all_index.extend(index11)
all_index.extend(index12)
all_index.extend(index13)
all_index.extend(index14)
all_index.extend(index15)
all_index.extend(index16)
all_index.extend(index17)
all_index.extend(index18)
all_index.extend(index19)
all_index.extend(index20)
all_index.extend(index21)
all_index.extend(index22)
all_index.extend(index23)
all_index.extend(index24)
all_index.extend(index25)
all_index.extend(index26)
all_index.extend(index27)
all_index.extend(index28)



recall_test = []
accuracy_test = []
fscore_test = []
precision_test = []
all_index = np.array(all_index)
trail = 0
for train_i, test_i in skf.split(all_index, y_all):
    print("========begin trail {:01d}===========".format(trail))
    all_train_index = all_index[train_i]
    test_index = all_index[test_i]
    data = dataset[0]
    data.train_mask = torch.zeros(data.num_nodes, dtype=torch.long)
    data.train_mask[all_train_index] = 1
    data.test_mask = torch.zeros(data.num_nodes, dtype=torch.long)
    data.test_mask[test_index] = 1
    loader = NeighborSampler(data, size=[25], num_hops=1, batch_size=128, shuffle=True, add_self_loops=True)

Here is what is in the data exactly:

I would appreciate your help. Thank you.

I’m unsure how these indices are created, but based on the previous stack trace it seems that your are trying to index some “neighboring samples”? In case you cannot isolate and fix the issue properly (by making sure that these invalid indices are not used or by increasing the size of the tensor, which should be indexed), you could torch.clamp the indices to a max. value of 2511.

1 Like

Thank you so much for your reply. I figured this out and keep data in the proper range.