{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# dtype\n", "\n", "[![](https://gitee.com/mindspore/docs/raw/r1.2/docs/programming_guide/source_zh_cn/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/r1.2/docs/programming_guide/source_zh_cn/dtype.ipynb) [![](https://gitee.com/mindspore/docs/raw/r1.2/docs/programming_guide/source_zh_cn/_static/logo_notebook.png)](https://obs.dualstack.cn-north-4.myhuaweicloud.com/mindspore-website/notebook/r1.2/programming_guide/mindspore_dtype.ipynb) [![](https://gitee.com/mindspore/docs/raw/r1.2/docs/programming_guide/source_zh_cn/_static/logo_modelarts.png)](https://console.huaweicloud.com/modelarts/?region=cn-north-4#/notebook/loading?share-url-b64=aHR0cHM6Ly9vYnMuZHVhbHN0YWNrLmNuLW5vcnRoLTQubXlodWF3ZWljbG91ZC5jb20vbWluZHNwb3JlLXdlYnNpdGUvbm90ZWJvb2svbW9kZWxhcnRzL3Byb2dyYW1taW5nX2d1aWRlL21pbmRzcG9yZV9kdHlwZS5pcHluYg==&image_id=65f636a0-56cf-49df-b941-7d2a07ba8c8c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 概述\n", "\n", "MindSpore张量支持不同的数据类型,包含`int8`、`int16`、`int32`、`int64`、`uint8`、`uint16`、`uint32`、`uint64`、`float16`、`float32`、`float64`、`bool_`,与NumPy的数据类型一一对应。\n", "\n", "在MindSpore的运算处理流程中,Python中的`int`数会被转换为定义的`int64`类型,`float`数会被转换为定义的`float32`类型。\n", "\n", "详细的类型支持情况请参考:。\n", "\n", "以下代码,打印MindSpore的数据类型int32。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Int32\n" ] } ], "source": [ "from mindspore import dtype as mstype\n", "\n", "data_type = mstype.int32\n", "print(data_type)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 数据类型转换接口\n", "\n", "MindSpore提供了以下几个接口,实现与NumPy数据类型和Python内置的数据类型间的转换。\n", "\n", "- `dtype_to_nptype`:将MindSpore的数据类型转换为NumPy对应的数据类型。\n", "\n", "- `dtype_to_pytype`:将MindSpore的数据类型转换为Python对应的内置数据类型。\n", "\n", "- `pytype_to_dtype`:将Python内置的数据类型转换为MindSpore对应的数据类型。\n", "\n", "以下代码实现了不同数据类型间的转换,并打印转换后的类型。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Int64\n", "\n" ] } ], "source": [ "from mindspore import dtype as mstype\n", "\n", "np_type = mstype.dtype_to_nptype(mstype.int32)\n", "ms_type = mstype.pytype_to_dtype(int)\n", "py_type = mstype.dtype_to_pytype(mstype.float64)\n", "\n", "print(np_type)\n", "print(ms_type)\n", "print(py_type)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }