Model Integration
In this section, we will set up our classification model by either importing or building it.
Project Setup
First step is to Create a Project with the nameIMDB, and the description Review Classifier.
The project name, IMDB, is set with the leap init command, as discussed in Dataset Integration above.
The project page contains the Network and Dashboard views. To toggle them, click the  buttons at the top.
 buttons at the top.
Dataset Block Setup
We'll start by pointing the model's dataset block to our imdb dataset. This will update the Dataset Block with the relevant input.
In the Network view, click the Dataset Block to display the Dataset Details panel on the right, then click Connect Dataset and select imdb dataset from the list. 
A more detailed explanation about this step can be found at Dataset Block.
Set Model Layers
Importing a Model from Tensorflow or PyTorch
Tensorleap can import a model saved by Tensorflow (PB/H5/JSON_TF2) and PyTorch (ONNX). More information can be found at Import Model.
In this sample code, a simple dense model is created using Tensorflow and then saved as a file named imdb-dense.h5.
import tensorflow as tf
MAX_FEATURES = 10000
SEQUENCE_LENGTH = 250
vectorized_inputs = tf.keras.Input(shape=250, dtype="int64")
x = tf.keras.layers.Embedding(MAX_FEATURES + 1, SEQUENCE_LENGTH)(vectorized_inputs)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(28, activation='relu')(x)
x = tf.keras.layers.GlobalAveragePooling1D()(x)
x = tf.keras.layers.Dropout(0.2)(x)
output = tf.keras.layers.Dense(2, activation='softmax')(x)
model = tf.keras.Model(inputs=vectorized_inputs, outputs=output)
model.save('imdb-dense.h5')For your convenience, you can also download this file here:
Import Model
- In the Network view, click the   - Import Modelbutton to open the- Import Modelpanel.
- Set the Revision Name to - imdb-denseand the Model Name to- pre-trained.
- For File Type, use - H5_TF2and select- imdb-dense.h5in the Upload File field.
- Click the Import button. 
- Once completed, the imported model, - imdb-dense,is added to the Versions view. Position your cursor over that version, click to Open Commit. to Open Commit.
- Back in the Network view, set the Dataset Block to point to the - imdbDataset Instance, and connect it to the first layer.

Building our Classification Model using UI
In this section, we will add layers, update their properties, and connect them together to form the model.
The model that we will build is based on a small dense neural network described below:
Embedding
input_dim=10001, output_dim=250
Dropout
rate=0.2
Dense
units=28, activation="relu"'
GlobalAveragePooling1D
Dropout
rate=0.2
Dense
units=2, activation="softmax"
Adding Layers and Their Properties
In the Network view, follow these steps for each layer:
- Right-click and add the corresponding layer. More info at Add Layers. 
- Update the corresponding properties in the Layer Properties view on the right. More info at Layer Properties. 
- Connect the Dataset to the first layer, and then connect all the layers in order. More info at Connections. 
All steps are illustrated below.
Build Model
Copy the imdb_dense.py file to the imdb folder we created in Dataset Integration. This file defines the dense model, and can be found here:
Tensorleap Model Integration Script
Edit the .tensorleap/model.py file and point it to the model defined in imdb_dense.py:
from pathlib import Path
from imdb_dense import build_model
def leap_save_model(target_file_path: Path):
    # Load your model
    model = build_model()
    # Save it to the path supplied as an arugment (has a .h5 suffix)
    model.save(target_file_path)
To validate the correctness of our code, run the following command:
leap check --allIf done correctly, there should be no errors and we can move forward and push the model using the following command:
leap push --model --description=imdb-dense --model-name=pre-trainedOpening the Model in the UI
Once completed, open the UI, where you should see the imported model, imdb_dense,in the Versions view. 
- Position your cursor over the view and click  on the right to Open Commit. on the right to Open Commit.
- Back on the Network view, set the Dataset Block to point to the - imdbDataset Instance, and connect it to the first layer.
Once all layers have been connected to each other, the model should look like this:

Add Loss and Optimizer
In this section, we will set the Categorical Crossentropy loss function, and connect it to both the dataset's ground truth and the last layer in our model. We'll then add an Adam Optimizer block and connect it to the loss block. For more information at Loss and Optimizer.
After completing this section, our model will be ready for training.
Within the Network view:
- Right-click and add the following: - Loss -> CategoricalCrossentropy 
- GroundTruth, and set it to - Ground Truth - classes
- Optimizer -> Adam 
 
- Connect the last Dense layer and the GroundTruth to the CategoricalCrossentropy block. Additionally, connect the Loss block to the Adam optimizer. 
All steps are illustrated below.

Add Visualizers
Visualizers defines how to visualize tensors within the model graph. For more info, see Visualizers.
Dataset Input Visualizer
There must be at least one visualizer connected to the model's input for analysis. To add the Visualizer for the input, follow the steps below.
Within the Network view:
- Right-click and choose Visualizer to add it. 
- Click the Visualizer node to open up the Visualizer Details on the right. 
- Choose - text_from_tokenfrom the- Selected Visulizerlist.
- Connect the Dataset node output to the input of the Visualizer node. 
Prediction and Ground Truth Visualizers
Additional visualizers will be connected to the prediction and ground truth.
To visualize the model's prediction output, follow the steps below:
Within the Network view:
- Right-click and choose Visualizer to add it. 
- Click the Visualizer node to open up the Visualizer Details on the right. 
- Choose - HorizontalBarfrom the- Selected Visualizerlist.
- Connect the last Dense layer output to the input of the Visualizer node. 
- Repeat the steps, and connect the second visualizer to the GroundTruth node's output. 
Save Network Version
Great! Your first version is ready to be saved.

Click the  button and set the
 button and set the Revision Name to dense-nn (Dense Neural Network). This adds the new version to the Versions view. For more information, see Save a Version.

Training
Tensorleap can import trained and untrained models. In our case, the model was created from scratch and needs to be trained.
To train the model, click  from the top bar. Let's set Batch Size to
 from the top bar. Let's set Batch Size to 32 and the Number of Epochs to 5 and click  . For more information, see Evaluate/Train Model.
. For more information, see Evaluate/Train Model.

Metrics
Once training begins, you can start tracking metrics in real-time.
Add a Dashboard and Dashlets
To add a new Metrics Dashboard, click  and fill in the dashboard name -
 and fill in the dashboard name - imdb.
Make sure that the model is selected from the Versions view, and add a new Dashlet.
Click  for a Line Dashlet, set the name to
 for a Line Dashlet, set the name to Loss, and turn on Split series by subset to separate training and validation metrics.

Next, add another Line Dashlet for the accuracy. Follow all the previous steps to add it and set the Dashlet Name to Accuracy and set the Y-Axis to metrics.Accuracy. Do not forget turn on  Split series by subset.
Once training begins, you can start tracking metrics in real-time.
From the Dashboard view on the right, select the tl_default_metrics from the list at the top. This will open up the Metrics dashboard.
As training progresses, you should see loss values declining and accuracy values increasing. When training is completed, the model achieved an accuracy of 89% at the 3rd epoch.


89% AccuracyUp Next - Model Perception Analysis
Tensorleap provides a host of innovative tools for model analysis and debugging. It tracks how each sample flows through each layer, and how each learned feature flows through the model. It also stores metrics in a big dataset to analyze the model's response to data.
We've discussed integrating and training our model. It's now time to analyze it. The next part of this tutorial will demonstrate various analyses of the model and data.
Once you are ready, proceed to Model Perception Analysis.
Last updated
Was this helpful?