Tensorflow学习系列(二): tensorflow基础

本文介绍了TensorFlow中的图基础知识,通过一个实例展示了数据如何从节点流动,包括两个初始值a和b,经过相乘和相加操作,最终在节点e得到23的输出。整个流程解释了TensorFlow中计算图的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如需转载,请注明出处,欢迎加入深度学习群 255568483

Graph basics由nodes(结点)Edges(边缘)组成。

用一个简单的例子来讲解:

 

数据从左到右,请参见箭头的方向

1.在开始的时候,有两个值53,他们可能来自其它的Graph 或者从文件中或者是用户直接输入的。

2.这两个初始化的值被传到input结点,在graph中被标记为aba被传给了结点cdb也执行相同的操作。

3.结点c执行相乘的操作,他从结点ab取值,输出15到结点e。而结点d执行相加操作,他从结点ab取值,输出8到结点e

4.结点e是最终的输出操作,他执行的是相加的操作,他收到的值为158,最终输出的结果为23.

整个流程执行完毕,执行过程用数据表达如下:

a = input1;b=input2;

c=a*b;d=a+b;

e=c+d;

 

下面通过一个例子来说明:


完整代码如下:

import tensorflow as tf

graph = tf.Graph()
with graph.as_default():
    with tf.name_scope("variables"):
        # 定义变量,共执行了多少次
        global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")
        # 输出的累加值求和
        total_output = tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output")

    with tf.name_scope("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"):
            output = tf.add(b, c, name="output")

    with tf.name_scope("update"):
        # 更新输出的累加值
        update_total = total_output.assign_add(output)
        # 更新操作次数
        increment_step = global_step.assign_add(1)

    # 记录操作日志
    with tf.name_scope("summaries"):
        avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")
        tf.summary.scalar(name="output_summary", tensor=output)
        tf.summary.scalar(name="total_summary", tensor=update_total)
        tf.summary.scalar(name="average_summary", tensor=avg)

    with tf.name_scope("global_ops"):
        # 初始化参数
        init = tf.initialize_all_variables()
        # 合并所有日志
        merged_summaries = tf.summary.merge_all()

sess = tf.Session(graph=graph)
writer = tf.summary.FileWriter('log/tensorflow-basic', graph)
sess.run(init)


# 帮助函数
def run_graph(input_tensor):
    feed_dict = {a: input_tensor}
    _, step, summary = sess.run([output, increment_step, merged_summaries], 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()

查看log文件

$ tensorboard --logdir="tensorflow-basic"
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:126] Couldn't open CUDA library libcufft.so.8.0. LD_LIBRARY_PATH: 
I tensorflow/stream_executor/cuda/cuda_fft.cc:344] Unable to load cuFFT DSO.
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally
Starting TensorBoard b'41' on port 6006
(You can navigate to http://127.0.1.1:6006)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值