@@ -558,46 +558,6 @@ def get_parents(self):
558
558
return [self .owner ]
559
559
return []
560
560
561
- def convert_string_keys_to_pytensor_variables (self , inputs_to_values ):
562
- r"""Convert the string keys to corresponding `Variable` with nearest name.
563
-
564
- Parameters
565
- ----------
566
- inputs_to_values :
567
- A dictionary mapping PyTensor `Variable`\s to values.
568
-
569
- Examples
570
- --------
571
-
572
- >>> import numpy as np
573
- >>> import pytensor.tensor as at
574
- >>> x = at.dscalar('x')
575
- >>> y = at.dscalar('y')
576
- >>> z = x + y
577
- >>> np.allclose(z.eval({'x' : 3, 'y' : 1}), 4)
578
- True
579
- """
580
- process_input_to_values = {}
581
- for i in inputs_to_values :
582
- if isinstance (i , str ):
583
- nodes_with_matching_names = get_var_by_name ([self ], i )
584
- length_of_nodes_with_matching_names = len (nodes_with_matching_names )
585
- if length_of_nodes_with_matching_names == 0 :
586
- raise Exception (f"{ i } not found in graph" )
587
- else :
588
- if length_of_nodes_with_matching_names > 1 :
589
- warnings .warn (
590
- f"Found { length_of_nodes_with_matching_names } pytensor variables with name { i } taking the first declared named variable for computation"
591
- )
592
- process_input_to_values [
593
- nodes_with_matching_names [
594
- length_of_nodes_with_matching_names - 1
595
- ]
596
- ] = inputs_to_values [i ]
597
- else :
598
- process_input_to_values [i ] = inputs_to_values [i ]
599
- return process_input_to_values
600
-
601
561
def eval (self , inputs_to_values = None ):
602
562
r"""Evaluate the `Variable`.
603
563
@@ -637,9 +597,29 @@ def eval(self, inputs_to_values=None):
637
597
if inputs_to_values is None :
638
598
inputs_to_values = {}
639
599
640
- inputs_to_values = self .convert_string_keys_to_pytensor_variables (
641
- inputs_to_values
642
- )
600
+ def convert_string_keys_to_variables ():
601
+ process_input_to_values = {}
602
+ for i in inputs_to_values :
603
+ if isinstance (i , str ):
604
+ nodes_with_matching_names = get_var_by_name ([self ], i )
605
+ length_of_nodes_with_matching_names = len (nodes_with_matching_names )
606
+ if length_of_nodes_with_matching_names == 0 :
607
+ raise Exception (f"{ i } not found in graph" )
608
+ else :
609
+ if length_of_nodes_with_matching_names > 1 :
610
+ raise Exception (
611
+ f"Found { length_of_nodes_with_matching_names } pytensor variables with name { i } "
612
+ )
613
+ process_input_to_values [
614
+ nodes_with_matching_names [
615
+ length_of_nodes_with_matching_names - 1
616
+ ]
617
+ ] = inputs_to_values [i ]
618
+ else :
619
+ process_input_to_values [i ] = inputs_to_values [i ]
620
+ return process_input_to_values
621
+
622
+ inputs_to_values = convert_string_keys_to_variables ()
643
623
644
624
if not hasattr (self , "_fn_cache" ):
645
625
self ._fn_cache = dict ()
0 commit comments