Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
daacd069
提交
daacd069
authored
10月 19, 2020
作者:
Brandon T. Willard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace theano.tensor alias T with tt in tests.scan_module
上级
b4dc02d6
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
54 行增加
和
52 行删除
+54
-52
test_scan_checkpoints.py
tests/scan_module/test_scan_checkpoints.py
+7
-7
test_scan_opt.py
tests/scan_module/test_scan_opt.py
+47
-45
没有找到文件。
tests/scan_module/test_scan_checkpoints.py
浏览文件 @
daacd069
...
@@ -2,7 +2,7 @@ import numpy as np
...
@@ -2,7 +2,7 @@ import numpy as np
import
pytest
import
pytest
import
theano
import
theano
import
theano.tensor
as
T
import
theano.tensor
as
tt
import
theano.gpuarray
import
theano.gpuarray
try
:
try
:
...
@@ -15,25 +15,25 @@ except ImportError:
...
@@ -15,25 +15,25 @@ except ImportError:
class
TestScanCheckpoint
:
class
TestScanCheckpoint
:
def
setup_method
(
self
):
def
setup_method
(
self
):
self
.
k
=
T
.
iscalar
(
"k"
)
self
.
k
=
tt
.
iscalar
(
"k"
)
self
.
A
=
T
.
vector
(
"A"
)
self
.
A
=
tt
.
vector
(
"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
(
self
.
A
),
outputs_info
=
tt
.
ones_like
(
self
.
A
),
non_sequences
=
self
.
A
,
non_sequences
=
self
.
A
,
n_steps
=
self
.
k
,
n_steps
=
self
.
k
,
)
)
result_check
,
_
=
theano
.
scan_checkpoints
(
result_check
,
_
=
theano
.
scan_checkpoints
(
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
fn
=
lambda
prior_result
,
A
:
prior_result
*
A
,
outputs_info
=
T
.
ones_like
(
self
.
A
),
outputs_info
=
tt
.
ones_like
(
self
.
A
),
non_sequences
=
self
.
A
,
non_sequences
=
self
.
A
,
n_steps
=
self
.
k
,
n_steps
=
self
.
k
,
save_every_N
=
100
,
save_every_N
=
100
,
)
)
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
=
tt
.
grad
(
self
.
result
.
sum
(),
self
.
A
)
self
.
grad_A_check
=
T
.
grad
(
self
.
result_check
.
sum
(),
self
.
A
)
self
.
grad_A_check
=
tt
.
grad
(
self
.
result_check
.
sum
(),
self
.
A
)
def
test_forward_pass
(
self
):
def
test_forward_pass
(
self
):
# Test forward computation of A**k.
# Test forward computation of A**k.
...
...
tests/scan_module/test_scan_opt.py
浏览文件 @
daacd069
import
numpy
as
np
import
numpy
as
np
import
theano
import
theano
import
theano.tensor
as
tt
from
theano
import
config
from
theano
import
config
from
theano
import
tensor
as
T
from
theano.scan_module.scan_op
import
Scan
from
theano.scan_module.scan_op
import
Scan
from
tests
import
unittest_tools
as
utt
from
tests
import
unittest_tools
as
utt
...
@@ -40,11 +40,11 @@ class TestGaussNewton:
...
@@ -40,11 +40,11 @@ class TestGaussNewton:
# create symbolic inputs and targets variables
# create symbolic inputs and targets variables
if
batch_size
==
1
:
if
batch_size
==
1
:
x
=
T
.
matrix
(
"inputs"
)
x
=
tt
.
matrix
(
"inputs"
)
t
=
T
.
matrix
(
"targets"
)
t
=
tt
.
matrix
(
"targets"
)
else
:
else
:
x
=
T
.
tensor3
(
"inputs"
)
x
=
tt
.
tensor3
(
"inputs"
)
t
=
T
.
tensor3
(
"inputs"
)
t
=
tt
.
tensor3
(
"inputs"
)
x
.
tag
.
test_value
=
inputs
.
get_value
(
borrow
=
True
)
x
.
tag
.
test_value
=
inputs
.
get_value
(
borrow
=
True
)
t
.
tag
.
test_value
=
targets
.
get_value
(
borrow
=
True
)
t
.
tag
.
test_value
=
targets
.
get_value
(
borrow
=
True
)
...
@@ -66,17 +66,17 @@ class TestGaussNewton:
...
@@ -66,17 +66,17 @@ class TestGaussNewton:
# recurrent function
# recurrent function
def
step
(
x_t
,
h_tm1
):
def
step
(
x_t
,
h_tm1
):
h
=
T
.
tanh
(
T
.
dot
(
h_tm1
,
W_hh
)
+
T
.
dot
(
x_t
,
W_xh
)
+
b_h
)
h
=
tt
.
tanh
(
tt
.
dot
(
h_tm1
,
W_hh
)
+
tt
.
dot
(
x_t
,
W_xh
)
+
b_h
)
return
h
return
h
# build recurrent graph
# build recurrent graph
if
batch_size
==
1
:
if
batch_size
==
1
:
h_0
=
T
.
alloc
(
0.0
,
10
)
.
astype
(
config
.
floatX
)
h_0
=
tt
.
alloc
(
0.0
,
10
)
.
astype
(
config
.
floatX
)
else
:
else
:
h_0
=
T
.
alloc
(
0.0
,
batch_size
,
10
)
.
astype
(
config
.
floatX
)
h_0
=
tt
.
alloc
(
0.0
,
batch_size
,
10
)
.
astype
(
config
.
floatX
)
h
,
updates
=
theano
.
scan
(
step
,
sequences
=
[
x
],
outputs_info
=
[
h_0
])
h
,
updates
=
theano
.
scan
(
step
,
sequences
=
[
x
],
outputs_info
=
[
h_0
])
# network output
# network output
y
=
T
.
dot
(
h
,
W_hy
)
+
b_y
y
=
tt
.
dot
(
h
,
W_hy
)
+
b_y
# Create Gauss-Newton-Matrix object. Not really of any use here, but I
# Create Gauss-Newton-Matrix object. Not really of any use here, but I
# need it for Hessian-Free optimization.
# need it for Hessian-Free optimization.
...
@@ -95,7 +95,7 @@ class TestGaussNewton:
...
@@ -95,7 +95,7 @@ class TestGaussNewton:
# Compute Gauss-Newton-Matrix times some vector `v` which is `p` in CG,
# Compute Gauss-Newton-Matrix times some vector `v` which is `p` in CG,
# but for simplicity, I just take the parameters vector because it's
# but for simplicity, I just take the parameters vector because it's
# already there.
# already there.
Gv
=
gn
(
v
=
params
,
cost
=
cost
,
parameters
=
params
,
damp
=
T
.
constant
(
1.0
))
Gv
=
gn
(
v
=
params
,
cost
=
cost
,
parameters
=
params
,
damp
=
tt
.
constant
(
1.0
))
# compile Theano function
# compile Theano function
f
=
theano
.
function
([],
[
cost_
]
+
Gv
,
givens
=
{
x
:
inputs
,
t
:
targets
},
mode
=
mode
)
f
=
theano
.
function
([],
[
cost_
]
+
Gv
,
givens
=
{
x
:
inputs
,
t
:
targets
},
mode
=
mode
)
...
@@ -121,9 +121,11 @@ class GaussNewtonMatrix(object):
...
@@ -121,9 +121,11 @@ class GaussNewtonMatrix(object):
def
__call__
(
self
,
v
,
cost
,
parameters
,
damp
):
def
__call__
(
self
,
v
,
cost
,
parameters
,
damp
):
# compute Gauss-Newton Matrix right-multiplied by `v`
# compute Gauss-Newton Matrix right-multiplied by `v`
Jv
=
T
.
Rop
(
self
.
_s
,
parameters
,
v
)
Jv
=
tt
.
Rop
(
self
.
_s
,
parameters
,
v
)
HJv
=
T
.
grad
(
T
.
sum
(
T
.
grad
(
cost
,
self
.
_s
)
*
Jv
),
self
.
_s
,
consider_constant
=
[
Jv
])
HJv
=
tt
.
grad
(
JHJv
=
T
.
grad
(
T
.
sum
(
HJv
*
self
.
_s
),
parameters
,
consider_constant
=
[
HJv
,
Jv
])
tt
.
sum
(
tt
.
grad
(
cost
,
self
.
_s
)
*
Jv
),
self
.
_s
,
consider_constant
=
[
Jv
]
)
JHJv
=
tt
.
grad
(
tt
.
sum
(
HJv
*
self
.
_s
),
parameters
,
consider_constant
=
[
HJv
,
Jv
])
# apply Tikhonov damping
# apply Tikhonov damping
JHJv
=
[
JHJvi
+
damp
*
vi
for
JHJvi
,
vi
in
zip
(
JHJv
,
v
)]
JHJv
=
[
JHJvi
+
damp
*
vi
for
JHJvi
,
vi
in
zip
(
JHJv
,
v
)]
...
@@ -142,17 +144,17 @@ class TestPushOutScanOutputDot(object):
...
@@ -142,17 +144,17 @@ class TestPushOutScanOutputDot(object):
# Test the case where the vector input to the dot is not already an
# Test the case where the vector input to the dot is not already an
# output of the inner function.
# output of the inner function.
v
=
T
.
vector
()
v
=
tt
.
vector
()
m
=
T
.
matrix
()
m
=
tt
.
matrix
()
output
=
T
.
dot
(
v
,
m
)
output
=
tt
.
dot
(
v
,
m
)
# Compile the function twice, once with the optimization and once
# Compile the function twice, once with the optimization and once
# without
# without
opt_mode
=
mode
.
including
(
"scan"
)
opt_mode
=
mode
.
including
(
"scan"
)
f_opt
=
theano
.
function
([
v
,
m
],
T
.
jacobian
(
output
,
v
),
mode
=
opt_mode
)
f_opt
=
theano
.
function
([
v
,
m
],
tt
.
jacobian
(
output
,
v
),
mode
=
opt_mode
)
no_opt_mode
=
mode
.
excluding
(
"scanOp_pushout_output"
)
no_opt_mode
=
mode
.
excluding
(
"scanOp_pushout_output"
)
f_no_opt
=
theano
.
function
([
v
,
m
],
T
.
jacobian
(
output
,
v
),
mode
=
no_opt_mode
)
f_no_opt
=
theano
.
function
([
v
,
m
],
tt
.
jacobian
(
output
,
v
),
mode
=
no_opt_mode
)
# Ensure that the optimization was performed correctly in f_opt
# Ensure that the optimization was performed correctly in f_opt
# The inner function of scan should have only one output and it should
# The inner function of scan should have only one output and it should
...
@@ -161,7 +163,7 @@ class TestPushOutScanOutputDot(object):
...
@@ -161,7 +163,7 @@ class TestPushOutScanOutputDot(object):
node
for
node
in
f_opt
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
Scan
)
node
for
node
in
f_opt
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
Scan
)
][
0
]
][
0
]
assert
len
(
scan_node
.
op
.
outputs
)
==
1
assert
len
(
scan_node
.
op
.
outputs
)
==
1
assert
not
isinstance
(
scan_node
.
op
.
outputs
[
0
],
T
.
Dot
)
assert
not
isinstance
(
scan_node
.
op
.
outputs
[
0
],
tt
.
Dot
)
# Ensure that the function compiled with the optimization produces
# Ensure that the function compiled with the optimization produces
# the same results as the function compiled without
# the same results as the function compiled without
...
@@ -177,12 +179,12 @@ class TestPushOutScanOutputDot(object):
...
@@ -177,12 +179,12 @@ class TestPushOutScanOutputDot(object):
# Test the case where the vector input to the dot is already a nitsot
# Test the case where the vector input to the dot is already a nitsot
# output of the inner function.
# output of the inner function.
a
=
T
.
matrix
()
a
=
tt
.
matrix
()
b
=
T
.
matrix
()
b
=
tt
.
matrix
()
def
inner_fct
(
vect
,
mat
):
def
inner_fct
(
vect
,
mat
):
vect_squared
=
vect
**
2
vect_squared
=
vect
**
2
return
T
.
dot
(
vect_squared
,
mat
),
vect_squared
return
tt
.
dot
(
vect_squared
,
mat
),
vect_squared
outputs
,
updates
=
theano
.
scan
(
outputs
,
updates
=
theano
.
scan
(
fn
=
inner_fct
,
outputs_info
=
[
None
]
*
2
,
sequences
=
a
,
non_sequences
=
b
fn
=
inner_fct
,
outputs_info
=
[
None
]
*
2
,
sequences
=
a
,
non_sequences
=
b
...
@@ -205,7 +207,7 @@ class TestPushOutScanOutputDot(object):
...
@@ -205,7 +207,7 @@ class TestPushOutScanOutputDot(object):
# NOTE: WHEN INFER_SHAPE IS REENABLED, BELOW THE SCAN MUST
# NOTE: WHEN INFER_SHAPE IS REENABLED, BELOW THE SCAN MUST
# HAVE ONLY 1 OUTPUT.
# HAVE ONLY 1 OUTPUT.
assert
len
(
scan_node
.
op
.
outputs
)
==
2
assert
len
(
scan_node
.
op
.
outputs
)
==
2
assert
not
isinstance
(
scan_node
.
op
.
outputs
[
0
],
T
.
Dot
)
assert
not
isinstance
(
scan_node
.
op
.
outputs
[
0
],
tt
.
Dot
)
# Ensure that the function compiled with the optimization produces
# Ensure that the function compiled with the optimization produces
# the same results as the function compiled without
# the same results as the function compiled without
...
@@ -222,12 +224,12 @@ class TestPushOutScanOutputDot(object):
...
@@ -222,12 +224,12 @@ class TestPushOutScanOutputDot(object):
# Test the case where the vector input to the dot is not already a
# Test the case where the vector input to the dot is not already a
# non-nitsot (in this case a sitsot) output of the inner function.
# non-nitsot (in this case a sitsot) output of the inner function.
a
=
T
.
matrix
()
a
=
tt
.
matrix
()
b
=
T
.
matrix
()
b
=
tt
.
matrix
()
def
inner_fct
(
seq1
,
previous_output1
,
nonseq1
):
def
inner_fct
(
seq1
,
previous_output1
,
nonseq1
):
output1
=
previous_output1
+
seq1
output1
=
previous_output1
+
seq1
output2
=
T
.
dot
(
output1
,
nonseq1
)
output2
=
tt
.
dot
(
output1
,
nonseq1
)
return
output1
,
output2
return
output1
,
output2
outputs
,
updates
=
theano
.
scan
(
outputs
,
updates
=
theano
.
scan
(
...
@@ -249,7 +251,7 @@ class TestPushOutScanOutputDot(object):
...
@@ -249,7 +251,7 @@ class TestPushOutScanOutputDot(object):
node
for
node
in
f_opt
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
Scan
)
node
for
node
in
f_opt
.
maker
.
fgraph
.
toposort
()
if
isinstance
(
node
.
op
,
Scan
)
][
0
]
][
0
]
assert
len
(
scan_node
.
op
.
outputs
)
==
2
assert
len
(
scan_node
.
op
.
outputs
)
==
2
assert
not
isinstance
(
scan_node
.
op
.
outputs
[
0
],
T
.
Dot
)
assert
not
isinstance
(
scan_node
.
op
.
outputs
[
0
],
tt
.
Dot
)
# Ensure that the function compiled with the optimization produces
# Ensure that the function compiled with the optimization produces
# the same results as the function compiled without
# the same results as the function compiled without
...
@@ -293,18 +295,18 @@ class TestPushOutSumOfDot:
...
@@ -293,18 +295,18 @@ class TestPushOutSumOfDot:
W
.
name
=
"W"
W
.
name
=
"W"
# Variables and their values
# Variables and their values
x
=
T
.
tensor3
(
"x"
)
x
=
tt
.
tensor3
(
"x"
)
x_value
=
np
.
random
.
normal
(
x_value
=
np
.
random
.
normal
(
size
=
(
seq_len
,
batch_size
,
dim
),
scale
=
0.0001
size
=
(
seq_len
,
batch_size
,
dim
),
scale
=
0.0001
)
.
astype
(
config
.
floatX
)
)
.
astype
(
config
.
floatX
)
ri
=
T
.
tensor3
(
"ri"
)
ri
=
tt
.
tensor3
(
"ri"
)
ri_value
=
x_value
ri_value
=
x_value
zi
=
T
.
tensor3
(
"zi"
)
zi
=
tt
.
tensor3
(
"zi"
)
zi_value
=
x_value
zi_value
=
x_value
init
=
T
.
alloc
(
np
.
cast
[
config
.
floatX
](
0
),
batch_size
,
dim
)
init
=
tt
.
alloc
(
np
.
cast
[
config
.
floatX
](
0
),
batch_size
,
dim
)
def
rnn_step1
(
def
rnn_step1
(
# sequences
# sequences
...
@@ -316,12 +318,12 @@ class TestPushOutSumOfDot:
...
@@ -316,12 +318,12 @@ class TestPushOutSumOfDot:
):
):
pre_r
=
ri
+
h
.
dot
(
U
)
pre_r
=
ri
+
h
.
dot
(
U
)
pre_z
=
zi
+
h
.
dot
(
V
)
pre_z
=
zi
+
h
.
dot
(
V
)
r
=
T
.
nnet
.
sigmoid
(
pre_r
)
r
=
tt
.
nnet
.
sigmoid
(
pre_r
)
z
=
T
.
nnet
.
sigmoid
(
pre_z
)
z
=
tt
.
nnet
.
sigmoid
(
pre_z
)
after_r
=
r
*
h
after_r
=
r
*
h
pre_h
=
x
+
after_r
.
dot
(
W
)
pre_h
=
x
+
after_r
.
dot
(
W
)
new_h
=
T
.
tanh
(
pre_h
)
new_h
=
tt
.
tanh
(
pre_h
)
res_h
=
z
*
new_h
+
(
1
-
z
)
*
h
res_h
=
z
*
new_h
+
(
1
-
z
)
*
h
return
res_h
return
res_h
...
@@ -338,7 +340,7 @@ class TestPushOutSumOfDot:
...
@@ -338,7 +340,7 @@ class TestPushOutSumOfDot:
mode
=
opt_mode
,
mode
=
opt_mode
,
)
)
cost
=
h
[
-
1
]
.
sum
()
cost
=
h
[
-
1
]
.
sum
()
grad1
=
T
.
grad
(
cost
,
[
U
,
V
,
W
])
grad1
=
tt
.
grad
(
cost
,
[
U
,
V
,
W
])
f_opt
=
theano
.
function
(
inputs
=
[
x
,
ri
,
zi
],
outputs
=
grad1
,
mode
=
opt_mode
)
f_opt
=
theano
.
function
(
inputs
=
[
x
,
ri
,
zi
],
outputs
=
grad1
,
mode
=
opt_mode
)
no_opt_mode
=
mode
.
excluding
(
"scanOp_pushout_output"
)
no_opt_mode
=
mode
.
excluding
(
"scanOp_pushout_output"
)
...
@@ -351,7 +353,7 @@ class TestPushOutSumOfDot:
...
@@ -351,7 +353,7 @@ class TestPushOutSumOfDot:
mode
=
no_opt_mode
,
mode
=
no_opt_mode
,
)
)
cost
=
h
[
-
1
]
.
sum
()
cost
=
h
[
-
1
]
.
sum
()
grad1
=
T
.
grad
(
cost
,
[
U
,
V
,
W
])
grad1
=
tt
.
grad
(
cost
,
[
U
,
V
,
W
])
f_no_opt
=
theano
.
function
(
inputs
=
[
x
,
ri
,
zi
],
outputs
=
grad1
,
mode
=
no_opt_mode
)
f_no_opt
=
theano
.
function
(
inputs
=
[
x
,
ri
,
zi
],
outputs
=
grad1
,
mode
=
no_opt_mode
)
# Validate that the optimization has been applied
# Validate that the optimization has been applied
...
@@ -361,8 +363,8 @@ class TestPushOutSumOfDot:
...
@@ -361,8 +363,8 @@ class TestPushOutSumOfDot:
for
output
in
scan_node_grad
.
op
.
outputs
:
for
output
in
scan_node_grad
.
op
.
outputs
:
assert
not
(
assert
not
(
isinstance
(
output
.
owner
.
op
,
T
.
elemwise
.
Elemwise
)
isinstance
(
output
.
owner
.
op
,
tt
.
elemwise
.
Elemwise
)
and
any
([
isinstance
(
i
,
T
.
Dot
)
for
i
in
output
.
owner
.
inputs
])
and
any
([
isinstance
(
i
,
tt
.
Dot
)
for
i
in
output
.
owner
.
inputs
])
)
)
# Compare the outputs of the two functions on the same input data.
# Compare the outputs of the two functions on the same input data.
...
@@ -373,20 +375,20 @@ class TestPushOutSumOfDot:
...
@@ -373,20 +375,20 @@ class TestPushOutSumOfDot:
def
test_non_zero_init
(
self
):
def
test_non_zero_init
(
self
):
# Test the case where the initial value for the nitsot output is non-zero
# Test the case where the initial value for the nitsot output is non-zero
input1
=
T
.
tensor3
()
input1
=
tt
.
tensor3
()
input2
=
T
.
tensor3
()
input2
=
tt
.
tensor3
()
input3
=
T
.
tensor3
()
input3
=
tt
.
tensor3
()
W
=
theano
.
shared
(
np
.
random
.
normal
(
size
=
(
4
,
5
)))
.
astype
(
config
.
floatX
)
W
=
theano
.
shared
(
np
.
random
.
normal
(
size
=
(
4
,
5
)))
.
astype
(
config
.
floatX
)
U
=
theano
.
shared
(
np
.
random
.
normal
(
size
=
(
6
,
7
)))
.
astype
(
config
.
floatX
)
U
=
theano
.
shared
(
np
.
random
.
normal
(
size
=
(
6
,
7
)))
.
astype
(
config
.
floatX
)
def
inner_fct
(
seq1
,
seq2
,
seq3
,
previous_output
):
def
inner_fct
(
seq1
,
seq2
,
seq3
,
previous_output
):
temp1
=
T
.
dot
(
seq1
,
W
)
+
seq3
temp1
=
tt
.
dot
(
seq1
,
W
)
+
seq3
temp2
=
T
.
dot
(
seq2
,
U
)
temp2
=
tt
.
dot
(
seq2
,
U
)
dot_output
=
T
.
dot
(
temp1
,
temp2
)
dot_output
=
tt
.
dot
(
temp1
,
temp2
)
return
previous_output
+
dot_output
return
previous_output
+
dot_output
init
=
T
.
as_tensor_variable
(
np
.
random
.
normal
(
size
=
(
3
,
7
)))
init
=
tt
.
as_tensor_variable
(
np
.
random
.
normal
(
size
=
(
3
,
7
)))
# Compile the function twice, once with the optimization and once
# Compile the function twice, once with the optimization and once
# without
# without
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论