栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

RuntimeError: Could not infer dtype of numpy.int64

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RuntimeError: Could not infer dtype of numpy.int64

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)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/875064.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号