Skip to content

Commit 2b25e97

Browse files
committed
Bugfix: check_tensor_sanity use of .numpy() only possible in eager mode.
Added workaround unsing tf.print, but couldn't get it to work with the logger object. Therefore the two cases are handled differently for now, but removing the if statement would be favorable.
1 parent 8ac051b commit 2b25e97

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

bayesflow/helper_functions.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@
2929

3030
def check_tensor_sanity(tensor, logger):
3131
"""Tests for the present of NaNs and Infs in a tensor."""
32-
33-
if tf.reduce_any(tf.math.is_nan(tensor)):
34-
num_na = tf.reduce_sum(tf.cast(tf.math.is_nan(tensor), tf.int8)).numpy()
35-
logger.warn(f"Warning! Returned estimates contain {num_na} nan values!")
36-
if tf.reduce_any(tf.math.is_inf(tensor)):
37-
num_inf = tf.reduce_sum(tf.cast(tf.math.is_inf(tensor), tf.int8)).numpy()
38-
logger.warn(f"Warning! Returned estimates contain {num_inf} inf values!")
32+
if tf.executing_eagerly():
33+
if tf.reduce_any(tf.math.is_nan(tensor)):
34+
num_na = tf.reduce_sum(tf.cast(tf.math.is_nan(tensor), tf.int8)).numpy()
35+
logger.warn(f"Warning! Returned estimates contain {num_na} nan values!")
36+
if tf.reduce_any(tf.math.is_inf(tensor)):
37+
num_inf = tf.reduce_sum(tf.cast(tf.math.is_inf(tensor), tf.int8)).numpy()
38+
logger.warn(f"Warning! Returned estimates contain {num_inf} inf values!")
39+
else:
40+
if tf.reduce_any(tf.math.is_nan(tensor)):
41+
num_na = tf.reduce_sum(tf.cast(tf.math.is_nan(tensor), tf.int8))
42+
tf.print("Warning! Returned estimates contain", num_na, "nan values!")
43+
if tf.reduce_any(tf.math.is_inf(tensor)):
44+
num_inf = tf.reduce_sum(tf.cast(tf.math.is_inf(tensor), tf.int8))
45+
tf.print(f"Warning! Returned estimates contain", num_inf, "inf values!")
3946

4047

4148
def merge_left_into_right(left_dict, right_dict):

0 commit comments

Comments
 (0)