2.keras implementation Mnist Handwritten numeral classification problem first attempt (Python) __python

Source: Internet
Author: User
Tags unpack theano keras

After downloading the mnist dataset from my last article, the next step is to see how Keras classifies it.


Reference blog:

http://blog.csdn.net/vs412237401/article/details/51983440


The time to copy the code found in this blog is not working here, the preliminary judgment is because the Windows and Linux system path differences, handling a bit of a problem, so modified a little


First look at the original:

Def load_mnist (path, kind= ' train '):        "" "Load mnist data  from  ' path ' ""        labels_path = os.path.join (path,  ') %s-labels-idx1-ubyte '  % kind)        images_path =  Os.path.join (path,  '%s-images-idx3-ubyte '  % kind)        with  Open (labels_path,  ' RB ')  as lbpath:            Magic, n = struct.unpack (' >ii ',  lbpath.read (8))             labels = np.fromfile (lbpath, dtype=np.uint8)         with open (images_path,  ' RB ')  as imgpath:            magic, num, rows, cols = struct.unpack (">IIII",  Imgpath.read ( &NBSP));         images = np.fromfile (imgpath, dtype=np.uint8). Reshape (len (labels),  784)        return images, labels         x_train, y_train = load_mnist ('.. /data ',  kind= ' train ')    print (' rows: %d, columns: %d '  %  (x_train.shape[ 0], X_TRAIN.SHAPE[1])    x_test, y_test = load_mnist ('.. /data ',  kind= ' t10k ')    print (' rows: %d, columns: %d '  %  (x_test.shape[0)  X_TEST.SHAPE[1])   My understanding is that he placed the Mnist dataset file directly into the Python Engineering folder under the Data folder.

For Windows users, the point of the above code problem is that:

1.load_mnist ('.. /data ', kind= ' train '), for the window system, the separator should be \, however, even if the change of this place, still does not work, do not know is not the reason for the previous path abbreviation;

2. The above path without shorthand, complete write out, and in the string before adding r, still not workable ... The error appears in the with open (Labels_path, ' RB ') as Lbpath:

3. Even if the path is manually tuned out, Labels_path = os.path.join (path, '%s-labels-idx1-ubyte '% kind), this sentence gives you the file path of the compressed package, read out is the compression package, also wrong. Magic, num, rows, cols = Struct.unpack (">IIII", Imgpath.read (16)) read out the wrong parameters.


In view of the above problems adjusted for half a day, come up with the following solutions

1. Put the mnist uncompressed file directly under the Python Engineering folder, and modify the above code, the following directly paste all the modified code

ImportOsImportstructImportNumPy asNpdefLoad_mnist (kind=' Train '): "" "Load mnist data from ' Path '" "" Labels_path = ('%s-labels.idx1-ubyte '% kind) Images_path = ('%s-images.idx3-ubyte '% kind) withOpen (Labels_path,' RB ') asLbpath:magic, n = struct.unpack (' >ii ', Lbpath.read (8)) labels = Np.fromfile (Lbpath, Dtype=np.uint8)PrintMagic, N,labels withOpen (Images_path,' RB ') asImgpath:magic, num, rows, cols = Struct.unpack (">IIII", Imgpath.read (16))PrintMagic, num, rows, cols images = Np.fromfile (Imgpath, dtype=np.uint8). Reshape (len (labels), 784) #28 *28=784 returnImages, labels x_train, y_train = Load_mnist (kind=' Train ')Print(' Rows:%d, columns:%d '% (X_train.shape[0], x_train.shape[1]) x_test, y_test = Load_mnist (kind=' t10k ')Print(' Rows:%d, columns:%d '% (X_test.shape[0], x_test.shape[1])ImportTheano Theano.config.floatX =' float32 'X_train = X_train.astype (theano.config.floatX) x_test = X_test.astype (Theano.config.floatX) fromKeras.utilsImportNp_utilsPrint(' 3 data: ', X_train[:3])Print(' 3 Labels: ', Y_train[:3]) Y_train_ohe = np_utils.to_categorical (y_train) #change numbers to 0/1 mode,for example change 5 to [0 0 0 0 1 0 0 0 0 0]Print(' 3 labels (one-hot): ', Y_train_ohe[:3]) fromKeras.modelsImportSequential fromKeras.layers.coreImportDense fromKeras.optimizersImportSGD np.random.seed (1) model = sequential () model.add (Dense (input_dim=x_train.shape[1), output_dim=50, init=' Uniform ', activation=' Tanh ') Model.add (dense) (input_dim=50, output_dim=50, init=' Uniform ', activation=' Tanh ')) Model.add (Dense (input_dim=50, output_dim=y_train_ohe.shape[1), init=' Uniform ', activation=' Softmax ')) SGD = SGD (lr=0.001, decay=1e-7, momentum=.9) model.compile (loss=' Categorical_crossentropy ', OPTIMIZER=SGD, metrics=["Accuracy"] Model.fit (X_train, Y_train_ohe, nb_epoch=50, batch_size=300, verbose=1, validation_split=0.1) y_train_pred = model.predict_classes (X_train, verbose=0)Print(' 3 predictions: ', Y_train_pred[:3]) Train_acc = np.sum (Y_train = = y_train_pred, axis=0)/x_train.shape[0]Print(' Training accuracy:%.8f%% '% (TRAIN_ACC *)) y_test_pred = Model.predict_classes (x_test, verbose=0) TEST_ACC = np.sum (y_test = y_test_pred, Axi s=0)/x_test.shape[0]Print(' Test accuracy:%.8f%% '% (TEST_ACC * 100))
The above code can run smoothly, but the classification results are not the same as expected
53400/54000 [============================>]-eta:0s-loss:0.2002-acc:0.9409
53700/54000 [================= ===========>.] -eta:0s-loss:0.2003-acc:0.9409
54000/54000 [==============================]-2s-loss:0.2002-acc:0.9409 -val_loss:0.1840-val_acc:0.9478
(' 3 predictions: ', Array ([3, 0, 4])
training accuracy:0.00000000%
   test accuracy:0.00000000%

The accuracy rate is deliberately printed to 8 digits after the decimal point, the accuracy rate incredibly 0 .... What happened.
is the label deranged. Kind of square, looking for the answer ....

Postscript:
After debug, I found the problem with my Python division operation, dividing two integers or integers. The Python version results in a seemingly 3.0 version without this problem.
If you want to get a float result, convert one of the forces into a float, as
1/2 = 0
1/float (2) =0.5

In fact, there are two ways to deal with, please refer to the following:
http://blog.csdn.net/yygydjkthh/article/details/39377265
The last 5 lines of code should be amended to:
TRAIN_ACC = np.sum (Y_train = = y_train_pred, axis=0)/float (x_train.shape[0))
print(% (TRAIN_ACC *))


y_test_pred = model.predict_classes (x_test, verbose=0)
TEST_ACC = np.sum (y_test = y_test_pred, axis=0) /float (x_test.shape[0])
print(% (TEST_ACC * 100))

This completes the operation of the mnist handwritten numeral classification instance using Keras.
Next, continue to learn theoretical knowledge and use of Python.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.