Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
08c97f34
提交
08c97f34
authored
6月 14, 2022
作者:
Brandon T. Willard
提交者:
Brandon T. Willard
6月 14, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add optional strict to Type.is_valid_value
上级
174117f9
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
72 行增加
和
51 行删除
+72
-51
type.py
aesara/graph/type.py
+2
-2
type.py
aesara/tensor/random/type.py
+54
-37
test_type.py
tests/tensor/random/test_type.py
+16
-12
没有找到文件。
aesara/graph/type.py
浏览文件 @
08c97f34
...
...
@@ -180,10 +180,10 @@ class Type(MetaObject):
return
None
def
is_valid_value
(
self
,
data
:
D
)
->
bool
:
def
is_valid_value
(
self
,
data
:
D
,
strict
:
bool
=
True
)
->
bool
:
"""Return ``True`` for any python object that would be a legal value for a `Variable` of this `Type`."""
try
:
self
.
filter
(
data
,
strict
=
True
)
self
.
filter
(
data
,
strict
=
strict
)
return
True
except
(
TypeError
,
ValueError
):
return
False
...
...
aesara/tensor/random/type.py
浏览文件 @
08c97f34
from
typing
import
Generic
,
TypeVar
import
numpy
as
np
import
aesara
from
aesara.graph.type
import
Type
T
=
TypeVar
(
"T"
,
np
.
random
.
RandomState
,
np
.
random
.
Generator
)
gen_states_keys
=
{
"MT19937"
:
([
"state"
],
[
"key"
,
"pos"
]),
"PCG64"
:
([
"state"
,
"has_uint32"
,
"uinteger"
],
[
"state"
,
"inc"
]),
...
...
@@ -18,22 +23,15 @@ gen_states_keys = {
numpy_bit_gens
=
{
0
:
"MT19937"
,
1
:
"PCG64"
,
2
:
"Philox"
,
3
:
"SFC64"
}
class
RandomType
(
Type
):
class
RandomType
(
Type
,
Generic
[
T
]
):
r"""A Type wrapper for `numpy.random.Generator` and `numpy.random.RandomState`."""
@classmethod
def
filter
(
cls
,
data
,
strict
=
False
,
allow_downcast
=
None
):
if
cls
.
is_valid_value
(
data
,
strict
):
return
data
else
:
raise
TypeError
()
@staticmethod
def
may_share_memory
(
a
,
b
):
def
may_share_memory
(
a
:
T
,
b
:
T
):
return
a
.
_bit_generator
is
b
.
_bit_generator
class
RandomStateType
(
RandomType
):
class
RandomStateType
(
RandomType
[
np
.
random
.
RandomState
]
):
r"""A Type wrapper for `numpy.random.RandomState`.
The reason this exists (and `Generic` doesn't suffice) is that
...
...
@@ -49,28 +47,38 @@ class RandomStateType(RandomType):
def
__repr__
(
self
):
return
"RandomStateType"
@staticmethod
def
is_valid_value
(
a
,
strict
):
if
isinstance
(
a
,
np
.
random
.
RandomState
):
return
True
def
filter
(
self
,
data
,
strict
:
bool
=
False
,
allow_downcast
=
None
):
"""
XXX: This doesn't convert `data` to the same type of underlying RNG type
as `self`. It really only checks that `data` is of the appropriate type
to be a valid `RandomStateType`.
In other words, it serves as a `Type.is_valid_value` implementation,
but, because the default `Type.is_valid_value` depends on
`Type.filter`, we need to have it here to avoid surprising circular
dependencies in sub-classes.
"""
if
isinstance
(
data
,
np
.
random
.
RandomState
):
return
data
if
not
strict
and
isinstance
(
a
,
dict
):
if
not
strict
and
isinstance
(
dat
a
,
dict
):
gen_keys
=
[
"bit_generator"
,
"gauss"
,
"has_gauss"
,
"state"
]
state_keys
=
[
"key"
,
"pos"
]
for
key
in
gen_keys
:
if
key
not
in
a
:
r
eturn
False
if
key
not
in
dat
a
:
r
aise
TypeError
()
for
key
in
state_keys
:
if
key
not
in
a
[
"state"
]:
r
eturn
False
if
key
not
in
dat
a
[
"state"
]:
r
aise
TypeError
()
state_key
=
a
[
"state"
][
"key"
]
state_key
=
dat
a
[
"state"
][
"key"
]
if
state_key
.
shape
==
(
624
,)
and
state_key
.
dtype
==
np
.
uint32
:
return
True
# TODO: Add an option to convert to a `RandomState` instance?
return
data
r
eturn
False
r
aise
TypeError
()
@staticmethod
def
values_eq
(
a
,
b
):
...
...
@@ -114,7 +122,7 @@ aesara.compile.register_view_op_c_code(
random_state_type
=
RandomStateType
()
class
RandomGeneratorType
(
RandomType
):
class
RandomGeneratorType
(
RandomType
[
np
.
random
.
Generator
]
):
r"""A Type wrapper for `numpy.random.Generator`.
The reason this exists (and `Generic` doesn't suffice) is that
...
...
@@ -130,16 +138,25 @@ class RandomGeneratorType(RandomType):
def
__repr__
(
self
):
return
"RandomGeneratorType"
@staticmethod
def
is_valid_value
(
a
,
strict
):
if
isinstance
(
a
,
np
.
random
.
Generator
):
return
True
def
filter
(
self
,
data
,
strict
=
False
,
allow_downcast
=
None
):
"""
XXX: This doesn't convert `data` to the same type of underlying RNG type
as `self`. It really only checks that `data` is of the appropriate type
to be a valid `RandomGeneratorType`.
In other words, it serves as a `Type.is_valid_value` implementation,
but, because the default `Type.is_valid_value` depends on
`Type.filter`, we need to have it here to avoid surprising circular
dependencies in sub-classes.
"""
if
isinstance
(
data
,
np
.
random
.
Generator
):
return
data
if
not
strict
and
isinstance
(
a
,
dict
):
if
"bit_generator"
not
in
a
:
r
eturn
False
if
not
strict
and
isinstance
(
dat
a
,
dict
):
if
"bit_generator"
not
in
dat
a
:
r
aise
TypeError
()
else
:
bit_gen_key
=
a
[
"bit_generator"
]
bit_gen_key
=
dat
a
[
"bit_generator"
]
if
hasattr
(
bit_gen_key
,
"_value"
):
bit_gen_key
=
int
(
bit_gen_key
.
_value
)
...
...
@@ -148,16 +165,16 @@ class RandomGeneratorType(RandomType):
gen_keys
,
state_keys
=
gen_states_keys
[
bit_gen_key
]
for
key
in
gen_keys
:
if
key
not
in
a
:
r
eturn
False
if
key
not
in
dat
a
:
r
aise
TypeError
()
for
key
in
state_keys
:
if
key
not
in
a
[
"state"
]:
r
eturn
False
if
key
not
in
dat
a
[
"state"
]:
r
aise
TypeError
()
return
True
return
data
r
eturn
False
r
aise
TypeError
()
@staticmethod
def
values_eq
(
a
,
b
):
...
...
tests/tensor/random/test_type.py
浏览文件 @
08c97f34
...
...
@@ -56,15 +56,17 @@ class TestRandomStateType:
with
pytest
.
raises
(
TypeError
):
rng_type
.
filter
(
1
)
rng
=
rng
.
get_state
(
legacy
=
False
)
assert
rng_type
.
is_valid_value
(
rng
,
strict
=
False
)
rng_dict
=
rng
.
get_state
(
legacy
=
False
)
rng
[
"state"
]
=
{}
assert
rng_type
.
is_valid_value
(
rng_dict
)
is
False
assert
rng_type
.
is_valid_value
(
rng_dict
,
strict
=
False
)
assert
rng_type
.
is_valid_value
(
rng
,
strict
=
False
)
is
False
rng_dict
[
"state"
]
=
{}
rng
=
{}
assert
rng_type
.
is_valid_value
(
rng
,
strict
=
False
)
is
False
assert
rng_type
.
is_valid_value
(
rng_dict
,
strict
=
False
)
is
False
rng_dict
=
{}
assert
rng_type
.
is_valid_value
(
rng_dict
,
strict
=
False
)
is
False
def
test_values_eq
(
self
):
...
...
@@ -147,15 +149,17 @@ class TestRandomGeneratorType:
with
pytest
.
raises
(
TypeError
):
rng_type
.
filter
(
1
)
rng
=
rng
.
__getstate__
()
assert
rng_type
.
is_valid_value
(
rng
,
strict
=
False
)
rng_dict
=
rng
.
__getstate__
()
assert
rng_type
.
is_valid_value
(
rng_dict
)
is
False
assert
rng_type
.
is_valid_value
(
rng_dict
,
strict
=
False
)
rng
[
"state"
]
=
{}
rng
_dict
[
"state"
]
=
{}
assert
rng_type
.
is_valid_value
(
rng
,
strict
=
False
)
is
False
assert
rng_type
.
is_valid_value
(
rng
_dict
,
strict
=
False
)
is
False
rng
=
{}
assert
rng_type
.
is_valid_value
(
rng
,
strict
=
False
)
is
False
rng
_dict
=
{}
assert
rng_type
.
is_valid_value
(
rng
_dict
,
strict
=
False
)
is
False
def
test_values_eq
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论