Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f5ed1817
提交
f5ed1817
authored
3月 15, 2011
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update tests to use .get_value() and .set_value() on shared variables.
上级
27084539
隐藏空白字符变更
内嵌
并排
正在显示
12 个修改的文件
包含
222 行增加
和
192 行删除
+222
-192
test_pfunc.py
theano/compile/tests/test_pfunc.py
+101
-98
test_shared.py
theano/compile/tests/test_shared.py
+30
-26
test_basic_ops.py
theano/sandbox/cuda/tests/test_basic_ops.py
+8
-8
test_blas.py
theano/sandbox/cuda/tests/test_blas.py
+9
-9
test_mlp.py
theano/sandbox/cuda/tests/test_mlp.py
+3
-3
test_neighbours.py
theano/sandbox/test_neighbours.py
+9
-9
mlp_test.py
theano/tensor/tests/mlp_test.py
+3
-3
test_blas.py
theano/tensor/tests/test_blas.py
+17
-13
test_opt.py
theano/tensor/tests/test_opt.py
+1
-1
test_shared_randomstreams.py
theano/tensor/tests/test_shared_randomstreams.py
+2
-1
test_sharedvar.py
theano/tensor/tests/test_sharedvar.py
+31
-15
test_scan.py
theano/tests/test_scan.py
+8
-6
没有找到文件。
theano/compile/tests/test_pfunc.py
浏览文件 @
f5ed1817
...
...
@@ -27,12 +27,12 @@ class Test_pfunc(unittest.TestCase):
b
=
shared
(
1
)
f1
=
pfunc
([
a
],
a
+
b
)
f2
=
pfunc
([
Param
(
a
,
default
=
44
)],
a
+
b
,
updates
=
{
b
:
b
+
1
})
self
.
failUnless
(
b
.
value
==
1
)
self
.
failUnless
(
b
.
get_value
()
==
1
)
self
.
failUnless
(
f1
(
3
)
==
4
)
self
.
failUnless
(
f2
(
3
)
==
4
)
self
.
failUnless
(
b
.
value
==
2
)
self
.
failUnless
(
b
.
get_value
()
==
2
)
self
.
failUnless
(
f1
(
3
)
==
5
)
b
.
value
=
0
b
.
set_value
(
0
)
self
.
failUnless
(
f1
(
3
)
==
3
)
# Example #2.
...
...
@@ -41,7 +41,7 @@ class Test_pfunc(unittest.TestCase):
f1
=
pfunc
([
a
],
a
+
b
)
f2
=
pfunc
([
a
],
a
*
b
)
self
.
failUnless
(
f1
(
5
)
==
12
)
b
.
value
=
8
b
.
set_value
(
8
)
self
.
failUnless
(
f1
(
5
)
==
13
)
self
.
failUnless
(
f2
(
4
)
==
32
)
...
...
@@ -49,7 +49,7 @@ class Test_pfunc(unittest.TestCase):
# CHECK: two functions (f1 and f2) can share w
w
=
shared
(
numpy
.
random
.
rand
(
2
,
2
),
'w'
)
wval
=
copy
.
copy
(
w
.
valu
e
)
wval
=
w
.
get_value
(
borrow
=
Fals
e
)
x
=
dmatrix
()
out1
=
w
+
x
...
...
@@ -65,8 +65,9 @@ class Test_pfunc(unittest.TestCase):
assert
numpy
.
all
(
f3
(
xval
)
==
xval
+
wval
)
# f3 changes the value of w
assert
numpy
.
all
(
f1
(
xval
)
==
xval
+
(
wval
-
1
))
# this same value is read by f1
w
.
value
*=
10
assert
numpy
.
all
(
f1
(
xval
)
==
xval
+
w
.
value
)
# this same value is read by f1
w
.
set_value
(
w
.
get_value
(
borrow
=
True
)
*
10
,
borrow
=
True
)
# this same value is read by f1
assert
numpy
.
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."""
...
...
@@ -92,7 +93,7 @@ class Test_pfunc(unittest.TestCase):
assert
f
()
==
numpy
.
sum
(
w_init
*
w_init
)
# Change the value of w and ensure the output changes accordingly.
w
.
value
+=
1.0
w
.
set_value
(
w
.
get_value
(
borrow
=
True
)
+
1.0
,
borrow
=
True
)
assert
f
()
==
numpy
.
sum
((
w_init
+
1
)
**
2
)
def
test_default_scalar_container
(
self
):
...
...
@@ -101,7 +102,7 @@ class Test_pfunc(unittest.TestCase):
x
=
shared
(
0.0
,
'x'
)
f
=
pfunc
([],
x
)
assert
f
()
==
0
x
.
value
+=
1
x
.
set_value
(
x
.
get_value
(
borrow
=
True
)
+
1
,
borrow
=
True
)
assert
f
()
==
1
def
test_param_strict
(
self
):
...
...
@@ -146,31 +147,33 @@ class Test_pfunc(unittest.TestCase):
b
=
shared
(
bval
)
b_out
=
b
*
2
assert
b
.
value
is
not
bval
# shared vars copy args.
assert
b
.
get_value
(
borrow
=
True
)
is
not
bval
# shared vars copy args.
bval
=
data_of
(
b
)
# so we do this to get at the underlying data
# by default, shared are not mutable unless doing an explicit update
f
=
pfunc
([],
[
b_out
],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
assert
numpy
.
all
(
b
.
value
==
numpy
.
arange
(
5
))
assert
numpy
.
all
(
b
.
get_value
(
borrow
=
True
)
==
numpy
.
arange
(
5
))
# using updates, b is now a mutable parameter
f
=
pfunc
([],
[
b_out
],
updates
=
[(
b
,
b_out
)],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
assert
(
b
.
value
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
# because of the update
assert
(
bval
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
# because of mutable=True
# because of the update
assert
(
b
.
get_value
(
borrow
=
True
)
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
assert
(
bval
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
# because of mutable=True
# do not depend on updates being in-place though!
bval
=
numpy
.
arange
(
5
)
b
.
value
=
bval
b
.
set_value
(
bval
,
borrow
=
True
)
bval
=
data_of
(
b
)
f
=
pfunc
([],
[
b_out
],
updates
=
[(
b
,
b_out
+
3
)],
mode
=
'FAST_RUN'
)
assert
(
f
()
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
assert
(
b
.
value
==
((
numpy
.
arange
(
5
)
*
2
)
+
3
))
.
all
()
# because of the update
assert
(
f
()
==
numpy
.
arange
(
5
)
*
2
)
.
all
()
# because of the update
assert
(
b
.
get_value
(
borrow
=
True
)
==
((
numpy
.
arange
(
5
)
*
2
)
+
3
))
.
all
()
# bval got modified to something...
assert
not
(
bval
==
numpy
.
arange
(
5
))
.
all
()
# ... but not to b.value !
assert
not
(
bval
==
b
.
value
)
.
all
()
assert
not
(
bval
==
b
.
get_value
(
borrow
=
True
)
)
.
all
()
def
test_param_allow_downcast_int
(
self
):
a
=
tensor
.
wvector
(
'a'
)
# int16
...
...
@@ -314,20 +317,20 @@ class Test_pfunc(unittest.TestCase):
x
=
shared
(
0
)
assign
=
pfunc
([],
[],
updates
=
{
x
:
3
})
assign
()
self
.
failUnless
(
x
.
value
==
3
)
self
.
failUnless
(
x
.
get_value
()
==
3
)
# Basic increment function.
x
.
value
=
0
x
.
set_value
(
0
)
inc
=
pfunc
([],
[],
updates
=
{
x
:
x
+
1
})
inc
()
self
.
failUnless
(
x
.
value
==
1
)
self
.
failUnless
(
x
.
get_value
()
==
1
)
# Increment by a constant value.
x
.
value
=
-
1
x
.
set_value
(
-
1
)
y
=
shared
(
2
)
inc_by_y
=
pfunc
([],
[],
updates
=
{
x
:
x
+
y
})
inc_by_y
()
self
.
failUnless
(
x
.
value
==
1
)
self
.
failUnless
(
x
.
get_value
()
==
1
)
def
test_duplicate_updates
(
self
):
x
,
y
=
dmatrices
(
'x'
,
'y'
)
...
...
@@ -338,18 +341,18 @@ class Test_pfunc(unittest.TestCase):
x
=
shared
(
0
)
assign
=
pfunc
([],
x
,
givens
=
{
x
:
3
})
assert
assign
()
==
3
assert
x
.
value
==
0
assert
x
.
get_value
(
borrow
=
True
)
==
0
y
=
tensor
.
ivector
()
f
=
pfunc
([
y
],
y
*
x
,
givens
=
{
x
:
6
})
assert
numpy
.
all
(
f
([
1
,
1
,
1
])
==
[
6
,
6
,
6
])
assert
x
.
value
==
0
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
numpy
.
all
(
f
([
1
,
1
,
1
])
==
[
11
,
11
,
11
])
assert
x
.
value
==
0
assert
x
.
get_value
()
==
0
def
test_clone0
(
self
):
x
=
shared
(
numpy
.
asarray
([
4
,
4
,
4
]))
...
...
@@ -358,10 +361,10 @@ class Test_pfunc(unittest.TestCase):
up
=
pfunc
([],
[],
updates
=
{
x
:
(
x
*
5
),
y
:(
x
*
5
)
+
y
,
z
:
((
x
*
5
)
+
y
)
**
z
})
up
()
print
x
.
value
assert
numpy
.
all
(
x
.
value
==
20
)
assert
numpy
.
all
(
y
.
value
==
24
)
assert
numpy
.
all
(
z
.
value
==
24
**
2
)
print
x
.
get_value
(
borrow
=
True
)
assert
numpy
.
all
(
x
.
get_value
()
==
20
)
assert
numpy
.
all
(
y
.
get_value
()
==
24
)
assert
numpy
.
all
(
z
.
get_value
()
==
24
**
2
)
def
test_default_updates
(
self
):
x
=
shared
(
0
)
...
...
@@ -369,16 +372,16 @@ class Test_pfunc(unittest.TestCase):
f
=
pfunc
([],
[
x
])
f
()
print
x
.
value
assert
x
.
value
==
1
print
x
.
get_value
()
assert
x
.
get_value
()
==
1
del
x
.
default_update
f
()
assert
x
.
value
==
2
assert
x
.
get_value
()
==
2
g
=
pfunc
([],
[
x
])
g
()
assert
x
.
value
==
2
assert
x
.
get_value
()
==
2
def
test_no_default_updates
(
self
):
x
=
shared
(
0
)
...
...
@@ -388,33 +391,33 @@ class Test_pfunc(unittest.TestCase):
# Test that the default update is taken into account in the right cases
f1
=
pfunc
([],
[
x
],
no_default_updates
=
True
)
f1
()
print
x
.
value
assert
x
.
value
==
0
print
x
.
get_value
()
assert
x
.
get_value
()
==
0
f2
=
pfunc
([],
[
x
],
no_default_updates
=
[
x
])
f2
()
print
x
.
value
assert
x
.
value
==
0
print
x
.
get_value
()
assert
x
.
get_value
()
==
0
f3
=
pfunc
([],
[
x
],
no_default_updates
=
[
x
,
y
])
f3
()
print
x
.
value
assert
x
.
value
==
0
print
x
.
get_value
()
assert
x
.
get_value
()
==
0
f4
=
pfunc
([],
[
x
],
no_default_updates
=
[
y
])
f4
()
print
x
.
value
assert
x
.
value
==
2
print
x
.
get_value
()
assert
x
.
get_value
()
==
2
f5
=
pfunc
([],
[
x
],
no_default_updates
=
[])
f5
()
print
x
.
value
assert
x
.
value
==
4
print
x
.
get_value
()
assert
x
.
get_value
()
==
4
f5
=
pfunc
([],
[
x
],
no_default_updates
=
False
)
f5
()
print
x
.
value
assert
x
.
value
==
6
print
x
.
get_value
()
assert
x
.
get_value
()
==
6
self
.
failUnlessRaises
(
TypeError
,
pfunc
,
[],
[
x
],
no_default_updates
=
(
x
))
self
.
failUnlessRaises
(
TypeError
,
pfunc
,
[],
[
x
],
no_default_updates
=
x
)
...
...
@@ -423,33 +426,33 @@ class Test_pfunc(unittest.TestCase):
# Mix explicit updates and no_default_updates
g1
=
pfunc
([],
[
x
],
updates
=
[(
x
,
x
-
1
)],
no_default_updates
=
True
)
g1
()
print
x
.
value
assert
x
.
value
==
5
print
x
.
get_value
()
assert
x
.
get_value
()
==
5
g2
=
pfunc
([],
[
x
],
updates
=
[(
x
,
x
-
1
)],
no_default_updates
=
[
x
])
g2
()
print
x
.
value
assert
x
.
value
==
4
print
x
.
get_value
()
assert
x
.
get_value
()
==
4
g3
=
pfunc
([],
[
x
],
updates
=
[(
x
,
x
-
1
)],
no_default_updates
=
[
x
,
y
])
g3
()
print
x
.
value
assert
x
.
value
==
3
print
x
.
get_value
()
assert
x
.
get_value
()
==
3
g4
=
pfunc
([],
[
x
],
updates
=
[(
x
,
x
-
1
)],
no_default_updates
=
[
y
])
g4
()
print
x
.
value
assert
x
.
value
==
2
print
x
.
get_value
()
assert
x
.
get_value
()
==
2
g5
=
pfunc
([],
[
x
],
updates
=
[(
x
,
x
-
1
)],
no_default_updates
=
[])
g5
()
print
x
.
value
assert
x
.
value
==
1
print
x
.
get_value
()
assert
x
.
get_value
()
==
1
g5
=
pfunc
([],
[
x
],
updates
=
[(
x
,
x
-
1
)],
no_default_updates
=
False
)
g5
()
print
x
.
value
assert
x
.
value
==
0
print
x
.
get_value
()
assert
x
.
get_value
()
==
0
def
test_default_updates_expressions
(
self
):
x
=
shared
(
0
)
...
...
@@ -462,17 +465,17 @@ class Test_pfunc(unittest.TestCase):
f1
=
pfunc
([
a
],
z
)
f1
(
12
)
print
x
assert
x
.
value
==
1
assert
x
.
get_value
()
==
1
f2
=
pfunc
([
a
],
z
,
no_default_updates
=
True
)
assert
f2
(
7
)
==
7
print
x
assert
x
.
value
==
1
assert
x
.
get_value
()
==
1
f3
=
pfunc
([
a
],
z
,
no_default_updates
=
[
x
])
assert
f3
(
9
)
==
9
print
x
assert
x
.
value
==
1
assert
x
.
get_value
()
==
1
def
test_default_updates_multiple
(
self
):
x
=
shared
(
0
)
...
...
@@ -483,23 +486,23 @@ class Test_pfunc(unittest.TestCase):
f1
=
pfunc
([],
[
x
,
y
])
f1
()
assert
x
.
value
==
-
1
assert
y
.
value
==
2
assert
x
.
get_value
()
==
-
1
assert
y
.
get_value
()
==
2
f2
=
pfunc
([],
[
x
,
y
],
updates
=
[(
x
,
x
-
2
)],
no_default_updates
=
[
y
])
f2
()
assert
x
.
value
==
-
3
assert
y
.
value
==
2
assert
x
.
get_value
()
==
-
3
assert
y
.
get_value
()
==
2
f3
=
pfunc
([],
[
x
,
y
],
updates
=
[(
x
,
x
-
2
)],
no_default_updates
=
True
)
f3
()
assert
x
.
value
==
-
5
assert
y
.
value
==
2
assert
x
.
get_value
()
==
-
5
assert
y
.
get_value
()
==
2
f4
=
pfunc
([],
[
x
,
y
],
updates
=
[(
y
,
y
-
2
)])
f4
()
assert
x
.
value
==
-
6
assert
y
.
value
==
0
assert
x
.
get_value
()
==
-
6
assert
y
.
get_value
()
==
0
def
test_default_updates_chained
(
self
):
x
=
shared
(
2
)
...
...
@@ -512,34 +515,34 @@ class Test_pfunc(unittest.TestCase):
f1
=
pfunc
([],
[
x
])
f1
()
print
x
.
value
,
y
.
value
,
z
.
value
assert
x
.
value
==
1
assert
y
.
value
==
-
1
assert
z
.
value
==
-
2
print
x
.
get_value
(),
y
.
get_value
(),
z
.
get_value
()
assert
x
.
get_value
()
==
1
assert
y
.
get_value
()
==
-
1
assert
z
.
get_value
()
==
-
2
f2
=
pfunc
([],
[
x
,
y
])
f2
()
assert
x
.
value
==
2
assert
y
.
value
==
-
2
assert
z
.
value
==
-
3
assert
x
.
get_value
()
==
2
assert
y
.
get_value
()
==
-
2
assert
z
.
get_value
()
==
-
3
f3
=
pfunc
([],
[
y
])
f3
()
assert
x
.
value
==
2
assert
y
.
value
==
-
3
assert
z
.
value
==
-
4
assert
x
.
get_value
()
==
2
assert
y
.
get_value
()
==
-
3
assert
z
.
get_value
()
==
-
4
f4
=
pfunc
([],
[
x
,
y
],
no_default_updates
=
[
x
])
f4
()
assert
x
.
value
==
2
assert
y
.
value
==
-
4
assert
z
.
value
==
-
5
assert
x
.
get_value
()
==
2
assert
y
.
get_value
()
==
-
4
assert
z
.
get_value
()
==
-
5
f5
=
pfunc
([],
[
x
,
y
,
z
],
no_default_updates
=
[
z
])
f5
()
assert
x
.
value
==
6
assert
y
.
value
==
-
5
assert
z
.
value
==
-
5
assert
x
.
get_value
()
==
6
assert
y
.
get_value
()
==
-
5
assert
z
.
get_value
()
==
-
5
def
test_default_updates_input
(
self
):
...
...
@@ -555,28 +558,28 @@ class Test_pfunc(unittest.TestCase):
f1
=
pfunc
([],
x
,
no_default_updates
=
True
)
f1
()
assert
x
.
value
==
0
assert
y
.
value
==
1
assert
x
.
get_value
()
==
0
assert
y
.
get_value
()
==
1
f2
=
pfunc
([],
x
,
no_default_updates
=
[
x
])
f2
()
assert
x
.
value
==
0
assert
y
.
value
==
1
assert
x
.
get_value
()
==
0
assert
y
.
get_value
()
==
1
f3
=
pfunc
([],
x
,
no_default_updates
=
[
y
])
f3
()
assert
x
.
value
==
1
assert
y
.
value
==
1
assert
x
.
get_value
()
==
1
assert
y
.
get_value
()
==
1
f4
=
pfunc
([
a
],
x
)
f4
(
2
)
assert
x
.
value
==
1
assert
y
.
value
==
3
assert
x
.
get_value
()
==
1
assert
y
.
get_value
()
==
3
f5
=
pfunc
([],
x
,
updates
=
{
y
:
y
-
1
})
f5
()
assert
x
.
value
==
3
assert
y
.
value
==
2
assert
x
.
get_value
()
==
3
assert
y
.
get_value
()
==
2
# a is needed as input if y.default_update is used
self
.
failUnlessRaises
(
TypeError
,
pfunc
,
[],
x
)
...
...
@@ -587,11 +590,11 @@ class Test_pfunc(unittest.TestCase):
b
=
2
*
a
# Use only the tip of the graph, a is not used
f
=
pfunc
([
b
],
b
)
print
'a.
value ='
,
a
.
value
assert
a
.
value
==
0
print
'a.
get_value() ='
,
a
.
get_value
()
assert
a
.
get_value
()
==
0
f
(
21
)
print
'a.
value ='
,
a
.
value
assert
a
.
value
==
0
print
'a.
get_value() ='
,
a
.
get_value
()
assert
a
.
get_value
()
==
0
def
test_givens_replaces_shared_variable
(
self
):
...
...
theano/compile/tests/test_shared.py
浏览文件 @
f5ed1817
...
...
@@ -44,8 +44,8 @@ class Test_SharedVariable(unittest.TestCase):
u
=
shared
(
'asdf'
,
strict
=
False
)
v
=
shared
(
'asdf'
,
strict
=
True
)
u
.
value
=
88
v
.
value
=
88
u
.
set_value
(
88
)
v
.
set_value
(
88
)
def
test_create_numpy_strict_false
(
self
):
...
...
@@ -96,14 +96,14 @@ class Test_SharedVariable(unittest.TestCase):
strict
=
False
)
# check that assignments to value are casted properly
u
.
value
=
[
3
,
4
]
assert
type
(
u
.
value
)
is
numpy
.
ndarray
assert
str
(
u
.
value
.
dtype
)
==
'float64'
assert
numpy
.
all
(
u
.
value
==
[
3
,
4
])
u
.
set_value
([
3
,
4
])
assert
type
(
u
.
get_value
()
)
is
numpy
.
ndarray
assert
str
(
u
.
get_value
(
borrow
=
True
)
.
dtype
)
==
'float64'
assert
numpy
.
all
(
u
.
get_value
()
==
[
3
,
4
])
# check that assignments of nonsense fail
try
:
u
.
value
=
'adsf'
u
.
set_value
(
'adsf'
)
assert
0
except
ValueError
:
pass
...
...
@@ -114,7 +114,8 @@ class Test_SharedVariable(unittest.TestCase):
assert
u
.
get_value
(
borrow
=
True
)
is
uval
def
test_scalar_strict
(
self
):
def
f
(
var
,
val
):
var
.
value
=
val
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
numpy
.
int64
(
7
),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lscalar
...
...
@@ -154,7 +155,8 @@ class Test_SharedVariable(unittest.TestCase):
def
test_tensor_strict
(
self
):
def
f
(
var
,
val
):
var
.
value
=
val
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
numpy
.
int64
([
7
]),
strict
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lvector
...
...
@@ -206,47 +208,48 @@ class Test_SharedVariable(unittest.TestCase):
# Since downcasting of a value now raises an Exception,
def
f
(
var
,
val
):
var
.
value
=
val
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
numpy
.
int64
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lscalar
f
(
b
,
8.23
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
int32
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
iscalar
f
(
b
,
8.23
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
int16
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
wscalar
f
(
b
,
8.23
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
int8
(
7
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
bscalar
f
(
b
,
8.23
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
float64
(
7.234
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dscalar
f
(
b
,
8
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
float32
(
7.234
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
fscalar
f
(
b
,
8
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
float
(
7.234
),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dscalar
f
(
b
,
8
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
7.234
,
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dscalar
f
(
b
,
8
)
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
c
=
shared
(
numpy
.
zeros
((
5
,
5
),
dtype
=
'float32'
),
allow_downcast
=
True
)
self
.
failUnlessRaises
(
TypeError
,
f
,
b
,
numpy
.
random
.
rand
(
5
,
5
))
...
...
@@ -254,37 +257,38 @@ class Test_SharedVariable(unittest.TestCase):
def
test_tensor_floatX
(
self
):
def
f
(
var
,
val
):
var
.
value
=
val
def
f
(
var
,
val
):
var
.
set_value
(
val
)
b
=
shared
(
numpy
.
int64
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
lvector
f
(
b
,[
8.23
])
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
int32
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
ivector
f
(
b
,[
8.23
])
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
int16
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
wvector
f
(
b
,[
8.23
])
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
int8
([
7
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
bvector
f
(
b
,[
8.23
])
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
float64
([
7.234
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
dvector
f
(
b
,[
8
])
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
b
=
shared
(
numpy
.
float32
([
7.234
]),
allow_downcast
=
True
)
assert
b
.
type
==
theano
.
tensor
.
fvector
f
(
b
,[
8
])
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
#numpy.float([7.234]) don't work
# b = shared(numpy.float([7.234]))
...
...
@@ -299,7 +303,7 @@ class Test_SharedVariable(unittest.TestCase):
b
=
shared
(
numpy
.
asarray
([
7.234
],
dtype
=
theano
.
config
.
floatX
),
allow_downcast
=
True
)
assert
b
.
dtype
==
theano
.
config
.
floatX
f
(
b
,[
8
])
assert
b
.
value
==
8
assert
b
.
get_value
()
==
8
c
=
shared
(
numpy
.
zeros
((
5
,
5
),
dtype
=
'float32'
),
allow_downcast
=
True
)
self
.
failUnlessRaises
(
TypeError
,
f
,
b
,
numpy
.
random
.
rand
(
5
,
5
))
...
...
theano/sandbox/cuda/tests/test_basic_ops.py
浏览文件 @
f5ed1817
...
...
@@ -212,10 +212,10 @@ def test_elemwise_empty():
f
=
pfunc
([
b
],
[],
updates
=
[(
a
,
a
+
b
)],
mode
=
mode_with_gpu
)
f2
=
pfunc
([
b
],
[],
updates
=
[(
a
,
a
+
b
)],
mode
=
mode_without_gpu
)
a0
=
a
.
value
*
1.0
a0
=
a
.
get_value
()
*
1.0
f
(
numpy
.
ones
((
0
,
0
),
dtype
=
'float32'
))
assert
numpy
.
all
(
a0
+
1.0
==
a
.
value
)
assert
numpy
.
all
(
a0
+
1.0
==
a
.
get_value
()
)
def
test_elemwise0
():
...
...
@@ -228,14 +228,14 @@ def test_elemwise0():
#check that we work inplace.
assert
f
.
maker
.
env
.
toposort
()[
1
]
.
op
.
destroy_map
.
items
()
==
[(
0
,[
0
])]
a0
=
a
.
value
*
1.0
print
'BEFORE ADD'
,
a
.
value
a0
=
a
.
get_value
()
*
1.0
print
'BEFORE ADD'
,
a
.
get_value
()
for
i
,
node
in
enumerate
(
f
.
maker
.
env
.
toposort
()):
print
i
,
node
f
(
numpy
.
ones
((
4
,
4
),
dtype
=
'float32'
))
print
'AFTER ADD'
,
a
.
value
print
'AFTER ADD'
,
a
.
get_value
()
assert
numpy
.
all
(
a0
+
1.0
==
a
.
value
)
assert
numpy
.
all
(
a0
+
1.0
==
a
.
get_value
()
)
def
test_elemwise_bad_broadcast
():
x
=
cuda
.
fmatrix
(
'x'
)
...
...
@@ -751,7 +751,7 @@ def test_gpualloc_input_on_gpu():
assert
sum
([
node
.
op
==
T
.
alloc
for
node
in
f
.
maker
.
env
.
toposort
()])
==
1
assert
sum
([
node
.
op
==
B
.
gpu_alloc
for
node
in
f_gpu
.
maker
.
env
.
toposort
()])
==
1
assert
numpy
.
allclose
(
numpy
.
ones
(
a
.
value
.
shape
)
+
9
,
f_gpu
(
9
))
assert
numpy
.
allclose
(
numpy
.
ones
(
a
.
get_value
(
borrow
=
True
)
.
shape
)
+
9
,
f_gpu
(
9
))
assert
numpy
.
allclose
(
f
(
5
),
f_gpu
(
5
))
def
test_gpujoin_gpualloc
():
...
...
@@ -788,7 +788,7 @@ def test_gpualloc_output_to_gpu():
assert
sum
([
node
.
op
==
T
.
alloc
for
node
in
f
.
maker
.
env
.
toposort
()])
==
1
assert
sum
([
node
.
op
==
B
.
gpu_alloc
for
node
in
f_gpu
.
maker
.
env
.
toposort
()])
==
1
assert
numpy
.
allclose
(
numpy
.
ones
(
a
.
value
.
shape
)
+
9
,
f_gpu
(
9
))
assert
numpy
.
allclose
(
numpy
.
ones
(
a
.
get_value
(
borrow
=
True
)
.
shape
)
+
9
,
f_gpu
(
9
))
assert
numpy
.
allclose
(
f
(
5
),
f_gpu
(
5
))
import
theano.tensor.tests.test_basic
...
...
theano/sandbox/cuda/tests/test_blas.py
浏览文件 @
f5ed1817
...
...
@@ -35,15 +35,15 @@ def test_dot22():
f
=
pfunc
([
b
],
[],
updates
=
[(
a
,
tensor
.
dot
(
a
,
b
))],
mode
=
mode_with_gpu
)
a0
=
a
.
value
*
1.0
a0
=
a
.
get_value
()
*
1.0
print
a0
for
i
,
node
in
enumerate
(
f
.
maker
.
env
.
toposort
()):
print
i
,
node
bval
=
my_rand
(
4
,
4
)
f
(
bval
)
print
a
.
value
print
a
.
get_value
()
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
),
a
.
value
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
),
a
.
get_value
()
)
def
test_dot22scalar
():
a
=
tensor
.
fmatrix
()
...
...
@@ -82,16 +82,16 @@ def test_gemm():
f
=
pfunc
([
b
,
c
],
[],
updates
=
[(
a
,
tensor
.
dot
(
a
,
b
)
+
tensor
.
exp
(
c
))],
mode
=
mode_with_gpu
)
assert
any
([
node
.
op
==
tcn
.
blas
.
gpu_gemm_inplace
for
node
in
f
.
maker
.
env
.
toposort
()])
a0
=
a
.
value
*
1.0
a0
=
a
.
get_value
()
*
1.0
print
a0
for
i
,
node
in
enumerate
(
f
.
maker
.
env
.
toposort
()):
print
i
,
node
bval
=
my_rand
(
4
,
4
)
cval
=
my_rand
(
4
,
4
)
f
(
bval
,
cval
)
print
a
.
value
print
a
.
get_value
()
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
)
+
numpy
.
exp
(
cval
),
a
.
value
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
)
+
numpy
.
exp
(
cval
),
a
.
get_value
()
)
def
test_gemm_no_inplace
():
...
...
@@ -104,7 +104,7 @@ def test_gemm_no_inplace():
f
=
pfunc
([
b
,
b2
],
[
tensor
.
dot
(
a
,
b2
)
+
c
],
updates
=
[(
a
,
tensor
.
dot
(
a
,
b
)
+
c
)],
mode
=
mode_with_gpu
)
a0
=
a
.
value
*
1.0
a0
=
a
.
get_value
()
*
1.0
#print a0
for
i
,
node
in
enumerate
(
f
.
maker
.
env
.
toposort
()):
print
i
,
node
...
...
@@ -112,9 +112,9 @@ def test_gemm_no_inplace():
bval
=
my_rand
(
4
,
4
)
bval2
=
my_rand
(
4
,
4
)
rval
=
f
(
bval
,
bval2
)
#print a.
value
#print a.
get_value()
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
)
+
cval
,
a
.
value
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval
)
+
cval
,
a
.
get_value
()
)
assert
numpy
.
allclose
(
numpy
.
dot
(
a0
,
bval2
)
+
cval
,
rval
)
if
0
:
...
...
theano/sandbox/cuda/tests/test_mlp.py
浏览文件 @
f5ed1817
...
...
@@ -322,9 +322,9 @@ def build_conv_nnet2_classif(use_gpu, isize, ksize, n_batch,
v
=
shared_fn
(
0.01
*
my_randn
(
n_hid
,
n_out
),
'v'
)
c
=
shared_fn
(
my_zeros
(
n_out
),
'c'
)
print
'ALLOCATING ARCH: w0 shape'
,
w0
.
value
.
shape
print
'ALLOCATING ARCH: w1 shape'
,
w1
.
value
.
shape
print
'ALLOCATING ARCH: v shape'
,
v
.
value
.
shape
print
'ALLOCATING ARCH: w0 shape'
,
w0
.
get_value
(
borrow
=
True
)
.
shape
print
'ALLOCATING ARCH: w1 shape'
,
w1
.
get_value
(
borrow
=
True
)
.
shape
print
'ALLOCATING ARCH: v shape'
,
v
.
get_value
(
borrow
=
True
)
.
shape
x
=
tensor
.
Tensor
(
dtype
=
'float32'
,
broadcastable
=
(
0
,
1
,
0
,
0
))(
'x'
)
y
=
tensor
.
fmatrix
(
'y'
)
...
...
theano/sandbox/test_neighbours.py
浏览文件 @
f5ed1817
...
...
@@ -24,13 +24,13 @@ def test_neibs():
f
=
function
([],
images2neibs
(
images
,
neib_shape
),
mode
=
mode_without_gpu
)
#print images.
value
#print images.
get_value(borrow=True)
neibs
=
f
()
#print neibs
g
=
function
([],
neibs2images
(
neibs
,
neib_shape
,
images
.
shape
),
mode
=
mode_without_gpu
)
#print g()
assert
numpy
.
allclose
(
images
.
value
,
g
())
assert
numpy
.
allclose
(
images
.
get_value
(
borrow
=
True
)
,
g
())
def
test_neibs_bad_shape
():
shape
=
(
2
,
3
,
10
,
10
)
...
...
@@ -121,7 +121,7 @@ def test_neibs_manual():
f
=
function
([],
images2neibs
(
images
,
neib_shape
),
mode
=
mode_without_gpu
)
#print images.
value
#print images.
get_value(borrow=True)
neibs
=
f
()
print
neibs
assert
numpy
.
allclose
(
neibs
,[[
0
,
1
,
4
,
5
],
...
...
@@ -151,7 +151,7 @@ def test_neibs_manual():
g
=
function
([],
neibs2images
(
neibs
,
neib_shape
,
images
.
shape
),
mode
=
mode_without_gpu
)
#print g()
assert
numpy
.
allclose
(
images
.
value
,
g
())
assert
numpy
.
allclose
(
images
.
get_value
(
borrow
=
True
),
g
())
def
test_neibs_step_manual
():
...
...
@@ -165,7 +165,7 @@ def test_neibs_step_manual():
for
mode_idx
,
mode
in
enumerate
(
modes
):
f
=
function
([],
images2neibs
(
images
,
neib_shape
,
neib_step
),
mode
=
mode
)
#print images.
value
#print images.
get_value(borrow=True)
neibs
=
f
()
if
mode_idx
==
0
:
assert
Images2Neibs
in
[
type
(
node
.
op
)
for
node
in
f
.
maker
.
env
.
toposort
()]
...
...
@@ -200,7 +200,7 @@ def test_neibs_step_manual():
#g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu)
#print g()
#assert numpy.allclose(images.
value
,g())
#assert numpy.allclose(images.
get_value(borrow=True)
,g())
def
test_neibs_wrap_centered_step_manual
():
...
...
@@ -275,7 +275,7 @@ def test_neibs_wrap_centered_step_manual():
#g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu)
#assert numpy.allclose(images.
value,
g())
#assert numpy.allclose(images.
get_value(borrow=True),
g())
def
test_neibs_gpu
():
...
...
@@ -297,14 +297,14 @@ def test_neibs_gpu():
f_gpu
=
function
([],
images2neibs
(
images
,
neib_shape
),
mode
=
mode_with_gpu
)
assert
any
([
isinstance
(
node
.
op
,
GpuImages2Neibs
)
for
node
in
f_gpu
.
maker
.
env
.
toposort
()])
#print images.
value
#print images.
get_value(borrow=True)
neibs
=
numpy
.
asarray
(
f_gpu
())
assert
numpy
.
allclose
(
neibs
,
f
())
#print neibs
g
=
function
([],
neibs2images
(
neibs
,
neib_shape
,
images
.
shape
),
mode
=
mode_with_gpu
)
assert
any
([
isinstance
(
node
.
op
,
GpuImages2Neibs
)
for
node
in
f
.
maker
.
env
.
toposort
()])
#print numpy.asarray(g())
assert
numpy
.
allclose
(
images
.
value
,
g
())
assert
numpy
.
allclose
(
images
.
get_value
(
borrow
=
True
),
g
())
def
speed_neibs
():
...
...
theano/tensor/tests/mlp_test.py
浏览文件 @
f5ed1817
...
...
@@ -257,9 +257,9 @@ def test_mlp():
batch_size
=
100
# size of the minibatch
# compute number of minibatches for training, validation and testing
n_train_batches
=
train_set_x
.
value
.
shape
[
0
]
/
batch_size
n_valid_batches
=
valid_set_x
.
value
.
shape
[
0
]
/
batch_size
n_test_batches
=
test_set_x
.
value
.
shape
[
0
]
/
batch_size
n_train_batches
=
train_set_x
.
get_value
(
borrow
=
True
)
.
shape
[
0
]
/
batch_size
n_valid_batches
=
valid_set_x
.
get_value
(
borrow
=
True
)
.
shape
[
0
]
/
batch_size
n_test_batches
=
test_set_x
.
get_value
(
borrow
=
True
)
.
shape
[
0
]
/
batch_size
######################
# BUILD ACTUAL MODEL #
...
...
theano/tensor/tests/test_blas.py
浏览文件 @
f5ed1817
...
...
@@ -206,20 +206,20 @@ class t_gemm(TestCase):
#f(z, a, x, y, b)
f
=
inplace_func
([],
gemm_inplace
(
tz
,
ta
,
tx
,
ty
,
tb
),
mode
=
compile
.
Mode
(
optimizer
=
None
,
linker
=
l
))
f
()
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
value
),
(
z_orig
,
z_after
,
z
,
z_after
-
z
))
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
get_value
(
borrow
=
True
)
),
(
z_orig
,
z_after
,
z
,
z_after
-
z
))
f
()
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
value
),
(
z_orig
,
z_after
,
z
,
z_after
-
z
))
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
get_value
(
borrow
=
True
)
),
(
z_orig
,
z_after
,
z
,
z_after
-
z
))
f
()
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
value
),
(
z_orig
,
z_after
,
z
,
z_after
-
z
))
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
get_value
(
borrow
=
True
)
),
(
z_orig
,
z_after
,
z
,
z_after
-
z
))
#tz.value *= 0 # clear z's value
y_T
=
ty
.
value
.
T
ty
.
value
=
tx
.
value
.
T
tx
.
value
=
y_T
y_T
=
ty
.
get_value
(
borrow
=
True
)
.
T
ty
.
set_value
(
tx
.
get_value
(
borrow
=
True
)
.
T
,
borrow
=
True
)
tx
.
set_value
(
y_T
,
borrow
=
True
)
f
()
# test that the transposed version of multiplication gives same answer
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
value
.
T
))
self
.
failUnless
(
_approx_eq
(
z_after
,
tz
.
get_value
(
borrow
=
True
)
.
T
))
t
(
C
,
A
,
B
)
t
(
C
.
T
,
A
,
B
)
...
...
@@ -670,7 +670,7 @@ def test_dot_vm():
f
=
theano
.
function
([],
theano
.
dot
(
v
,
m
),
mode
=
mode_blas_opt
)
# Assert they produce the same output
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
v
.
value
,
m
.
value
))
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
v
.
get_value
(),
m
.
get_value
()
))
assert
sum
([
isinstance
(
node
.
op
,
T
.
Dot
)
for
node
in
f
.
maker
.
env
.
toposort
()
])
==
1
...
...
@@ -684,7 +684,7 @@ def test_dot_mv():
f
=
theano
.
function
([],
theano
.
dot
(
m
,
v
),
mode
=
mode_blas_opt
)
# Assert they produce the same output
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
m
.
value
,
v
.
value
))
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
m
.
get_value
(),
v
.
get_value
()
))
assert
sum
([
isinstance
(
node
.
op
,
T
.
Dot
)
for
node
in
f
.
maker
.
env
.
toposort
()
])
==
1
...
...
@@ -700,7 +700,8 @@ def test_gemv1():
f
=
theano
.
function
([],
v2
+
theano
.
dot
(
m
,
v1
),
mode
=
mode_blas_opt
)
# Assert they produce the same output
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
m
.
value
,
v1
.
value
)
+
v2_orig
)
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
Gemv
)
...
...
@@ -712,7 +713,8 @@ def test_gemv1():
# Assert they produce the same output
f
()
assert
numpy
.
allclose
(
v2
.
value
,
numpy
.
dot
(
m
.
value
,
v1
.
value
)
+
v2_orig
)
assert
numpy
.
allclose
(
v2
.
get_value
(),
numpy
.
dot
(
m
.
get_value
(),
v1
.
get_value
())
+
v2_orig
)
topo
=
f
.
maker
.
env
.
toposort
()
assert
len
(
topo
)
==
1
assert
isinstance
(
topo
[
0
]
.
op
,
Gemv
)
...
...
@@ -730,7 +732,8 @@ def test_gemv2():
f
=
theano
.
function
([],
v2
+
theano
.
dot
(
v1
,
m
),
mode
=
mode_blas_opt
)
# Assert they produce the same output
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
v1
.
value
,
m
.
value
)
+
v2
.
value
)
assert
numpy
.
allclose
(
f
(),
numpy
.
dot
(
v1
.
get_value
(),
m
.
get_value
())
+
v2
.
get_value
())
topo
=
f
.
maker
.
env
.
toposort
()
assert
sum
(
isinstance
(
node
.
op
,
Gemv
)
for
node
in
topo
)
==
1
assert
topo
[
-
1
]
.
op
.
inplace
==
False
...
...
@@ -741,7 +744,8 @@ def test_gemv2():
# Assert they produce the same output
f
()
assert
numpy
.
allclose
(
v2
.
value
,
numpy
.
dot
(
v1
.
value
,
m
.
value
)
+
v2_orig
)
assert
numpy
.
allclose
(
v2
.
get_value
(),
numpy
.
dot
(
v1
.
get_value
(),
m
.
get_value
())
+
v2_orig
)
topo
=
f
.
maker
.
env
.
toposort
()
assert
sum
(
isinstance
(
node
.
op
,
Gemv
)
for
node
in
topo
)
==
1
if
config
.
mode
!=
'FAST_COMPILE'
:
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
f5ed1817
...
...
@@ -832,7 +832,7 @@ class test_fusion(unittest.TestCase):
for
x
in
range
(
nb_repeat
):
f
(
*
val_inputs
)
t1
=
time
.
time
()
out
=
out
.
value
out
=
out
.
get_value
()
times
[
id
]
=
t1
-
t0
atol
=
1e-8
...
...
theano/tensor/tests/test_shared_randomstreams.py
浏览文件 @
f5ed1817
...
...
@@ -29,7 +29,8 @@ class T_SharedRandomStreams(unittest.TestCase):
assert
numpy
.
all
(
g
()
==
g
())
assert
numpy
.
all
(
abs
(
nearly_zeros
())
<
1e-5
)
assert
isinstance
(
rv_u
.
rng
.
value
,
numpy
.
random
.
RandomState
)
assert
isinstance
(
rv_u
.
rng
.
get_value
(
borrow
=
True
),
numpy
.
random
.
RandomState
)
def
test_basics
(
self
):
random
=
RandomStreams
(
utt
.
fetch_seed
())
...
...
theano/tensor/tests/test_sharedvar.py
浏览文件 @
f5ed1817
import
numpy
import
unittest
import
warnings
import
theano
from
theano.gof.python25
import
all
...
...
@@ -319,15 +320,15 @@ def makeSharedTester(shared_constructor_,
if
x
.
__class__
.
__name__
!=
'csr_matrix'
:
#sparse matrix don't support inplace affectation
x_shared
.
container
.
value
[:]
=
nd
assert
(
numpy
.
asarray
(
x_shared
.
value
)
==
nd
)
.
all
()
assert
(
numpy
.
asarray
(
x_shared
.
get_value
(
borrow
=
True
)
)
==
nd
)
.
all
()
#This should always share value!
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
assert
may_share_memory
(
old_data
,
x_shared
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
))
nd
[
0
]
+=
1
x_shared
.
container
.
value
[
0
]
=
nd
[
0
]
assert
(
numpy
.
asarray
(
x_shared
.
value
[
0
])
==
nd
[
0
])
.
all
()
assert
(
numpy
.
asarray
(
x_shared
.
value
[
1
:])
==
nd
[
1
:])
.
all
()
assert
(
numpy
.
asarray
(
x_shared
.
get_value
(
borrow
=
True
)
[
0
])
==
nd
[
0
])
.
all
()
assert
(
numpy
.
asarray
(
x_shared
.
get_value
(
borrow
=
True
)
[
1
:])
==
nd
[
1
:])
.
all
()
#This should always share value!
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
assert
may_share_memory
(
old_data
,
x_shared
.
get_value
(
borrow
=
True
,
return_internal_type
=
True
))
...
...
@@ -336,23 +337,31 @@ def makeSharedTester(shared_constructor_,
#sparse matrix don't support inplace affectation
nd
+=
1
#THIS DON't DO WHAT WE EXPECT the contain of a is not updated for CudaNdarray, but it is for ndarray
x_shared
.
value
[:]
=
nd
#assert (numpy.asarray(x_shared.
value
)!=nd).all()
x_shared
.
get_value
(
borrow
=
True
)
[:]
=
nd
#assert (numpy.asarray(x_shared.
get_value(borrow=True)
)!=nd).all()
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
x_shared
.
value
x_shared
.
get_value
(
borrow
=
True
)
# Test by .value
# As we know that .value is deprecated, we filter out the warning
warnings
.
filterwarnings
(
action
=
'ignore'
,
message
=
'The .value property of shared variables is deprecated.'
)
nd
+=
1
old_data
=
x_shared
.
container
.
storage
[
0
]
x_shared
.
value
=
nd
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
value
),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
==
self
.
set_value_inplace
# restore the warning filters
warnings
.
resetwarnings
()
# Test by set_value with borrow=False
nd
+=
1
old_data
=
x_shared
.
container
.
storage
[
0
]
x_shared
.
set_value
(
nd
,
borrow
=
False
)
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
value
),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
get_value
(
borrow
=
True
)),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
==
self
.
set_value_inplace
# Test by set_value with borrow=False when new data casted.
...
...
@@ -360,14 +369,16 @@ def makeSharedTester(shared_constructor_,
nd
+=
1
old_data
=
x_shared
.
container
.
storage
[
0
]
x_shared
.
set_value
(
self
.
cast_value
(
nd
),
borrow
=
False
)
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
value
),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
get_value
(
borrow
=
True
)),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
==
self
.
set_casted_value_inplace
# Test by set_value with borrow=True
nd
+=
1
old_data
=
x_shared
.
container
.
storage
[
0
]
x_shared
.
set_value
(
nd
.
copy
(),
borrow
=
True
)
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
value
),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
get_value
(
borrow
=
True
)),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
==
self
.
set_value_inplace
# Test by set_value with borrow=True when new data casted.
...
...
@@ -375,7 +386,7 @@ def makeSharedTester(shared_constructor_,
nd
+=
1
old_data
=
x_shared
.
container
.
storage
[
0
]
x_shared
.
set_value
(
self
.
cast_value
(
nd
.
copy
()),
borrow
=
True
)
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
value
),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
numpy
.
allclose
(
self
.
ref_fct
(
x_shared
.
get_value
(
borrow
=
True
)
),
self
.
ref_fct
(
self
.
cast_value
(
nd
)))
assert
may_share_memory
(
old_data
,
x_shared
.
container
.
storage
[
0
])
==
self
.
set_casted_value_inplace
def
test_specify_shape
(
self
):
...
...
@@ -395,7 +406,8 @@ def makeSharedTester(shared_constructor_,
x1_shared
=
self
.
shared_constructor
(
x1_1
)
x1_specify_shape
=
tensor
.
specify_shape
(
x1_shared
,
x1_1
.
shape
)
x1_shared
.
set_value
(
x1_2
)
assert
numpy
.
allclose
(
self
.
ref_fct
(
x1_shared
.
value
),
self
.
ref_fct
(
x1_2
))
assert
numpy
.
allclose
(
self
.
ref_fct
(
x1_shared
.
get_value
(
borrow
=
True
)),
self
.
ref_fct
(
x1_2
))
shape_op_fct
=
theano
.
function
([],
x1_shared
.
shape
)
topo
=
shape_op_fct
.
maker
.
env
.
toposort
()
if
theano
.
config
.
mode
!=
'FAST_COMPILE'
:
...
...
@@ -460,7 +472,9 @@ def makeSharedTester(shared_constructor_,
(
tensor
.
as_tensor_variable
(
x1_1
.
shape
[
0
]),
x1_shared
.
shape
[
1
]))
x1_shared
.
set_value
(
x1_2
)
assert
numpy
.
allclose
(
self
.
ref_fct
(
x1_shared
.
value
),
self
.
ref_fct
(
x1_2
))
assert
numpy
.
allclose
(
self
.
ref_fct
(
x1_shared
.
get_value
(
borrow
=
True
)),
self
.
ref_fct
(
x1_2
))
shape_op_fct
=
theano
.
function
([],
x1_shared
.
shape
)
topo
=
shape_op_fct
.
maker
.
env
.
toposort
()
shape_op_fct
()
...
...
@@ -529,7 +543,7 @@ def makeSharedTester(shared_constructor_,
assert
all
(
node
.
op
.
inplace
for
node
in
topo
if
node
.
op
.
__class__
.
__name__
==
"GpuGemm"
)
#Their is no inplace gemm for sparse
#assert all(node.op.inplace for node in topo if node.op.__class__.__name__ == "StructuredDot")
s_shared_specify
=
tensor
.
specify_shape
(
s_shared
,
s_shared
.
value
.
shape
)
s_shared_specify
=
tensor
.
specify_shape
(
s_shared
,
s_shared
.
get_value
(
borrow
=
True
)
.
shape
)
#now test with the specify shape op in the output
f
=
theano
.
function
([],
s_shared
.
shape
,
...
...
@@ -544,8 +558,10 @@ def makeSharedTester(shared_constructor_,
assert
all
(
node
.
op
==
tensor
.
blas
.
gemm_inplace
for
node
in
topo
if
isinstance
(
node
.
op
,
tensor
.
blas
.
Gemm
))
assert
all
(
node
.
op
.
inplace
for
node
in
topo
if
node
.
op
.
__class__
.
__name__
==
"GpuGemm"
)
#now test with the specify shape op in the inputs and outputs
a_shared
=
tensor
.
specify_shape
(
a_shared
,
a_shared
.
value
.
shape
)
b_shared
=
tensor
.
specify_shape
(
b_shared
,
b_shared
.
value
.
shape
)
a_shared
=
tensor
.
specify_shape
(
a_shared
,
a_shared
.
get_value
(
borrow
=
True
)
.
shape
)
b_shared
=
tensor
.
specify_shape
(
b_shared
,
b_shared
.
get_value
(
borrow
=
True
)
.
shape
)
f
=
theano
.
function
([],
s_shared
.
shape
,
updates
=
{
s_shared
:
theano
.
dot
(
a_shared
,
b_shared
)
...
...
theano/tests/test_scan.py
浏览文件 @
f5ed1817
...
...
@@ -225,9 +225,11 @@ class T_Scan(unittest.TestCase):
v_x0
=
asarrayX
(
rng
.
uniform
())
# compute the output i numpy
v_out
=
numpy
.
zeros
((
4
,))
v_out
[
0
]
=
v_u
[
0
]
*
W_in
.
value
+
v_x0
*
W
.
value
v_out
[
0
]
=
(
v_u
[
0
]
*
W_in
.
get_value
(
borrow
=
True
)
+
v_x0
*
W
.
get_value
(
borrow
=
True
))
for
step
in
xrange
(
1
,
4
):
v_out
[
step
]
=
v_u
[
step
]
*
W_in
.
value
+
v_out
[
step
-
1
]
*
W
.
value
v_out
[
step
]
=
(
v_u
[
step
]
*
W_in
.
get_value
(
borrow
=
True
)
+
v_out
[
step
-
1
]
*
W
.
get_value
(
borrow
=
True
))
theano_values
=
f3
(
v_u
,
v_x0
)
assert
numpy
.
allclose
(
theano_values
,
v_out
)
...
...
@@ -539,8 +541,8 @@ class T_Scan(unittest.TestCase):
assert
numpy
.
allclose
(
theano_y0
,
numpy_y0
[
3
:])
assert
numpy
.
allclose
(
theano_y1
,
numpy_y1
[
1
:])
assert
numpy
.
allclose
(
theano_y2
,
numpy_y2
)
assert
numpy
.
allclose
(
W1
.
value
,
numpy_W1
)
assert
numpy
.
allclose
(
W2
.
value
,
numpy_W2
)
assert
numpy
.
allclose
(
W1
.
get_value
(
borrow
=
True
),
numpy_W1
)
assert
numpy
.
allclose
(
W2
.
get_value
(
borrow
=
True
),
numpy_W2
)
...
...
@@ -622,7 +624,7 @@ class T_Scan(unittest.TestCase):
n_steps
=
3
this_f
(
n_steps
)
numpy_state
=
v_state
*
(
2
**
(
n_steps
))
assert
numpy
.
allclose
(
state
.
value
,
numpy_state
)
assert
numpy
.
allclose
(
state
.
get_value
(
borrow
=
True
)
,
numpy_state
)
def
test_map_functionality
(
self
):
def
f_rnn
(
u_t
):
...
...
@@ -1005,7 +1007,7 @@ class T_Scan(unittest.TestCase):
f()
print X.
value
print X.
get_value(borrow=True)
'''
def
test_scan_output_padding
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论