pytorch tensor相关知识点

创建 tensor

  • list 或者 numpy array 创建 tensor
import torch
import numpy as np
x = torch.tensor([[-1,1],[1, -1]])
x = torch.from_numpy(np.array([[1, -1], [-1, 1]]))
  • 全 0 tensor (zero tensor)
x = torch.zeros([2, 2])
  • 全 1 tensor (unit tensor)
x = torch.ones([2, 2])

tensor 的常见运算

  • squeeze: 移除长度为1的指定维度
x = torch.zeros([1, 2, 3])
print('before',x.shape)
x = x.squeeze(0)
print('after',x.shape)
'''输出:
before torch.Size([1, 2, 3])
after torch.Size([2, 3])
'''
  • unsqueeze:拓展一个新的维度,在一个指定维度前面添加一个长度为1的新维度
x = torch.zeros([2, 3])
print('before',x.shape)
x = x.unsqueeze(1)
print('after',x.shape)
```python
'''输出:
before torch.Size([2, 3])
after torch.Size([2, 1, 3])
'''
  • Transpose:转置两个指定维度
x = torch.zeros([2, 3])
print('before',x.shape)
x = x.transpose(0, 1)
print('after',x.shape)
```python
'''输出:
before torch.Size([2, 3])
after torch.Size([3, 2])
'''
  • cat:连接多个张量
x = torch.zeros([2, 1, 3])
y = torch.zeros([2, 3, 3])
z = torch.zeros([2, 2, 3])
w = torch.cat([x, y, z], dim = 1)
w.shape
'''输出:
torch.Size([2, 6, 3])
'''
  • sum加在一起
x = torch.ones([2, 3])
y = x.sum()
y
'''输出:
tensor(6.)
'''

Tensor vs Numpy

属性

PytorchNumpy
x.shapex.shape
x.dtypex.dtype
  • tensor.numel() 查看tensor内元素个数
  • trensor.nelment() 查看tensor内元素个数

改变形状

PytorchNumPy
x.reshape / x.viewx.reshape
x.squeeze()x.squeeze()
x.unsqueeze(1)np.expand_dims(x, 1)

设备

default 情况下,tensor 和 modules 会在CPU上面计算

x = x.to('cpu')
x = x.to('cuda')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值