Skip to content

API operator examples #11

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Binary file modified .DS_Store
Binary file not shown.
Binary file modified api-examples/.DS_Store
Binary file not shown.
Binary file modified api-examples/operator/.DS_Store
Binary file not shown.
384 changes: 384 additions & 0 deletions api-examples/operator/.ipynb_checkpoints/ops.dsplit-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,384 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f244a0e4-7a41-4db8-a9b6-6da4b3ed0964",
"metadata": {},
"source": [
"## mindspore.ops.dsplit(input, indices_or_sections) -〉 tuple[Tensor]\n",
"沿着第三轴将输入Tensor分割成多个子Tensor。等同于axis = 2时的 ops.tensor_split 。\n",
"- 输入:\n",
" * input (Tensor)\n",
" * indices_or_sections (Union[int, tuple(int), list(int)])\n",
"- 返回:mindspore的tensor。"
]
},
{
"cell_type": "markdown",
"id": "1889e83d-6e4e-419a-a308-9bb0e4b551d9",
"metadata": {},
"source": [
"## 1、参数比较:\n",
"| mindspore | torch |\n",
"| :----: | :----: |\n",
"| input | input |\n",
"| indices_or_sections | indices_or_sections |"
]
},
{
"cell_type": "markdown",
"id": "eb184c6e-9cc7-4410-b92d-228b4bc1015b",
"metadata": {},
"source": [
"## 2、返回值比较 \n",
"当indices_or_sections 为 int时:"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "a651e5cd-21c8-44fa-be0b-824d3e393aeb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"input:\n",
" [[[ 0. 1. 2. 3.]\n",
" [ 4. 5. 6. 7.]]\n",
"\n",
" [[ 8. 9. 10. 11.]\n",
" [12. 13. 14. 15.]]]\n",
"\n",
"\n",
"torch output:\n",
" (tensor([[[ 0., 1.],\n",
" [ 4., 5.]],\n",
"\n",
" [[ 8., 9.],\n",
" [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n",
" [ 6., 7.]],\n",
"\n",
" [[10., 11.],\n",
" [14., 15.]]], dtype=torch.float64))\n",
"\n",
"\n",
"ms output:\n",
" (Tensor(shape=[2, 2, 2], dtype=Float64, value=\n",
"[[[ 0.00000000e+00, 1.00000000e+00],\n",
" [ 4.00000000e+00, 5.00000000e+00]],\n",
" [[ 8.00000000e+00, 9.00000000e+00],\n",
" [ 1.20000000e+01, 1.30000000e+01]]]), Tensor(shape=[2, 2, 2], dtype=Float64, value=\n",
"[[[ 2.00000000e+00, 3.00000000e+00],\n",
" [ 6.00000000e+00, 7.00000000e+00]],\n",
" [[ 1.00000000e+01, 1.10000000e+01],\n",
" [ 1.40000000e+01, 1.50000000e+01]]]))\n"
]
}
],
"source": [
"import numpy as np\n",
"import torch\n",
"import mindspore as ms\n",
"from mindspore import Tensor, ops\n",
"\n",
"input = np.arange(16.0).reshape(2, 2, 4)\n",
"print(\"input:\\n\", input)\n",
"print(\"\\n\")\n",
"output1 = torch.dsplit(torch.tensor(input), 2)\n",
"print(\"torch output:\\n\", output1)\n",
"print(\"\\n\")\n",
"output2 = ms.ops.dsplit(ms.tensor(input), 2)\n",
"print(\"ms output:\\n\", output2)"
]
},
{
"cell_type": "markdown",
"id": "e3ffb2b7-4a3d-48ac-933e-267ed55a487f",
"metadata": {},
"source": [
"当indices_or_sections 为list时:"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "7f0098d3-f25e-4f4c-b530-a9da368a3842",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch output1:\n",
" (tensor([[[ 0., 1., 2.],\n",
" [ 4., 5., 6.]],\n",
"\n",
" [[ 8., 9., 10.],\n",
" [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n",
" [ 7.]],\n",
"\n",
" [[11.],\n",
" [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n",
"\n",
"\n",
"ms output2:\n",
" (tensor([[[ 0., 1., 2.],\n",
" [ 4., 5., 6.]],\n",
"\n",
" [[ 8., 9., 10.],\n",
" [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n",
" [ 7.]],\n",
"\n",
" [[11.],\n",
" [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n"
]
}
],
"source": [
"output1 = torch.dsplit(torch.tensor(input), [3, 6])\n",
"print(\"torch output1:\\n\", output1)\n",
"print(\"\\n\")\n",
"output2 = ms.ops.dsplit(ms.tensor(input), [3, 6])\n",
"print(\"ms output2:\\n\", output1)"
]
},
{
"cell_type": "markdown",
"id": "6171dd9a-ad43-4312-aff7-ccdb24686491",
"metadata": {},
"source": [
"### 与tensor_split接口比较:"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "fe162c23-2033-48c5-be31-c35606ef4aed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch output1:\n",
" (tensor([[[ 0., 1.],\n",
" [ 4., 5.]],\n",
"\n",
" [[ 8., 9.],\n",
" [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n",
" [ 6., 7.]],\n",
"\n",
" [[10., 11.],\n",
" [14., 15.]]], dtype=torch.float64))\n",
"torch output2:\n",
" (tensor([[[ 0., 1., 2.],\n",
" [ 4., 5., 6.]],\n",
"\n",
" [[ 8., 9., 10.],\n",
" [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n",
" [ 7.]],\n",
"\n",
" [[11.],\n",
" [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n"
]
}
],
"source": [
"output1 = torch.tensor_split(torch.tensor(input), 2, 2)\n",
"print(\"torch output1:\\n\", output1)\n",
"output2 = torch.tensor_split(torch.tensor(input), [3, 6], 2)\n",
"print(\"torch output2:\\n\", output2)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "a3a9196a-46b4-484a-ba52-8ed96c18aab1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ms output1:\n",
" (Tensor(shape=[2, 2, 2], dtype=Float64, value=\n",
"[[[ 0.00000000e+00, 1.00000000e+00],\n",
" [ 4.00000000e+00, 5.00000000e+00]],\n",
" [[ 8.00000000e+00, 9.00000000e+00],\n",
" [ 1.20000000e+01, 1.30000000e+01]]]), Tensor(shape=[2, 2, 2], dtype=Float64, value=\n",
"[[[ 2.00000000e+00, 3.00000000e+00],\n",
" [ 6.00000000e+00, 7.00000000e+00]],\n",
" [[ 1.00000000e+01, 1.10000000e+01],\n",
" [ 1.40000000e+01, 1.50000000e+01]]]))\n",
"ms output2:\n",
" (Tensor(shape=[2, 2, 3], dtype=Float64, value=\n",
"[[[ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00],\n",
" [ 4.00000000e+00, 5.00000000e+00, 6.00000000e+00]],\n",
" [[ 8.00000000e+00, 9.00000000e+00, 1.00000000e+01],\n",
" [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01]]]), Tensor(shape=[2, 2, 1], dtype=Float64, value=\n",
"[[[ 3.00000000e+00],\n",
" [ 7.00000000e+00]],\n",
" [[ 1.10000000e+01],\n",
" [ 1.50000000e+01]]]), Tensor(shape=[2, 2, 0], dtype=Float64, value=\n",
"))\n"
]
}
],
"source": [
"output1 = ms.ops.tensor_split(ms.tensor(input), 2, 2)\n",
"print(\"ms output1:\\n\", output1)\n",
"output2 = ms.ops.tensor_split(ms.tensor(input), [3, 6], 2)\n",
"print(\"ms output2:\\n\", output2)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "bd32c32a-2262-494b-a2c6-993b1326a7a6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tensor([[0, 1],\n",
" [2, 3],\n",
" [4, 5]]),\n",
" tensor([[6, 7],\n",
" [8, 9]]))"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = torch.arange(10).reshape(5, 2)\n",
"torch.split(a, 3)"
]
},
{
"cell_type": "markdown",
"id": "e71d8217-1c2f-4fde-aa01-4191cba54d9c",
"metadata": {},
"source": [
"当axis=2时,ms与torch的dsplit与tensor_split接口返回值一致。\n",
"但ms的两个接口输出格式有区别。"
]
},
{
"cell_type": "markdown",
"id": "b7a362b7-f9df-4dfe-8743-1d4089218fb3",
"metadata": {},
"source": [
"### 与torch.split的比较: \n",
"第二个参数含义不同:\n",
"<!DOCTYPE html>\n",
"<html lang=\"en\">\n",
"<head>\n",
" <meta charset=\"UTF-8\">\n",
" <style>\n",
"table{border-top:1px solid #333;border-left:1px solid #333;border-spacing:0;background-color:#fff;width:100%}\n",
"table td{border-bottom:1px solid #333;border-right:1px solid #333;font-size:13px;padding:5px}\n",
".et2{text-align:center ;}\n",
".font0{color:rgb(0, 0, 0);}\n",
"</style>\n",
"</head>\n",
"<body>\n",
" <table style=\"width:358.02pt\"> \n",
" <colgroup>\n",
" <col width=\"48\" style=\"width:48.00pt;\"> \n",
" <col width=\"170\" style=\"width:170.00pt;\"> \n",
" <col width=\"140\" style=\"width:140.80pt;\"> \n",
" </colgroup>\n",
" <tbody>\n",
" <tr height=\"16\"> \n",
" <td class=\"et2\"></td> \n",
" <td class=\"et2\">torch.dsplit: indices_or_sections</td> \n",
" <td class=\"et2\">torch.split: split_size_or_sections</td> \n",
" </tr> \n",
" <tr height=\"16\"> \n",
" <td class=\"et2\">int</td> \n",
" <td class=\"et2\">切分的份数</td> \n",
" <td class=\"et2\">切分每份的大小</td> \n",
" </tr> \n",
" <tr height=\"16\"> \n",
" <td class=\"et2\">list</td> \n",
" <td class=\"et2\">在索引处进行切分</td> \n",
" <td class=\"et2\">切分每份的大小</td> \n",
" </tr> \n",
" </tbody>\n",
"</table>\n",
"</body>\n",
"</html>\n",
"\n",
"所以为了得到同样的输出,入参需要改变,如下:"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "88508217-74b0-42d9-bd5a-35f6fc659018",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch output1:\n",
" (tensor([[[ 0., 1.],\n",
" [ 4., 5.]],\n",
"\n",
" [[ 8., 9.],\n",
" [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n",
" [ 6., 7.]],\n",
"\n",
" [[10., 11.],\n",
" [14., 15.]]], dtype=torch.float64))\n",
"torch output2:\n",
" (tensor([[[ 0., 1., 2.],\n",
" [ 4., 5., 6.]],\n",
"\n",
" [[ 8., 9., 10.],\n",
" [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n",
" [ 7.]],\n",
"\n",
" [[11.],\n",
" [15.]]], dtype=torch.float64))\n"
]
}
],
"source": [
"output1 = torch.split(torch.tensor(input), 2, 2)\n",
"print(\"torch output1:\\n\", output1)\n",
"output2 = torch.split(torch.tensor(input), [3, 1], 2)\n",
"print(\"torch output2:\\n\", output2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading