Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import tensorflow as tf
  2. from keras.models import Sequential
  3. from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
  4.  
  5. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
  6.  
  7. (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
  8. x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
  9. x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
  10. input_shape = (28, 28, 1)
  11. # Making sure that the values are float so that we can get decimal points after division
  12. x_train = x_train.astype('float32')
  13. x_test = x_test.astype('float32')
  14. # Normalizing the RGB codes by dividing it to the max RGB value.
  15. x_train /= 255
  16. x_test /= 255
  17. print('x_train shape:', x_train.shape)
  18. print('Number of images in x_train', x_train.shape[0])
  19. print('Number of images in x_test', x_test.shape[0])
  20.  
  21. model = Sequential()
  22. model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))
  23. model.add(MaxPooling2D(pool_size=(2, 2)))
  24. model.add(Flatten()) # Flattening the 2D arrays for fully connected layers
  25. model.add(Dense(128, activation=tf.nn.relu))
  26. model.add(Dropout(0.2))
  27. model.add(Dense(10,activation=tf.nn.softmax))
  28.  
  29. model.compile(optimizer='adam',
  30. loss='sparse_categorical_crossentropy',
  31. metrics=['accuracy'])
  32. model.fit(x=x_train,y=y_train, epochs=100)
  33.  
  34. model.evaluate(x_test, y_test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement