Skip to content

Commit

Permalink
doc: update docs/pytorch.md (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaZeroo committed Mar 5, 2024
1 parent 435fdea commit 5fc5c06
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/pytorch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down

0 comments on commit 5fc5c06

Please sign in to comment.