【Python初学】使用Tensorflow 的可视化工具TensorBoard

本文通过实例演示如何使用TensorFlow构建神经网络,包括定义神经层、训练模型并使用TensorBoard进行可视化,展示了从数据准备到模型训练的完整过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在PyCharm中写下如下代码,当做练习

import tensorflow as tf


def add_layer(inputs, in_size, out_size, activation_function=None):  # 添加神经层
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):
            Weights = tf.Variable(tf.random.normal([in_size, out_size]), name='W')  # 权重,矩阵大写W,in_size,out_size代表行列矩阵
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([1, out_size])+0.1, name='b')  # 因为biases不为0, so +0.1
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.matmul(inputs, Weights)+biases
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)
        return outputs


# # 定义两个输入
with tf.name_scope('inputs'):
    xs = tf.compat.v1.placeholder(tf.float32, [None, 1], name='x_input')
    ys = tf.compat.v1.placeholder(tf.float32, [None, 1], name='y_input')


# # 定义了两个神经层
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)   # 隐藏层 ,10个神经元
prediction = add_layer(l1, 10, 1, activation_function=None)   # 输出层


# 预测值与真实值之间的误差
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
with tf.name_scope('train'):
    train_step = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(loss)  # 对误差进行更正,0.1为学习效率,minimize(loss)减少学习误差
#init = tf.compat.v1.global_variables_initializer()  # 初始变量

sess = tf.compat.v1.Session()
writer = tf.compat.v1.summary.FileWriter("logs/", sess.graph)  # graph表示整个框架

# 最重要的一步
sess.run(tf.compat.v1.global_variables_initializer())

运行之后在PyCharm左边生成logs文件夹
在这里插入图片描述
接下来就是如何使用TensorBoard
打开cmd窗口,找到TensorBoard所在的文件夹,复制路径。
在这里插入图片描述
在这里插入图片描述
接着找到你想要打开可视化文件所在的位置,复制文件路径,输入cmd----》tensorboard --logdir=你的路径。
在这里插入图片描述
在这里插入图片描述
注意:如果上述http://yxz-PC:6006/不起作用,而你的操作系统是Windows可以试试 localhost:6006。
在这里插入图片描述


import tensorflow as tf
import numpy as np


def add_layer(inputs, in_size, out_size, n_layer, activation_function=None):  # 添加神经层
    layer_name = 'layer%s'% n_layer
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):
            Weights = tf.Variable(tf.random.normal([in_size, out_size]), name='W')  # 权重,矩阵大写W,in_size,out_size代表行列矩阵
            tf.compat.v1.summary.histogram(layer_name + '/weights', Weights)
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([1, out_size])+0.1, name='b')  # 因为biases不为0, so +0.1
            tf.compat.v1.summary.histogram(layer_name + '/biases', biases)
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.matmul(inputs, Weights)+biases
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)
            tf.compat.v1.summary.histogram(layer_name + '/outputs', outputs)
        return outputs

# 生成一些真实的数据


x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise


# # 定义两个输入
with tf.name_scope('inputs'):
    xs = tf.compat.v1.placeholder(tf.float32, [None, 1], name='x_input')
    ys = tf.compat.v1.placeholder(tf.float32, [None, 1], name='y_input')


# # 定义了两个神经层
l1 = add_layer(xs, 1, 10, n_layer=1, activation_function=tf.nn.relu)   # 隐藏层 ,10个神经元
prediction = add_layer(l1, 10, 1, n_layer=2, activation_function=None)   # 输出层


# 预测值与真实值之间的误差
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
    tf.compat.v1.summary.scalar('loss', loss)
with tf.name_scope('train'):
    train_step = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(loss)  # 对误差进行更正,0.1为学习效率,minimize(loss)减少学习误差
#init = tf.compat.v1.global_variables_initializer()  # 初始变量


sess = tf.compat.v1.Session()
merged = tf.compat.v1.summary.merge_all()
writer = tf.compat.v1.summary.FileWriter("logs/", sess.graph)  # graph表示整个框架

# 最重要的一步
sess.run(tf.compat.v1.global_variables_initializer())


for i in range(100):
    sess.run(train_step, feed_dict={xs:x_data, ys:y_data})
    if i%5 == 0:
        result = sess.run(merged, feed_dict={xs:x_data, ys:y_data})
        writer.add_summary(result, i)

上述代码是实现的图形更多,可以观察loss的形状。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值