基于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 相关文章推荐
浅谈scrapy 的基本命令介绍
Jun 13 Python
利用Python写一个爬妹子的爬虫
Jun 08 Python
Python闭包思想与用法浅析
Dec 27 Python
在Python中表示一个对象的方法
Jun 25 Python
Python面向对象封装操作案例详解
Dec 31 Python
Python Tkinter Entry和Text的添加与使用详解
Mar 04 Python
Python读取文件内容为字符串的方法(多种方法详解)
Mar 04 Python
使用Nibabel库对nii格式图像的读写操作
Jul 01 Python
python合并多个excel文件的示例
Sep 23 Python
python 调用Google翻译接口的方法
Dec 09 Python
pandas中DataFrame检测重复值的实现
May 26 Python
python周期任务调度工具Schedule使用详解
Nov 23 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从给定url获取文件扩展名的方法
2015/03/14 PHP
解决更换PHP5.4以上版本后Dedecms后台登录空白问题的方法
2015/10/23 PHP
mysql_escape_string()函数用法分析
2016/04/25 PHP
php strftime函数获取日期时间(switch用法)
2018/05/16 PHP
Yii框架学习笔记之应用组件操作示例
2019/11/13 PHP
javascript 24小时弹出一次的代码(利用cookies)
2009/09/03 Javascript
js控制div及网页相关属性的代码
2009/12/19 Javascript
SwfUpload在IE10上不出现上传按钮的解决方法
2013/06/25 Javascript
js实现网站最上边可关闭的浮动广告条代码
2015/09/04 Javascript
解决WordPress使用CDN后博文无法评论的错误
2015/12/15 Javascript
js为什么不能正确处理小数运算?
2015/12/29 Javascript
sencha ext js 6 快速入门(必看)
2016/06/01 Javascript
JS监听微信、支付宝等移动app及浏览器的返回、后退、上一页按钮的事件方法
2016/08/05 Javascript
JS实现页面中所有img对象添加onclick事件及新窗口查看图片的方法
2016/12/27 Javascript
JavaScript限定范围拖拽及自定义滚动条应用(3)
2017/05/17 Javascript
JavaScript中发出HTTP请求最常用的方法
2018/07/12 Javascript
实用Javascript调试技巧分享(小结)
2019/06/18 Javascript
JS为什么说async/await是generator的语法糖详解
2019/07/11 Javascript
Layer.js实现表格溢出内容省略号显示,悬停显示全部的方法
2019/09/16 Javascript
nodejs环境使用Typeorm连接查询Oracle数据
2019/12/05 NodeJs
Vue发布订阅模式实现过程图解
2020/04/30 Javascript
Python获取任意xml节点值的方法
2015/05/05 Python
python生成IP段的方法
2015/07/07 Python
Sanic框架请求与响应实例分析
2018/07/16 Python
学习python的前途 python挣钱
2019/02/27 Python
django迁移数据库错误问题解决
2019/07/29 Python
CSS3字体效果的设置方法小结
2016/06/13 HTML / CSS
Beach Bunny Swimwear官网:设计师泳装和性感比基尼
2019/03/13 全球购物
Stutterheim瑞典:瑞典高级外套时装品牌
2019/06/24 全球购物
Java如何格式化日期
2012/08/07 面试题
.NET笔试题(20个问题)
2016/02/02 面试题
大学生创业感言
2014/01/25 职场文书
大学生职业生涯规划书
2014/03/14 职场文书
篮球比赛策划方案
2014/06/05 职场文书
科学发展观标语
2014/10/08 职场文书
和领导吃饭祝酒词
2015/08/11 职场文书