doc: update docs/pytorch.md (#554)

This commit is contained in:
JavaZero 2024-03-05 21:23:43 +08:00 committed by GitHub
parent 435fdead51
commit 5fc5c065df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -174,6 +174,30 @@ tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
注意: 所有在CPU上的Tensors, 除了CharTensor, 都可以转换为Numpy array并可以反向转换.
### squeeze函数
```python
>>> x = torch.rand(1, 2, 1, 28, 1)
>>> x.squeeze().shape # squeeze不加参数默认去除所有为1的维度
torch.Size([2, 28])
>>> x.squeeze(dim=0).shape # squeeze加参数去除指定为1的维度
torch.Size([2, 1, 28, 1])
>>> x.squeeze(1).shape # squeeze加参数如果不为1则不变
torch.Size([1, 2, 1, 28, 1])
>>> torch.squeeze(x,-1).shape # 既可以是函数,也可以是方法
torch.Size([1, 2, 1, 28])
```
### unsqueeze函数
```python
>>> x = torch.rand(2, 28)
>>> x.unsqueeze(0).shape # unsqueeze必须加参数 _ 2 _ 28 _
torch.Size([1, 2, 28]) # 参数代表在哪里添加维度 0 1 2
>>> torch.unsqueeze(x, -1).shape # 既可以是函数,也可以是方法
torch.Size([2, 28, 1])
```
导入 Imports
---