Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
67e1c377
提交
67e1c377
authored
11月 04, 2013
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
some pep8
上级
9fd1db06
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
38 行增加
和
25 行删除
+38
-25
setup.py
setup.py
+0
-1
rng_mrg.py
theano/sandbox/rng_mrg.py
+38
-24
没有找到文件。
setup.py
浏览文件 @
67e1c377
...
@@ -190,7 +190,6 @@ def do_setup():
...
@@ -190,7 +190,6 @@ def do_setup():
packages
=
find_packages
(),
packages
=
find_packages
(),
install_requires
=
[
'numpy>=1.5.0'
,
'scipy>=0.7.2'
],
install_requires
=
[
'numpy>=1.5.0'
,
'scipy>=0.7.2'
],
package_data
=
{
package_data
=
{
''
:
[
'*.txt'
,
'*.rst'
,
'*.cu'
,
'*.cuh'
,
'*.c'
,
'*.sh'
,
'*.pkl'
,
''
:
[
'*.txt'
,
'*.rst'
,
'*.cu'
,
'*.cuh'
,
'*.c'
,
'*.sh'
,
'*.pkl'
,
''
:
[
'*.txt'
,
'*.rst'
,
'*.cu'
,
'*.cuh'
,
'*.c'
,
'*.sh'
,
'*.pkl'
,
'ChangeLog'
],
'ChangeLog'
],
'theano.misc'
:
[
'*.sh'
]
'theano.misc'
:
[
'*.sh'
]
...
...
theano/sandbox/rng_mrg.py
浏览文件 @
67e1c377
...
@@ -5,7 +5,9 @@ Generator code in SSJ package (L'Ecuyer & Simard)
...
@@ -5,7 +5,9 @@ Generator code in SSJ package (L'Ecuyer & Simard)
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
"""
"""
import
sys
,
warnings
import
sys
import
warnings
import
numpy
import
numpy
from
theano
import
Op
,
Apply
,
shared
,
config
,
Variable
from
theano
import
Op
,
Apply
,
shared
,
config
,
Variable
...
@@ -36,6 +38,7 @@ def matVecModM(A, s, m):
...
@@ -36,6 +38,7 @@ def matVecModM(A, s, m):
x
[
i
]
=
r
+
m
x
[
i
]
=
r
+
m
return
x
return
x
def
multMatVect
(
v
,
A
,
m1
,
B
,
m2
):
def
multMatVect
(
v
,
A
,
m1
,
B
,
m2
):
#multiply the first half of v by A with a modulo of m1
#multiply the first half of v by A with a modulo of m1
#and the second half by B with a modulo of m2
#and the second half by B with a modulo of m2
...
@@ -79,9 +82,11 @@ A2p134 = numpy.asarray(
...
@@ -79,9 +82,11 @@ A2p134 = numpy.asarray(
[
1401213391
,
1178684362
,
1431130166
]])
[
1401213391
,
1178684362
,
1431130166
]])
np_int32_vals
=
[
numpy
.
int32
(
i
)
for
i
in
(
0
,
7
,
9
,
15
,
16
,
22
,
24
)]
np_int32_vals
=
[
numpy
.
int32
(
i
)
for
i
in
(
0
,
7
,
9
,
15
,
16
,
22
,
24
)]
def
ff_2p134
(
rstate
):
def
ff_2p134
(
rstate
):
return
multMatVect
(
rstate
,
A1p134
,
M1
,
A2p134
,
M2
)
return
multMatVect
(
rstate
,
A1p134
,
M1
,
A2p134
,
M2
)
def
ff_2p72
(
rstate
):
def
ff_2p72
(
rstate
):
return
multMatVect
(
rstate
,
A1p72
,
M1
,
A2p72
,
M2
)
return
multMatVect
(
rstate
,
A1p72
,
M1
,
A2p72
,
M2
)
...
@@ -93,8 +98,8 @@ def mrg_next_value(rstate, new_rstate):
...
@@ -93,8 +98,8 @@ def mrg_next_value(rstate, new_rstate):
#i0, i7, i9, i15, i16, i22, i24 = [numpy.int32(i) for i in (0, 7, 9, 15, 16, 22, 24)]
#i0, i7, i9, i15, i16, i22, i24 = [numpy.int32(i) for i in (0, 7, 9, 15, 16, 22, 24)]
i0
,
i7
,
i9
,
i15
,
i16
,
i22
,
i24
=
np_int32_vals
i0
,
i7
,
i9
,
i15
,
i16
,
i22
,
i24
=
np_int32_vals
#first component
#first component
y1
=
(((
x12
&
MASK12
)
<<
i22
)
+
(
x12
>>
i9
)
y1
=
(((
x12
&
MASK12
)
<<
i22
)
+
(
x12
>>
i9
)
+
+
((
x13
&
MASK13
)
<<
i7
)
+
(
x13
>>
i24
))
((
x13
&
MASK13
)
<<
i7
)
+
(
x13
>>
i24
))
assert
type
(
y1
)
==
numpy
.
int32
assert
type
(
y1
)
==
numpy
.
int32
if
(
y1
<
0
or
y1
>=
M1
):
#must also check overflow
if
(
y1
<
0
or
y1
>=
M1
):
#must also check overflow
...
@@ -135,6 +140,7 @@ def mrg_next_value(rstate, new_rstate):
...
@@ -135,6 +140,7 @@ def mrg_next_value(rstate, new_rstate):
else
:
else
:
return
(
x11
-
x21
)
*
NORM
return
(
x11
-
x21
)
*
NORM
class
mrg_uniform_base
(
Op
):
class
mrg_uniform_base
(
Op
):
def
__init__
(
self
,
output_type
,
inplace
=
False
):
def
__init__
(
self
,
output_type
,
inplace
=
False
):
Op
.
__init__
(
self
)
Op
.
__init__
(
self
)
...
@@ -145,17 +151,19 @@ class mrg_uniform_base(Op):
...
@@ -145,17 +151,19 @@ class mrg_uniform_base(Op):
self
.
warned_numpy_version
=
False
self
.
warned_numpy_version
=
False
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
\
return
(
type
(
self
)
==
type
(
other
)
and
and
self
.
output_type
==
other
.
output_type
\
self
.
output_type
==
other
.
output_type
and
and
self
.
inplace
==
other
.
inplace
self
.
inplace
==
other
.
inplace
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
output_type
)
^
hash
(
self
.
inplace
)
return
hash
(
type
(
self
))
^
hash
(
self
.
output_type
)
^
hash
(
self
.
inplace
)
def
__str__
(
self
):
def
__str__
(
self
):
if
self
.
inplace
:
if
self
.
inplace
:
s
=
"inplace"
s
=
"inplace"
else
:
s
=
"no_inplace"
else
:
return
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
output_type
,
s
)
s
=
"no_inplace"
return
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
output_type
,
s
)
def
make_node
(
self
,
rstate
,
size
):
def
make_node
(
self
,
rstate
,
size
):
# error checking slightly redundant here, since
# error checking slightly redundant here, since
...
@@ -166,7 +174,7 @@ class mrg_uniform_base(Op):
...
@@ -166,7 +174,7 @@ class mrg_uniform_base(Op):
[
rstate
,
size
],
[
rstate
,
size
],
[
rstate
.
type
(),
self
.
output_type
()])
[
rstate
.
type
(),
self
.
output_type
()])
def
grad
(
self
,
inputs
,
ograd
):
def
grad
(
self
,
inputs
,
ograd
):
return
[
None
for
i
in
inputs
]
return
[
None
for
i
in
inputs
]
def
R_op
(
self
,
inputs
,
eval_points
):
def
R_op
(
self
,
inputs
,
eval_points
):
...
@@ -187,8 +195,8 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -187,8 +195,8 @@ class mrg_uniform(mrg_uniform_base):
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
):
rstate
,
size
=
inp
rstate
,
size
=
inp
o_rstate
,
o_sample
=
out
o_rstate
,
o_sample
=
out
numpy_version
=
numpy
.
__version__
.
split
(
'.'
)
numpy_version
=
numpy
.
__version__
.
split
(
'.'
)
if
not
self
.
warned_numpy_version
and
int
(
numpy_version
[
0
])
<=
1
and
int
(
numpy_version
[
1
])
<
3
:
if
not
self
.
warned_numpy_version
and
int
(
numpy_version
[
0
])
<=
1
and
int
(
numpy_version
[
1
])
<
3
:
print
"Warning: you must use numpy version 1.3.0 or higher with the python version of this op. Otherwise numpy leak memory. and numpy"
print
"Warning: you must use numpy version 1.3.0 or higher with the python version of this op. Otherwise numpy leak memory. and numpy"
self
.
warned_numpy_version
=
True
self
.
warned_numpy_version
=
True
...
@@ -201,20 +209,21 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -201,20 +209,21 @@ class mrg_uniform(mrg_uniform_base):
for
s
in
size
:
for
s
in
size
:
n_elements
*=
s
n_elements
*=
s
n_streams
,
_
=
rstate
.
shape
n_streams
,
_
=
rstate
.
shape
rval
=
numpy
.
zeros
(
n_elements
,
dtype
=
self
.
output_type
.
dtype
)
rval
=
numpy
.
zeros
(
n_elements
,
dtype
=
self
.
output_type
.
dtype
)
err_orig
=
numpy
.
seterr
(
over
=
'ignore'
)
err_orig
=
numpy
.
seterr
(
over
=
'ignore'
)
try
:
try
:
for
i
in
xrange
(
n_elements
):
for
i
in
xrange
(
n_elements
):
sample
=
mrg_next_value
(
rstate
[
i
%
n_streams
],
rstate
[
i
%
n_streams
])
sample
=
mrg_next_value
(
rstate
[
i
%
n_streams
],
rstate
[
i
%
n_streams
])
rval
[
i
]
=
sample
rval
[
i
]
=
sample
finally
:
finally
:
numpy
.
seterr
(
**
err_orig
)
numpy
.
seterr
(
**
err_orig
)
o_rstate
[
0
]
=
node
.
outputs
[
0
]
.
type
.
filter
(
rstate
)
# send to GPU if necessary
o_rstate
[
0
]
=
node
.
outputs
[
0
]
.
type
.
filter
(
rstate
)
# send to GPU if necessary
o_sample
[
0
]
=
node
.
outputs
[
1
]
.
type
.
filter
(
rval
.
reshape
(
size
))
# send to GPU if necessary
o_sample
[
0
]
=
node
.
outputs
[
1
]
.
type
.
filter
(
rval
.
reshape
(
size
))
# send to GPU if necessary
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
rstate
,
size
=
inp
rstate
,
size
=
inp
...
@@ -228,7 +237,7 @@ class mrg_uniform(mrg_uniform_base):
...
@@ -228,7 +237,7 @@ class mrg_uniform(mrg_uniform_base):
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
if
self
.
output_type
.
dtype
==
'float32'
:
if
self
.
output_type
.
dtype
==
'float32'
:
otype
=
'float'
otype
=
'float'
NORM
=
'4.6566126e-10f'
#
numpy.float32(1.0/(2**31+65))
NORM
=
'4.6566126e-10f'
#
numpy.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# numpy.float32(number * M1) < 1.0
# numpy.float32(number * M1) < 1.0
else
:
else
:
...
@@ -392,7 +401,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
...
@@ -392,7 +401,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
def
c_support_code_apply
(
self
,
node
,
nodename
):
def
c_support_code_apply
(
self
,
node
,
nodename
):
if
self
.
output_type
.
dtype
==
'float32'
:
if
self
.
output_type
.
dtype
==
'float32'
:
otype
=
'float'
otype
=
'float'
NORM
=
'4.6566126e-10f'
#
numpy.float32(1.0/(2**31+65))
NORM
=
'4.6566126e-10f'
#
numpy.float32(1.0/(2**31+65))
# this was determined by finding the biggest number such that
# this was determined by finding the biggest number such that
# numpy.float32(number * M1) < 1.0
# numpy.float32(number * M1) < 1.0
else
:
else
:
...
@@ -476,7 +485,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
...
@@ -476,7 +485,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
}
}
}
}
"""
%
locals
()
"""
%
locals
()
def
c_code
(
self
,
node
,
nodename
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
nodename
,
inp
,
out
,
sub
):
rstate
,
size
=
inp
rstate
,
size
=
inp
...
@@ -491,7 +500,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
...
@@ -491,7 +500,7 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
else
:
else
:
otype
=
'double'
otype
=
'double'
SYNC
=
"CNDA_THREAD_SYNC"
;
SYNC
=
"CNDA_THREAD_SYNC"
return
"""
return
"""
//////// <code generated by mrg_uniform>
//////// <code generated by mrg_uniform>
...
@@ -593,7 +602,8 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
...
@@ -593,7 +602,8 @@ class GPU_mrg_uniform(mrg_uniform_base, GpuOp):
}
}
//////// </ code generated by mrg_uniform>
//////// </ code generated by mrg_uniform>
"""
%
locals
()
"""
%
locals
()
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
7
,)
return
(
7
,)
...
@@ -662,7 +672,7 @@ class MRG_RandomStreams(object):
...
@@ -662,7 +672,7 @@ class MRG_RandomStreams(object):
elif
seed
>=
M2
:
elif
seed
>=
M2
:
raise
ValueError
(
'seed should be less than
%
i'
%
M2
,
seed
)
raise
ValueError
(
'seed should be less than
%
i'
%
M2
,
seed
)
self
.
rstate
=
numpy
.
asarray
([
seed
]
*
6
,
dtype
=
'int32'
)
self
.
rstate
=
numpy
.
asarray
([
seed
]
*
6
,
dtype
=
'int32'
)
elif
len
(
seed
)
==
6
:
elif
len
(
seed
)
==
6
:
if
seed
[
0
]
==
0
and
seed
[
1
]
==
0
and
seed
[
2
]
==
0
:
if
seed
[
0
]
==
0
and
seed
[
1
]
==
0
and
seed
[
2
]
==
0
:
raise
ValueError
(
'The first 3 values of seed should not be all 0'
,
seed
)
raise
ValueError
(
'The first 3 values of seed should not be all 0'
,
seed
)
if
seed
[
3
]
==
0
and
seed
[
4
]
==
0
and
seed
[
5
]
==
0
:
if
seed
[
3
]
==
0
and
seed
[
4
]
==
0
and
seed
[
5
]
==
0
:
...
@@ -690,7 +700,7 @@ class MRG_RandomStreams(object):
...
@@ -690,7 +700,7 @@ class MRG_RandomStreams(object):
"""
"""
assert
n_streams
<
2
**
72
assert
n_streams
<
2
**
72
assert
n_streams
>
0
assert
n_streams
>
0
rval
=
numpy
.
zeros
((
n_streams
,
6
),
dtype
=
'int32'
)
rval
=
numpy
.
zeros
((
n_streams
,
6
),
dtype
=
'int32'
)
rval
[
0
]
=
self
.
rstate
rval
[
0
]
=
self
.
rstate
for
i
in
xrange
(
1
,
n_streams
):
for
i
in
xrange
(
1
,
n_streams
):
rval
[
i
]
=
ff_2p72
(
rval
[
i
-
1
])
rval
[
i
]
=
ff_2p72
(
rval
[
i
-
1
])
...
@@ -776,11 +786,13 @@ class MRG_RandomStreams(object):
...
@@ -776,11 +786,13 @@ class MRG_RandomStreams(object):
# currently no Theano node that will do a frombuffer
# currently no Theano node that will do a frombuffer
# reinterpretation.
# reinterpretation.
u
=
self
.
pretty_return
(
node_rstate
,
u
=
self
.
pretty_return
(
node_rstate
,
*
GPU_mrg_uniform
.
new
(
node_rstate
,
ndim
,
dtype
,
size
))
*
GPU_mrg_uniform
.
new
(
node_rstate
,
ndim
,
dtype
,
size
))
else
:
else
:
node_rstate
=
shared
(
self
.
get_substream_rstates
(
nstreams
))
node_rstate
=
shared
(
self
.
get_substream_rstates
(
nstreams
))
u
=
self
.
pretty_return
(
node_rstate
,
u
=
self
.
pretty_return
(
node_rstate
,
*
mrg_uniform
.
new
(
node_rstate
,
ndim
,
dtype
,
size
))
*
mrg_uniform
.
new
(
node_rstate
,
ndim
,
dtype
,
size
))
r
=
u
*
(
high
-
low
)
+
low
r
=
u
*
(
high
-
low
)
+
low
if
u
.
type
.
broadcastable
!=
r
.
type
.
broadcastable
:
if
u
.
type
.
broadcastable
!=
r
.
type
.
broadcastable
:
...
@@ -934,4 +946,6 @@ def mrg_random_make_inplace(node):
...
@@ -934,4 +946,6 @@ def mrg_random_make_inplace(node):
new_op
=
op
.
__class__
(
op
.
output_type
,
inplace
=
True
)
new_op
=
op
.
__class__
(
op
.
output_type
,
inplace
=
True
)
return
new_op
.
make_node
(
*
node
.
inputs
)
.
outputs
return
new_op
.
make_node
(
*
node
.
inputs
)
.
outputs
return
False
return
False
optdb
.
register
(
'random_make_inplace_mrg'
,
opt
.
in2out
(
mrg_random_make_inplace
,
ignore_newtrees
=
True
),
99
,
'fast_run'
,
'inplace'
)
optdb
.
register
(
'random_make_inplace_mrg'
,
opt
.
in2out
(
mrg_random_make_inplace
,
ignore_newtrees
=
True
),
99
,
'fast_run'
,
'inplace'
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论