Dataset of MNIST not found from Tensorflow - tensorflow

I have a python script whose initial lines read
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
When I run the script it gives this error
'no module such as tensorflow.tutorials.examples.mnist'.
I have already pip installed Tensorflow, what do i do now?

From your comment I can see that you are using Anaconda. Have you tried to install it with conda install tensorflow? Also, you could try to create an environment first, and install tensorflow afterwards with conda create -n tensorflow pip python=2.7 (or 3.x depending on your system) within it.

Related

Error when installing TensorFlow through pip

I'm following the instructions from https://www.tensorflow.org/install/pip#python-version-support to install tensorflow. I've used tensorflow before but for some reason, it has just stopped working. I'm using VS Code.
First, I check that pip and python are compatible:
python3 --version
python3 -m pip --version
I have Python 3.10.8, with pip 22.3.1 so it should be compatible. Then, I create a conda environment and activate it:
conda create --name tf python=3.9
conda activate tf
However, now when I try
pip install tensorflow
I get the error
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow
How can I fix this? I also tried things in the past like installing tensorflow through Conda, but I get errors when importing tensorflow there.
Could you please try again by opening anaconda cmd propmt and run below code:
conda create --name tf_new python=3.10
conda activate tf_new
then install tensorflow using:
pip install tensorflow
Now open the VS code by selecting the same created VirEnv tf_new from Anaconda navigator.
In VScode, Open a new jupyter notebook file and run the below code by selecting the tf_new (Conda VirEnv) in kernel(from top right) .
import tensorflow as tf
tf.__version__
Output:
'2.11.0'

cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization'

I am trying to build a ANN model using Tensorflow library on Spyder. Afte ı set my training and test data, ı imported the keras library as seen below
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
But the prcocess did not finished normally.I tookthe same error-> cannnot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization'
I am using the following versions Spyder 4.2.5 tensorflow 2.7
Please create a new virtual environment in ananconda to install TensorFlow:
conda create -n tf tensorflow #"tf" is the name for your TensorFlow environment
conda activate tf # to activate the virtual environment
pip install tensorflow # install tensorlfow in that environment
Select the same "tf" environment in anaconda navigator and install Spyder to launch and type below code to check if TensorFlow installed properly:
import tensorflow as tf
Now please try again executing your code in this Spyder IDE.

Why can't I access the keras.initializers class?

I'm trying to use the HeUniform() method from the keras.initializers class, bu when I go to run my code, I get the following output:
AttributeError: module 'tensorflow_core.keras.initializers' has no attribute 'HeUniform'
The code that I ran is the following:
init = tf.keras.initializers.HeUniform()
My imports are:
import gym
import tensorflow as tf
import numpy as np
from tensorflow import keras
from collections import deque
import time
import random
I'm using tensorflow version 2.0.0 installed from conda. I tried to upgrade the version, but I was getting further errors saying that version 2.2.0 and 2.4.1 does not exist for conda. Please advise.
Take a look at the tensorflow api documentation for version 2.0. There is no class tf.keras.initializers.HeUniform. Instead, there is a function tf.keras.initializers.he_uniform. You can find the source code for the function on GitHub.
TensorFlow recommends installing with pip. In the past, I have created a new conda environment specifically for tensorflow. It is bad practice to mix conda and pip packages, so that's why I create a new environment just for the specific version of tensorflow.
conda create -n tf2 python=3.8
conda activate tf2
python -m pip install --no-cache-dir tensorflow==2.4.1
Just keep in mind that the GPU versions depend on specific versions of CUDA and cuDNN. See https://www.tensorflow.org/install/source#gpu for more information about the requirements.

tensorflow import issue for python 3.7

After installing tensorflow for python 3.7, I tried to import it but it gave me a syntax error.
the command used for installation :
sudo -H pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.10.0-py3-none-any.whl
I don't think tensorflow has supported python3.7 yet. Is there a way to fix it?
enter image description here
You can solve this by simply changing the name of all these (Ctrl + R and Replace All) from async to for example async1 as also this post suggests.
But even after fixing it you will probably encounter other problems with Tensorflow and Python 3.7.0 . For more details see my answer here: Does TensorFlow 1.9 support Python 3.7
tensorflow import for python 3.7
for Anaconda 2019.03 with Python 3.7 version
for CPU
create command:
conda create -n tensorflow_env tensorflow
activate command:
conda activate tensorflow_env
Import command:
(tensorflow_env) C:\WINDOWS\system32>python
import tensorflow as tf
print(tf.version)
for GPU
create command:
conda create -n tensorflow_gpuenv tensorflow-gpu
activate command:
conda activate tensorflow_gpuenv
Import command:
(tensorflow_gpuenv) C:\WINDOWS\system32>python
import tensorflow as tf
print(tf.version)

tensorflow is only available in virtual env in anaconda

I installed tensorflow-gpu in anaconda3.
The steps I followed are:
conda create -n tensorflow pip python=3.6
source activate tensorflow
pip install tensorflow_gpu-1.8.0-cp36-cp36m-linux_x86_64.whl
After these steps, I find that if I use the python and ipython in anaconda3/bin, I can't import tensorflow (no module named tensorflow)
But If I activate the environment of tensorflow or I use the python and ipython in anaconda3/env/tensorflow/bin, I can import tensorflow.
And there is also no directory for tensorflow inanaconda3/lib/python3.6/site-package
Anyone knows why?
You created a conda environment and installed Tensorflow to that environment. Therefore you can only use Tensorflow inside that environment.
If you wish to use Tensorflow outside of an environment then don't activate the environment you created and just pip install Tensorflow.