pytorch(03)tensor的操作
张量操作
一、张量的拼接
- torch.cat()
功能:将张量按维度dim进行拼接,且[不会扩张张量的维度]
- tensors:张量序列
- dim:要拼接的维度
torch.cat(tensors,
dim=0,
out=None)
flag = True
# flag = False
if flag:
t1 = torch.full((4, 4), 10)
t2 = torch.full((4, 4), 5)
print(t1)
print(t2)
t3 = torch.cat([t1, t2], dim=0,out=None)
print(t3)
print("t1.shape:{}\nt2.shape:{}\nt3.shape:{}".format(t1.shape, t2.shape, t3.shape))
tensor([[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10]])
tensor([[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5]])
tensor([[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[ 5, 5, 5, 5],
[ 5, 5, 5, 5],
[ 5, 5, 5, 5],
[ 5, 5, 5, 5]])
t1.shape:torch.Size([4, 4])
t2.shape:torch.Size([4, 4])
t3.shape:torch.Size([8, 4])
- torch.stack()
功能:在新创建的维度dim上进行拼接[会扩张张量的维度]
- tensors:张量序列
- dim:要拼接的维度
torch.stack(
tensors,
dim=0,
out=None)
flag = True
# flag = False
if flag:
t1 = torch.full((2, 4), 10)
t2 = torch.full((2, 4), 5)
print(t1)
print(t2)
t3 = torch.stack([t1, t1, t2], dim=1,out=None)
print(t3)
print("t1.shape:{}\nt2.shape:{}\nt3.shape:{}".format(t1.shape, t2.shape, t3.shape))
tensor([[10, 10, 10, 10],
[10, 10, 10, 10]])
tensor([[5, 5, 5, 5],
[5, 5, 5, 5]])
tensor([[[10, 10, 10, 10],
[10, 10, 10, 10],
[ 5, 5, 5, 5]],
[[10, 10, 10, 10],
[10, 10, 10, 10],
[ 5, 5, 5, 5]]])
t1.shape:torch.Size([2, 4])
t2.shape:torch.Size([2, 4])
t3.shape:torch.Size([2, 3, 4])
二、张量的切分
- torch.chunk(input,chunks,dim=0)
将tensor按照dim进行平均切分返回张量列表,若不能整除,最后一份tensor小于其他张量
- input:需要切分的张量
- chunks:要切分的份数
- dim:要切分的维度
flag = True
# flag = False
if flag:
t1 = torch.full((6, 5), 10)
t3 = torch.chunk(t1, chunks=3, dim=0)
for num, t in enumerate(t3):
print(num, t, t.shape)
0 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
1 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
2 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
- torch.split(tensor,split_size_or_sections,dim)
将张量按照dim进行切分,返回张量列表
split_size_or_sections:为int时表示每一份的长度,为list时表示按list元素切分
flag = True
# flag = False
if flag:
t1 = torch.full((6, 5), 10)
t3 = torch.split(t1, split_size_or_sections=2, dim=0)
t4 = torch.split(t1, [2, 1, 1, 1, 1], dim=0)
for num, t in enumerate(t4):
print(num, t, t.shape)
for num, t in enumerate(t3):
print(num, t, t.shape)
0 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
1 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
2 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
3 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
4 tensor([[10, 10, 10, 10, 10]]) torch.Size([1, 5])
0 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
1 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
2 tensor([[10, 10, 10, 10, 10],
[10, 10, 10, 10, 10]]) torch.Size([2, 5])
三、tensor索引
- torch.index_select(input,dim,index,out=None)
按照index索引数据返回依索引数据拼接的张量
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 5))
t_list = [0, 1]
t2 = torch.tensor(t_list,dtype=torch.long)
t4 = torch.index_select(t1, dim=0, index=t2, out=None)
print(t1)
print(t4)
tensor([[5, 6, 1, 6, 8],
[2, 3, 6, 9, 1],
[3, 4, 2, 9, 5],
[1, 4, 7, 3, 8],
[7, 7, 9, 8, 7],
[1, 8, 9, 9, 5]])
tensor([[5, 6, 1, 6, 8],
[2, 3, 6, 9, 1]])
- torch.masked_select(input,mask,out=None)
按mask中的True进行索引,并返回一维张量,其中mask代表与input同形状的布尔类型张量
eq,ne,gt,ge,lt,le
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 5))
t1_mask = t1.ge(5)
t4 = torch.masked_select(t1,t1_mask)
print(t1)
print(t1_mask)
print(t4)
tensor([[5, 6, 1, 6, 8],
[2, 3, 6, 9, 1],
[3, 4, 2, 9, 5],
[1, 4, 7, 3, 8],
[7, 7, 9, 8, 7],
[1, 8, 9, 9, 5]])
tensor([[ True, True, False, True, True],
[False, False, True, True, False],
[False, False, False, True, True],
[False, False, True, False, True],
[ True, True, True, True, True],
[False, True, True, True, True]])
tensor([5, 6, 6, 8, 6, 9, 9, 5, 7, 8, 7, 7, 9, 8, 7, 8, 9, 9, 5])
四、张量变换
3.1 torch.reshape(input,shape)
变换张量形状,当张量在内存中是连续时,新张量与input共享数据内存
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 5))
t2 = torch.reshape(t1, (-1,3,5))
t1[0,0] = 33
print(t1, id(t1.data))
print(t2, id(t2.data))
tensor([[33, 6, 1, 6, 8],
[ 2, 3, 6, 9, 1],
[ 3, 4, 2, 9, 5],
[ 1, 4, 7, 3, 8],
[ 7, 7, 9, 8, 7],
[ 1, 8, 9, 9, 5]]) 1778886266112
tensor([[[33, 6, 1, 6, 8],
[ 2, 3, 6, 9, 1],
[ 3, 4, 2, 9, 5]],
[[ 1, 4, 7, 3, 8],
[ 7, 7, 9, 8, 7],
[ 1, 8, 9, 9, 5]]]) 1778886266112
3.2 torch.transpose(input,dim0,dim1)
交换两个维度,即转置.
在图像预处理部分会用到,比如我们的输入图像是 c*h*w,维数乘高乘宽,可以使用两次变换得到h*w*c
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(2, 6, 3))
t2 = torch.transpose(t1,1,2)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]],
[[3, 8, 7],
[7, 9, 8],
[7, 1, 8],
[9, 9, 5],
[6, 3, 7],
[7, 8, 7]]]) torch.Size([2, 6, 3])
tensor([[[5, 6, 3, 1, 2, 1],
[6, 8, 6, 3, 9, 4],
[1, 2, 9, 4, 5, 7]],
[[3, 7, 7, 9, 6, 7],
[8, 9, 1, 9, 3, 8],
[7, 8, 8, 5, 7, 7]]]) torch.Size([2, 3, 6])
3.3 torch.t(input)
二维张量转置,相当于对二维矩阵作torch.transpose(input,0,1)
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3))
t2 = torch.t(t1)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])
tensor([[5, 6, 3, 1, 2, 1],
[6, 8, 6, 3, 9, 4],
[1, 2, 9, 4, 5, 7]]) torch.Size([3, 6])
3.4 torch.squeeze(input,dim=None,out=None)
压缩长度为1的维度(轴),若指定维度,当且仅当该轴长度为1时,可以被移除
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3, 1))
t2 = torch.squeeze(t1)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[[5],
[6],
[1]],
[[6],
[8],
[2]],
[[3],
[6],
[9]],
[[1],
[3],
[4]],
[[2],
[9],
[5]],
[[1],
[4],
[7]]]) torch.Size([6, 3, 1])
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])
3.5 torch.usqueeze(input,dim,out=None)
依据dim扩展维度
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3))
t2 = torch.unsqueeze(t1,dim=2)
print(t1,t1.shape)
print(t2,t2.shape)
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])
tensor([[[5],
[6],
[1]],
[[6],
[8],
[2]],
[[3],
[6],
[9]],
[[1],
[3],
[4]],
[[2],
[9],
[5]],
[[1],
[4],
[7]]]) torch.Size([6, 3, 1])
张量的数学运算
张量的加减乘除,对数指数幂函数,三角函数
- torch.add(input,alpha=1,outher,out=None)
实现逐元素计算input+alpha*other,input是第一个张量,alpha是乘项因子,other是第二个张量。
torch.addcdiv()
\]
torch.addcmul(input,value=1,tensor1,tensor2,out=None)
\]
flag = True
# flag = False
if flag:
torch.manual_seed(1)
t1 = torch.randint(1, 10, size=(6, 3))
t2 = torch.ones_like(t1)
t3 = torch.add(t1,t2,alpha=2)
print(t1,t1.shape)
print(t2,t2.shape)
print(t3,t3.shape)
tensor([[5, 6, 1],
[6, 8, 2],
[3, 6, 9],
[1, 3, 4],
[2, 9, 5],
[1, 4, 7]]) torch.Size([6, 3])
tensor([[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]) torch.Size([6, 3])
tensor([[ 7, 8, 3],
[ 8, 10, 4],
[ 5, 8, 11],
[ 3, 5, 6],
[ 4, 11, 7],
[ 3, 6, 9]]) torch.Size([6, 3])
线性回归
线性回归是分析一个变量与另外一个变量之间关系的方法
因变量:y 自变量:x 关系:线性
y = wx+b
分析:求解w,b
求解步骤:
- 确定模型,Model:y = wx+b
- 选择损失函数,MSE:
\]
- 求解梯度并更新w,b
w = w - LR* w.grad
b = b -LR* b.grad
pytorch(03)tensor的操作的更多相关文章
- Pytorch Tensor 常用操作
https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...
- 对pytorch中Tensor的剖析
不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ...
- pytorch之Tensor
#tensor和numpy import torch import numpy as np numpy_tensor = np.random.randn(3,4) print(numpy_tensor ...
- Tensor索引操作
#Tensor索引操作 ''''' Tensor支持与numpy.ndarray类似的索引操作,语法上也类似 如无特殊说明,索引出来的结果与原tensor共享内存,即修改一个,另一个会跟着修改 ''' ...
- pytorch中tensor数据和numpy数据转换中注意的一个问题
转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...
- [Pytorch]Pytorch中tensor常用语法
原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ...
- PyTorch中的CUDA操作
CUDA(Compute Unified Device Architecture)是NVIDIA推出的异构计算平台,PyTorch中有专门的模块torch.cuda来设置和运行CUDA相关操作.本 ...
- [Pytorch]Pytorch的tensor变量类型转换
原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ...
- pytorch中Math operation操作:torch.ger()
torch.ger(vec1, vec2, out=None) → Tensor Outer product of vec1 and vec2. If vec1 is a vector of size ...
随机推荐
- P1541 乌龟棋(DP)
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行NNN个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第NNN格是终点,游戏要求玩家控制一个乌龟 ...
- The Balance HDU - 1709 母函数(板子变化)
题意: 现在你被要求用天平和一些砝码来量一剂药.当然,这并不总是可以做到的.所以你应该找出那些不能从范围[1,S]中测量出来的品质.S是所有重量的总质量. 输入一个n,后面有n个数,表示这n个物品的质 ...
- hdu2639 Bone Collector II
Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in the ...
- Codeforces Beta Round #92 (Div. 2 Only) B. Permutations
You are given n k-digit integers. You have to rearrange the digits in the integers so that the diffe ...
- Chrony时间同步
chrony 服务器 yum -y install chrony cp /etc/chrony.conf{,.bak} #备份默认配置 cat > /etc/chrony.conf <&l ...
- Linux网络文件下载
wget 以网络下载 maven 包为例 wget -c http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-ma ...
- 2.hello rabbitmq
作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-22 22:49:50 星期一 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...
- ++i和i++的区别
它们两个的数值变化的区别,我这里就不多说了 这里主要说明两者在效率上的区别 (1)首先如果是自带的数据类型,比如int型,++i和i++,编译器的实现方式是相同的,两者并没有效率上的区别,虽然也有副本 ...
- Leetcode(215)-数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...
- Verilog基础语法总结
去年小学期写的,push到博客上好了 Verilog 的基本声明类型 wire w1; // 线路类型 reg [-3:4] r1; // 八位寄存器 integer mem[0:2047]; // ...