{"id":1616,"date":"2021-12-17T21:15:11","date_gmt":"2021-12-17T21:15:11","guid":{"rendered":"https:\/\/laserphotonics.uk\/?p=1616"},"modified":"2021-12-19T22:57:57","modified_gmt":"2021-12-19T22:57:57","slug":"tensorflow-gpu-on-windows","status":"publish","type":"post","link":"https:\/\/laserphotonics.uk\/?p=1616","title":{"rendered":"tensorflow gpu on windows"},"content":{"rendered":"\n<p><a href=\"https:\/\/towardsdatascience.com\/how-to-finally-install-tensorflow-gpu-on-windows-10-63527910f255\">How to Finally Install TensorFlow 2 GPU on Windows 10 in&nbsp;2021 | Towards Data Science<\/a><\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\"><strong>SPECIAL NOTE: <\/strong>The following instruction only works when you will install tensorflow directly in window, NOT through miniconda\/anaconda; Tested and it doesn&#8217;t work if you install all following required software but run tensorflow through conda enviroment. <\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\"><strong>SOLUTION in mimiconda:<\/strong> Python 3.8:\u00a0conda install tensorflow-gpu=2.3 tensorflow=2.3=mkl_py38h1fcfbd6_0 (<strong>running this solve problem on windows on 19\/12\/2021, tested and and worked, the reason we use Python 3.8 and Tensorflow 2.3 here is for CST2021 Python  (support python 3.6-3.8) compability on Superlens server.<\/strong>)<\/p>\n\n\n\n<p id=\"b986\">I think Windows users, including me, have suffered enough. You are probably stumbling on this after trying hours or even days to make this work. So, as a kindness, I will just cut to the chase and show you the steps you need to install TensorFlow GPU on Windows 10 without giving the usual blog intro.<\/p>\n\n\n\n<h1 id=\"d2a3\">Step 1: Find out the TF version and its drivers.<\/h1>\n\n\n\n<p id=\"3a78\">The first, very important step is to go to this&nbsp;<a href=\"https:\/\/www.tensorflow.org\/install\/source?authuser=1#gpuhttps:\/\/www.tensorflow.org\/install\/source?authuser=1#gpu\" rel=\"noreferrer noopener\" target=\"_blank\">link<\/a>&nbsp;and decide which TF version you want to install. Based on this, the CUDA driver versions and other software versions change.<\/p>\n\n\n\n<p id=\"1401\">As of writing&nbsp;this guide, TF 2.6.0 is the latest, and we will be installing that one.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*qtE4sVWe4nQgAOtkaPbIsA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"2bc0\">We are only interested in the TF version, cuDNN, and CUDA versions. We keep this tab open and move on to the next step.<\/p>\n\n\n\n<h1 id=\"b042\">Step 2: Install Microsoft Visual Studio<\/h1>\n\n\n\n<p id=\"cddd\">Next, we install Microsoft Visual Studio. Note that this one is different than Visual Studio&nbsp;<em>Code<\/em>, which is a lightweight IDE so many people love.<\/p>\n\n\n\n<p id=\"b6d4\">Go to&nbsp;<a href=\"https:\/\/visualstudio.microsoft.com\/vs\/community\/\" rel=\"noreferrer noopener\" target=\"_blank\">this link<\/a>&nbsp;and press download:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/0\/1*bNNEhkw0mjDjy7DbCwxl2Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"c535\">Run the downloaded executable and it will take a minute to download the requirements. Then, it will ask you to choose what workloads to install. We don\u2019t want any, so just click install without workloads and click continue. After the installation is done, it will ask you to sign in but you don\u2019t have to.<\/p>\n\n\n\n<h1 id=\"e168\">Step 3: Install the NVIDIA CUDA toolkit<\/h1>\n\n\n\n<p id=\"f5c6\">NVIDIA CUDA toolkit contains the drivers for your NVIDIA GPU. Depending on your Windows, they may or may not be already installed. If installed, we should check their version and see if they are compatible with the TensorFlow version we want to install.<\/p>\n\n\n\n<p id=\"23f4\">Go to your Settings on Windows and choose \u201cApps &amp; Features\u201d. Then, search for NVIDIA:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/0\/1*ytgFKe13e2m-vBsXu8JZTg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"58e4\">We want to install TF 2.6.0 which requires NVIDIA CUDA Toolkit version 11.2 (see the first link to double-check). If your drivers are any other version, delete all the ones that have \u201cNVIDIA CUDA\u201d in their title (leave the others). Then, go to&nbsp;<code>Local Disk (C:) &gt; Program Files &gt; NVIDIA GPU Computing Toolkit &gt; CUDA<\/code>. There, you will see a folder with the CUDA version as a name. Delete that folder.<\/p>\n\n\n\n<p id=\"7371\">If you search for NVIDIA and no CUDA toolkit is found, go to&nbsp;<a href=\"https:\/\/www.youtube.com\/redirect?event=video_description&amp;redir_token=QUFFLUhqa014Rk11SHFnMGhiOERiVFVORXRrVkppRGhqZ3xBQ3Jtc0tuV2hsdTFfSHItNy1YM2hlbU1pQW9QSkpmTU1neUt3Rm5VWWE4VHYtMEZoRkYzN3dQTTFBeDU2MDZtQjRtY29BSHVpX2RsWmJ1VWpKenYxb1VWdHNkcklGbG5mbUdIZHJhQ2M5RzFwVGZCRnZkd2R2Zw&amp;q=https%3A%2F%2Fdeveloper.nvidia.com%2Fcuda-toolkit-archive\" rel=\"noreferrer noopener\" target=\"_blank\">this page<\/a>. Here is what it looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*99WiClq42rdlPA9JdoMlQg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"2d1c\">Here, we see three 11.2 versions, which are what we need (we got the version from the&nbsp;<a href=\"https:\/\/www.tensorflow.org\/install\/source?authuser=1#gpu\" rel=\"noreferrer noopener\" target=\"_blank\">first TF version link<\/a>&nbsp;I provided). Click on any of them and choose Windows 10, and download the network installer:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*7p47fdIj1K4oI7igwF_zfQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"f695\">Follow the on-screen prompts and install the drivers with default parameters. Then, restart your computer and come back.<\/p>\n\n\n\n<h1 id=\"ca46\">Step 4: Install cuDNN<\/h1>\n\n\n\n<p id=\"0047\">For TensorFlow 2.6.0, cuDNN 8.1 is required. Go to&nbsp;<a href=\"https:\/\/developer.nvidia.com\/cudnn\" rel=\"noreferrer noopener\" target=\"_blank\">this page<\/a>&nbsp;and press download:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/0\/1*7rQ_aKONbjoTPf9mw0Koxg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"12a2\">It will ask you for an NVIDIA Developer account:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*Zc7C68ltCtur3EHUt9ix-A.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"9c11\">If you don\u2019t have an account already, click \u201cJoin now\u201d and enter your email. Fill up the form \u2014 the standard stuff. Then, go back to the&nbsp;<a href=\"https:\/\/developer.nvidia.com\/rdp\/cudnn-download\" rel=\"noreferrer noopener\" target=\"_blank\">cuDNN download page<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*_Ibtf7V6dObyVuNwCxSvmA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"5bc2\">At the top, it will ask you to fill out a survey. Fill it out and you will be presented with the above page. Click on the first one since it is the one compatible with CUDA Toolkit v. 11.*. There, you will see a Windows version, which you should download.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*JW1H6THeHy6djD_F9DP3kw.png\" alt=\"\"\/><\/figure>\n\n\n\n<h1 id=\"9fb9\">Step 5: Extract the ZIP folder and copy core directories<\/h1>\n\n\n\n<p id=\"c4b1\">Extract the downloaded ZIP folder:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/0\/1*fhF4xxV_JchkpnyAxdlytg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"3420\">Open the&nbsp;<code>cuda<\/code>&nbsp;folder and&nbsp;<strong>copy<\/strong>&nbsp;the three folders at the top (<code>bin, include, lib<\/code>). Then, go to&nbsp;<code>C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.2<\/code>&nbsp;and paste them there.<\/p>\n\n\n\n<p id=\"be9a\">Explorer tells you that these folders already exist, which you should press&nbsp;<em>Replace the files in the destination<\/em>. That\u2019s it, we are done with the software requirements! Restart your computer once again.<\/p>\n\n\n\n<h1 id=\"6fbe\">Step 6: Add CUDA toolkit to PATH<\/h1>\n\n\n\n<p id=\"d546\">Now, it is time to add a few folders to the environment variables. In the last destination,&nbsp;<code>C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.2<\/code>, there is a&nbsp;<code>bin<\/code>&nbsp;and folder:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/0\/1*bsYzmabat34RjaOdFZXccg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"dc5b\">Open it and copy the file path. Then, press the \u201cStart\u201d (Windows) button and type \u201cEnvironment variables\u201d:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*HFq6hU8xWBkWB5YEFzEn_A.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"f4f0\">Open it and go to \u201cEnvironment variables\u201d. This will open this pop-up window:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*PG_oDfIkcmW3nMdbulTmsg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"b210\">Choose the \u201cPath\u201d from the top and press edit. Press \u201cNew\u201d and paste the copied link there.<\/p>\n\n\n\n<p id=\"c2ad\">Then, go back to the GPU toolkit folder and open the&nbsp;<code>libnvvp<\/code>&nbsp;folder:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*oOfy_jHrhb3YE0MyNXeNIA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"398f\">Copy its path and paste it to Environment variables just like you did with the&nbsp;<code>bin<\/code>&nbsp;folder. Then, close all pop-ups, saving the changes.<\/p>\n\n\n\n<h1 id=\"20eb\">Step 7: Install TensorFlow inside a virtual environment with Jupyter Lab<\/h1>\n\n\n\n<p id=\"3ae6\">Finally, we are ready to install TensorFlow. Create a virtual environment with your preferred package manager. I use&nbsp;<code>conda<\/code>, so I create a&nbsp;<code>conda<\/code>&nbsp;environment named&nbsp;<code>tf<\/code>&nbsp;with Python version 3.8.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">conda create -n tf python==3.8<br>conda activate tf<br>pip install --upgrade tensorflow<br>pip install jupyterlab ipykernel<\/pre>\n\n\n\n<p id=\"37b2\">It is important that both TensorFlow and JupyterLab are installed with either&nbsp;<code>pip<\/code>&nbsp;or&nbsp;<code>conda<\/code>. You will get a&nbsp;<code>ModelNotFoundError<\/code>&nbsp;in JupyterLab if they are installed from different channels.<\/p>\n\n\n\n<p id=\"7dcf\">Next, we should add the&nbsp;<code>conda<\/code>&nbsp;environment to Jupyterlab so that it is listed as a valid kernel when we launch a session:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ipython kernel install --user --name=&lt;name of the kernel, `tf` for our case&gt;<\/pre>\n\n\n\n<p id=\"582b\">If you launch JupyterLab, you should be able to see the environment as a kernel. Create a new notebook and run this snippet to check if TF can detect your GPU:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import tensorflow as tf<br>from tensorflow.python.client import device_lib<br><br>print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))<br>device_lib.list_local_devices()<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/proxy\/1*AR_0qfZyoDX1Rmv02pXTkA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"2f1b\">As the output says, I have a single GPU and at the end, it shows its name. If you have a similar output, then my job here is done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Finally Install TensorFlow 2 GPU on Windows 10 in&nbsp;2021 | Towards Data Science SPECIAL NOTE: The following instruction only works when you will install tensorflow directly in window, NOT through miniconda\/anaconda; Tested and it doesn&#8217;t work if you install all following required software but run tensorflow through conda enviroment. SOLUTION in mimiconda: Python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,2],"tags":[],"_links":{"self":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts\/1616"}],"collection":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1616"}],"version-history":[{"count":4,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts\/1616\/revisions"}],"predecessor-version":[{"id":1622,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts\/1616\/revisions\/1622"}],"wp:attachment":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}