Import External Code

To include any existing code in your Tensorleap Dataset Script you can create a library and import all your existing classes and functions to your script.

Here is an example of how to import a Custom Layer from an external file.

Custom Layer Import from File

  1. First, create a file that includes the custom layers that are included in your model. Here we show an example of a custom dense layer.
import tensorflow as tf

class CustomDense(tf.keras.layers.Layer):
    def __init__(self, n, **args):
        super(CustomDense, self).__init__()
        self.n = n
        self.dense = tf.keras.layers.Dense(self.n)
    def call(self, inputs):
        return self.dense(inputs) 
    def get_config(self):
        config = super().get_config()
            "n": self.n,
        return config

2. In your Tensorleap Data Folder, create a new folder that will include the file you've created and an empty file. For example:

├── ....
|   ├── custom_layers/
|           ├──
|           └──

3. Now, use one of two possible methods to import the classes Into the dataset script.

Append system path

Tensorleap dataset script
# custom function imports
import sys
from custom_dense import CustomDense
leap_binder.set_custom_layer(CustomDense, 'CustomDense')


Import using importlib

Tensorleap dataset script
# custom function imports
import importlib.util
import sys
from inspect import getmembers, isclass
# adding custom layers from a file using imports
spec = importlib.util.spec_from_file_location('custom_layers.custom_dense',
custom_layers_module = importlib.util.module_from_spec(spec)

custom_layers_titles = getmembers(custom_layers_module, isclass)
for i in range(len(custom_layers_titles)):
    class_name = custom_layers_titles[i][0]
    function_class = getattr(custom_layers_module, class_name)
    leap_binder.set_custom_layer(function_class, class_name)

Parsing the dataset will now import the custom layers from the external files and add it to your Tensorleap environment.

Last updated