Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
5a3a683b
提交
5a3a683b
authored
3月 06, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added comments and tests for RandomFunction
上级
cbe7fef9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
68 行增加
和
6 行删除
+68
-6
raw_random.py
theano/tensor/raw_random.py
+17
-4
test_raw_random.py
theano/tensor/tests/test_raw_random.py
+51
-2
没有找到文件。
theano/tensor/raw_random.py
浏览文件 @
5a3a683b
...
@@ -23,6 +23,7 @@ class RandomStateType(gof.Type):
...
@@ -23,6 +23,7 @@ class RandomStateType(gof.Type):
"""
"""
def
__str__
(
self
):
def
__str__
(
self
):
return
'RandomStateType'
return
'RandomStateType'
def
filter
(
self
,
data
,
strict
=
False
):
def
filter
(
self
,
data
,
strict
=
False
):
if
self
.
is_valid_value
(
data
):
if
self
.
is_valid_value
(
data
):
return
data
return
data
...
@@ -115,7 +116,6 @@ class RandomFunction(gof.Op):
...
@@ -115,7 +116,6 @@ class RandomFunction(gof.Op):
the random draw.
the random draw.
"""
"""
args
=
map
(
tensor
.
as_tensor
,
args
)
if
shape
==
()
or
shape
==
[]:
if
shape
==
()
or
shape
==
[]:
shape
=
tensor
.
lvector
()
shape
=
tensor
.
lvector
()
else
:
else
:
...
@@ -127,20 +127,33 @@ class RandomFunction(gof.Op):
...
@@ -127,20 +127,33 @@ class RandomFunction(gof.Op):
print
>>
sys
.
stderr
,
'WARNING: RandomState instances should be in RandomStateType'
print
>>
sys
.
stderr
,
'WARNING: RandomState instances should be in RandomStateType'
if
0
:
if
0
:
raise
TypeError
(
'r must be RandomStateType instance'
,
r
)
raise
TypeError
(
'r must be RandomStateType instance'
,
r
)
# assert shape.type == tensor.lvector doesn't work because we want to ignore the
# the following doesn't work because we want to ignore the broadcastable flags in
# broadcastable vector
# shape.type
assert
len
(
args
)
<=
len
(
self
.
args
)
# assert shape.type == tensor.lvector
# convert args to Tensor instances
# and append enough None's to match the length of self.args
args
=
map
(
tensor
.
as_tensor
,
args
)
if
len
(
args
)
>
len
(
self
.
args
):
raise
TypeError
(
'Too many args for this kind of random generator'
)
args
+=
(
None
,)
*
(
len
(
self
.
args
)
-
len
(
args
))
args
+=
(
None
,)
*
(
len
(
self
.
args
)
-
len
(
args
))
assert
len
(
args
)
==
len
(
self
.
args
)
# build the inputs to this Apply by overlaying args on self.args
inputs
=
[]
inputs
=
[]
for
arg
,
default
in
zip
(
args
,
self
.
args
):
for
arg
,
default
in
zip
(
args
,
self
.
args
):
assert
arg
is
None
or
default
.
type
.
dtype
==
arg
.
type
.
dtype
assert
arg
is
None
or
default
.
type
.
dtype
==
arg
.
type
.
dtype
input
=
default
if
arg
is
None
else
arg
input
=
default
if
arg
is
None
else
arg
inputs
.
append
(
input
)
inputs
.
append
(
input
)
return
gof
.
Apply
(
self
,
return
gof
.
Apply
(
self
,
[
r
,
shape
]
+
inputs
,
[
r
,
shape
]
+
inputs
,
[
r
.
type
(),
self
.
outtype
()])
[
r
.
type
(),
self
.
outtype
()])
def
perform
(
self
,
node
,
inputs
,
(
rout
,
out
)):
def
perform
(
self
,
node
,
inputs
,
(
rout
,
out
)):
# Use self.fn to draw shape worth of random numbers.
# Numbers are drawn from r if self.inplace is True, and from a copy of r if
# self.inplace is False
r
,
shape
,
args
=
inputs
[
0
],
inputs
[
1
],
inputs
[
2
:]
r
,
shape
,
args
=
inputs
[
0
],
inputs
[
1
],
inputs
[
2
:]
assert
type
(
r
)
==
numpy
.
random
.
RandomState
assert
type
(
r
)
==
numpy
.
random
.
RandomState
r_orig
=
r
r_orig
=
r
...
...
theano/tensor/tests/test_raw_random.py
浏览文件 @
5a3a683b
...
@@ -16,7 +16,6 @@ class T_random_function(unittest.TestCase):
...
@@ -16,7 +16,6 @@ class T_random_function(unittest.TestCase):
assert
getattr
(
rf
,
'destroy_map'
,
{})
==
{}
assert
getattr
(
rf
,
'destroy_map'
,
{})
==
{}
rng_R
=
random_state_type
()
rng_R
=
random_state_type
()
print
rng_R
post_r
,
out
=
rf
(
rng_R
,
(
4
,))
post_r
,
out
=
rf
(
rng_R
,
(
4
,))
...
@@ -37,8 +36,58 @@ class T_random_function(unittest.TestCase):
...
@@ -37,8 +36,58 @@ class T_random_function(unittest.TestCase):
assert
rf
.
inplace
assert
rf
.
inplace
assert
getattr
(
rf
,
'destroy_map'
,
{})
!=
{}
assert
getattr
(
rf
,
'destroy_map'
,
{})
!=
{}
def
test_args
(
self
):
"""Test that arguments to RandomFunction are honored"""
rf2
=
RandomFunction
(
numpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
,
-
2.0
,
2.0
)
rf4
=
RandomFunction
(
numpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
,
-
4.0
,
4.0
,
inplace
=
True
)
rng_R
=
random_state_type
()
# use make_node to override some of the self.args
post_r2
,
out2
=
rf2
(
rng_R
,
(
4
,))
post_r2_4
,
out2_4
=
rf2
(
rng_R
,
(
4
,),
-
4.0
)
post_r2_4_4
,
out2_4_4
=
rf2
(
rng_R
,
(
4
,),
-
4.0
,
4.0
)
post_r4
,
out4
=
rf4
(
rng_R
,
(
4
,))
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r4
,
mutable
=
True
)],
[
out2
,
out4
,
out2_4
,
out2_4_4
],
accept_inplace
=
True
)
f2
,
f4
,
f2_4
,
f2_4_4
=
f
()
f2b
,
f4b
,
f2_4b
,
f2_4_4b
=
f
()
assert
numpy
.
allclose
(
f2
*
2
,
f4
)
assert
numpy
.
allclose
(
f2_4_4
,
f4
)
assert
not
numpy
.
allclose
(
f4
,
f4b
)
def
test_inplace_optimization
(
self
):
def
test_inplace_optimization
(
self
):
print
>>
sys
.
stderr
,
"WARNING NOT IMPLEMENTED T_random_function.test_inplace_optimization"
"""Test that arguments to RandomFunction are honored"""
#inplace = False
rf2
=
RandomFunction
(
numpy
.
random
.
RandomState
.
uniform
,
tensor
.
dvector
,
-
2.0
,
2.0
)
rng_R
=
random_state_type
()
# use make_node to override some of the self.args
post_r2
,
out2
=
rf2
(
rng_R
,
(
4
,))
f
=
compile
.
function
(
[
compile
.
In
(
rng_R
,
value
=
numpy
.
random
.
RandomState
(
55
),
update
=
post_r2
,
mutable
=
True
)],
out2
,
mode
=
'FAST_RUN'
)
#DEBUG_MODE can't pass the id-based test below
# test that the RandomState object stays the same from function call to function call,
# but that the values returned change from call to call.
id0
=
id
(
f
[
rng_R
])
val0
=
f
()
assert
id0
==
id
(
f
[
rng_R
])
val1
=
f
()
assert
id0
==
id
(
f
[
rng_R
])
assert
not
numpy
.
allclose
(
val0
,
val1
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论