简体   繁体   English

导入 input_data MNIST 张量流不起作用

[英]import input_data MNIST tensorflow not working

TensorFlow MNIST example not running with fully_connected_feed.py TensorFlow MNIST 示例未与fully_connected_feed.py 一起运行

I checked this out and realized that input_data was not built-in.我检查了这个并意识到input_data不是内置的。 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?我正在使用 iPython (Jupyter),所以我需要将我的工作目录更改为我下载的这个文件夹吗? or can I add this to my tensorflow directory?或者我可以将它添加到我的tensorflow目录中吗? 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我用pip (在我的OSX上)安装了tensorflow ,当前位置是~/anaconda/lib/python2.7/site-packages/tensorflow/__init__.py

Are these files meant to be accessed directly through tensorflow like sklearn datasets?这些文件是否tensorflowsklearn数据集一样通过tensorflow直接访问? or am I just supposed to cd into the directory and work from there?还是我只是应该 cd 进入目录并从那里开始工作? The example is not clear.例子不清楚。

EDIT:编辑:

This post is very out-dated这篇文章已经过时了

So let's assume that you are in the directory: /somePath/tensorflow/tutorial (and this is your working directory).因此,让我们假设您位于以下目录中: /somePath/tensorflow/tutorial (这是您的工作目录)。

All you need to do is to download the input_data.py file and place it like this.您需要做的就是下载input_data.py文件并像这样放置它。 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.main.py并且它也在同一目录中。

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).完成后,您可以开始运行main.py ,它将开始下载文件并将它们放在 MNIST_data 文件夹中(一旦它们在那里,脚本下次将不会下载它们)。

The old tutorial said, to import the MNIST data, use:老教程说,要导入MNIST数据,使用:

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.这很有效。

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.我没有下载你做的文件夹,但我通过 pip 安装了 tensorflow,然后我遇到了类似的问题。

My workaround was to replace我的解决方法是更换

import tensorflow.examples.tutorials.mnist.input_data

with

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

I am using different version - following Install on Windows with Docker here - and had similar problem.我使用不同的版本-继在Windows上安装泊坞这里-并有类似的问题。

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). 1.进入Linux命令行,找出我的Docker镜像上的input_data.py在哪里(在你的情况下你提到你必须手动下载它。在我的情况下,它已经在这里)。 I used the follwing linux command:我使用了以下 linux 命令:

$ 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: 2. 启动 Python 并使用 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: 4.添加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.如果您找到更好的选择,请告诉我。 :) :)

I might be kinda late, but for tensorflow version 0.12.1, you might wanna use input_data.read_data_sets instead.我可能有点晚了,但是对于 tensorflow 0.12.1 版,您可能想改用 input_data.read_data_sets。

Basically using this function to load the data from your local drive that you had downloaded from http://yann.lecun.com/exdb/mnist/ .基本上使用此功能从您从http://yann.lecun.com/exdb/mnist/下载的本地驱动器加载数据。

from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('data_set/') from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('data_set/')

对于TensorFlow API 2.0 ,mnist 数据的位置更改为: tf.keras.datasets.mnist.load_data

If you're using Tensorflow 2.0 or higher, you need to install tensorflow_datasets first:如果您使用的是 Tensorflow 2.0 或更高版本,则需要先安装 tensorflow_datasets:

pip install tensorflow_datasets

or if you're using an Anaconda distribution:或者如果您使用的是 Anaconda 发行版:

conda install tensorflow_datasets

from the command line.从命令行。

If you're using a Jupyter Notebook you will need to install and enable ipywidgets.如果您使用的是 Jupyter Notebook,则需要安装并启用 ipywidgets。 According to the docs ( https://ipywidgets.readthedocs.io/en/stable/user_install.html ) using pip:根据使用 pip 的文档( https://ipywidgets.readthedocs.io/en/stable/user_install.html ):

pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension

If you're using an Anaconda distribution, install ipywidgets from the command line like such:如果您使用的是 Anaconda 发行版,请从命令行安装 ipywidgets,如下所示:

conda install -c conda-forge ipywidgets

With the Anaconda distribution there is no need to enable the extension, conda handles this for you.使用 Anaconda 发行版,无需启用扩展,conda 会为您处理。

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.如果您按照这些说明操作,您应该可以毫无错误地使用它。

cd your_mnist_dir &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/mnist_data.pkl &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/t10k-images-idx3-ubyte.gz &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/t10k-labels-idx1-ubyte.gz &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/train-images-idx3-ubyte.gz &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/train-labels-idx1-ubyte.gz

MNIST input_data 是内置的,它不是一个单独的模块,它在 Tensorflow 模块内部,试试

from tensorflow.examples.tutorials.mnist import input_data

MNIST data set included as a part of tensorflow examples tutorial, If we want to use this : MNIST 数据集包含在 tensorflow 示例教程中,如果我们想使用它:

Import MNIST data to identify handwritten digites导入 MNIST 数据以识别手写数字

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

There's now a much easier way to load MNIST data into tensorflow without having to download the data by using Tensorflow 2 and Tensorflow Datasets现在有一种更简单的方法可以将 MNIST 数据加载到 tensorflow 中,而无需使用 Tensorflow 2 和 Tensorflow Datasets 下载数据

To get started, make sure you import Tensorflow and specify the 2nd version:首先,请确保导入 Tensorflow 并指定第二个版本:

%tensorflow_version 2.x
import tensorflow as tf

Then load the data into a dictionary using the following code:然后使用以下代码将数据加载到字典中:

MNIST_data = tfds.load(name = "mnist")

and Then split the data into train and test:然后将数据拆分成训练和测试:

train, test = MNIST_data['train'] , MNIST_data['test']

Now you can use these data generators however you like.现在您可以随意使用这些数据生成器。

Remove the lines:删除行:

from tensorflow.examples.tutorials.mnist import input_data
fashion_mnist = input_data.read_data_sets('input/data',one_hot=True)

and the line below will suffice:下面的行就足够了:

fashion_mnist = keras.datasets.fashion_mnist

Note that if the dataset is not available in the examples built-in to the keras, this will download the dataset and solve the problem.请注意,如果数据集在 keras 内置的示例中不可用,这将下载数据集并解决问题。 :) :)

As TensorFlow official website shown, All MNIST data is hosted on http://yann.lecun.com/exdb/mnist/正如 TensorFlow 官网所示,所有 MNIST 数据都托管在http://yann.lecun.com/exdb/mnist/

在此处输入图片说明

For Tensorflow API above 2.0, to use MNIST dataset following command can be used,对于 2.0 以上的 Tensorflow API,要使用 MNIST 数据集,可以使用以下命令,

import tensorflow_datasets as tfds
data = tfds.load(name = "mnist")

The following steps work perfectly in my Notebook:以下步骤在我的笔记本中完美运行:

step 1 : get Python files from github : !git clone https://github.com/tensorflow/tensorflow.git第 1 步:从 github 获取 Python 文件: !git clone https://github.com/tensorflow/tensorflow.git

step 2 : append these files in my Python path :第 2 步:将这些文件附加到我的 Python 路径中:

import sys

sys.path.append('/content/tensorflow/tensorflow/examples/tutorials/mnist')

step 3 : load the MNIST data with 'input_data' fonction第 3 步:使用 'input_data' 函数加载 MNIST 数据

import input_data

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

That's all !就这样 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM