Keras
Keras models
There are two main models: the Sequential model and the Model class. So models are class.
They have methods and attributes in common.
- model.layers
- model.inputs
- model.outputs
- model.summary()
- model.get_config()
- model.get_weights()
- model.set_weights(weights)
- model.to_json
- model.to_yaml()
- model.save_weights(filepath)
- model.load_weights(filepath, by_name=False)
You can create model by subclassing the Model class.
import keras
class SimpleMLP(keras.Model):
def __init__(self, use_bn=False, use_dp=False, num_classes=10):
...
def call(self, inputs):
...
model = SimpleMLP()
model.compile(...)
model.fit(...)
Sequential model
The Sequential model is a linear stack of layers.
import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
model = Sequential()
conv1_1 = Conv2D(32, (3,3), activation='relu', input_shape=(100,100,3))
conv1_2 = Conv2D(32, (3,3), activation='relu')
pool1 = MaxPooling2D(pool_size=(2,2))
model.add(conv1_1)
model.add(conv1_2)
model.add(pool1)
layers
If a layer has a single node
- layer.input
- layer.output
- layer.input_shape
- layer.output_shape
If a layer has multiple nodes - layer.get_input_at(node_index)
- layer.get_output_at(node_index)
- layer.get_input_shape_at(node_index)
- layer.get_output_shape_at(node_index)
Visualization
from keras.utils import plot_model
plot_model(model, to_file='model.png')
Applications
from keras.applications.vgg16 import VGG16
vgg16_model = VGG16(weights='imagenet', include_top=True)
plot_model(vgg16_model, to_file='vgg16_model.png')
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5
553467904/553467096 [==============================] - 55s 0us/step