基于Tensorflow讀取MNIST數(shù)據(jù)集時(shí)網(wǎng)絡(luò)超時(shí)的解決方式
最近在學(xué)習(xí)TensorFlow,比較煩人的是使用tensorflow.examples.tutorials.mnist.input_data讀取數(shù)據(jù)
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
時(shí),經(jīng)常出現(xiàn)網(wǎng)絡(luò)連接錯(cuò)誤
解決方法其實(shí)很簡(jiǎn)單,這里我們可以看一下input_data.py的源代碼(這里截取關(guān)鍵部分)
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
可以看到,代碼會(huì)先檢查文件是否存在,如果不存在再進(jìn)行下載,那么我是不是自己下載數(shù)據(jù)不就行了?
MNIST的數(shù)據(jù)集是從Yann LeCun教授的官網(wǎng)下載,下載完成之后修改一下我們讀取數(shù)據(jù)的代碼,加上我們下載的路徑即可
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
測(cè)試一下
成功!
補(bǔ)充知識(shí):在tensorflow的使用中,from tensorflow.examples.tutorials.mnist import input_data報(bào)錯(cuò)
最近在學(xué)習(xí)使用python的tensorflow的使用,使用編輯器為spyder,在輸入以下代碼時(shí)會(huì)報(bào)錯(cuò):
from tensorflow.examples.tutorials.mnist import input_data
報(bào)錯(cuò)內(nèi)容如下:
from tensorflow.python.autograph.lang.special_functions import stack
ImportError: cannot import name 'stack'
為了解決這個(gè)問(wèn)題,在
File "K:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\autograph_init_.py"文件中直接把
from tensorflow.python.autograph.lang.special_functions import stack
這一行注釋掉了,問(wèn)題并沒(méi)有解決。然后又把下面一行注釋掉了:
from tensorflow.python.autograph.lang.special_functions import tensor_list
問(wèn)題解決,但報(bào)了一大頓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數(shù)據(jù)集時(shí)網(wǎng)絡(luò)超時(shí)的解決方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中的變量及簡(jiǎn)單數(shù)據(jù)類型應(yīng)用
這篇文章主要介紹了Python中的變量及簡(jiǎn)單數(shù)據(jù)類型應(yīng)用,簡(jiǎn)單的數(shù)據(jù)類型包括字符串和數(shù)字,更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-03-03Python按行讀取文件的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Python按行讀取文件的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06pandas庫(kù)中to_datetime()方法的使用解析
這篇文章主要介紹了pandas庫(kù)中to_datetime()方法的使用解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07AI生成圖片Stable?Diffusion環(huán)境搭建與運(yùn)行方法
Stable?Diffusion是一種基于擴(kuò)散過(guò)程的生成模型,由Ge?et?al.在2021年提出,該模型利用了隨機(jī)變量的穩(wěn)定分布,通過(guò)遞歸地應(yīng)用擴(kuò)散過(guò)程來(lái)生成高質(zhì)量的圖像,這篇文章主要介紹了AI圖片生成Stable?Diffusion環(huán)境搭建與運(yùn)行,需要的朋友可以參考下2023-05-05Python pyinstaller庫(kù)的安裝配置教程分享
pyinstaller模塊主要用于python代碼打包成exe程序直接使用,這樣在其它電腦上即使沒(méi)有python環(huán)境也是可以運(yùn)行的。本文就來(lái)和大家分享一下pyinstaller庫(kù)的安裝配置教程,希望對(duì)大家有所幫助2023-04-04python常用時(shí)間庫(kù)time、datetime與時(shí)間格式之間的轉(zhuǎn)換教程
Python項(xiàng)目中很多時(shí)候會(huì)需要將時(shí)間在Datetime格式和TimeStamp格式之間轉(zhuǎn)化,下面這篇文章主要給大家介紹了關(guān)于python常用時(shí)間庫(kù)time、datetime與時(shí)間格式之間轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2023-02-02