无法将Tensorflow标量摘要写入事件日志

时间:2018-05-05 08:44:50

标签: tensorflow tensorboard filewriter summary scalar

我正在努力学习如何自己编写张流代码,但我仍然坚持这个非常基本的问题: 在学习步骤期间,我无法将任何标量摘要写入事件文件,这是Tensorboard所需的。这是我的代码:

import tensorflow as tf
import numpy as np
WORKDIR = "/content/log"

x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)

linear_model = tf.layers.Dense(units=1)

y_pred = linear_model(x)

sess = tf.Session()
init = tf.global_variables_initializer()
with tf.name_scope("TRAIN"):
  loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = optimizer.minimize(loss)

writer = tf.summary.FileWriter(WORKDIR + "/tmp/5", graph=sess.graph)

sess.run(init)

for i in range(1000):
  _, loss_value = sess.run((train, loss))
  if i % 50 == 0:
    writer.add_summary(summary=loss_value, global_step=i)
    print(str(i)+" step: "+str(loss_value))
    writer.flush()
writer.close()

我得到的错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-732ee0fb1e1c> in <module>()
     27   _, loss_value = sess.run((train, loss))
     28   if i % 50 == 0:
---> 29     writer.add_summary(summary=loss_value, global_step=i)
     30     print(str(i)+" step: "+str(loss_value))
     31     writer.flush()

/usr/local/lib/python3.6/dist- 
packages/tensorflow/python/summary/writer/writer.py in add_summary(self, 
 summary, global_step)
    123     # to save space - we just store the metadata on the first value 
with a
    124     # specific tag.
--> 125     for value in summary.value:
    126       if not value.metadata:
    127         continue

AttributeError: 'numpy.float32' object has no attribute 'value'

请帮助我纠正我的代码,因为我已经阅读了很多关于该主题的问题,但从未遇到过这个特定的问题。 感谢

2 个答案:

答案 0 :(得分:1)

您应该完全创建图形(损失和优化器是静态图形的一部分),然后您应该创建会话,最后,您可以创建一个编写器。 保持正确的顺序,然后就可以了。

 import tensorflow as tf
 import numpy as np
 WORKDIR = "/content/log"
 x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
 y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)
 linear_model = tf.layers.Dense(units=1)
 y_pred = linear_model(x)
 with tf.name_scope("TRAIN"):
 loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)
 optimizer = tf.train.GradientDescentOptimizer(0.01)
 train = optimizer.minimize(loss)
 init = tf.global_variables_initializer()
 with tf.Session() as sess:
      writer = tf.summary.FileWriter(WORKDIR + "/tmp/5", graph=sess.graph)
      sess.run(init)
      for i in range(1000):
         _, loss_value = sess.run((train, loss))
         if i % 50 == 0:
         writer.add_summary(summary=loss_value, global_step=i)
         print(str(i)+" step: "+str(loss_value))
         writer.flush()
       writer.close()

答案 1 :(得分:1)

summary operations收集标量摘要或一般摘要。考虑这个问题的另一种方法是通过摘要op包装您想要汇总的元素,而不是直接记录元素。此换行将创建"a Tensor containsing a Summary protobuf",即the acceptable format of add_summary

详细说明,您将通过以下方式创建摘要操作:

loss_summ = tf.summary.scalar('loss', loss)

然后运行此操作以获取张量/值:

_, loss_value, loss_summ_val = sess.run((train, loss, loss_summ))

最后,添加返回的张量/值,而不是实际的loss_value到摘要:

writer.add_summary(summary=loss_summ_val, global_step=i)

通过最少的必要更改,以下代码将起作用。然而,正如&#34; nairouz mrabah&#34;在另一个答案中建议,代码的结构可以改进。

import tensorflow as tf
import numpy as np
WORKDIR = "/content/log"

x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)

linear_model = tf.layers.Dense(units=1)

y_pred = linear_model(x)

sess = tf.Session()
init = tf.global_variables_initializer()
with tf.name_scope("TRAIN"):
  loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)
  loss_summ = tf.summary.scalar('loss', loss)
  optimizer = tf.train.GradientDescentOptimizer(0.01)
  train = optimizer.minimize(loss)

writer = tf.summary.FileWriter(WORKDIR + "/tmp/5", graph=sess.graph)

sess.run(init)

for i in range(1000):
  # _, loss_value = sess.run((train, loss))
  _, loss_value, loss_summ_val = sess.run((train, loss, loss_summ))
  if i % 50 == 0:
    # writer.add_summary(summary=loss_value, global_step=i)
    writer.add_summary(summary=loss_summ_val, global_step=i)
    print(str(i)+" step: "+str(loss_value))
    writer.flush()
writer.close()