torch的常用数据操作

torch的常用数据操作

常用方法

  • torch.arange(num[, dtype=torch.float32]) # 返回一个数据种类dtype且长度num的一维向量
  • .shape # 返回张量的形状
  • .numel() # 返回张量的元素个数
  • .reshape(tuple) # tuple整数元组;返回一个形状为tuple的张量
  • torch.zeros(tuple) # 返回一个元素值全为0且为tuple的张量
  • torch.ones(tuple) # 返回一个元素值全为1且为tuple的张量
  • torch.randn(tuple) # 返回一个元素tuple且其中的每个元素都从均值为0、标准差为1的标准⾼斯分布(正态分布)中随机采样的张量
  • torch.tensor(array) # array数组;返回array的张量
  • torch.zeros_like(tensor) # tensor张量;返回与tensor张量形状一样的0张量

运算符

  • python运算符差不多( + - * / ** > < >= <= == 等)
  • torch.exp(tensor) # tensor张量;返回e的tensor次方的张量
  • torch.cat(tensorTuple ,dim = 0) # tensorTuple张量元组;dim已多少维标准;返回拼接的之后的张量
  • .sum() # 对张量所有元素求和

广播机制

  • 说白了就是比较小的张量按整数比例扩大, 扩大到与大的张量形状相同时,再进行运算

索引与切片

  • Python列表类似
  • [: , : ] # 冒号:是对此维进行选择;逗号,是区分不同维

节省内存

  • id(Ele) # 可以获取元素的地址
  • X[ : ] = X + Y或X += Y来减少操作的内存开销

转换

  • .numpy() # 转换为numpy数组
  • torch.tensor(numpy) # numpy数组转化为torch张量
  • .item(), float(tensor), int(tensor) # 将⼤小为1的张量转换为Python标量

torch的常用数据操作实例

## torch的常用数据操作实例
### 常用方法 
print(torch.arange(8)) # 返回一个数据种类int且长度8的一维向量
print(torch.arange(5, dtype = torch.float32))  # 返回一个数据种类torch.float32且长度8的一维向量

# ########
y = torch.tensor([[ 0,  1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11], [12, 13, 14, 15]])
print(y.shape) # 返回张量的形状

# ########
print(y.numel())  # 返回张量的元素个数

# ########
print(y.reshape(2, 8)) # 返回一个形状为(2, 8)的张量

# ########
print(torch.zeros(3, 10)) # 返回一个元素值全为0且为(3, 10)的张量

# ########
print(torch.ones(4, 9)) # 返回一个元素值全为1且为(4, 9)的张量

# ########
print(torch.randn(5, 5)) # 返回一个元素(5, 5)且其中的每个元素都从均值为0、标准差为1的标准⾼斯分布(正态分布)中随机采样的张量

# ########
y = torch.tensor([[ 0,  1,  2,  3], [ 4,  5,  6,  7], [ 8,  9, 10, 11], [12, 13, 14, 15]]) # 返回array的张量
print(y)

# ########
print(torch.zeros_like(y)) # 返回与y张量形状一样的0张量

### 运算符
a = torch.arange(12).reshape(3, 4) # 和python运算符差不多( + - * / ** > < >= <= == 等) 
b = torch.tensor([[ 1,  2,  2,  3], [ 5,  6,  6,  7], [ 8,  9, 10, 11]])
print(a + b, a - b, a * b, a / b , a ** b)

# ########
a = torch.arange(12).reshape(3, 4)
print(torch.exp(a)) # tensor张量;返回e的tensor次方的张量

# ########
c = torch.arange(27).reshape(3, 3, 3)
print(torch.cat((c , c) ,dim = 0), torch.cat((c , c) ,dim = 1), torch.cat((c , c) ,dim = 2)) # tensorTuple张量元组;dim已多少维标准;返回拼接的之后的张量

# ########
d = torch.arange(27).reshape(3, 3, 3)
print(d.sum()) # 对张量所有元素求和

### 广播机制
a = torch.arange(27).reshape((3, 1, 9))# 说白了就是比较小的张量按整数比例扩大, 扩大到与大的张量形状相同时,再进行运算
b = torch.arange(3).reshape((3, 1))
print(a + b)

### 索引与切片
a = torch.arange(81).reshape((3, 3, 9))
print(a, a[: : ], a[0 : 2, 1: 2, 5:8])

### 节省内存
a = torch.arange(9).reshape((3, 3))
ida = id(a)
b = torch.arange(9).reshape((3, 3))
print(ida) # 可以获取元素的地址
a[ : ] = a + b
print(id(a) == ida)
a += b
print(id(a) == ida)

### 转换
t = torch.arange(12).reshape((4, 3))
np = t.numpy()
print(np) # 转换为numpy数组
print(torch.tensor(np))  # numpy数组转化为torch张量
one = torch.tensor([1])
print(one.item(), float(one), int(one) )# 将⼤小为1的张量转换为Python标量
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值