Converting Mag and Phase to Real and Imaginary in Tensorflow 2 on Windows 10

As of Tensorflow 2.1, you cannot do the following when running in a windows 10 environment on the GPU (it does work fine if you are running on the CPU though):

r = tf.complex(magnitude, 0.0) * tf.math.exp(tf.complex(0.0,1.0)*tf.complex(phase, 0.0))

This results in a barrage of “CUDA_ERROR_LAUNCH_FAILED: unspecified launch failure” error messages. I filed a bug report here: https://github.com/tensorflow/tensorflow/issues/38443

You can see from the tensorflow code, I am doing a simple operation: converting the magnitude and phase representation of a complex number into real and imaginary representation. I am doing this by utilizing the phase rotation properties of the exponential function (i.e. e^{i phase}).

I did come up with a workaround which I am posting here to prevent someone else spending hours of their time on a similar error, questioning their judgement, and thinking this should trivially work. My workaround converts magnitude and phase to real and imaginary through Euler’s formula.

def mag_phase_2_real_imag(mag, phase):      
    cos_phase = tf.math.cos(phase)
    sin_phase = tf.math.sin(phase)
    r= tf.complex(mag*cos_phase, mag*sin_phase)
    return  r
#    

real_imag = tf.keras.layers.Lambda(lambda x:mag_phase_2_real_imag(x[0], x[1]))([mag, phase])