9951 explained code solutions for 126 technologies


python-kerasHow can I use Python and Keras to create a Convolutional Neural Network?


To create a Convolutional Neural Network (CNN) using Python and Keras, you need to follow these steps:

  1. Import the necessary libraries:
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten
  1. Create a model and add layers:
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
  1. Compile the model:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  1. Fit the model:
model.fit(x_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=(x_test, y_test))
  1. Evaluate the model:
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
  1. Make predictions:
predictions = model.predict(x_test)
  1. Save the model:
model.save('my_model.h5')

Helpful links

Edit this code on GitHub