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 call Model._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
get_layer(keras_tensor)[source]

Returns the corresponding layer to a keras tensor.

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)
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

rename_layer(keras_tensor, name)[source]

Renames the layer of the keras_tensor

name_tensor(keras_tensor, name)[source]

Add a layer with this name that does nothing.

Usefull to mark a tensor.

keras_copy(obj)[source]

Copies a keras object by using the get_config method.

predict_wrapper(func, names)[source]
save_model(model, fname, overwrite=False, attrs={})[source]

Saves the weights and the config of model in the HDF5 file fname. The model config is saved as: f.attrs["model"] = model.to_json().encode('utf-8'), where f is the HDF5 file.

load_model(fname, custom_objects={})[source]

Loads the model and weights from fname. Counterpart to save_model().

get_hdf5_attr(fname, attr_name, default=None)[source]

Returns the toplevel attribute attr_name of the hdf5 file fname. If default is not None and the attribute is not present, then default is returned.