Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
9a0bfece
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
9a0bfece
编写于
4月 25, 2022
作者:
F
Feiyu Chan
提交者:
GitHub
4月 25, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove redundant computation in Categorical.probs (#42114)
上级
6553a9d7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
16 addition
and
35 deletion
+16
-35
python/paddle/distribution/categorical.py
python/paddle/distribution/categorical.py
+16
-35
未找到文件。
python/paddle/distribution/categorical.py
浏览文件 @
9a0bfece
...
@@ -115,6 +115,8 @@ class Categorical(distribution.Distribution):
...
@@ -115,6 +115,8 @@ class Categorical(distribution.Distribution):
self
.
logits
=
self
.
_to_tensor
(
logits
)[
0
]
self
.
logits
=
self
.
_to_tensor
(
logits
)[
0
]
if
self
.
dtype
!=
convert_dtype
(
self
.
logits
.
dtype
):
if
self
.
dtype
!=
convert_dtype
(
self
.
logits
.
dtype
):
self
.
logits
=
tensor
.
cast
(
self
.
logits
,
dtype
=
self
.
dtype
)
self
.
logits
=
tensor
.
cast
(
self
.
logits
,
dtype
=
self
.
dtype
)
dist_sum
=
paddle
.
sum
(
self
.
logits
,
axis
=-
1
,
keepdim
=
True
)
self
.
_prob
=
self
.
logits
/
dist_sum
def
sample
(
self
,
shape
):
def
sample
(
self
,
shape
):
"""Generate samples of the specified shape.
"""Generate samples of the specified shape.
...
@@ -297,42 +299,21 @@ class Categorical(distribution.Distribution):
...
@@ -297,42 +299,21 @@ class Categorical(distribution.Distribution):
"""
"""
name
=
self
.
name
+
'_probs'
name
=
self
.
name
+
'_probs'
if
len
(
self
.
_prob
.
shape
)
==
1
:
# batch_shape is empty
dist_sum
=
paddle
.
sum
(
self
.
logits
,
axis
=-
1
,
keepdim
=
True
)
return
paddle
.
gather
(
prob
=
self
.
logits
/
dist_sum
self
.
_prob
,
value
.
reshape
(
[
-
1
],
name
=
name
),
name
=
name
).
reshape
(
shape
=
list
(
prob
.
shape
)
value
.
shape
,
name
=
name
)
value_shape
=
list
(
value
.
shape
)
if
len
(
shape
)
==
1
:
num_value_in_one_dist
=
np
.
prod
(
value_shape
)
index_value
=
paddle
.
reshape
(
value
,
[
num_value_in_one_dist
,
1
])
index
=
index_value
else
:
else
:
num_dist
=
np
.
prod
(
shape
[:
-
1
])
if
len
(
value
.
shape
)
==
1
:
num_value_in_one_dist
=
value_shape
[
-
1
]
return
paddle
.
take_along_axis
(
prob
=
paddle
.
reshape
(
prob
,
[
num_dist
,
shape
[
-
1
]])
self
.
_prob
,
if
len
(
value_shape
)
==
1
:
paddle
.
reshape
(
value
=
nn
.
expand
(
value
,
[
num_dist
])
value
,
(
len
(
self
.
_prob
.
shape
)
-
1
)
*
[
1
]
+
[
-
1
],
value_shape
=
shape
[:
-
1
]
+
value_shape
name
=
name
),
index_value
=
paddle
.
reshape
(
value
,
[
num_dist
,
-
1
,
1
])
axis
=-
1
)
if
shape
[:
-
1
]
!=
value_shape
[:
-
1
]:
else
:
raise
ValueError
(
return
paddle
.
take_along_axis
(
self
.
_prob
,
value
,
axis
=-
1
)
"shape of value {} must match shape of logits {}"
.
format
(
str
(
value_shape
[:
-
1
]),
str
(
shape
[:
-
1
])))
index_prefix
=
paddle
.
unsqueeze
(
arange
(
num_dist
,
dtype
=
index_value
.
dtype
),
axis
=-
1
)
index_prefix
=
nn
.
expand
(
index_prefix
,
[
1
,
num_value_in_one_dist
])
index_prefix
=
paddle
.
unsqueeze
(
index_prefix
,
axis
=-
1
)
if
index_value
.
dtype
!=
index_prefix
.
dtype
:
tensor
.
cast
(
index_prefix
,
dtype
=
index_value
.
dtype
)
index
=
concat
([
index_prefix
,
index_value
],
axis
=-
1
)
# value is the category index to search for the corresponding probability.
select_prob
=
gather_nd
(
prob
,
index
)
return
paddle
.
reshape
(
select_prob
,
value_shape
,
name
=
name
)
def
log_prob
(
self
,
value
):
def
log_prob
(
self
,
value
):
"""Log probabilities of the given category. Refer to ``probs`` method.
"""Log probabilities of the given category. Refer to ``probs`` method.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录