Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
540af3cc
提交
540af3cc
authored
5月 12, 2011
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
差异文件
Merged
上级
2deb19cb
1bac3e5b
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
104 行增加
和
47 行删除
+104
-47
rng_mrg.py
theano/sandbox/rng_mrg.py
+60
-24
test_rng_mrg.py
theano/sandbox/test_rng_mrg.py
+18
-7
basic.py
theano/scalar/basic.py
+5
-5
basic.py
theano/tensor/basic.py
+10
-7
test_basic.py
theano/tensor/tests/test_basic.py
+3
-2
test_incsubtensor.py
theano/tensor/tests/test_incsubtensor.py
+3
-2
test_opt.py
theano/tensor/tests/test_opt.py
+5
-0
没有找到文件。
theano/sandbox/rng_mrg.py
浏览文件 @
540af3cc
...
...
@@ -589,6 +589,35 @@ class GPU_mrg_uniform(mrg_uniform_base):
def
c_code_cache_version
(
self
):
return
(
4
,)
def
guess_n_streams
(
size
,
warn
=
True
):
"""
Return a guess at a good number of streams.
:param warn: If True, warn when a guess cannot be made (in which case
we return 30 * 256).
"""
# TODO: a smart way of choosing the number of streams, see #612.
# Note that this code was moved out of `MRG_RandomStreams` so that it can
# be easily accessed from tests, where we want to disable the warning.
if
(
isinstance
(
size
,
(
tuple
,
list
))
and
all
([
isinstance
(
i
,
int
)
for
i
in
size
])):
# We can make a guess.
r
=
1
for
s
in
size
:
r
*=
s
if
r
>
6
:
r
=
r
/
6
# chosen as fastest for rbm_benchmark
return
r
else
:
if
warn
:
assert
False
print
>>
sys
.
stderr
,
(
"MRG_RandomStreams Can't determine #streams from "
"size (
%
s), guessing 30*256"
)
%
str
(
size
)
return
30
*
256
class
MRG_RandomStreams
(
object
):
"""Module component with similar interface to numpy.random (numpy.random.RandomState)"""
...
...
@@ -654,18 +683,7 @@ class MRG_RandomStreams(object):
return
rval
def
n_streams
(
self
,
size
):
# TODO: a smart way of choosing the number of streams, see #612.
if
isinstance
(
size
,
(
tuple
,
list
))
and
all
([
isinstance
(
i
,
int
)
for
i
in
size
]):
r
=
1
for
s
in
size
:
r
*=
s
if
r
>
6
:
r
=
r
/
6
# chosen as fastest for rbm_benchmark
return
r
print
>>
sys
.
stderr
,
(
"MRG_RandomStreams Can't determine #streams from "
"size (
%
s), guessing 30*256"
)
%
str
(
size
)
return
30
*
256
return
guess_n_streams
(
size
,
warn
=
True
)
def
pretty_return
(
self
,
node_rstate
,
new_rstate
,
sample
):
sample
.
rstate
=
node_rstate
...
...
@@ -674,7 +692,8 @@ class MRG_RandomStreams(object):
node_rstate
.
default_update
=
new_rstate
return
sample
def
uniform
(
self
,
size
=
None
,
low
=
0.0
,
high
=
1.0
,
ndim
=
None
,
dtype
=
config
.
floatX
,
nstreams
=
None
):
def
uniform
(
self
,
size
,
low
=
0.0
,
high
=
1.0
,
ndim
=
None
,
dtype
=
'floatX'
,
nstreams
=
None
):
"""
Sample a tensor of given size whose element from a uniform
distribution between low and high.
...
...
@@ -683,10 +702,14 @@ class MRG_RandomStreams(object):
ndim may be a plain integer to supplement the missing
information.
:param
:
size: Can be a list of integer or Theano variable
:param size: Can be a list of integer or Theano variable
(ex: the shape of other Theano Variable)
TODO: can size be None?
:param dtype: The output data type.
"""
if
dtype
==
'floatX'
:
dtype
=
config
.
floatX
if
isinstance
(
size
,
tuple
):
msg
=
"size must be a tuple of int or a Theano variable"
assert
all
([
isinstance
(
i
,
int
)
or
isinstance
(
i
,
Variable
)
...
...
@@ -728,16 +751,19 @@ class MRG_RandomStreams(object):
raise
NotImplementedError
(
'Increase the size to match the broadcasting pattern of `low` and `high` arguments'
)
return
r
def
binomial
(
self
,
size
=
None
,
n
=
1
,
p
=
0.5
,
ndim
=
None
,
dtype
=
'int64'
):
def
binomial
(
self
,
size
=
None
,
n
=
1
,
p
=
0.5
,
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
if
n
==
1
:
if
dtype
==
'float32'
and
self
.
use_cuda
:
return
cast
(
self
.
uniform
(
size
=
size
,
dtype
=
dtype
)
<
p
,
dtype
)
if
dtype
==
'float32'
and
self
.
use_cuda
:
x
=
self
.
uniform
(
size
=
size
,
dtype
=
dtype
,
nstreams
=
nstreams
)
else
:
return
cast
(
self
.
uniform
(
size
=
size
)
<
p
,
dtype
)
x
=
self
.
uniform
(
size
=
size
,
nstreams
=
nstreams
)
return
cast
(
x
<
p
,
dtype
)
else
:
raise
NotImplementedError
(
"MRG_RandomStreams.binomial with n > 1"
)
def
multinomial
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
ndim
=
None
,
dtype
=
'int64'
):
def
multinomial
(
self
,
size
=
None
,
n
=
1
,
pvals
=
None
,
ndim
=
None
,
dtype
=
'int64'
,
nstreams
=
None
):
"""
Sample `n` (currently `n` needs to be 1) times from a multinomial
distribution defined by probabilities pvals.
...
...
@@ -758,22 +784,31 @@ class MRG_RandomStreams(object):
ndim
,
size
,
pvals
[:,
0
])
assert
ndim
==
1
bcast
=
bcast
+
(
pvals
.
type
.
broadcastable
[
-
1
],)
unis
=
self
.
uniform
(
size
=
size
,
ndim
=
1
)
unis
=
self
.
uniform
(
size
=
size
,
ndim
=
1
,
nstreams
=
nstreams
)
op
=
multinomial
.
MultinomialFromUniform
(
dtype
)
return
op
(
pvals
,
unis
)
else
:
raise
NotImplementedError
((
"MRG_RandomStreams.multinomial only"
" implemented with n == 1 and pvals.ndim = 2"
))
def
normal
(
self
,
size
=
None
,
avg
=
0.0
,
std
=
1.0
,
ndim
=
None
,
dtype
=
config
.
floatX
):
def
normal
(
self
,
size
=
None
,
avg
=
0.0
,
std
=
1.0
,
ndim
=
None
,
dtype
=
'floatX'
,
nstreams
=
None
):
"""
:param: size: Can be a list of integer or Theano variable(ex: the shape of other Theano Variable)
:param size: Can be a list of integers or Theano variables (ex: the
shape of another Theano Variable)
:param dtype: The output data type.
:param nstreams: Number of streams.
"""
# We need an even number of ]0,1[ samples. Then we split them
# in two halves. First half becomes our U1's for Box-Muller,
# second half our U2's. See Wikipedia page:
# http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
if
dtype
==
'floatX'
:
dtype
=
config
.
floatX
evened
=
False
constant
=
False
if
isinstance
(
size
,
tuple
)
and
all
([
isinstance
(
i
,
int
)
for
i
in
size
]):
...
...
@@ -786,7 +821,8 @@ class MRG_RandomStreams(object):
else
:
#if even, don't change, if odd, +1
n_samples
=
prod
(
size
)
+
(
prod
(
size
)
%
2
)
flattened
=
self
.
uniform
(
size
=
(
n_samples
,),
dtype
=
dtype
)
flattened
=
self
.
uniform
(
size
=
(
n_samples
,),
dtype
=
dtype
,
nstreams
=
nstreams
)
if
constant
:
U1
=
flattened
[:
n_samples
//
2
]
...
...
theano/sandbox/test_rng_mrg.py
浏览文件 @
540af3cc
...
...
@@ -350,7 +350,9 @@ def test_uniform():
print
'ON CPU with size=(
%
s):'
%
str
(
size
)
x
=
tensor
.
matrix
()
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
u
=
R
.
uniform
(
size
=
size
)
# Note: we specify `nstreams` to avoid a warning.
u
=
R
.
uniform
(
size
=
size
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
f
=
theano
.
function
(
var_input
,
u
,
mode
=
mode
)
assert
any
([
isinstance
(
node
.
op
,
theano
.
sandbox
.
rng_mrg
.
mrg_uniform
)
for
node
in
f
.
maker
.
env
.
toposort
()])
...
...
@@ -366,7 +368,8 @@ def test_uniform():
print
''
print
'ON GPU with size=(
%
s):'
%
str
(
size
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
u
=
R
.
uniform
(
size
=
size
,
dtype
=
'float32'
)
u
=
R
.
uniform
(
size
=
size
,
dtype
=
'float32'
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
assert
u
.
dtype
==
'float32'
#well, it's really that this test w GPU doesn't make sense otw
f
=
theano
.
function
(
var_input
,
theano
.
Out
(
theano
.
sandbox
.
cuda
.
basic_ops
.
gpu_from_host
(
u
),
...
...
@@ -421,7 +424,9 @@ def test_binomial():
print
''
print
'ON CPU with size=(
%
s) and mean(
%
d):'
%
(
str
(
size
),
mean
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
u
=
R
.
binomial
(
size
=
size
,
p
=
mean
)
# Note: we specify `nstreams` to avoid a warning.
u
=
R
.
binomial
(
size
=
size
,
p
=
mean
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
f
=
theano
.
function
(
var_input
,
u
,
mode
=
mode
)
theano
.
printing
.
debugprint
(
f
)
out
=
f
(
*
input
)
...
...
@@ -433,7 +438,9 @@ def test_binomial():
print
''
print
'ON GPU with size=(
%
s) and mean(
%
d):'
%
(
str
(
size
),
mean
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
u
=
R
.
binomial
(
size
=
size
,
p
=
mean
,
dtype
=
'float32'
)
u
=
R
.
binomial
(
size
=
size
,
p
=
mean
,
dtype
=
'float32'
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
assert
u
.
dtype
==
'float32'
#well, it's really that this test w GPU doesn't make sense otw
f
=
theano
.
function
(
var_input
,
theano
.
Out
(
theano
.
sandbox
.
cuda
.
basic_ops
.
gpu_from_host
(
u
),
...
...
@@ -478,7 +485,9 @@ def test_normal0():
print
'ON CPU:'
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
n
=
R
.
normal
(
size
=
size
,
avg
=
avg
,
std
=
std
)
# Note: we specify `nstreams` to avoid a warning.
n
=
R
.
normal
(
size
=
size
,
avg
=
avg
,
std
=
std
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
f
=
theano
.
function
(
var_input
,
n
,
mode
=
mode
)
theano
.
printing
.
debugprint
(
f
)
out
=
f
(
*
input
)
...
...
@@ -491,7 +500,8 @@ def test_normal0():
print
''
print
'ON GPU:'
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
True
)
n
=
R
.
normal
(
size
=
size
,
avg
=
avg
,
std
=
std
,
dtype
=
'float32'
)
n
=
R
.
normal
(
size
=
size
,
avg
=
avg
,
std
=
std
,
dtype
=
'float32'
,
nstreams
=
rng_mrg
.
guess_n_streams
(
size
,
warn
=
False
))
assert
n
.
dtype
==
'float32'
#well, it's really that this test w GPU doesn't make sense otw
f
=
theano
.
function
(
var_input
,
theano
.
Out
(
theano
.
sandbox
.
cuda
.
basic_ops
.
gpu_from_host
(
n
),
...
...
@@ -557,7 +567,8 @@ def test_multinomial():
pvals
=
numpy
.
asarray
(
numpy
.
random
.
uniform
(
size
=
sample_size
))
pvals
=
numpy
.
apply_along_axis
(
lambda
row
:
row
/
numpy
.
sum
(
row
),
1
,
pvals
)
R
=
MRG_RandomStreams
(
234
,
use_cuda
=
False
)
m
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
config
.
floatX
)
# Note: we specify `nstreams` to avoid a warning.
m
=
R
.
multinomial
(
pvals
=
pvals
,
dtype
=
config
.
floatX
,
nstreams
=
30
*
256
)
f
=
theano
.
function
([],
m
,
mode
=
mode_
)
theano
.
printing
.
debugprint
(
f
)
out
=
f
()
...
...
theano/scalar/basic.py
浏览文件 @
540af3cc
...
...
@@ -1034,11 +1034,11 @@ def div_proxy(x, y):
# division"), we will change the semantics of "/" on integer types in
# Theano 0.4. Until then, it is forbidden to use "/" on integers.
raise
IntegerDivisionError
(
"Dividing two integers with '/' is
forbidden until Theano v0.4
"
"
is released (where the result will be a
floating point "
"
number). In the meantime, please either use '//' for intege
r "
"
division, or cast one of the arguments to a floating poin
t "
"
type for float
division."
)
"Dividing two integers with '/' is
currently forbidden
"
"
to avoid confusion between integer and
floating point "
"
divisions. Please either use '//' for integer division, o
r "
"
cast one of the arguments to a floating point type for floa
t "
"division."
)
else
:
return
true_div
(
x
,
y
)
...
...
theano/tensor/basic.py
浏览文件 @
540af3cc
...
...
@@ -2589,11 +2589,11 @@ def div_proxy(x, y):
as_tensor_variable
(
y
)
.
dtype
in
discrete_dtypes
):
# See the same in scalar/basic.py
raise
IntegerDivisionError
(
"Dividing two integer arrays with '/' is
forbidden until
"
"
Theano v0.4 is released (where the result will be a floating
"
"
point number). In the meantime, please either use '//' f
or "
"
integer division, or cast one of the arguments to a floating
"
"
point type for float
division."
)
"Dividing two integer arrays with '/' is
currently forbidden
"
"
to avoid confusion between integer and floating point
"
"
divisions. Please either use '//' for integer division,
or "
"
cast one of the arguments to a floating point type for float
"
"division."
)
else
:
return
true_div
(
x
,
y
)
...
...
@@ -3044,8 +3044,11 @@ def setsubtensor(x, y, idx_list, inplace=False):
print
>>
sys
.
stderr
,
"tensor.setsubtensor is deprecated - please use set_subtensor"
the_op
=
IncSubtensor
(
idx_list
,
inplace
,
set_instead_of_inc
=
True
)
return
the_op
(
x
,
y
,
*
Subtensor
.
collapse
(
idx_list
,
lambda
entry
:
isinstance
(
entry
,
Variable
)))
def
incsubtensor
(
x
,
y
,
idx_list
,
inplace
=
False
):
print
>>
sys
.
stderr
,
"tensor.incsubtensor is deprecated - please use inc_subtensor"
def
incsubtensor
(
x
,
y
,
idx_list
,
inplace
=
False
,
show_warning
=
True
):
# Note that `show_warning` should only be set to False by tests, in order
# to make sure this old code is still working.
if
show_warning
:
print
>>
sys
.
stderr
,
"tensor.incsubtensor is deprecated - please use inc_subtensor"
the_op
=
IncSubtensor
(
idx_list
,
inplace
,
set_instead_of_inc
=
False
)
return
the_op
(
x
,
y
,
*
Subtensor
.
collapse
(
idx_list
,
lambda
entry
:
isinstance
(
entry
,
Variable
)))
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
540af3cc
...
...
@@ -458,8 +458,9 @@ _grad_broadcast_div_mod_normal = dict(same_shapes = (rand(2, 3), rand(2, 3)),
div_grad_rtol
=
None
if
config
.
floatX
==
'float32'
:
#We raise the relative tolerence for the grad as their is error in float32
#This is probably caused by our way of computing the gradient error.
# We raise the relative tolerance for the grad as there can be errors in
# float32.
# This is probably caused by our way of computing the gradient error.
div_grad_rtol
=
0.025
DivTester
=
makeBroadcastTester
(
op
=
true_div
,
expected
=
lambda
x
,
y
:
x
/
y
,
...
...
theano/tensor/tests/test_incsubtensor.py
浏览文件 @
540af3cc
...
...
@@ -32,7 +32,8 @@ class Test_incsubtensor(unittest.TestCase):
if
do_set
:
resut
=
T
.
setsubtensor
(
a
,
increment
,
[
sl1
,
sl2
])
else
:
resut
=
T
.
incsubtensor
(
a
,
increment
,
[
sl1
,
sl2
])
resut
=
T
.
incsubtensor
(
a
,
increment
,
[
sl1
,
sl2
],
show_warning
=
False
)
f
=
theano
.
function
([
a
,
increment
,
sl2_end
],
resut
)
...
...
@@ -59,7 +60,7 @@ class Test_incsubtensor(unittest.TestCase):
def
inc_slice
(
*
s
):
def
just_numeric_args
(
a
,
b
):
return
T
.
incsubtensor
(
a
,
b
,
s
)
return
T
.
incsubtensor
(
a
,
b
,
s
,
show_warning
=
False
)
return
just_numeric_args
# vector
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
540af3cc
...
...
@@ -2490,6 +2490,7 @@ class T_local_sum(unittest.TestCase):
assert
numpy
.
allclose
(
f
(
input
),
input
.
sum
())
config
.
warn
.
sum_sum_bug
=
False
f
=
theano
.
function
([
a
],
a
.
sum
(
0
)
.
sum
(
0
)
.
sum
(
0
),
mode
=
self
.
mode
)
assert
len
(
f
.
maker
.
env
.
nodes
)
==
1
assert
numpy
.
allclose
(
f
(
input
),
input
.
sum
())
...
...
@@ -2499,6 +2500,7 @@ class T_local_sum(unittest.TestCase):
input
=
numpy
.
arange
(
3
*
3
*
3
,
dtype
=
config
.
floatX
)
.
reshape
(
3
,
3
,
3
)
dims
=
[(
0
,
0
),(
1
,
0
),(
2
,
0
),(
0
,
1
),(
1
,
1
),(
2
,
1
)]
config
.
warn
.
sum_sum_bug
=
False
for
d
,
dd
in
dims
:
f
=
theano
.
function
([
a
],
a
.
sum
(
d
)
.
sum
(
dd
),
mode
=
self
.
mode
)
assert
numpy
.
allclose
(
f
(
input
),
input
.
sum
(
d
)
.
sum
(
dd
))
...
...
@@ -2544,6 +2546,7 @@ class T_local_sum(unittest.TestCase):
assert
len
(
f
.
maker
.
env
.
nodes
)
==
nb_nodes
[
2
]
assert
f
.
maker
.
env
.
toposort
()[
-
1
]
.
op
==
T
.
alloc
config
.
warn
.
sum_sum_bug
=
False
for
d
,
dd
in
[(
0
,
0
),(
1
,
0
),(
2
,
0
),(
0
,
1
),(
1
,
1
),(
2
,
1
)]:
f
=
theano
.
function
([
a
],
t_like
(
a
)
.
sum
(
d
)
.
sum
(
dd
),
mode
=
mode
)
print
f
.
maker
.
env
.
toposort
()
...
...
@@ -2603,6 +2606,8 @@ class T_local_sum_dimshuffle(unittest.TestCase):
c_val
=
rng
.
randn
(
2
,
2
,
2
)
.
astype
(
config
.
floatX
)
d_val
=
numpy
.
asarray
(
rng
.
randn
(),
config
.
floatX
)
config
.
warn
.
sum_sum_bug
=
False
config
.
warn
.
sum_div_dimshuffle_bug
=
False
for
i
,
s
in
enumerate
(
sums
):
print
i
f
=
theano
.
function
([
a
,
b
,
c
,
d
],
s
,
mode
=
self
.
mode
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论