torch.Tensor.uniform_ 和torch.numel

torch.Tensor.uniform_

在 PyTorch 中,函数或方法名末尾带有下划线(_)通常表示这是一个 in-place 操作,也就是说,这个操作会直接修改原始张量Tensor的内容,而不是创建一个新的张量。

torch.Tensor.uniform_ 是一个例子。这个方法会将Tensor的值替换为均匀分布的随机数。带有下划线的 in-place 操作方法通常比非 in-place 操作更节省内存,但需要小心使用,因为它们会改变原始数据。(注意⚠️没有torch.Tensor.uniform这个方法)

import torch

phi = torch.empty(5, 3).uniform_(0,1)
phi

#输出
tensor([[0.1814, 0.9549, 0.3581],
        [0.7673, 0.7157, 0.2609],
        [0.6042, 0.4815, 0.6890],
        [0.5536, 0.0687, 0.4459],
        [0.4212, 0.9510, 0.6925]])

torch.numel

Tensor.numel()用法相同。
作用都是:返回tensor中所有的参数

>>> a = torch.randn(1, 2, 3, 4, 5)
>>> torch.numel(a)
120

def check_trainable_params(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值