当前位置:网站首页>pytorch的tensor创建和操作记录
pytorch的tensor创建和操作记录
2022-08-02 19:51:00 【此何人哉tan】
目录
Indexing, Slicing, Joining, Mutating Ops
一、Tensr
1、Tensor属性
numel # Returns the total number of elements in the input tensor.
repeat_interleaves
>>> a = torch.randn(1, 2, 3, 4, 5)
>>> torch.numel(a)
120
>>> a = torch.zeros(4,4)
>>> torch.numel(a)
16
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])
>>> y = torch.tensor([[1, 2], [3, 4]])
>>> torch.repeat_interleave(y, 2)
tensor([1, 1, 2, 2, 3, 3, 4, 4])
>>> torch.repeat_interleave(y, 3, dim=1)
tensor([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4]])
>>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
tensor([[1, 2],
[3, 4],
[3, 4]])
>>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0, output_size=3)
tensor([[1, 2],
[3, 4],
[3, 4]]
2、创建
(1)
(2) 其它
>>> torch.eye(3)
tensor([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
二、Tensor基本操作
Indexing, Slicing, Joining, Mutating Ops
tensor.stack()
tensor.cat()
torch.squeeze()
torch.unsqueeze()
>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 1, 2])
三、Random sampling
bernoulli
multinomial
normal
poisson
rand
rand_like
randint
randint_like
randn
randn_like
randperm
# In-place random sampling
torch.Tensor.bernoulli_()
torch.Tensor.cauchy_()
torch.Tensor.exponential_()
torch.Tensor.geometric_()
torch.Tensor.log_normal_()
torch.Tensor.normal_()
torch.Tensor.random_()
torch.Tensor.uniform_()
四、数学运算
1、按元素操作
torch — PyTorch 1.12 documentationhttps://pytorch.org/docs/stable/torch.html#pointwise-ops
(1)加减乘除、绝对值
add
sub # subtract, Alias for torch.sub().
mul # multiply, Alias for torch.mul().
div # divide , Alias for torch.div().
abs # absolute, Alias for torch.abs()
加法举例:
import torch
a_list = [[1, -2, 3], [-4, 5, -6]]
a_tensor = torch.tensor(list1)
print(a_tensor)
# output:
tensor([[ 1, -2, 3],
[-4, 5, -6]])
# 加法操作
# ops 1
a_tensor.add(10)
# output1
tensor([[11, 8, 13],
[ 6, 15, 4]])
# ops 2
b_tensor = torch.ones_like(a_tensor)
a_tensor.add(b_tensor, alpha=19) # alpha=1(默认),a_tensor = alpha*b_tensor + a_tensor
# output1
tensor([[20, 17, 22],
[15, 24, 13]])
(2)指数、对数、幂运算、开方运算
(3)三角&反三角函数运算函数运算
(4)双曲线反双曲线运算
(5) 其他常用操作
clamp
reshape
view
clamp # clip Alias for torch.clamp().
a = torch.randn(4)
# output1
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
torch.clamp(a, min=-0.5, max=0.5)
# output2
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
(6)
# torch.bmm
>>> input = torch.randn(10, 3, 4)
>>> mat2 = torch.randn(10, 4, 5)
>>> res = torch.bmm(input, mat2)
>>> res.size()
torch.Size([10, 3, 5])
参考: torch.clamp — PyTorch 1.12 documentation
PyTorch:view() 与 reshape() 区别详解_地球被支点撬走啦的博客-CSDN博客_reshape和view
边栏推荐
猜你喜欢
Parse common methods in the Collection interface that are overridden by subclasses
ssdp协议搜索GB28181设备
SCANIA SCANIA OTL tag is introduced
太魔人招新啦|快来加入我们吧!
溜不溜是个问题
Brain-computer interface 003 | Musk said that he has realized a virtual self-dialogue with the cloud, and related concept shares have risen sharply
姑姑:给小学生出点口算题
解析Collection接口中的常用的被实现子类重写的方法
程序员也许都缺一个“二舅”精神
分享一个 web 应用版本监测 (更新) 的工具库
随机推荐
golang 源码分析:juju/ratelimit
2022-07-26
基于“无依赖绝对定位”实现的圣杯三栏布局
Five data structures of Redis and their corresponding usage scenarios
Thread线程类基本使用(下)
笑话:如果你在河边等待得足够久,你会看到你的敌人的尸体漂过,是怎么翻译出来的?
J9数字货币论:识别Web3新的稀缺性:开源开发者
es 官方诊断工具
SCANIA SCANIA OTL tag is introduced
EasyExcel实现动态列解析和存表
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
解析List接口中的常用的被实现子类重写的方法
健康报告-设计与实现
golang刷leetcode 动态规划(13) 最长公共子序列
Thread线程类基本使用(上)
PG's SQL execution plan
扫码预约 | 观看Apache Linkis数据处理实践以及计算治理能力
leetcode刷题记录:7.整数反转,8.字符串转整数,9.回文数
7月29-31 | APACHECON ASIA 2022
What is a Field Service Management System (FSM)?what is the benefit?