K-Veritas
Tutorial · TensorFlow / Keras

Keras: IMDB text

IMDB sentiment with a Keras embedding model. Shows the same callback pattern on a text task loaded from keras.datasets.

Install

The framework, plus the K-Veritas CLI (see the Overview for install).

pip install tensorflow

train.py

The complete script. The KVERITAS_ lines are the only additions to an ordinary training script; everything else is standard TensorFlow / Keras.

import numpy as np
import tensorflow as tf

SEED = 42
tf.keras.utils.set_random_seed(SEED)
print(f"KVERITAS_INPUT src=seed:{SEED}", flush=True)

VOCAB = 20000
MAXLEN = 200

print("KVERITAS_PHASE name=data", flush=True)
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data(num_words=VOCAB)
x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=MAXLEN)
x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=MAXLEN)

model = tf.keras.Sequential([
    tf.keras.layers.Input((MAXLEN,)),
    tf.keras.layers.Embedding(VOCAB, 64),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(64, activation="relu"),
    tf.keras.layers.Dense(1, activation="sigmoid"),
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
print(f"KVERITAS_MODEL params={model.count_params()} arch=embedding_mlp precision=fp32", flush=True)
print(f"KVERITAS_WORKLOAD dataset_size={len(x_train)} epochs=4 batch_size=256", flush=True)


class KVeritas(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        for name, value in (logs or {}).items():
            print(f"KVERITAS_METRIC name={name.replace('/', '_')} value={float(value):.4f} step={epoch}", flush=True)


print("KVERITAS_PHASE name=train", flush=True)
model.fit(x_train, y_train, validation_split=0.1, epochs=4, batch_size=256,
          verbose=2, callbacks=[KVeritas()])

print("KVERITAS_PHASE name=evaluate", flush=True)
loss, acc = model.evaluate(x_test, y_test, verbose=0)
print(f"KVERITAS_METRIC name=test_accuracy value={acc:.4f}", flush=True)
print(f"KVERITAS_CLAIM metric=test_accuracy value={acc:.4f}", flush=True)
print(f"final test_accuracy={acc:.4f}", flush=True)

Run and seal

Wrap the script, seal a signed report, and verify it offline.

kveritas init
kveritas run -- python train.py
kveritas seal --output report.pdf
kveritas verify report.pdf

What it produced here

4 epochs reached about 87.6% test accuracy.

All tutorials · The directives explained