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]])
相关文章
|
3月前
|
机器学习/深度学习 PyTorch 算法框架/工具
torch.nn.Linear的使用方法
torch.nn.Linear的使用方法
107 0
|
PyTorch 算法框架/工具
pytorch中torch.clamp()使用方法
pytorch中torch.clamp()使用方法
458 0
pytorch中torch.clamp()使用方法
|
存储 PyTorch 算法框架/工具
Tensor to img && imge to tensor (pytorch的tensor转换)
Tensor to img && imge to tensor (pytorch的tensor转换)
|
17天前
|
PyTorch 算法框架/工具
【Pytorch】解决Fan in and fan out can not be computed for tensor with fewer than 2 dimensions
本文提供了两种解决PyTorch中由于torchtext版本问题导致的“Fan in and fan out can not be computed for tensor with fewer than 2 dimensions”错误的方法。
29 2
|
16天前
|
机器学习/深度学习 PyTorch 算法框架/工具
【Pytorch】Expected hidden[0] size (2, 136, 256), got [2, 256, 256]
文章解决了PyTorch中LSTM模型因输入数据的批次大小不一致导致的“Expected hidden[0] size”错误,并提供了两种解决方案:调整批次大小或在DataLoader中设置drop_last=True来丢弃最后一个不足批次大小的数据。
26 1
torch.argmax(dim=1)用法
)torch.argmax(input, dim=None, keepdim=False)返回指定维度最大值的序号;
599 0
|
数据格式
详解torch.size
详解torch.size
172 0
详解torch.size
|
PyTorch 算法框架/工具 索引
Pytorch函数view、permute、squeeze、usqueeze
Pytorch函数view、permute、squeeze、usqueeze
100 0
|
PyTorch 算法框架/工具 异构计算
Pytorch出现RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor)
这个问题的主要原因是输入的数据类型与网络参数的类型不符。
367 0
|
存储 测试技术
测试模型时,为什么要with torch.no_grad(),为什么要model.eval(),如何使用with torch.no_grad(),model.eval(),同时使用还是只用其中之一
在测试模型时,我们通常使用with torch.no_grad()和model.eval()这两个方法来确保模型在评估过程中的正确性和效率。
767 0