未验证 提交 567dabeb 编写于 作者: C cyberslack_lee 提交者: GitHub

修改COPY-FROM No.18 (#54842)

上级 3138e7aa
......@@ -152,6 +152,12 @@ def monkey_patch_math_tensor():
def _ndim_(var):
return len(var.shape)
def ndimension(var):
return len(var.shape)
def dim(var):
return len(var.shape)
@property
def _size_(var):
return int(np.prod(var.shape))
......@@ -174,8 +180,8 @@ def monkey_patch_math_tensor():
('__len__', _len_),
('__index__', _index_),
('astype', astype),
('dim', lambda x: len(x.shape)),
('ndimension', lambda x: len(x.shape)),
('dim', dim),
('ndimension', ndimension),
('ndim', _ndim_),
('size', _size_),
('T', _T_),
......
......@@ -1260,6 +1260,7 @@ class Variable(metaclass=VariableMetaClass):
In Static Graph Mode:
.. code-block:: python
:name: code-example-1
import paddle.fluid as fluid
cur_program = fluid.Program()
......@@ -1271,6 +1272,7 @@ class Variable(metaclass=VariableMetaClass):
In Dygraph Mode:
.. code-block:: python
:name: code-example-2
import paddle.fluid as fluid
import numpy as np
......@@ -5743,8 +5745,9 @@ class Program:
use :code:`clone` after :code:`Opimizer.minimize`, but we still
recommend you to use :code:`clone` before using :code:`Opimizer.minimize`.
For Example:
::
Examples:
.. code-block:: python
:name: code-example-1
import paddle
import paddle.static as static
......@@ -5778,6 +5781,7 @@ class Program:
after :code:`clone`:
.. code-block:: python
:name: code-example-2
import paddle
......@@ -5795,6 +5799,7 @@ class Program:
1. To clone a test program, the sample code is:
.. code-block:: python
:name: code-example-3
import paddle
import paddle.static as static
......@@ -5847,6 +5852,7 @@ class Program:
2. The clone method can be avoid if you create program for training and program for testing individually.
.. code-block:: python
:name: code-example-4
import paddle
import paddle.static as static
......@@ -7236,6 +7242,7 @@ def program_guard(main_program, startup_program=None):
Examples:
.. code-block:: python
:name: code-example-1
import paddle
......@@ -7251,6 +7258,7 @@ def program_guard(main_program, startup_program=None):
Examples:
.. code-block:: python
:name: code-example-2
import paddle
......
......@@ -323,6 +323,48 @@ def monkey_patch_variable():
"""
return len(self.shape)
def ndimension(self):
"""
Returns the dimension of current Variable
Returns:
the dimension
Examples:
.. code-block:: python
import paddle
paddle.enable_static()
# create a static Variable
x = paddle.static.data(name='x', shape=[3, 2, 1])
# print the dimension of the Variable
print(x.ndimension)
"""
return len(self.shape)
def dim(self):
"""
Returns the dimension of current Variable
Returns:
the dimension
Examples:
.. code-block:: python
import paddle
paddle.enable_static()
# create a static Variable
x = paddle.static.data(name='x', shape=[3, 2, 1])
# print the dimension of the Variable
print(x.dim)
"""
return len(self.shape)
def _scalar_add_(var, value):
return _scalar_op_(var, 1.0, value)
......@@ -509,8 +551,8 @@ def monkey_patch_variable():
('append', append),
('item', _item),
('pop', pop),
('dim', lambda x: len(x.shape)),
('ndimension', lambda x: len(x.shape)),
('dim', dim),
('ndimension', ndimension),
('ndim', _ndim_),
(
'__add__',
......
......@@ -896,9 +896,11 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None):
3. If it is in static graph mode, any tensors or operations created outside
or inside of ``true_fn`` and ``false_fn`` will be in net building
regardless of which branch is selected at runtime. This has frequently
surprised users who expected a lazy semantics. For example:
surprised users who expected a lazy semantics.
Examples:
.. code-block:: python
:name: code-example-1
import paddle
......@@ -933,6 +935,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None):
Examples:
.. code-block:: python
:name: code-example-2
import paddle
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册