CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-deeplearning4j--deeplearning4j-modelimport

Keras model import functionality for DeepLearning4J

Pending
Overview
Eval results
Files

layer-support.mddocs/

Layer Support

Comprehensive mapping of Keras layer types to DeepLearning4J layers. The library supports most common neural network layer types and provides automatic translation of parameters and configurations.

Core Layer Types

Dense (Fully Connected) Layers

Maps Keras Dense layers to DeepLearning4J DenseLayer.

// Keras Dense layer configuration supported:
// - units (output_dim): number of output units
// - activation: activation function
// - use_bias: whether to use bias
// - kernel_initializer (init): weight initialization
// - bias_initializer: bias initialization  
// - kernel_regularizer (W_regularizer): weight regularization
// - bias_regularizer (b_regularizer): bias regularization
// - activity_regularizer: output regularization
// - kernel_constraint: weight constraints
// - bias_constraint: bias constraints

Supported Parameters:

  • Output dimensions
  • Activation functions (relu, sigmoid, tanh, softmax, linear, etc.)
  • Weight and bias initialization
  • L1/L2 regularization
  • Dropout (when combined with Dropout layer)

Convolutional Layers

Maps Keras Convolution1D and Convolution2D layers to DeepLearning4J ConvolutionLayer.

// Keras Convolution layer configuration supported:
// - filters (nb_filter): number of convolution filters
// - kernel_size (nb_row, nb_col): convolution kernel size
// - strides (subsample): stride values
// - padding (border_mode): padding type ('same', 'valid')
// - activation: activation function
// - use_bias: whether to use bias
// - kernel_initializer: weight initialization
// - bias_initializer: bias initialization
// - kernel_regularizer: weight regularization
// - bias_regularizer: bias regularization

Supported Configurations:

  • 1D and 2D convolutions
  • Stride and padding settings
  • Multiple filter sizes
  • Same and valid padding modes
  • All standard activation functions

Pooling Layers

Maps Keras pooling layers to DeepLearning4J pooling layers.

// Supported pooling types:
// - MaxPooling1D -> SubsamplingLayer with PoolingType.MAX
// - MaxPooling2D -> SubsamplingLayer with PoolingType.MAX  
// - AveragePooling1D -> SubsamplingLayer with PoolingType.AVG
// - AveragePooling2D -> SubsamplingLayer with PoolingType.AVG
// - GlobalMaxPooling1D -> GlobalPoolingLayer with PoolingType.MAX
// - GlobalMaxPooling2D -> GlobalPoolingLayer with PoolingType.MAX
// - GlobalAveragePooling1D -> GlobalPoolingLayer with PoolingType.AVG
// - GlobalAveragePooling2D -> GlobalPoolingLayer with PoolingType.AVG

Configuration Options:

  • Pool size and stride settings
  • Padding configurations
  • Global pooling support

Recurrent Layers

Maps Keras LSTM layers to DeepLearning4J LSTM layers.

// Keras LSTM layer configuration supported:
// - units: number of LSTM units
// - activation: activation function for gates
// - recurrent_activation: recurrent activation function
// - use_bias: whether to use bias
// - kernel_initializer: input weight initialization
// - recurrent_initializer: recurrent weight initialization
// - bias_initializer: bias initialization
// - dropout: input dropout rate
// - recurrent_dropout: recurrent dropout rate
// - return_sequences: whether to return full sequence
// - return_state: whether to return cell state
// - go_backwards: process sequence backwards
// - stateful: maintain state between batches
// - unroll: unroll the recurrent computation

Features:

  • Bidirectional LSTM support
  • Sequence-to-sequence and sequence-to-one configurations
  • Dropout variants
  • State management

Utility Layers

Activation Layers

Maps Keras Activation layers to DeepLearning4J ActivationLayer.

// Supported activation functions:
// - relu -> ReLU
// - sigmoid -> Sigmoid  
// - tanh -> Tanh
// - softmax -> Softmax
// - linear -> Identity
// - softplus -> Softplus
// - softsign -> Softsign
// - hard_sigmoid -> HardSigmoid
// - elu -> ELU
// - selu -> SELU
// - swish -> Swish

Dropout Layers

Maps Keras Dropout layers to DeepLearning4J DropoutLayer.

// Keras Dropout configuration:
// - rate: dropout probability (0.0 to 1.0)
// - noise_shape: shape for dropout mask
// - seed: random seed for reproducibility

Flatten Layers

Maps Keras Flatten layers to DeepLearning4J preprocessors.

// Flattens multi-dimensional input to 1D
// Automatically handles different input shapes
// Maps to appropriate DL4J InputPreProcessor

Embedding Layers

Maps Keras Embedding layers to DeepLearning4J EmbeddingLayer.

// Keras Embedding configuration:
// - input_dim: vocabulary size
// - output_dim: embedding dimension
// - embeddings_initializer: weight initialization
// - embeddings_regularizer: weight regularization
// - embeddings_constraint: weight constraints
// - mask_zero: mask zero values
// - input_length: input sequence length

Normalization Layers

Batch Normalization

Maps Keras BatchNormalization layers to DeepLearning4J BatchNormalization.

// Keras BatchNormalization configuration:
// - axis: normalization axis
// - momentum: momentum for moving averages
// - epsilon: small constant for numerical stability
// - center: whether to use beta parameter
// - scale: whether to use gamma parameter
// - beta_initializer: beta initialization
// - gamma_initializer: gamma initialization
// - moving_mean_initializer: moving mean initialization
// - moving_variance_initializer: moving variance initialization
// - beta_regularizer: beta regularization
// - gamma_regularizer: gamma regularization
// - beta_constraint: beta constraints
// - gamma_constraint: gamma constraints

Custom and Specialized Layers

Local Response Normalization

Custom implementation for Local Response Normalization (LRN).

// KerasLRN class provides:
// - alpha: normalization parameter
// - beta: normalization parameter  
// - depth_radius: normalization radius
// - bias: bias parameter

Zero Padding

Maps Keras ZeroPadding1D and ZeroPadding2D layers to appropriate preprocessors.

// Zero padding configuration:
// - padding: padding values for each dimension
// - Supports symmetric and asymmetric padding

Merge Layers

Maps Keras Merge layers to DeepLearning4J merge vertices.

// Supported merge modes:
// - add -> ElementWiseVertex with Add operation
// - multiply -> ElementWiseVertex with Product operation  
// - average -> ElementWiseVertex with Average operation
// - maximum -> ElementWiseVertex with Max operation
// - concatenate -> MergeVertex
// - dot -> DotProductVertex

Layer Mapping Process

Automatic Layer Detection

The library automatically detects Keras layer types and maps them to appropriate DeepLearning4J layers:

// KerasLayer factory method
public static KerasLayer getKerasLayerFromConfig(
    Map<String, Object> layerConfig, 
    boolean enforceTrainingConfig
) throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

Configuration Translation

Each layer type has specific configuration mapping:

  1. Parameter Names: Keras parameter names are translated to DL4J equivalents
  2. Data Types: Keras data types are converted to DL4J types
  3. Shapes: Input/output shapes are properly configured
  4. Activations: Activation functions are mapped between frameworks

Weight Transfer

Weights are automatically transferred with proper shape handling:

// Weight copying process:
// 1. Extract weights from HDF5 format
// 2. Handle parameter naming conventions (TensorFlow vs Theano backends)
// 3. Reshape weights to match DL4J expectations
// 4. Apply to corresponding DL4J layers

Supported Layer Types Reference

Keras LayerDL4J MappingSupport Level
DenseDenseLayerFull
Convolution1DConvolutionLayerFull
Convolution2DConvolutionLayerFull
MaxPooling1DSubsamplingLayerFull
MaxPooling2DSubsamplingLayerFull
AveragePooling1DSubsamplingLayerFull
AveragePooling2DSubsamplingLayerFull
GlobalMaxPooling1DGlobalPoolingLayerFull
GlobalMaxPooling2DGlobalPoolingLayerFull
GlobalAveragePooling1DGlobalPoolingLayerFull
GlobalAveragePooling2DGlobalPoolingLayerFull
LSTMLSTMFull
DropoutDropoutLayerFull
ActivationActivationLayerFull
FlattenPreprocessorFull
EmbeddingEmbeddingLayerFull
BatchNormalizationBatchNormalizationFull
MergeMergeVertex/ElementWiseVertexFull
ZeroPadding1DPreprocessorFull
ZeroPadding2DPreprocessorFull
InputInputTypeFull

Unsupported Features

Layer Types Not Supported

  • Lambda layers with custom functions
  • Custom layers without DL4J equivalents
  • Some specialized layers from newer Keras versions

Configuration Limitations

  • Some advanced regularization techniques
  • Certain constraint types
  • Complex custom initializers

Handling Unsupported Features

try {
    ComputationGraph model = KerasModelImport.importKerasModelAndWeights("model.h5", true);
} catch (UnsupportedKerasConfigurationException e) {
    System.out.println("Unsupported feature: " + e.getMessage());
    
    // Try with relaxed enforcement
    ComputationGraph model = KerasModelImport.importKerasModelAndWeights("model.h5", false);
    System.out.println("Model imported with warnings");
}

Custom Layer Extensions

For unsupported layer types, you can extend the library:

// Example custom layer implementation
public class MyCustomKerasLayer extends KerasLayer {
    
    public MyCustomKerasLayer(Map<String, Object> layerConfig, boolean enforceTrainingConfig) 
        throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
        super(layerConfig, enforceTrainingConfig);
        // Custom implementation
    }
    
    @Override
    public Layer getLayer() throws UnsupportedKerasConfigurationException {
        // Return appropriate DL4J layer
    }
    
    @Override
    public InputType getOutputType(InputType... inputTypes) 
        throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
        // Return output type
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-deeplearning4j--deeplearning4j-modelimport

docs

builder-pattern.md

configuration-import.md

index.md

layer-support.md

model-import.md

pretrained-models.md

separate-files-import.md

tile.json