What it is
Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, Microsoft Cognitive Toolkit (CNTK), or Theano. It allows for easy and fast prototyping of deep learning models with minimal code.
Keras allows you to define deep learning models using Sequential or Functional APIs. You can build layers, compile models, train with fit(), evaluate, and make predictions easily. It supports a wide variety of layers, optimizers, loss functions, and metrics.
Installation
pip install kerasGetting started
The smallest useful thing you can do with it, and what each part means.
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(32, activation='relu', input_shape=(784,)),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])import numpy as np
X_train = np.random.rand(100,784)
y_train = np.random.randint(0,10,100)
model.fit(X_train, y_train, epochs=5, batch_size=10)Advanced usage
Where the library earns its place over a simpler alternative.
from tensorflow.keras import Input, Model
inputs = Input(shape=(784,))
x = layers.Dense(64, activation='relu')(inputs)
outputs = layers.Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='loss', patience=3)
model.fit(X_train, y_train, epochs=50, callbacks=[early_stop])model.save('my_keras_model.h5')
new_model = keras.models.load_model('my_keras_model.h5')Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- ValueError: Input arrays should have the same number of samples
- Ensure that your features and labels arrays have the same number of samples.
- InvalidArgumentError
- Check that the input shape matches the model's expected input.
- ModuleNotFoundError: No module named 'keras'
- Install Keras using pip or conda in your current Python environment.
Best practices
- Normalize input data for faster convergence.
- Use validation sets and callbacks to prevent overfitting.
- Leverage the Functional API for complex models like multi-input/multi-output networks.
- Use pretrained models from Keras Applications for transfer learning.
- Monitor training with TensorBoard for visual insights.
Background
Why it exists, and what it was reacting to.
Keras was developed by François Chollet and released in March 2015. Its design philosophy focuses on user-friendliness, modularity, and extensibility. Keras became popular for its simple API that abstracts the complexities of deep learning, and it was later integrated tightly with TensorFlow as its official high-level API.
