Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d95eaa17
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d95eaa17
编写于
4月 05, 2023
作者:
zhouweiwei2014
提交者:
GitHub
4月 05, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix Tensor.item to np.array(Tensor).item (#52483)
上级
ea8aa432
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
15 addition
and
11 deletion
+15
-11
paddle/fluid/pybind/eager_method.cc
paddle/fluid/pybind/eager_method.cc
+1
-1
python/paddle/fluid/dygraph/math_op_patch.py
python/paddle/fluid/dygraph/math_op_patch.py
+4
-4
python/paddle/fluid/dygraph/varbase_patch_methods.py
python/paddle/fluid/dygraph/varbase_patch_methods.py
+2
-2
python/paddle/nn/decode.py
python/paddle/nn/decode.py
+1
-1
python/paddle/nn/functional/pooling.py
python/paddle/nn/functional/pooling.py
+4
-2
python/paddle/static/nn/metric.py
python/paddle/static/nn/metric.py
+3
-1
未找到文件。
paddle/fluid/pybind/eager_method.cc
浏览文件 @
d95eaa17
...
...
@@ -143,7 +143,7 @@ static PyObject* tensor_method_numpy(TensorObject* self,
"correct and will be "
"removed in future. For Tensor contain only one element, Please "
"modify "
" 'Tensor.numpy()[0]' to '
Tensor.item(
)' as soon as "
" 'Tensor.numpy()[0]' to '
float(Tensor
)' as soon as "
"possible, "
"otherwise 'Tensor.numpy()[0]' will raise error in future."
;
py_rank
=
1
;
...
...
python/paddle/fluid/dygraph/math_op_patch.py
浏览文件 @
d95eaa17
...
...
@@ -139,21 +139,21 @@ def monkey_patch_math_varbase():
),
"only one element variable can be converted to float."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
float
(
var
.
item
()
)
return
float
(
np
.
array
(
var
).
flatten
()[
0
]
)
def
_long_
(
var
):
numel
=
np
.
prod
(
var
.
shape
)
assert
numel
==
1
,
"only one element variable can be converted to long."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
int
(
var
.
item
()
)
return
int
(
np
.
array
(
var
).
flatten
()[
0
]
)
def
_int_
(
var
):
numel
=
np
.
prod
(
var
.
shape
)
assert
numel
==
1
,
"only one element variable can be converted to int."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
int
(
var
.
item
()
)
return
int
(
np
.
array
(
var
).
flatten
()[
0
]
)
def
_len_
(
var
):
assert
var
.
ndim
>
0
,
"len() of a 0D tensor is wrong"
...
...
@@ -171,7 +171,7 @@ def monkey_patch_math_varbase():
),
"only one element variable can be converted to python index."
tensor
=
var
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"variable's tensor is not initialized"
return
int
(
var
.
item
()
)
return
int
(
np
.
array
(
var
).
flatten
()[
0
]
)
@
property
def
_ndim_
(
var
):
...
...
python/paddle/fluid/dygraph/varbase_patch_methods.py
浏览文件 @
d95eaa17
...
...
@@ -718,11 +718,11 @@ def monkey_patch_varbase():
),
"When Variable is used as the condition of if/while , Variable can only contain one element."
if
framework
.
global_var
.
_in_eager_mode_
:
assert
self
.
_is_initialized
(),
"tensor not initialized"
return
bool
(
self
.
item
(
)
>
0
)
return
bool
(
np
.
array
(
self
)
>
0
)
else
:
tensor
=
self
.
value
().
get_tensor
()
assert
tensor
.
_is_initialized
(),
"tensor not initialized"
return
bool
(
self
.
item
(
)
>
0
)
return
bool
(
np
.
array
(
tensor
)
>
0
)
def
__bool__
(
self
):
return
self
.
__nonzero__
()
...
...
python/paddle/nn/decode.py
浏览文件 @
d95eaa17
...
...
@@ -712,7 +712,7 @@ def _dynamic_decode_imperative(
step_idx
=
0
step_idx_tensor
=
paddle
.
full
(
shape
=
[
1
],
fill_value
=
step_idx
,
dtype
=
"int64"
)
while
cond
.
item
():
while
np
.
array
(
cond
)
.
item
():
(
step_outputs
,
next_states
,
next_inputs
,
next_finished
)
=
decoder
.
step
(
step_idx_tensor
,
inputs
,
states
,
**
kwargs
)
...
...
python/paddle/nn/functional/pooling.py
浏览文件 @
d95eaa17
...
...
@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
numpy
as
np
from
paddle
import
_C_ops
,
_legacy_C_ops
,
in_dynamic_mode
from
paddle.fluid.framework
import
Variable
,
in_dygraph_mode
...
...
@@ -706,7 +708,7 @@ def _unpool_output_size(x, kernel_size, stride, padding, output_size):
else
:
for
i
,
var
in
enumerate
(
output_size
):
if
isinstance
(
var
,
Variable
):
output_size
[
i
]
=
var
.
item
()
output_size
[
i
]
=
np
.
array
(
var
)
.
item
()
if
len
(
output_size
)
==
len
(
kernel_size
)
+
2
:
output_size
=
output_size
[
2
:]
...
...
@@ -1609,7 +1611,7 @@ def adaptive_avg_pool2d(x, output_size, data_format='NCHW', name=None):
if
in_dygraph_mode
():
output_size
=
[
item
.
item
(
0
)
if
isinstance
(
item
,
Variable
)
else
item
np
.
array
(
item
)
.
item
(
0
)
if
isinstance
(
item
,
Variable
)
else
item
for
item
in
output_size
]
# output_size support Variable in static graph mode
...
...
python/paddle/static/nn/metric.py
浏览文件 @
d95eaa17
...
...
@@ -14,6 +14,8 @@
"""
All layers just related to metric.
"""
import
numpy
as
np
import
paddle
from
paddle
import
_legacy_C_ops
from
paddle.fluid.data_feeder
import
check_variable_and_dtype
...
...
@@ -76,7 +78,7 @@ def accuracy(input, label, k=1, correct=None, total=None):
if
total
is
None
:
total
=
_varbase_creator
(
dtype
=
"int32"
)
_k
=
k
.
item
(
0
)
if
isinstance
(
k
,
Variable
)
else
k
_k
=
np
.
array
(
k
)
.
item
(
0
)
if
isinstance
(
k
,
Variable
)
else
k
topk_out
,
topk_indices
=
_legacy_C_ops
.
top_k_v2
(
input
,
'k'
,
_k
,
'sorted'
,
False
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录