基于Tensorflow读取MNIST数据集时网络超时的解决方式


Posted in Python onJune 22, 2020

最近在学习TensorFlow,比较烦人的是使用tensorflow.examples.tutorials.mnist.input_data读取数据

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('/temp/mnist_data/')
X = mnist.test.images.reshape(-1, n_steps, n_inputs)
y = mnist.test.labels

基于Tensorflow读取MNIST数据集时网络超时的解决方式

时,经常出现网络连接错误

解决方法其实很简单,这里我们可以看一下input_data.py的源代码(这里截取关键部分)

def maybe_download(filename, work_directory):
 """Download the data from Yann's website, unless it's already here."""
 if not os.path.exists(work_directory):
 os.mkdir(work_directory)
 filepath = os.path.join(work_directory, filename)
 if not os.path.exists(filepath):
 filepath, _ = urllib.request.urlretrieve(SOURCE_URL + filename, filepath)
 statinfo = os.stat(filepath)
 print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
return filepath

可以看到,代码会先检查文件是否存在,如果不存在再进行下载,那么我是不是自己下载数据不就行了?

MNIST的数据集是从Yann LeCun教授的官网下载,下载完成之后修改一下我们读取数据的代码,加上我们下载的路径即可

from tensorflow.examples.tutorials.mnist import input_data
import os

data_path = os.path.join('.', 'temp', 'data')
mnist = input_data.read_data_sets(datapath)
X = mnist.test.images.reshape(-1, n_steps, n_inputs)
y = mnist.test.labels

测试一下

基于Tensorflow读取MNIST数据集时网络超时的解决方式

成功!

补充知识:在tensorflow的使用中,from tensorflow.examples.tutorials.mnist import input_data报错

最近在学习使用python的tensorflow的使用,使用编辑器为spyder,在输入以下代码时会报错:

from tensorflow.examples.tutorials.mnist import input_data

报错内容如下:

from tensorflow.python.autograph.lang.special_functions import stack
ImportError: cannot import name 'stack'

为了解决这个问题,在

File "K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\autograph_init_.py"文件中直接把
from tensorflow.python.autograph.lang.special_functions import stack

这一行注释掉了,问题并没有解决。然后又把下面一行注释掉了:

from tensorflow.python.autograph.lang.special_functions import tensor_list

问题解决,但报了一大顿warning:

WARNING:tensorflow:From C:/Users/phmnku/.spyder-py3/tensorflow_prac/classification.py:4: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data\train-images-idx3-ubyte.gz
WARNING:tensorflow:From K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data\train-labels-idx1-ubyte.gz
WARNING:tensorflow:From K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\tf_should_use.py:189: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.

但是程序好歹能用了

以上这篇基于Tensorflow读取MNIST数据集时网络超时的解决方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
状态机的概念和在Python下使用状态机的教程
Apr 11 Python
python常用函数详解
Sep 13 Python
python 编写简单网页服务器的实例
Jun 01 Python
Python使用Shelve保存对象方法总结
Jan 28 Python
python读取有密码的zip压缩文件实例
Feb 08 Python
python dlib人脸识别代码实例
Apr 04 Python
Python3.8中使用f-strings调试
May 22 Python
Pyqt5实现英文学习词典
Jun 24 Python
python使用socket 先读取长度,在读取报文内容示例
Sep 26 Python
Django 再谈一谈json序列化
Mar 16 Python
Python异常原理及异常捕捉实现过程解析
Mar 25 Python
python如何构建mock接口服务
Jan 28 Python
在Mac中配置Python虚拟环境过程解析
Jun 22 #Python
tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
Jun 22 #Python
TensorFlow的环境配置与安装教程详解(win10+GeForce GTX1060+CUDA 9.0+cuDNN7.3+tensorflow-gpu 1.12.0+python3.5.5)
Jun 22 #Python
keras的load_model实现加载含有参数的自定义模型
Jun 22 #Python
python GUI模拟实现计算器
Jun 22 #Python
keras CNN卷积核可视化,热度图教程
Jun 22 #Python
python实现斗地主分牌洗牌
Jun 22 #Python
You might like
PHP APC缓存配置、使用详解
2014/03/06 PHP
PHP使用finfo_file()函数检测上传图片类型的实现方法
2017/04/18 PHP
PHP 7安装调试工具Xdebug扩展的方法教程
2017/06/17 PHP
Prototype使用指南之range.js
2007/01/10 Javascript
jQuery的写法不同导致的兼容性问题的解决方法
2010/07/29 Javascript
jQuery的运行机制和设计理念分析
2011/04/05 Javascript
jQuery之ajax删除详解
2014/02/27 Javascript
nodejs之请求路由概述
2014/07/05 NodeJs
js使用DOM设置单选按钮、复选框及下拉菜单的方法
2015/01/20 Javascript
JavaScript必知必会(五) eval 的使用
2016/06/08 Javascript
AngularJS入门教程之路由与多视图详解
2016/08/19 Javascript
Angular 中 select指令用法详解
2016/09/29 Javascript
浅析JavaScript中作用域和作用域链
2016/12/06 Javascript
微信小程序实现swiper切换卡内嵌滚动条不显示的方法示例
2018/12/20 Javascript
关于微信小程序获取小程序码并接受buffer流保存为图片的方法
2019/06/07 Javascript
Webpack中loader打包各种文件的方法实例
2019/09/03 Javascript
VUE 实现动态给对象增加属性,并触发视图更新操作示例
2019/11/29 Javascript
JS面向对象编程实现的拖拽功能案例详解
2020/03/03 Javascript
微信小程序 wx:for 与 wx:for-items 与 wx:key的正确用法
2020/05/19 Javascript
为python设置socket代理的方法
2015/01/14 Python
详解python读取和输出到txt
2019/03/29 Python
浅谈django2.0 ForeignKey参数的变化
2019/08/06 Python
python 直接赋值和copy的区别详解
2019/08/07 Python
python实现的读取网页并分词功能示例
2019/10/29 Python
使用pytorch搭建AlexNet操作(微调预训练模型及手动搭建)
2020/01/18 Python
python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例
2020/02/25 Python
使用python 计算百分位数实现数据分箱代码
2020/03/03 Python
解决python虚拟环境切换无效的问题
2020/04/30 Python
浅谈amaze-ui中datepicker和datetimepicker注意的几点
2020/08/21 HTML / CSS
销售经理工作职责
2014/02/03 职场文书
项目建议书模板
2014/05/12 职场文书
社区巾帼文明岗事迹材料
2014/06/03 职场文书
大学英语专业求职信
2014/06/21 职场文书
城市规划应届生推荐信
2014/09/08 职场文书
学校副校长四风对照检查材料整改措施
2014/09/25 职场文书
物流仓管员岗位职责
2015/04/01 职场文书