Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
23dbc210
提交
23dbc210
authored
1月 25, 2017
作者:
Benjamin Scellier
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
file theano/compile/tests/test_pfunc.py
上级
b14960b4
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
125 行增加
和
125 行删除
+125
-125
test_pfunc.py
theano/compile/tests/test_pfunc.py
+125
-125
没有找到文件。
theano/compile/tests/test_pfunc.py
浏览文件 @
23dbc210
...
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, print_function, division
import
unittest
from
nose.plugins.skip
import
SkipTest
import
numpy
import
numpy
as
np
import
theano
from
theano.tensor
import
dmatrix
,
iscalar
,
lscalar
,
dmatrices
...
...
@@ -50,7 +50,7 @@ class Test_pfunc(unittest.TestCase):
def
test_shared
(
self
):
# CHECK: two functions (f1 and f2) can share w
w
=
shared
(
n
umpy
.
random
.
rand
(
2
,
2
),
'w'
)
w
=
shared
(
n
p
.
random
.
rand
(
2
,
2
),
'w'
)
wval
=
w
.
get_value
(
borrow
=
False
)
x
=
dmatrix
()
...
...
@@ -58,24 +58,24 @@ class Test_pfunc(unittest.TestCase):
out2
=
w
*
x
f1
=
pfunc
([
x
],
[
out1
])
f2
=
pfunc
([
x
],
[
out2
])
xval
=
n
umpy
.
random
.
rand
(
2
,
2
)
assert
n
umpy
.
all
(
f1
(
xval
)
==
xval
+
wval
)
assert
n
umpy
.
all
(
f2
(
xval
)
==
xval
*
wval
)
xval
=
n
p
.
random
.
rand
(
2
,
2
)
assert
n
p
.
all
(
f1
(
xval
)
==
xval
+
wval
)
assert
n
p
.
all
(
f2
(
xval
)
==
xval
*
wval
)
# CHECK: updating a shared value
f3
=
pfunc
([
x
],
out1
,
updates
=
[(
w
,
(
w
-
1
))])
# f3 changes the value of w
assert
n
umpy
.
all
(
f3
(
xval
)
==
xval
+
wval
)
assert
n
p
.
all
(
f3
(
xval
)
==
xval
+
wval
)
# this same value is read by f1
assert
n
umpy
.
all
(
f1
(
xval
)
==
xval
+
(
wval
-
1
))
assert
n
p
.
all
(
f1
(
xval
)
==
xval
+
(
wval
-
1
))
w
.
set_value
(
w
.
get_value
(
borrow
=
True
)
*
10
,
borrow
=
True
)
# this same value is read by f1
assert
n
umpy
.
all
(
f1
(
xval
)
==
xval
+
w
.
get_value
(
borrow
=
True
))
assert
n
p
.
all
(
f1
(
xval
)
==
xval
+
w
.
get_value
(
borrow
=
True
))
def
test_no_shared_as_input
(
self
):
"""Test that shared variables cannot be used as function inputs."""
w_init
=
n
umpy
.
random
.
rand
(
2
,
2
)
w_init
=
n
p
.
random
.
rand
(
2
,
2
)
w
=
shared
(
w_init
.
copy
(),
'w'
)
try
:
pfunc
([
w
],
theano
.
tensor
.
sum
(
w
*
w
))
...
...
@@ -89,16 +89,16 @@ class Test_pfunc(unittest.TestCase):
# Ensure it is possible to (implicitly) use a shared variable in a
# function, as a 'state' that can be updated at will.
rng
=
n
umpy
.
random
.
RandomState
(
1827
)
rng
=
n
p
.
random
.
RandomState
(
1827
)
w_init
=
rng
.
rand
(
5
)
w
=
shared
(
w_init
.
copy
(),
'w'
)
reg
=
theano
.
tensor
.
sum
(
w
*
w
)
f
=
pfunc
([],
reg
)
assert
f
()
==
n
umpy
.
sum
(
w_init
*
w_init
)
assert
f
()
==
n
p
.
sum
(
w_init
*
w_init
)
# Change the value of w and ensure the output changes accordingly.
w
.
set_value
(
w
.
get_value
(
borrow
=
True
)
+
1.0
,
borrow
=
True
)
assert
f
()
==
n
umpy
.
sum
((
w_init
+
1
)
**
2
)
assert
f
()
==
n
p
.
sum
((
w_init
+
1
)
**
2
)
def
test_default_scalar_container
(
self
):
# Similar in spirit to test_default_container, but updating a scalar
...
...
@@ -117,14 +117,14 @@ class Test_pfunc(unittest.TestCase):
f
=
pfunc
([
In
(
a
,
strict
=
False
)],
[
out
])
# works, rand generates float64 by default
f
(
n
umpy
.
random
.
rand
(
8
))
f
(
n
p
.
random
.
rand
(
8
))
# works, casting is allowed
f
(
n
umpy
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
(
n
p
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
=
pfunc
([
In
(
a
,
strict
=
True
)],
[
out
])
try
:
# fails, f expects float64
f
(
n
umpy
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
f
(
n
p
.
array
([
1
,
2
,
3
,
4
],
dtype
=
'int32'
))
except
TypeError
:
pass
...
...
@@ -134,20 +134,20 @@ class Test_pfunc(unittest.TestCase):
# using mutable=True will let fip change the value in aval
fip
=
pfunc
([
In
(
a
,
mutable
=
True
)],
[
a_out
],
mode
=
'FAST_RUN'
)
aval
=
n
umpy
.
random
.
rand
(
10
)
aval
=
n
p
.
random
.
rand
(
10
)
aval2
=
aval
.
copy
()
assert
n
umpy
.
all
(
fip
(
aval
)
==
(
aval2
*
2
))
assert
not
n
umpy
.
all
(
aval
==
aval2
)
assert
n
p
.
all
(
fip
(
aval
)
==
(
aval2
*
2
))
assert
not
n
p
.
all
(
aval
==
aval2
)
# using mutable=False should leave the input untouched
f
=
pfunc
([
In
(
a
,
mutable
=
False
)],
[
a_out
],
mode
=
'FAST_RUN'
)
aval
=
n
umpy
.
random
.
rand
(
10
)
aval
=
n
p
.
random
.
rand
(
10
)
aval2
=
aval
.
copy
()
assert
n
umpy
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
n
umpy
.
all
(
aval
==
aval2
)
assert
n
p
.
all
(
f
(
aval
)
==
(
aval2
*
2
))
assert
n
p
.
all
(
aval
==
aval2
)
def
test_shared_mutable
(
self
):
bval
=
n
umpy
.
arange
(
5
)
bval
=
n
p
.
arange
(
5
)
b
=
shared
(
bval
)
b_out
=
b
*
2
...
...
@@ -158,26 +158,26 @@ class Test_pfunc(unittest.TestCase):
# by default, shared are not mutable unless doing an explicit update
f
=
pfunc
([],
[
b_out
],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
n
umpy
.
arange
(
5
)
*
2
)
.
all
()
assert
n
umpy
.
all
(
b
.
get_value
(
borrow
=
True
)
==
numpy
.
arange
(
5
))
assert
(
f
()
==
n
p
.
arange
(
5
)
*
2
)
.
all
()
assert
n
p
.
all
(
b
.
get_value
(
borrow
=
True
)
==
np
.
arange
(
5
))
# using updates, b is now a mutable parameter
f
=
pfunc
([],
[
b_out
],
updates
=
[(
b
,
b_out
)],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
assert
(
f
()
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
# because of the update
assert
(
b
.
get_value
(
borrow
=
True
)
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
assert
(
bval
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
# because of mutable=True
assert
(
b
.
get_value
(
borrow
=
True
)
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
assert
(
bval
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
# because of mutable=True
# do not depend on updates being in-place though!
bval
=
n
umpy
.
arange
(
5
)
bval
=
n
p
.
arange
(
5
)
b
.
set_value
(
bval
,
borrow
=
True
)
bval
=
data_of
(
b
)
f
=
pfunc
([],
[
b_out
],
updates
=
[(
b
,
(
b_out
+
3
))],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
(
n
umpy
.
arange
(
5
)
*
2
))
.
all
()
assert
(
f
()
==
(
n
p
.
arange
(
5
)
*
2
))
.
all
()
# because of the update
assert
(
b
.
get_value
(
borrow
=
True
)
==
((
n
umpy
.
arange
(
5
)
*
2
)
+
3
))
.
all
()
assert
(
b
.
get_value
(
borrow
=
True
)
==
((
n
p
.
arange
(
5
)
*
2
)
+
3
))
.
all
()
# bval got modified to something...
assert
not
(
bval
==
n
umpy
.
arange
(
5
))
.
all
()
assert
not
(
bval
==
n
p
.
arange
(
5
))
.
all
()
# ... but not to b.value !
assert
not
(
bval
==
b
.
get_value
(
borrow
=
True
))
.
all
()
...
...
@@ -192,16 +192,16 @@ class Test_pfunc(unittest.TestCase):
# Both values are in range. Since they're not ndarrays (but lists),
# they will be converted, and their value checked.
assert
n
umpy
.
all
(
f
([
3
],
[
6
],
1
)
==
10
)
assert
n
p
.
all
(
f
([
3
],
[
6
],
1
)
==
10
)
# Values are in range, but a dtype too large has explicitly been given
# For performance reasons, no check of the data is explicitly performed
# (It might be OK to change this in the future.)
self
.
assertRaises
(
TypeError
,
f
,
[
3
],
n
umpy
.
array
([
6
],
dtype
=
'int16'
),
1
)
[
3
],
n
p
.
array
([
6
],
dtype
=
'int16'
),
1
)
# Value too big for a, silently ignored
assert
n
umpy
.
all
(
f
([
2
**
20
],
numpy
.
ones
(
1
,
dtype
=
'int8'
),
1
)
==
2
)
assert
n
p
.
all
(
f
([
2
**
20
],
np
.
ones
(
1
,
dtype
=
'int8'
),
1
)
==
2
)
# Value too big for b, raises TypeError
self
.
assertRaises
(
TypeError
,
f
,
[
3
],
[
312
],
1
)
...
...
@@ -220,17 +220,17 @@ class Test_pfunc(unittest.TestCase):
(
a
+
b
+
c
))
# If the values can be accurately represented, everything is OK
assert
n
umpy
.
all
(
f
(
0
,
0
,
0
)
==
0
)
assert
n
p
.
all
(
f
(
0
,
0
,
0
)
==
0
)
# If allow_downcast is True, idem
assert
n
umpy
.
allclose
(
f
(
0.1
,
0
,
0
),
0.1
)
assert
n
p
.
allclose
(
f
(
0.1
,
0
,
0
),
0.1
)
# If allow_downcast is False, nope
self
.
assertRaises
(
TypeError
,
f
,
0
,
0.1
,
0
)
# If allow_downcast is None, it should work iff floatX=float32
if
config
.
floatX
==
'float32'
:
assert
n
umpy
.
allclose
(
f
(
0
,
0
,
0.1
),
0.1
)
assert
n
p
.
allclose
(
f
(
0
,
0
,
0.1
),
0.1
)
else
:
self
.
assertRaises
(
TypeError
,
f
,
0
,
0
,
0.1
)
...
...
@@ -246,10 +246,10 @@ class Test_pfunc(unittest.TestCase):
# If the values can be accurately represented, everything is OK
z
=
[
0
]
assert
n
umpy
.
all
(
f
(
z
,
z
,
z
)
==
0
)
assert
n
p
.
all
(
f
(
z
,
z
,
z
)
==
0
)
# If allow_downcast is True, idem
assert
n
umpy
.
allclose
(
f
([
0.1
],
z
,
z
),
0.1
)
assert
n
p
.
allclose
(
f
([
0.1
],
z
,
z
),
0.1
)
# If allow_downcast is False, nope
self
.
assertRaises
(
TypeError
,
f
,
z
,
[
0.1
],
z
)
...
...
@@ -271,22 +271,22 @@ class Test_pfunc(unittest.TestCase):
g
=
pfunc
([
a
,
b
,
c
],
(
a
+
b
+
c
),
allow_input_downcast
=
False
)
# All values are in range. Since they're not ndarrays (but lists
# or scalars), they will be converted, and their value checked.
assert
n
umpy
.
all
(
g
([
3
],
[
6
],
0
)
==
9
)
assert
n
p
.
all
(
g
([
3
],
[
6
],
0
)
==
9
)
# Values are in range, but a dtype too large has explicitly been given
# For performance reasons, no check of the data is explicitly performed
# (It might be OK to change this in the future.)
self
.
assertRaises
(
TypeError
,
g
,
[
3
],
n
umpy
.
array
([
6
],
dtype
=
'int16'
),
0
)
[
3
],
n
p
.
array
([
6
],
dtype
=
'int16'
),
0
)
# Value too big for b, raises TypeError
self
.
assertRaises
(
TypeError
,
g
,
[
3
],
[
312
],
0
)
h
=
pfunc
([
a
,
b
,
c
],
(
a
+
b
+
c
))
# Default: allow_input_downcast=None
# Everything here should behave like with False
assert
n
umpy
.
all
(
h
([
3
],
[
6
],
0
)
==
9
)
assert
n
p
.
all
(
h
([
3
],
[
6
],
0
)
==
9
)
self
.
assertRaises
(
TypeError
,
h
,
[
3
],
n
umpy
.
array
([
6
],
dtype
=
'int16'
),
0
)
[
3
],
n
p
.
array
([
6
],
dtype
=
'int16'
),
0
)
self
.
assertRaises
(
TypeError
,
h
,
[
3
],
[
312
],
0
)
def
test_allow_downcast_floatX
(
self
):
...
...
@@ -298,21 +298,21 @@ class Test_pfunc(unittest.TestCase):
h
=
pfunc
([
a
,
b
],
(
a
+
b
),
allow_input_downcast
=
None
)
# If the values can be accurately represented, OK
assert
n
umpy
.
all
(
f
(
0
,
[
0
])
==
0
)
assert
n
umpy
.
all
(
g
(
0
,
[
0
])
==
0
)
assert
n
umpy
.
all
(
h
(
0
,
[
0
])
==
0
)
assert
n
p
.
all
(
f
(
0
,
[
0
])
==
0
)
assert
n
p
.
all
(
g
(
0
,
[
0
])
==
0
)
assert
n
p
.
all
(
h
(
0
,
[
0
])
==
0
)
# For the vector: OK iff allow_input_downcast is True
assert
n
umpy
.
allclose
(
f
(
0
,
[
0.1
]),
0.1
)
assert
n
p
.
allclose
(
f
(
0
,
[
0.1
]),
0.1
)
self
.
assertRaises
(
TypeError
,
g
,
0
,
[
0.1
])
self
.
assertRaises
(
TypeError
,
h
,
0
,
[
0.1
])
# For the scalar: OK if allow_input_downcast is True,
# or None and floatX==float32
assert
n
umpy
.
allclose
(
f
(
0.1
,
[
0
]),
0.1
)
assert
n
p
.
allclose
(
f
(
0.1
,
[
0
]),
0.1
)
self
.
assertRaises
(
TypeError
,
g
,
0.1
,
[
0
])
if
config
.
floatX
==
'float32'
:
assert
n
umpy
.
allclose
(
h
(
0.1
,
[
0
]),
0.1
)
assert
n
p
.
allclose
(
h
(
0.1
,
[
0
]),
0.1
)
else
:
self
.
assertRaises
(
TypeError
,
h
,
0.1
,
[
0
])
...
...
@@ -340,7 +340,7 @@ class Test_pfunc(unittest.TestCase):
def
test_update_err_broadcast
(
self
):
# Test that broadcastable dimensions raise error
data
=
n
umpy
.
random
.
rand
(
10
,
10
)
.
astype
(
'float32'
)
data
=
n
p
.
random
.
rand
(
10
,
10
)
.
astype
(
'float32'
)
output_var
=
shared
(
name
=
"output"
,
value
=
data
)
# the update_var has type matrix, and the update expression
...
...
@@ -350,7 +350,7 @@ class Test_pfunc(unittest.TestCase):
def
test_duplicate_updates
(
self
):
x
,
y
=
dmatrices
(
'x'
,
'y'
)
z
=
shared
(
n
umpy
.
ones
((
2
,
3
)))
z
=
shared
(
n
p
.
ones
((
2
,
3
)))
self
.
assertRaises
(
ValueError
,
theano
.
function
,
[
x
,
y
],
[
z
],
updates
=
[(
z
,
(
z
+
x
+
y
)),
(
z
,
(
z
-
x
))])
...
...
@@ -362,29 +362,29 @@ class Test_pfunc(unittest.TestCase):
y
=
tensor
.
ivector
()
f
=
pfunc
([
y
],
(
y
*
x
),
givens
=
{
x
:
6
})
assert
n
umpy
.
all
(
f
([
1
,
1
,
1
])
==
[
6
,
6
,
6
])
assert
n
p
.
all
(
f
([
1
,
1
,
1
])
==
[
6
,
6
,
6
])
assert
x
.
get_value
()
==
0
z
=
tensor
.
ivector
()
c
=
z
*
y
f
=
pfunc
([
y
],
(
c
+
7
),
givens
=
{
z
:
theano
.
_asarray
([
4
,
4
,
4
],
dtype
=
'int32'
)})
assert
n
umpy
.
all
(
f
([
1
,
1
,
1
])
==
[
11
,
11
,
11
])
assert
n
p
.
all
(
f
([
1
,
1
,
1
])
==
[
11
,
11
,
11
])
assert
x
.
get_value
()
==
0
def
test_clone0
(
self
):
x
=
shared
(
n
umpy
.
asarray
([
4
,
4
,
4
]))
y
=
shared
(
n
umpy
.
asarray
([
4
,
4
,
4
]))
z
=
shared
(
n
umpy
.
asarray
([
2
,
2
,
2
]))
x
=
shared
(
n
p
.
asarray
([
4
,
4
,
4
]))
y
=
shared
(
n
p
.
asarray
([
4
,
4
,
4
]))
z
=
shared
(
n
p
.
asarray
([
2
,
2
,
2
]))
up
=
pfunc
([],
[],
updates
=
{
x
:
(
x
*
5
),
y
:
((
x
*
5
)
+
y
),
z
:
(((
x
*
5
)
+
y
)
**
z
)})
up
()
assert
n
umpy
.
all
(
x
.
get_value
()
==
20
)
assert
n
umpy
.
all
(
y
.
get_value
()
==
24
)
assert
n
umpy
.
all
(
z
.
get_value
()
==
(
24
**
2
))
assert
n
p
.
all
(
x
.
get_value
()
==
20
)
assert
n
p
.
all
(
y
.
get_value
()
==
24
)
assert
n
p
.
all
(
z
.
get_value
()
==
(
24
**
2
))
def
test_default_updates
(
self
):
x
=
shared
(
0
)
...
...
@@ -625,7 +625,7 @@ class Test_pfunc(unittest.TestCase):
# There was a bug in CVM, triggered when a shared variable
# was its own update expression.
a
=
shared
(
1.
,
'a'
)
b
=
shared
(
n
umpy
.
ones
((
2
,
3
)),
'b'
)
b
=
shared
(
n
p
.
ones
((
2
,
3
)),
'b'
)
# The order of the variables is not determined, so we try
# both shared variables.
...
...
@@ -650,7 +650,7 @@ class Test_pfunc(unittest.TestCase):
# Like test_update_same, but the update expression is simplified until
# it is found to be equal to the original variable
a
=
shared
(
1.
,
'a'
)
b
=
shared
(
n
umpy
.
ones
((
2
,
3
)),
'b'
)
b
=
shared
(
n
p
.
ones
((
2
,
3
)),
'b'
)
# See comment in test_update_same about why we try both
# shared variables.
...
...
@@ -695,12 +695,12 @@ class Test_aliasing_rules(unittest.TestCase):
def
test_shared_constructor_copies
(
self
):
# shared constructor makes copy
# (rule #2)
orig_a
=
n
umpy
.
zeros
((
2
,
2
))
orig_a
=
n
p
.
zeros
((
2
,
2
))
A
=
self
.
shared
(
orig_a
)
assert
not
n
umpy
.
may_share_memory
(
orig_a
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
orig_a
,
data_of
(
A
))
# rule #2 reading back from theano-managed memory
assert
not
n
umpy
.
may_share_memory
(
A
.
get_value
(
borrow
=
False
),
assert
not
n
p
.
may_share_memory
(
A
.
get_value
(
borrow
=
False
),
data_of
(
A
))
def
test_sparse_input_aliasing_affecting_inplace_operations
(
self
):
...
...
@@ -732,7 +732,7 @@ class Test_aliasing_rules(unittest.TestCase):
# Test 1. If the same variable is given twice
# Compute bogus values
m
=
sp
.
csc_matrix
(
n
umpy
.
asarray
(
m
=
sp
.
csc_matrix
(
n
p
.
asarray
(
[[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
...
...
@@ -742,7 +742,7 @@ class Test_aliasing_rules(unittest.TestCase):
# Since we used inplace operation v and m may be corrupted
# so we need to recreate them
m
=
sp
.
csc_matrix
(
n
umpy
.
asarray
(
m
=
sp
.
csc_matrix
(
n
p
.
asarray
(
[[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
...
...
@@ -751,7 +751,7 @@ class Test_aliasing_rules(unittest.TestCase):
m_copy
=
m
.
copy
()
vals
=
f
(
m
,
m_copy
)
assert
n
umpy
.
allclose
(
vals
.
todense
(),
bogus_vals
.
todense
())
assert
n
p
.
allclose
(
vals
.
todense
(),
bogus_vals
.
todense
())
def
test_input_aliasing_affecting_inplace_operations
(
self
):
...
...
@@ -771,8 +771,8 @@ class Test_aliasing_rules(unittest.TestCase):
# Test 1. If the same variable is given twice
# Compute bogus values
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
[
0
,
0
,
0
,
1
,
0
],
...
...
@@ -781,8 +781,8 @@ class Test_aliasing_rules(unittest.TestCase):
# Since we used inplace operation v and m may be corrupted
# so we need to recreate them
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
],
[
0
,
0
,
1
,
0
,
0
],
[
0
,
0
,
0
,
1
,
0
],
...
...
@@ -791,7 +791,7 @@ class Test_aliasing_rules(unittest.TestCase):
v_copy
=
v
.
copy
()
vals
=
f
(
v
,
v_copy
,
m
,
m_copy
)
assert
n
umpy
.
allclose
(
vals
,
bogus_vals
)
assert
n
p
.
allclose
(
vals
,
bogus_vals
)
def
test_partial_input_aliasing_affecting_inplace_operations
(
self
):
...
...
@@ -822,15 +822,15 @@ class Test_aliasing_rules(unittest.TestCase):
theano
.
dot
((
z
*
4
),
m3
)))
# Compute bogus values
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
],
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
],
[
0
,
1
]],
dtype
=
'float64'
)
bogus_vals
=
f
(
v
[:
2
],
v
[
1
:
3
],
v
[
2
:
4
],
m
,
m
,
m
)
# Since we used inplace operation v and m may be corrupted
# so we need to recreate them
v
=
n
umpy
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
umpy
.
asarray
([[
1
,
0
],
v
=
n
p
.
asarray
([
1
,
2
,
3
,
4
,
5
],
dtype
=
'float64'
)
m
=
n
p
.
asarray
([[
1
,
0
],
[
0
,
1
]],
dtype
=
'float64'
)
m_copy1
=
m
.
copy
()
v_copy1
=
v
.
copy
()
...
...
@@ -838,86 +838,86 @@ class Test_aliasing_rules(unittest.TestCase):
v_copy2
=
v
.
copy
()
vals
=
f
(
v
[:
2
],
v_copy1
[
1
:
3
],
v_copy2
[
2
:
4
],
m
,
m_copy1
,
m_copy2
)
assert
n
umpy
.
allclose
(
vals
,
bogus_vals
)
assert
n
p
.
allclose
(
vals
,
bogus_vals
)
def
test_potential_output_aliasing_induced_by_updates
(
self
):
A
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
)))
B
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
)))
C
=
n
umpy
.
zeros
((
2
,
2
))
A
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
)))
B
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
)))
C
=
n
p
.
zeros
((
2
,
2
))
D
=
tensor
.
dmatrix
()
DD
=
D
+
5
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
D
),
(
B
,
D
)])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
D
[:]),
(
B
,
D
)])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
(
D
+
5
)),
(
B
,
D
[:])])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
[],
updates
=
[(
A
,
(
D
+
5
)),
(
B
,
D
)])
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
f
=
pfunc
([
D
],
DD
,
updates
=
[(
A
,
DD
[:
1
]),
(
B
,
DD
)])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
f
=
pfunc
([
D
],
DD
,
updates
=
[(
A
,
DD
[:
1
]),
(
B
,
(
DD
[:
1
]
*
2
))])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
f
=
pfunc
([
D
],
(
DD
*
4
),
updates
=
[(
A
,
(
DD
[:
1
]
*
3
)),
(
B
,
(
DD
[:
1
]
*
2
))])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
f
=
pfunc
([
D
],
(
DD
*
4
),
updates
=
[(
A
,
(
DD
[:
1
]
*
3
)),
(
B
,
(
DD
[:
1
]
*
3
))])
R
=
f
(
C
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
umpy
.
may_share_memory
(
R
,
data_of
(
A
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
R
,
data_of
(
A
))
def
test_no_aliasing_0
(
self
):
# B is a shared variable, A is updated with B's contents
# we need A to be copied to avoid aliasing
A
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
-
.
5
)
A
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
-
.
5
)
f
=
pfunc
([],
[],
updates
=
[(
A
,
B
)])
f
()
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
def
test_no_aliasing_1
(
self
):
# B is a shared variable, A is updated with B's contents
# since B is being updated as well, we don't need to copy anything
# to avoid aliasing shared variables.
A
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
umpy
.
zeros
((
2
,
2
))
-
.
5
)
A
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
+
.
5
)
B
=
self
.
shared
(
n
p
.
zeros
((
2
,
2
))
-
.
5
)
C
=
tensor
.
dmatrix
()
f
=
pfunc
([
C
],
[],
updates
=
[(
A
,
B
),
(
B
,
C
)])
z
=
n
umpy
.
zeros
((
2
,
2
))
z
=
n
p
.
zeros
((
2
,
2
))
f
(
z
)
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
# Theano tries to maintain its own memory space.
assert
not
n
umpy
.
may_share_memory
(
z
,
data_of
(
B
))
assert
n
umpy
.
all
(
data_of
(
B
)
==
z
)
assert
not
n
p
.
may_share_memory
(
z
,
data_of
(
B
))
assert
n
p
.
all
(
data_of
(
B
)
==
z
)
def
test_no_aliasing_2
(
self
):
# B and A take one another's values
# no copying is necessary since each one is updated.
orig_a
=
n
umpy
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
umpy
.
zeros
((
2
,
2
))
-
.
5
orig_a
=
n
p
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
p
.
zeros
((
2
,
2
))
-
.
5
A
=
self
.
shared
(
orig_a
)
B
=
self
.
shared
(
orig_b
)
...
...
@@ -927,15 +927,15 @@ class Test_aliasing_rules(unittest.TestCase):
f
=
pfunc
([],
[],
updates
=
[(
A
,
B
),
(
B
,
A
)])
f
()
# correctness
assert
n
umpy
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
umpy
.
all
(
data_of
(
B
)
==
+.
5
)
assert
n
p
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
p
.
all
(
data_of
(
B
)
==
+.
5
)
# shared vars may not be aliased
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
# theano should have been smart enough to not make copies
assert
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
umpy
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
assert
n
p
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
p
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
def
test_no_aliasing_2b
(
self
):
# B and A take one another's values
...
...
@@ -943,8 +943,8 @@ class Test_aliasing_rules(unittest.TestCase):
# The twist one `test_no_aliasing_2` is that each shared var is updated
# with a view of the other one.
orig_a
=
n
umpy
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
umpy
.
zeros
((
2
,
2
))
-
.
5
orig_a
=
n
p
.
zeros
((
2
,
2
))
+
.
5
orig_b
=
n
p
.
zeros
((
2
,
2
))
-
.
5
A
=
self
.
shared
(
orig_a
)
B
=
self
.
shared
(
orig_b
)
...
...
@@ -955,30 +955,30 @@ class Test_aliasing_rules(unittest.TestCase):
# theano.printing.debugprint(f)
f
()
# correctness (doesn't actually test the view...)
assert
n
umpy
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
umpy
.
all
(
data_of
(
B
)
==
+.
5
)
assert
n
p
.
all
(
data_of
(
A
)
==
-.
5
)
assert
n
p
.
all
(
data_of
(
B
)
==
+.
5
)
# shared vars may not be aliased
assert
not
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
assert
not
n
p
.
may_share_memory
(
data_of
(
A
),
data_of
(
B
))
# theano should have been smart enough to not make copies
if
theano
.
config
.
mode
not
in
[
'DebugMode'
,
'DEBUG_MODE'
,
'FAST_COMPILE'
]:
# We don't ask DebugMode and FAST_COMPILE not to make copy.
# We have the right to do so.
assert
n
umpy
.
all
(
data_of
(
A
)
<
5
)
assert
n
p
.
all
(
data_of
(
A
)
<
5
)
data_of_b
+=
10
assert
n
umpy
.
all
(
data_of
(
A
)
>
5
)
assert
n
p
.
all
(
data_of
(
A
)
>
5
)
data_of_b
-=
10
assert
n
umpy
.
all
(
data_of
(
B
)
<
5
)
assert
n
p
.
all
(
data_of
(
B
)
<
5
)
data_of_a
+=
10
assert
n
umpy
.
all
(
data_of
(
B
)
>
5
)
assert
n
p
.
all
(
data_of
(
B
)
>
5
)
data_of_a
-=
10
# N.B. may_share_memory is what we mean, but does it work?
assert
n
umpy
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
umpy
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
assert
n
p
.
may_share_memory
(
data_of
(
A
),
data_of_b
)
assert
n
p
.
may_share_memory
(
data_of
(
B
),
data_of_a
)
# N.B. This pattern could form a memory leak - each shared
# variable always points to a view, and that view gets
...
...
@@ -995,9 +995,9 @@ class Test_rebuild_strict(unittest.TestCase):
x
,
y
=
tensor
.
ivectors
(
'x'
,
'y'
)
z
=
x
*
y
f
=
theano
.
function
([
w
,
y
],
z
,
givens
=
[(
x
,
w
)],
rebuild_strict
=
False
)
z_val
=
f
(
n
umpy
.
ones
((
3
,
5
),
dtype
=
'int32'
),
numpy
.
arange
(
5
,
dtype
=
'int32'
))
z_val
=
f
(
n
p
.
ones
((
3
,
5
),
dtype
=
'int32'
),
np
.
arange
(
5
,
dtype
=
'int32'
))
assert
z_val
.
ndim
==
2
assert
n
umpy
.
all
(
z_val
==
numpy
.
ones
((
3
,
5
))
*
numpy
.
arange
(
5
))
assert
n
p
.
all
(
z_val
==
np
.
ones
((
3
,
5
))
*
np
.
arange
(
5
))
if
__name__
==
'__main__'
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论