Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4969ddfe
提交
4969ddfe
authored
10月 13, 2016
作者:
Cesar Laurent
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Finalized tests and docstring.
上级
f0ff84d9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
45 行增加
和
36 行删除
+45
-36
scan_checkpoint.py
theano/scan_module/scan_checkpoint.py
+20
-0
test_scan_checkpoint.py
theano/scan_module/tests/test_scan_checkpoint.py
+25
-36
没有找到文件。
theano/scan_module/scan_checkpoint.py
浏览文件 @
4969ddfe
...
@@ -63,6 +63,21 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None,
...
@@ -63,6 +63,21 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None,
the computations of scan (ie they will have to be recomputed
the computations of scan (ie they will have to be recomputed
during the gradient computation).
during the gradient computation).
Returns
-------
tuple
Tuple of the form (outputs, updates); ``outputs`` is either a
Theano variable or a list of Theano variables representing the
outputs of ``scan`` (in the same order as in ``outputs_info``).
``updates`` is a subclass of dictionary specifying the update rules for
all shared variables used in scan.
This dictionary should be passed to ``theano.function`` when you compile
your function. The change compared to a normal dictionary is that we
validate that keys are SharedVariable and addition of those dictionary
are validated to be consistent.
Note that only the last time step of ``outputs`` can be used with this
type of scan.
See Also
See Also
--------
--------
scan : Looping in Theano.
scan : Looping in Theano.
...
@@ -76,6 +91,11 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None,
...
@@ -76,6 +91,11 @@ def scan_with_checkpoints(fn, sequences=[], outputs_info=None,
if
not
isinstance
(
non_sequences
,
list
):
if
not
isinstance
(
non_sequences
,
list
):
non_sequences
=
[
non_sequences
]
non_sequences
=
[
non_sequences
]
# Check that outputs_info has no taps:
for
element
in
outputs_info
:
if
isinstance
(
element
,
dict
)
and
'taps'
in
element
:
raise
RuntimeError
(
"scan_with_checkpoints doesn't work with taps."
)
# Determine how many steps the original scan would run
# Determine how many steps the original scan would run
if
n_steps
is
None
:
if
n_steps
is
None
:
n_steps
=
sequences
[
0
]
.
shape
[
0
]
n_steps
=
sequences
[
0
]
.
shape
[
0
]
...
...
theano/scan_module/tests/test_scan_checkpoint.py
浏览文件 @
4969ddfe
...
@@ -6,25 +6,25 @@ import unittest
...
@@ -6,25 +6,25 @@ import unittest
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
T
from
pygpu.gpuarray
import
GpuArrayException
class
TestScanCheckpoint
(
unittest
.
TestCase
):
class
TestScanCheckpoint
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
k
=
T
.
iscalar
(
"k"
)
self
.
k
=
T
.
iscalar
(
"k"
)
A
=
T
.
vector
(
"A"
)
self
.
A
=
T
.
vector
(
"A"
)
self
.
k
=
k
self
.
A
=
A
result
,
_
=
theano
.
scan
(
result
,
_
=
theano
.
scan
(
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
outputs_info
=
T
.
ones_like
(
A
),
outputs_info
=
T
.
ones_like
(
self
.
A
),
non_sequences
=
A
,
non_sequences
=
self
.
A
,
n_steps
=
k
)
n_steps
=
self
.
k
)
result_check
,
_
=
theano
.
scan_with_checkpoints
(
result_check
,
_
=
theano
.
scan_with_checkpoints
(
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
outputs_info
=
T
.
ones_like
(
A
),
outputs_info
=
T
.
ones_like
(
self
.
A
),
non_sequences
=
A
,
non_sequences
=
self
.
A
,
n_steps
=
k
,
n_steps
=
self
.
k
,
save_every_N
=
5
0
)
save_every_N
=
10
0
)
self
.
result
=
result
[
-
1
]
self
.
result
=
result
[
-
1
]
self
.
result_check
=
result_check
[
-
1
]
self
.
result_check
=
result_check
[
-
1
]
self
.
grad_A
=
T
.
grad
(
self
.
result
.
sum
(),
self
.
A
)
self
.
grad_A
=
T
.
grad
(
self
.
result
.
sum
(),
self
.
A
)
...
@@ -44,33 +44,22 @@ class TestScanCheckpoint(unittest.TestCase):
...
@@ -44,33 +44,22 @@ class TestScanCheckpoint(unittest.TestCase):
out
,
out_check
=
f
(
range
(
10
),
100
)
out
,
out_check
=
f
(
range
(
10
),
100
)
assert
numpy
.
allclose
(
out
,
out_check
)
assert
numpy
.
allclose
(
out
,
out_check
)
@unittest.skipUnless
(
theano
.
gpuarray
.
type
.
_context_reg
[
None
],
'Requires gpuarray backend.'
)
def
test_memory
(
self
):
def
test_memory
(
self
):
"""Test that scan_checkpoint reduces memory usage."""
"""Test that scan_checkpoint reduces memory usage."""
k
=
T
.
iscalar
(
"k"
)
if
None
not
in
theano
.
gpuarray
.
type
.
list_contexts
():
A
=
T
.
vector
(
"A"
)
return
unittest
.
SkipTest
(
'Requires gpuarray backend.'
)
result
,
updates
=
theano
.
scan
(
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
f
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
outputs_info
=
T
.
ones_like
(
A
),
outputs
=
self
.
grad_A
)
non_sequences
=
A
,
f_check
=
theano
.
function
(
inputs
=
[
self
.
A
,
self
.
k
],
n_steps
=
k
)
outputs
=
self
.
grad_A_check
)
result_check
,
updates_check
=
theano
.
scan_with_checkpoints
(
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
outputs_info
=
T
.
ones_like
(
A
),
non_sequences
=
A
,
n_steps
=
k
,
save_every_N
=
10000
)
result
=
result
[
-
1
]
result_check
=
result_check
[
-
1
]
grad_A
=
T
.
grad
(
result
.
sum
(),
A
)
grad_A_check
=
T
.
grad
(
result_check
.
sum
(),
A
)
f
=
theano
.
function
(
inputs
=
[
A
,
k
],
outputs
=
grad_A
,
updates
=
updates
+
updates_check
)
f_check
=
theano
.
function
(
inputs
=
[
A
,
k
],
outputs
=
grad_A_check
,
updates
=
updates
+
updates_check
)
free_gmem
=
theano
.
gpuarray
.
type
.
_context_reg
[
None
]
.
free_gmem
free_gmem
=
theano
.
gpuarray
.
type
.
_context_reg
[
None
]
.
free_gmem
data
=
numpy
.
ones
(
free_gmem
/
40.
,
dtype
=
numpy
.
float32
)
data
=
numpy
.
ones
(
free_gmem
/
3000
,
dtype
=
numpy
.
float32
)
# Check that it works with the checkpoints
# Check that it works with the checkpoints
f_check
(
data
,
1000
000
)
f_check
(
data
,
1000
)
# Check that the basic scan fails in that case
# Check that the basic scan fails in that case
self
.
assertRaises
(
MemoryError
,
f
,
data
,
1000000
)
self
.
assertRaises
(
GpuArrayException
,
f
,
data
,
1000
)
def
test_taps_error
(
self
):
"""Test that an error rises if we use taps in outputs_info."""
self
.
assertRaises
(
RuntimeError
,
theano
.
scan_with_checkpoints
,
lambda
:
None
,
[],
{
'initial'
:
self
.
A
,
'taps'
:
[
-
2
]})
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论