Getting Started

Rztdl is a deep learning library wrapper written on top of Tensorflow which provide higher level abstraction to building and training models.

Creating a model

How to specify model architecture for a simple model

from rztdl.dl import RZTModel
from rztdl.dl.components.layers import Input, Dense
from rztdl.dl.helpers.activations import Relu
from rztdl.dl.helpers.initializers import RandomNormal
from rztdl.dl.components.losses import MeanSquaredError
from rztdl.dl.components.optimizers import Adam

model = RZTModel(name="simple_model")

model.add(Input(name="input_layer", shape=[3]))  # Input layer for feeding data
model.add(Dense(name="dense_1", units=10, kernel_initializer=RandomNormal(mean=0.5, stddev=0.1)))  # gets input from previous component
model.add(Dense(name="dense_2", units=1, activation=Relu()))  # Gets input from dense_2
model.add(Input(name="label", shape=[1]))  # Input layer for label
# Need to specify inputs for Mean Squared Error as it has two inputs prediction and labels
model.add(MeanSquaredError(name="mse", predictions="dense_2", labels="label"))

# Adam has only one input so it will automatically be connected to MeanSquaredError which is the previous layer
model.add(Adam(name="adam", learning_rate=0.1))

View model architecture

The graph of the model can be viewed by

model.plot() #In Jupyter
#    or
model.plot(to_file="file_name.png") #In any other editor

![src/docs/images/a.png] (src/docs/images/a.png)

Training a model

def data_gen(): # data to be used for training
    # Replace random data generation to actual data
    import numpy as np
    for i in range(100):  # Replace this logic to file read / custom data read
        # Keys are input layer names
        yield {"input_layer": np.random.random([3]), "label": np.random.random([1])}

model.fit(data=data_gen,  # generator function which yield one record  at a time(dict with keys as input layer name
          optimizers=['adam'],  # List of optimizer to train on (Multiple optimizers are supported)
          learning_rate=[0.1],  # List of learning rates corresponding to each optimizer provided
          batch_size=32,        # batch size to be used for training
          epochs=3,  # No of epochs to train on
          metrics=['mse'],  # List of metrics to be logged during training and testing
          test_data=data_gen,  # data to run test/validation on (optional)
          log_frequency=1,  # Determines when to log metrics, Defaults to 1 (Log per batch)
          model_save_path="/tmp/checkpoint",  # Path to be used for saving the checkpoint
          tensorboard_summaries=True,  # default true => Indicates if tensorboard summaries have to be created          
)

Using validation data

The model can be validated on any dataset by adding the following 3 params to model.fit API

model.fit(
            .... # All other params
            ....
            valid_data=data_gen, # Data to be used for validation
            valid_frequency=1, # When to run the validation (base on interval type)
            valid_interval_type='Batch' # Either batch / epoch

Validation can be done base on epochs or batches

If interval type is batch and frequency is 2. Validation will run after every two batches

If interval type is epoch and frequency is 3. Validation will run after every three batches

Inference / Prediction

Infer using model object

Use this mode if you are training and predicting in the same session/ run

def prediction_data_gen():
    # Replace random data generation to actual data
    import numpy as np
    for i in range(100):  # Replace this logic to file read / custom data read
        # Keys are input layer name (label is nto required as it won't be needed for prediction)
        yield {"input_layer": np.random.random([3])}

output = model.predict(data=prediction_data_gen, # generator function which yield one record  at a time(dict with keys as input layer name)
                       layers=["dense_2"],  # List of layers/operators to be predicted
                       batch_size=32,  # batch size to be used for prediction
                       model_load_path="/tmp/checkpoint",  # checkpoint path from where weights have to be loaded
                       )

# model.predict  returns a iterator (lazy) which yields output

for batch_output in output:
    # batch_output will be a dictionary with keys corresponding to each layer_name specified in the `layers` parameter
    # batch_output = {"dense_2" : numpy_array of shape (batch_size, 1)} 1= output shape of dense_2 layer 
    print(i)

Infer without model object

This api lets your predict on a checkpoint without re-creating the model instance.

from rztdl.flows.infer import Inference

predictor = Inference(components=['dense_2'],  # List of layers/operators to be predicted
                      model_load_path="/tmp/checkpoint"  # Checkpoint path from where model has to be loaded
                      )

prediction1 = predictor.predict(batch_size=32, # batch size used for prediction
                                data=prediction_data_gen,  # The data
                                )

prediction2 = predictor.predict(batch_size=64, # batch size used for prediction
                                data=prediction_data_gen,  # The data
                                )