code: def train():
      total_loss = total_examples = 0
  
      for index in tqdm(train_loader):
          # Sample positive and negative labels.
          pos_edge_label_index = train_edge_label_index[:, index]
          neg_edge_label_index = torch.stack(
              [
                  pos_edge_label_index[0],
                  torch.randint(
                      num_users,
                      num_users + num_books,
                      (index.numel(),),
                      device=device,
                  ),
              ],
              dim=0,
          )
          edge_label_index = torch.cat(
              [
                  pos_edge_label_index,
                  neg_edge_label_index,
              ],
              dim=1,
          )
  
          optimizer.zero_grad()
          pos_rank, neg_rank = model(
              data.edge_index, edge_label_index
          ).chunk(2)
  
          loss = model.recommendation_loss(
              pos_rank,
              neg_rank,
              node_id=edge_label_index.unique(),
          )
          loss.backward()
          optimizer.step()
  
          total_loss += float(loss) * pos_rank.numel()
          total_examples += pos_rank.numel()
  
      return total_loss / total_examples
  
imports: import os.path
  import torch
  from tqdm import tqdm
  from torch_geometric.datasets import AmazonBook
  from torch_geometric.nn import LightGCN
  from torch_geometric.utils import degree
label_desc: Use image node_id 1 for calling a global function with example usage: train() and returns: unknown