Tensorflow error in Jupyter notebook - tensorflow

After having installed tensorflow on Ubuntu 16.0 on AWS,
by using :
conda install -c conda-forge tensorflow=1.2.1
When typing on terminal SSH :
ipython
import tensorflow as tf
tf.__version__
It returns 1.2.1 and everything is fine.
But, when this code is run on Jupyter Ipython notebook
launched from same python install (anaconda),
I got this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-b6f7b46cc0dd> in <module>()
1 import tensorflow as tf
----> 2 tf.__version__
AttributeError: module 'tensorflow' has no attribute '__version__'
Don't understand how this error can come over since the python distribution
is the same.
Is there a way to check the install of Tensorflow ?
EDIT:
This post https://github.com/tensorflow/tensorflow/issues/3369
shows it might be related to sudo access to some folders.... when installed.
TBD

Related

import tensorflow failing in Jupyter notebook, but not Anaconda prompt

I have created a tf2 tensorflow environment using Anaconda. I can import tensorflow no issue via the command prompt. When I activate this environment and launch Jupyter notebook, I get:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_20912/1332388178.py in <module>
1 # TensorFlow and tf.keras
----> 2 import tensorflow as tf
3
4 # Helper libraries
5 import numpy as np
ModuleNotFoundError: No module named 'tensorflow'
I have created run this to add the kernel to Jupyter (although I thought it just launched using the current active conda environment anyway).
python -m ipykernel install --user --name <Environment_Name>
But still no joy when explicitly using the tf2 kernel in Jupyter. Although jupyter seems to be starting off the active tf2 environment anyway.
[I 13:58:30.178 NotebookApp] Kernel shutdown: 603e39de-2b2e-4228-86ce-b135811ea301
[I 13:58:30.438 NotebookApp] Kernel started: 98f24818-ae84-4947-9c88-9b1814d9c768, name: tf2
[I 13:59:49.915 NotebookApp] Saving file at /tensorflow/Fashion MNIST.ipynb
Create a new environment for tensorflow using below code:
conda create -n tf tensorflow python=3.5
conda activate tf
As you have already created tf2 environment, activate the same environment in cmd prompt as below:
conda activate tf2
then insatll:
conda install pip
pip install tensorflow
Now, Select the "tf2"(you created) in anaconda environments and open the JUPYTER notebook in the same environment and type:
import tensorflow as tf
if there is no error - tensorflow successfully installed.
You can check the tensorflow version -
print(tf.__version__)
Further, you can install any package using !pip install package_name in Jupyter Notebook.

Can't install keras or tensorflow in Windows 10 version - Anaconda

I have just had to reset Windows 10 to factory settings. I have re-installed Anaconda/Spyder and I am having problems installing Keras and TensorFlow at the moment.
I have attempted both pip and conda installations of TensorFlow but I am getting the below stack trace when I just attempt to run "import tensorflow as tf", or "import tf" (I wasn't sure which was correct)
To install tensorflow, I ran the below commands in the base Anaconda prompt as admin.
conda create -n tf tensorflow
conda activate tf
When I run the above import, I get the below error.
runfile('C:/Users/username/.spyder-py3/untitled0.py', wdir='C:/Users/username/.spyder-py3')
Traceback (most recent call last):
File "C:\Users\username.spyder-py3\untitled0.py", line 15, in
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'
I have tried re-installing Anaconda, etc to no avail.
Anybody know what is wrong here?
Follow these instructions to install Tensorflow on Spyder
#Create environment
conda create -n tf python=3.7 anaconda
#Activate environment
activate tf
#Install Spyder
conda install spyder
#Install Tensorflow
conda install tensorflow
#Test installation
import tensorflow as tf

ModuleNotFoundError: No module named 'tensorflow_datasets'

I try to run
import tensorflow_datasets as tfds
but got the following error
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-46a8a2031c9c> in <module>
----> 1 import tensorflow_datasets as tfds
ModuleNotFoundError: No module named 'tensorflow_datasets'
I have the tensorflow_datasets results, below is a snippets of pip list
tensorboard 2.1.1
tensorflow 2.1.0
tensorflow-datasets 2.1.0
tensorflow-estimator 2.1.0
tensorflow-hub 0.7.0
tensorflow-metadata 0.21.1
I am running using Ubuntu 16.04 with Anaconda virtual environment. How should I resolve this issue?
Not an issue as jupyterlab does not pickup the virtual environment. We need to set according to the following: https://janakiev.com/blog/jupyter-virtual-envs/

Numpy API version mismatch

I'm trying to install tensorflow from source on a Gentoo system (I think I need to do this to get it to use CUDA 9.1).
I'm able to build tensorflow, and then install it as a user with:
pip3 install --no-cache-dir --user /tmp/tensorflow_pkg/tensorflow-1.6.0rc1-cp35-cp35m-linux_x86_64.whl
When I try to import tensorflow I get:
>RuntimeError Traceback (most recent call last)
>RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
>
>ImportError Traceback (most recent call last)
>ImportError: numpy.core.multiarray failed to import
>
>ImportError Traceback (most recent call last)
>ImportError: numpy.core.umath failed to import
>
>ImportError Traceback (most recent call last)
>ImportError: numpy.core.umath failed to import
So my guess is that tensorflow was built against a different version of numpy than my system default (1.13.3).
The question is how to fix it? There's a lot about this process that's unfamiliar to me, so I would be grateful for any pointers in doing any of the following:
Telling the tensorflow build to use the system-wide numpy. Tensorflow uses bazel for the build process
Figure out what version of numpy tensorflow wants and change my system numpy to that.
Something else??? I can't do much with pip on a system-wide level because pip and Gentoo don't get along. I tried installing via Anaconda but then tensorflow couldn't see my gpus. Installing in a virtualenv with pip (which used to work) didn't work, I think because I've got cuda 9.1 installed. Perhaps I should downgrade to 9.0???
Any help appreciated!
Someone I always figure out how to do something the second after I post on SO! I installed into a virtualenv and did pip3 install --upgrade numpy in the virtualenv. All appears to be well...

Module Not found error on Google Colaboratory

Getting a weird import error when running the following code.
! pip install --user --upgrade git+https://github.com/broadinstitute/keras-resnet
import keras
import keras_resnet
Basically, I'm trying to install keras_resnet for keras_retinanet on Google colab however I'm getting a not found error. Output is below. The installation completes but then the import fails. Tested it locally and it works only fails on Google Colaboratory.
Running setup.py install for keras-resnet ... - \ done
Successfully installed keras-resnet-0.0.8
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-25-4521fd3221d7> in <module>()
2 #os.chdir('keras-retinanet')
3 import keras
----> 4 import keras_resnet
5 #from keras-retinanet.keras_retinanet.models.resnet import custom_objects
6 #model = keras.models.load_model('/path/to/model.h5', custom_objects=custom_objects)
ModuleNotFoundError: No module named 'keras_resnet'
Try dropping the --user? This worked for me:
!pip install --upgrade git+https://github.com/broadinstitute/keras-resnet
import keras
import keras_resnet