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 class
KerasLayer.DimOrder
-
Field Summary
Fields Modifier and Type Field Description protected String
className
protected KerasLayerConfiguration
conf
protected KerasLayer.DimOrder
dimOrder
protected double
dropout
protected List<String>
inboundLayerNames
protected int[]
inputShape
protected Integer
kerasMajorVersion
protected Layer
layer
protected String
layerName
protected Map<String,Object>
originalLayerConfig
protected List<String>
outboundLayerNames
protected GraphVertex
vertex
protected double
weightL1Regularization
protected double
weightL2Regularization
protected Map<String,INDArray>
weights
-
Constructor Summary
Constructors Modifier Constructor Description protected
KerasLayer()
Default constructor.protected
KerasLayer(Integer kerasVersion)
Constructor with Keras version only.protected
KerasLayer(Map<String,Object> layerConfig)
Constructor.protected
KerasLayer(Map<String,Object> layerConfig, boolean enforceTrainingConfig)
Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
clearCustomLayers()
Clear all custom layersstatic void
clearLambdaLayers()
Clear all lambda layersvoid
copyWeightsToLayer(Layer layer)
Copy Keras layer weights to DL4J Layer.String
getClassName()
Get Keras layer class name.KerasLayer.DimOrder
getDimOrder()
Get Keras layer backend dimension order.List<String>
getInboundLayerNames()
Get list of inbound layers.InputPreProcessor
getInputPreprocessor(InputType... inputType)
Gets appropriate DL4J InputPreProcessor for given InputTypes.int[]
getInputShape()
Get layer input shape.Integer
getKerasMajorVersion()
Get Keras major version of this layer.Layer
getLayer()
Gets corresponding DL4J Layer, if any.String
getLayerName()
Get Keras layer name.protected long
getNInFromConfig(Map<String,? extends KerasLayer> previousLayers)
Some DL4J layers need explicit specification of number of inputs, which Keras does infer.int
getNumParams()
Returns number of trainable parameters in layer.InputType
getOutputType(InputType... inputType)
Get layer output type.GraphVertex
getVertex()
Gets corresponding DL4J Vertex, if any.Map<String,INDArray>
getWeights()
boolean
isInputPreProcessor()
Whether this Keras layer maps to a DL4J InputPreProcessor.boolean
isLayer()
Whether this Keras layer maps to a DL4J Layer.boolean
isValidInboundLayer()
Indicates whether this layer a valid inbound layer.boolean
isVertex()
Whether this Keras layer maps to a DL4J Vertex.static void
registerCustomLayer(String layerName, Class<? extends KerasLayer> configClass)
Register a custom layerstatic void
registerLambdaLayer(String lambdaLayerName, SameDiffLambdaLayer sameDiffLambdaLayer)
Register a lambda layervoid
setInboundLayerNames(List<String> inboundLayerNames)
Set list of inbound layers.void
setLayer(Layer layer)
void
setWeights(Map<String,INDArray> weights)
Set weights for Keras layer.boolean
usesRegularization()
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 UnsupportedKerasConfigurationException
Default 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:
InvalidKerasConfigurationException
UnsupportedKerasConfigurationException
-
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:
InvalidKerasConfigurationException
UnsupportedKerasConfigurationException
-
-
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 InvalidKerasConfigurationException
Indicates 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
-
-