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

来源:互联网 发布:学sql和access哪个好 编辑:程序博客网 时间:2024/06/06 12:26

如需转载,请注明出处,欢迎加入深度学习群 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 tfgraph = 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 locallyI tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locallyI 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 locallyI tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locallyStarting TensorBoard b'41' on port 6006(You can navigate to http://127.0.1.1:6006)





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 配置充足玩lol卡怎么办 电脑买贵了应该怎么办 微信红包收不了钱怎么办 mac版excel卡死怎么办 锐捷网卡为空怎么办 淘宝上买东西卖家不发货怎么办 快递员没给单号怎么办 淘宝物流把货弄丢了怎么办 刚买的卡没激活怎么办 腾讯王卡40g用完怎么办 手机欠费不知道电话号码怎么办 手机欠费了不用了怎么办 闲鱼恶意差评怎么办 淘宝买家账户体检中心违规怎么办 淘宝卖家账户体检中心违规怎么办 淘宝好评被删除评价怎么办 淘宝没收到货确认收货了怎么办 美团评论被删怎么办 拼多多恶意差评怎么办 淘宝评论被删了怎么办 淘宝买东西错怪店家了怎么办 淘宝骗删除差评怎么办 淘宝买家号虚假交易违规怎么办 淘宝商家一直不发货怎么办 淘宝商家不发货也不退款怎么办 淘宝买家恶意差评怎么办 淘宝评价完了忘截图了怎么办 12306app登录不上怎么办 淘宝换绑支付宝失败怎么办 淘宝和手机不兼容怎么办 换号之后微信怎么办 手机不兼容的应用程序怎么办 微信版本低登录不了怎么办 软件与系统不兼容怎么办 软件与手机系统不兼容怎么办 qq和手机不兼容怎么办 来个软件不兼容怎么办 安卓8.0不兼容app怎么办 两条内存不兼容怎么办 王者荣耀软件不兼容怎么办 冒险岛不兼容win7怎么办