[source]

CustomObjectScope

keras.utils.generic_utils.CustomObjectScope()

Provides a scope that changes to _GLOBAL_CUSTOM_OBJECTS cannot escape.

Code within a with statement will be able to access custom objects by name. Changes to global custom objects persist within the enclosing with statement. At end of the with statement, global custom objects are reverted to state at beginning of the with statement.

Example

Consider a custom object MyObject

with CustomObjectScope({"MyObject":MyObject}):
    layer = Dense(..., W_regularizer="MyObject")
    # save, load, etc. will recognize custom object by name

[source]

HDF5Matrix

keras.utils.io_utils.HDF5Matrix(datapath, dataset, start=0, end=None, normalizer=None)

Representation of HDF5 dataset to be used instead of a Numpy array.

Example

x_data = HDF5Matrix('input/file.hdf5', 'data')
model.predict(x_data)

Providing start and end allows use of a slice of the dataset.

Optionally, a normalizer function (or lambda) can be given. This will be called on every slice of data retrieved.

Arguments

  • datapath: string, path to a HDF5 file
  • dataset: string, name of the HDF5 dataset in the file specified in datapath
  • start: int, start of desired slice of the specified dataset
  • end: int, end of desired slice of the specified dataset
  • normalizer: function to be called on data when retrieved

Returns

An array-like HDF5 dataset.


to_categorical

to_categorical(y, num_classes=None)

Converts a class vector (integers) to binary class matrix.

E.g. for use with categorical_crossentropy.

Arguments

  • y: class vector to be converted into a matrix (integers from 0 to num_classes).
  • num_classes: total number of classes.

Returns

A binary matrix representation of the input.


normalize

normalize(x, axis=-1, order=2)

Normalizes a Numpy array.

Arguments

  • x: Numpy array to normalize.
  • axis: axis along which to normalize.
  • order: Normalization order (e.g. 2 for L2 norm).

Returns

A normalized copy of the array.


custom_object_scope

custom_object_scope()

Provides a scope that changes to _GLOBAL_CUSTOM_OBJECTS cannot escape.

Convenience wrapper for CustomObjectScope. Code within a with statement will be able to access custom objects by name. Changes to global custom objects persist within the enclosing with statement. At end of the with statement, global custom objects are reverted to state at beginning of the with statement.

Example

Consider a custom object MyObject

with custom_object_scope({"MyObject":MyObject}):
layer = Dense(..., W_regularizer="MyObject")
# save, load, etc. will recognize custom object by name

Arguments

  • *args: Variable length list of dictionaries of name, class pairs to add to custom objects.

Returns

Object of type CustomObjectScope.


get_custom_objects

get_custom_objects()

Retrieves a live reference to the global dictionary of custom objects.

Updating and clearing custom objects using custom_object_scope is preferred, but get_custom_objects can be used to directly access _GLOBAL_CUSTOM_OBJECTS.

Example

get_custom_objects().clear()
get_custom_objects()["MyObject"] = MyObject

Returns

Global dictionary of names to classes (_GLOBAL_CUSTOM_OBJECTS).


serialize_keras_object

serialize_keras_object(instance)

deserialize_keras_object

deserialize_keras_object(identifier, module_objects=None, custom_objects=None, printable_module_name='object')

get_file

get_file(fname, origin, untar=False, md5_hash=None, cache_subdir='datasets')

Downloads a file from a URL if it not already in the cache.

Passing the MD5 hash will verify the file after download as well as if it is already present in the cache.

Arguments

  • fname: name of the file
  • origin: original URL of the file
  • untar: boolean, whether the file should be decompressed
  • md5_hash: MD5 hash of the file for verification
  • cache_subdir: directory being used as the cache

Returns

Path to the downloaded file


convert_all_kernels_in_model

convert_all_kernels_in_model(model)

Converts all convolution kernels in a model from Theano to TensorFlow.

Also works from TensorFlow to Theano.

Arguments

  • model: target model for the conversion.

plot_model

plot_model(model, to_file='model.png', show_shapes=False, show_layer_names=True)