tensorflow summary demo with linear-model

本文详细介绍如何使用tf.summary和TensorBoard来可视化TensorFlow模型的训练过程,包括损失、准确率等关键指标,以及如何将这些信息写入本地硬盘并进行可视化展示。

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

tf.summary + tensorboard 用来把graph图中的相关信息,如结构图、学习率、准确率、Loss等数据,写入到本地硬盘,并通过浏览器可视化之。

 

整理的代码如下:

import tensorflow as tf

x_train = [1, 2, 3, 6, 8]
y_train = [4.8, 8.5, 10.4, 21.0, 25.3]

x = tf.placeholder(tf.float32, name='x')
y = tf.placeholder(tf.float32, name='y')

W = tf.Variable(1, dtype=tf.float32, name='W')
b = tf.Variable(0, dtype=tf.float32, name='b')

linear_model = W * x + b

with tf.name_scope("loss-model"):
    loss = tf.reduce_sum(tf.square(linear_model - y))
    acc = tf.sqrt(loss)
    tf.summary.scalar("loss", loss)
    tf.summary.scalar("acc", acc)

with tf.name_scope("Parameters"):
    tf.summary.scalar("W", W)
    tf.summary.scalar("b", b)

tf.summary.histogram('histogram/norm', tf.random_normal(shape=[100], mean=10*b, stddev=1),)
tf.summary.image("image", tf.random_uniform([6,10,10,3]), max_outputs=10)

train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

# 收集所有的操作数据
merged = tf.summary.merge_all()

# 创建writer对象
writer = tf.summary.FileWriter('./logs', sess.graph)

# 训练
for i in range(300):
    summary, _ = sess.run([merged, train_step], {x: x_train, y: y_train})
    if i%10 == 0:
        writer.add_summary(summary, i)

curr_W, curr_b, curr_loss, curr_acc = sess.run([W, b, loss, acc], {x: x_train, y: y_train})
print("After train W: %f, b: %f, loss: %f, acc: %f" % (curr_W, curr_b, curr_loss, curr_acc))

运行结束后,可以看到logs文件夹如下所示:

logs folder

 

在当前目录,运行tensorboard --logdir=./logs --port 6006,浏览器打开,得到如下页面

其中,颜色越深的切片越老,颜色越浅的切片越新。

 

参考:

https://www.jianshu.com/p/0ad3761e3614

https://blog.youkuaiyun.com/akadiao/article/details/79551180

https://blog.youkuaiyun.com/dcrmg/article/details/79810142 

 

转载于:https://www.cnblogs.com/xbit/p/10071386.html

Loading model from: E:\anaconda\envs\control\lib\site-packages\lpips\weights\v0.1\alex.pth E:\anaconda\envs\control\lib\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing `weights=Inception_V3_Weights.IMAGENET1K_V1`. You can also use `weights=Inception_V3_Weights.DEFAULT` to get the most up-to-date weights. warnings.warn(msg) Traceback (most recent call last): File "inference.py", line 129, in <module> main(args) File "inference.py", line 80, in main z_embed, _ = model(z, None, secret) File "E:\anaconda\envs\control\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl return forward_call(*input, **kwargs) File "F:\python_demo\RoSteALS-main\cldm\ae.py", line 535, in forward eps, posterior = self.control(image, c) # SecretEncoder3返回的是(秘密信息隐空间表达,None) File "E:\anaconda\envs\control\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl return forward_call(*input, **kwargs) File "F:\python_demo\RoSteALS-main\cldm\ae.py", line 89, in forward c = self.encode(c) # 将01字符串映射为隐空间表达 File "F:\python_demo\RoSteALS-main\cldm\ae.py", line 79, in encode x = self.secret_scaler(x) File "E:\anaconda\envs\control\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl return forward_call(*input, **kwargs) File "E:\anaconda\envs\control\lib\site-packages\torch\nn\modules\container.py", line 139, in forward input = module(input) File "E:\anaconda\envs\control\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl return forward_call(*input, **kwargs) File "E:\anaconda\envs\control\lib\site-packages\torch\nn\modules\linear.py", line 114, in forward return F.linear(input, self.weight, self.bias) RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x100 and 30x3072) (control) PS F:\python_demo\RoSteALS-main> 这个报错是什么意思,怎么解决
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值