Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7c480a16
提交
7c480a16
authored
3月 25, 2017
作者:
amrithasuresh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Updated numpy as np
上级
6c1db380
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
19 行增加
和
19 行删除
+19
-19
raw_random.py
theano/tensor/raw_random.py
+19
-19
没有找到文件。
theano/tensor/raw_random.py
浏览文件 @
7c480a16
...
...
@@ -4,7 +4,7 @@ from __future__ import absolute_import, print_function, division
import
sys
from
copy
import
copy
import
numpy
import
numpy
as
np
from
six
import
string_types
from
six.moves
import
reduce
,
xrange
...
...
@@ -38,7 +38,7 @@ class RandomStateType(gof.Type):
raise
TypeError
()
def
is_valid_value
(
self
,
a
):
return
type
(
a
)
==
n
umpy
.
random
.
RandomState
return
type
(
a
)
==
n
p
.
random
.
RandomState
def
values_eq
(
self
,
a
,
b
):
sa
=
a
.
get_state
()
...
...
@@ -47,7 +47,7 @@ class RandomStateType(gof.Type):
if
sa
[
0
]
!=
sb
[
0
]:
return
False
# 1-D array of 624 unsigned integer keys
if
not
n
umpy
.
all
(
sa
[
1
]
==
sb
[
1
]):
if
not
n
p
.
all
(
sa
[
1
]
==
sb
[
1
]):
return
False
# integer "pos" representing the position in the array
if
sa
[
2
]
!=
sb
[
2
]:
...
...
@@ -67,17 +67,17 @@ class RandomStateType(gof.Type):
def
get_size
(
self
,
shape_info
):
# The size is the data, that have constant size.
state
=
n
umpy
.
random
.
RandomState
()
.
get_state
()
state
=
n
p
.
random
.
RandomState
()
.
get_state
()
size
=
0
for
elem
in
state
:
if
isinstance
(
elem
,
str
):
size
+=
len
(
elem
)
elif
isinstance
(
elem
,
n
umpy
.
ndarray
):
elif
isinstance
(
elem
,
n
p
.
ndarray
):
size
+=
elem
.
size
*
elem
.
itemsize
elif
isinstance
(
elem
,
int
):
size
+=
n
umpy
.
dtype
(
"int"
)
.
itemsize
size
+=
n
p
.
dtype
(
"int"
)
.
itemsize
elif
isinstance
(
elem
,
float
):
size
+=
n
umpy
.
dtype
(
"float"
)
.
itemsize
size
+=
n
p
.
dtype
(
"float"
)
.
itemsize
else
:
raise
NotImplementedError
()
return
size
...
...
@@ -151,7 +151,7 @@ class RandomFunction(gof.Op):
fn
,
outtype
,
inplace
,
ndim_added
=
state
self
.
fn
=
fn
if
isinstance
(
fn
,
string_types
):
self
.
exec_fn
=
getattr
(
n
umpy
.
random
.
RandomState
,
fn
)
self
.
exec_fn
=
getattr
(
n
p
.
random
.
RandomState
,
fn
)
else
:
self
.
exec_fn
=
fn
self
.
outtype
=
outtype
...
...
@@ -240,7 +240,7 @@ class RandomFunction(gof.Op):
# 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
:]
assert
type
(
r
)
==
n
umpy
.
random
.
RandomState
,
(
type
(
r
),
r
)
assert
type
(
r
)
==
n
p
.
random
.
RandomState
,
(
type
(
r
),
r
)
# If shape == [], that means no shape is enforced, and numpy is
# trusted to draw the appropriate number of samples, numpy uses
...
...
@@ -260,7 +260,7 @@ class RandomFunction(gof.Op):
r
=
copy
(
r
)
rout
[
0
]
=
r
rval
=
self
.
exec_fn
(
r
,
*
(
args
+
[
shape
]))
if
(
not
isinstance
(
rval
,
n
umpy
.
ndarray
)
or
if
(
not
isinstance
(
rval
,
n
p
.
ndarray
)
or
str
(
rval
.
dtype
)
!=
node
.
outputs
[
1
]
.
type
.
dtype
):
rval
=
theano
.
_asarray
(
rval
,
dtype
=
node
.
outputs
[
1
]
.
type
.
dtype
)
...
...
@@ -527,13 +527,13 @@ def binomial(random_state, size=None, n=1, p=0.5, ndim=None,
"""
if
prob
is
not
None
:
p
=
prob
print
(
"DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as n
umpy
."
,
file
=
sys
.
stderr
)
print
(
"DEPRECATION WARNING: the parameter prob to the binomal fct have been renamed to p to have the same name as n
p
."
,
file
=
sys
.
stderr
)
n
=
tensor
.
as_tensor_variable
(
n
)
p
=
tensor
.
as_tensor_variable
(
p
)
ndim
,
size
,
bcast
=
_infer_ndim_bcast
(
ndim
,
size
,
n
,
p
)
if
n
.
dtype
==
'int64'
:
try
:
n
umpy
.
random
.
binomial
(
n
=
numpy
.
asarray
([
2
,
3
,
4
],
dtype
=
'int64'
),
p
=
numpy
.
asarray
([
.
1
,
.
2
,
.
3
],
dtype
=
'float64'
))
n
p
.
random
.
binomial
(
n
=
np
.
asarray
([
2
,
3
,
4
],
dtype
=
'int64'
),
p
=
np
.
asarray
([
.
1
,
.
2
,
.
3
],
dtype
=
'float64'
))
except
TypeError
:
# THIS WORKS AROUND A NUMPY BUG on 32bit machine
n
=
tensor
.
cast
(
n
,
'int32'
)
...
...
@@ -583,7 +583,7 @@ def random_integers_helper(random_state, low, high, size):
out_size
=
out_size
+
(
dim_len
,)
# Build the indices over which to loop
out
=
n
umpy
.
ndarray
(
out_size
)
out
=
n
p
.
ndarray
(
out_size
)
broadcast_ind
=
_generate_broadcasting_indices
(
out_size
,
low
.
shape
,
high
.
shape
)
# Iterate over these indices, drawing one sample at a time from numpy
...
...
@@ -716,8 +716,8 @@ def permutation_helper(random_state, n, shape):
shape
=
()
out_shape
=
list
(
shape
)
out_shape
.
append
(
n
)
out
=
n
umpy
.
empty
(
out_shape
,
int
)
for
i
in
n
umpy
.
ndindex
(
*
shape
):
out
=
n
p
.
empty
(
out_shape
,
int
)
for
i
in
n
p
.
ndindex
(
*
shape
):
out
[
i
]
=
random_state
.
permutation
(
n
)
# print 'RETURNING', out.shape
...
...
@@ -801,7 +801,7 @@ def multinomial_helper(random_state, n, pvals, size):
# Build the indices over which to loop
# Note that here, the rows (inner-most 1D subtensors) of pvals and out
# are indexed, not their individual elements
out
=
n
umpy
.
ndarray
(
out_size
)
out
=
n
p
.
ndarray
(
out_size
)
broadcast_ind
=
_generate_broadcasting_indices
(
size
,
n
.
shape
,
pvals
.
shape
[:
-
1
])
# Iterate over these indices, drawing from one multinomial at a
...
...
@@ -815,16 +815,16 @@ def multinomial_helper(random_state, n, pvals, size):
# of probabilities meets or exceeds 1.0.
# In perfect arithmetic this would be correct, but in float32 or
# float64 it is too strict.
pisum
=
n
umpy
.
sum
(
pvi
)
pisum
=
n
p
.
sum
(
pvi
)
if
1.0
<
pisum
<
1.0
+
1e-5
:
# correct if we went a little over
# because mtrand.pyx has a ValueError that will trigger if
# sum(pvals[:-1]) > 1.0
pvi
=
pvi
*
(
1.0
-
5e-5
)
# pvi = pvi * .9
pisum
=
n
umpy
.
sum
(
pvi
)
pisum
=
n
p
.
sum
(
pvi
)
elif
pvi
[
-
1
]
<
5e-5
:
# will this even work?
pvi
=
pvi
*
(
1.0
-
5e-5
)
pisum
=
n
umpy
.
sum
(
pvi
)
pisum
=
n
p
.
sum
(
pvi
)
assert
pisum
<=
1.0
,
pisum
out
[
mi
]
=
random_state
.
multinomial
(
n
=
n
[
ni
],
pvals
=
pvi
.
astype
(
'float64'
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论