ModuleNotFoundError: No module named 'tensorflow.examples'

ModuleNotFoundError: No module named 'tensorflow.examples'

当我导入tensorflow时

import tensorflow as tf

我没有收到错误。但是,我确实收到以下错误。如果有帮助,我正在使用 spyder。

根据其他问题,我使用 conda 和 pip 安装确保了最新的 (v1.8) tensorflow。这并没有解决问题。请协助。

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

ModuleNotFoundError: No module named 'tensorflow.examples'

有时在下载 TF 时,示例目录可能不可用。您可以通过 link 将 GitHub 存储库中的 'example' 目录放入 tensorflow python wheel 文件夹中来纠正它。这样你就不需要更改代码了。

如果这不起作用,请尝试将 import tensorflow.examples.tutorials.mnist.input_data as input_data 替换为 import input_data,如 link 中所述:

希望对您有所帮助!!!

我在Mac上解决了这个问题,只需将官方示例复制到tensorflow_core/examples目录即可。

  1. 拉取tensorflow代码

    git clone https://github.com/tensorflow/tensorflow

  2. 复制示例到系统python3目录

    cp -a tensorflow/examples/ /usr/local/lib/python3.7/site-packages/tensorflow_core/examples/

有时 TensorFlow 示例未预先下载,因此您可能需要 运行 下面的命令使用下面的代码从 Github 安装示例。

!pip install -q git+https://github.com/tensorflow/examples.git

我认为你应该在 tensorflow 2 上像下面这样使用

import tensorflow_datasets
mnist = tensorflow_datasets.load('mnist')

使用下面的命令,它会下载数据。它来自 tensorflow documentation

import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

您需要下载数据集才能使用。

命令:

pip install tensorflow-datasets

代码部分:

mnist_train = tfds.load(name="mnist", split="train")

你现在已经完成了。编码愉快! :)

不同方法 OS: Windows

复制自:

https://github.com/tensorflow/examples/tree/master/tensorflow_examples

[python folder]\Lib\site-packages\tensorflow_examples

使用

import tensorflow_examples

例子

from tensorflow_examples.models import pix2pix

但对于数据集使用:

pip install tensorflow_datasets

我通过将 **tutorial** 目录添加到 tensorflow_core 解决了这个问题,通常在缺少此文件时会弹出此问题

  1. ..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples 检查此目录以查看是否有 tutorials 文件。 lack of tutorial file
  2. 如果没有,请转至 https://github.com/tensorflow/tensorflow 下载 zip 文件,然后全部解压(或打开)。 download tutorial file
  3. tensorflow-master\tensorflow\examples\ 中找到教程文件,并将其复制到 ..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples
  4. 问题已解决。 run
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
im = mnist.train.images[1]
im = im.reshape(-1, 28)
plt.imshow(im)

在 Tensorflow 2.0 中加载 mnist 数据集:

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

引用如下: TensorFlow 2 quickstart for beginners

另一种方法(也适用于本地保存的数据集):

DATA_URL = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz'

path = tf.keras.utils.get_file('mnist.npz', DATA_URL)
with np.load(path) as data:
  train_examples = data['x_train']
  train_labels = data['y_train']
  test_examples = data['x_test']
  test_labels = data['y_test']

引用如下: Load NumPy data

您只需要下载丢失的文件并复制到tensorflow-core/examples即可。

对我来说 windows 10 是: C:\Users\Amirreza\AppData\Local\Programs\Python\Python37\Lib\site-packages\tensorflow_core\examples

此文件夹已在 September/2020 删除。参见 their repository

我使用了命令:

git clone https://github.com/tensorflow/tensorflow.git
tensorflow> git checkout c31acd156c