tensorflow48 《面向机器智能的TensorFlow实战》笔记-03-03 tensorflow基本矢量图、summary、feed_dict

# 《面向机器智能的Tensor Flow实战》03 TensorFlow基础
# win10 Tensorflow-gpu1.1.0 python3.5.3
# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
# 原书代码(tensorflow0.8):https://github.com/backstopmedia/tensorflowbook
# tensorflow不同版本api变化:https://github.com/tensorflow/tensorflow/releases
# filename:tfmi03.03.py # tensorflow基本矢量图、总结、命名空间、feed_dict参数

import tensorflow as tf
import numpy as np

# tf.mul、tf.sub 被弃用,现在使用的是 tf.multiply、tf.subtract
# 移除了原来的 tf summary 运算符,比如 tf.scalar_summary. 取而代之的是 tf.summary.scalar
graph = tf.Graph()

with graph.as_default():
    with tf.name_scope("variables"):
        global_step = tf.Variable(0, dtype=tf.int32, name="global_step")

        increment_step = global_step.assign_add(1)

        previous_value = tf.Variable(0.0, dtype=tf.float32, name="previous_value")

    with tf.name_scope("exercise_transformation"):
        with tf.name_scope("input"):
            a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")

        with tf.name_scope("intermediate_layer"):
            b = tf.reduce_prod(a, name="product_b")
            c = tf.reduce_sum(a, name="sum_c")

        with tf.name_scope("output"):
            d = tf.add(b, c, name="add_d")
            output = tf.subtract(d, previous_value, name="output")
            update_prev = previous_value.assign(output)

    with tf.name_scope("summaries"):
        tf.summary.scalar("output_summary", output)
        tf.summary.scalar("prod_summary", b)
        tf.summary.scalar("sum_summary", c)

    with tf.name_scope("global_ops"):
        init = tf.global_variables_initializer()
        merged_summaries = tf.summary.merge_all()

sess = tf.Session(graph=graph)

writer = tf.summary.FileWriter('./tfmi/improved_graph', graph)

sess.run(init)

def run_graph(input_tensor):
    """
    辅助函数;运行给定输入张量的图形并保存摘要
    """
    feed_dict = {a: input_tensor}
    output, summary, step = sess.run([update_prev, merged_summaries, increment_step], feed_dict=feed_dict)
    writer.add_summary(summary, global_step=step)


# 测试不同参数
run_graph([2, 8])
run_graph([3, 1, 3, 3])
run_graph([8])
run_graph([1, 2, 3])
run_graph([11, 4])
run_graph([4, 1])
run_graph([7, 3, 1])
run_graph([6, 3])
run_graph([0, 2])
run_graph([4, 5, 6])

# 将摘要写入磁盘
writer.flush()

writer.close()
sess.close()

# tensorboard --logdir="./tfmi/improved_graph"

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值