Can't access Tensorflow & Pytorch from Jupyter notebook or Lab? - tensorflow

I can't access tensorflow or pytorch from Jupyter notebook or lab. My OS is macOS big sur . I installed python#3.8 using homebrew. If I try to run this:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
simple code on Jupyter notebook , it is showing this output:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-3c075791d0d7> 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 created separate virtual environment and install tensorflow on it. But I installed jupyter notebook & lab outside virtual environment. Is there anyway to access tensorflow & pytorch from jupyter notebook or lab without using Anaconda?

Related

pandas and matplotlib not found on my tensorflow conda environment

I use a MacBook pro-2015, and I use the monetary.
I was able to pip install tensorflow to a new conda environment, but after it, pandas and matplotlib was no longer found.
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
Below is the traceback of error:
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 2
1 import numpy as np
----> 2 import matplotlib.pyplot as plt
3 import tensorflow as tf
4 import pandas as pd
ModuleNotFoundError: No module named 'matplotlib'
I have been able to solve this problem
I created a conda environment "myenv" where I installed tensorflow
Then I activated the conda environment in my terminal using
conda activate myenv
Then I installed the packages/libraries that I need
conda install pandas
conda install matplotlib
Just use this method to install any package into any environment you are working on

Error while importing 'tensorflow_federated' on Google Colab Tutorial

I've been working on the tutorial for Federated Learning for Image Classification, and while running the tutorial code on Google colab its giving me error while importing tensorflow_federated
Federated Learning for Image Classification
Code (getting error on line 4):
import collections
import numpy as np
import tensorflow as tf
import tensorflow_federated as tff
np.random.seed(0)
tff.federated_computation(lambda: 'Hello, World!')()
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-a23308ec3f7c> in <module>()
3 import numpy as np
4 import tensorflow as tf
----> 5 import tensorflow_federated as tff
6
7 np.random.seed(0)
6 frames
/usr/local/lib/python3.7/dist-packages/tensorflow_federated/python/common_libs/structure.py in <module>()
263
264 def to_odict(struct: Struct,
--> 265 recursive: bool = False) -> collections.OrderedDict[str, Any]:
266 """Returns `struct` as an `OrderedDict`, if possible.
267
TypeError: 'type' object is not subscriptable
I've tried updating the python version to 3.9 (as mentioned in some of the fixes available) but it didn't work.
Solved:
I followed this issue and installed the 0.20.0 version of tensorflow-federated which worked for me
!pip install --quiet tensorflow-federated==0.20.0
#!pip install --quiet --upgrade tensorflow-federated
!pip install --quiet --upgrade nest-asyncio
import nest_asyncio
nest_asyncio.apply()

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.

No module named 'tensorflow.keras.layers.experimental.preprocessing'

Below the code
import numpy as np
np.random.seed(0)
from sklearn import datasets
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format ='retina'
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
below the Error message
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
2 try:
----> 3 from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
4 except ImportError:
ModuleNotFoundError: No module named 'tensorflow.keras.layers.experimental.preprocessing'
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
<ipython-input-5-943507dd87a6> in <module>
6 get_ipython().run_line_magic('config', "InlineBackend.figure_format ='retina'")
7
----> 8 from keras.models import Sequential
9 from keras.layers import Dense
10 from keras.optimizers import SGD
~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
4 except ImportError:
5 raise ImportError(
----> 6 'Keras requires TensorFlow 2.2 or higher. '
7 'Install TensorFlow via `pip install tensorflow`')
8
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow
Note:` I think, the main problem is Tensorflow version. I used somes command and that's are bellow,
conda create -n tf tensorflow
conda activate tf
and I also used the below command
conda create -n tf-gpu tensorflow-gpu
conda activate tf-gpu
But it don't works , Please help for solve the error.
you need to update the version of your TensorFlow. For me, 2.2.0 solved the problem. I also checked with the higher versions and worked ok.
pip install tensorflow==2.2.0
or
pip install tensorflow-gpu==2.2.0
You need update TensorFlow. You can try with
pip install tensorflow==2.0.0
or, if you use gpu version
pip install tensorflow-gpu==2.0.0
If doesn't solve your issue, you can also try with 2.2.0 version.
For more details, in this issue follow this answer

Unable to import distributions module from Tensorflow (Dockerized)

I'm running the official TF docker repo using the Jupyter UI on localhost. It seems that TF is working in general, as I am able to import it, but when trying to import the distributions module I get an error:
print tf.__version__
import tf.distributions as dist
1.8.0
ImportErrorTraceback (most recent call last)
<ipython-input-3-4d440943cb46> in <module>()
1 print tf.__version__
----> 2 import tf.distributions as dist
ImportError: No module named tf.distributions
Try this
import tensorflow as tf
from tensorflow import distributions as dist
I don't think you can use import aliases for other import statements in python. I'm not too sure about this, but I think that's the problem.
FYI, I tested this on Python 3.5.2 and Tensorflow 1.8.0