Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
63144068
提交
63144068
authored
6月 28, 2011
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Bring dtype and casting behaviour of MRG closer from shared randomstreams
Partly reverts 5ff6e487f285.
上级
2b19b4ad
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
25 行增加
和
31 行删除
+25
-31
rng_mrg.py
theano/sandbox/rng_mrg.py
+25
-31
没有找到文件。
theano/sandbox/rng_mrg.py
浏览文件 @
63144068
...
@@ -10,7 +10,7 @@ import numpy
...
@@ -10,7 +10,7 @@ import numpy
from
theano
import
Op
,
Apply
,
shared
,
config
,
Variable
from
theano
import
Op
,
Apply
,
shared
,
config
,
Variable
from
theano.tensor
import
(
raw_random
,
TensorType
,
as_tensor_variable
,
from
theano.tensor
import
(
raw_random
,
TensorType
,
as_tensor_variable
,
get_vector_length
,
cast
,
opt
)
get_vector_length
,
cast
,
opt
,
scal
)
from
theano.tensor
import
zeros_like
,
sqrt
,
log
,
sin
,
cos
,
join
,
prod
from
theano.tensor
import
zeros_like
,
sqrt
,
log
,
sin
,
cos
,
join
,
prod
from
theano.compile
import
optdb
from
theano.compile
import
optdb
from
theano.gof
import
local_optimizer
from
theano.gof
import
local_optimizer
...
@@ -49,16 +49,6 @@ def multMatVect(v, A, m1, B, m2):
...
@@ -49,16 +49,6 @@ def multMatVect(v, A, m1, B, m2):
r
[
3
:]
=
matVecModM
(
B
,
v
[
3
:],
m2
)
r
[
3
:]
=
matVecModM
(
B
,
v
[
3
:],
m2
)
return
r
return
r
def
cast_if_untyped
(
x
,
dtype
):
"""Return `x` cast as a numpy scalar of type `dtype` if `x` is untyped."""
if
hasattr
(
x
,
'dtype'
):
# `x` is already typed.
return
x
else
:
# We intend to do this on regular Python int / float objects.
assert
isinstance
(
x
,
int
)
or
isinstance
(
x
,
float
)
return
numpy
.
array
(
x
,
dtype
=
dtype
)
#MRG31k3p
#MRG31k3p
#generator constants :
#generator constants :
...
@@ -703,7 +693,7 @@ class MRG_RandomStreams(object):
...
@@ -703,7 +693,7 @@ class MRG_RandomStreams(object):
node_rstate
.
default_update
=
new_rstate
node_rstate
.
default_update
=
new_rstate
return
sample
return
sample
def
uniform
(
self
,
size
,
low
=
0
,
high
=
1
,
ndim
=
None
,
dtype
=
'floatX'
,
def
uniform
(
self
,
size
,
low
=
0
.0
,
high
=
1.0
,
ndim
=
None
,
dtype
=
None
,
nstreams
=
None
):
nstreams
=
None
):
"""
"""
Sample a tensor of given size whose element from a uniform
Sample a tensor of given size whose element from a uniform
...
@@ -714,23 +704,25 @@ class MRG_RandomStreams(object):
...
@@ -714,23 +704,25 @@ class MRG_RandomStreams(object):
information.
information.
:param low: Lower bound of the interval on which values are sampled.
:param low: Lower bound of the interval on which values are sampled.
If
not already typed, it is
cast into dtype.
If
the ``dtype`` arg is provided, ``low`` will be
cast into dtype.
:param high: Higher bound of the interval on which values are sampled.
:param high: Higher bound of the interval on which values are sampled.
If
not already typed, it is
cast into dtype.
If
the ``dtype`` arg is provided, ``high`` will be
cast into dtype.
: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)
(ex: the shape of other Theano Variable)
:param dtype: The output data type.
:param dtype: The output data type. If dtype is not specified, it will
be inferred from the dtype of low and high, but will be at least as
precise as floatX.
"""
"""
if
dtype
==
'floatX'
:
low
=
as_tensor_variable
(
low
)
dtype
=
config
.
floatX
high
=
as_tensor_variable
(
high
)
if
dtype
is
None
:
dtype
=
scal
.
upcast
(
config
.
floatX
,
low
.
dtype
,
high
.
dtype
)
# We cast `low` and `high` into `dtype` to make sure we do not upcast
low
=
cast
(
low
,
dtype
=
dtype
)
# e.g. float32 into float64.
high
=
cast
(
high
,
dtype
=
dtype
)
low
=
cast_if_untyped
(
low
,
dtype
)
high
=
cast_if_untyped
(
high
,
dtype
)
if
isinstance
(
size
,
tuple
):
if
isinstance
(
size
,
tuple
):
msg
=
"size must be a tuple of int or a Theano variable"
msg
=
"size must be a tuple of int or a Theano variable"
...
@@ -815,13 +807,15 @@ class MRG_RandomStreams(object):
...
@@ -815,13 +807,15 @@ class MRG_RandomStreams(object):
raise
NotImplementedError
((
"MRG_RandomStreams.multinomial only"
raise
NotImplementedError
((
"MRG_RandomStreams.multinomial only"
" implemented with n == 1 and pvals.ndim = 2"
))
" implemented with n == 1 and pvals.ndim = 2"
))
def
normal
(
self
,
size
=
None
,
avg
=
0
,
std
=
1
,
ndim
=
None
,
def
normal
(
self
,
size
=
None
,
avg
=
0
.0
,
std
=
1.0
,
ndim
=
None
,
dtype
=
'floatX'
,
nstreams
=
None
):
dtype
=
None
,
nstreams
=
None
):
"""
"""
:param size: Can be a list of integers or Theano variables (ex: the
:param size: Can be a list of integers or Theano variables (ex: the
shape of another Theano Variable)
shape of another Theano Variable)
:param dtype: The output data type.
:param dtype: The output data type. If dtype is not specified, it will
be inferred from the dtype of low and high, but will be at least as
precise as floatX.
:param nstreams: Number of streams.
:param nstreams: Number of streams.
"""
"""
...
@@ -829,14 +823,14 @@ class MRG_RandomStreams(object):
...
@@ -829,14 +823,14 @@ class MRG_RandomStreams(object):
# in two halves. First half becomes our U1's for Box-Muller,
# in two halves. First half becomes our U1's for Box-Muller,
# second half our U2's. See Wikipedia page:
# second half our U2's. See Wikipedia page:
# http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
# http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
avg
=
as_tensor_variable
(
avg
)
std
=
as_tensor_variable
(
std
)
if
dtype
==
'floatX'
:
if
dtype
is
None
:
dtype
=
config
.
floatX
dtype
=
scal
.
upcast
(
config
.
floatX
,
avg
.
dtype
,
std
.
dtype
)
# We cast `avg` and `std` into `dtype` to make sure we do not upcast
avg
=
cast
(
avg
,
dtype
)
# e.g. float32 into float64.
std
=
cast
(
std
,
dtype
)
avg
=
cast_if_untyped
(
avg
,
dtype
)
std
=
cast_if_untyped
(
std
,
dtype
)
evened
=
False
evened
=
False
constant
=
False
constant
=
False
...
@@ -861,7 +855,7 @@ class MRG_RandomStreams(object):
...
@@ -861,7 +855,7 @@ class MRG_RandomStreams(object):
U2
=
flattened
[
prod
(
flattened
.
shape
)
//
2
:]
U2
=
flattened
[
prod
(
flattened
.
shape
)
//
2
:]
#normal_samples = zeros_like(flattened)
#normal_samples = zeros_like(flattened)
sqrt_ln_U1
=
sqrt
(
numpy
.
array
(
-
2.0
,
dtype
=
dtype
)
*
log
(
U1
))
sqrt_ln_U1
=
sqrt
(
-
2.0
*
log
(
U1
))
# TypeError: 'TensorVariable' object does not support item assignment
# TypeError: 'TensorVariable' object does not support item assignment
# so this doesn't work...
# so this doesn't work...
#normal_samples[:n_samples/2] = sqrt_ln_U1 * cos(2.0*numpy.pi*U2)
#normal_samples[:n_samples/2] = sqrt_ln_U1 * cos(2.0*numpy.pi*U2)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论