{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "2579fa00-0dc6-4672-8a96-5a6bb315881f", "metadata": {}, "source": [ "# 张量视图\n", "\n", "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/docs/mindspore/source_zh_cn/design/tensor_view.ipynb)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "d2f9816d-fa7a-4524-accb-4fa3c30a2dc4", "metadata": {}, "source": [ "张量视图(Tensor Views)是指一个Tensor经过[view类算子](#view类算子)的返回值,与该Tensor共享内存数据,避免了数据复制,从而可以进行快速且内存高效的重塑、切片和逐元素操作。\n", "\n", "例如,要获取Tensor t的视图,可以用t.view(...)。\n", "\n", "```python\n", "from mindspore import Tensor\n", "import numpy as np\n", "t = Tensor(np.array([[1, 2, 3], [2, 3, 4]], dtype=np.float32))\n", "b = t.view((3, 2))\n", "# Modifying view Tensor changes base Tensor as well.\n", "b[0][0] = 100\n", "print(t[0][0])\n", "# 100\n", "```\n", "\n", "由于视图与其原来的Tensor共享底层数据,因此如果在视图中修改数据,它也会反映在原来的Tensor中。\n", "\n", "通常,MindSpore算子操作会返回一个新的Tensor作为输出,例如[add()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.add.html)。\n", "但在视图操作的情况下,输出是输入Tensor的视图,以避免不必要的数据复制。创建视图时不会发生数据移动,视图Tensor只是改变它解析相同数据的方式。\n", "使用Tensor Views可能会使内存储存连续的Tensor产生内存存储非连续的Tensor。用户应格外注意,因为连续性可能会对性能产生隐式影响。[transpose()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.transpose.html)是一个常见的例子。\n", "\n", "```python\n", "from mindspore import Tensor\n", "import numpy as np\n", "base = Tensor([[0, 1], [2, 3]])\n", "base.is_contiguous()\n", "# True\n", "t = base.transpose(1, 0) # t is a view of base. No data movement happened here.\n", "t.is_contiguous()\n", "# False\n", "# To get a contiguous Tensor, call `.contiguous()` to enforce\n", "# copying data when `t` is not contiguous.\n", "c = t.contiguous()\n", "c.is_contiguous()\n", "# True\n", "```\n", "\n", "## view类算子\n", "\n", "作为参考,以下是MindSpore中支持view特性算子的完整列表:\n", "\n", "[broadcast_to()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.broadcast_to.html)\n", "\n", "[diagonal()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.diagonal.html)\n", "\n", "[expand_as()](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.expand_as.html)\n", "\n", "[movedim()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.movedim.html)\n", "\n", "[narrow()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.narrow.html)\n", "\n", "[permute()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.permute.html)\n", "\n", "[squeeze()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.squeeze.html)\n", "\n", "[transpose()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.transpose.html)\n", "\n", "[t()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.t.html)\n", "\n", "[T](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.T.html)\n", "\n", "[unsqueeze()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.unsqueeze.html)\n", "\n", "[view()](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.view.html)\n", "\n", "[view_as()](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore/Tensor/mindspore.Tensor.view_as.html)\n", "\n", "[unbind()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.unbind.html)\n", "\n", "[split()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.split.html)\n", "\n", "[hsplit()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.hsplit.html)\n", "\n", "[vsplit()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.vsplit.html)\n", "\n", "[tensor_split()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.tensor_split.html)\n", "\n", "[swapaxes()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.swapaxes.html)\n", "\n", "[swapdims()](https://www.mindspore.cn/docs/zh-CN/master/api_python/ops/mindspore.ops.swapdims.html)" ] } ], "metadata": { "kernelspec": { "display_name": "MindSpore", "language": "python", "name": "mindspore" }, "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.7.5" }, "vscode": { "interpreter": { "hash": "8c9da313289c39257cb28b126d2dadd33153d4da4d524f730c81a4aaccbd2ca7" } } }, "nbformat": 4, "nbformat_minor": 5 }