Home > other >  Python: why error No module named 'dataset. Mnist'. The dataset has been installed 1.1.2
Python: why error No module named 'dataset. Mnist'. The dataset has been installed 1.1.2

Time:11-19

Code:
The import sys, OS
Sys. Path. Append (OS. Pardir) # in order to import the parent directory of the file by setting the
The import numpy as np
From the dataset. Mnist import load_mnist # # # # error there is problem # # # # No module named 'dataset. Mnist'
The from PIL import Image


Def img_show (img) :
Pil_img=Image. Fromarray (np) uint8 (img))
Pil_img. The show ()

(x_train t_train), (x_test, t_test)=load_mnist (flatten=True, normalize=False)

Img=x_train [0]
Label=t_train [0]
Print (label) # 5

Print (img. Shape) # (784)
Img=img. Reshape (28, 28) # change the shape of the image to the size of the original
Print (img. Shape) # (28, 28)

Img_show (img)

CodePudding user response:

Don't know whether you have solved the problem
In this case, the dataset is not your dataset1.1.2, (introduction to deep learning) refers to the books in the code to the dataset file directory minist. Py,
Download address for https://www.ituring.com.cn/book/1921
I under Windows 7 or an error, I put the
Sys. Path. Append (OS) pardir) amended the run successfully

CodePudding user response:

@ enike, you is how to change

CodePudding user response:

I know, because the cause of the loading paths, if your mnist_show. Py and mnist. PKL under the level of the same path, then change to the from mnist import load_mnist

CodePudding user response:

Call the mnist. Py load_mnist method
 # coding: utf-8 
Try:
The import urllib. Request
Except ImportError:
Raise ImportError (' You should use Python 3 x ')
The import OS. Path
The import gzip
The import pickled
The import OS
The import numpy as np

Url_base='http://yann.lecun.com/exdb/mnist/'
Key_file={
'train_img' : 'train - images - idx3 does - ubyte. Gz',
'train_label' : 'train - labels - idx1 - ubyte. Gz',
'test_img' : 't10k - images - idx3 does - ubyte. Gz',
'test_label' : 't10k - labels - idx1 - ubyte. Gz'
}

Dataset_dir=OS. The path. The dirname (OS) path) abspath (__file__))
Save_file dataset_dir +="./mnist PKL
"
Train_num=60000
Test_num=10000
Img_dim=(1, 28, 28)
Img_size=784


Def _download (file_name) :
File_path=dataset_dir + "/" + file_name

If OS. Path. The exists (file_path) :
Return

Print (" Downloading "+ file_name +"...
")Urllib. Request. Urlretrieve (url_base + file_name, file_path)
Print (" Done ")


Def download_mnist () :
In the for v key_file. Values () :
_download (v)


Def _load_label (file_name) :
File_path=dataset_dir + "/" + file_name

Print (" Converting "+ file_name +" to NumPy Array...
")With gzip. Open (file_path, 'rb') as f:
Labels=np. Frombuffer (f.r ead (), np. Uint8, offset=8)
Print (" Done ")

The return labels


Def _load_img (file_name) :
File_path=dataset_dir + "/" + file_name

Print (" Converting "+ file_name +" to NumPy Array...
")With gzip. Open (file_path, 'rb') as f:
Data=https://bbs.csdn.net/topics/np.frombuffer (f.r ead (), np. Uint8, offset=16)
Data=(1, img_size https://bbs.csdn.net/topics/data.reshape
Print (" Done ")

The return data


Def _convert_numpy () :
The dataset={}
The dataset [' train_img]=_load_img (key_file [' train_img '])
The dataset [' train_label]=_load_label (key_file [' train_label '])
The dataset [' test_img]=_load_img (key_file [' test_img '])
The dataset [' test_label]=_load_label (key_file [' test_label '])

Return the dataset


Def init_mnist () :
Download_mnist ()
The dataset=_convert_numpy ()
Print (" Creating pickle file...
")With the open (save_file, 'wb) as f:
Pickle. Dump (dataset, f, 1)
Print (" Done!" )


Def _change_one_hot_label (X) :
T=np. Zeros ((X.s considering, 10))
For independence idx, row in enumerate (T) :
Row [[independence idx]] X=1

Return T


Def load_mnist (normalize=True, flatten=True, one_hot_label=False) :
"" "read MNIST dataset

The Parameters
-- -- -- -- -- -- -- -- -- --
To normalize the image pixel values regularization is 0.0 ~ 1.0
One_hot_label:
One_hot_label to True, return label as one - hot array
Refers to the one - hot array [0,0,1,0,0,0,0,0,0,0] such an array of
Flatten: whether the image for a one-dimensional array

Returns
-- -- -- -- -- -- --
(training images, the label), (test image, label)
"" "
If not OS. Path. The exists (save_file) :
Init_mnist ()

With the open (save_file, 'rb') as f:
The dataset=pickle. The load (f)

If the normalize:
For the key in (' train_img ', 'test_img) :
The dataset [key]=dataset [key] astype (np) float32)
[key] the dataset/=255.0

If one_hot_label:
The dataset [' train_label]=_change_one_hot_label (dataset [' train_label '])
The dataset [' test_label]=_change_one_hot_label (dataset [' test_label '])

If not flatten:
For the key in (' train_img ', 'test_img) :
The dataset [key]=dataset [key]. Reshape (1, 1, 28, 28)

Return (a dataset [' train_img], the dataset [' train_label ']), (the dataset [' test_img], the dataset [' test_label '])


If __name__=="__main__ ':
Init_mnist ()

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related