Getting Started with TensorBoard: Visualizing a Simple Logistic Regression in TensorFlow
This guide shows how to use TensorBoard to visualize the training process of a simple logistic regression model in TensorFlow, covering data generation, model definition, summary creation, file writing, and launching TensorBoard to inspect graphs and scalar curves.
TensorBoard can display the full computational graph of a neural network as well as various trend curves such as histograms and scalars.
The article walks through a simple logistic‑regression experiment to demonstrate how to use TensorBoard for visualization. The procedure consists of the following steps:
Wrap variables with tf.variable_scope('custom_label') to group them.
Create histogram summaries with tf.summary.histogram('custom_hist', variable_to_monitor) .
Create scalar summaries with tf.summary.scalar('custom_scalar', scalar_to_monitor) .
Merge all summaries using tf.summary.merge_all() , which returns a handle merge_op for later execution.
Instantiate a FileWriter to write logs to a local directory.
During training, call FileWriter.add_summary(result, step) to write each step’s values into the TensorBoard buffer.
The complete Python code (with detailed comments) is shown below.
# -*- coding: utf-8 -*-
# -*- author: knock -*-
import tensorflow as tf
import numpy as np
'''TensorBoard: Visualize logistic‑regression training process'''
# 1. Generate synthetic data
x = np.linspace(-1, 1, 100)[:, np.newaxis] # shape (100, 1)
noise = np.random.normal(0, 0.1, size=x.shape) # noise
y = np.power(x, 2) + noise # target values
# 2. Define TensorFlow placeholders
with tf.variable_scope('Inputs'):
input_x = tf.placeholder(tf.float32, x.shape, name='input_x')
input_y = tf.placeholder(tf.float32, y.shape, name='input_y')
# 3. Build a simple network: input → hidden (ReLU) → output
with tf.variable_scope('Net'):
hidden_1 = tf.layers.dense(input_x, 10, tf.nn.relu, name='hidden_layer')
output = tf.layers.dense(hidden_1, 1, name='output_layer')
# Add histograms for hidden layer and predictions
tf.summary.histogram('hidden_out', hidden_1)
tf.summary.histogram('prediction', output)
# 4. Compute mean‑squared error loss
loss = tf.losses.mean_squared_error(input_y, output, scope='loss')
# 5. Optimize loss with gradient descent
train_op = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss)
# Record loss as a scalar summary
tf.summary.scalar('loss', loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('./logs', sess.graph) # write graph and summaries
merge_op = tf.summary.merge_all()
# 6. Train for 100 steps and write summaries each step
for step in range(100):
_, result = sess.run([train_op, merge_op], {input_x: x, input_y: y})
writer.add_summary(result, step)
# To launch TensorBoard, run in a terminal:
# tensorboard --logdir logs
# Then open http://localhost:6006 in a browser.Running the command tensorboard --logdir logs starts TensorBoard (e.g., at http://0.0.0.0:6006), where the user can explore the computational graph, histogram distributions of hidden activations and predictions, and the loss curve over training steps.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ThinkingAgent
Sharing the latest AI-native technologies and real-world implementations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
