in keras, model must be compiled before fit()predict() and evaluate()
compile() is to tell tensorflow how to train the model, the most important thing is to specify the loss function(which deviation we want to minimize), optimizer(how to update weights) and metrics(which point we should pay attention to).
model.fit( x = , # input train-dataset y = , # output train_dataset batch_size = , # amount of input data in a single batch epochs = , # traveral all train dataset in a epoch verbose = , # 0 for not output anythings; 1 for progress bar model; 2 for each epoch a line model. callbacks = , # some functions that auto invoked while train, such as monitor, save, modify train process.
)
help(tf.keras.Model.fit) # for more infos
train model on a batch
1 2 3 4 5 6 7 8 9
loss_and_metrics = model.train_on_batch(x,y)
''' x: input data in a single batch y: label/output sample_weight:(optional) return: a list contains loss and other metircs '''
create checkpoint
we can create checkpoint mannually:
1 2 3 4 5 6
model = tf.keras.Sequential() optimizer = tf.keras.optimizer.Adam()
checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer) checkpoint.save('path for save checkpoint')
or we can use callbacks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
model = tf.keras.Sequential([ #layers info. ]) model.compile(optimizer='adam', loss='mse')
checkpoint_cb = tf.keras.callbacks.ModelCheckpoint( filepath='model_checkpoints/ckpt-{epoch}.h5', save_weights_only=True, # only save weight save_best_only=True, # only save the best weight on train dataset(it means only update when a better result's emergence) monitor='val_loss', # use val_loss as the metrics. mode='min'# min is better. )
model.fit(x, y, epochs = , validation_data=(x, y), callbacks = [checkpoint_cb])
resume from a checkpoint
1 2 3
checkpoint.restore(tf.train.latest_checkpoint('path to checkpoint'))
model.load_weights('model_checkpoints/ckpt-5.h5')
evaluate a model
1 2 3 4 5 6 7 8 9 10 11
model.evaluate( x = , y = , batch_size = , steps = , # it will run for steps batchs while evaluate.
)
help(tf.keras.Model.evaluate) # for more infos.
fit and evaluate has _generator model, generator can return a batch of data in each yield, it may be use when dataset is too large.
predict from a model
1 2 3 4 5 6 7 8 9 10 11
model.predict( x = , y = , batch_size = , steps = ,
# keras save model. model.save("my_model") model.save("my_model.h5") model = tf.keras.models.load_model("my_model") model = tf.keras.models.load_model("my_model.h5")
# keras save weight. model.save_weights("my_model_weights.ckpt") model.load_weights("my_model_weights.ckpt")
# without keras tf.save_model.save(model, "my_model") loaded_model = tf.saved_model.load("my_model")
### convolutional layers. # Conv 1D is usually used to process audio data, Conv 2D for graphic data, Conv 3D for video that have one frame in each a timescale. layers.Conv1D( filters, # amount of cores. kernel_size, strides, padding = , # 'same' or 'valid' activation =, use_bias = True, kernel_initializer = 'globrot_uniform', # the apporch of initialize weights. ......... ) layers.Conv2D() layers.Conv3D()
# transpose convolutional layer, Deconvolution pr up-convolution, it can recover low resolution pics to high resolution pics. keras.layers.Conv1DTranspose()
keras.layers.ZeroPadding1D() # zero padding. keras.layers.Cropping1D() keras.layers.UpSampling1D() # up sample.
we can use callbacks to see what happen inside the model.
keras.callbacks.ModelCheckpoint(): save keras model or weights with some frequency.
keras.callbacks.EarlyStopping(): when xxx happen, stop train.
data should be pre-process
pre-process data type
description
image
keras.preprocessing.iamge.load_img() # load image as arrays keras.preprocessing.image.img_to_array() keras.preprocessing.image.array_to_img() keras.preprocessing.image.ImageGenerator # generate tensor image data in real time