code: def test_set_node_features_cpu():
      g = GraphData()
      g.add_nodes(10)
  
      # Test adding node features at whole graph level.
      try:
          g.node_features["node_feat"] = torch.randn((9, 10))
          fail_here()
      except SizeMismatchException:
          pass
  
      g.node_features["node_feat"] = torch.randn((10, 10))
      g.node_features["zero"] = torch.zeros(10)
      g.node_features["idx"] = torch.tensor(
          list(range(10)), dtype=torch.long
      )
  
      for i in range(g.get_node_num()):
          assert g.node_features["idx"][i] == i
          assert g.node_features["node_feat"][i].shape == torch.Size(
              [10]
          )
  
      # Test modifying node features partially
      g.node_features["zero"][:5] = torch.ones(5)
      for i in range(5):
          assert g.node_features["zero"][i] == 1
  
      embedding_layer = nn.Embedding(
          num_embeddings=100, embedding_dim=10
      )
      optimizer = torch.optim.Adam(params=embedding_layer.parameters())
      target = torch.ones(10)
      init_loss = -torch.nn.functional.cosine_similarity(
          embedding_layer(g.node_features["idx"]).sum(dim=-1),
          target,
          dim=0,
      )
      for _ in range(100):
          g.node_features["node_emb"] = embedding_layer(
              g.node_features["idx"]
          )
          node_emb_sum = g.node_features["node_emb"].sum(dim=-1)
          loss = -torch.nn.functional.cosine_similarity(
              node_emb_sum, target, dim=0
          )
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()
      print(
          "Initial loss = {}. Final loss = {}".format(init_loss, loss)
      )
      assert init_loss > loss
  
imports: import gc
  import time
  import matplotlib.pyplot
  import torch
  import torch.nn
  import pytest
  from ...data.data import GraphData, from_batch, from_dgl, to_batch
  from ...data.utils import EdgeNotFoundException, SizeMismatchException
label_desc: Use image node_id 3 for calling a global function with example usage: test_set_node_features_cpu() without return types