基于Tensorflow讀取MNIST數(shù)據(jù)集時網(wǎng)絡(luò)超時的解決方式
最近在學(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
時,經(jīng)常出現(xiàn)網(wǎng)絡(luò)連接錯誤
解決方法其實很簡單,這里我們可以看一下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
可以看到,代碼會先檢查文件是否存在,如果不存在再進行下載,那么我是不是自己下載數(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
測試一下
成功!
補充知識:在tensorflow的使用中,from tensorflow.examples.tutorials.mnist import input_data報錯
最近在學(xué)習(xí)使用python的tensorflow的使用,使用編輯器為spyder,在輸入以下代碼時會報錯:
from tensorflow.examples.tutorials.mnist import input_data
報錯內(nèi)容如下:
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數(shù)據(jù)集時網(wǎng)絡(luò)超時的解決方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中的變量及簡單數(shù)據(jù)類型應(yīng)用
這篇文章主要介紹了Python中的變量及簡單數(shù)據(jù)類型應(yīng)用,簡單的數(shù)據(jù)類型包括字符串和數(shù)字,更多詳細內(nèi)容,需要的小伙伴可以參考一下2022-03-03AI生成圖片Stable?Diffusion環(huán)境搭建與運行方法
Stable?Diffusion是一種基于擴散過程的生成模型,由Ge?et?al.在2021年提出,該模型利用了隨機變量的穩(wěn)定分布,通過遞歸地應(yīng)用擴散過程來生成高質(zhì)量的圖像,這篇文章主要介紹了AI圖片生成Stable?Diffusion環(huán)境搭建與運行,需要的朋友可以參考下2023-05-05python常用時間庫time、datetime與時間格式之間的轉(zhuǎn)換教程
Python項目中很多時候會需要將時間在Datetime格式和TimeStamp格式之間轉(zhuǎn)化,下面這篇文章主要給大家介紹了關(guān)于python常用時間庫time、datetime與時間格式之間轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2023-02-02