Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
567dabeb
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
567dabeb
编写于
7月 05, 2023
作者:
C
cyberslack_lee
提交者:
GitHub
7月 05, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改COPY-FROM No.18 (#54842)
上级
3138e7aa
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
97 addition
and
38 deletion
+97
-38
python/paddle/fluid/dygraph/math_op_patch.py
python/paddle/fluid/dygraph/math_op_patch.py
+8
-2
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+35
-27
python/paddle/fluid/layers/math_op_patch.py
python/paddle/fluid/layers/math_op_patch.py
+44
-2
python/paddle/static/nn/control_flow.py
python/paddle/static/nn/control_flow.py
+10
-7
未找到文件。
python/paddle/fluid/dygraph/math_op_patch.py
浏览文件 @
567dabeb
...
@@ -152,6 +152,12 @@ def monkey_patch_math_tensor():
...
@@ -152,6 +152,12 @@ def monkey_patch_math_tensor():
def
_ndim_
(
var
):
def
_ndim_
(
var
):
return
len
(
var
.
shape
)
return
len
(
var
.
shape
)
def
ndimension
(
var
):
return
len
(
var
.
shape
)
def
dim
(
var
):
return
len
(
var
.
shape
)
@
property
@
property
def
_size_
(
var
):
def
_size_
(
var
):
return
int
(
np
.
prod
(
var
.
shape
))
return
int
(
np
.
prod
(
var
.
shape
))
...
@@ -174,8 +180,8 @@ def monkey_patch_math_tensor():
...
@@ -174,8 +180,8 @@ def monkey_patch_math_tensor():
(
'__len__'
,
_len_
),
(
'__len__'
,
_len_
),
(
'__index__'
,
_index_
),
(
'__index__'
,
_index_
),
(
'astype'
,
astype
),
(
'astype'
,
astype
),
(
'dim'
,
lambda
x
:
len
(
x
.
shape
)
),
(
'dim'
,
dim
),
(
'ndimension'
,
lambda
x
:
len
(
x
.
shape
)
),
(
'ndimension'
,
ndimension
),
(
'ndim'
,
_ndim_
),
(
'ndim'
,
_ndim_
),
(
'size'
,
_size_
),
(
'size'
,
_size_
),
(
'T'
,
_T_
),
(
'T'
,
_T_
),
...
...
python/paddle/fluid/framework.py
浏览文件 @
567dabeb
...
@@ -1260,6 +1260,7 @@ class Variable(metaclass=VariableMetaClass):
...
@@ -1260,6 +1260,7 @@ class Variable(metaclass=VariableMetaClass):
In Static Graph Mode:
In Static Graph Mode:
.. code-block:: python
.. code-block:: python
:name: code-example-1
import paddle.fluid as fluid
import paddle.fluid as fluid
cur_program = fluid.Program()
cur_program = fluid.Program()
...
@@ -1271,6 +1272,7 @@ class Variable(metaclass=VariableMetaClass):
...
@@ -1271,6 +1272,7 @@ class Variable(metaclass=VariableMetaClass):
In Dygraph Mode:
In Dygraph Mode:
.. code-block:: python
.. code-block:: python
:name: code-example-2
import paddle.fluid as fluid
import paddle.fluid as fluid
import numpy as np
import numpy as np
...
@@ -5743,21 +5745,22 @@ class Program:
...
@@ -5743,21 +5745,22 @@ class Program:
use :code:`clone` after :code:`Opimizer.minimize`, but we still
use :code:`clone` after :code:`Opimizer.minimize`, but we still
recommend you to use :code:`clone` before using :code:`Opimizer.minimize`.
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
import paddle.static as static
import paddle.static as static
paddle.enable_static()
paddle.enable_static()
img = static.data(name='image', shape=[None, 784])
img = static.data(name='image', shape=[None, 784])
pred = static.nn.fc(x=img, size=10, actvation='relu')
pred = static.nn.fc(x=img, size=10, actvation='relu')
loss = paddle.mean(pred)
loss = paddle.mean(pred)
# Here we use clone before Momentum
# Here we use clone before Momentum
test_program = static.default_main_program().clone(for_test=True)
test_program = static.default_main_program().clone(for_test=True)
optimizer = paddle.optimizer.Momentum(learning_rate=0.01, momentum=0.9)
optimizer = paddle.optimizer.Momentum(learning_rate=0.01, momentum=0.9)
optimizer.minimize(loss)
optimizer.minimize(loss)
Args:
Args:
...
@@ -5778,6 +5781,7 @@ class Program:
...
@@ -5778,6 +5781,7 @@ class Program:
after :code:`clone`:
after :code:`clone`:
.. code-block:: python
.. code-block:: python
:name: code-example-2
import paddle
import paddle
...
@@ -5795,6 +5799,7 @@ class Program:
...
@@ -5795,6 +5799,7 @@ class Program:
1. To clone a test program, the sample code is:
1. To clone a test program, the sample code is:
.. code-block:: python
.. code-block:: python
:name: code-example-3
import paddle
import paddle
import paddle.static as static
import paddle.static as static
...
@@ -5847,6 +5852,7 @@ class Program:
...
@@ -5847,6 +5852,7 @@ class Program:
2. The clone method can be avoid if you create program for training and program for testing individually.
2. The clone method can be avoid if you create program for training and program for testing individually.
.. code-block:: python
.. code-block:: python
:name: code-example-4
import paddle
import paddle
import paddle.static as static
import paddle.static as static
...
@@ -7235,30 +7241,32 @@ def program_guard(main_program, startup_program=None):
...
@@ -7235,30 +7241,32 @@ def program_guard(main_program, startup_program=None):
Default: None.
Default: None.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
:name: code-example-1
import paddle
import paddle
paddle.enable_static()
paddle.enable_static()
main_program = paddle.static.Program()
main_program = paddle.static.Program()
startup_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(main_program, startup_program):
with paddle.static.program_guard(main_program, startup_program):
data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')
data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')
hidden = paddle.static.nn.fc(x=data, size=10, activation='relu')
hidden = paddle.static.nn.fc(x=data, size=10, activation='relu')
Notes: The temporary :code:`Program` can be used if the user does not need
Notes: The temporary :code:`Program` can be used if the user does not need
to construct either of startup program or main program.
to construct either of startup program or main program.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
:name: code-example-2
import paddle
import paddle
paddle.enable_static()
paddle.enable_static()
main_program = paddle.static.Program()
main_program = paddle.static.Program()
# does not care about startup program. Just pass a temporary value.
# does not care about startup program. Just pass a temporary value.
with paddle.static.program_guard(main_program, paddle.static.Program()):
with paddle.static.program_guard(main_program, paddle.static.Program()):
data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')
data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')
"""
"""
from
.data_feeder
import
check_type
from
.data_feeder
import
check_type
...
...
python/paddle/fluid/layers/math_op_patch.py
浏览文件 @
567dabeb
...
@@ -323,6 +323,48 @@ def monkey_patch_variable():
...
@@ -323,6 +323,48 @@ def monkey_patch_variable():
"""
"""
return
len
(
self
.
shape
)
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
):
def
_scalar_add_
(
var
,
value
):
return
_scalar_op_
(
var
,
1.0
,
value
)
return
_scalar_op_
(
var
,
1.0
,
value
)
...
@@ -509,8 +551,8 @@ def monkey_patch_variable():
...
@@ -509,8 +551,8 @@ def monkey_patch_variable():
(
'append'
,
append
),
(
'append'
,
append
),
(
'item'
,
_item
),
(
'item'
,
_item
),
(
'pop'
,
pop
),
(
'pop'
,
pop
),
(
'dim'
,
lambda
x
:
len
(
x
.
shape
)
),
(
'dim'
,
dim
),
(
'ndimension'
,
lambda
x
:
len
(
x
.
shape
)
),
(
'ndimension'
,
ndimension
),
(
'ndim'
,
_ndim_
),
(
'ndim'
,
_ndim_
),
(
(
'__add__'
,
'__add__'
,
...
...
python/paddle/static/nn/control_flow.py
浏览文件 @
567dabeb
...
@@ -896,16 +896,18 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None):
...
@@ -896,16 +896,18 @@ 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
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
or inside of ``true_fn`` and ``false_fn`` will be in net building
regardless of which branch is selected at runtime. This has frequently
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.
.. code-block:: python
Examples:
.. code-block:: python
:name: code-example-1
import paddle
import paddle
a = paddle.zeros((1, 1))
a = paddle.zeros((1, 1))
b = paddle.zeros((1, 1))
b = paddle.zeros((1, 1))
c = a * b
c = a * b
out = paddle.static.nn.cond(a < b, lambda: a + c, lambda: b * b)
out = paddle.static.nn.cond(a < b, lambda: a + c, lambda: b * b)
No matter whether ``a < b`` , ``c = a * b`` will be in net building and
No matter whether ``a < b`` , ``c = a * b`` will be in net building and
run. ``a + c`` and ``b * b`` will be in net building, but only one
run. ``a + c`` and ``b * b`` will be in net building, but only one
...
@@ -933,6 +935,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None):
...
@@ -933,6 +935,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None):
Examples:
Examples:
.. code-block:: python
.. code-block:: python
:name: code-example-2
import paddle
import paddle
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录