Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
471c9400
提交
471c9400
authored
9月 06, 2011
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8 from abergeron/shared_pickle
Shared pickle
上级
4dc285b2
0faf7819
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
66 行增加
和
2 行删除
+66
-2
__init__.py
theano/sandbox/cuda/__init__.py
+16
-1
test_var.py
theano/sandbox/cuda/tests/test_var.py
+19
-0
sharedvar.py
theano/tensor/sharedvar.py
+9
-1
test_sharedvar.py
theano/tensor/tests/test_sharedvar.py
+22
-0
没有找到文件。
theano/sandbox/cuda/__init__.py
浏览文件 @
471c9400
...
@@ -2,6 +2,7 @@ import atexit, logging, os, stat, sys
...
@@ -2,6 +2,7 @@ import atexit, logging, os, stat, sys
from
theano.compile
import
optdb
from
theano.compile
import
optdb
from
theano.gof.cmodule
import
get_lib_extension
from
theano.gof.cmodule
import
get_lib_extension
from
theano.configparser
import
config
,
AddConfigVar
,
StrParam
from
theano.configparser
import
config
,
AddConfigVar
,
StrParam
from
theano.tensor.sharedvar
import
load_shared_variable
import
nvcc_compiler
import
nvcc_compiler
_logger_name
=
'theano.sandbox.cuda'
_logger_name
=
'theano.sandbox.cuda'
...
@@ -275,11 +276,25 @@ def handle_shared_float32(tf):
...
@@ -275,11 +276,25 @@ def handle_shared_float32(tf):
"""
"""
if
tf
:
if
tf
:
import
theano.compile
import
theano.compile
import
copy_reg
theano
.
compile
.
shared_constructor
(
float32_shared_constructor
)
theano
.
compile
.
shared_constructor
(
float32_shared_constructor
)
# this is a bit of hackery to make the shared variables load
# with the proper type.
copy_reg
.
pickle
(
theano
.
tensor
.
basic
.
TensorVariable
,
reduce_tensor_variable
,
load_shared_variable
)
else
:
else
:
raise
NotImplementedError
(
'removing our handler'
)
raise
NotImplementedError
(
'removing our handler'
)
def
reduce_tensor_variable
(
var
):
if
isinstance
(
var
.
owner
.
op
,
HostFromGpu
)
and
len
(
var
.
owner
.
inputs
)
==
1
\
and
isinstance
(
var
.
owner
.
inputs
[
0
],
CudaNdarraySharedVariable
):
return
load_shared_variable
,
(
var
.
owner
.
inputs
[
0
]
.
get_value
(),)
else
:
# this will make protocol 2 a little bit less efficient
# but there is no way around it.
return
var
.
__reduce__
()
if
config
.
device
.
startswith
(
'gpu'
):
if
config
.
device
.
startswith
(
'gpu'
):
use
(
device
=
config
.
device
,
force
=
config
.
force_device
)
use
(
device
=
config
.
device
,
force
=
config
.
force_device
)
elif
config
.
init_gpu_device
:
elif
config
.
init_gpu_device
:
...
...
theano/sandbox/cuda/tests/test_var.py
浏览文件 @
471c9400
...
@@ -4,10 +4,29 @@ from nose.plugins.skip import SkipTest
...
@@ -4,10 +4,29 @@ from nose.plugins.skip import SkipTest
from
theano.sandbox.cuda.var
import
float32_shared_constructor
as
f32sc
from
theano.sandbox.cuda.var
import
float32_shared_constructor
as
f32sc
from
theano.sandbox.cuda
import
CudaNdarrayType
,
cuda_available
from
theano.sandbox.cuda
import
CudaNdarrayType
,
cuda_available
import
theano
# Skip test if cuda_ndarray is not available.
# Skip test if cuda_ndarray is not available.
if
cuda_available
==
False
:
if
cuda_available
==
False
:
raise
SkipTest
(
'Optional package cuda disabled'
)
raise
SkipTest
(
'Optional package cuda disabled'
)
def
test_shared_pickle
():
import
pickle
picklestring
=
"ctheano.tensor.sharedvar
\n
load_shared_variable
\n
p0
\n
(cnumpy.core.multiarray
\n
_reconstruct
\n
p1
\n
(cnumpy
\n
ndarray
\n
p2
\n
(I0
\n
tp3
\n
S'b'
\n
p4
\n
tp5
\n
Rp6
\n
(I1
\n
(I2
\n
tp7
\n
cnumpy
\n
dtype
\n
p8
\n
(S'f4'
\n
p9
\n
I0
\n
I1
\n
tp10
\n
Rp11
\n
(I3
\n
S'<'
\n
p12
\n
NNNI-1
\n
I-1
\n
I0
\n
tp13
\n
bI00
\n
S'
\\
x00
\\
x00
\\
x80?
\\
x00
\\
x00
\\
x00@'
\n
p14
\n
tp15
\n
btp16
\n
Rp17
\n
."
g
=
pickle
.
loads
(
picklestring
)
v
=
numpy
.
array
([
1.0
,
2.0
],
dtype
=
'float32'
)
# This test will always be on the GPU
assert
isinstance
(
g
,
theano
.
tensor
.
basic
.
TensorVariable
)
assert
isinstance
(
g
.
owner
,
theano
.
gof
.
graph
.
Apply
)
assert
isinstance
(
g
.
owner
.
op
,
theano
.
sandbox
.
cuda
.
HostFromGpu
)
assert
isinstance
(
g
.
owner
.
inputs
[
0
],
CudaNdarrayType
.
SharedVariable
)
assert
(
g
.
owner
.
inputs
[
0
]
.
get_value
()
==
v
)
.
all
()
# Make sure it saves the same way (so that the tests before are not bogus)
s
=
theano
.
tensor
.
as_tensor_variable
(
theano
.
shared
(
v
))
assert
pickle
.
dumps
(
s
)
==
picklestring
def
test_float32_shared_constructor
():
def
test_float32_shared_constructor
():
npy_row
=
numpy
.
zeros
((
1
,
10
),
dtype
=
'float32'
)
npy_row
=
numpy
.
zeros
((
1
,
10
),
dtype
=
'float32'
)
...
...
theano/tensor/sharedvar.py
浏览文件 @
471c9400
...
@@ -5,9 +5,17 @@ from basic import TensorType, _tensor_py_operators, autocast_int, autocast_float
...
@@ -5,9 +5,17 @@ from basic import TensorType, _tensor_py_operators, autocast_int, autocast_float
from
theano.compile
import
shared_constructor
,
SharedVariable
from
theano.compile
import
shared_constructor
,
SharedVariable
from
theano
import
config
from
theano
import
config
def
load_shared_variable
(
val
):
return
theano
.
tensor
.
as_tensor_variable
(
theano
.
shared
(
val
))
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
# _tensor_py_operators is first to have its version of __{gt,ge,lt,le}__
class
TensorSharedVariable
(
_tensor_py_operators
,
SharedVariable
):
class
TensorSharedVariable
(
_tensor_py_operators
,
SharedVariable
):
pass
def
__reduce_ex__
(
self
,
proto
):
# This is for loading on the GPU if present.
if
self
.
dtype
==
'float32'
:
return
load_shared_variable
,
(
self
.
get_value
(),)
else
:
return
super
(
TensorSharedVariable
,
self
)
.
__reduce_ex__
(
proto
)
@shared_constructor
@shared_constructor
def
tensor_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
,
borrow
=
False
,
broadcastable
=
None
):
def
tensor_constructor
(
value
,
name
=
None
,
strict
=
False
,
allow_downcast
=
None
,
borrow
=
False
,
broadcastable
=
None
):
...
...
theano/tensor/tests/test_sharedvar.py
浏览文件 @
471c9400
...
@@ -637,3 +637,25 @@ test_shared_options=makeSharedTester(
...
@@ -637,3 +637,25 @@ test_shared_options=makeSharedTester(
cast_value_
=
numpy
.
asarray
,
cast_value_
=
numpy
.
asarray
,
op_by_matrix_
=
False
,
op_by_matrix_
=
False
,
name
=
'test_shared_options'
)
name
=
'test_shared_options'
)
def
test_shared_pickle
():
import
pickle
picklestring
=
"ctheano.tensor.sharedvar
\n
load_shared_variable
\n
p0
\n
(cnumpy.core.multiarray
\n
_reconstruct
\n
p1
\n
(cnumpy
\n
ndarray
\n
p2
\n
(I0
\n
tp3
\n
S'b'
\n
p4
\n
tp5
\n
Rp6
\n
(I1
\n
(I2
\n
tp7
\n
cnumpy
\n
dtype
\n
p8
\n
(S'f4'
\n
p9
\n
I0
\n
I1
\n
tp10
\n
Rp11
\n
(I3
\n
S'<'
\n
p12
\n
NNNI-1
\n
I-1
\n
I0
\n
tp13
\n
bI00
\n
S'
\\
x00
\\
x00
\\
x80?
\\
x00
\\
x00
\\
x00@'
\n
p14
\n
tp15
\n
btp16
\n
Rp17
\n
."
g
=
pickle
.
loads
(
picklestring
)
v
=
numpy
.
array
([
1.0
,
2.0
],
dtype
=
'float32'
)
assert
g
.
type
==
theano
.
tensor
.
fvector
if
theano
.
config
.
device
.
startswith
(
'cpu'
):
assert
isinstance
(
g
,
theano
.
tensor
.
sharedvar
.
TensorSharedVariable
)
assert
(
g
.
get_value
()
==
v
)
.
all
()
if
theano
.
config
.
device
.
startswith
(
'gpu'
):
assert
isinstance
(
g
,
theano
.
tensor
.
basic
.
TensorVariable
)
# we don't go digging deeper because we don't want to
# import theano.sandbox.cuda.
# some other tests are there.
# Make sure it saves the same way (so that the tests before are not bogus)
s
=
theano
.
tensor
.
as_tensor_variable
(
theano
.
shared
(
v
))
assert
pickle
.
dumps
(
s
)
==
picklestring
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论