pdpd123

Machine_Learning

May 22nd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import keras
  2. from keras.datasets import mnist
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Dropout, LeakyReLU, Conv2D
  5. from keras.optimizers import RMSprop
  6. from keras import backend as K
  7. K.tensorflow_backend._get_available_gpus()
  8. from matplotlib.pyplot import imshow
  9. import numpy as np
  10. from tensorflow.examples.tutorials.mnist import input_data
  11.  
  12. # 讀入 MNIST
  13. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  14.  
  15. # the data, shuffled and split between train and test sets
  16. x_train = x_train.reshape(60000 , 784)
  17. x_test = x_test.reshape(10000, 784)
  18.  
  19. x_train = x_train.astype('float32')
  20. x_test = x_test.astype('float32')
  21. x_train /= 255
  22. x_test /= 255
  23.  
  24. # convert class vectors to binary class matrices
  25. num_classes = 10
  26. y_train = keras.utils.to_categorical(y_train, num_classes)
  27. y_test = keras.utils.to_categorical(y_test, num_classes)
  28.  
  29. print(x_train.shape, 'train samples')
  30. print(x_test.shape, 'test samples')
  31. print(y_train.shape, 'train labels')
  32. print(y_test.shape, 'test labels')
  33.  
  34. # modeling
  35. model = Sequential()
  36. model.add(Dense(680, activation=LeakyReLU(), input_shape=(784,)))
  37. model.add(Dense(1230, activation=LeakyReLU()))
  38. model.add(Dense(10, activation='softmax'))
  39. #model.summary()
  40.  
  41. print('modling complete')
  42.  
  43. # training
  44. model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  45.  
  46. print('training complete')
  47.  
  48. # testing
  49.  
  50. model.fit(x_train, y_train, batch_size=150, epochs=20, verbose=1)
  51.  
  52. scores = model.evaluate(x_test, y_test)
  53.  
  54. print('Acc on testing data : ', scores[1])
Add Comment
Please, Sign In to add comment