No Module named “tensorflow.examples.tutorial“.

关于用Tensorflow2.4导入mnist数据集时,No Module named “tensorflow.examples.tutorial”.

这个问题,已经有很多博客提到,大部分都给出这样的解决方案:找到tensorflow目录下的core,core下面的examples,如果在里面没有tutorial文件,就到官网下载tutorial文件夹,然后存到examples目录下。
但是还是报错。下面给出基于以上步骤后,我自己的解决方案。

首先,点开tutorial里面的mnist,里面有一个_init_.py文件
在这里插入图片描述
点进去之后,拉到最后,原代码是:
from tensorflow.examples.tutorials.mnist import data
这里请看我上一张截图,按照文件夹顺序,应该是tensorflow/core/examples/tutorials/mnist
所以当然会报错,找不到tensorflow.examples

所以,改成如下的样子,保存即可。
在这里插入图片描述
另外需要注意的是,有些人的example文件夹,可能是examples,有的人的是example。一定要和上图中代码的保持一致。这里建议,如果你的文件夹名字是example,而不是examples的话,重命名文件夹为examples。

为什么不把examples全都换成example呢?因为还有其他的一些py文件,源代码里面用的都是examples,方便起见,还是把example改成examples吧!

最后,在我们的训练代码里面,同样,把
from tensorflow.examples.tutorials.mnist inport input_data
改成
from tensorflow.core.examples.tutorials.mnist inport input_data
在这里插入图片描述
通过以上步骤,即可解决其他博客没有照顾到的这个问题。

猜你喜欢

转载自blog.csdn.net/WessonX/article/details/120574030