def val_where(cond, tval, fval):
if isinstance(tval, tf.Tensor):<EOL><INDENT>return tf.where(cond, tval, fval)<EOL><DEDENT>elif isinstance(tval, tuple):<EOL><INDENT>cls = type(tval)<EOL>return cls(*(val_where(cond, t, f) for t, f in zip(tval, fval)))<EOL><DEDENT>else:<EOL><INDENT>raise Exception(TypeError)<EOL><DEDENT>
Like tf.where but works on namedtuples.