code: def learn(
      model,
      train_set,
      n_epochs,
      batch_size=256,
      learning_rate=0.01,
      reg=0.0,
      verbose=True,
      optimizer="sgd",
      device=torch.device("cpu"),
  ):
      model = model.to(device)
      criteria = nn.MSELoss(reduction="sum")
      optimizer = OPTIMIZER_DICT[optimizer](
          params=model.parameters(), lr=learning_rate, weight_decay=reg
      )
  
      progress_bar = trange(1, n_epochs + 1, disable=not verbose)
      for _ in progress_bar:
          sum_loss = 0.0
          count = 0
          for batch_id, (u_batch, i_batch, r_batch) in enumerate(
              train_set.uir_iter(batch_size, shuffle=True)
          ):
              u_batch = torch.from_numpy(u_batch).to(device)
              i_batch = torch.from_numpy(i_batch).to(device)
              r_batch = torch.tensor(r_batch, dtype=torch.float).to(
                  device
              )
  
              preds = model(u_batch, i_batch)
              loss = criteria(preds, r_batch)
  
              optimizer.zero_grad()
              loss.backward()
              optimizer.step()
  
              sum_loss += loss.data.item()
              count += len(u_batch)
  
              if batch_id % 10 == 0:
                  progress_bar.set_postfix(loss=(sum_loss / count))
  
file_path: cornac/cornac/models/mf/backend_pt.py
imports: import torch
  import torch.nn
  from tqdm.auto import trange
label_desc: Use image node_id 1 for calling a global function with example usage: learn(model, train_set, n_epochs, batch_size, learning_rate, reg, verbose, optimizer, device) without return types