from visualdl import LogWriter, LogReader
import random
def write(log_dir):
log_writer = LogWriter(dir = log_dir, sync_cycle = 5)
with log_writer.mode("train") as logw:
# this scalar will record value in mode "train" and has tag "record1"
scalar_train_1 = logw.scalar("record1")
# this scalar will record value in mode "train" and has tag "record2"
scalar_train_2 = logw.scalar("record2")
with log_writer.mode("test") as logw:
# this scalar will record value in mode "test" and has tag "record1"
scalar_val_1 = logw.scalar("record1")
# this scalar will record value in mode "test" and has tag "record2"
scalar_val_2 = logw.scalar("record2")
for i in range(5):
# add scalar record with step from 0 to 4
scalar_train_1.add_record(i, random.random())
scalar_train_2.add_record(i, random.random())
scalar_val_1.add_record(i, random.random())
scalar_val_2.add_record(i, random.random())
def read(log_dir):
log_reader = LogReader(dir = log_dir)
# get all mode
modes = log_reader.modes()
for mode in modes:
# switch log_reader to appointed mode
with log_reader.mode(mode) as logr:
# get all tags, in this case, tag will be record1 or record2
tags = logr.tags("scalar")
for tag in tags:
# each tag consists of many records
records = logr.scalar(tag = tag).records()
for step, record in enumerate(records):
print("mode %s -- tag %s -- step %d -- val %.2f" % (mode, tag, step, record))
if __name__ == "__main__":
log_dir = "vdl_log"
write(log_dir)
read(log_dir)