Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow. This page provides a collection of examples showcasing various Keras functionalities.
Installation
Before you start, make sure you have Keras installed. You can install it using pip:
pip install keras
Examples
1. Basic Neural Network
Here's a simple example of a neural network using Keras:
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
2. Convolutional Neural Network
Convolutional Neural Networks (CNNs) are particularly effective for image recognition tasks. Here's an example of a CNN using Keras:
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=(64, 64, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
3. Recurrent Neural Network
Recurrent Neural Networks (RNNs) are great for sequence prediction problems. Here's an example of an RNN using Keras:
from keras.models import Sequential
from keras.layers import LSTM, Dense
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(100, 1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
Further Reading
For more examples and tutorials, check out the Keras documentation.
Convolutional Neural Network
Recurrent Neural Network