import input_data MNIST tensorflow not working

104,038

Solution 1

So let's assume that you are in the directory: /somePath/tensorflow/tutorial (and this is your working directory).

All you need to do is to download the input_data.py file and place it like this. Let's assume that the file name you invoke:

import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
...

is main.py and it is also in the same directory.

Once this is done, you can just start running main.py which will start downloading the files and will put them in the MNIST_data folder (once they are there the script will not be downloading them next time).

Solution 2

The old tutorial said, to import the MNIST data, use:

import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

This will cause the error. The new tutorial uses the following code to do so:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

And this works well.

Solution 3

How can I start the tutorial

I didn't download the folder you did but I installed tensorflow by pip and then I had similar problem.

My workaround was to replace

import tensorflow.examples.tutorials.mnist.input_data

with

import tensorflow.examples.tutorials.mnist.input_data as input_data

Solution 4

I am using different version - following Install on Windows with Docker here - and had similar problem.

An easy workaround I've found was:

1.Into the Linux command line, figure out where is the input_data.py on my Docker image (in your case you mentionned that you had to download it manually. In my case, it was already here). I used the follwing linux command:

$ sudo find . -print | grep -i '.*[.]py'

I've got the files & path

./tensorflow/g3doc/tutorials/mnist/mnist.py
./tensorflow/g3doc/tutorials/mnist/input_data.py

2.launch Python and type the following command using SYS:

>> import sys
>> print(sys.path)

you will get the existing paths.

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat']

4.add the path of inputa_data.py:

>> sys.path.insert(1,'/tensorflow/tensorflow/g3doc/tutorials/mnist')

Hope that it can help. If you found better option, let me know. :)

Solution 5

If you're using Tensorflow 2.0 or higher, you need to install tensorflow_datasets first:

pip install tensorflow_datasets

or if you're using an Anaconda distribution:

conda install tensorflow_datasets

from the command line.

If you're using a Jupyter Notebook you will need to install and enable ipywidgets. According to the docs (https://ipywidgets.readthedocs.io/en/stable/user_install.html) using pip:

pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension

If you're using an Anaconda distribution, install ipywidgets from the command line like such:

conda install -c conda-forge ipywidgets

With the Anaconda distribution there is no need to enable the extension, conda handles this for you.

Then import into your code:

import tensorflow_datasets as tfds
mnist = tfds.load(name='mnist')

You should be able to use it without error if you follow these instructions.

Share:
104,038
O.rka
Author by

O.rka

I am an academic researcher studying machine-learning and microorganisms

Updated on January 22, 2021

Comments

  • O.rka
    O.rka over 3 years

    TensorFlow MNIST example not running with fully_connected_feed.py

    I checked this out and realized that input_data was not built-in. So I downloaded the whole folder from here. How can I start the tutorial:

    import input_data
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    
    
    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    <ipython-input-6-a5af65173c89> in <module>()
    ----> 1 import input_data
          2 mnist = tf.input_data.read_data_sets("MNIST_data/", one_hot=True)
    
    ImportError: No module named input_data
    

    I'm using iPython (Jupyter) so do I need to change my working directory to this folder I downloaded? or can I add this to my tensorflow directory? If so, where do I add the files? I installed tensorflow with pip (on my OSX) and the current location is ~/anaconda/lib/python2.7/site-packages/tensorflow/__init__.py

    Are these files meant to be accessed directly through tensorflow like sklearn datasets? or am I just supposed to cd into the directory and work from there? The example is not clear.

    EDIT:

    This post is very out-dated

  • Alexandru Ceausu
    Alexandru Ceausu over 8 years
    For me it worked like this: import tensorflow.examples.tutorials.mnist.input_data
  • Mattia Baldari
    Mattia Baldari over 7 years
    Someone know what "one_hot=True" means? And where someone could go and read these details from his own? Thank you
  • Mattia Baldari
    Mattia Baldari over 7 years
    For possible interested people, I found a possible answer, which is: "A one-hot vector is a vector which is 0 in most dimensions, and 1 in a single dimension.". Full explanation at page: tensorflow.org/versions/r0.10/tutorials/mnist/beginners/… (search for the word 'one-hot')
  • user3731622
    user3731622 about 7 years
    When I try the method you mention from new tutorial, I get this error: urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>. Any idea why?
  • user3731622
    user3731622 about 7 years
    I was able to manually download the data from here. Make sure to download the file to the tensorflow MNIST_data folder tensorflow\examples\tutorials\mnist After doing this the input_data.read_data_sets("MNIST_data", one_hot=True) worked.
  • Rich
    Rich about 7 years
    Hey, this worked for me, but I had to update pandas first. Use "sudo pip install pandas --upgrade" for that
  • SuperTetelman
    SuperTetelman about 7 years
    @user3731622, I was getting that error occasionally as well. A bit of googling brought me to some other bug trackers for the issue. Apparently the website hosting that data is known to go down from time to time and it doesn't automatically try another mirror.
  • a3.14_Infinity
    a3.14_Infinity almost 7 years
    let's say we have a vector for images 0 to 9. So, it is a 10 element vector. one-hot vector means that only one class is active at a time. So, class of '6' images is active. or that element in vector has value of 1.
  • Ritwik Biswas
    Ritwik Biswas almost 6 years
    For additional context. During the training process, your optimizer (say gradient descent) minimizes the distance between your model's output and the one-hot vector. The smaller this distance is, the more accurate your model is. A common distance function to minimize is the cross-entropy function. Essentially your model needs a reference to compare it's output to so the weights and biases can continue to be adjusted until an acceptable accuracy is achieved. This reference is the one-hot encoded vector.
  • Suyama87
    Suyama87 almost 3 years
    Since the quoted link to input_data.py does not work, [this][1] file worked for me. [1]: gist.github.com/haje01/14b0e5d8bd5428df781e