NN GitHub

Model

To create a model, first you have to define a model function. Defining a model as a function allows it to be called multiple times with different parameters for training, evaluation and prediction. Once a model is created it can used for training or making predictions.

Model Function

Creating a model function involves two steps:

  1. Defining the neural network (layers, number of units, activations)
  2. Configuring the learning process (loss, optimizer, evaluation metrics)
def model(inputs):
    # Define the network architecture
    hidden = nn.Dense(units=64, activation='relu')(inputs)
    outputs = nn.Dense(units=10)(hidden)
    # Compute predictions for prediction mode
    predictions = nn.tf.argmax(outputs, axis=1)

    # Configure the learning process
    return dict(outputs=outputs,
                predictions=predictions,
                loss='softmax_cross_entropy',
                optimizer=('GradientDescent', 0.001),
                metrics=['accuracy'])

It can also use the mode argument to handle different modes - nn.TRAIN, nn.EVAL and nn.PREDICT:

def model(inputs, mode):
    ...

Advanced Usage

To have more control, you may compute loss, metrics etc. directly using labels and mode:

def model(inputs, labels, mode):
    # Define the network architecture
    hidden = nn.Dense(units=64, activation='relu')(inputs)
    outputs = nn.Dense(units=10)(hidden)
    # Compute predictions for prediction mode
    predictions = nn.tf.argmax(outputs, axis=1)

    # In prediction mode, simply return predictions without configuring learning process
    if mode == nn.PREDICT:
        return predictions

    # Configure the learning process for training and evaluation modes
    loss = nn.losses.softmax_cross_entropy(labels, outputs)
    optimizer = nn.optimizers.GradientDescent(0.001)
    accuracy = nn.metrics.accuracy(labels, predictions)
    return dict(loss=loss,
                optimizer=optimizer,
                metrics={'accuracy': accuracy})

Creating Model

Once a model function is defined, create a model using nn.Model class:

model = nn.Model(model)

Save/load model parameters, training progress etc. by specifying a model directory:

model = nn.Model(model, directory='/tmp/my_model')

If directory is not specified, a temporary folder is used.

Decorator Syntax

Alternatively, you can use the nn.model function instead of nn.Model class to create a model using decorator syntax:

@nn.model
def model(inputs):
    ...

which is equivalent to:

def model(inputs):
    ...

model = nn.Model(model)

You can also pass keyword arguments to the decorator function:

@nn.model(directory='/tmp/my_model')
def model(inputs):
    ...

which is equivalent to:

def model(inputs):
    ...

model = nn.Model(model, directory='/tmp/my_model')

Training

Train the model using training data:

model.train(x, y=None, epochs=1, batch_size=128, shuffle=True, **keywords)

Evaluate the model performance on test or validation data:

loss_and_metrics = model.evaluate(x, y=None, batch_size=128, **keywords)

Use the model to make predictions for new data:

predictions = model.predict(x, batch_size=128, **keywords)
# or call the model directly
predictions = model(x, batch_size=128, **keywords)

x and y are expected to be NumPy arrays.

Next Steps