利用Tensorboard可视化网络结构

在使用Tensorboard进行机器学习项目可视化时,遇到Terminal返回的网址无法打开的问题。通过将localhost替换为127.0.0.1,成功解决了这一问题,现在可以正常访问Tensorboard的网络结构可视化了。

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

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets('MNIST_data',one_hot=True)

batch_size=100
n_batch=mnist.train.num_examples//batch_size

with tf.name_scope('input'):
    x=tf.placeholder(tf.float32,[None,784],name='x_input')
    y=tf.placeholder(tf.float32,[None,10],name='y_input')

with tf.name_scope('layer'):
    with tf.name_scope('weights'):
        w=tf.Variable(tf.random_normal([784,10]),name='W')
    with tf.name_scope('biases'):
        b=tf.Variable(tf.zeros([10]),'b')
    with tf.name_scope('Wx_plus_b'):
        Wx_plus_b=tf.matmul(x,w)+b
    with tf.name_scope('softmax'):
        y_pred=tf.nn.tanh(Wx_plus_b)

with tf.name_scope('loss'):
    loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_pred))
with tf.name_scope('accuracy'):
    accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y,1),tf.argmax(y_pred,1)),tf.float32))

init=tf.global_variables_initializer()
with tf.name_scope('train'):
    train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)


with tf.Session() as sess:
    sess.run(init)
    writer=tf.summary.FileWriter('../logs',sess.graph)
    for epoch in range(21):
        for batch in range(n_batch):
            x_batch,y_batch=mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:x_batch,y:y_batch})
        print('Iter'+str(epoch)+', accuracy='+str(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})))

遇到了一个问题:Terminal返回的网址总是打不开
后来经过网络查询,把原本的输入

tensorboard --logdir='yourpath'

改为

tensorboard --logdir='yourpath' --host=127.0.0.1

就可以正常打开了,原本网址localhost名称的地方变为了127.0.0.1
总体框架图细节展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值