Tensorflow进阶笔记 --- #"1"#

Network
• 阅读 3469

丛零开始

看了一篇入门级的文章 Tensorflow学习笔记2:About Session, Graph, Operation and Tensor,讲的很简单,清楚,如果刚开始接触tensorflow看一下就会很明白怎么去跑一个最简单的网络,如下这种网络。

import tensorflow as tf
a = tf.constant(1)
b = tf.constant(2)
c = tf.constant(3)
d = tf.constant(4)
add1 = tf.add(a, b)
mul1 = tf.mul(b, c)
add2 = tf.add(c, d)
output = tf.add(add1, mul1)
with tf.Session() as sess:
    print sess.run(output)

Tensorflow进阶笔记 --- #

tensorflow可以有两种入口的方式

  1. session
  2. tf.app.run

session

一个Session可能会拥有一些资源,例如Variable或者Queue。当我们不再需要该session的时候,需要将这些资源进行释放。有两种方式,

1. 调用session.close()方法;
2. 使用with tf.Session()创建上下文(Context)来执行,当上下文退出时自动释放。

使用session的方式也很好理解,先建立图,再去定义变量,执行。

import tensorflow as tf

# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b

# Launch the graph in a session.
sess = tf.Session()

# Evaluate the tensor 'c'.
print sess.run(c)
sess.close()

# result: [3., 8.]

上面第二点提到可以创建上下文,这样的方式更有利于自动清理变量

import tensorflow as tf

# Build a graph.
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b

with tf.Session() as sess:
    print sess.run(c)

tf.app.run

看到很多源代码中都会出现

if __name__ == "__main__":
    tf.app.run()

这样的代码其实是在执行解析flag后再去执行main函数,源代码如下

# tensorflow/tensorflow/python/platform/default/_app.py  
  
# Copyright 2015 Google Inc. All Rights Reserved.  
#  
# Licensed under the Apache License, Version 2.0 (the "License");  
# you may not use this file except in compliance with the License.  
# You may obtain a copy of the License at  
#  
#     http://www.apache.org/licenses/LICENSE-2.0  
#  
# Unless required by applicable law or agreed to in writing, software  
# distributed under the License is distributed on an "AS IS" BASIS,  
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
# See the License for the specific language governing permissions and  
# limitations under the License.  
# ==============================================================================  
  
"""Generic entry point script."""  
from __future__ import absolute_import  
from __future__ import division  
from __future__ import print_function  
  
import sys  
  
from tensorflow.python.platform import flags  
  
  
def run(main=None):  
  f = flags.FLAGS  
  f._parse_flags()  
  main = main or sys.modules['__main__'].main  
  sys.exit(main(sys.argv))  
  

在同一个python文件下,可以def一个main函数,run的时候就会去找这个main函数执行。

def main(unused_argv):
    test()
    
点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
tensorflow目标检测API安装及测试
1.环境安装配置1.1安装tensorflow安装tensorflow不再仔细说明,但是版本一定要是1.91.2下载TensorflowobjectdetectionAPI下载地址:https://github.com/tensorflow/models1.3 Protobuf 的安装与配置
GoCoding GoCoding
4年前
TensorFlow 的 JupyterLab 环境
TensorFlow准备JupyterLab交互式笔记本环境,方便我们边写代码、边做笔记。基础环境以下是本文的基础环境,不详述安装过程了。Ubuntuubuntu18.04.5desktopamd64.isoCUDAcuda11.2.2460.32.03linux.runlibcudnn88.1.1.331cuda11.
Wesley13 Wesley13
3年前
4. Tensorflow的Estimator实践原理
1\.Tensorflow高效流水线Pipeline(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cnblogs.com%2Fhuangyc%2Fp%2F10340766.html)2\.Tensorflow的数据处理中的Dataset和Iter
Wesley13 Wesley13
3年前
2019年20款热门机器学习相关开源项目
1\.TensorFlowStar126932Watch8582Fork74374Githubhttps://github.com/tensorflow/tensorflow是一个基于数据流编程(dataflowprogramming)的符号数学系统,被广泛应用于各类机器学习(m
Easter79 Easter79
3年前
Tensorflow2.0
Tensorflow2.01.Tensorflow简介1.Tensorflow是什么1.Google开源软件库1.采用数据流图,用于数值计算2.支
Easter79 Easter79
3年前
Tensorflow源码解析1
1主流深度学习框架对比当今的软件开发基本都是分层化和模块化的,应用层开发会基于框架层。比如开发LinuxDriver会基于Linuxkernel,开发Androidapp会基于AndroidFramework。深度学习也不例外,框架层为上层模型开发提供了强大的多语言接口、稳定的运行时、高效的算子,以及完
Stella981 Stella981
3年前
Linux下安装tensorflow
说明:文章中很多内容都是复制黏贴的,写的不是很好,但是发现这是我博客园里头阅读量最高的文章,为避免误人子弟还是先说明一下吧。\toc\Linux下安装tensorflow安装添加tensorflow的环境。执行命令:condacreatentensorflowpytho
Easter79 Easter79
3年前
Tensorflow开篇:环境安装2—tensorflow1.10.0
前提已经安装pyhon环境,具体可以参考Tensorflow开篇环境安装(python3.5.2)(https://my.oschina.net/zhys513/blog/edit/863579)tensorflow官网:http://tensorflow.google.cn/(https://www.oschina.net/action/Go
Wesley13 Wesley13
3年前
AI运行环境的搭建
安装tensorflow安装环境为CENTOS6.8操作系统,pip安装tensorflow后提示GLIBC版本过低。考虑到升级GLIBC有一定的风险,所以决定使用编译安装的方式安装tensorflow。基本流程是按照这篇教程:http://www.jianshu.com/p/fdb7b54b616e/(https://w
Easter79 Easter79
3年前
Tensorflow2.0全网最新教程来啦
Tensorflow2.0来啦,废话不多说,直接介绍Tensorflow2.0介绍:tensorflow是GOOGLE在2015年底发布的一款深度学习框架,也是目前全世界用得最多,发展最好的深度学习框架。2019年3月8日,GOOGLE发布最新tensorflow2版本。新版本的tensorflow有很多新特征,更快
Easter79 Easter79
3年前
Tensorflow.cifar_数据下载过程(数据输出)
1、环境:Win7x64、python3.7x64、tensorflow1.14、CPUi59400F2、3、 3.1、cifar10,没有数据,全新下载,下到默认目录(C:\\Users\\Administrator\\tensorflow\_datasets),全过程控制台输出:(20190903)"C:\ProgramF