tensorflow 牛刀小试,模型框架搭建

import tensorflow as tf

graph = tf.Graph()

arr = [i for i in range(20)]
with graph.as_default():
    sess = tf.Session(graph=graph)
    
    with tf.name_scope("variables"):
        global_steps = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_steps")

        
    with tf.name_scope("input"):
        a = tf.placeholder(tf.int32, shape=[], name="placeholder_a")  #转化为张量

        
    with tf.name_scope("update"):
        increment_step = global_steps.assign_add(1)

    
    #注意这里,summary.scaler接受的参数是标量,如果是一个张量数组或者带维度的张量,可以使用tf.reduce_sum()降维,变为标量
    with tf.name_scope("summary"):
        output = a
        #output = tf.reduce_sum(a)
        tf.summary.scalar("output", output)


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

        
    writer = tf.summary.FileWriter("./train", sess.graph)
    sess.run(init)


    def run_graph(input_tensor):
        feed_dict = {a: input_tensor} #字典的值可以使数字、字符串、列表、numpy,当shape=[],则为数字
        step, summary = sess.run([increment_step, merged_summaries], feed_dict = feed_dict)
        writer.add_summary(summary, global_step=step)


    for i in range(len(arr)):
        input_tensor = i
        run_graph(input_tensor)

在代码中存在五个命名空间,但是显示时只有四个,我记得一种解释是tensorboard并不会将tf.summary显示出来,而在本模块并没有进行任何张量操作,所以不显示。

修改代码:

在summaries命名空间添加节点计算

import tensorflow as tf

graph = tf.Graph()

arr = [i for i in range(20)]
with graph.as_default():
    sess = tf.Session(graph=graph)
    with tf.name_scope("variables"):
        global_steps = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_steps")

    with tf.name_scope("input"):
        a = tf.placeholder(tf.int32, shape=[], name="placeholder_a")

    with tf.name_scope("update"):
        increment_step = global_steps.assign_add(1)

    with tf.name_scope("summaries"):
        b = tf.constant(dtype=tf.int32, shape=[], value=2)
        output = a + b
        #tf.summary.scalar("output", output)
        tf.summary.scalar("output", a)

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

    writer = tf.summary.FileWriter("./train", sess.graph)
    sess.run(init)


    def run_graph(input_tensor):
        feed_dict = {a: input_tensor}
        step, summary = sess.run([increment_step, merged_summaries], feed_dict = feed_dict)
        writer.add_summary(summary, global_step=step)


    for i in range(len(arr)):
        input_tensor = i
        run_graph(input_tensor)

则summaries可以显示出来。 

对于像这种简单的不追求模型复用(呗做成模块来给其他人用),可以按照这种框架来进行搭建

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值