Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f67638b6
提交
f67638b6
authored
12月 14, 2022
作者:
Maxim Kochurov
提交者:
Maxim Kochurov
12月 16, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove ConsiderConstant
上级
09010219
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
1 行增加
和
106 行删除
+1
-106
gradient.py
pytensor/gradient.py
+0
-63
__init__.py
pytensor/tensor/__init__.py
+1
-1
test_gradient.py
tests/test_gradient.py
+0
-42
没有找到文件。
pytensor/gradient.py
浏览文件 @
f67638b6
...
...
@@ -2108,44 +2108,6 @@ def _is_zero(x):
return
"yes"
class
ConsiderConstant
(
ViewOp
):
def
grad
(
self
,
args
,
g_outs
):
return
[
g_out
.
zeros_like
(
g_out
)
for
g_out
in
g_outs
]
consider_constant_
=
ConsiderConstant
()
def
consider_constant
(
x
):
"""Consider an expression constant when computing gradients.
DEPRECATED: use `zero_grad` or `disconnected_grad` instead.
The expression itself is unaffected, but when its gradient is
computed, or the gradient of another expression that this
expression is a subexpression of, it will not be backpropagated
through. In other words, the gradient of the expression is
truncated to 0.
:param x: A PyTensor expression whose gradient should be truncated.
:return: The expression is returned unmodified, but its gradient
is now truncated to 0.
.. versionadded:: 0.7
"""
warnings
.
warn
(
(
"`ConsiderConstant` is deprecated; use `zero_grad` or "
"`disconnected_grad` instead."
),
category
=
DeprecationWarning
,
stacklevel
=
3
,
)
return
ConsiderConstant
()(
x
)
class
ZeroGrad
(
ViewOp
):
def
grad
(
self
,
args
,
g_outs
):
return
[
g_out
.
zeros_like
(
g_out
)
for
g_out
in
g_outs
]
...
...
@@ -2352,28 +2314,3 @@ def grad_scale(x, multiplier):
0.416...
"""
return
GradScale
(
multiplier
)(
x
)
DEPRECATED_NAMES
=
[
(
"consider_constant_"
,
"`consider_constant_` is deprecated; use `zero_grad` or `disconnected_grad` instead."
,
ConsiderConstant
(),
),
]
def
__getattr__
(
name
):
"""Intercept module-level attribute access of deprecated symbols.
Adapted from https://stackoverflow.com/a/55139609/3006474.
"""
from
warnings
import
warn
for
old_name
,
msg
,
old_object
in
DEPRECATED_NAMES
:
if
name
==
old_name
:
warn
(
msg
,
DeprecationWarning
,
stacklevel
=
2
)
return
old_object
raise
AttributeError
(
f
"module {__name__} has no attribute {name}"
)
pytensor/tensor/__init__.py
浏览文件 @
f67638b6
...
...
@@ -99,7 +99,7 @@ def _get_vector_length_Constant(op: Union[Op, Variable], var: Constant) -> int:
import
pytensor.tensor.exceptions
# noqa
from
pytensor.gradient
import
consider_constant
,
grad
,
hessian
,
jacobian
# noqa
from
pytensor.gradient
import
grad
,
hessian
,
jacobian
# noqa
# adds shared-variable constructors
from
pytensor.tensor
import
sharedvar
# noqa
...
...
tests/test_gradient.py
浏览文件 @
f67638b6
...
...
@@ -766,48 +766,6 @@ def test_subgraph_grad():
assert
np
.
sum
(
np
.
abs
(
true_grad
-
pgrad
))
<
0.00001
class
TestConsiderConstant
:
def
test_op_removed
(
self
):
from
pytensor.gradient
import
ConsiderConstant
,
consider_constant
x
=
matrix
(
"x"
)
with
pytest
.
deprecated_call
():
y
=
x
*
consider_constant
(
x
)
f
=
pytensor
.
function
([
x
],
y
)
assert
ConsiderConstant
not
in
[
type
(
node
.
op
)
for
node
in
f
.
maker
.
fgraph
.
toposort
()
]
def
test_grad
(
self
):
from
pytensor.gradient
import
consider_constant
rng
=
np
.
random
.
default_rng
(
seed
=
utt
.
fetch_seed
())
a
=
np
.
asarray
(
rng
.
standard_normal
((
5
,
5
)),
dtype
=
config
.
floatX
)
x
=
matrix
(
"x"
)
with
pytest
.
deprecated_call
():
expressions_gradients
=
[
(
x
*
consider_constant
(
x
),
x
),
(
x
*
consider_constant
(
exp
(
x
)),
exp
(
x
)),
(
consider_constant
(
x
),
at
.
constant
(
0.0
)),
(
x
**
2
*
consider_constant
(
x
),
2
*
x
**
2
),
]
for
expr
,
expr_grad
in
expressions_gradients
:
g
=
grad
(
expr
.
sum
(),
x
)
# gradient according to pytensor
f
=
pytensor
.
function
([
x
],
g
,
on_unused_input
=
"ignore"
)
# desired gradient
f2
=
pytensor
.
function
([
x
],
expr_grad
,
on_unused_input
=
"ignore"
)
assert
np
.
allclose
(
f
(
a
),
f2
(
a
))
class
TestZeroGrad
:
def
setup_method
(
self
):
self
.
rng
=
np
.
random
.
default_rng
(
seed
=
utt
.
fetch_seed
())
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论