Building deep learning models
The core data structure of Keras is a model, which is a way to organize layers. There are two types of model:
- Sequential: The main type of model. It is simply a linear stack of layers.
- Keras functional API: These are used for more complex architectures.
You define a sequential model as follows:
from keras.models import Sequential model = Sequential()
Once a model is defined, you can add one or more layers. The stacking operation is provided by the add()
statement:
from keras.layers import Dense, Activation
For example, add a first fully connected NN layer and the Activation
function:
model.add(Dense(output_dim=64, input_dim=100)) model.add(Activation("relu"))
Then add a second softmax
layer:
model.add(Dense(output_dim=10)) model.add(Activation("softmax"))
If the model looks fine, you must compile the model by using the model.compile
function, specifying the loss
function and the optimizer
function to be used:
model.compile(loss='categorical_crossentropy',\ ...