Skip to content
Paweł Redzyński edited this page Jan 4, 2021 · 6 revisions

Showcase:

Install dvc from dvclive branch: https://github.com/iterative/dvc/tree/dvclive

Install dvclive. (pip install -e .)

Artificial example:

import dvclive
import time


dvclive.init("logs", summarize=True)


def metric(i):
    return 1 - 1/(1+i)

def loss(i):
    return 1-(metric(i))


for i in range(100):
    dvclive.log("metric", metric(i))
    dvclive.log("loss", loss(i))

    dvclive.next_step()
    time.sleep(3)

A bit more real use case:

import numpy as np
from tensorflow import keras
from tensorflow.keras import layers

import dvclive
from dvclive.keras import DvcLiveCallback

num_classes = 10
input_shape = (28, 28, 1)


def get_data():
    (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

    x_train = x_train.astype("float32") / 255
    x_test = x_test.astype("float32") / 255
    x_train = np.expand_dims(x_train, -1)
    x_test = np.expand_dims(x_test, -1)

    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    return (x_train, y_train), (x_test, y_test)

def get_model():
    model= keras.Sequential(
    [
        keras.Input(shape=input_shape),
        layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Flatten(),
        layers.Dropout(0.5),
        layers.Dense(num_classes, activation="softmax"),
    ])
    model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
    return model

if __name__ == "__main__":
    model =get_model()
    (x_train, y_train), (_, _) = get_data()

    dvclive.init("logs")
    model.fit(x_train, y_train, batch_size=128, epochs=10, validation_split=0.1, callbacks=DvcLiveCallback())

Upon running, look for logs.html in your directory, open it, and keep refreshing.

Note, that the only thing that we did in the latter example was calling dvclive.init and providing a callback for Keras.

Clone this wiki locally