Tensorflow学习系列(二): tensorflow基础
来源:互联网 发布:学sql和access哪个好 编辑:程序博客网 时间:2024/06/06 12:26
如需转载,请注明出处,欢迎加入深度学习群 255568483
Graph basics由nodes(结点)和Edges(边缘)组成。
用一个简单的例子来讲解:
数据从左到右,请参见箭头的方向
1.在开始的时候,有两个值5和3,他们可能来自其它的Graph 或者从文件中或者是用户直接输入的。
2.这两个初始化的值被传到input结点,在graph中被标记为a和b,a被传给了结点c和d,b也执行相同的操作。
3.结点c执行相乘的操作,他从结点a和b取值,输出15到结点e。而结点d执行相加操作,他从结点a和b取值,输出8到结点e。
4.结点e是最终的输出操作,他执行的是相加的操作,他收到的值为15和8,最终输出的结果为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
- Tensorflow学习系列(二): tensorflow基础
- Tensorflow系列二:入门
- tensorflow学习笔记二:入门基础
- tensorflow学习记录(二):入门基础
- tensorflow学习笔记二:入门基础
- TensorFlow基础(二)
- Tensorflow 基础(二)
- TensorFlow学习系列(一):初识TensorFlow
- tensorflow(二)学习记录
- Tensorflow C++学习(二)
- tensorflow学习(二)
- tensorflow学习二
- tensorflow基础学习笔记
- Tensorflow基础学习
- Tensorflow基础学习笔记
- tensorflow学习笔记(二十一):tensorflow可视化
- TensorFlow学习笔记(二):TensorFlow入门
- Tensorflow实战学习(二)【怎么安装TensorFlow】
- 浅谈jQuery使用this和event.target
- ios 设备所在网络IP地址获取
- hdu 1083 Courses(二分图最大匹配)
- [VM]解决虚拟机声音滋滋声
- Write Posts With Rstudio, Rmarkdown Format And Publish Directly To Wordpress With Knitr & Rwordpress
- Tensorflow学习系列(二): tensorflow基础
- 手机端调用接口获取图片路径带有中文名称不能获取的问题。
- [Python]pyc pyo文件
- 事务配置
- company
- Python学习Iterable和Iterator
- Harris角点两篇比较好的文章
- Cent OS 6.5 安装JDK并进行环境变量的配置
- java 动态代理