1. 程式人生 > >tensorboard檢視tensorboard例子程式碼

tensorboard檢視tensorboard例子程式碼

#coding=utf-8

import tensorflow as tf import os import numpy as np os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

input_img   = tf.placeholder(dtype=tf.float32) input_label = tf.placeholder(dtype=tf.float32)

param_kernel1   = tf.get_variable(name='param_kernel1',shape=[3,3,1,8]) param_bias1     = tf.get_variable(name='param_bias1',shape=[8]) param_kernel2   = tf.get_variable(name='param_kernel2',shape=[3,3,8,8]) param_bias2     = tf.get_variable(name='param_bias2',shape=[8]) param_kernel3   = tf.get_variable(name='param_kernel3',shape=[3,3,8,1])

output1 = tf.nn.conv2d(input=input_img,filter=param_kernel1,strides=[1,1,1,1],padding='SAME') output1_bias    = tf.add(output1,param_bias1) output2 = tf.nn.conv2d(input=output1_bias,filter=param_kernel2,strides=[1,1,1,1],padding='SAME') output2_bias    = tf.add(output2,param_bias2) output_end_tmp  = tf.nn.conv2d(input=output2_bias,filter=param_kernel3,strides=[1,1,1,1],padding='SAME') output_end  = tf.squeeze(output_end_tmp)

loss=tf.reduce_mean(tf.square(input_label-output_end)) train_step  = tf.train.AdamOptimizer(0.001).minimize(loss) sess=tf.Session() sess.run(tf.global_variables_initializer())

tf.summary.scalar('Loss',loss) tf.summary.image('output2',tf.transpose(output2,perm=[3,1,2,0]),max_outputs=8)#具體請檢視tf.summary.image()的api介紹 merged_summary_op=tf.summary.merge_all() summary_writer=tf.summary.FileWriter('./',sess.graph)

for i in range(0,100):     img=np.random.random((1,32,32,1))     label=np.random.random((32,32))     [a,theloss]=sess.run([train_step,loss],feed_dict={input_img:img,input_label:label})     print(theloss)     summary=sess.run(merged_summary_op,feed_dict={input_img:img,input_label:label})     summary_writer.add_summary(summary,i)

#執行本程式碼後,按照tensorboard的使用步驟檢視即可