zhangzheny 2018-03-09 03:54 采纳率: 33.3%
浏览 3499
已结题

tensorboard 可视化时 embedding中没有数据怎么回事?

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
from tensorflow.contrib.tensorboard.plugins import projector

mnist=input_data.read_data_sets('mnist_data/', one_hot=True)
tf.reset_default_graph()
max_step=1001
image_num=3000
DIR="D:/Jupyter"
sess=tf.Session()

embedding=tf.Variable(tf.stack(mnist.test.images[:image_num]),trainable=False,name="embedding")

def variable_sumaries(var):
    with tf.name_scope("sumaries"):
            mean=tf.reduce_mean(var)
            tf.summary.scalar("mean",mean)
            with tf.name_scope("stddev"):
                stddev=tf.sqrt(tf.reduce_mean(mean-var))
            tf.summary.scalar("stddev",stddev)
            tf.summary.scalar("max",tf.reduce_max(var))
            tf.summary.scalar("min",tf.reduce_min(var))
            tf.summary.histogram("histogram",var)
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("input_reshape"):
    image_reshape_input=tf.reshape(x,shape=[-1,28,28,1])
    tf.summary.image("input",image_reshape_input,10)

with tf.name_scope("layer"):
    with tf.name_scope("weights"):
        W = tf.Variable(tf.zeros([784,10]),tf.float32,name="W")
        variable_sumaries(W)
    with tf.name_scope("biases"):
        b= tf.Variable(tf.zeros([10]),tf.float32,name="b")
        variable_sumaries(b)
    with tf.name_scope("wx_plus_b"):
        wx_plus_b=tf.matmul(x,W)+b
    with tf.name_scope("softmax"):
        softmax=tf.nn.softmax(wx_plus_b)

sess.run(tf.global_variables_initializer())
with tf.name_scope("loss"):
    loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=softmax))
    tf.summary.scalar("loss",loss)
with tf.name_scope("train"):
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

with tf.name_scope("accuracy"):
    with tf.name_scope("correct_prediction"):
        correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(softmax,1))

    with tf.name_scope("accuracy"):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        tf.summary.scalar("accuracy",accuracy)
if tf.gfile.Exists(DIR+"/projector/projector/metadata.csv"):
    tf.gfile.DeleteRecursively(DIR+"/projector/projector/metadata.csv")
with open(DIR+"/projector/projector/metadata.csv","w") as f:
    lables= sess.run(tf.argmax(mnist.test.labels[:],1))
    for i in range(image_num):
        f.write(str(lables[i])+'\n')

merged=tf.summary.merge_all()
projector_writer=tf.summary.FileWriter(DIR+"/projector/projector",sess.graph)

saver=tf.train.Saver()
config=projector.ProjectorConfig()
embed=config.embeddings.add()
embed.tensor_name=embedding.name
embed.metadata_path=DIR+"/projector/projector/metadata.csv"
embed.sprite.image_path=DIR+"/projector/data/mnist_spliter.png"
embed.sprite.single_image_dim.extend([28,28])
projector.visualize_embeddings(projector_writer,config)

for i in range(max_step):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    run_options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata=tf.RunMetadata()
    summary, _ =sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys},options=run_options,run_metadata=run_metadata)
    projector_writer.add_run_metadata(run_metadata,'step %03d' % i)
    projector_writer.add_summary(summary,i)
    if i% 100==0:
        print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))

saver.save(sess, DIR+"/projector/projector/a_model.ckpt",global_step=max_step)
projector_writer.close()
sess.close()

图片说明

  • 写回答

1条回答 默认 最新

  • devmiao 2018-03-09 16:13
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题