diktya.func_api_helpers¶
-
trainable
(model, trainable)[source]¶ Sets all layers in model to trainable and restores the state afterwards.
Warning
Be aware, that the keras
Model.compile
method is lazy. You might want to callModel._make_train_function
to force a compilation.Parameters: - model – keras model
- trainable (bool) – set layer.traiable to this value
Example:
model = Model(x, y) with trainable(model, False): # layers of model are now not trainable # Do something z = model(y) [...] # now the layers of `model` are trainable again
-
sequential
(layers, ns=None, trainable=True)[source]¶ The functional flexible counter part to the keras Sequential model.
Parameters: - layers (list) – Can be a arbitrary nested list of layers.
The layers will be called sequentially. Can contain
None
’s - ns (optional str) – Namespace prefix of the layers
- trainable (optional bool) – set the layer’s trainable attribute to this value.
Returns: A function that takes a tensor as input, applies all the layers, and returns the output tensor.
Simple example:
Call a list of layers.
x = Input(shape=(32,)) y = sequential([ Dense(10), LeakyReLU(0.4), Dense(10, activation='sigmoid'), ])(x) m = Model(x, y)
Advanced example:
Use a function to construct reoccuring blocks. The
conv
functions returns a nested list of layers. This allows one to nicely combine and stack different building blocks function.def conv(n, depth=2, f=3, activation='relu'): layers = [ [ Convolution2D(n, f, f, border_mode='same'), BatchNormalization(), Activation(activation) ] for _ in range(depth) ] return layers + [MaxPooling2D()] x = Input(shape=(32,)) y = sequential([ conv(32), conv(64), conv(128), Flatten(), Dense(10, activation='sigmoid'), ])(x, ns='classifier') m = Model(x, y)
- layers (list) – Can be a arbitrary nested list of layers.
The layers will be called sequentially. Can contain
-
concat
(tensors, axis=1, **kwargs)[source]¶ Wrapper around keras merge function.
Parameters: - tensors – list of keras tensors
- axis – concat on this axis
- kwargs – passed to the merge function
Returns: The concatenated tensor
-
name_tensor
(keras_tensor, name)[source]¶ Add a layer with this
name
that does nothing.Usefull to mark a tensor.
-
save_model
(model, fname, overwrite=False, attrs={})[source]¶ Saves the weights and the config of
model
in the HDF5 filefname
. The model config is saved as:f.attrs["model"] = model.to_json().encode('utf-8')
, wheref
is the HDF5 file.
-
load_model
(fname, custom_objects={})[source]¶ Loads the model and weights from
fname
. Counterpart tosave_model()
.