TensorFlow2的tensorflow1的静态图实现
import tensorflow as tf
g = tf.compat.v1.Graph()
with g.as_default():
x = tf.compat.v1.placeholder(name='x', shape=[], dtype=tf.string)
y = tf.compat.v1.placeholder(name='y', shape=[], dtype=tf.string)
z = tf.strings.join([x,y],name = "join",separator = " ")
with tf.compat.v1.Session(graph = g) as sess:
# fetches的结果非常像一个函数的返回值,而feed_dict中的占位符相当于函数的参数序列。
result = sess.run(fetches = z,feed_dict = {x:"hello",y:"world"})
print(result)
tensorflow动态图的展示:
#https://www.cnblogs.com/xiximayou/p/12673167.html
#tensorflow three different graphs
import tensorflow as tf
# 使用autograph构建静态图
@tf.function
def strjoin(x,y):
z = tf.strings.join([x,y],separator = " ")
tf.print(z)
return z
result = strjoin(tf.constant("hello"),tf.constant("world"))
print(result)
import datetime
# 创建日志
stamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
logdir = './data/autograph/%s' % stamp
writer = tf.summary.create_file_writer(logdir)
# 开启autograph跟踪
tf.summary.trace_on(graph=True, profiler=True)
# 执行autograph
result = strjoin("hello","world")
# 将计算图信息写入日志
with writer.as_default():
tf.summary.trace_export(
name="autograph",
step=0,
profiler_outdir=logdir)
# 启动 tensorboard在jupyter中的魔法命令
%load_ext tensorboard
# 启动tensorboard
%tensorboard --logdir ./data/autograph/
整个动态图运行:
该总结对于学习使用tensorflow board 的启动及应用可以参考。