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:
- Import the necessary libraries:
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten
- 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'))
- Compile the model:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
- Fit the model:
model.fit(x_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=(x_test, y_test))
- Evaluate the model:
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
- Make predictions:
predictions = model.predict(x_test)
- Save the model:
model.save('my_model.h5')
Helpful links
More of Python Keras
- How do I use Python Keras to create a Zoom application?
- How do I use Python Keras to zip a file?
- How do I save weights in a Python Keras model?
- How can I enable verbose mode when using Python Keras?
- How can I use word2vec and Keras to develop a machine learning model in Python?
- How can I use Python with Keras to build a deep learning model?
- How can I use Python Keras to develop a reinforcement learning model?
- How can I resolve the issue of Python module Tensorflow.keras not being found?
- How do I install the Python Keras .whl file?
- How can I use batch normalization in Python Keras?
See more codes...