RuntimeError: Expected target size [12, 12], got [12]

Hi, I’m a graduate student trying to do GNN semi-supervised learning using pytorch.

The structure of all my data is 100x100 in size,
supervised learning data is 12.

Below is the corresponding code.
I think the size is correct, but I want to know why I get the above error.

If you need more information, please leave a comment and I’ll get back to you.

I’m not an expert, so my question may be lacking in many areas. I’ll try to provide the best information I can.

Thank you

import os
import pickle
import pandas as pd
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
import torch.optim as optim
import torch

1. Custom Dataset

class CustomGraphDataset(Dataset):
def init(self, drug_pkl_folder_path, disease_pkl_folder_path, csv_path=None):
self.drug_file_paths = [os.path.join(drug_pkl_folder_path, file) for file in os.listdir(drug_pkl_folder_path) if file.endswith(‘.pkl’)]
self.disease_file_paths = [os.path.join(disease_pkl_folder_path, file) for file in os.listdir(disease_pkl_folder_path) if file.endswith(‘.pkl’)]
self.labels = {} # drug to index mapping
self.label_to_index = {} # disease to index mapping
self.index_to_label = {} # index to disease mapping

    if csv_path:
        df = pd.read_csv(csv_path)
        unique_labels = df['disease'].unique().tolist()
        for idx, unique_label in enumerate(unique_labels):
            self.label_to_index[unique_label] = idx
            self.index_to_label[idx] = unique_label

        for idx, row in df.iterrows():
            drug = row['drug']
            disease = row['disease']
            self.labels[drug] = self.label_to_index[disease]

def __len__(self):
    return len(self.drug_file_paths)

def __getitem__(self, idx):
    with open(self.drug_file_paths[idx], 'rb') as f:
        drug_data = pickle.load(f)
    with open(self.disease_file_paths[idx], 'rb') as f:
        disease_data = pickle.load(f)

    drug_name = os.path.basename(self.drug_file_paths[idx]).replace('.pkl', '')

    if drug_name in self.labels:
        label = self.labels[drug_name]
        return drug_data, disease_data, torch.tensor(label, dtype=torch.long)
    else:
        return drug_data, disease_data, None

2. DataLoader

supervised_dataset = CustomGraphDataset(‘model data set/supervised drug’, ‘model data set/supervised disease’, ‘C:/Users/tae97/Desktop/linkinfo/model data set/supervised.csv’)
unsupervised_drug_dataset = CustomGraphDataset(‘model data set/unsupervised drug’, ‘model data set/unsupervised disease’)
unsupervised_disease_dataset = CustomGraphDataset(‘model data set/unsupervised drug’, ‘model data set/unsupervised disease’)

supervised_loader = DataLoader(supervised_dataset, batch_size=32, shuffle=True)
unsupervised_drug_loader = DataLoader(unsupervised_drug_dataset, batch_size=32, shuffle=True)
unsupervised_disease_loader = DataLoader(unsupervised_disease_dataset, batch_size=32, shuffle=True)

3. Model

num_classes = len(supervised_dataset.label_to_index)

class SimpleGNN(nn.Module):
def init(self, num_classes):
super(SimpleGNN, self).init()
self.fc = nn.Linear(100, num_classes)

def forward(self, x):
    return self.fc(x)

4. Training

model = SimpleGNN(num_classes)
print(model)

optimizer = optim.Adam(model.parameters(), lr=0.001)
loss_function = nn.CrossEntropyLoss()

epochs = 10
for epoch in range(epochs):
for drug_data, disease_data, label in supervised_loader:
if label is None:
continue
drug_data = drug_data.float()
label = label.long()
optimizer.zero_grad()
outputs = model(drug_data)
loss = loss_function(outputs, label)
loss.backward()
optimizer.step()