Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1117ea5e
提交
1117ea5e
authored
4月 25, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
4月 26, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow keyword arguments in eval method
上级
98070db1
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
52 行增加
和
15 行删除
+52
-15
basic.py
pytensor/graph/basic.py
+32
-15
test_basic.py
tests/graph/test_basic.py
+20
-0
没有找到文件。
pytensor/graph/basic.py
浏览文件 @
1117ea5e
...
@@ -555,13 +555,20 @@ class Variable(Node, Generic[_TypeType, OptionalApplyType]):
...
@@ -555,13 +555,20 @@ class Variable(Node, Generic[_TypeType, OptionalApplyType]):
return
[
self
.
owner
]
return
[
self
.
owner
]
return
[]
return
[]
def
eval
(
self
,
inputs_to_values
=
None
):
def
eval
(
r"""Evaluate the `Variable`.
self
,
inputs_to_values
:
dict
[
Union
[
"Variable"
,
str
],
Any
]
|
None
=
None
,
**
kwargs
,
):
r"""Evaluate the `Variable` given a set of values for its inputs.
Parameters
Parameters
----------
----------
inputs_to_values :
inputs_to_values :
A dictionary mapping PyTensor `Variable`\s to values.
A dictionary mapping PyTensor `Variable`\s or names to values.
Not needed if variable has no required inputs.
kwargs :
Optional keyword arguments to pass to the underlying `pytensor.function`
Examples
Examples
--------
--------
...
@@ -591,10 +598,7 @@ class Variable(Node, Generic[_TypeType, OptionalApplyType]):
...
@@ -591,10 +598,7 @@ class Variable(Node, Generic[_TypeType, OptionalApplyType]):
"""
"""
from
pytensor.compile.function
import
function
from
pytensor.compile.function
import
function
if
inputs_to_values
is
None
:
def
convert_string_keys_to_variables
(
inputs_to_values
)
->
dict
[
"Variable"
,
Any
]:
inputs_to_values
=
{}
def
convert_string_keys_to_variables
(
input_to_values
):
new_input_to_values
=
{}
new_input_to_values
=
{}
for
key
,
value
in
inputs_to_values
.
items
():
for
key
,
value
in
inputs_to_values
.
items
():
if
isinstance
(
key
,
str
):
if
isinstance
(
key
,
str
):
...
@@ -608,19 +612,32 @@ class Variable(Node, Generic[_TypeType, OptionalApplyType]):
...
@@ -608,19 +612,32 @@ class Variable(Node, Generic[_TypeType, OptionalApplyType]):
new_input_to_values
[
key
]
=
value
new_input_to_values
[
key
]
=
value
return
new_input_to_values
return
new_input_to_values
inputs_to_values
=
convert_string_keys_to_variables
(
inputs_to_values
)
parsed_inputs_to_values
:
dict
[
Variable
,
Any
]
=
{}
if
inputs_to_values
is
not
None
:
parsed_inputs_to_values
=
convert_string_keys_to_variables
(
inputs_to_values
)
if
not
hasattr
(
self
,
"_fn_cache"
):
if
not
hasattr
(
self
,
"_fn_cache"
):
self
.
_fn_cache
=
dict
()
self
.
_fn_cache
:
dict
=
dict
()
inputs
=
tuple
(
sorted
(
inputs_to_values
.
keys
(),
key
=
id
))
inputs
=
tuple
(
sorted
(
parsed_inputs_to_values
.
keys
(),
key
=
id
))
if
inputs
not
in
self
.
_fn_cache
:
cache_key
=
(
inputs
,
tuple
(
kwargs
.
items
()))
self
.
_fn_cache
[
inputs
]
=
function
(
inputs
,
self
)
try
:
args
=
[
inputs_to_values
[
param
]
for
param
in
inputs
]
fn
=
self
.
_fn_cache
[
cache_key
]
except
(
KeyError
,
TypeError
):
fn
=
None
rval
=
self
.
_fn_cache
[
inputs
](
*
args
)
if
fn
is
None
:
fn
=
function
(
inputs
,
self
,
**
kwargs
)
try
:
self
.
_fn_cache
[
cache_key
]
=
fn
except
TypeError
as
exc
:
warnings
.
warn
(
"Keyword arguments could not be used to create a cache key for the underlying variable. "
f
"A function will be recompiled on every call with such keyword arguments.
\n
{exc}"
)
return
rval
args
=
[
parsed_inputs_to_values
[
param
]
for
param
in
inputs
]
return
fn
(
*
args
)
def
__getstate__
(
self
):
def
__getstate__
(
self
):
d
=
self
.
__dict__
.
copy
()
d
=
self
.
__dict__
.
copy
()
...
...
tests/graph/test_basic.py
浏览文件 @
1117ea5e
...
@@ -6,6 +6,7 @@ import pytest
...
@@ -6,6 +6,7 @@ import pytest
from
pytensor
import
shared
from
pytensor
import
shared
from
pytensor
import
tensor
as
pt
from
pytensor
import
tensor
as
pt
from
pytensor.compile
import
UnusedInputError
from
pytensor.graph.basic
import
(
from
pytensor.graph.basic
import
(
Apply
,
Apply
,
NominalVariable
,
NominalVariable
,
...
@@ -30,6 +31,7 @@ from pytensor.graph.basic import (
...
@@ -30,6 +31,7 @@ from pytensor.graph.basic import (
)
)
from
pytensor.graph.op
import
Op
from
pytensor.graph.op
import
Op
from
pytensor.graph.type
import
Type
from
pytensor.graph.type
import
Type
from
pytensor.tensor
import
constant
from
pytensor.tensor.math
import
max_and_argmax
from
pytensor.tensor.math
import
max_and_argmax
from
pytensor.tensor.type
import
TensorType
,
iscalars
,
matrix
,
scalars
,
vector
from
pytensor.tensor.type
import
TensorType
,
iscalars
,
matrix
,
scalars
,
vector
from
pytensor.tensor.type_other
import
NoneConst
from
pytensor.tensor.type_other
import
NoneConst
...
@@ -359,6 +361,24 @@ class TestEval:
...
@@ -359,6 +361,24 @@ class TestEval:
with
pytest
.
raises
(
Exception
,
match
=
"o not found in graph"
):
with
pytest
.
raises
(
Exception
,
match
=
"o not found in graph"
):
t
.
eval
({
"o"
:
1
})
t
.
eval
({
"o"
:
1
})
def
test_eval_kwargs
(
self
):
with
pytest
.
raises
(
UnusedInputError
):
self
.
w
.
eval
({
self
.
z
:
3
,
self
.
x
:
2.5
})
assert
self
.
w
.
eval
({
self
.
z
:
3
,
self
.
x
:
2.5
},
on_unused_input
=
"ignore"
)
==
6.0
@pytest.mark.filterwarnings
(
"error"
)
def
test_eval_unashable_kwargs
(
self
):
y_repl
=
constant
(
2.0
,
dtype
=
"floatX"
)
assert
self
.
w
.
eval
({
self
.
x
:
1.0
},
givens
=
((
self
.
y
,
y_repl
),))
==
6.0
with
pytest
.
warns
(
UserWarning
,
match
=
"Keyword arguments could not be used to create a cache key"
,
):
# givens dict is not hashable
assert
self
.
w
.
eval
({
self
.
x
:
1.0
},
givens
=
{
self
.
y
:
y_repl
})
==
6.0
class
TestAutoName
:
class
TestAutoName
:
def
test_auto_name
(
self
):
def
test_auto_name
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论