diff --git a/tutorials/source_en/beginner/tensor.md b/tutorials/source_en/beginner/tensor.md index 351da52ef6008be9492809279806398d4976c1a2..746cbe7228e072ebca22499116ad1e50d0b01509 100644 --- a/tutorials/source_en/beginner/tensor.md +++ b/tutorials/source_en/beginner/tensor.md @@ -11,7 +11,6 @@ A tensor is a special data structure that is similar to arrays and matrices. [Te ```python import numpy as np import mindspore -from mindspore import ops from mindspore import Tensor ``` @@ -25,7 +24,7 @@ There are multiple methods for creating tensors. When building a tensor, you can ```python data = [1, 0, 1, 0] - x_data = Tensor(data) + x_data = mindspore.tensor(data) print(x_data, x_data.shape, x_data.dtype) ``` @@ -39,12 +38,12 @@ There are multiple methods for creating tensors. When building a tensor, you can ```python np_array = np.array(data) - x_np = Tensor(np_array) - print(x_np, x_np.shape, x_np.dtype) + x_from_np = mindspore.tensor(np_array) + print(x_from_np, x_from_np.shape) ``` ```text - [1 0 1 0] (4,) Int64 + [1 0 1 0] (4,) ``` - **Generating a tensor by using init** @@ -59,9 +58,9 @@ There are multiple methods for creating tensors. When building a tensor, you can from mindspore.common.initializer import One, Normal # Initialize a tensor with ones - tensor1 = mindspore.Tensor(shape=(2, 2), dtype=mindspore.float32, init=One()) + tensor1 = mindspore.tensor(shape=(2, 2), dtype=mindspore.float32, init=One()) # Initialize a tensor from normal distribution - tensor2 = mindspore.Tensor(shape=(2, 2), dtype=mindspore.float32, init=Normal()) + tensor2 = mindspore.tensor(shape=(2, 2), dtype=mindspore.float32, init=Normal()) print("tensor1:\n", tensor1) print("tensor2:\n", tensor2) @@ -72,8 +71,8 @@ There are multiple methods for creating tensors. When building a tensor, you can [[1. 1.] [1. 1.]] tensor2: - [[-0.00063482 -0.00916224] - [ 0.01324238 -0.0171206 ]] + [[-0.0107513 0.00407822] + [-0.00113699 0.00081491]] ``` The `init` is used for delayed initialization in parallel mode. Usually, it is not recommended to use `init` interface to initialize parameters. @@ -117,7 +116,7 @@ Tensor attributes include shape, data type, transposed tensor, item size, number - strides: the number of bytes to traverse in each dimension of `Tensor`, which is a tuple. ```python -x = Tensor(np.array([[1, 2], [3, 4]]), mindspore.int32) +x = mindspore.tensor(np.array([[1, 2], [3, 4]]), mindspore.int32) print("x_shape:", x.shape) print("x_dtype:", x.dtype) @@ -143,7 +142,7 @@ x_strides: (8, 4) Tensor indexing is similar to NumPy indexing. Indexing starts from 0, negative indexing means indexing in reverse order, and colons `:` and `...` are used for slicing. ```python -tensor = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) +tensor = mindspore.tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) print("First row: {}".format(tensor[0])) print("value of bottom right corner: {}".format(tensor[1, 1])) @@ -165,8 +164,8 @@ There are many operations between tensors, including arithmetic, linear algebra, > Common arithmetic operations include: addition (+), subtraction (-), multiplication (\*), division (/), modulo (%), and exact division (//). ```python -x = Tensor(np.array([1, 2, 3]), mindspore.float32) -y = Tensor(np.array([4, 5, 6]), mindspore.float32) +x = mindspore.tensor(np.array([1, 2, 3]), mindspore.float32) +y = mindspore.tensor(np.array([4, 5, 6]), mindspore.float32) output_add = x + y output_sub = x - y @@ -195,8 +194,8 @@ floordiv: [4. 2. 2.] [concat](https://www.mindspore.cn/docs/en/r2.7.0/api_python/ops/mindspore.ops.concat.html) connects a series of tensors in a given dimension. ```python -data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) -data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32)) +data1 = mindspore.tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) +data2 = mindspore.tensor(np.array([[4, 5], [6, 7]]).astype(np.float32)) output = ops.concat((data1, data2), axis=0) print(output) @@ -215,8 +214,8 @@ shape: [stack](https://www.mindspore.cn/docs/en/r2.7.0/api_python/ops/mindspore.ops.stack.html) combines two tensors from another dimension. ```python -data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) -data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32)) +data1 = mindspore.tensor(np.array([[0, 1], [2, 3]]).astype(np.float32)) +data2 = mindspore.tensor(np.array([[4, 5], [6, 7]]).astype(np.float32)) output = ops.stack([data1, data2]) print(output) @@ -242,7 +241,7 @@ Tensor and NumPy can be converted to each other. Use [Tensor.asnumpy()](https://www.mindspore.cn/docs/en/r2.7.0/api_python/mindspore/Tensor/mindspore.Tensor.asnumpy.html) to convert Tensor to NumPy, which is same as tensor building. ```python -t = Tensor([1., 1., 1., 1., 1.]) +t = mindspore.tensor([1., 1., 1., 1., 1.]) print(f"t: {t}", type(t)) n = t.asnumpy() print(f"n: {n}", type(n)) @@ -255,7 +254,7 @@ n: [1. 1. 1. 1. 1.] ### NumPy to Tensor -Use `Tensor()` to convert NumPy to Tensor. +Use [Tensor.from_numpy()](https://www.mindspore.cn/docs/en/r2.7.0/api_python/mindspore/Tensor/mindspore.Tensor.asnumpy.html) to convert NumPy to Tensor, which operates via memory sharing (zero-copy) for better performance, with the constraint that input NumPy arrays must be memory-contiguous (verifiable with numpy.iscontiguous()). ```python n = np.ones(5) @@ -271,4 +270,22 @@ print(f"t: {t}", type(t)) ```text n: [2. 2. 2. 2. 2.] t: [2. 2. 2. 2. 2.] +``` + +Use [mindspore.tensor()](https://www.mindspore.cn/docs/en/r2.7.0/api_python/mindspore/mindspore.tensor.html) for direct creation results in data copying. + +```python +n = np.ones(5) +t = mindspore.tensor(n) +``` + +```python +np.add(n, 1, out=n) +print(f"n: {n}", type(n)) +print(f"t: {t}", type(t)) +``` + +```text +n: [2. 2. 2. 2. 2.] +t: [1. 1. 1. 1. 1.] ``` \ No newline at end of file diff --git a/tutorials/source_zh_cn/beginner/tensor.ipynb b/tutorials/source_zh_cn/beginner/tensor.ipynb index f617e004ea0bbf7c217626f7909597c02291ddd9..0930fcda68939eee745761ccedecb11efac8bbcb 100644 --- a/tutorials/source_zh_cn/beginner/tensor.ipynb +++ b/tutorials/source_zh_cn/beginner/tensor.ipynb @@ -31,7 +31,6 @@ "source": [ "import numpy as np\n", "import mindspore\n", - "from mindspore import ops\n", "from mindspore import Tensor" ] }, @@ -63,7 +62,7 @@ ], "source": [ "data = [1, 0, 1, 0]\n", - "x_data = Tensor(data)\n", + "x_data = mindspore.tensor(data)\n", "print(x_data, x_data.shape, x_data.dtype)" ] }, @@ -78,21 +77,21 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[1 0 1 0] (4,) Int64\n" + "[1 0 1 0] (4,)\n" ] } ], "source": [ "np_array = np.array(data)\n", - "x_np = Tensor(np_array)\n", - "print(x_np, x_np.shape, x_np.dtype)" + "x_from_np = mindspore.tensor(np_array)\n", + "print(x_from_np, x_from_np.shape)" ] }, { @@ -123,8 +122,8 @@ " [[1. 1.]\n", " [1. 1.]]\n", "tensor2:\n", - " [[-0.00063482 -0.00916224]\n", - " [ 0.01324238 -0.0171206 ]]\n" + " [[-0.0107513 0.00407822]\n", + " [-0.00113699 0.00081491]]\n" ] } ], @@ -220,7 +219,7 @@ } ], "source": [ - "x = Tensor(np.array([[1, 2], [3, 4]]), mindspore.int32)\n", + "x = mindspore.tensor(np.array([[1, 2], [3, 4]]), mindspore.int32)\n", "\n", "print(\"x_shape:\", x.shape)\n", "print(\"x_dtype:\", x.dtype)\n", @@ -257,7 +256,7 @@ } ], "source": [ - "tensor = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", + "tensor = mindspore.tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", "\n", "print(\"First row: {}\".format(tensor[0]))\n", "print(\"value of bottom right corner: {}\".format(tensor[1, 1]))\n", @@ -295,8 +294,8 @@ } ], "source": [ - "x = Tensor(np.array([1, 2, 3]), mindspore.float32)\n", - "y = Tensor(np.array([4, 5, 6]), mindspore.float32)\n", + "x = mindspore.tensor(np.array([1, 2, 3]), mindspore.float32)\n", + "y = mindspore.tensor(np.array([4, 5, 6]), mindspore.float32)\n", "\n", "output_add = x + y\n", "output_sub = x - y\n", @@ -339,8 +338,8 @@ } ], "source": [ - "data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", - "data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32))\n", + "data1 = mindspore.tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", + "data2 = mindspore.tensor(np.array([[4, 5], [6, 7]]).astype(np.float32))\n", "output = ops.concat((data1, data2), axis=0)\n", "\n", "print(output)\n", @@ -374,8 +373,8 @@ } ], "source": [ - "data1 = Tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", - "data2 = Tensor(np.array([[4, 5], [6, 7]]).astype(np.float32))\n", + "data1 = mindspore.tensor(np.array([[0, 1], [2, 3]]).astype(np.float32))\n", + "data2 = mindspore.tensor(np.array([[4, 5], [6, 7]]).astype(np.float32))\n", "output = ops.stack([data1, data2])\n", "\n", "print(output)\n", @@ -392,7 +391,7 @@ "\n", "### Tensor转换为NumPy\n", "\n", - "可以使用 [Tensor.asnumpy()](https://www.mindspore.cn/docs/zh-CN/r2.7.0/api_python/mindspore/Tensor/mindspore.Tensor.asnumpy.html) 将Tensor变量转换为NumPy变量。" + "可以使用 [Tensor.asnumpy()](https://www.mindspore.cn/docs/zh-CN/r2.7.0/api_python/mindspore/Tensor/mindspore.Tensor.asnumpy.html) 将MindSpore的Tensor转换为NumPy数据。" ] }, { @@ -410,7 +409,7 @@ } ], "source": [ - "t = Tensor([1., 1., 1., 1., 1.])\n", + "t = mindspore.tensor([1., 1., 1., 1., 1.])\n", "print(f\"t: {t}\", type(t))\n", "n = t.asnumpy()\n", "print(f\"n: {n}\", type(n))" @@ -422,7 +421,7 @@ "source": [ "### NumPy转换为Tensor\n", "\n", - "使用`Tensor()`将NumPy变量转换为Tensor变量。" + "使用 [Tensor.from_numpy()](https://www.mindspore.cn/docs/zh-CN/r2.7.0/api_python/mindspore/Tensor/mindspore.Tensor.asnumpy.html) 将NumPy数据转换为MindSpore的Tensor。此方法不拷贝数据,共用数据存储地址,速度较快,但NumPy数据需为连续(用numpy.iscontiguous()判断)。" ] }, { @@ -454,13 +453,50 @@ "print(f\"n: {n}\", type(n))\n", "print(f\"t: {t}\", type(t))" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "使用 [mindspore.tensor](https://www.mindspore.cn/docs/zh-CN/r2.7.0/api_python/mindspore/mindspore.tensor.html) 直接创建,此方法会对数据进行拷贝。" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "n = np.ones(5)\n", + "t = mindspore.tensor(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "n: [2. 2. 2. 2. 2.] \n", + "t: [1. 1. 1. 1. 1.] \n" + ] + } + ], + "source": [ + "np.add(n, 1, out=n)\n", + "print(f\"n: {n}\", type(n))\n", + "print(f\"t: {t}\", type(t))" + ] } ], "metadata": { "kernelspec": { - "display_name": "MindSpore", + "display_name": "ms25-kernel", "language": "python", - "name": "mindspore" + "name": "ms25-kernel" }, "language_info": { "codemirror_mode": { @@ -472,7 +508,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.5" + "version": "3.9.21" } }, "nbformat": 4,