How to install tensorflow with Conda - tensorflow

I have been trying to install tensorflow via Conda for a couple days now with no luck.
I made a fresh install of anaconda3/miniconda3 followed the steps to fix the .bash_profile issue. Then
$ conda create -n tensorflow pip python=3.6
seems to work. However
$ source activate tensorflow
does nothing. I tried conda activate tensorflow and nothing. If I type conda info I can see that the tensorflow env is running. But I have no ability to type within it?

Welcome to Python and Tensorflow.
This bit really takes a long time to figure out unless somebody told you how to do so.
Follow this. Assume you installed Miniconda (then python is automatically installed) and you are using linux (cuz you mentioned 'source')
You want to make an conda environment named 'ai3' and you wanna install many packages like tensorflow within 'ai3' env.
In your command line,
1) Create ai3 env: see https://conda.io/docs/user-guide/tasks/manage-environments.html for details
$ conda create -n ai3 python=3.6
2) Log your current command window into the ai3 env
$ source activate ai3
3) Install packages, for example tensorflow: details in https://www.tensorflow.org/install/install_linux but that does not really help FOBs
$ pip install tensorflow
4) Enter yes whenever your command window asks sth. Once it's done, then you wish to use tensorflow (TF) in python. Do this
$ python
$ >> import tensorflow as tf
$ >> hello = tf.constant('Hello, TensorFlow!')
$ >> with tf.Session() as sess:
$ >> sess.run(hello)
4-1-0) Obviously you don't wanna do this all the time. Download Pycharm community edition (in https://www.jetbrains.com/pycharm/download/#section=linux) and install.
4-1-1) Create a new project in any location you like with interpreter 'ai3'. Interpreter is the env you just prepared in step 1,2,3): click 'add local' by clicking a button at the right end of the 'Interpreter' line -> a python file at '/miniconda3/envs/ai3/bin/python'
5) Now work with many tutorials out there on this setup

First you need to check the naming convention you are using. I know that's how documentation suggests, but I think it's misleading.
Name your environment according to what you want to be doing inside of it. Tensorflow is a package (that can be a python package) that you will use inside of the environment so you can name it something like:
$ conda create --name 'machinelearning-env' pip python=3.6
Also if you just installed Anaconda you do not need to specify the python designation.
Then you need to activate your environment with
source activate (whatever you named your conda env)
Next you can install with :
:~/anaconda3/envs$ pip install --ignore-installed --upgrade (the tensorflow url found here: https://www.tensorflow.org/install/install_linux#the_url_of_the_tensorflow_python_package )
Finally instantiate python and then you can import tensorflow with :
import tensorflow as tf
I suggest you refer back to the tensorflow documentation as it's all there. Good luck!

You should first give a name to your environment then give package names that you want to install ie.
conda create -n tensorflow_env tensorflow
Easiest way for me at least.

Related

Why use the pip in a conda virtual environment makes the global effect?

previously, I installed the tensorflow 1.13 in my machine.
There are some projects depending on different version of tensorflow and I do not want to mixed up different version of tensowflow.
So I just tried create a env called tf2.0 and used pip to install tensorflow 2.0.0b1 in that specific virtual environment.
However, after I ran 'pip install tensorflow-gpu==2.0.0b1` in that "tf2.0" conda environment, I found that it takes effect globally, which mean I have to use tensorflow-gpu 2.0.0b1 even when that virtual env "tf2.0" disactivated.
I wish I could use tensorflow 1.13 when virtual env is deactivated.
It's hard to troubleshoot the described conditions without more details (exact commands run, showing PATH before and after and post activation, etc.). Nevertheless, you can try switching to following the most recent recommendations for mixing Conda and Pip. Namely, avoid installing things ad hoc, which is prone to using the wrong pip and clobbering packages, but instead define a YAML file and always create the whole env in one go.
As a minimal example:
my_env.yaml
name: my_env
channels:
- defaults
dependencies:
- python
- pip
- pip:
- tensorflow-gpu==2.0.0b1
which can be created with conda env create -f my_env.yaml. Typically, it is best to include everything possible in the "non-pip" section of dependencies.
It is mostly that you used a wrong pip. To make sure you are using correct pip, it is usually a good practice to do
python -m pip install —user PACKAGE_NAME
Given that you have conda, pip should be the last resort.
Conda channel conda-forge most likely has the latest package version you are looking for.
conda install -c conda-forge PACKAGE_NAME
If you have to use pip, make sure you are in an environment and that environment has its own pip.
conda create -n test python=3.7
conda activate test
python -m pip install PACKAGE_NAME
From your described problem, I can guess that your environment is not activated in which you are trying to install the tensorflow2.0
Please make sure to activate the environment after making it.
so after creating the environment do this-
conda activate tf2.0
make sure you see this
(tf2.0) C:\Users\XYZ>
And then you install your tensorflow.

tensorflow on crestle with python 3.7

I am using crestle.ai site to build some models. In the Jupyter notebook when I try to import something from keras I get :
****ModuleNotFoundError: No module named 'tensorflow'****
I found that tensorflow does not work with Python 3.7 which is pre-installed in crestle. So I tried several ways to install Python 3.5 but nothing worked
!conda install python=3.5.0 --yes
I got:
Solving environment: failed UnsatisfiableError: The following specifications were found to be in conflict:
- jupyter_contrib_nbextensions -> jupyter_highlight_selected_word[version='>=0.1.1'] ->
python[version='>=3.7,<3.8.0a0'] -> readline[version='>=7.0,<8.0a0']
- jupyter_contrib_nbextensions -> jupyter_highlight_selected_word[version='>=0.1.1'] ->
python[version='>=3.7,<3.8.0a0'] -> tk[version='>=8.6.8,<8.7.0a0']
- jupyter_contrib_nbextensions -> jupyter_highlight_selected_word[version='>=0.1.1'] ->
python[version='>=3.7,<3.8.0a0'] -> xz[version='>=5.2.4,<6.0a0']
- python=3.5.0 Use "conda info " to see the dependencies for each package.
!pip3 install --upgrade tensorflow-gpu
I got
Collecting tensorflow-gpu Could not find a version that satisfies
the requirement tensorflow-gpu (from versions: ) No matching
distribution found for tensorflow-gpu
!pip install --upgrade tensorflow
I got:
Collecting tensorflow Could not find a version that satisfies the
requirement tensorflow (from versions: ) No matching distribution
found for tensorflow
I am following suggestions from different blogs and don't know what I am doing or if I am doing it right.
As per Anand's suggestion
!conda env list
​
**# conda environments:
#
base /home/nbuser/.anaconda3
new_environment /home/nbuser/.anaconda3/envs/new_environment
py36 /home/nbuser/.anaconda3/envs/py36
tensorflow /home/nbuser/.anaconda3/envs/tensorflow**
!source activate py36
/bin/sh: 1: source: not found
Do I need some path command?
Edit:
!activate py36
I got no output!
!pip3 install --upgrade tensorflow
Collecting tensorflow
Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
enter image description here
enter image description here
Edit:
Thanks. I was not aware of the terminal and was doing it wrong!
This is what I got after $pip install tensorflow-gpu
after pip command
What do I need to do next? I tried in Jupyter importing modules from keras but again I got:
Using TensorFlow backend.
ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 from keras.models import Sequential
I also tried to use keras on crestle.com instead (previous was crestle.ai). I got this:
keras on crestle.com
It shows python 3.6 but the similar issue..
python[version='>=3.7,<3.8.0a0'] This is part of your error, this basically means that you are still unable to get out of using Python3.7, hence the no module found error, I would suggest you to make a new environment in conda using.
conda create --name py36 python=3.6
In your conda prompt, then activate this environment by using
After that use conda env list and you should see an Enviornment named py36.
Activate this environment by using source activate py36
Edit If you are using conda prompt not terminal, directly use activate py36
Then try pip commands again, once the environment is activated.
If it still fails, let me know and we will see what the problem might be.
Edit You have to use this commands in the terminal that comes with crestle and not in its's Jupyter notebook.
The terminal can be accessed from the top right, you can see the New_>Terminal Button. And if it says conda is not installed, you might follow this link. https://www.digitalocean.com/community/tutorials/how-to-install-the-anaconda-python-distribution-on-ubuntu-16-04

why cant i update tensorflow 1.7.0 by conda

I am currently using tensorflow 1.2.1 and I am trying to update to version 1.7.0 using conda, but it is downgraded to 1.1.0. Why is this happening?
The default tensorflow version under conda package manager is 1.1.0.
Try creating a new environment within Anaconda with conda virtual environment manager (refer to this doc for more information):
$ conda create -n tensorflow
So the subsequent tensorflow installation won't mess up with your default Anaconda environment (I personally experienced this).
After successfully creating the virtual environment, activate it by:
$ source activate tensorflow
Your prompt should then change to:
(tensorflow) $
To install tensorflow version 1.7.0 in the new prompt, use pip instead of conda:
(tensorflow) $ pip install --ignore-installed --upgrade TF_PYTHON_URL
where TF_PYTHON_URL is the url of the tensorflow package with the latest version 1.7.0 (choose according to your python version).
Note that packages (e.g., spyder) you want to use together with tensorflow which are not already in the new environment should be installed:
(tensorflow) $ conda install spyder
This step can be merged with Step 1 by issuing a single command in the default prompt:
$ conda create -n tensorflow spyder
Every time you work with tensorflow, activate the (tensorflow) environment using Step 2, and after you finish, deactivate the environment to revert back to the default prompt:
(tensorflow) $ source deactivate
Hope these can help :-)

tensorboard: command not found

I installed TensorFlow on my MacBook Pro 10.12.5 from source code by steps described here.
https://www.tensorflow.org/install/install_sources
TensorFlow itself works well but I cannot run TensorBoard.
It seems tensorboard is not installed properly.
When I try running tensorboard --logdir=... it says -bash: tensorboard: command not found. And locate tensorboard returns empty.
Do I need any additional step to install tensorboard?
You could call tensorboard as a python module like this:
python3 -m tensorboard.main --logdir=~/my/training/dir
or add this to your .profile
alias tensorboard='python3 -m tensorboard.main'
If no other methods work then try this one. It may help you.
1. check the location of Tensorflow
pip show tensorflow
It will show output something like this.
...
Name: tensorflow
Version: 1.4.0
Location: /home/abc/xy/.local/lib/python2.7/site-packages
...
2. Go to that location you get from the above output.
cd /home/abc/xy/.local/lib/python2.7/site-packages
There you can see a directory named tensorboard.
cd tensorboard
3. There must be a file named 'main.py'.
4. Execute the following command to launch the tensorboard.
python main.py --logdir=/path/to/log_file/
If you installed Tensorflow with Virtualenv, then first Check whether you have activated the tensorflow envirnoment or not
If you have activated the tensorflow session then your command prompt will look like this :
If not, Write the below command and try running tensorboard again.
source ~/tensorflow/bin/activate
Run this command:
python3 -m tensorboard.main --logdir=logdir
To run directory you can use,
Change =logdir to ="dir/TensorFlow"
(Directory path)
What version of Tensorflow are you running? Older versions don't include Tensorboard.
If you do have a newer version, I see you are using OSX, which apparently caused some problems for other people: https://github.com/tensorflow/tensorflow/issues/2115 Check this page to fix it!
As a MacPorts user, I'm used to running things from out of the path
/opt/local/bin. When you install a python package via MacPorts, that's
where the executables go --- even if they're just symbolic links to
files to a main python repository in
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/
pip installs things into the latter directory, but apparently does NOT
add the symbolic link to /opt/local/bin
This has never been an issue (or even come up) for me before, because
I've only used pip to install (non-executable) packages which load
from within python. In conclusion, there is a
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/tensorboard
This is a pip / MacPorts-SOP mismatch / user error*, and nothing to do
with tensorboard in particular. Please close this issue. Thanks for
your help.
*my 'locate' database was in the process of updating but hadn't completed
Quickest solution -
echo "alias tensorboard='python3 -m tensorboard.main'" >> ~/.bash_profile
After adding this to your .bash_profile you can use
tensorboard --logdir=/path
If you are using pycharm in windows environment this may help:
python -m tensorboard.main --logdir=logs

How do I install TensorFlow's tensorboard?

How do I install TensorFlow's tensorboard?
The steps to install Tensorflow are here: https://www.tensorflow.org/install/
For example, on Linux for CPU-only (no GPU), you would type this command:
pip install -U pip
pip install tensorflow
Since TensorFlow depends on TensorBoard, running the following command should not be necessary:
pip install tensorboard
Try typing which tensorboard in your terminal. It should exist if you installed with pip as mentioned in the tensorboard README (although the documentation doesn't tell you that you can now launch tensorboard without doing anything else).
You need to give it a log directory. If you are in the directory where you saved your graph, you can launch it from your terminal with something like:
tensorboard --logdir .
or more generally:
tensorboard --logdir /path/to/log/directory
for any log directory.
Then open your favorite web browser and type in localhost:6006 to connect.
That should get you started. As for logging anything useful in your training process, you need to use the TensorFlow Summary API. You can also use the TensorBoard callback in Keras.
If your Tensorflow install is located here:
/usr/local/lib/python2.7/dist-packages/tensorflow
then the python command to launch Tensorboard is:
$ python /usr/local/lib/python2.7/dist-packages/tensorflow/tensorboard/tensorboard.py --logdir=/home/user/Documents/.../logdir
The installation from pip allows you to use:
$ tensorboard --logdir=/home/user/Documents/.../logdir
It may be helpful to make an alias for it.
Install and find your tensorboard location:
pip install tensorboard
pip show tensorboard
Add the following alias in .bashrc:
alias tensorboard='python pathShownByPip/tensorboard/main.py'
Open another terminal or run exec bash.
For Windows users, cd into pathShownByPip\tensorboard and run python main.py from there.
For Python 3.x, use pip3 instead of pip, and don't forget to use python3 in the alias.
TensorBoard isn't a separate component. TensorBoard comes packaged with TensorFlow.
Adding this just for the sake of completeness of this question (some questions may get closed as duplicate of this one).
I usually use user mode for pip ie. pip install --user even if instructions assume root mode. That way, my tensorboard installation was in ~/.local/bin/tensorboard, and it was not in my path (which shouldn't be ideal either). So I was not able to access it.
In this case, running
sudo ln -s ~/.local/bin/tensorboard /usr/bin
should fix it.
pip install tensorflow.tensorboard # install tensorboard
pip show tensorflow.tensorboard
# Location: c:\users\<name>\appdata\roaming\python\python35\site-packages
# now just run tensorboard as:
python c:\users\<name>\appdata\roaming\python\python35\site-packages\tensorboard\main.py --logdir=<logidr>
If you're using the anaconda distribution of Python, then simply do:
$❯ conda install -c conda-forge tensorboard
or
$❯ conda install -c anaconda tensorboard
Also, you can have a look at various builds by search the packages repo by:
$❯ anaconda search -t conda tensorboard
which would list the channels and the corresponding builds, the supported OS, Python versions etc.,
The pip package you are looking for is tensorflow-tensorboard developed by Google.
If you installed TensorFlow using pip, then the location of TensorBoard can be retrieved by issuing the command which tensorboard on the terminal. You can then edit the TensorBoard file, if necessary.
It is better not to mix up the virtual environments or perform installation on the root directory. Steps I took for hassle free installation are as below. I used conda for installing all my dependencies instead of pip. I'm answering with extra details, because when I tried to install tensor board and tensor flow on my root env, it messed up.
Create a virtual env
conda create --name my_env python=3.6
Activate virtual environment
source activate my_env
Install basic required modules
conda install pandas
conda install tensorflow
Install tensor board
conda install -c condo-forge tensor board
Hope that helps
I have a local install of tensorflow 1.15.0 (with tensorboard obviously included) on MacOS.
For me, the path to the relevant file within my user directory is Library/Python/3.7/lib/python/site-packages/tensorboard/main.py. So, which does not work for me, but you have to look for the file named main.py, which is weird since it apparently is named something else for other users.