1.0 TensorFlow graphs

Tensorflow是基于graph based computation:

如:

a=(b+c)∗(c+2)

可分解为

d=b+c

e=c+2

a=d∗e

这样做的目的在于,d=b+c,e=c+2,这两个式子就可以并行地计算。

2.0 A Simple TensorFlow example

import tensorflow as tf

# first, create a TensorFlow constant
const = tf.constant(2.0, name="const") # create TensorFlow variables
b = tf.Variable(2.0, name='b')
c = tf.Variable(1.0, name='c') # now create some operations
d = tf.add(b, c, name='d')
e = tf.add(c, const, name='e')
a = tf.multiply(d, e, name='a')

声明地常量和变量可以可选地使用一个string来标记,Tensorflow会根据初始值推断变量或常量地类型,也可以使用dtype参数来显式指定类型,如tf.float32tf.int32等,更多类型

注意:

It’s important to note that, as the Python code runs through these commands, the variables haven’t actually been declared as they would have been if you just had a standard Python declaration (i.e. b = 2.0).  Instead, all the constants, variables, operations and the computational graph are only created when the initialisation commands are run.

在执行上述python代码地时候,这些变量和常量并不像标准python那样立刻被声明,而是所有地常量、变量、运算和计算图(computational graph)必须在初始化命令执行时才会被创建。

# setup the variable initialisation
init_op = tf.global_variables_initializer()

To run the operations between the variables, we need to start a TensorFlow session – tf.Session.  The TensorFlow session is an object where all operations are run. TensorFlow was initially created in a static graph paradigm – in other words, first all the operations and variables are defined (the graph structure) and then these are compiled within the tf.Sessionobject.

为了执行变量间的运算,需要启动一个tf.Session,所有运算都是在session对象中运行的。TensorFlow首先创建静态图。

# start the session
with tf.Session() as sess:
# initialise the variables
sess.run(init_op)
# compute the output of the graph
a_out = sess.run(a)
print("Variable a is {}".format(a_out))

此处a是运算(operation)而不是变量,所以可以run。

Note something cool - 在a之前还声明了d和e,在计算a之前需要先执行d和e,我们不需要显式执行d和e,Tensorflow通过数据流图(data flow graph)知道a所有的依赖,自动执行依赖的运算。使用TensorBoard的功能可以看到TensorFlow创建的graph:

2.1 The TensorFlow placeholder

Let’s also say that we didn’t know what the value of the array b would be during the declaration phase of the TensorFlow problem (i.e. before the with tf.Session() as sess) stage.  In this case, TensorFlow requires us to declare the basic structure of the data by using the tf.placeholder variable declaration.  Let’s use it for b:

在Tensorflow训练问题声明阶段,我们可能不知道数组b的具体值。这种情况下,需要我们使用变量声明 tf.placeholder 来声明数据的基本结构。

# create TensorFlow variables
b = tf.placeholder(tf.float32, [None, 1], name='b')

第二个参数是data的shape,placeholder可以接收一个None的size参数。

a_out = sess.run(a, feed_dict={b: np.arange(0, 10)[:, np.newaxis]})

调用session.run时需要将b的具体数据feed进去。

Tensorflow_入门学习_1的更多相关文章

  1. Tensorflow_入门学习_2_一个神经网络栗子

    3.0 A Neural Network Example 载入数据: from tensorflow.examples.tutorials.mnist import input_data mnist ...

  2. vue入门学习(基础篇)

    vue入门学习总结: vue的一个组件包括三部分:template.style.script. vue的数据在data中定义使用. 数据渲染指令:v-text.v-html.{{}}. 隐藏未编译的标 ...

  3. Hadoop入门学习笔记---part4

    紧接着<Hadoop入门学习笔记---part3>中的继续了解如何用java在程序中操作HDFS. 众所周知,对文件的操作无非是创建,查看,下载,删除.下面我们就开始应用java程序进行操 ...

  4. Hadoop入门学习笔记---part3

    2015年元旦,好好学习,天天向上.良好的开端是成功的一半,任何学习都不能中断,只有坚持才会出结果.继续学习Hadoop.冰冻三尺,非一日之寒! 经过Hadoop的伪分布集群环境的搭建,基本对Hado ...

  5. PyQt4入门学习笔记(三)

    # PyQt4入门学习笔记(三) PyQt4内的布局 布局方式是我们控制我们的GUI页面内各个控件的排放位置的.我们可以通过两种基本方式来控制: 1.绝对位置 2.layout类 绝对位置 这种方式要 ...

  6. PyQt4入门学习笔记(一)

    PyQt4入门学习笔记(一) 一直没有找到什么好的pyqt4的教程,偶然在google上搜到一篇不错的入门文档,翻译过来,留以后再复习. 原始链接如下: http://zetcode.com/gui/ ...

  7. Hadoop入门学习笔记---part2

    在<Hadoop入门学习笔记---part1>中感觉自己虽然总结的比较详细,但是始终感觉有点凌乱.不够系统化,不够简洁.经过自己的推敲和总结,现在在此处概括性的总结一下,认为在准备搭建ha ...

  8. Retrofit 入门学习

    Retrofit 入门学习官方RetrofitAPI 官方的一个例子 public interface GitHubService { @GET("users/{user}/repos&qu ...

  9. MyBatis入门学习教程-使用MyBatis对表执行CRUD操作

    上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...

随机推荐

  1. Flutter实战视频-移动电商-46.详细页_自定义TabBar Widget

    46.详细页_自定义TabBar Widget 主要实现详情和评论的tab provide定义变量 自己做一个tab然后用provide去控制 定义两个变量来判断是左侧选中了还是右侧选中了.并定义一个 ...

  2. PHP中正则表达式学习及应用(三)

    正则表达式中的“模式修正符” 1.运算顺序    2.模式修正符 i 正则内容在匹配时候不区分大小写(默认是区分的) 例如: <?php $mode="/[a-z]/i"; ...

  3. Python3.6 字典的内置方法

    1.dict.clear(self) 删除字典内所有元素 2.dict.copy(self) 返回一个字典的浅复制,拷贝父对象,不会拷贝对象的内部的子对象 3.dict.fromkeys(self,  ...

  4. C#项目中一些文件类型说明

    designer.cs  是窗体设计器生成的代码文件,作用是对窗体上的控件做初始化工作 (在函数InitializeComponent()中)VS2003以前都把这部分代码放到窗体的cs文件中,由于这 ...

  5. 洛谷 - P1414 - 又是毕业季II - 因数

    https://www.luogu.org/problemnew/show/P1414 以后这种gcd的还是尽可能往分解那里想一下. 先把每个数分解,他的所有因子都会cnt+1. 然后从最大的可能因子 ...

  6. 洛谷 - P3377 - 【模板】左偏树(可并堆) - 左偏树 - 并查集

    https://www.luogu.org/problemnew/show/P3377 左偏树+并查集 左偏树维护两个可合并的堆,并查集维护两个堆元素合并后可以找到正确的树根. 关键点在于删除一个堆的 ...

  7. HDU - 1715 - 大菲波数 - JAVA

    http://acm.hdu.edu.cn/showproblem.php?pid=1715 import java.io.*; import java.util.*; import java.mat ...

  8. Codeforces 749C【模拟】

    FST的时候好像挂了挺多人的~ 其实思路没啥难的,就是更好地理解题意吧,1到n一直循环,直到没有人能vote,一个人能vote也能叉掉一个人,一个人被叉就不能vote,判谁赢. 其实我管vote干嘛, ...

  9. bzoj 3653: 谈笑风生【dfs序+主席树】

    考虑b的两种情况,一种是p的祖先,这种点有min(k,de[p]-1)个,然后每个这种b都有si[p]-1个c点可选: 另一种是p的子孙,要求是在p的子树内且deep在de[p]+1~de[p]+k之 ...

  10. iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)

    由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付功能加到了自己的代码中.一些根据文档来 ...