docstr: Test the loss_coef_{key} config options.
docstr_tok: ["Test","the","loss_coef_","{","key","}","config","options","."]
code: def test_loss_coef(self):
      """Test the loss_coef_{key} config options."""
  
      config = _default_config()
      config["embed_pdrop"] = 0
      config["resid_pdrop"] = 0
      config["attn_pdrop"] = 0
      # set initial action coef to 0
      config["loss_coef_actions"] = 0
  
      observation_space = gym.spaces.Box(-1.0, 1.0, shape=(3,))
      action_spaces = [
          gym.spaces.Box(-1.0, 1.0, shape=(1,)),
          gym.spaces.Discrete(4),
      ]
  
      for action_space in action_spaces:
          batch = SampleBatch(
              {
                  SampleBatch.OBS: np.array(
                      [
                          [
                              [0.0, 0.0, 0.0],
                              [0.0, 0.0, 0.0],
                              [0.0, 1.0, 2.0],
                              [3.0, 4.0, 5.0],
                          ]
                      ],
                      dtype=np.float32,
                  ),
                  SampleBatch.ACTIONS: (
                      np.array(
                          [[[0.0], [0.0], [1.0], [0.5]]],
                          dtype=np.float32,
                      )
                      if isinstance(action_space, gym.spaces.Box)
                      else np.array([[0, 0, 1, 3]], dtype=np.int64)
                  ),
                  SampleBatch.RETURNS_TO_GO: np.array(
                      [[[0.0], [0.0], [100.0], [90.0], [80.0]]],
                      dtype=np.float32,
                  ),
                  SampleBatch.T: np.array(
                      [[0, 0, 0, 1]], dtype=np.int32
                  ),
                  SampleBatch.ATTENTION_MASKS: np.array(
                      [[0.0, 0.0, 1.0, 1.0]], dtype=np.float32
                  ),
              }
          )
  
          keys = [
              SampleBatch.ACTIONS,
              SampleBatch.OBS,
              SampleBatch.RETURNS_TO_GO,
          ]
          for key in keys:
              # create policy and run loss with different coefs
              # create policy 1 with coef = 1
              config1 = config.copy()
              config1[f"loss_coef_{key}"] = 1.0
              policy1 = DTTorchPolicy(
                  observation_space, action_space, config1
              )
  
              loss1 = policy1.loss(
                  policy1.model, policy1.dist_class, batch
              )
              loss1 = loss1.detach().cpu().item()
  
              # create policy 2 with coef = 10
              config2 = config.copy()
              config2[f"loss_coef_{key}"] = 10.0
              policy2 = DTTorchPolicy(
                  observation_space, action_space, config2
              )
              # Copy the weights over so they output the same loss without scaling.
              policy2.set_weights(policy1.get_weights())
  
              loss2 = policy2.loss(
                  policy2.model, policy2.dist_class, batch
              )
              loss2 = loss2.detach().cpu().item()
  
              # Compare loss, should be factor of 10 difference.
              self.assertAlmostEqual(
                  loss2 / loss1,
                  10.0,
                  places=3,
                  msg="the two losses should be different to a factor of 10.",
              )
  
file_path: ray/rllib_contrib/dt/tests/test_dt_policy.py
imports: import unittest
  from typing import Dict
  import gymnasium
  import numpy
  from rllib_dt.dt.dt_torch_policy import DTTorchPolicy
  import ray
  from ray.rllib.policy.sample_batch import SampleBatch
  from ray.rllib.utils.framework import try_import_tf, try_import_torch
label_desc: Use image node_id 7 for calling the TestDTPolicy obj's underlying member method code with example usage: obj.test_loss_coef() without return types