{"id":1058,"date":"2020-04-14T11:07:00","date_gmt":"2020-04-14T17:07:00","guid":{"rendered":"http:\/\/www.gergltd.com\/home\/?p=1058"},"modified":"2020-04-14T21:01:48","modified_gmt":"2020-04-15T03:01:48","slug":"converting-mag-and-phase-in-tensorflow-on-windows-10","status":"publish","type":"post","link":"https:\/\/www.gergltd.com\/home\/converting-mag-and-phase-in-tensorflow-on-windows-10\/","title":{"rendered":"Converting Mag and Phase to Real and Imaginary in Tensorflow 2 on Windows 10"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">r = tf.complex(magnitude, 0.0) * tf.math.exp(tf.complex(0.0,1.0)*tf.complex(phase, 0.0))<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This results in a barrage of &#8220;CUDA_ERROR_LAUNCH_FAILED: unspecified launch failure&#8221; error messages. I filed a bug report here: <a href=\"https:\/\/github.com\/tensorflow\/tensorflow\/issues\/38443\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/tensorflow\/tensorflow\/issues\/38443<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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}).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;s formula.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def mag_phase_2_real_imag(mag, phase):      \n    cos_phase = tf.math.cos(phase)\n    sin_phase = tf.math.sin(phase)\n    r= tf.complex(mag*cos_phase, mag*sin_phase)\n    return  r\n#    \n\nreal_imag = tf.keras.layers.Lambda(lambda x:mag_phase_2_real_imag(x&#91;0], x&#91;1]))(&#91;mag, phase])  <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;CUDA_ERROR_LAUNCH_FAILED: unspecified launch failure&#8221; error messages. I filed a bug report [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1058","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/1058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/comments?post=1058"}],"version-history":[{"count":5,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/1058\/revisions"}],"predecessor-version":[{"id":1068,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/posts\/1058\/revisions\/1068"}],"wp:attachment":[{"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/media?parent=1058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/categories?post=1058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gergltd.com\/home\/wp-json\/wp\/v2\/tags?post=1058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}