A model in keras is just a group of layers.
A complete example that goes through creating, training, and evaluating a keras model:
from sklearn.datasets import load_iris
from sklearn.preprocessing import LabelBinarizer
from sklearn.utils import shuffle
import keras
# Load dataset
iris = load_iris()
data = iris.data
enc = LabelBinarizer()
target = enc.fit_transform(iris.target)
X, y = shuffle(data, target, random_state=0)
# Make model
inputs = keras.Input(shape=(4,))
x = keras.layers.Dense(5, activation='relu')(inputs)
outputs = keras.layers.Dense(3, activation='softmax')(x)
model = keras.Model(inputs=inputs, outputs=outputs)
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train
model.fit(x=X, y=y, batch_size=8, epochs=150, validation_split=0.3)
# Evaluate
loss, accuracy = model.evaluate(X, y)
# Predict
predictions = model.predict(X) # softmax gives us a probability of each category
The Model
class requires two things, the inputs
to a model and the outputs
to a model. There is an optimal name
parameter
There are two ways to instantiate a Model
class:
Model
classFunction API method:
import tensorflow as tf
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
Saving a model whole is super easy.
model.save('my_model')
This will create a directory called my_model
with assets
, saved_model.pb
, and variables
as contents. This only works if using tf.keras
instead of native keras. See below if using native keras.
To load it again:
model = tf.keras.models.load_model('my_model')
Saving the model as a single HDF5 is an option, however some items are not saved, such as custom layers and external losses and metrics. These can be quite annoying to add back later if you get a model from someone else.
H5:
model.save('model.h5')
model = tf.keras.models.load_model('model.h5')
model.get_weights()
model.set_weights(weights)
model.save_weights('file_path.h5')
model.load_weights('file_path.h5')
model.to_json()
model = tf.keras.models.model_from_json(config)
new_model = tf.keras.models.clone_model(model)
The Sequential class allows you to add layers sequentially to a model.
model = tf.keras.Sequential()
model.add(tf.keras.Input(shape=(16,))) # Add input layer that accepts a feature vector of length 16
model.add(tf.keras.layers.Dense(6)) # Adds a layer containing 6 neurons
Model.summary()
can be used to summarize your model, but outputting the layers
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 3)] 0
_________________________________________________________________
dense (Dense) (None, 4) 16
_________________________________________________________________
dense_1 (Dense) (None, 5) 25
=================================================================
Total params: 41
Trainable params: 41
Non-trainable params: 0
_________________________________________________________________
Keras training APIs involve compiling, fitting, evaluating, and predicting using a model.
Prepares the model for training (does a lot of hidden stuff).
Model.compile(
optimizer="rmsprop",
loss=None,
metrics=None,
loss_weights=None,
weighted_metrics=None,
run_eagerly=None,
steps_per_execution=None,
**kwargs
)
['accuracy']
is the most common metric[10, 1]
would weight the first loss function 10 times heavier than the second loss function.Used to train a model.
Model.fit(
x=None,
y=None,
batch_size=None,
epochs=1,
verbose=1,
callbacks=None,
validation_split=0.0,
validation_data=None,
shuffle=True,
class_weight=None,
sample_weight=None,
initial_epoch=0,
steps_per_epoch=None,
validation_steps=None,
validation_batch_size=None,
validation_freq=1,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
)
(x_val, y_val)
format. Do not use with validation_split
.Returns a History
object that can be used for plotting.
Like fit, but without the training. Used to find the loss and metric values for the model.
Model.evaluate(
x=None,
y=None,
batch_size=None,
verbose=1,
sample_weight=None,
steps=None,
callbacks=None,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
return_dict=False,
)
Used to predict data. A batch is expected.
Model.predict(
x,
batch_size=None,
verbose=0,
steps=None,
callbacks=None,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
)
Numpy array of predictions is returned