未验证 提交 0b57f766 编写于 作者: J Jiabin Yang 提交者: GitHub

Fix variable cn doc (#1485)

* add astype api in Variable and refine code style

* add astype api in Variable and refine code style

* add astype api in Variable and refine code style

* refine doc
上级 2afcfd42
...@@ -22,24 +22,22 @@ Variable ...@@ -22,24 +22,22 @@ Variable
**示例代码:** **示例代码:**
在静态图形模式下: 在静态图形模式下:
.. code-block:: python
.. code-block:: python import paddle.fluid as fluid
cur_program = fluid.Program()
import paddle.fluid as fluid cur_block = cur_program.current_block()
cur_program = fluid.Program() new_variable = cur_block.create_var(name="X",
cur_block = cur_program.current_block() shape=[-1, 23, 48],
new_variable = cur_block.create_var(name="X", dtype='float32')
shape=[-1, 23, 48], 在 `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ 模式下:
dtype='float32') .. code-block:: python
在 `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ 模式下:
.. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy as np import numpy as np
with fluid.dygraph.guard(): with fluid.dygraph.guard():
new_variable = fluid.dygraph.to_variable(np.arange(10)) new_variable = fluid.dygraph.to_variable(np.arange(10))
.. py:method:: detach() .. py:method:: detach()
...@@ -245,7 +243,7 @@ Variable ...@@ -245,7 +243,7 @@ Variable
**注意:** **注意:**
**1. 该API参数只在非** `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ **模式下生效** **1. 该API只在非** `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ **模式下生效**
获取该 :ref:`api_guide_Variable` 的静态描述字符串 获取该 :ref:`api_guide_Variable` 的静态描述字符串
...@@ -279,6 +277,50 @@ Variable ...@@ -279,6 +277,50 @@ Variable
print(new_variable.to_string(True, True)) print(new_variable.to_string(True, True))
.. py:method:: astype(self, dtype)
将该 :ref:`api_guide_Variable` 中的数据转换成目标 ``Dtype``
**参数:**
- **self** ( :ref:`api_guide_Variable` ) - 当前 :ref:`api_guide_Variable` , 用户不需要传入。
- **dtype** (int | float | float64) - 希望转换成的 ``Dtype``
返回:一个全新的转换了 ``Dtype`` 的 :ref:`api_guide_Variable`
返回类型: :ref:`api_guide_Variable`
**示例代码**
在静态图模式下:
.. code-block:: python
import paddle.fluid as fluid
startup_prog = fluid.Program()
main_prog = fluid.Program()
with fluid.program_guard(startup_prog, main_prog):
original_variable = fluid.data(name = "new_variable", shape=[2,2], dtype='float32')
new_variable = original_variable.astype('int64')
print("new var's dtype is: {}".format(new_variable.dtype))
在 `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ 模式下:
.. code-block:: python
import paddle.fluid as fluid
import numpy as np
x = np.ones([2, 2], np.float32)
with fluid.dygraph.guard():
original_variable = fluid.dygraph.to_variable(x)
print("original var's dtype is: {}, numpy dtype is {}".format(original_variable.dtype, original_variable.numpy().dtype))
new_variable = original_variable.astype('int64')
print("new var's dtype is: {}, numpy dtype is {}".format(new_variable.dtype, new_variable.numpy().dtype))
属性 属性
:::::::::::: ::::::::::::
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册