TensorBoard視覺化程式碼
阿新 • • 發佈:2018-12-03
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.contrib.tensorboard.plugins import projector #載入資料集 mnist=input_data.read_data_sets("MNIST_data",one_hot=True) #執行次數 max_steps=1001 #圖片數量 image_num=2000 #檔案路徑 DIR="C:/Users/thisi/PycharmProjects/20181127/" #定義會話 sess=tf.Session() #載入圖片 embedding=tf.Variable(tf.stack(mnist.test.images[:image_num]),trainable=False,name='embedding') #引數概要 def variable_summaries(var): with tf.name_scope('summaries'): mean=tf.reduce_mean(var) tf.summary.scalar('mean',mean) with tf.name_scope('stddev'): stddev=tf.sqrt(tf.reduce_mean(tf.square(var-mean))) 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'): #這裡的none表示第一個維度可以是任意的長度 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_shape_input=tf.reshape(x,[-1,28,28,1]) tf.summary.image('input',image_shape_input,10) #建立一個簡單神經網路 with tf.name_scope('layer'): with tf.name_scope('weights'): W=tf.Variable(tf.zeros([784,10]),name='W') variable_summaries(W) with tf.name_scope('biases'): b=tf.Variable(tf.zeros([10]),name='b') variable_summaries(b) with tf.name_scope('wx_plus_b'): wx_plus_b=tf.matmul(x,W)+b with tf.name_scope('softmax'): prediction=tf.nn.softmax(wx_plus_b) with tf.name_scope('loss'): #交叉熵代價函式 loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction)) tf.summary.scalar('loss',loss) with tf.name_scope('train'): #使用梯度下降法 train_step=tf.train.GradientDescentOptimizer(0.5).minimize(loss) #初始化變數 sess.run(tf.global_variables_initializer()) with tf.name_scope('accuracy'): with tf.name_scope('correct_prediction'): #結果存放在一個布林型列表中 correct_prediction=tf.equal((tf.argmax(y,1)),tf.argmax(prediction,1))#argmax返回一維張量中最大的值所在的位置 with tf.name_scope('accuracy'): #求準確率 accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) tf.summary.scalar('accuracy',accuracy) #產生metadata檔案 # if tf.gfile.Exists(DIR+'projector/projector/metadata.tsv'): # tf.gfile.DeleteRecursively(DIR+'projector/projector/metadata.tsv') with open(DIR+'projector/projector/metadata.tsv','w') as f: labels=sess.run(tf.argmax(mnist.test.labels[:],1)) for i in range(image_num): f.write(str(labels[i])+'\n') #合併所有的summary 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.tsv' embed.sprite.image_path=DIR+'projector/data/mnist_10k_sprite.png' embed.sprite.single_image_dim.extend([28,28]) projector.visualize_embeddings(projector_writer,config) for i in range(max_steps): #每個批次100個樣本 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: acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}) print("Iter"+str(i)+",Testing Accuracy= "+str(acc)) saver.save(sess,DIR+'projector/projector/a_model.ckpt',global_step=max_steps) projector_writer.close() sess.close()