diff --git a/pytensor/graph/basic.py b/pytensor/graph/basic.py index 29b2043f6d..3d94ce83b7 100644 --- a/pytensor/graph/basic.py +++ b/pytensor/graph/basic.py @@ -616,16 +616,20 @@ def eval( """ from pytensor.compile.function import function + ignore_unused_input = kwargs.get("on_unused_input", None) in ("ignore", "warn") + def convert_string_keys_to_variables(inputs_to_values) -> dict["Variable", Any]: new_input_to_values = {} for key, value in inputs_to_values.items(): if isinstance(key, str): matching_vars = get_var_by_name([self], key) if not matching_vars: - raise ValueError(f"{key} not found in graph") + if not ignore_unused_input: + raise ValueError(f"{key} not found in graph") elif len(matching_vars) > 1: raise ValueError(f"Found multiple variables with name {key}") - new_input_to_values[matching_vars[0]] = value + else: + new_input_to_values[matching_vars[0]] = value else: new_input_to_values[key] = value return new_input_to_values diff --git a/tests/graph/test_basic.py b/tests/graph/test_basic.py index 08c352ab71..84ffb365b5 100644 --- a/tests/graph/test_basic.py +++ b/tests/graph/test_basic.py @@ -367,6 +367,10 @@ def test_eval_kwargs(self): 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 + # regression test for https://github.com/pymc-devs/pytensor/issues/1084 + q = self.x + 1 + assert q.eval({"x": 1, "y": 2}, on_unused_input="ignore") == 2.0 + @pytest.mark.filterwarnings("error") def test_eval_unashable_kwargs(self): y_repl = constant(2.0, dtype="floatX")