Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7a49c0da
提交
7a49c0da
authored
3月 09, 2009
作者:
james@X40
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
deprecated RModule, added comments to RandomStreams
上级
0786f2af
隐藏空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
273 行增加
和
103 行删除
+273
-103
__init__.py
theano/tensor/__init__.py
+4
-4
__init__.py
theano/tensor/deprecated/__init__.py
+0
-0
__init__.pyc
theano/tensor/deprecated/__init__.pyc
+0
-0
rmodule.py
theano/tensor/deprecated/rmodule.py
+94
-0
test_rmodule.py
theano/tensor/deprecated/test_rmodule.py
+0
-0
test_rmodule.pyc
theano/tensor/deprecated/test_rmodule.pyc
+0
-0
randomstreams.py
theano/tensor/randomstreams.py
+38
-93
test_naacl09.py
theano/tensor/tests/test_naacl09.py
+9
-6
test_randomstreams.py
theano/tensor/tests/test_randomstreams.py
+128
-0
没有找到文件。
theano/tensor/__init__.py
浏览文件 @
7a49c0da
...
...
@@ -6,11 +6,11 @@ from basic import *
import
opt
import
blas
import
raw_random
,
r
module
from
r
module
import
\
Random
Kit
,
RModule
import
raw_random
,
r
andomstreams
from
r
andomstreams
import
\
Random
Streams
random
=
Random
Kit
(
'random'
)
random
=
Random
Streams
(
seed
=
0xBAD5EED
)
"""Imitate the numpy.random symbol with a tensor.random one"""
from
elemwise
import
\
...
...
theano/tensor/deprecated/__init__.py
0 → 100644
浏览文件 @
7a49c0da
theano/tensor/deprecated/__init__.pyc
0 → 100644
浏览文件 @
7a49c0da
File added
theano/tensor/deprecated/rmodule.py
0 → 100644
浏览文件 @
7a49c0da
"""Define RModule, a Module providing random number streams in Theano graphs."""
__docformat__
=
"restructuredtext en"
import
sys
import
functools
from
functools
import
partial
from
collections
import
deque
import
numpy
from
...compile
import
(
SymbolicInputKit
,
SymbolicInput
,
Module
,
KitComponent
,
module
,
Method
,
Member
,
In
,
Component
)
from
...gof
import
Container
from
...tensor
import
raw_random
class
RandomKit
(
SymbolicInputKit
):
def
__init__
(
self
,
name
,
value
=
None
):
super
(
RandomKit
,
self
)
.
__init__
(
name
)
self
.
value
=
value
def
gen
(
self
,
op
,
*
args
,
**
kwargs
):
random_state_result
=
raw_random
.
random_state_type
()
new_r
,
out
=
op
(
random_state_result
,
*
args
,
**
kwargs
)
self
.
add_input
(
SymbolicInput
(
random_state_result
,
update
=
new_r
))
out
.
rng
=
new_r
out
.
auto
=
self
return
out
def
distribute
(
self
,
value
,
indices
,
containers
):
rg
=
partial
(
numpy
.
random
.
RandomState
(
int
(
value
))
.
randint
,
2
**
30
)
elems
=
deque
(
zip
(
indices
,
containers
))
i
=
0
while
elems
:
index
,
container
=
elems
.
popleft
()
while
i
<=
index
:
curr
=
rg
()
i
+=
1
rs
=
numpy
.
random
.
RandomState
(
int
(
curr
))
container
.
data
=
rs
def
binomial
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
binomial
,
*
args
,
**
kwargs
)
def
uniform
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
uniform
,
*
args
,
**
kwargs
)
def
normal
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
normal
,
*
args
,
**
kwargs
)
def
random_integers
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
random_integers
,
*
args
,
**
kwargs
)
rk
=
RandomKit
(
'rk'
,
0xBAD5EED
)
class
RModule
(
Module
):
"""Module providing random number streams in Theano graphs."""
def
__init__
(
self
,
components
=
{},
**
kwcomponents
):
super
(
RModule
,
self
)
.
__init__
(
components
,
**
kwcomponents
)
self
.
random
=
RandomKit
(
'rkit'
)
self
.
_rkit
=
KitComponent
(
self
.
random
)
def
__wrapper__
(
self
,
x
):
x
=
module
.
wrap
(
x
)
if
isinstance
(
x
,
Method
):
x
.
kits
+=
[
self
.
random
]
return
x
def
_instance_seed
(
self
,
inst
,
seed
,
recursive
=
True
):
seedgen
=
numpy
.
random
.
RandomState
(
seed
)
if
recursive
:
#Here, we recurse through all the components (inst2) contained in (inst)
#and seeds each subcomponent that is an RModule
for
path
,
c
in
self
.
flat_components_map
(
True
):
if
isinstance
(
c
,
RModule
):
inst2
=
inst
for
name
in
path
:
inst2
=
inst2
[
name
]
# A Kit (c._rkit.kit) contains a list of io.SymbolicIn instances
# and the distribute method takes a value (seed), a list of indices
# and a list of corresponding Container instances. In this
# situation it will reseed all the rngs using the containers
# associated to them.
c
.
_rkit
.
kit
.
distribute
(
seedgen
.
random_integers
(
2
**
30
),
xrange
(
len
(
inst2
.
_rkit
)),
inst2
.
_rkit
)
else
:
self
.
_rkit
.
kit
.
distribute
(
seedgen
.
random_integers
(
2
**
30
),
xrange
(
len
(
inst
.
_rkit
)),
inst
.
_rkit
)
theano/tensor/
tests
/test_rmodule.py
→
theano/tensor/
deprecated
/test_rmodule.py
浏览文件 @
7a49c0da
File moved
theano/tensor/deprecated/test_rmodule.pyc
0 → 100644
浏览文件 @
7a49c0da
File added
theano/tensor/r
module
.py
→
theano/tensor/r
andomstreams
.py
浏览文件 @
7a49c0da
"""Define R
Module, a Module providing random number streams in
Theano graphs."""
"""Define R
andomStreams, providing random number variables for
Theano graphs."""
__docformat__
=
"restructuredtext en"
import
sys
import
functools
from
functools
import
partial
from
collections
import
deque
import
sys
import
numpy
from
..compile
import
(
SymbolicInputKit
,
SymbolicInput
,
Module
,
KitComponent
,
module
,
Method
,
Member
,
In
,
Component
)
from
..compile
import
module
,
In
,
Component
from
..gof
import
Container
from
..tensor
import
raw_random
class
RandomKit
(
SymbolicInputKit
):
def
__init__
(
self
,
name
,
value
=
None
):
super
(
RandomKit
,
self
)
.
__init__
(
name
)
self
.
value
=
value
def
gen
(
self
,
op
,
*
args
,
**
kwargs
):
random_state_result
=
raw_random
.
random_state_type
()
new_r
,
out
=
op
(
random_state_result
,
*
args
,
**
kwargs
)
self
.
add_input
(
SymbolicInput
(
random_state_result
,
update
=
new_r
))
out
.
rng
=
new_r
out
.
auto
=
self
return
out
def
distribute
(
self
,
value
,
indices
,
containers
):
rg
=
partial
(
numpy
.
random
.
RandomState
(
int
(
value
))
.
randint
,
2
**
30
)
elems
=
deque
(
zip
(
indices
,
containers
))
i
=
0
while
elems
:
index
,
container
=
elems
.
popleft
()
while
i
<=
index
:
curr
=
rg
()
i
+=
1
rs
=
numpy
.
random
.
RandomState
(
int
(
curr
))
container
.
data
=
rs
def
binomial
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
binomial
,
*
args
,
**
kwargs
)
def
uniform
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
uniform
,
*
args
,
**
kwargs
)
def
normal
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
normal
,
*
args
,
**
kwargs
)
def
random_integers
(
self
,
*
args
,
**
kwargs
):
return
self
.
gen
(
raw_random
.
random_integers
,
*
args
,
**
kwargs
)
rk
=
RandomKit
(
'rk'
,
0xBAD5EED
)
class
RModule
(
Module
):
"""Module providing random number streams in Theano graphs."""
def
__init__
(
self
,
components
=
{},
**
kwcomponents
):
super
(
RModule
,
self
)
.
__init__
(
components
,
**
kwcomponents
)
self
.
random
=
RandomKit
(
'rkit'
)
self
.
_rkit
=
KitComponent
(
self
.
random
)
def
__wrapper__
(
self
,
x
):
x
=
module
.
wrap
(
x
)
if
isinstance
(
x
,
Method
):
x
.
kits
+=
[
self
.
random
]
return
x
def
_instance_seed
(
self
,
inst
,
seed
,
recursive
=
True
):
seedgen
=
numpy
.
random
.
RandomState
(
seed
)
if
recursive
:
#Here, we recurse through all the components (inst2) contained in (inst)
#and seeds each subcomponent that is an RModule
for
path
,
c
in
self
.
flat_components_map
(
True
):
if
isinstance
(
c
,
RModule
):
inst2
=
inst
for
name
in
path
:
inst2
=
inst2
[
name
]
# A Kit (c._rkit.kit) contains a list of io.SymbolicIn instances
# and the distribute method takes a value (seed), a list of indices
# and a list of corresponding Container instances. In this
# situation it will reseed all the rngs using the containers
# associated to them.
c
.
_rkit
.
kit
.
distribute
(
seedgen
.
random_integers
(
2
**
30
),
xrange
(
len
(
inst2
.
_rkit
)),
inst2
.
_rkit
)
else
:
self
.
_rkit
.
kit
.
distribute
(
seedgen
.
random_integers
(
2
**
30
),
xrange
(
len
(
inst
.
_rkit
)),
inst
.
_rkit
)
class
RandomStreamsInstance
(
object
):
"""RandomStreamsInstance"""
def
__init__
(
self
,
random_streams
,
memo
,
default_seed
):
...
...
@@ -168,10 +83,8 @@ class RandomStreamsInstance(object):
return
raise
KeyError
(
item
)
class
RandomStreams
(
Component
):
"""Module with similar interface to numpy.random (numpy.random.RandomState)"""
"""Module
component
with similar interface to numpy.random (numpy.random.RandomState)"""
random_state_results
=
[]
"""A list of pairs of the form (input_r, output_r). This will be over-ridden by the module
...
...
@@ -183,11 +96,18 @@ class RandomStreams(Component):
generator that provides seeds for member streams"""
def
__init__
(
self
,
seed
=
None
):
"""
:type seed: None or int
:param seed: a default seed to initialize the RandomState instances after build. See
`RandomStreamsInstance.__init__` for more details.
"""
super
(
RandomStreams
,
self
)
.
__init__
()
self
.
random_state_results
=
[]
self
.
default_instance_seed
=
seed
def
allocate
(
self
,
memo
):
"""override `Component.allocate` """
for
old_r
,
new_r
in
self
.
random_state_results
:
assert
old_r
not
in
memo
memo
[
old_r
]
=
In
(
old_r
,
...
...
@@ -196,11 +116,23 @@ class RandomStreams(Component):
mutable
=
True
)
def
build
(
self
,
mode
,
memo
):
#print 'MODE', mode
#returns a list of containers
"""override `Component.build` """
return
RandomStreamsInstance
(
self
,
memo
,
self
.
default_instance_seed
)
def
gen
(
self
,
op
,
*
args
,
**
kwargs
):
"""Create a new random stream in this container.
:param op: a RandomFunction instance to
:param args: interpreted by `op`
:param kwargs: interpreted by `op`
:returns: The symbolic random draw part of op()'s return value. This function stores
the updated RandomStateType Result for use at `build` time.
:rtype: TensorResult
"""
random_state_result
=
raw_random
.
random_state_type
()
new_r
,
out
=
op
(
random_state_result
,
*
args
,
**
kwargs
)
out
.
rng
=
random_state_result
...
...
@@ -210,15 +142,28 @@ class RandomStreams(Component):
def
binomial
(
self
,
*
args
,
**
kwargs
):
"""Return a symbolic binomial sample
This is a shortcut for a call to `self.gen`
"""
return
self
.
gen
(
raw_random
.
binomial
,
*
args
,
**
kwargs
)
def
uniform
(
self
,
*
args
,
**
kwargs
):
"""Return a symbolic uniform sample
This is a shortcut for a call to `self.gen`
"""
return
self
.
gen
(
raw_random
.
uniform
,
*
args
,
**
kwargs
)
def
normal
(
self
,
*
args
,
**
kwargs
):
"""Return a symbolic normal sample
This is a shortcut for a call to `self.gen`
"""
return
self
.
gen
(
raw_random
.
normal
,
*
args
,
**
kwargs
)
def
random_integers
(
self
,
*
args
,
**
kwargs
):
"""Return a symbolic random integer sample
This is a shortcut for a call to `self.gen`
"""
return
self
.
gen
(
raw_random
.
random_integers
,
*
args
,
**
kwargs
)
theano/tensor/tests/test_naacl09.py
浏览文件 @
7a49c0da
...
...
@@ -18,7 +18,7 @@ def cross_entropy(target, output, axis=1):
"""
return
-
T
.
mean
(
target
*
T
.
log
(
output
)
+
(
1
-
target
)
*
T
.
log
(
1
-
output
),
axis
=
axis
)
class
QuadraticDenoisingAA
(
T
.
R
Module
):
class
QuadraticDenoisingAA
(
module
.
Module
):
"""Quadratic de-noising Auto-encoder
WRITEME
...
...
@@ -59,6 +59,8 @@ class QuadraticDenoisingAA(T.RModule):
"""
super
(
QuadraticDenoisingAA
,
self
)
.
__init__
()
self
.
random
=
T
.
RandomStreams
()
# MODEL CONFIGURATION
# self.regularize = regularize
self
.
tie_weights
=
tie_weights
...
...
@@ -75,11 +77,11 @@ class QuadraticDenoisingAA(T.RModule):
# PARAMETERS
if
_qfilters
is
None
:
self
.
qfilters
=
[
theano
.
Member
(
T
.
dmatrix
())
for
i
in
xrange
(
n_quadratic_filters
)]
self
.
qfilters
=
[
theano
.
Member
(
T
.
dmatrix
(
'q
%
i'
%
i
))
for
i
in
xrange
(
n_quadratic_filters
)]
else
:
self
.
qfilters
=
[
theano
.
Member
(
q
)
for
q
in
_qfilters
]
self
.
w1
=
theano
.
Member
(
T
.
matrix
())
if
_w1
is
None
else
theano
.
Member
(
_w1
)
self
.
w1
=
theano
.
Member
(
T
.
matrix
(
'w1'
))
if
_w1
is
None
else
theano
.
Member
(
_w1
)
if
_w2
is
None
:
if
not
tie_weights
:
self
.
w2
=
theano
.
Member
(
T
.
matrix
())
...
...
@@ -87,8 +89,8 @@ class QuadraticDenoisingAA(T.RModule):
self
.
w2
=
self
.
w1
.
T
else
:
self
.
w2
=
theano
.
Member
(
_w2
)
self
.
b1
=
theano
.
Member
(
T
.
vector
())
if
_b1
is
None
else
theano
.
Member
(
_b1
)
self
.
b2
=
theano
.
Member
(
T
.
vector
())
if
_b2
is
None
else
theano
.
Member
(
_b2
)
self
.
b1
=
theano
.
Member
(
T
.
vector
(
'b1'
))
if
_b1
is
None
else
theano
.
Member
(
_b1
)
self
.
b2
=
theano
.
Member
(
T
.
vector
(
'b2'
))
if
_b2
is
None
else
theano
.
Member
(
_b2
)
# # REGULARIZATION COST
# self.regularization = self.build_regularization()
...
...
@@ -173,6 +175,7 @@ class QuadraticDenoisingAA(T.RModule):
if
(
input_size
is
None
)
^
(
hidden_size
is
None
):
raise
ValueError
(
"Must specify input_size and hidden_size or neither."
)
super
(
QuadraticDenoisingAA
,
self
)
.
_instance_initialize
(
obj
,
{})
obj
.
random
.
initialize
()
if
seed
is
not
None
:
R
=
N
.
random
.
RandomState
(
seed
)
else
:
...
...
@@ -189,7 +192,7 @@ class QuadraticDenoisingAA(T.RModule):
obj
.
qfilters
=
[
R
.
uniform
(
size
=
sz
,
low
=
-
inf
,
high
=
inf
)
*
qfilter_relscale
\
for
qf
in
self
.
qfilters
]
if
seed
is
not
None
:
obj
.
seed
(
seed
,
recursive
=
True
)
obj
.
random
.
seed
(
seed
)
obj
.
lr
=
lr
...
...
theano/tensor/tests/test_randomstreams.py
0 → 100644
浏览文件 @
7a49c0da
__docformat__
=
"restructuredtext en"
import
sys
import
unittest
import
numpy
from
theano.tensor.randomstreams
import
RandomStreams
,
raw_random
from
theano.compile
import
Module
,
Method
,
Member
from
theano
import
tensor
from
theano
import
compile
,
gof
class
T_RandomStreams
(
unittest
.
TestCase
):
def
test_basics
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
uniform
((
2
,
2
)))
m
.
gn
=
Method
([],
m
.
random
.
normal
((
2
,
2
)))
made
=
m
.
make
()
made
.
random
.
initialize
()
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
gn_val0
=
made
.
gn
()
rng_seed
=
numpy
.
random
.
RandomState
(
234
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
#print fn_val0
numpy_val0
=
rng
.
uniform
(
size
=
(
2
,
2
))
numpy_val1
=
rng
.
uniform
(
size
=
(
2
,
2
))
#print numpy_val0
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_seed_in_initialize
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
uniform
((
2
,
2
)))
made
=
m
.
make
()
made
.
random
.
initialize
(
seed
=
888
)
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
rng_seed
=
numpy
.
random
.
RandomState
(
888
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
#print fn_val0
numpy_val0
=
rng
.
uniform
(
size
=
(
2
,
2
))
numpy_val1
=
rng
.
uniform
(
size
=
(
2
,
2
))
#print numpy_val0
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_seed_fn
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
m
.
fn
=
Method
([],
m
.
random
.
uniform
((
2
,
2
)))
made
=
m
.
make
()
made
.
random
.
initialize
(
seed
=
789
)
made
.
random
.
seed
(
888
)
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
rng_seed
=
numpy
.
random
.
RandomState
(
888
)
.
randint
(
2
**
30
)
rng
=
numpy
.
random
.
RandomState
(
int
(
rng_seed
))
#int() is for 32bit
#print fn_val0
numpy_val0
=
rng
.
uniform
(
size
=
(
2
,
2
))
numpy_val1
=
rng
.
uniform
(
size
=
(
2
,
2
))
#print numpy_val0
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_getitem
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
out
=
m
.
random
.
uniform
((
2
,
2
))
m
.
fn
=
Method
([],
out
)
made
=
m
.
make
()
made
.
random
.
initialize
(
seed
=
789
)
made
.
random
.
seed
(
888
)
rng
=
numpy
.
random
.
RandomState
()
rng
.
set_state
(
made
.
random
[
out
.
rng
]
.
get_state
())
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
numpy_val0
=
rng
.
uniform
(
size
=
(
2
,
2
))
numpy_val1
=
rng
.
uniform
(
size
=
(
2
,
2
))
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
def
test_setitem
(
self
):
m
=
Module
()
m
.
random
=
RandomStreams
(
234
)
out
=
m
.
random
.
uniform
((
2
,
2
))
m
.
fn
=
Method
([],
out
)
made
=
m
.
make
()
made
.
random
.
initialize
(
seed
=
789
)
made
.
random
.
seed
(
888
)
rng
=
numpy
.
random
.
RandomState
(
823874
)
made
.
random
[
out
.
rng
]
=
numpy
.
random
.
RandomState
(
823874
)
fn_val0
=
made
.
fn
()
fn_val1
=
made
.
fn
()
numpy_val0
=
rng
.
uniform
(
size
=
(
2
,
2
))
numpy_val1
=
rng
.
uniform
(
size
=
(
2
,
2
))
assert
numpy
.
all
(
fn_val0
==
numpy_val0
)
assert
numpy
.
all
(
fn_val1
==
numpy_val1
)
if
__name__
==
'__main__'
:
from
theano.tests
import
main
main
(
"test_randomstreams"
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论