创建 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
属性
Pytorch | Numpy |
---|---|
x.shape | x.shape |
x.dtype | x.dtype |
tensor.numel()
查看tensor内元素个数trensor.nelment()
查看tensor内元素个数
改变形状
Pytorch | NumPy |
---|---|
x.reshape / x.view | x.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')