Deep learning Python Script Training Keras mnist digital recognition model __python

Source: Internet
Author: User
Tags random seed python script keras

This script is a training Keras mnist digital Recognition program, previously sent, today to achieve the forecast,


# larger CNN for the mnist Dataset # 2.Negative dimension size caused by subtracting 5 from 1 for ' conv2d_4/convolution ' ( OP: ' conv2d ') with input shapes # 3.userwarning:update your ' conv2d ' call to the Keras 2 Api:http://blog.csdn.net/johini eli/article/details/69222956 # 4.Error checking input:expected conv2d_1_input to have shape (None, a, 1) but go

T array with shape (60000, 1, a) # talk to Wumi,you good. # python 3.5.4 # keras.__version__: 2.0.6 # thensorflow 1.2.1 # Theano 0.10.0beta1 # Good Blog # http://blog.csdn.net/s hizhengxin123/article/details/72383728 # http://www.360doc.com/content/17/0415/12/1489589_645772879.shtml #  Recommand Another framework http://tflearn.org/examples/import numpy import Keras from keras.datasets import mnist from Keras.models import sequential from keras.layers import dense to keras.layers import dropout from keras.layers import F Latten from keras.layers.convolutional import conv2d from keras.layers.convolutional import MAxpooling2d from keras.utils import np_utils import Matplotlib.pyplot as plt to keras.constraints import maxnorm from Ke 
Ras.optimizers Import SGD from keras.preprocessing import image import Skimage.io # fix random seed for reproducibility

Seed = 7 Numpy.random.seed (SEED) # Load data (X_train, Y_train), (x_test, y_test) = Mnist.load_data () plt.subplot (221) Plt.imshow (X_train[0], Cmap=plt.get_cmap (' Gray ')) Plt.show () # Reshape to be [samples][pixels][width][height] X_train = X_train.reshape (X_train.shape[0], 1). Astype (' float32 ') x_test = X_test.reshape (x_test.shape[0), 28, 28, 1). Astype (' float32 ') # X_train = X_train.reshape (1, 1). Astype (' float32 ') valueerror:cannot reshape array of size 470  40000 into shape (1,28,28,1) #X_test = X_test.reshape (1, 1). Astype (' float32 ') valueerror:cannot reshape array of Size 47040000 into shape (1,28,28,1) # X_train = X_train.reshape (X_train.shape[0], 1, a). Astype (' float32 ') # X_test = X_test.reshape (X_test.shape[0]1, Astype (' float32 ') <---4 # normalize inputs from 0-255 to 0-1 x_train = x_train/255 x_test = x_test/ 255 # One hot encode outputs Y_train = np_utils.to_categorical (y_train) y_test = np_utils.to_categorical (y_test) Num_class
    es = y_test.shape[1] # # #raw # define the larger model Def Larger_model (): # Create Model model = sequential () Model.add (conv2d (5, 5), padding= ' valid ', input_shape= (1), activation= ' Relu ')) # Model.add (30, 5 , 5), padding= ' valid ', input_shape= (28,1), activation= ' Relu ')) <----3,2 Model.add (maxpooling2d (2, 2)) Model.add (Dropout (0.4)) Model.add (conv2d (3, 3), activation= ' Relu ') Model.add (Maxpooling2d (pool_size = (2, 2)) Model.add (Dropout (0.4)) Model.add (Flatten ()) Model.add (dense (128, activation= ' Relu ')) Model.add ( Dropout (0.4)) Model.add (Dense (activation= ' relu ')) Model.add (Dropout (0.4)) Model.add (Dense (num_classes, act
    ivation= ' Softmax ')# Compile Model # Optimizer Optimizer # loss loss function Model.compile (loss= ' categorical_crossentropy ', optimizer= ' Adam ', metrics=[' accuracy '] return model # builds the model = Larger_model () # Fit the Model # Fit function returns a History object whose h The Istory.history property records the change of the value of the loss function and other metrics with the epoch, and if there is a validation set, it also contains the changes of these indices of the validation set Model.fit (X_train, Y_train, validation_data= (X_test, Y_test), epochs=10, batch_size=200, verbose=2) # epochs too bigger # Model.fit (X_train, Y_train, V Alidation_data= (X_test, Y_test), nb_epoch=200, batch_size=200, verbose=2) # Final evaluation of the model scores = MODEL.E Valuate (X_test, Y_test, verbose=0) print ("Large CNN Error:%.2f%%"% (100-scores[1] *)) # Save the Model Model.save (' D:\\works\\jetbrians\\pycharmprojects\\trypicture\\my_model.h5 ') # Creates a HDF5 file ' My_model.h5 ' del model # Reloa d The Modle # returns a compiled model # identical to the previous one # modeltrained = sequential () # model = Modeltraine D.load_model (' D:\\works\\jetbrianS\\pycharmprojects\\trypicture\\my_model.h5 ') # https://gist.github.com/ageitgey/  A40DDED08E82E59724C70DA23786BBF0 # Write a number in a picture # predict numbers #image_path = './lena.jpg ' # method 1 # Load pic #img = image.load_img (Image_path, target_size=) # handle pic #x = Image.img_to_array (img) #x = numpy.exp And_dims (x, axis=0) #x = Preprocess_input (x) # Method2 #img2 = Skimage.io.imread (Image_path, As_grey=true) #skimage. io.im Show (IMG2) #plt. Show () #img2 = Numpy.reshape (Img2, (1, 1)). Astype (' float32 ') # predicts numbers #https://baijiahao.baidu.c OM/S?ID=1574962680356106&AMP;WFR=SPIDER&AMP;FOR=PC #predict = model.predict (Img2, verbose=0) #result = Model.prediect  _classes (Img2, verbose=0) #print (predict[0]) #print (result[0)) #some warning tips The TensorFlow library wasn ' t compiled
To use AVX2 instructions, but this are available on your machine and could the up CPU speed. #have no idea what ' s the meaning original data style ================= training log D:\applications\ANaconda3\python.exe d:/works/jetbrians/pycharmprojects/trypicture/trainmodel/trainmodel.py Using TensorFlow
Backend. Train on 60000 samples, validate on 10000 samples Epoch 1/10 62S-LOSS:0.8830-ACC:0.7027-VAL_LOSS:0.1566-VAL_ACC : 0.9545 Epoch 2/10 56s-loss:0.3130-acc:0.9078-val_loss:0.0955-val_acc:0.9712 Epoch 3/10 61s-loss:0.2342-  acc:0.9340-val_loss:0.0737-val_acc:0.9763 Epoch 4/10 58S-LOSS:0.1924-ACC:0.9458-VAL_LOSS:0.0643-VAL_ACC: 0.9802 Epoch 5/10 60s-loss:0.1678-acc:0.9534-val_loss:0.0541-val_acc:0.9848 Epoch 6/10 cc:0.9578-val_loss:0.0468-val_acc:0.9849 Epoch 7/10 53S-LOSS:0.1396-ACC:0.9617-VAL_LOSS:0.0464-VAL_ACC: 0.9852 Epoch 8/10 55s-loss:0.1303-acc:0.9647-val_loss:0.0422-val_acc:0.9871 Epoch 9/10 c:0.9656-val_loss:0.0398-val_acc:0.9871 Epoch 10/10 53S-LOSS:0.1156-ACC:0.9680-VAL_LOSS:0.0370-VAL_ACC: 0.9876 Large CNN error:1.24%

Process finished with exit code 0   

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.