中信建設(shè)有限責任公司洪波頁優(yōu)化軟件
在進行Mnist手寫識別的項目中,出現(xiàn)了Mnist數(shù)據(jù)集下載出錯的問題,報出以下錯誤:
Exception: URL fetch failure on https://s3.amazonaws.com/img-datasets/mnist.npz: None – [WinError 10060] 由于連接方在一段時間后沒有正確答復(fù)或連接的主機沒有反應(yīng),連接嘗試失敗。
MNIST數(shù)據(jù)集包含四個gz文件。這些文件分別包含訓(xùn)練集圖像、訓(xùn)練集標簽、測試集圖像和測試集標簽。
你可以從官方網(wǎng)站下載這些文件。以下是MNIST數(shù)據(jù)集的官方網(wǎng)站鏈接:http://yann.lecun.com/exdb/mnist/
在該網(wǎng)站上,你可以找到以下四個文件:
- train-images-idx3-ubyte.gz:訓(xùn)練集圖像
- train-labels-idx1-ubyte.gz:訓(xùn)練集標簽
- t10k-images-idx3-ubyte.gz:測試集圖像
- t10k-labels-idx1-ubyte.gz:測試集標簽
你可以下載這些文件,并將它們保存在本地路徑中。然后,你可以使用適當?shù)膸?#xff08;如gzip和numpy)來解壓和加載這些文件,以獲取MNIST數(shù)據(jù)集的特征和標簽。
以下是一個示例代碼,演示如何加載MNIST數(shù)據(jù)集的圖像和標簽:
import gzip
import numpy as npdef load_mnist_images(path):with gzip.open(path, 'rb') as f:# 跳過文件頭f.read(16)# 讀取圖像數(shù)據(jù)buf = f.read()# 將字節(jié)數(shù)據(jù)轉(zhuǎn)換為numpy數(shù)組data = np.frombuffer(buf, dtype=np.uint8)# 重新整形為圖像數(shù)組data = data.reshape(-1, 28, 28)return datadef load_mnist_labels(path):with gzip.open(path, 'rb') as f:# 跳過文件頭f.read(8)# 讀取標簽數(shù)據(jù)buf = f.read()# 將字節(jié)數(shù)據(jù)轉(zhuǎn)換為numpy數(shù)組labels = np.frombuffer(buf, dtype=np.uint8)return labels# 指定文件路徑
train_images_path = 'path_to_train-images-idx3-ubyte.gz'
train_labels_path = 'path_to_train-labels-idx1-ubyte.gz'
test_images_path = 'path_to_t10k-images-idx3-ubyte.gz'
test_labels_path = 'path_to_t10k-labels-idx1-ubyte.gz'# 加載訓(xùn)練集圖像和標簽
train_images = load_mnist_images(train_images_path)
train_labels = load_mnist_labels(train_labels_path)# 加載測試集圖像和標簽
test_images = load_mnist_images(test_images_path)
test_labels = load_mnist_labels(test_labels_path)# 打印數(shù)據(jù)集信息
print("訓(xùn)練集樣本數(shù)量:", train_images.shape[0])
print("測試集樣本數(shù)量:", test_images.shape[0])
print("輸入特征形狀:", train_images[0].shape)
print("標簽形狀:", train_labels.shape)# 進行模型訓(xùn)練和評估的代碼可以繼續(xù)編寫...