Class KerasLayer
- java.lang.Object
-
- org.deeplearning4j.nn.modelimport.keras.KerasLayer
-
- Direct Known Subclasses:
Keras2DEmbedding,KerasActivation,KerasAlphaDropout,KerasBatchNormalization,KerasBidirectional,KerasConvolution,KerasCropping1D,KerasCropping2D,KerasCropping3D,KerasDense,KerasDropout,KerasELU,KerasEmbedding,KerasFlatten,KerasGaussianDropout,KerasGaussianNoise,KerasGlobalPooling,KerasInput,KerasLambda,KerasLeakyReLU,KerasLoss,KerasLRN,KerasLSTM,KerasMasking,KerasMerge,KerasPermute,KerasPoolHelper,KerasPooling1D,KerasPooling2D,KerasPooling3D,KerasPReLU,KerasReLU,KerasRepeatVector,KerasReshape,KerasSimpleRnn,KerasSoftmax,KerasSpaceToDepth,KerasSpatialDropout,KerasTFOpLayer,KerasThresholdedReLU,KerasUpsampling1D,KerasUpsampling2D,KerasUpsampling3D,KerasZeroPadding1D,KerasZeroPadding2D,KerasZeroPadding3D
public class KerasLayer extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKerasLayer.DimOrder
-
Field Summary
Fields Modifier and Type Field Description protected StringclassNameprotected KerasLayerConfigurationconfprotected KerasLayer.DimOrderdimOrderprotected doubledropoutprotected List<String>inboundLayerNamesprotected int[]inputShapeprotected IntegerkerasMajorVersionprotected Layerlayerprotected StringlayerNameprotected Map<String,Object>originalLayerConfigprotected List<String>outboundLayerNamesprotected GraphVertexvertexprotected doubleweightL1Regularizationprotected doubleweightL2Regularizationprotected Map<String,INDArray>weights
-
Constructor Summary
Constructors Modifier Constructor Description protectedKerasLayer()Default constructor.protectedKerasLayer(Integer kerasVersion)Constructor with Keras version only.protectedKerasLayer(Map<String,Object> layerConfig)Constructor.protectedKerasLayer(Map<String,Object> layerConfig, boolean enforceTrainingConfig)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidclearCustomLayers()Clear all custom layersstatic voidclearLambdaLayers()Clear all lambda layersvoidcopyWeightsToLayer(Layer layer)Copy Keras layer weights to DL4J Layer.StringgetClassName()Get Keras layer class name.KerasLayer.DimOrdergetDimOrder()Get Keras layer backend dimension order.List<String>getInboundLayerNames()Get list of inbound layers.InputPreProcessorgetInputPreprocessor(InputType... inputType)Gets appropriate DL4J InputPreProcessor for given InputTypes.int[]getInputShape()Get layer input shape.IntegergetKerasMajorVersion()Get Keras major version of this layer.LayergetLayer()Gets corresponding DL4J Layer, if any.StringgetLayerName()Get Keras layer name.protected longgetNInFromConfig(Map<String,? extends KerasLayer> previousLayers)Some DL4J layers need explicit specification of number of inputs, which Keras does infer.intgetNumParams()Returns number of trainable parameters in layer.InputTypegetOutputType(InputType... inputType)Get layer output type.GraphVertexgetVertex()Gets corresponding DL4J Vertex, if any.Map<String,INDArray>getWeights()booleanisInputPreProcessor()Whether this Keras layer maps to a DL4J InputPreProcessor.booleanisLayer()Whether this Keras layer maps to a DL4J Layer.booleanisValidInboundLayer()Indicates whether this layer a valid inbound layer.booleanisVertex()Whether this Keras layer maps to a DL4J Vertex.static voidregisterCustomLayer(String layerName, Class<? extends KerasLayer> configClass)Register a custom layerstatic voidregisterLambdaLayer(String lambdaLayerName, SameDiffLambdaLayer sameDiffLambdaLayer)Register a lambda layervoidsetInboundLayerNames(List<String> inboundLayerNames)Set list of inbound layers.voidsetLayer(Layer layer)voidsetWeights(Map<String,INDArray> weights)Set weights for Keras layer.booleanusesRegularization()Indicates whether layer uses regularization.
-
-
-
Field Detail
-
className
protected String className
-
layerName
protected String layerName
-
inputShape
protected int[] inputShape
-
dimOrder
protected KerasLayer.DimOrder dimOrder
-
layer
protected Layer layer
-
vertex
protected GraphVertex vertex
-
weightL1Regularization
protected double weightL1Regularization
-
weightL2Regularization
protected double weightL2Regularization
-
dropout
protected double dropout
-
kerasMajorVersion
protected Integer kerasMajorVersion
-
conf
protected KerasLayerConfiguration conf
-
-
Constructor Detail
-
KerasLayer
protected KerasLayer(Integer kerasVersion) throws UnsupportedKerasConfigurationException
Constructor with Keras version only.- Parameters:
kerasVersion- major Keras version (1 or 2)- Throws:
UnsupportedKerasConfigurationException- Unsupported Keras configuration
-
KerasLayer
protected KerasLayer() throws UnsupportedKerasConfigurationExceptionDefault constructor.- Throws:
UnsupportedKerasConfigurationException- Unsupported Keras configuration
-
KerasLayer
protected KerasLayer(Map<String,Object> layerConfig) throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException
Constructor.- Parameters:
layerConfig- dictionary containing Keras layer configuration- Throws:
InvalidKerasConfigurationExceptionUnsupportedKerasConfigurationException
-
KerasLayer
protected KerasLayer(Map<String,Object> layerConfig, boolean enforceTrainingConfig) throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException
Constructor. "enforceTrainingConfig" parameter controls whether layer is built for training. This controls behavior of certain exceptions. In training mode, passing an unsupported regularizer will generate an error. In non-training mode, it generates only a warning.- Parameters:
layerConfig- dictionary containing Keras layer configurationenforceTrainingConfig- whether layer should be built for training (controls certain exceptions)- Throws:
InvalidKerasConfigurationExceptionUnsupportedKerasConfigurationException
-
-
Method Detail
-
registerLambdaLayer
public static void registerLambdaLayer(String lambdaLayerName, SameDiffLambdaLayer sameDiffLambdaLayer)
Register a lambda layer- Parameters:
lambdaLayerName- name of the lambda layer in the serialized Keras modelsameDiffLambdaLayer- SameDiffLambdaLayer instance to map to Keras Lambda layer
-
clearLambdaLayers
public static void clearLambdaLayers()
Clear all lambda layers
-
registerCustomLayer
public static void registerCustomLayer(String layerName, Class<? extends KerasLayer> configClass)
Register a custom layer- Parameters:
layerName- name of custom layer classconfigClass- class of custom layer
-
clearCustomLayers
public static void clearCustomLayers()
Clear all custom layers
-
getKerasMajorVersion
public Integer getKerasMajorVersion()
Get Keras major version of this layer.- Returns:
- Keras version as integer
-
getClassName
public String getClassName()
Get Keras layer class name.- Returns:
- Keras layer class name
-
getLayerName
public String getLayerName()
Get Keras layer name.- Returns:
- layer name
-
getInputShape
public int[] getInputShape()
Get layer input shape.- Returns:
- input shape
-
getDimOrder
public KerasLayer.DimOrder getDimOrder()
Get Keras layer backend dimension order.- Returns:
- Keras layer (backend) dimension order
-
getInboundLayerNames
public List<String> getInboundLayerNames()
Get list of inbound layers.- Returns:
- list of inbound layer names
-
setInboundLayerNames
public void setInboundLayerNames(List<String> inboundLayerNames)
Set list of inbound layers.- Parameters:
inboundLayerNames- list of inbound layer names
-
getNumParams
public int getNumParams()
Returns number of trainable parameters in layer.- Returns:
- number of trainable parameters
-
usesRegularization
public boolean usesRegularization()
Indicates whether layer uses regularization.- Returns:
- boolean
-
setWeights
public void setWeights(Map<String,INDArray> weights) throws InvalidKerasConfigurationException
Set weights for Keras layer.- Parameters:
weights- Map of named NDArrays- Throws:
InvalidKerasConfigurationException
-
copyWeightsToLayer
public void copyWeightsToLayer(Layer layer) throws InvalidKerasConfigurationException
Copy Keras layer weights to DL4J Layer.- Parameters:
layer- DL4J layer- Throws:
InvalidKerasConfigurationException- Invalid Keras configuration
-
isLayer
public boolean isLayer()
Whether this Keras layer maps to a DL4J Layer.- Returns:
- true or false
-
getLayer
public Layer getLayer()
Gets corresponding DL4J Layer, if any.- Returns:
- DL4J Layer
- See Also:
Layer
-
setLayer
public void setLayer(Layer layer)
-
isVertex
public boolean isVertex()
Whether this Keras layer maps to a DL4J Vertex.- Returns:
- true or false
-
getVertex
public GraphVertex getVertex()
Gets corresponding DL4J Vertex, if any.- Returns:
- DL4J Vertex
- See Also:
GraphVertex
-
isInputPreProcessor
public boolean isInputPreProcessor()
Whether this Keras layer maps to a DL4J InputPreProcessor.- Returns:
- true or false
-
getNInFromConfig
protected long getNInFromConfig(Map<String,? extends KerasLayer> previousLayers) throws UnsupportedKerasConfigurationException
Some DL4J layers need explicit specification of number of inputs, which Keras does infer. This method searches through previous layers until a FeedForwardLayer is found. These layers have nOut values that subsequently correspond to the nIn value of this layer.- Parameters:
previousLayers-- Returns:
- Throws:
UnsupportedKerasConfigurationException
-
getInputPreprocessor
public InputPreProcessor getInputPreprocessor(InputType... inputType) throws InvalidKerasConfigurationException
Gets appropriate DL4J InputPreProcessor for given InputTypes.- Parameters:
inputType- Array of InputTypes- Returns:
- DL4J InputPreProcessor
- Throws:
InvalidKerasConfigurationException- Invalid Keras configuration- See Also:
InputPreProcessor
-
getOutputType
public InputType getOutputType(InputType... inputType) throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException
Get layer output type.- Parameters:
inputType- Array of InputTypes- Returns:
- output type as InputType
- Throws:
InvalidKerasConfigurationException- Invalid Keras configurationUnsupportedKerasConfigurationException
-
isValidInboundLayer
public boolean isValidInboundLayer() throws InvalidKerasConfigurationExceptionIndicates whether this layer a valid inbound layer. Currently, only (known) DL4J Layers and inputs are valid inbound layers. "Preprocessor" layers (reshaping, merging, etc.) are replaced by their own inbound layers.- Returns:
- boolean indicating whether layer is valid inbound layer
- Throws:
InvalidKerasConfigurationException- See Also:
Layer
-
-