TensorFlow —— Demo
import tensorflow as tf
g = tf.Graph() # 创建一个Graph对象
在模型中有两个“全局”风格的Variable对象:global_step 和 total_output 。它们本质上是全局的,因此在声明它们时需要与数据流图的其他节点区分开,并把它们放入自己的名称作用域。trainable= False的设置使得该Variable对象对象不会对模型造成影响(因为没有任何训练的步骤),但该设置明确指定了这些Variable对象只能通过手工设置。
shape 为 None 代表流经边的张量为任意长度的向量;
shape 为 [] 代表流经边的张量为一个标量。
构建数据流图
with g.as_default(): # 将 Graph对象 设为默认 Graph对象
with tf.name_scope('variables'):
# 记录数据流图运行次数的 Variable 对象
# 这是一种常见的范式,在整个API中,这种范式会频繁出现
global_step = tf.Variable(0, dtype= tf.float32, trainable= False, name= 'global_step')
# 追踪该模型的所有输出随时间的累加和的 Variable 对象
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= 'prod_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'):
# 用最新的输出更新Variable对象total_output
update_total = total_output.assign(output)
# 将Variable对象global_step增 1 ,只要数据流图运行,该操作便需要进行
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('Output', output)
tf.summary.scalar('Sum of outputs over time', update_total)
tf.summary.scalar('Average of outputs over time', avg)
with tf.name_scope('global_ops'):
# 初始化Op
init = tf.global_variables_initializer()
# 将所有汇总数据合并到一个Op中
merged_summaries = tf.summary.merge_all()
INFO:tensorflow:Summary name Sum of outputs over time is illegal; using Sum_of_outputs_over_time instead.
INFO:tensorflow:Summary name Average of outputs over time is illegal; using Average_of_outputs_over_time instead.
为什么将 tf.summary.merge_all() \(Op\) 放在 “\(global\_ops\)” 名称作用域,而非放在 “\(summaries\)” 作用域?
- 一般而言,将
tf.summary.merge_all()与其他全局\(Op\)放在一起是最佳做法。我们的数据流图只为汇总数据设置了一个环节,但这并不妨碍去想象一个拥有Variable、Op和名称作用域等的不同汇总数据的数据流图。通过保持tf.summary.merge_all()的分离,可确保用户无需记忆放置它的特定“summary” 代码块,从而比较容易找到该\(Op\)。
运行数据流图
sess = tf.Session(graph= g)
# 保存汇总数据
writer = tf.summary.FileWriter('E:/Graphs/xin_graph', g)
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()
关于Tensorboard
Tensorboard踩坑记:https://zhuanlan.zhihu.com/p/29284886
TensorFlow —— Demo的更多相关文章
- android应用市场、社区客户端、漫画App、TensorFlow Demo、歌词显示、动画效果等源码
Android精选源码 MVP架构Android应用市场项目 android刻度盘控件源码 Android实现一个社区客户端 android商品详情页上拉查看详情 基于RxJava+Retrofit2 ...
- cnn汉字识别 tensorflow demo
# -*- coding: utf-8 -*- import tensorflow as tf import os import random import tensorflow.contrib.sl ...
- TensorFlow 在android上的Demo(1)
转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...
- TensorFlow Lite demo——就是为嵌入式设备而存在的,底层调用NDK神经网络API,注意其使用的tf model需要转换下,同时提供java和C++ API,无法使用tflite的见后
Introduction to TensorFlow Lite TensorFlow Lite is TensorFlow’s lightweight solution for mobile and ...
- YOLO2:实时目标检测视频教程,视频演示, Android Demo ,开源教学项目,论文。
实时目标检测和分类 GIF 图: 视频截图: 论文: https://arxiv.org/pdf/1506.02640.pdf https://arxiv.org/pdf/1612.08242.pdf ...
- 学习笔记TF066:TensorFlow移动端应用,iOS、Android系统实践
TensorFlow对Android.iOS.树莓派都提供移动端支持. 移动端应用原理.移动端.嵌入式设备应用深度学习方式,一模型运行在云端服务器,向服务器发送请求,接收服务器响应:二在本地运行模型, ...
- 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)
承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行 在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...
- Distributed TensorFlow
Distributed TensorFlow Todo list: Distributed TensorFlow简介 Distributed TensorFlow的部署与运行 对3个台主机做多卡GPU ...
- darkflow测试和训练yolo
转自 https://blog.csdn.net/u011961856/article/details/76582669参考自github:https://github.com/thtrieu/dar ...
随机推荐
- C#连接oracle数据库提示ORA-12154: TNS: 无法解析指定的连接标识符
C#连接oracle数据库提示ORA-12154: TNS: 无法解析指定的连接标识符如果PLSQL Develope能连接上而用代码无法连接上则可以考虑sqlnet.ora文件中是否有NAMES.D ...
- Asp.net Api中使用OAuth2.0实现“客户端验证”
一.实现继承自OAuthAuthorizationServerProvider的类,实现以"客户端验证"方式传入的相关认证和access_token发放. public class ...
- oracle实用基础
Oracle修改字段名 alter table 表名 rename column 旧字段名 to 新字段名例子:alter table T_STUDENT rename column NAME to ...
- Log4j – Log4j 2 API
Overview The Log4j 2 API provides the interface that applications should code to and provides the ad ...
- BZOJ-1864-[Zjoi2006]三色二叉树(树形dp)
Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...
- .NET Core多平台项目模板eShopOnContainers编译手记
之前写了一个功能性的文件上传asp.net core的小程序,加上点七七八八的东西,勉强能够应付了,打算学习一下微软的官方.NET CORE微服务示例https://github.com/dotnet ...
- 基于gitHub+hexo搭建的个人博客
文章导航 前期准备 安装hexo 修改hexo主题 自定义主题 部署本地文件到github查看 我的第一篇博客 前期准备 下载安装git命令行工具.node及npm环境 注册自己的GitHub账号 安 ...
- 微信小程序入门(前言)
最近接到一个开发微信小程序的任务,由于没有开发过小程序,所以只能查看官方文档.查找相关博文.资料来开发. 微信小程序一开始出现就受到热烈的追捧,因为其"无需安装.用完即走"的理念确 ...
- 项目实战3—Keepalived 实现高可用
实现基于Keepalived高可用集群网站架构 环境:随着业务的发展,网站的访问量越来越大,网站访问量已经从原来的1000QPS,变为3000QPS,目前业务已经通过集群LVS架构可做到随时拓展,后端 ...
- linux7.2系统中安装Nmon并使用
前提 安装linux系统中遇到一个问题,设置ip以后则ping不通,简单总结几步: 1.设置ip 进入 /etc/sysconfig/network-scripts目录下,修改文件名为ifcfg-en ...