简单粗暴的tensorflow-TensorBoard可视化
# tensorboard可视化参数 summary_writer = tf.summary.create_file_writer('./tensorboard') #存放 TensorBoard 的记录文件 # 开始模型训练 for batch_index in range(num_batches): # ...(训练代码,当前batch的损失值放入变量loss中) with summary_writer.as_default(): # 希望使用的记录器 tf.summary.scalar("loss", loss, step=batch_index) tf.summary.scalar("MyScalar", my_scalar, step=batch_index) # 还可以添加其他自定义的变量 # 启动tensorboard tensorboard --logdir=./tensorboard # 访问客户端 http://name-of-your-computer:6006 # 开启trace,查看Graph 和 Profile 信息 tf.summary.trace_on(graph=True, profiler=True) # 开启Trace,可以记录图结构和profile信息 # 进行训练 with summary_writer.as_default(): tf.summary.trace_export(name="model_trace", step=0, profiler_outdir=log_dir) # 保存Trace信息到文件
import tensorflow as tf from zh.model.mnist.mlp import MLP from zh.model.utils import MNISTLoader num_batches = 1000 batch_size = 50 learning_rate = 0.001 log_dir = 'tensorboard' model = MLP() data_loader = MNISTLoader() optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate) summary_writer = tf.summary.create_file_writer(log_dir) # 实例化记录器 tf.summary.trace_on(profiler=True) # 开启Trace(可选) for batch_index in range(num_batches): X, y = data_loader.get_batch(batch_size) with tf.GradientTape() as tape: y_pred = model(X) loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y, y_pred=y_pred) loss = tf.reduce_mean(loss) print("batch %d: loss %f" % (batch_index, loss.numpy())) with summary_writer.as_default(): # 指定记录器 tf.summary.scalar("loss", loss, step=batch_index) # 将当前损失函数的值写入记录器 grads = tape.gradient(loss, model.variables) optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables)) with summary_writer.as_default(): tf.summary.trace_export(name="model_trace", step=0, profiler_outdir=log_dir) # 保存Trace信息到文件(可选)
天道酬勤 循序渐进 技压群雄