在 Tensorboard 中使用 Tensorflow v2.0 显示图表
Display graph using Tensorflow v2.0 in Tensorboard
以下代码使用 TensorFlow v2.0
import tensorflow as tf
a = tf.constant(6.0, name = "constant_a")
b = tf.constant(3.0, name = "constant_b")
c = tf.constant(10.0, name = "constant_c")
d = tf.constant(5.0, name = "constant_d")
mul = tf.multiply(a, b , name = "mul")
div = tf.divide(c,d, name ="div")
addn = tf.add_n([mul, div], name = "addn")
writer = tf.summary.create_file_writer("./tensorboard")
with writer.as_default():
tf.summary.scalar("addn_harsha", addn, step=1)
我是 Python 和 Tensorflow 的新手。
我可以使用上面的代码在 Tensorboard 中创建 Scalar。
但是我无法生成相同的图形。
在 TensorFlow v1.0 中,我们这样写:
writer = tf.summary.FileWriter("./tensorboard", sess.graph)
但是在TensorFlow v2.0中,不再使用Session。
那么,我们可以编写什么来使用 TensorFlow v2.0
在 TensorBoard 中创建图形
可以通过多种方式做到这一点,但存在两个问题。主要是TensorFlow 2.0一般都是工作在eager模式下,所以根本就没有graph可以log。我发现的另一个问题,至少在我的安装中,是当我尝试加载带有图形的日志目录时,2.0 Tensorboard 崩溃。我想这会得到修复,但现在我只能检查使用 Tensorboard 1.15 以 2.0 编写的结果图。
据我所知,在 TensorFlow 2.0 中至少有两种写图的方法。现在最直接的方法是在训练中使用 Keras 模型和 TensorBoard
回调以及 write_graph=True
。这可能看起来像这样:
import tensorflow as tf
import numpy as np
# Make Keras model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, input_shape=(10,)))
model.compile(optimizer=tf.keras.optimizers.SGD(), loss='MSE')
# Make callback
log_dir = '...'
tb_cbk = tf.keras.callbacks.TensorBoard(log_dir, write_graph=True)
# Fit to some data using the callback
x, y = np.ones((100, 10)), np.ones((100, 1))
model.fit(x, y, batch_size=5, epochs=2, callbacks=[tb_cbk])
如果您只想将任意一段 TensorFlow 代码转换为图形,可以使用 tf.function
. This will convert a regular Python function into a graph, or better, a callable that generates graphs on demand, which you can then save. To do this, though, you would need a presumed tf.summary.graph
graph function that is not there yet. The function exists, however, it is just not exposed in the main API (not sure if they will incorporate it in the future), but you can access it through the summary_ops_v2
模块。你可以这样使用它:
import tensorflow as tf
from tensorflow.python.ops import summary_ops_v2
# Some function to convert into a graph
@tf.function
def my_fun(x):
return 2 * x
# Test
a = tf.constant(10, tf.float32)
b = my_fun(a)
tf.print(b)
# 20
# Log the function graph
log_dir = '...'
writer = tf.summary.create_file_writer(log_dir)
with writer.as_default():
# Get concrete graph for some given inputs
func_graph = my_fun.get_concrete_function(a).graph
# Write the graph
summary_ops_v2.graph(func_graph.as_graph_def(), step=0)
writer.close()
同样,在这两种情况下,我只能在 1.x 版本的 Tensorboard 中可视化结果,但生成的日志文件是正确的。
以下代码使用 TensorFlow v2.0
import tensorflow as tf
a = tf.constant(6.0, name = "constant_a")
b = tf.constant(3.0, name = "constant_b")
c = tf.constant(10.0, name = "constant_c")
d = tf.constant(5.0, name = "constant_d")
mul = tf.multiply(a, b , name = "mul")
div = tf.divide(c,d, name ="div")
addn = tf.add_n([mul, div], name = "addn")
writer = tf.summary.create_file_writer("./tensorboard")
with writer.as_default():
tf.summary.scalar("addn_harsha", addn, step=1)
我是 Python 和 Tensorflow 的新手。 我可以使用上面的代码在 Tensorboard 中创建 Scalar。 但是我无法生成相同的图形。
在 TensorFlow v1.0 中,我们这样写: writer = tf.summary.FileWriter("./tensorboard", sess.graph)
但是在TensorFlow v2.0中,不再使用Session。 那么,我们可以编写什么来使用 TensorFlow v2.0
在 TensorBoard 中创建图形可以通过多种方式做到这一点,但存在两个问题。主要是TensorFlow 2.0一般都是工作在eager模式下,所以根本就没有graph可以log。我发现的另一个问题,至少在我的安装中,是当我尝试加载带有图形的日志目录时,2.0 Tensorboard 崩溃。我想这会得到修复,但现在我只能检查使用 Tensorboard 1.15 以 2.0 编写的结果图。
据我所知,在 TensorFlow 2.0 中至少有两种写图的方法。现在最直接的方法是在训练中使用 Keras 模型和 TensorBoard
回调以及 write_graph=True
。这可能看起来像这样:
import tensorflow as tf
import numpy as np
# Make Keras model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, input_shape=(10,)))
model.compile(optimizer=tf.keras.optimizers.SGD(), loss='MSE')
# Make callback
log_dir = '...'
tb_cbk = tf.keras.callbacks.TensorBoard(log_dir, write_graph=True)
# Fit to some data using the callback
x, y = np.ones((100, 10)), np.ones((100, 1))
model.fit(x, y, batch_size=5, epochs=2, callbacks=[tb_cbk])
如果您只想将任意一段 TensorFlow 代码转换为图形,可以使用 tf.function
. This will convert a regular Python function into a graph, or better, a callable that generates graphs on demand, which you can then save. To do this, though, you would need a presumed tf.summary.graph
graph function that is not there yet. The function exists, however, it is just not exposed in the main API (not sure if they will incorporate it in the future), but you can access it through the summary_ops_v2
模块。你可以这样使用它:
import tensorflow as tf
from tensorflow.python.ops import summary_ops_v2
# Some function to convert into a graph
@tf.function
def my_fun(x):
return 2 * x
# Test
a = tf.constant(10, tf.float32)
b = my_fun(a)
tf.print(b)
# 20
# Log the function graph
log_dir = '...'
writer = tf.summary.create_file_writer(log_dir)
with writer.as_default():
# Get concrete graph for some given inputs
func_graph = my_fun.get_concrete_function(a).graph
# Write the graph
summary_ops_v2.graph(func_graph.as_graph_def(), step=0)
writer.close()
同样,在这两种情况下,我只能在 1.x 版本的 Tensorboard 中可视化结果,但生成的日志文件是正确的。