or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builder-pattern.mdconfiguration-import.mdindex.mdlayer-support.mdmodel-import.mdpretrained-models.mdseparate-files-import.md

configuration-import.mddocs/

0

# Configuration Import

1

2

Import model configurations without weights, useful for creating model architectures that can be trained separately or loaded with different weights.

3

4

## Functional API Configuration Import

5

6

Import configuration for Keras Functional API models from JSON files.

7

8

```java { .api }

9

public static ComputationGraphConfiguration importKerasModelConfiguration(String modelJsonFilename)

10

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

11

12

public static ComputationGraphConfiguration importKerasModelConfiguration(String modelJsonFilename, boolean enforceTrainingConfig)

13

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

14

```

15

16

### Parameters

17

18

- `modelJsonFilename` (String): Path to JSON file containing Keras model configuration

19

- `enforceTrainingConfig` (boolean): Whether to enforce training-related configurations

20

21

### Returns

22

23

- `ComputationGraphConfiguration`: DeepLearning4J computation graph configuration

24

25

### Exceptions

26

27

- `IOException`: File I/O errors when reading the JSON file

28

- `InvalidKerasConfigurationException`: Malformed or invalid Keras model configuration

29

- `UnsupportedKerasConfigurationException`: Keras features not supported by DeepLearning4J

30

31

### Usage Examples

32

33

```java

34

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;

35

import org.deeplearning4j.nn.conf.ComputationGraphConfiguration;

36

import org.deeplearning4j.nn.graph.ComputationGraph;

37

38

// Import configuration only

39

ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("model_config.json");

40

41

// Create model from configuration (without pre-trained weights)

42

ComputationGraph model = new ComputationGraph(config);

43

model.init();

44

45

// Now you can train the model or load weights separately

46

```

47

48

## Sequential Configuration Import

49

50

Import configuration for Keras Sequential models from JSON files.

51

52

```java { .api }

53

public static MultiLayerConfiguration importKerasSequentialConfiguration(String modelJsonFilename)

54

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

55

56

public static MultiLayerConfiguration importKerasSequentialConfiguration(String modelJsonFilename, boolean enforceTrainingConfig)

57

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

58

```

59

60

### Parameters

61

62

- `modelJsonFilename` (String): Path to JSON file containing Keras Sequential model configuration

63

- `enforceTrainingConfig` (boolean): Whether to enforce training-related configurations

64

65

### Returns

66

67

- `MultiLayerConfiguration`: DeepLearning4J multi-layer network configuration

68

69

### Exceptions

70

71

- `IOException`: File I/O errors when reading the JSON file

72

- `InvalidKerasConfigurationException`: Malformed or invalid Keras model configuration

73

- `UnsupportedKerasConfigurationException`: Keras features not supported by DeepLearning4J

74

75

### Usage Examples

76

77

```java

78

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;

79

import org.deeplearning4j.nn.conf.MultiLayerConfiguration;

80

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;

81

82

// Import Sequential configuration only

83

MultiLayerConfiguration config = KerasModelImport.importKerasSequentialConfiguration("sequential_config.json");

84

85

// Create model from configuration

86

MultiLayerNetwork model = new MultiLayerNetwork(config);

87

model.init();

88

89

// Train or load weights as needed

90

```

91

92

## Generating Configuration Files

93

94

Keras model configurations can be saved to JSON using Keras/TensorFlow:

95

96

### Python Code to Export Configuration

97

98

```python

99

# In Python with Keras

100

import keras

101

102

# Load or create your model

103

model = keras.models.load_model('my_model.h5')

104

105

# Save configuration to JSON

106

model_json = model.to_json()

107

with open('model_config.json', 'w') as f:

108

f.write(model_json)

109

110

# For Sequential models

111

if hasattr(model, 'get_config'):

112

config = model.get_config()

113

with open('sequential_config.json', 'w') as f:

114

json.dump({'class_name': 'Sequential', 'config': config}, f)

115

```

116

117

## Use Cases

118

119

### Transfer Learning

120

121

```java

122

// Import architecture from pre-trained model

123

ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("pretrained_config.json");

124

125

// Create new model with same architecture

126

ComputationGraph model = new ComputationGraph(config);

127

model.init();

128

129

// Train on your specific dataset

130

// ... training code ...

131

```

132

133

### Model Architecture Exploration

134

135

```java

136

// Import and examine model configuration

137

ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("model_config.json");

138

139

// Access configuration details

140

System.out.println("Number of layers: " + config.getVertices().size());

141

System.out.println("Input types: " + Arrays.toString(config.getNetworkInputTypes()));

142

System.out.println("Output names: " + Arrays.toString(config.getNetworkOutputs()));

143

```

144

145

### Custom Weight Loading

146

147

```java

148

// Import configuration

149

ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("model_config.json");

150

151

// Create model

152

ComputationGraph model = new ComputationGraph(config);

153

model.init();

154

155

// Load custom weights (not from Keras)

156

// ... custom weight loading logic ...

157

```

158

159

## Configuration File Format

160

161

The JSON configuration file should follow Keras model serialization format:

162

163

### Functional API Model Format

164

```json

165

{

166

"class_name": "Model",

167

"config": {

168

"name": "model_name",

169

"layers": [...],

170

"input_layers": [...],

171

"output_layers": [...]

172

}

173

}

174

```

175

176

### Sequential Model Format

177

```json

178

{

179

"class_name": "Sequential",

180

"config": [

181

{

182

"class_name": "Dense",

183

"config": {...}

184

},

185

...

186

]

187

}

188

```

189

190

## Training Configuration Enforcement

191

192

When `enforceTrainingConfig` is set to `true`:

193

- Unsupported training configurations will throw exceptions

194

- All regularizers must be supported

195

- All optimizers must be supported

196

- All loss functions must be supported

197

198

When `enforceTrainingConfig` is set to `false`:

199

- Unsupported configurations generate warnings but don't stop import

200

- Allows importing models with partially supported features

201

- Useful for inference-only scenarios