torch.clamp、torch.div用法

简介: torch.clamp(input,min,max)

torch.clamp(input,min,max)

  • 对输入的 input 张量中每个值做截断操作
  • input(Tensor),输入张量;
  • min(Number) ,截断范围最小值;
  • max(Number),截断范围最大值;

例子如下

>>> a = torch.randn(4)
>>> a
tensor([-1.7120,  0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000,  0.1734, -0.0478, -0.0922])

torch.div(input,other)

  • input 张量中值除以other 张量中对应的元素值,other 可为张量也可为 标量

参数解析

  • input(Tensor) ,输入张量,作为被除数;
  • other(Tensor or Number) ,作除数的张量或标量;


代码示例

>>> a = torch.randn(5)
>>> a
tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
>>> torch.div(a, 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7439,  0.9275])
>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
        [ 0.1815, -1.0111,  0.9805, -1.5923],
        [ 0.1062,  1.4581,  0.7759, -1.2344],
        [-0.1830, -0.0313,  1.1908, -1.4757]])
>>> b = torch.randn(4)
>>> b
tensor([ 0.8032,  0.2930, -0.8113, -0.2308])
>>> torch.div(a, b)
tensor([[-0.4620, -6.6051,  0.5676,  1.2637],
        [ 0.2260, -3.4507, -1.2086,  6.8988],
        [ 0.1322,  4.9764, -0.9564,  5.3480],
        [-0.2278, -0.1068, -1.4678,  6.3936]])
相关文章
|
6天前
|
机器学习/深度学习 PyTorch 算法框架/工具
torch.nn.Linear的使用方法
torch.nn.Linear的使用方法
68 0
|
PyTorch 算法框架/工具
pytorch中torch.clamp()使用方法
pytorch中torch.clamp()使用方法
329 0
pytorch中torch.clamp()使用方法
|
6天前
|
存储 PyTorch 算法框架/工具
torch.Storage()是什么?和torch.Tensor()有什么区别?
torch.Storage()是什么?和torch.Tensor()有什么区别?
9 1
torch.argmax(dim=1)用法
)torch.argmax(input, dim=None, keepdim=False)返回指定维度最大值的序号;
547 0
|
11月前
|
数据格式
详解torch.size
详解torch.size
127 0
详解torch.size
|
11月前
|
PyTorch 算法框架/工具 索引
Pytorch函数view、permute、squeeze、usqueeze
Pytorch函数view、permute、squeeze、usqueeze
|
PyTorch 算法框架/工具 异构计算
Pytorch出现RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor)
这个问题的主要原因是输入的数据类型与网络参数的类型不符。
202 0
|
存储 测试技术
测试模型时,为什么要with torch.no_grad(),为什么要model.eval(),如何使用with torch.no_grad(),model.eval(),同时使用还是只用其中之一
在测试模型时,我们通常使用with torch.no_grad()和model.eval()这两个方法来确保模型在评估过程中的正确性和效率。
620 0
|
PyTorch 算法框架/工具
torch.split 的用法
这将返回一个元组,包含 3 个大小分别为 (6, 2)、(6, 2) 和 (6, 4) 的张量。 需要注意的是,当给定的拆分大小不等于张量在指定维度上的大小时,torch.split() 会引发一个异常。
375 0
|
PyTorch 算法框架/工具 Python
查看torch中的所有函数
hspmm hstack hub hypot i0 i0_ igamma igammac iinfo imag import_ir_module import_ir_module_from_buffer index_add index_copy index_fill index_put index_put_ index_select init_num_t
304 0