Target-Input mismatch error

Hello (Mufei, I’m sure you’ll respond to this first :smiley: ),

I keep getting “Target size (torch.Size([20804])) must be the same as input size (torch.Size([15321])” in one of my batches. There is no graph with 15,321 nodes or 20804 labels. Any idea where I should be looking for: Here is my load_graphs code,

" if self.mode == ‘train’:
graph_files = ‘dglgraph/training’
self.train_list = [f for f in listdir(graph_files)]
onlyfiles = self.train_list
self.train_graphs = []
self.train_labels = []
self.graph_id = []
self.train_features = []
for i in range(len(onlyfiles)):
fn = str(onlyfiles[i])
fn = ‘dglgraph/training/’+fn
g, l = load_graphs(fn)
graph = g[0]
num_nodes = graph.number_of_nodes()
for j in range(num_nodes):
label = l
self.counter += 1
print('labels: ', len(self.train_labels))

And here is the collate function:

"def collate(sample):
graphs, labels_d =map(list, zip(*sample))
labels = []
for g in labels_d:
for k, v in g.items():
feats =[]
for g in graphs:
feat = g.ndata[‘node_attributes’]
graph = dgl.batch(graphs)
feats = torch.from_numpy(np.concatenate(feats))
labels = torch.from_numpy(np.concatenate(labels))
return graph, feats, labels

Thanks so much.

I think it depends on how you saved the graphs. In particular, are there multiple labels associated with some graphs? If you take a look at your collate function, the problem might arise from

labels = []
for g in labels_d:
    for k, v in g.items():

and there might be multiple pairs of (k, v) in g.items().

1 Like

Spot on!
A few graphs had doubled the number of edges due to a glitch.
I redid them and it was all cake walk!