Как в anaconda navigator добавить tensorflow
Перейти к содержимому

Как в anaconda navigator добавить tensorflow

  • автор:

TensorFlow#

TensorFlow enables your data science, machine learning, and artificial intelligence workflows. This page shows how to install TensorFlow using the conda package manager included in Anaconda and Miniconda.

TensorFlow CPU with conda is supported on 64-bit Ubuntu Linux 16.04 or later and macOS 10.12.6 or later.

TensorFlow GPU with conda is only available though version 2.4.1 (2021). For the latest TensorFlow GPU installation, follow the installation instructions on the TensorFlow website.

Install TensorFlow#

  1. Download and install Anaconda or Miniconda.
  2. Open a terminal application and use the default bash shell.
  3. Choose a name for your TensorFlow environment, such as “tf”.
  4. Use the following commands to install the current release of TensorFlow. CPU-only is recommended for beginners.

CPU-only TensorFlow

conda create -n tf tensorflow conda activate tf 

GPU TensorFlow
Note GPU TensorFlow is only available via conda for Windows and Linux.

conda create -n tf-gpu tensorflow-gpu conda activate tf-gpu 

TensorFlow is now installed and ready to use.

For using TensorFlow with a GPU, refer to the TensorFlow documentation, specifically the section on device placement.

CUDA versions#

GPU TensorFlow uses CUDA. For a version compatibility table for GPU TensorFlow on Linux, see https://www.tensorflow.org/install/source#gpu. For Windows, see https://www.tensorflow.org/install/source_windows#gpu.

GPU TensorFlow conda packages are currently only supported for Windows or Linux.

TensorFlow 2.10 was the last release that supported GPU on Windows Native.

To install GPU TensorFlow with a non-default CUDA version like 9.0, run the following commands:

conda create -n tf-gpu-cuda9 tensorflow-gpu cudatoolkit=9.0 conda activate tf-gpu-cuda9 

Nightly builds#

Advanced users may wish to install the latest nightly build of TensorFlow. These nightly builds are unstable and are only available as pip packages on PyPI.

To install the nightly build of CPU-only TensorFlow:

conda create -n tf-n python conda activate tf-n pip install tf-nightly 

Or, to install the nightly build of GPU TensorFlow on Linux or Windows:

conda create -n tf-n-gpu python conda activate tf-n-gpu pip install tf-nightly-gpu 

How to Install and Import Keras in Anaconda/Jupyter Notebooks

Do you work in Jupyter Notebooks and have an issue in installing and hence importing Keras? Well, you are at the right place.

I was in the same boat a few days back. I struggled for a few hours and could not get a breakthrough and gave up that day. The next day, I again started with a different approach and it clicked!

Just a disclaimer I work on Mac OSx Sierra(10.12.6) and this post is all about installing Keras and importing keras in Jupyter Notebook.

Installing Keras in Anaconda

So, first I did what I usually do to install any library.

pip install keras

But, it did not actually work. When I tried to import keras in my Jupyter Notebook, I got the below error:

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

So, what I did next is to try installing tensorflow as per the error message. Guess what? I got another error:

ERROR: Cannot uninstall ‘wrapt’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

I tried uninstalling and then re-installing and keep on getting some error or another.

So, I did a couple of search in google and tried the below suggestions:

conda update wrapt pip install tensorflow pip install tensorflow-gpu==1.14 pip install tensorflow ==1.14

No! Nada! Nothing worked!

But finally, I got a solution which actually worked and it is simple! Stay tuned!

A Working Solution:
Step 1: Create a new environment

Open the terminal and create a new environment. I have named my environment “keras_env“.

conda create --name keras_env
Step 2: Activate the environment

Now, activate the environment created above.

conda activate keras_env
Step 3: Install keras

To install keras, we need to type the below command:

conda install -c anaconda keras

It will take some time to install.

After analyzing, it will show a list of packages to be installed and will ask for a confirmation to proceed. Press Y to continue.

Step 5: Import Keras in Jupyter Notebook

Once the installation is complete, open Anaconda Environments. The new environment created above should be there. For me, it is called “keras_env“. Now, search for the library Keras in the new environment. It should be right there if everything goes well.

It should have also installed tensorflow.

Now, go back home and check if the “Applications on” is set to the new environment. For me, it is keras_env.

Finally, you are all set to open the Jupyter Notebook. So, when I clicked on Jupyter Notebook, it took some time to install first, and then it opened. You may get a message like below in Anaconda.

Once the Jupyter Notebook is open, import keras and Voila!

It took so much time to install and import keras that I totally forgot why I was even trying to import Keras!

Happy Learning and Happy Coding!

Please clap once if this post actually solve your problem.

8 comments

Anandan Subramani

Thank you very much. keras_env works. It imports following models successfully:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D However, I am running into another issue. When I import pandas or numpy or sklearn it fails. In other words, I can import only keras, not the models in standard base environment So, I created a new environment called ‘combo_env’ and pushed both keras and base into it, here is how:
(keras_env) python -m ipykernel install –user –name=combo_env
activate base
(base) python -m ipykernel install –user –name=combo_env
now in Jupyter notebook under comb_env only standard modules like pandas, numpy, sklearn work, not keras Could you please help, how to create a kernel where keras and other standard regular models can be imported Thanks

Install and Run TensorFlow and Keras on GPU using Anaconda Navigator

  • Open Anaconda Navigator (Source: iNNovationMerge)

Create Environment (Source: iNNovationMerge)

  • Click on Create

Create new environment

Python selection (Source: iNNovationMerge)

  • Enter environment name (Ex: neural_tf)
  • Select Python and version as 3.7
  • Click on create

Add conda-forge URL to Channels

  • Add Channels (Source: iNNovationMerge)

Install Tensorflow GPU

  • Select newly created Environment (neural_tf)
  • Search for tensorflow-gpu
  • Select listed packages
  • Click on Apply
  • Install packages by clicking Apply again
  • Install Tensorflow (Source: iNNovationMerge)
  • Install Tensorflow (Source: iNNovationMerge)

Install Keras

Install Keras (Source: iNNovationMerge)

  • Select newly created Environment (neural_tf)
  • Search for keras
  • Select listed packages
  • Click on Apply
  • Install packages by clicking Apply again

Install CUDA Tookit 10.0

  • Open link CUDA Toolkit Archive
  • Select CUDA Toolkit 10.1
  • Select
    • Operating System -> Windows
    • Architecture -> x86_64
    • Version -> 10
    • Installer Type -> exe
    • Download the exe
    • Run and Install
  • Install Cuda Toolkit (Source: iNNovationMerge)
  • Install Cuda Toolkit (Source: iNNovationMerge)

Download cuDNN 10.1

  • Login to Nvidia Developer website
  • Select cuDNN v8.0.5 for CUDA 10.1
  • Click on link cuDNN Library for Windows10
  • Install CuDNN (Source: iNNovationMerge)
  • Install CuDNN (Source: iNNovationMerge)

Set Environmental Variables

Set Environment Variable (Source: iNNovationMerge)

  • Unzip downloaded zip file to any folder(C:\cudnn-10.1-windows10-x64-v8.0.5.39)
  • Add its bin path to the Environmental Variables
    • C:\cudnn-10.1-windows10-x64-v8.0.5.39\cuda\bin
  • Add Nvidia Smi folder path to the Environmental Variables
    • C:\Program Files\NVIDIA Corporation\NVSMI

Check Nvidia Driver installation

nvidia-smi (Source: iNNovationMerge)

  • Open command prompt
  • Run command : nvidia-smi
  • You should be able to see the GPU information as below

Testing by invoking GPU from Tensorflow

  • Open command prompt
  • Activate environment by running below command
    • conda activate neural_tf
    import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf gpus = tf.config.experimental.list_physical_devices('GPU') print(gpus) # Output: # [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] 
  • Testing installation (Source: iNNovationMerge)

Great, iNNovationMerge hope that you have understood how to install and Run tensorflow on GPU

TensorFlow set up with Anaconda/Jupyter Notebooks

TensorFlow has become one of the most used frameworks in the deep learning space providing ability to train models in python providing encapsulation of the boilerplate code and aiding in deployment of applications on cloud. Setting up TensorFlow with Jupyter Notebooks has been one of the issues which I faced when trying to get it to work so I’m putting together a series of steps just as FYI ( in case my memory goes bust someday ).

Anaconda Prompt

There are two ways in which a user can go about adding libraries and performing other operations in anaconda. The choice is totally up to the user depending on their comforts with the command line or with the GUI . So the user can do this
• Using the User Interface — Anaconda Navigator.
• Using Anaconda Prompt — the command line tool.

Both of the approaches have their own pros and cons. I pursued using anaconda prompt ( or anaconda CLI) given my recent obsession with working on black and white windows.

Virtual Environments

To provide a brief overview, a virtual environment simply provides a separate environment where you can do application development using specified version of libraries. It prevents issues/errors in code execution related to deprecation of commands or release of newer versions.

To create a virtual environment the command is as below:

(base) C:\Users\Aditya> conda create -n myEnvironment 

The environment can be activated using the following command : conda activate. Post the command, user can see the base changing to indicating change of environment.

(base) C:\Users\Aditya> conda activate myEnvironment
(myEnvironment) C:\Users\Aditya>

You can exit the environment using the command conda deactivate

(myEnvironment) C:\Users\Aditya> conda deactivate 

To delete the environment using:

(base) C:\Users\Aditya> conda env remove -n myEnvironment

Setting up TensorFlow Environment using Anaconda

Now that we’ve done CRUD with the virtual environments, for tensorflow user can create a virtual environment using following set of commands below.

# Creating tensorflow virtual environment 
(base) C:\Users\Aditya\> conda create -n tf tensorflow
#Activate the environment
(base) C:\Users\Aditya\> conda activate tf

It installs the required set of libraries to start with Tensorflow. To test whether the libraries are installed or not we can run the following commands as a basic sanity check:

(base) C:\Users\Aditya> conda activate tf (tf) C:\Users\Aditya>python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf;
>>> tf.__version__ # Checking the version of the library.
'2.1.0'

Final Flourish: Setting up Jupyter Notebooks to use Tensorflow virtual environment

Now that we have installation of libraries out of the way, we need to make sure that we’re able to use the virtual environment with Jupyter Notebook.

Install nb_conda for having the ability to change kernels when using Jupyter Notebooks. This will help user to change the kernel option on landing page of the jupyter notebooks.

conda install -c anaconda nb_conda

Now, even after installing this you might not be seeing tf as a kernel option in Jupyter Notebooks. The error is due to virtual environment not having a IPython kernel. So, we need to set up an IPython kernel for the virtual environment. Use the following commands to set up IPython kernel in your virtual environment in anaconda prompt.

conda install -c anaconda ipykernel

Now when you run Jupyter Notebooks you should be able to see tensorflow virtual environment option in your anaconda environments:

And you should be able to import TensorFlow into your Jupyter Notebook.

Conclusion

Jupyter notebooks by itself is an amazing application for implementing data analysis and building machine learning models. Ability to use Tensorflow and other ML frameworks takes it a step further when it comes to running quick analysis on data.

This is my first attempt at writing a technical article in quite some time. Any comments, feedback and constructive criticism is always welcome. I can be reached on twitter @Aditya1791 & LinkedIn.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *