code: def learn(
      vae,
      train_set,
      n_epochs,
      batch_size,
      learn_rate,
      beta,
      verbose,
      device=torch.device("cpu"),
  ):
      optimizer = torch.optim.Adam(
          params=vae.parameters(), lr=learn_rate
      )
      num_steps = estimate_batches(train_set.num_users, batch_size)
  
      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)
          ):
              u_batch = train_set.matrix[u_ids, :]
              u_batch.data = np.ones(len(u_batch.data))  # Binarize data
              u_batch = u_batch.A
              u_batch = torch.tensor(
                  u_batch, dtype=torch.float32, device=device
              )
  
              # Reconstructed batch
              u_batch_, mu, logvar = vae(u_batch)
  
              loss = vae.loss(u_batch, u_batch_, mu, logvar, beta)
              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))
  
      return vae
  
file_path: cornac/cornac/models/vaecf/vaecf.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(vae, train_set, n_epochs, batch_size, learn_rate, beta, verbose, device) and returns: vae