<jupyter_start><jupyter_text>Comparison with traditional ML<jupyter_code>from sklearn.ensemble import RandomForestClassifier
import torch.nn as nn
model = RandomForestClassifier()
model.fit(data.x[data.train_mask].detach().cpu().numpy() 
          , data.y[data.train_mask].detach().cpu().numpy())
y_pred = model.predict(data.x[data.test_mask].detach().cpu().numpy() )
my_classification_report(y_actual = data.y[data.test_mask].detach().cpu().numpy()
                         , y_pred = y_pred)<jupyter_output>precision    recall  f1-score   support

           0       0.54      0.42      0.47       130
           1       0.55      0.75      0.64        91
           2       0.49      0.81      0.61       144
           3       0.81      0.51      0.63       319
           4       0.48      0.58      0.53       149
           5       0.60      0.49      0.53       103
           6       0.49      0.56      0.52        64

    accuracy                           0.57      1000
   macro avg       0.56      0.59      0.56      1000
weighted avg       0.61      0.57      0.57      1000

accuracy : 0.573
[[ 54  14  23   5  18   5  11]
 [  5  68   4   8   2   2   2]
 [  4   6 116   8   4   1   5]
 [ 17   9  48 163  57  16   9]
 [  8   7  25  14  86   7   2]
 [  6  12  16   4   6  50   9]
 [  6   7   7   0   5   3  36]]<jupyter_text>Comparison with traditional simple NN<jupyter_code>class SimpleNN(nn.Module):
    def __init__(self, input_size , hidden_size , output_size):
        super(SimpleNN, self).__init__()
        
        # Inputs to hidden layer linear transformation
        self.fc1 = nn.Linear(in_features = input_size , out_features = hidden_size)
        self.fc2 = nn.Linear(in_features = hidden_size , out_features = hidden_size)
        self.dropout = nn.Dropout(p = 0.5)
        
        # Output layer
        self.out = nn.Linear(in_features = hidden_size , out_features = output_size)
        
    def forward(self,x):
        
        #Pass output of BERT through Fully connected layer
        x = self.fc1(x)
        x = torch.relu(x)
        
        #x = self.dropout(x)
        
        x = self.fc2(x)
        x = torch.relu(x)
        
        #Output layer
        x = self.out(x)
        x = F.softmax(x , dim = 1)
        
        return x

model = SimpleNN(input_size = dataset.num_features , hidden_size = 64, output_size = dataset.num_classes)
print(model)
# Use GPU
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
model = model.to(device)
data = data.to(device)

# Initialize Optimizer
learning_rate = 0.01
optimizer = torch.optim.Adam(model.parameters(), 
                             lr=learning_rate)

# Define loss function (CrossEntropyLoss for Classification Problems with 
# probability distributions)
criterion = torch.nn.CrossEntropyLoss()

def train():
    model.train()
    optimizer.zero_grad() 
    # Use all data as input, because all nodes have node features
    out = model(data.x.unsqueeze(0))  
    # Only use nodes with labels available for loss calculation --> mask
    loss = criterion(out[: ,data.train_mask].squeeze(0) , data.y[data.train_mask])  
    loss.backward() 
    optimizer.step()
    return loss

def test():
    model.eval()
    out = model(data.x.unsqueeze(0)) 
    # Use the class with highest probability.
    pred = out.argmax(dim=2)  
    # Check against ground-truth labels.
    test_correct = pred[: , data.test_mask] == data.y[data.test_mask]  
    # Derive ratio of correct predictions.
    test_acc = int(test_correct.sum()) / int(data.test_mask.sum())  
    return pred , test_acc
losses = []
start_time = time.time()
for epoch in range(0, 1001):
    loss = train()
    losses.append(loss)
    if epoch % 100 == 0:
        print('Epoch: {}, Loss: {} , Time : {} secs'.format(epoch , loss , round(time.time() - start_time , 2)))
        start_time = time.time()
pred , test_acc = test()
my_classification_report(y_actual = data.y[data.test_mask].detach().cpu().numpy()
                         , y_pred = pred[:,data.test_mask].squeeze(0).detach().cpu().numpy())<jupyter_output>precision    recall  f1-score   support

           0       0.45      0.10      0.16       130
           1       0.24      0.55      0.33        91
           2       0.18      0.58      0.27       144
           3       0.47      0.12      0.19       319
           4       0.43      0.06      0.11       149
           5       0.17      0.18      0.18       103
           6       0.21      0.28      0.24        64

    accuracy                           0.23      1000
   macro avg       0.31      0.27      0.21      1000
weighted avg       0.35      0.23      0.20      1000

accuracy : 0.23
[[ 13  31  59   8   3   7   9]
 [  3  50  22   6   0   9   1]
 [  3  18  84   6   4  12  17]
 [  5  59 144  37   3  46  25]
 [  3  23  79  13   9  10  12]
 [  2  16  55   8   1  19   2]
 [  0  14  25   0   1   6  18]]
StarcoderdataJupyterStructuredCleanDedup