Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
2133f3dd
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2133f3dd
编写于
9月 08, 2021
作者:
zhouweiwei2014
提交者:
GitHub
9月 08, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add API Tensor.T for reverse dim of Tensor (#35379)
上级
12155358
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
88 addition
and
8 deletion
+88
-8
python/paddle/fluid/dygraph/math_op_patch.py
python/paddle/fluid/dygraph/math_op_patch.py
+11
-0
python/paddle/fluid/dygraph/varbase_patch_methods.py
python/paddle/fluid/dygraph/varbase_patch_methods.py
+6
-6
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+49
-0
python/paddle/fluid/tests/unittests/test_math_op_patch.py
python/paddle/fluid/tests/unittests/test_math_op_patch.py
+14
-0
python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py
...ddle/fluid/tests/unittests/test_math_op_patch_var_base.py
+6
-0
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+2
-2
未找到文件。
python/paddle/fluid/dygraph/math_op_patch.py
浏览文件 @
2133f3dd
...
...
@@ -148,6 +148,16 @@ def monkey_patch_math_varbase():
def
_size_
(
var
):
return
np
.
prod
(
var
.
shape
)
@
property
def
_T_
(
var
):
if
len
(
var
.
shape
)
==
1
:
return
var
perm
=
[]
for
i
in
range
(
len
(
var
.
shape
)):
perm
.
insert
(
0
,
i
)
out
,
_
=
_C_ops
.
transpose2
(
var
,
'axis'
,
perm
)
return
out
def
_scalar_add_
(
var
,
value
):
return
_scalar_elementwise_op_
(
var
,
1.0
,
value
)
...
...
@@ -271,6 +281,7 @@ def monkey_patch_math_varbase():
(
'ndimension'
,
lambda
x
:
len
(
x
.
shape
)),
(
'ndim'
,
_ndim_
),
(
'size'
,
_size_
),
(
'T'
,
_T_
),
(
'__add__'
,
_binary_creator_
(
'__add__'
,
'elementwise_add'
,
False
,
_scalar_add_
)),
## a+b == b+a. Do not need to reverse explicitly
...
...
python/paddle/fluid/dygraph/varbase_patch_methods.py
浏览文件 @
2133f3dd
...
...
@@ -88,17 +88,17 @@ def monkey_patch_varbase():
"""
# Note: getattr(self, attr, None) will call x.grad=x.gradient(), but gradient() only available in dygraph.
# It will fail. So, for propery
in dygraph only, should not let i
t getattr(self, attr, None).
attr_not_need_keys
=
[
'grad'
]
# It will fail. So, for propery
that different between dynamic and static graph, should no
t getattr(self, attr, None).
attr_not_need_keys
=
[
'grad'
,
'T'
]
if
isinstance
(
self
,
ParamBase
):
attr_kwargs
=
self
.
__dict__
.
copy
()
else
:
attr_names
=
[]
for
name
in
dir
(
self
):
if
name
not
in
attr_not_need_keys
and
not
(
inspect
.
ismethod
(
getattr
(
self
,
name
))
or
name
.
startswith
(
'_'
)
):
attr_names
.
append
(
name
)
if
name
not
in
attr_not_need_keys
:
if
not
inspect
.
ismethod
(
getattr
(
self
,
name
))
and
not
name
.
startswith
(
'_'
):
attr_names
.
append
(
name
)
attr_kwargs
=
{
name
:
getattr
(
self
,
name
)
for
name
in
attr_names
}
attr_keys
=
[
'block'
,
'shape'
,
'dtype'
,
'type'
,
'name'
,
'persistable'
]
...
...
python/paddle/fluid/framework.py
浏览文件 @
2133f3dd
...
...
@@ -1503,6 +1503,55 @@ class Variable(object):
"""
return
self
.
desc
.
type
()
@
property
def
T
(
self
):
"""
Permute current Variable with its dimensions reversed.
If `n` is the dimensions of `x` , `x.T` is equivalent to `x.transpose([n-1, n-2, ..., 0])`.
Examples:
.. code-block:: python
import paddle
paddle.enable_static()
x = paddle.ones(shape=[2, 3, 5])
x_T = x.T
exe = paddle.static.Executor()
x_T_np = exe.run(paddle.static.default_main_program(), fetch_list=[x_T])[0]
print(x_T_np.shape)
# (5, 3, 2)
"""
if
len
(
self
.
shape
)
==
1
:
return
self
perm
=
[]
for
i
in
range
(
len
(
self
.
shape
)):
perm
.
insert
(
0
,
i
)
out
=
self
.
block
.
create_var
(
name
=
unique_name
.
generate_with_ignorable_key
(
self
.
name
+
'.tmp'
),
dtype
=
self
.
dtype
,
type
=
self
.
type
,
persistable
=
False
,
stop_gradient
=
False
)
input_shape
=
self
.
block
.
create_var
(
name
=
unique_name
.
generate_with_ignorable_key
(
self
.
name
+
'.tmp'
),
dtype
=
self
.
dtype
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
persistable
=
False
,
stop_gradient
=
False
)
self
.
block
.
append_op
(
type
=
'transpose2'
,
inputs
=
{
'X'
:
[
self
]},
outputs
=
{
'Out'
:
[
out
],
'XShape'
:
[
input_shape
]},
attrs
=
{
'axis'
:
perm
})
return
out
def
clone
(
self
):
"""
Returns a new static Variable, which is the clone of the original static
...
...
python/paddle/fluid/tests/unittests/test_math_op_patch.py
浏览文件 @
2133f3dd
...
...
@@ -335,6 +335,20 @@ class TestMathOpPatches(unittest.TestCase):
fetch_list
=
[
z
])
self
.
assertTrue
(
np
.
array_equal
(
out
[
0
],
out_np
))
@
prog_scope
()
def
test_T
(
self
):
x_np
=
np
.
random
.
randint
(
-
100
,
100
,
[
2
,
8
,
5
,
3
]).
astype
(
"int32"
)
out_np
=
x_np
.
T
x
=
paddle
.
static
.
data
(
name
=
"x"
,
shape
=
[
2
,
8
,
5
,
3
],
dtype
=
"int32"
)
z
=
x
.
T
exe
=
fluid
.
Executor
()
out
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"x"
:
x_np
},
fetch_list
=
[
z
])
self
.
assertTrue
(
np
.
array_equal
(
out
[
0
],
out_np
))
@
prog_scope
()
def
test_ndim
(
self
):
a
=
paddle
.
static
.
data
(
name
=
"a"
,
shape
=
[
10
,
1
])
...
...
python/paddle/fluid/tests/unittests/test_math_op_patch_var_base.py
浏览文件 @
2133f3dd
...
...
@@ -527,6 +527,12 @@ class TestMathOpPatchesVarBase(unittest.TestCase):
np
.
array_equal
(
x
.
where
(
a
,
b
).
numpy
(),
paddle
.
where
(
x
,
a
,
b
).
numpy
()))
x_np
=
np
.
random
.
randn
(
3
,
6
,
9
,
7
)
x
=
paddle
.
to_tensor
(
x_np
)
x_T
=
x
.
T
self
.
assertTrue
(
x_T
.
shape
,
[
7
,
9
,
6
,
3
])
self
.
assertTrue
(
np
.
array_equal
(
x_T
.
numpy
(),
x_np
.
T
))
self
.
assertTrue
(
inspect
.
ismethod
(
a
.
dot
))
self
.
assertTrue
(
inspect
.
ismethod
(
a
.
logsumexp
))
self
.
assertTrue
(
inspect
.
ismethod
(
a
.
multiplex
))
...
...
python/paddle/tensor/__init__.py
浏览文件 @
2133f3dd
...
...
@@ -367,8 +367,8 @@ tensor_method_func = [ #noqa
'real'
,
'imag'
,
'digamma'
,
'diagonal'
'trunc'
'diagonal'
,
'trunc'
,
'bitwise_and'
,
'bitwise_or'
,
'bitwise_xor'
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录