opendilab / DI-engine

OpenDILab Decision AI Engine. The Most Comprehensive Reinforcement Learning Framework B.P.
https://di-engine-docs.readthedocs.io
Apache License 2.0
3k stars 366 forks source link

RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x138 and 64x138) #814

Closed WangJuan6 closed 3 months ago

WangJuan6 commented 3 months ago

Dear Author, I want to handle the complex observation, and I ran the code by python ding/example/ppo_with_complex_obs.py. However, I got an error:

Traceback (most recent call last):
  File "/data/projects/20240701/DI-engine/ding/example/ppo_with_complex_obs.py", line 205, in <module>
    main()
  File "/data/projects/20240701/DI-engine/ding/example/ppo_with_complex_obs.py", line 201, in main
    task.run()
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 207, in run
    self.forward(fn)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 51, in runtime_handler
    return func(task, *args, **kwargs)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 275, in forward
    g = fn(ctx)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 239, in forward
    g = self.forward(fn, ctx, async_mode=False)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 51, in runtime_handler
    return func(task, *args, **kwargs)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 275, in forward
    g = fn(ctx)
  File "/data/projects/20240701/DI-engine/ding/framework/middleware/collector.py", line 63, in __call__
    current_inferencer(ctx)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 239, in forward
    g = self.forward(fn, ctx, async_mode=False)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 51, in runtime_handler
    return func(task, *args, **kwargs)
  File "/data/projects/20240701/DI-engine/ding/framework/task.py", line 275, in forward
    g = fn(ctx)
  File "/data/projects/20240701/DI-engine/ding/framework/middleware/functional/collector.py", line 82, in _inference
    inference_output = policy.forward(obs, **ctx.collect_kwargs)
  File "/data/projects/20240701/DI-engine/ding/policy/ppo.py", line 428, in _forward_collect
    output = self._collect_model.forward(data, mode='compute_actor_critic')
  File "/data/projects/20240701/DI-engine/ding/model/wrapper/model_wrappers.py", line 537, in forward
    output = self._model.forward(*args, **kwargs)
  File "/data/projects/20240701/DI-engine/ding/model/wrapper/model_wrappers.py", line 84, in forward
    return self._model.forward(*args, **kwargs)
  File "/data/projects/20240701/DI-engine/ding/model/template/vac.py", line 242, in forward
    return getattr(self, mode)(x)
  File "/data/projects/20240701/DI-engine/ding/model/template/vac.py", line 347, in compute_actor_critic
    value = self.critic_head(critic_embedding)['pred']
  File "/home/wj/anaconda3/envs/di/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "/data/projects/20240701/DI-engine/ding/model/common/head.py", line 1087, in forward
    x = self.main(x)
  File "/home/wj/anaconda3/envs/di/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/wj/anaconda3/envs/di/lib/python3.9/site-packages/torch/nn/modules/container.py", line 204, in forward
    input = module(input)
  File "/home/wj/anaconda3/envs/di/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/wj/anaconda3/envs/di/lib/python3.9/site-packages/torch/nn/modules/linear.py", line 114, in forward
    return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x138 and 64x138)

Can you help me to solve this problem? Thanks

WangJuan6 commented 3 months ago

Hi, I read the code and I think the reason is that the value of the encoderhidden​​size_list

https://github.com/opendilab/DI-engine/blob/7f951592a3d7b39c8fda44081fc40002f0ee27fc/ding/model/template/vac.py#L56

is not adapted to the values ​​of critic_headhidden​​size and actor_headhidden​​size:

https://github.com/opendilab/DI-engine/blob/7f951592a3d7b39c8fda44081fc40002f0ee27fc/ding/model/template/vac.py#L33

I am not sure my understanding is correct, but I hope it can be of help.

PaParaZz1 commented 3 months ago

You should keep the last element in encoder_hidden_size_list as the same as the number of critic_head_hidden_size. In the default arguments of VAC, they are both 64.

BTW, you can directly print this nn.Module to examine the concrete network layer settings.

WangJuan6 commented 3 months ago

I modified the parameters:

https://github.com/opendilab/DI-engine/blob/7f951592a3d7b39c8fda44081fc40002f0ee27fc/ding/model/template/vac.py#L131

into:

critic_head_hidden_size

It works, thanks for your reply!