RuntimeError: Could not infer dtype of numpy.int64
问题:再写强化学习代码时,遇到该错误,代码如下,对于传的参数max_action=2.0 ,使用 self.action_scale = torch.tensor(max_action) 会报如上错误,主要是max_action变为了numpy.int64 ,所以不能用torch.tensor()
class Actor(nn.Module):
# def __init__(self, action_dim, state_dim, log_std_min=-20, log_std_max=2, max_action=None): # 这样会把min, max变为numpy.float
def __init__(self, action_dim, state_dim, env, max_action=None):
super(Actor, self).__init__()
# self.min_log_std = log_std_min
# self.max_log_std = log_std_max
self.f1 = nn.Linear(state_dim, 200)
self.f2 = nn.Linear(200, 200)
self.mean = nn.Linear(200, action_dim)
self.log_std = nn.Linear(200, action_dim)
self.mean.weight.data.uniform_(-INIT_W, INIT_W)
self.mean.bias.data.uniform_(-INIT_W, INIT_W)
self.log_std.weight.data.uniform_(-INIT_W, INIT_W)
self.log_std.bias.data.uniform_(-INIT_W, INIT_W)
# action scaling
if max_action == None:
self.action_scale = torch.tensor(1.)
self.action_bias = torch.tensor(0.)
else:
self.action_scale = torch.tensor(max_action)
self.action_bias = torch.tensor(0.)
解决:使用torch.tensor() 的时候加参数dtype=torch.float 就可以。
self.action_scale = torch.tensor(float(max_action), dtype=torch.float)
self.action_bias = torch.tensor(0., dtype=torch.float)
|