JobPlus知识库 IT 工业智能4.0 文章
tensorboard同时显示训练数据和测试数据的曲线

学着用tensorboard在一个模型里面有训练模型和测试模型的损失函数曲线对比,上网找了好多,但是还都是一个曲线,即自己画的是这样的

但是想要的是这样的:

到底应该怎么样呢?

简单操作:

[python] 

  1. tensorboard --logdir=run1:"/home/.../summary",run2:"/home/.../summary"   

其实只要在终端同时运行几个events即可,events就是summary生成的那个东西,

然后注意了,还有个骚操作:

注意,左上角的ignore outliers in chart scaling 没点时是上图,如果点了,就是下面的图了

最后附上我的代码,可以自己耍一下。

[python] 

  1. # -*- coding: utf-8 -*-  
  2. """ 
  3. Created on Tue May 29 09:17:55 2018 
  4.  
  5. @author: 618 
  6. """  
  7.   
  8. import tensorflow as tf  
  9. import numpy as np  
  10.   
  11. trX = np.linspace(-1, 1, 100)  
  12. trY = trX + np.random.randn(*trX.shape)*0.33  
  13.   
  14. teX = np.linspace(2, 3, 100)  
  15. teY = teX  
  16.   
  17. X = tf.placeholder(tf.float32, [100,], name="input_x")  
  18. Y = tf.placeholder(tf.float32, [100,], name="output_y")  
  19. X1 = tf.placeholder(tf.float32, [100,], name="input_x1")  
  20. Y1 = tf.placeholder(tf.float32, [100,], name="output_y1")  
  21.   
  22. def model(X, w):  
  23.     return tf.multiply(X, w)  
  24.   
  25. w = tf.Variable(0.1, name='weights')  
  26.   
  27. with tf.name_scope("cost"):  
  28.     y_model = model(X, w)  
  29.     cost = tf.reduce_mean(tf.square(Y - y_model))  
  30. tf.summary.scalar('loss', cost)  
  31.   
  32. with tf.name_scope("train"):  
  33.     train_op = tf.train.AdamOptimizer(0.01).minimize(cost)  
  34.   
  35. with tf.name_scope("test_cost"):  
  36.     y_model = model(X, w)  
  37.     test_cost = tf.reduce_mean(tf.square(Y - y_model))  
  38. tf.summary.scalar('test_loss', test_cost)  
  39.   
  40. merged = tf.summary.merge_all()  
  41. with tf.Session() as sess:  
  42.     tf.global_variables_initializer().run()  
  43.     summary_writer = tf.summary.FileWriter('./log/train', sess.graph)  
  44.     summary_writer1 = tf.summary.FileWriter('./log/test')  
  45.     for i in range(1000):  
  46.         feed_dict = {}  
  47.         if i % 100 == 0:  
  48.             print(sess.run(cost, feed_dict={X: trX, Y: trY}))  
  49.             print(sess.run(test_cost, feed_dict={X: teX, Y: teY}))  
  50.             summary, _ = sess.run([merged, test_cost], feed_dict={X: teX, Y: teY})  
  51.             summary_writer1.add_summary(summary, i)  
  52.         else:  
  53.             summary, _ =sess.run([merged, train_op], feed_dict={X: trX, Y: trY})  
  54.             summary_writer.add_summary(summary,i)  
  55.     print(sess.run(w))  
  56.     summary_writer.close()  
  57.     summary_writer1.close()  
  58.   
  59.   
  60.   
  61. ''''' 
  62. import tensorflow as tf 
  63. import numpy as np 
  64.  
  65. trX = np.linspace(-1, 1, 100) 
  66. trY = trX + np.random.randn(*trX.shape)*0.33 
  67.  
  68. teX = np.linspace(2, 3, 100) 
  69. teY = teX + 1 
  70.  
  71. X = tf.placeholder(tf.float32, [100,], name="input_x") 
  72. Y = tf.placeholder(tf.float32, [100,], name="output_y") 
  73. X1 = tf.placeholder(tf.float32, [100,], name="input_x1") 
  74. Y1 = tf.placeholder(tf.float32, [100,], name="output_y1") 
  75.  
  76. def model(X, w): 
  77.     return tf.multiply(X, w) 
  78.  
  79. w = tf.Variable(0.1, name='weights') 
  80.  
  81. y_model = model(X, w) 
  82. cost = tf.reduce_mean(tf.square(Y - y_model)) 
  83. tf.summary.scalar('loss', cost) 
  84. train_op = tf.train.AdamOptimizer(0.01).minimize(cost) 
  85.  
  86. y_model = model(X1, w) 
  87. test_cost = tf.reduce_mean(tf.square(Y1 - y_model)) 
  88. tf.summary.scalar('test_loss', test_cost) 
  89.  
  90. merged = tf.summary.merge_all() 
  91. with tf.Session() as sess: 
  92.     tf.global_variables_initializer().run() 
  93.     summary_writer = tf.summary.FileWriter('./log', sess.graph) 
  94.     for i in range(1000): 
  95.         sess.run([train_op], feed_dict={X: trX, Y: trY, X1: teX, Y1: teY}) 
  96.         if i % 100 == 0: 
  97.             print(sess.run(cost, feed_dict={X: trX, Y: trY, X1: teX, Y1: teY})) 
  98.             print(sess.run(test_cost, feed_dict={X: trX, Y: trY, X1: teX, Y1: teY})) 
  99.         summary = sess.run(merged, feed_dict={X: trX, Y: trY, X1: teX, Y1: teY}) 
  100.         summary_writer.add_summary(summary,i) 
  101.     print(sess.run(w)) 
  102.     summary_writer.close() 
  103. '''  


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
206人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序