''' how you might build a convolutional neural network (CNN) for the MNIST dataset using the Keras deep learning library in Python. ''' #Load the MNIST dataset from keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() #Preprocess the data from keras.utils import to_categorical x_train = x_train.reshape((x_train.shape[0], 28, 28, 1)) x_train = x_train.astype('float32') / 255.0 y_train = to_categorical(y_train) x_test = x_test.reshape((x_test.shape[0], 28, 28, 1)) x_test = x_test.astype('float32') / 255.0 y_test = to_categorical(y_test) #Build the CNN model from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense model = Sequential() model.add(Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1))) model.add(MaxPooling2D((2,2))) model.add(Conv2D(64, (3,3), activation='relu')) model.add(MaxPooling2D((2,2))) model.add(Flatten()) model.add(Dense(64, activation='relu')) model.add(Dense(10, activation='softmax')) #Compile the model model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) #Train the model model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test)) #Evaluate the model loss, acc = model.evaluate(x_test, y_test) print('Test Accuracy: %.3f' % (acc * 100))