Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Tensorflow functions should typically work on both eager and graph tensors. This means that you can just use the following implementation: def lin_to_db(x: float | tf.Tensor) -> tf.Tensor: ...
Answer
#1: Initial revision
Tensorflow functions should typically work on both eager and graph tensors. This means that you can just use the following implementation: ```python def lin_to_db(x: float | tf.Tensor) -> tf.Tensor: """ convert signal to noise ratio (SNR) from linear to dB """ return 10. * tf.math.log(x) / tf.math.log(10.) ``` As you correctly pointed out, this does affect the output in the sense that the output will always be a `tf.Tensor`, even if the input is a `float`. You seem to depict it as a disadvantage, but I would argue that this is actually an advantage. After all, no matter what type the input is (`float`, `tf.Tensor`, `np.ndarray`, ...) the output will always have the same, known type. If you need the resulting tensor to some other type, you can always convert it as follows: ```python lin_to_db(x).numpy().item() ``` Note that this code works for any `x` that can be (implicitly) converted to a `tf.Tensor`.