Skip to content

映射文档 No.48 #5840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## [torch 参数更多 ]torch.inverse

### [torch.inverse](https://pytorch.org/docs/stable/generated/torch.inverse.html?highlight=inverse#torch.inverse)

```python
torch.inverse(input, *, out=None)
```

### [paddle.linalg.inv](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/inv_cn.html)

```python
paddle.linalg.inv(x, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> input </font> | <font color='red'> x </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> out </font> | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |


### 转写示例

#### out:指定输出
```python
# Pytorch 写法
torch.inverse(torch.tensor([[2., 0.], [0., 2.]]), out=y)

# Paddle 写法
paddle.assign(paddle.inverse(paddle.to_tensor([[2, 0], [0, 2]], dtype='float32')), y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多 ]torch.lu

### [torch.lu](https://pytorch.org/docs/stable/generated/torch.inverse.html?highlight=inverse#torch.inverse)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

链接不正确


```python
torch.lu(A, pivots=True, get_infos=False, *, out)
```

### [paddle.linalg.lu](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/lu_cn.html)

```python
paddle.linalg.lu(x, pivot=True, get_infos=False, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> A </font> | <font color='red'> x </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> pivots </font> | <font color='red'> pivot </font> | 输入的 bool ,仅参数名不同。 |
| <font color='red'> get_infos </font> | <font color='red'> get_infos </font> | 输入的 bool ,仅参数名不同。 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里参数名是相同的~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更正

| <font color='red'> out </font> | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |

### 转写示例

#### out:指定输出
```python
# Pytorch 写法
torch.lu(torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]), get_infos=True, out=(A_LU, pivots, info))

# Paddle 写法
lu, p, info = paddle.linalg.lu(paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64'), get_infos=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以用paddle.assign实现赋值吗

Copy link
Contributor Author

@YiZoey YiZoey Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle.assign(x, output)中output为Tensor,此处assign会报TypeError: Unsupport paddle.assign([Variable, Variable...]) with non-scalar variable;
paddle.linalg.lu返回值为tuple,且tuple内Tensor维度不一致, 因此无法将结果转为list等方式拼接;
尚未找到合适赋值方式,暂未修改

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多 ]torch.lu_unpack
### [torch.lu_unpack](https://pytorch.org/docs/stable/generated/torch.lu_unpack.html?highlight=lu_unpack#torch.lu_unpack)

```python
torch.lu_unpack(LU_data, LU_pivots, unpack_data=True, unpack_pivots=True, *, out=None)
```

### [paddle.linalg.lu_unpack](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/lu_unpack_cn.html)

```python
paddle.linalg.lu_unpack(x, y, unpack_ludata=True, unpack_pivots=True, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> LU_data </font> | <font color='red'> x </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> LU_pivots </font> | <font color='red'> y </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> unpack_data </font> | <font color='red'> unpack_ludata </font> | 输入的 bool ,仅参数名不同。 |
| <font color='red'> unpack_pivots </font> | <font color='red'> unpack_pivots </font> | 输入的 bool ,仅参数名不同。 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数名是一样的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更正

| <font color='red'> out </font> | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |

### 转写示例

#### out:指定输出
```python
# Pytorch 写法
torch.lu_unpack(*torch.lu(torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])), out=(P, L, U))

# Paddle 写法
P,L,U = paddle.linalg.lu_unpack(*paddle.linalg.lu(paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64')))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

试一下可以用paddle.assign实现吗

Copy link
Contributor Author

@YiZoey YiZoey Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同paddle.linalg.lu

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## [torch 参数更多 ]torch.matrix_power
### [torch.matrix_power](https://pytorch.org/docs/stable/generated/torch.matrix_power.html?highlight=matrix_power)
```python
torch.matrix_power(input, n, *, out=None)
```

### [paddle.linalg.matrix_power](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/matrix_power_cn.html)
```python
paddle.linalg.matrix_power(x, n, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| input | x | 输入的 Tensor ,仅参数名不同。 |
| n | n | 输入的 int ,仅参数名不同。 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数名相同~

| out | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |


### 转写示例
#### out:指定输出
```python
# Pytorch 写法
torch.matrix_power(torch.tensor([[1., 2., 3.],[1., 4., 9.],[1., 8., 27.]]), 2, out=y)

# Paddle 写法
paddle.assign(paddle.linalg.matrix_power(paddle.to_tensor([[1, 2, 3],[1, 4, 9],[1, 8, 27]], dtype='float64'), 2), y)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里paddle的写法中,可以将1换成 1. ,这样就不用显式写dtype了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## [torch 参数更多 ]torch.matrix_rank
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

签名和api名不一致~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更正

### [torch.matrix_rank](https://pytorch.org/docs/stable/generated/torch.linalg.matrix_rank.html?highlight=matrix_rank#torch.linalg.matrix_rank)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里少了linalg 吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原在线文档中该函数为torch.matrix_rank,pytorch1.13中实际只有torch.linalg.matrix_rank,已修改该api名及其文件名

```python
torch.linalg.matrix_rank(A, tol=None, hermitian=False, *, out=None)
```

### [paddle.linalg.matrix_rank](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/linalg/matrix_rank_cn.html)
```python
paddle.linalg.matrix_rank(x, tol=None, hermitian=False, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:
### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| A | x | 输入的 Tensor ,仅参数名不同。 |
| tol | tol | 输入的 Tensor 或者 float,仅参数名不同。 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数名是一致的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更正

| hermitian | hermitian | 输入的 bool ,仅参数名不同。 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数名是一致的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更正

| out | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |


### 转写示例
#### out:指定输出
```python
# Pytorch 写法
torch.linalg.matrix_rank(torch.ones(3, 4, 5, 5), tol=0.01, hermitian=True, out=y)

# Paddle 写法
paddle.assign(paddle.linalg.matrix_rank(paddle.ones(shape=[3, 4, 5, 5]), tol=0.01, hermitian=True), y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多 ]torch.dot

### [torch.dot](https://pytorch.org/docs/stable/generated/torch.dot.html?highlight=dot#torch.dot)

```python
torch.dot(input, other, *, out=None)
```

### [paddle.dot](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/dot_cn.html#dot)

```python
paddle.dot(x, y, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> input </font> | <font color='red'> x </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> other </font> | <font color='red'> y </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> out </font> | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |


### 转写示例

#### out:指定输出
```python
# Pytorch 写法
torch.dot(torch.tensor([2, 3]), torch.tensor([2, 1]), out=y)

# Paddle 写法
y = paddle.dot(paddle.to_tensor([2, 3]), paddle.to_tensor([2, 1]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

赋值可以用paddle.assign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多 ]torch.ger

### [torch.ger](https://pytorch.org/docs/stable/generated/torch.ger.html?highlight=ger#torch.ger)

```python
torch.ger(input, vec2, *, out=None)
```

### [paddle.outer](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/outer_cn.html)

```python
paddle.outer(x, y, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> input </font> | <font color='red'> x </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> vec2 </font> | <font color='red'> y </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> out </font> | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |


### 转写示例

#### out:指定输出
```python
# Pytorch 写法
torch.ger(torch.arange(1., 5.), torch.arange(1., 4.), out=y)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写的比较复杂,建议写简略一些


# Paddle 写法
paddle.assign(paddle.outer(paddle.arange(1, 5).astype('float32'), paddle.arange(1, 4).astype('float32')), y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多 ]torch.inner

### [torch.inner](https://pytorch.org/docs/stable/generated/torch.inner.html?highlight=inner#torch.inner)

```python
torch.inner(input, other, *, out=None)
```

### [paddle.inner](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/inner_cn.html)

```python
paddle.inner(x, y, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> input </font> | <font color='red'> x </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> other </font> | <font color='red'> y </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> out </font> | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |


### 转写示例

#### out:指定输出
```python
# Pytorch 写法
torch.inner(torch.arange(1., 7.).reshape((2, 3)), torch.arange(1., 10.).reshape((3, 3)), out=y)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后面可以尽量把示例写的简单些,直接写list表示Tensor也行,这个本质是伪代码的形式让用户一眼看了就懂转换思路的。写出核心转换思路就可以,辅助运行的代码应省尽省。


# Paddle 写法
paddle.assign(paddle.inner(paddle.arange(1, 7).reshape((2, 3)).astype('float32'), paddle.arange(1, 10).reshape((3, 3)).astype('float32')), y)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## [torch 参数更多 ]torch.outer

### [torch.outer](https://pytorch.org/docs/stable/generated/torch.outer.html#torch.outer)

```python
torch.outer(input, vec2, *, out=None)
```

### [paddle.outer](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/outer_cn.html)

```python
paddle.outer(x, y, name=None)
```

其中 Pytorch 相比 Paddle 支持更多其他参数,具体如下:

### 参数映射
| PyTorch | PaddlePaddle | 备注 |
| ------------- | ------------ | ------------------------------------------------------ |
| <font color='red'> input </font> | <font color='red'> x </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> vec2 </font> | <font color='red'> y </font> | 输入的 Tensor ,仅参数名不同。 |
| <font color='red'> out </font> | - | 表示输出的 Tensor,PaddlePaddle 无此参数,需要进行转写。 |


### 转写示例

#### out:指定输出
```python
# Pytorch 写法
torch.outer(torch.arange(1., 5.), torch.arange(1., 4.), out=y)

# Paddle 写法
paddle.assign(paddle.outer(paddle.arange(1, 5).astype('float32'), paddle.arange(1, 4).astype('float32')), y)
```