CNN1DInterface

class ketos.neural_networks.cnn.CNN1DInterface(convolutional_layers=[{'activation': 'relu', 'batch_normalization': True, 'filter_shape': 128, 'max_pool': None, 'n_filters': 8, 'padding': 'causal', 'strides': 2}, {'activation': 'relu', 'batch_normalization': True, 'filter_shape': 64, 'max_pool': {'pool_size': 8, 'strides': 8}, 'n_filters': 16, 'padding': 'causal', 'strides': 2}, {'activation': 'relu', 'batch_normalization': True, 'filter_shape': 32, 'max_pool': {'pool_size': 8, 'strides': 8}, 'n_filters': 32, 'padding': 'causal', 'strides': 2}, {'activation': 'relu', 'batch_normalization': True, 'filter_shape': 16, 'max_pool': None, 'n_filters': 64, 'padding': 'causal', 'strides': 2}, {'activation': 'relu', 'batch_normalization': True, 'filter_shape': 8, 'max_pool': None, 'n_filters': 128, 'padding': 'causal', 'strides': 2}, {'activation': 'relu', 'batch_normalization': True, 'filter_shape': 4, 'max_pool': {'pool_size': 4, 'strides': 4}, 'n_filters': 256, 'padding': 'causal', 'strides': 2}], dense_layers=[{'activation': 'relu', 'batch_normalization': True, 'dropout': 0.5, 'n_hidden': 512}, {'activation': 'relu', 'batch_normalization': True, 'dropout': 0.5, 'n_hidden': 128}], n_classes=2, optimizer=Adam ketos recipe, loss_function=CategoricalCrossentropy ketos recipe, metrics=[CategoricalAccuracy ketos recipe, Precision ketos recipe, Recall ketos recipe])[source]

Create an 1D (temporal) CNN model with the standardized Ketos interface.

Args:
convolutional_layers: list

A list of dictionaries containing the detailed specification for the convolutional layers. Each layer is specified as a dictionary with the following format:

>>> {'n_filters':96, "filter_shape":(11,11), 'strides':4, 'padding':'valid', activation':'relu', 'max_pool': {'pool_size':(3,3) , 'strides':(2,2)}, 'batch_normalization':True} 
dense_layers: list

A list of dictionaries containing the detailed specification for the fully connected layers. Each layer is specified as a dictionary with the following format:

>>> {'n_hidden':4096, 'activation':'relu', 'batch_normalization':True, 'dropout':0.5} 
n_classes:int

The number of classes the network will be used to classify. The output will be this number of values representing the scores for each class. Scores sum to 1.0.

optimizer: ketos.neural_networks.RecipeCompat object

A recipe compatible optimizer (i.e.: wrapped by the ketos.neural_networksRecipeCompat class)

loss_function: ketos.neural_networks.RecipeCompat object

A recipe compatible loss_function (i.e.: wrapped by the ketos.neural_networksRecipeCompat class)

metrics: list of ketos.neural_networks.RecipeCompat objects

A list of recipe compatible metrics (i.e.: wrapped by the ketos.neural_networksRecipeCompat class). These metrics will be computed on each batch during training.

Examples:

>>> # Most users will create a model based on a Ketos recipe 
>>> # The one below, specifies a CNN with 3 convolutional layers and 2 dense layers
>>>
>>> recipe = {'conv_set':[[64, False], [128, True], [256, True]], 
...   'dense_set': [512, ],
...   'n_classes':2,
...   'optimizer': {'name':'Adam', 'parameters': {'learning_rate':0.005}},
...   'loss_function': {'name':'FScoreLoss', 'parameters':{}},  
...   'metrics': [{'name':'CategoricalAccuracy', 'parameters':{}}]
... }
>>> # To create the CNN, simply  use the  'build_from_recipe' method:
>>> cnn = CNNInterface.build_from_recipe(recipe, recipe_compat=False) 

Methods

transform_batch(x, y[, n_classes])

Transforms a training batch into the format expected by the network.

Attributes

checkpoint_dir

early_stopping_monitor

Sets an early stopping monitor.

log_dir

test_generator

train_generator

val_generator

valid_losses

valid_metrics

valid_optimizers

classmethod transform_batch(x, y, n_classes=2)[source]

Transforms a training batch into the format expected by the network.

When this interface is subclassed to make new neural_network classes, this method can be overwritten to accomodate any transformations required. Common operations are reshaping of input arrays and parsing or one hot encoding of the labels.

Args:
x:numpy.array

The batch of inputs with shape (batch_size, width, height)

y:numpy.array

The batch of labels. Each label must be represented as an integer, ranging from zero to n_classes The array is expected to have a field named ‘label’.

n_classes:int

The number of possible classes for one hot encoding.

Returns:
X:numpy.array

The transformed batch of inputs

Y:numpy.array

The transformed batch of labels

Examples:

>>> import numpy as np
>>> # Create a batch of 10 5x5 arrays
>>> inputs = np.random.rand(10,5,5)
>>> inputs.shape
(10, 5, 5)
>>> # Create a batch of 10 labels (0 or 1)
>>> labels = np.random.choice([0,1], size=10)
>>> labels.shape
(10,)
>>> transformed_inputs, transformed_labels = NNInterface.transform_batch(inputs, labels, n_classes=2)
>>> transformed_inputs.shape
(10, 5, 5, 1)
>>> transformed_labels.shape
(10, 2)