天天看點

PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程

1.1 錯誤

教程上使用torchvision來下載下傳MNIST資料集,代碼如下:

# 使用内置函數下載下傳資料集
train_set = mnist.MNIST('./data', train=True, download=True)
test_set = mnist.MNIST('./data', train=False, download=True)      

運作時報錯,主要資訊如下:

AttributeError: module 'torch.nn' has no attribute 'ModuleDict'      

1.2 原因

查閱後發現是版本沖突的問題,0.4.1版本才有ModuleDict子產品,于是将原先的0.4.0版本的torch解除安裝重新下載下傳0.4.1版本

1.3 解決

# 解除安裝
pip uninstall torch
# 重新安裝
pip install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl      
PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程
PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程

同時也更新了對應的torchvision版本:0.4.1==》0.2.2

# 解除安裝
pip uninstall torchvision

# 安裝
pip install torchvision==0.2.2      
PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程

2.1 錯誤

修改之後可以下載下傳,但是經常下載下傳到一半出現錯誤

EOFError: Compressed file ended before the end-of-stream marker was reached      

2.2 原因

查閱後發現是下載下傳錯誤,導緻檔案損壞。

檢視自己工程所在目錄,轉到raw檔案夾下:

PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程

可以發現檔案夾中有部分解壓過的檔案,和未解壓的檔案,如果手動解壓的話同樣會出現上述錯誤;

2.3 解決

重新删除/下載下傳幾次後,仍然不行,

于是決定通過百度網盤手動下載下傳檔案,可以參考這裡​​@RainingZ 【mnist資料集下載下傳位址】​​,親測有效。

下載下傳完成是這樣:

PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程

把他們拷貝到工程中的PycharmProjects\PyTorchTest\Include\data\MNIST\raw檔案夾下(不需要解壓),

再次運作程式:

import numpy as np
import torch
from torchvision.datasets import mnist # 導入pytorch内置資料
from torch import nn
from torch.autograd import Variable

# 使用内置函數下載下傳資料集
train_set = mnist.MNIST('./data', train=True, download=True)    # 若未找到資料集 則自動下載下傳
test_set = mnist.MNIST('./data', train=False, download=True)      
PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程

此時raw檔案夾下内容變為:

PyTorch使用【内置函數下載下傳資料集MNIST】時出現的錯誤及解決過程

成功*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。