code: def learn(
      cvae,
      train_set,
      n_epochs,
      batch_size,
      learn_rate,
      beta,
      alpha_1,
      alpha_2,
      verbose,
      device=torch.device("cpu"),
  ):
      optimizer = torch.optim.Adam(
          params=cvae.parameters(), lr=learn_rate
      )
  
      x = train_set.user_graph.matrix[
          : train_set.num_users, : train_set.num_users
      ]
      y = train_set.matrix
      progress_bar = trange(1, n_epochs + 1, disable=not verbose)
      for _ in progress_bar:
          sum_loss = 0.0
          count = 0
          for batch_id, u_ids in enumerate(
              train_set.user_iter(batch_size, shuffle=False)
          ):
              y_batch = y[u_ids, :]
              y_batch.data = np.ones(len(y_batch.data))  # Binarize data
              y_batch = y_batch.A
              y_batch = torch.tensor(
                  y_batch, dtype=torch.float32, device=device
              )
  
              x_batch = x[u_ids, :]
              x_batch = x_batch.A
              x_batch = torch.tensor(
                  x_batch, dtype=torch.float32, device=device
              )
  
              # Reconstructed batch
              (
                  y_batch_,
                  mu_qz,
                  logvar_qz,
                  mu_qhx,
                  logvar_qhx,
                  mu_qhy,
                  logvar_qhy,
                  mu_ph,
                  logvar_ph,
              ) = cvae(y_batch, x_batch)
  
              loss = cvae.loss(
                  y_batch,
                  y_batch_,
                  mu_qz,
                  logvar_qz,
                  mu_qhx,
                  logvar_qhx,
                  mu_qhy,
                  logvar_qhy,
                  mu_ph,
                  logvar_ph,
                  alpha_1,
                  alpha_2,
                  beta,
              )
              optimizer.zero_grad()
              loss.backward()
              optimizer.step()
  
              sum_loss += loss.data.item()
              count += y_batch.shape[0]
  
              if batch_id % 10 == 0:
                  progress_bar.set_postfix(loss=(sum_loss / count))
  
      return cvae
  
file_path: cornac/cornac/models/cvaecf/cvaecf.py
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from tqdm.auto import trange
  from ...utils import estimate_batches
label_desc: Use image node_id 1 for calling a global function with example usage: learn(cvae, train_set, n_epochs, batch_size, learn_rate, beta, alpha_1, alpha_2, verbose, device) and returns: cvae