Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
993df98e
提交
993df98e
authored
12月 01, 2014
作者:
Pierre Luc Carrier
提交者:
--global
6月 01, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add breakpoint Op
上级
1bf7ea39
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
98 行增加
和
0 行删除
+98
-0
breakpoint.py
theano/tests/breakpoint.py
+98
-0
没有找到文件。
theano/tests/breakpoint.py
0 → 100644
浏览文件 @
993df98e
import
pdb
import
theano
import
theano.tensor
as
T
from
theano.gof
import
Op
,
Apply
class
PdbBreakpoint
(
Op
):
"""
This is an identity-like op with the side effect of enforcing a
conditional breakpoint, inside a theano function, based on a symbolic
scalar condition.
@type name: String
@param name: name of the conditional breakpoint. To be printed when the
breakpoint is activated.
:note: WARNING. At least one of the outputs of the op must be used
otherwise the op will be removed from the Theano graph
due to its outputs being unused
:note: WARNING. Employing the function inside a theano graph can prevent
Theano from applying certain optimizations to improve
performance, reduce memory consumption and/or reduce
numerical instability.
Detailed explanation:
As of 2014-12-01 the PdbBreakpoint op is not known by any
optimization. Setting a PdbBreakpoint op in the middle of a
pattern that is usually optimized out will block the optimization.
Example:
.. code-block:: python
import theano
import theano.tensor as T
from theano.tests.breakpoint import PdbBreakpoint
input = T.fvector()
target = T.fvector()
# Mean squared error between input and target
mse = (input - target) ** 2
# Conditional breakpoint to be activated if the total MSE is higher
# than 100. The breakpoint will monitor the inputs, targets as well
# as the individual error values
breakpointOp = PdbBreakpoint("MSE too high")
condition = T.gt(mse.sum(), 100)
mse, _, _ = breakpointOp(condition, mse, input, target)
# Compile the theano function
fct = theano.function([input, target], mse)
# Use the function
print fct([10, 0], [10, 5]) # Will NOT activate the breakpoint
print fct([0, 0], [10, 5]) # Will activate the breakpoint
"""
__props__
=
()
def
__init__
(
self
,
name
):
self
.
name
=
name
def
make_node
(
self
,
condition
,
*
monitored_vars
):
# Validate that the condition is a scalar (else it is not obvious how
# is should be evaluated)
assert
(
condition
.
ndim
==
0
)
# Build the Apply node
inputs
=
[
condition
]
+
list
(
monitored_vars
)
outputs
=
[
inp
.
type
.
make_variable
()
for
inp
in
monitored_vars
]
return
Apply
(
op
=
self
,
inputs
=
inputs
,
outputs
=
outputs
)
def
perform
(
self
,
node
,
inputs
,
output_storage
):
condition
=
inputs
[
0
]
monitored
=
inputs
[
1
:]
if
condition
:
print
"-------------------------------------------------"
print
"Conditional breakpoint
%
s activated"
%
self
.
name
print
"The monitored variables are stored in 'monitored'"
print
"-------------------------------------------------"
pdb
.
set_trace
()
for
i
in
range
(
len
(
output_storage
)):
output_storage
[
i
][
0
]
=
monitored
[
i
]
def
grad
(
self
,
inputs
,
output_gradients
):
return
([
inputs
[
0
]
.
zeros_like
()
.
astype
(
theano
.
config
.
floatX
)]
+
output_gradients
)
def
infer_shape
(
self
,
inputs
,
input_shapes
):
# Return the shape of every input but the condition
return
input_shapes
[
1
:]
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论