ImportTensorFlow as TFImportNumPy as NPdefAdd_layer (inputs, in_size, Out_size, N_layer, activation_function=None):#add One more layer and return the output of this layerLayer_name ='layer%s'%N_layer with Tf.name_scope (layer_name): With Tf.name_scope ('jason_niu_weights'): Weights= TF. Variable (Tf.random_normal ([In_size, Out_size]), name='W') Tf.summary.histogram (Layer_name+'/weights', Weights) with Tf.name_scope ('jason_niu_biases'): Biases= TF. Variable (Tf.zeros ([1, out_size]) + 0.1, name='b') Tf.summary.histogram (Layer_name+'/biases', biases) with Tf.name_scope ('Jason_niu_wx_plus_b'): Wx_plus_b=Tf.add (Tf.matmul (inputs, Weights), biases)ifActivation_function isnone:outputs=Wx_plus_bElse: Outputs=activation_function (Wx_plus_b,) Tf.summary.histogram (Layer_name+'/outputs', outputs)returnoutputs#Make up some real dataX_data = Np.linspace (-1, 1, 300) [:, Np.newaxis]noise= Np.random.normal (0, 0.05, X_data.shape) Y_data= Np.square (x_data)-0.5 +Noise#define placeholder for inputs to networkWith Tf.name_scope ('jason_niu_inputs'): XS= Tf.placeholder (Tf.float32, [None, 1], name='X_input') Ys= Tf.placeholder (Tf.float32, [None, 1], name='Y_input')#Add hidden layerL1 = Add_layer (xs, 1, ten, n_layer=1, activation_function=Tf.nn.relu)#Add output LayerPrediction = Add_layer (L1, 1, n_layer=2, activation_function=None)#The error between Prediciton and real dataWith Tf.name_scope ('Jason_niu_loss'): Loss= Tf.reduce_mean (Tf.reduce_sum (Tf.square (YS-prediction), Reduction_indices=[1])) Tf.summary.scalar ('Jason_niu_loss', loss)With Tf.name_scope ('Jason_niu_train'): Train_step= Tf.train.GradientDescentOptimizer (0.1). Minimize (loss) Sess=TF. Session () merged= Tf.summary.merge_all () writer = Tf.summary.FileWriter ("logs3/", Sess.graph)#Important StepSess.run (Tf.global_variables_initializer ()) forIinchRange (+): Sess.run (Train_step, Feed_dict={xs:x_data, ys:y_data})ifI% = = 0:result = Sess.run (merged,feed_dict={xs:x_data, ys:y_data}) writer.add_summary (result, i)
TF:TF Tensorboard Practice: The neural network tensorboard form to get events.out.tfevents file +dos run the file local server output to the Web page visualization-jason Niu