Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question RuntimeError: Class values must be smaller than num_classes.

Discussion in 'ML-Agents' started by krishna268, Jun 14, 2024.

  1. krishna268

    krishna268

    Joined:
    Mar 1, 2022
    Posts:
    6
    Hi, I am trying to build a MotoGP racing game and using imitation learning to achieve my objective. I followed this video of CodeMonkey . I first created the .demo file by training it by myself. But when I try to train it using imitiation learning by running the following command:

    Code (CSharp):
    1. mlagents-learn config/MotoGP.yaml --run-id=Imitation9
    and then press the play button , the game kind of runs for a second or two and then stops and I get the above error. This is the error in full detail:

    Code (CSharp):
    1. Connected new brain: BikerBehavior?team=0
    2. [INFO] Hyperparameters for behavior name BikerBehavior:
    3.         trainer_type:   ppo
    4.         hyperparameters:
    5.           batch_size:   10
    6.           buffer_size:  100
    7.           learning_rate:        0.0003
    8.           beta: 0.0005
    9.           epsilon:      0.2
    10.           lambd:        0.99
    11.           num_epoch:    3
    12.           shared_critic:        False
    13.           learning_rate_schedule:       linear
    14.           beta_schedule:        constant
    15.           epsilon_schedule:     linear
    16.         network_settings:
    17.           normalize:    False
    18.           hidden_units: 128
    19.           num_layers:   2
    20.           vis_encode_type:      simple
    21.           memory:       None
    22.           goal_conditioning_type:       hyper
    23.           deterministic:        False
    24.         reward_signals:
    25.           extrinsic:
    26.             gamma:      0.99
    27.             strength:   1.0
    28.             network_settings:
    29.               normalize:        False
    30.               hidden_units:     128
    31.               num_layers:       2
    32.               vis_encode_type:  simple
    33.               memory:   None
    34.               goal_conditioning_type:   hyper
    35.               deterministic:    False
    36.           gail:
    37.             gamma:      0.99
    38.             strength:   0.5
    39.             network_settings:
    40.               normalize:        False
    41.               hidden_units:     128
    42.               num_layers:       2
    43.               vis_encode_type:  simple
    44.               memory:   None
    45.               goal_conditioning_type:   hyper
    46.               deterministic:    False
    47.             learning_rate:      0.0003
    48.             encoding_size:      None
    49.             use_actions:        False
    50.             use_vail:   False
    51.             demo_path:  Trainer/MotoGPTrainer.demo
    52.         init_path:      None
    53.         keep_checkpoints:       5
    54.         checkpoint_interval:    500000
    55.         max_steps:      500000
    56.         time_horizon:   64
    57.         summary_freq:   10000
    58.         threaded:       False
    59.         self_play:      None
    60.         behavioral_cloning:
    61.           demo_path:    Trainer/MotoGPTrainer.demo
    62.           steps:        0
    63.           strength:     0.5
    64.           samples_per_update:   0
    65.           num_epoch:    None
    66.           batch_size:   None
    67. D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\torch_entities\utils.py:289: UserWarning: The use of `x.T` on tensors of dimension other than 2 to reverse their shape is deprecated and it will throw an error in a future release. Consider `x.mT` to transpose batches of matrices or `x.permute(*torch.arange(x.ndim - 1, -1, -1))` to reverse the dimensions of a tensor. (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\aten\src\ATen\native\TensorShape.cpp:3679.)
    68.   torch.nn.functional.one_hot(_act.T, action_size[i]).float()
    69. [INFO] Exported results\Imitation9\BikerBehavior\BikerBehavior-128.onnx
    70. [INFO] Copied results\Imitation9\BikerBehavior\BikerBehavior-128.onnx to results\Imitation9\BikerBehavior.onnx.
    71. Traceback (most recent call last):
    72.   File "C:\Users\G524\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    73.     return _run_code(code, main_globals, None,
    74.   File "C:\Users\G524\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    75.     exec(code, run_globals)
    76.   File "D:\MotoGP Demo\venv\Scripts\mlagents-learn.exe\__main__.py", line 7, in <module>
    77.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\learn.py", line 264, in main
    78.     run_cli(parse_command_line())
    79.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\learn.py", line 260, in run_cli
    80.     run_training(run_seed, options, num_areas)
    81.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\learn.py", line 136, in run_training
    82.     tc.start_learning(env_manager)
    83.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents_envs\timers.py", line 305, in wrapped
    84.     return func(*args, **kwargs)
    85.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\trainer_controller.py", line 175, in start_learning
    86.     n_steps = self.advance(env_manager)
    87.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents_envs\timers.py", line 305, in wrapped
    88.     return func(*args, **kwargs)
    89.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\trainer_controller.py", line 250, in advance
    90.     trainer.advance()
    91.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\trainer\rl_trainer.py", line 302, in advance
    92.     if self._update_policy():
    93.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\trainer\on_policy_trainer.py", line 111, in _update_policy
    94.     update_stats = self.optimizer.bc_module.update()
    95.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\torch_entities\components\bc\module.py", line 95, in update
    96.     run_out = self._update_batch(mini_batch_demo, self.n_sequences)
    97.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\torch_entities\components\bc\module.py", line 178, in _update_batch
    98.     bc_loss = self._behavioral_cloning_loss(
    99.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\torch_entities\components\bc\module.py", line 118, in _behavioral_cloning_loss
    100.     one_hot_expert_actions = ModelUtils.actions_to_onehot(
    101.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\torch_entities\utils.py", line 288, in actions_to_onehot
    102.     onehot_branches = [
    103.   File "D:\MotoGP Demo\venv\lib\site-packages\mlagents\trainers\torch_entities\utils.py", line 289, in <listcomp>
    104.     torch.nn.functional.one_hot(_act.T, action_size[i]).float()
    105. RuntimeError: Class values must be smaller than num_classes.
    You can look into the .yaml file by observing the above code. I tried to read a similar problems posted by other and from there I tried experimenting by removing the behavioral_cloning: part away from the .yaml file. There is no error in the command prompt console , the game continues to stay in the play mode but the bike doesn't move an inch. Can anyone here help me how to solve this?

    Also the ML agents package has not seen any update since 2021. Why is that so?
     
  2. krishna268

    krishna268

    Joined:
    Mar 1, 2022
    Posts:
    6
    Hi can anyone answer this question to the best of their knowledge , it been 10 days and I am waiting . May be @
    jeffrey_unity538 can provide any insights