Welcome to diktya’s documentation!¶
diktya.callbacks¶
-
class
SampleGAN
(sample_func, discriminator_func, z, real_data, callbacks, should_sample_func=None)[source]¶ Bases:
keras.callbacks.Callback
Keras callback that provides samples
on_epoch_end
to other callbacks.Parameters: - sample_func – is called with
z
and should return fake samples. - discriminator_func – Should return the discriminator score.
- z – Batch of random vectors
- real_data – Batch of real data
- callbacks – List of callbacks, called with the generated samples.
- should_sample_func (optional) – Gets the current epoch and returns a bool if we should sample at the given epoch.
- sample_func – is called with
-
class
VisualiseGAN
(nb_samples, output_dir=None, show=False, preprocess=None)[source]¶ Bases:
keras.callbacks.Callback
Visualise
nb_samples
fake images from the generator.Warning
Cannot be used as normal keras callback. Can only be used as callback for the SampleGAN callback.
Parameters: - nb_samples – number of samples
- output_dir (optional) – Save image to this directory. Format is
{epoch:05d}
. - (default (show) – False): Show images as matplotlib plot
- preprocess (optional) – Apply this preprocessing function to the generated images.
-
class
SaveModels
(models, output_dir=None, every_epoch=50, overwrite=True, hdf5_attrs=None)[source]¶ Bases:
keras.callbacks.Callback
-
class
DotProgressBar
[source]¶ Bases:
diktya.callbacks.OnEpochEnd
-
class
LearningRateScheduler
(optimizer, schedule)[source]¶ Bases:
keras.callbacks.Callback
Learning rate scheduler
Parameters: - optimizer (keras Optimizer) – schedule the learning rate of this optimizer
- schedule (dict) – Dictionary of epoch -> lr_value
-
class
AutomaticLearningRateScheduler
(optimizer, metric='loss', min_improvement=0.001, epoch_patience=3, factor=0.25)[source]¶ Bases:
keras.callbacks.Callback
This callback automatically reduces the learning rate of the optimizer. If the
metric
did not improve by at least themin_improvement
amount in the lastepoch_patience
epochs, the learning rate ofoptimizer
will be decreased byfactor
.Parameters:
-
class
HistoryPerBatch
(output_dir=None, extra_metrics=None)[source]¶ Bases:
keras.callbacks.Callback
Saves the metrics of every batch.
Parameters: - output_dir (optional str) – Save history and plot to this directory.
- extra_metrics (optional list) – Also montior this metrics.
-
batch_history
¶ history of every batch. Use
batch_history[metric_name][epoch_idx][batch_idx]
to index.
-
epoch_history
¶ history of every epoch. Use
epoch_history[metric_name][epoch_idx]
to index.
-
history
¶
-
metrics
¶ List of metrics to montior.
-
plot_callback
(fname=None, every_nth_epoch=1, **kwargs)[source]¶ Returns a keras callback that plots this figure
on_epoch_end
.Parameters: - fname (optional str) – filename where to save the plot. Default is
{self.output}/history.png
- every_nth_epoch – Plot frequency
- **kwargs – Passed to
self.plot(**kwargs)
- fname (optional str) – filename where to save the plot. Default is
-
plot
(metrics=None, fig=None, ax=None, skip_first_epoch=False, use_every_nth_batch=1, save_as=None, batch_window_size=128, percentile=(1, 99), end=None, kwargs=None)[source]¶ Plots the losses and variance for every epoch.
Parameters: - metrics (list) – this metric names will be plotted
- skip_first_epoch (bool) – skip the first epoch. Use full if the first batch has a high loss and brakes the scaling of the loss axis.
- fig – matplotlib figure
- ax – matplotlib axes
- save_as (str) – Save figure under this path. If
save_as
is a relative path andself.output_dir
is set, it is appended toself.output_dir
.
Returns: A tuple of fig, axes
-
class
SaveModelAndWeightsCheckpoint
(filepath, monitor='val_loss', verbose=0, save_best_only=False, mode='auto', hdf5_attrs=None)[source]¶ Bases:
keras.callbacks.Callback
Similiar to keras ModelCheckpoint, but uses
save_model()
to save the model and weights in one file.filepath can contain named formatting options, which will be filled the value of epoch and keys in logs (passed in on_epoch_end).
For example: if filepath is weights.{epoch:02d}-{val_loss:.2f}.hdf5, then multiple files will be save with the epoch number and the validation loss.
- # Arguments
filepath: string, path to save the model file. monitor: quantity to monitor. verbose: verbosity mode, 0 or 1. save_best_only: if save_best_only=True,
the latest best model according to the validation loss will not be overwritten.- mode: one of {auto, min, max}.
- If save_best_only=True, the decision to overwrite the current save file is made based on either the maximization or the minization of the monitored. For val_acc, this should be max, for val_loss this should be min, etc. In auto mode, the direction is automatically inferred from the name of the monitored quantity.
hdf5_attrs: Dict of attributes for the hdf5 file.
diktya.gan¶
-
class
GAN
(generator: keras.engine.training.Model, discriminator: keras.engine.training.Model)[source]¶ Bases:
diktya.models.AbstractModel
Generative Adversarial Networks (GAN) are a unsupervised learning framework. It consists of a generator and a discriminator network. The generator recieves a noise vector as input and produces some fake data. The discriminator is trained to distinguish between fake data from the generator and real data. The generator is optimized to fool the discriminator. Please refere to Goodwellow et. al for a detail introduction into GANs.
Parameters: - generator (Model) – model of the generator. Must have one output and one input must be named z.
- discriminator (Model) – model of the discriminator. Must have exaclty one input named data. For every sample, the output must be a scalar between 0 and 1.
z = Input(shape=(20,), name='z') data = Input(shape=(1, 32, 32), name='real') n = 64 fake = sequential([ Dense(2*16*n, activation='relu'), Reshape(2*n, 4, 4), ])(z) realness = sequential([ Convolution2D(n, 3, 3, border='same'), LeakyRelu(0.3), Flatten(), Dense(1), ]) generator = Model(z, fake) generator.compile(Adam(lr=0.0002, beta_1=0.5), 'binary_crossentropy') discriminator = Model(data, realness) discriminator.compile(Adam(lr=0.0002, beta_1=0.5), 'binary_crossentropy') gan = GAN(generator, discriminator) gan.fit_generator(...)
-
input_names
¶
-
uses_learning_phase
¶
-
train_on_batch
(inputs)[source]¶ Runs a single weight update on a single batch of data. Updates both generator and discriminator.
Parameters: - inputs (optional) –
Inputs for both the discriminator and the geneator. It can either be a numpy array, a list or dict.
- numpy array:
real
- list:
[real]
,[real, z]
- dict:
{'real': real}
,{'real': real, 'z': z}
, {'real': real, 'z': z, 'additional_input', x}
- dict:
- numpy array:
- generator_inputs (optional dict) – This inputs will only be passed to the generator.
- discriminator_inputs (optional dict) – This inputs will only be passed to the discriminator.
Returns: A list of metrics. You can get the names of the metrics with
metrics_names()
.- inputs (optional) –
-
fit_generator
(generator, nb_batches_per_epoch, nb_epoch, batch_size=128, verbose=1, train_on_batch='train_on_batch', callbacks=[])[source]¶ Fits the generator and discriminator on data generated by a Python generator. The generator is not run in parallel as in keras.
Parameters:
-
generate
(inputs=None, nb_samples=None)[source]¶ Use the generator to generate data.
Parameters: - inputs – Dictionary of name to input arrays to the generator. Can include the random noise z or some conditional varialbes.
- nb_samples – Specifies how many samples will be generated, if z is not in the inputs dictionary.
Returns: A numpy array with the generated data.
-
random_z
(batch_size=32)[source]¶ Samples z from uniform distribution between -1 and 1. The returned array is of shape
(batch_size, ) + self.z_shape[1:]
-
interpolate
(x, y, nb_steps=100)[source]¶ Interpolates linear between two points in the z-space.
Parameters: - x – point in the z-space
- y – point in the z-space
- nb_steps – interpolate that many points
Returns: The generated data from the interpolated points. The data corresponding to
x
andy
are on the first and last position of the returned array.
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()
.
diktya.blocks¶
Note
The functions in this module are espacially usefull together
with the sequential
function.
-
conv2d_block
(n, filters=3, depth=1, border='same', activation='relu', batchnorm=True, pooling=None, up=False, subsample=1)[source]¶ 2D-Convolutional block consisting of possible muliple repetitions of
Convolution2D
,BatchNormalization
, andActivation
layers and can be finished by either aMaxPooling2D
, aAveragePooling2D
or aUpSampling2D
layer.Parameters: - n – number of filters of the convolution layer
- filters – shape of the filters are
(filters, filters)
- depth – repeat the convolutional, batchnormalization, activation blocks this many times
- border – border_mode of the Convolution2D layer
- activation – name or activation or a advanced Activation layer.
- batchnorm – use batchnorm layer if true. If it is an integer it indicates the batchnorm mode.
- pooling – if given, either
max
or avg for MaxPooling2D or AveragePooling2D - up – if true, use a UpSampling2D as last layer. Cannot be true if also pooling is given.
Returns: A nested list containing the layers.
-
resnet
(n, filters=3, activation='relu')[source]¶ A ResNet block. If the number of filter maps is not equal to
n
, aconv2d_block()
withn
filter maps is added.Parameters: - n – number of filters
- filters – size of the conv filters
Returns: A function that takes a keras tensor as input and runs the resnet block
diktya.distributions¶
-
class
TruncNormal
(a, b, mean, std)[source]¶ Bases:
diktya.distributions.Distribution
Normal distribution truncated between [a;b].
-
length
¶
-
-
class
Uniform
(low, high)[source]¶ Bases:
diktya.distributions.Distribution
-
length
¶
-
-
class
DistributionCollection
(distributions)[source]¶ Bases:
diktya.distributions.Distribution
,diktya.distributions.Normalization
A collection of multiple distributions:
Parameters: distributions – A list of tuples where the tuples have the form * (name, distribution) * (name, distribution, nb_elems) * (name, distribution, nb_elems, normalization) distribution must be a subclass of Distribution
. The nb_elems specify how many elements are drawn from the distribution, if omitted it will be set to 1. The normalization specifies how it is noramlised. It can be omitted and will then be set to dist.default_normalization().Example:
dist = DistributionCollection([ ("x_rotation", Normal(0, 1)), ("y_rotation", Uniform(-np.pi, np.pi), 1, SinCosAngleNorm()), ("center", Normal(0, 2), 2), ]) # Sample 10 vectors from the collection arr = dist.sample(10) # The array is a structured numpy array. The keys are the one from # constructure distributions dictionary. arr["x_rotation"][0] # Normalizes the arr samples, according to the normalisation normed = dist.normalize(arr) # the normalization/denormalization should be invariant assert np.allclose(dist.denormalize(normed), arr)
diktya.random_search¶
-
fmin
(f, space_func, n=50, n_jobs='n_cpus', verbose=0)[source]¶ Minimizes
f
by using random search.Parameters: - f (function) – function to optimize. Gets output of space_func as input.
- space_func (function) – Returns random samples form the search space.
- n (int) – Number of samples to run. Default 50
- n_jobs (int|str) – Number of parallel jobs. Use
'n_cpus'` for same amount as cpus avialable. Default ``'n_cpus'
.
Simple Example:
def quadratic_function(): return (x - 2) ** 2 def space_function(): return np.random.uniform(-2, 4) results = fmin(quadratic_function, space_function, n=50) # sorted by score print("Min score: {}".format(results[0][0]))
diktya.layers.core¶
-
class
Subtensor
(start, stop, step=1, axis=0, **kwargs)[source]¶ Bases:
keras.engine.topology.Layer
Selects only a part of the input.
Parameters: -
get_output_shape_for
(input_shape)[source]¶ Computes the output shape of the layer given an input shape (assumes that the layer will be built to match that input shape).
- # Arguments
- input_shape: shape tuple (tuple of integers)
- or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
-
call
(x, mask=None)[source]¶ This is where the layer’s logic lives.
- # Arguments
- x: input tensor, or list/tuple of input tensors. mask: a masking tensor (or list of tensors). Used mainly in RNNs.
- # Returns:
- A tensor or list/tuple of tensors.
-
get_config
()[source]¶ Returns a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Container (one layer of abstraction above).
-
-
class
SplitAt
(axis=0, **kwargs)[source]¶ Bases:
keras.engine.topology.Layer
-
get_output_shape_for
(input_shapes)[source]¶ Computes the output shape of the layer given an input shape (assumes that the layer will be built to match that input shape).
- # Arguments
- input_shape: shape tuple (tuple of integers)
- or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
-
compute_mask
(x, masks=None)[source]¶ Computes an output masking tensor, given an input tensor (or list thereof) and an input mask (or list thereof).
- # Arguments
- input: tensor or list of tensors. input_mask: tensor or list of tensors.
- # Returns
- None or a tensor (or list of tensors,
- one per output tensor of the layer).
-
call
(xs, mask=None)[source]¶ This is where the layer’s logic lives.
- # Arguments
- x: input tensor, or list/tuple of input tensors. mask: a masking tensor (or list of tensors). Used mainly in RNNs.
- # Returns:
- A tensor or list/tuple of tensors.
-
get_config
()[source]¶ Returns a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Container (one layer of abstraction above).
-
-
class
Swap
(a, b, **kwargs)[source]¶ Bases:
keras.engine.topology.Layer
-
call
(x, mask=None)[source]¶ This is where the layer’s logic lives.
- # Arguments
- x: input tensor, or list/tuple of input tensors. mask: a masking tensor (or list of tensors). Used mainly in RNNs.
- # Returns:
- A tensor or list/tuple of tensors.
-
get_config
()[source]¶ Returns a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Container (one layer of abstraction above).
-
-
class
Switch
(**kwargs)[source]¶ Bases:
keras.engine.topology.Layer
-
get_output_shape_for
(input_shape)[source]¶ Computes the output shape of the layer given an input shape (assumes that the layer will be built to match that input shape).
- # Arguments
- input_shape: shape tuple (tuple of integers)
- or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
-
-
class
ZeroGradient
(**kwargs)[source]¶ Bases:
keras.engine.topology.Layer
Consider the gradient allways zero. Wraps the
theano.gradient.zero_grad
function.
-
class
InBounds
(low=-1, high=1, clip=True, weight=15, **kwargs)[source]¶ Bases:
keras.engine.topology.Layer
Between
low
andhigh
this layer is the identity. If the value is not in bounds a regularization loss is added to the model.Parameters: - low – lower bound
- high – upper bound
- clip – Clip output if out of bounds
- weight – The regularization loss is multiplied by this
-
build
(input_shape)[source]¶ Creates the layer weights. Must be implemented on all layers that have weights.
- # Arguments
- input_shape: Keras tensor (future input to layer)
- or list/tuple of Keras tensors to reference for weight shape computations.
-
call
(x, mask=None)[source]¶ This is where the layer’s logic lives.
- # Arguments
- x: input tensor, or list/tuple of input tensors. mask: a masking tensor (or list of tensors). Used mainly in RNNs.
- # Returns:
- A tensor or list/tuple of tensors.
-
get_config
()[source]¶ Returns a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Container (one layer of abstraction above).
-
class
BatchLoss
(axis=1, normalize=True, l1=0.0, l2=0.0, **kwargs)[source]¶ Bases:
keras.engine.topology.Layer
Regularizes the activation to have
std = 1
andmean = 0
.Parameters: -
call
(x, mask=None)[source]¶ This is where the layer’s logic lives.
- # Arguments
- x: input tensor, or list/tuple of input tensors. mask: a masking tensor (or list of tensors). Used mainly in RNNs.
- # Returns:
- A tensor or list/tuple of tensors.
-
get_config
()[source]¶ Returns a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Container (one layer of abstraction above).
-
diktya.preprocessing.image¶
diktya.plot.latexify¶
Create native looking matplotlib plots¶
Adapted from: http://bkanuka.com/articles/native-latex-plots/
diktya (Greek for networks) contains some complementary utilities for theano and keras .
- Implementation of a Generative Adversarial Network.
- Some usefull helpers for the keras functional API .
- Callbacks for learning rate scheduling and history recording.