TensorFlow 初级
- 代码详见:NoteBook
- 我的博客:TensorFlow 核心——数据流图
TensorFlow 是基于数据流图 (Data Flow Graph), 支持自动微分 (简称AD) 的数值计算库。本文仅仅考虑低级 API.
TensorFlow 的计算图模型一般分为两个步骤:创建计算图,在 Session 中运行。(暂不考虑 Eager)
为了更好的管理模型,最好在特定的 Graph
中创建模型,且对于实现不同功能的模块最好按照 name_scope
对其进行划分。下面是一个 Demo:
# Explicitly create a Graph object
graph = tf.Graph()
with graph.as_default():
with tf.name_scope("variables"):
# Variable to keep track of how many times the graph has been run
global_step = tf.Variable(0, dtype=tf.int32, name="global_step")
# Variable that keeps track of the sum of all output values over time:
total_output = tf.Variable(0.0, dtype=tf.float32, name="total_output")
# Primary transformation Operations
with tf.name_scope("transformation"):
# Separate input layer
with tf.name_scope("input"):
# Create input placeholder- takes in a Vector
a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")
# Separate middle layer
with tf.name_scope("intermediate_layer"):
b = tf.reduce_prod(a, name="product_b")
c = tf.reduce_sum(a, name="sum_c")
# Separate output layer
with tf.name_scope("output"):
output = tf.add(b, c, name="output")
with tf.name_scope("update"):
# Increments the total_output Variable by the latest output
update_total = total_output.assign_add(output)
# Increments the above `global_step` Variable, should be run whenever the graph is run
increment_step = global_step.assign_add(1)
# Summary Operations
with tf.name_scope("summaries"):
avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")
# Creates summaries for output node
tf.summary.scalar('Output', output)
tf.summary.scalar('Sum of outputs over time', update_total)
tf.summary.scalar('Average of outputs over time', avg)
# Global Variables and Operations
with tf.name_scope("global_ops"):
# Initialization Op
init = tf.initialize_all_variables()
# Merge all summaries into one Operation
merged_summaries = tf.summary.merge_all()
def run_graph(input_tensor):
"""
Helper function; runs the graph with given input tensor and saves summaries
"""
feed_dict = {a: input_tensor}
out, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict)
writer.add_summary(summary, global_step=step)
# Start a Session, using the explicitly created Graph
sess = tf.Session(graph=graph)
# Open a SummaryWriter to save summaries
writer = tf.summary.FileWriter('../graph/improved_graph', graph)
# Initialize Variables
sess.run(init)
# Run the graph with various inputs
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])
# Write the summaries to disk
writer.flush()
# Close the SummaryWriter
writer.close()
# Close the session
sess.close()

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
html2canvas+jQuery+SpringMVC 实现网页转图片并保存到服务器
前端使用的是 RequireJS + jQuery 后端使用的是 SpringMVC + MyBatis 更多精彩 更多技术博客,请移步 asing1elife's blog 涉及资料 html2canvas 官网 将转换后的图片存储服务器的参考 将网页转换为图片 下载插件包 html2canvas 目前最新版是 v-1.0.0-aplha.12 ,该版本使用的是 ES6 语法 但本项目使用的是 jQuery ,并且基于 ES5 的语法,所以引入最新版插件时会一直报错 Uncaught (in promise) 对于没有使用的 ES6 语法的项目,建议下载 2017 年的版本,本文使用的是 v-0.5.0-beta4 调用插件进行转换 最新版及官网介绍的方法是基于 ES6 的语法结构,对于使用 ES5 语法的项目,需要使用如下方法 方法第一个参数是传入 DOM 元素,而通过 jQuery 获取的 DOM 元素实际上是一个集合,所以需要通过下标指定具体元素后插件才能正常获取 方法第二个参数是传入 options 配置,对于 ES5 语法需要使用 onrendered: function(...
-
下一篇
nodejs,python,sublime和Eclipse的包管理器
Python的包管理器叫pip。 首先安装Python运行环境Python 3.7.0:https://www.python.org/downloads/release/python-370/ Python安装完毕之后,即可使用命令行py -m pip install requests安装Python的包管理器pip。 pip安装完成后,会出现在Python运行环境目录下的Lib/site-packages文件夹里。 现在就可以使用pip安装需要的库了,比如安装django: pip3 install django 安装完毕后,在site-packages文件夹下会多出一个文件夹django, nodejs npm install 包名,将库安装到当前项目的node_modules文件夹下。 如果是npm -g install , 则会把库安装到C:Usersi042416AppDataRoaming pm下面: Sublime Sublime Text的包管理器: Sublime Text所有安装的插件都位于安装目录/Data/Packages/下面 Eclipse 所有安装的插件列...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS7设置SWAP分区,小内存服务器的救世主
- Mario游戏-低调大师作品
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7,8上快速安装Gitea,搭建Git服务器