美文网首页
TensorFlow 概览

TensorFlow 概览

作者: Moscow1147 | 来源:发表于2018-07-08 09:53 被阅读0次
Grotta Palazzese 帕拉泽塞岩洞餐厅-波利尼亚诺

TensorFlow 本质上是一个大规模的运算框架,它的运算被抽象成一张运算矢量图。就像在这边看到一张运算图一样,上面的节点代表运算或者状态。当我们完成了一些运算或者结束了一些状态的时候,我们的数据就从一个节点流到另外一个节点。

TensorFlow 计算图

在 TensorFlow 中:

由图 (Graph) 表示计算任务,

图 (Graph) 中的节点 (node) 称之为 op (operation),节点一般执行某种抽象运算

图 (Graph) 中的边 (或线) (edge) 表示节点 op 输入/输出的数据流,数据流以张量 (tensor) 表示

通过变量 (Variable) 维护状态

使用 feed 和 fetch 为任意的操作 (arbitrary operation) 赋值或从中获取数据

TensorFlow是一种典型的基于符号编程的数值计算框架,从总架构上分为两层:

  1. 前端:负责计算图的构造,支持多语言,其中Python提供的 API最为成熟;
  2. 后端:负责计算图的执行,使用C++实现,直接操作分布式的CPU/GPU环境。

TensorFlow 的程序的运作过程也是包含两部分:

  1. 构建一个包含 tf.Tensor 对象的图,这个图详细的描述了每一个 Tensor 是怎样基于其它可得到的 Tensor 去计算的。
  2. 通过 Session 去启动图,运行部分子图去得到一个期望的结果。

Tensor 是向量、矩阵或者更高维度的代名词。在内部,Tensor 是 TensorFlow 的基本数据类型,代表 n 维数组。Tensor 代表了部分定义的极端,并最终产生一个值。

A tf.Tensor object represents aparticily defined computation that will eventually produce a value.

tf.Tensor 和 numpy 的 ndarray 很像。

Tensor 是同一种 datatype 的 n 维数组。Tensor 的数据类型可以是 float32、int32 或者是 string (string 可以说是 TF 的基本数据类型,可以是 Tensor 的单个数据元素)。虽然一个 Tensor 只有一种 datatype,但是可以将任何数据类型序列化为 string,并且存储到 tf.Tensor 中。使用 tf.case 可以将 tf.Tensor 从一种数据类型转换为另一种数据类型。从 Python 对象来创建 tf.Tensor 时,应该指定一个数据类型,虽然这是一个可选的,但是如果没有指定, TensorFlow 将自动进行类型转换,Python 中的 integer 转换成 tf.int32,float point number 转换成 tf.float32,其他转换与 numpy 的转换规则相同。

Tensor 也有秩 (rank)

Rank Math entity
0 Scalar (magnitude only)
1 Vector (magnitude and direction)
2 Matrix (table of numbers)
3 3-Tensor (cube of numbers)
n n-Tensor (you get the idea)

Tensor 也有 shape 属性,返回一个 TensorShape 对象,shape 可以用 Python 中的列表或者元组表示。如果输入的 Tensor 的 shape 是已知的,那么 op 也会产生一个已知 shape 的 Tensor ,但是在某些情况下,只能在 Graph 执行的时候才可以获知 Tensor 的 shape。使用tf.shape可以在 runtime 的时候动态的注入一个 tf.Tensor 依靠其 shape 来操纵一个 Tensor shape,从而获得一个指定 shape 的 Tensor。tf.Tensor 有 size 的概念,但是没有 size 属性,tf.Tensor 的 shape 可以通过 tf.reshape来动态的改变。

关于 切片 没有找到更多详细介绍,应该不会出现太大变化。

一些特殊类型的 Tensor :

  • tf.Variable
  • tf.Constant
  • tf.Planceholder
  • tf.SparseTensor

除了 tf.Variable 之外,其他 3 个 Tensor 的值是不变的 (immutable)。

With the exception of tf.Variable, the value of a tensor is immutable, which means that in the context of a single execution tensors only have a single value. However, evaluating the same tensor twice can return different values; for example that tensor can be the result of reading data from disk, or generating a random number.

Tensor-like objects:

  • tf.Tensor
  • tf.Variable
  • numpy.ndarry
  • list (and lists of tensor-like objects)
  • Scalar python types: bool float int str

tensor-like objects 在需要时均会隐式调用 tf.convert_to_tensor 方法,每一次调用就会生成一个新的 Tensor ,为了防止多次重复调用同一个 tensor-like object ,导致内存不足,应该手动显示调用该方法,而在之后均使用其生成的 Tensor。

Print Tensor

  • print (Tensor)

    很少使用,这种方法只会打印一个象征,没有实际值。

  • tf.Print(t, [t])

    打印第二个参数中的 Tensor 集,返回第一个参数的 Tensor。返回的Tensor 必须被使用,只有这样,才会有打印输出,否则该语句什么也不执行。因为图的执行,只执行依赖的子图。

Evaluate a Tensor

Tensor.eval() 这个方法仅在一个默认的会话 (Session) 是启动的时候工作,返回一个和 Tensor有相同 context 的 numpy 数组。

在 Python 中,op构造器的返回值代表该 op 的输出。

tf.constant(42.0) 代表创建了一个 op 添加到默认图中,并返回一个 tf.Tensor。

tf.Graph 对它所包含的 op 定义了一个 namespace ,TensorFlow 会自动的为其添加一个独一无二的 name ,但是也提供了两种方式去重写 op 的 name。

  • name 参数

    tf.constant(42.0, name="answer") 该语句创建了一个 answer 的 op ,并且返回了一个 “answer:0”(0 只代表产生序列)的 Tensor。如果已经存在 answer 的 op ,TensorFlow 将会改为 "answer_1""answer_2"来使其 name 是独一无二的。

  • tf.name_scope 函数

    为 op 指定 namespace ,添加一个以 namespace 前缀以/分割。

启动图

  1. tf.Session() 创建 Session 对象,如果没有任何参数将会启动默认默认图。
  2. tf.Session.run([], options, run_metadata)运行图,后面两个是可选参数,第一个列表是 fetch 的 Tensor 列表。
  3. tf.Session.close(),关闭会话释放资源。对于这个可以使用 with代码块,自动关闭会话。

相关文章

  • TensorFlow 概览

    TensorFlow 本质上是一个大规模的运算框架,它的运算被抽象成一张运算矢量图。就像在这边看到一张运算图一样,...

  • TensorFlow 概览

    TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。下面这张图就...

  • 《Scikit-Learn与TensorFlow机器学习实用指南

    第1章 机器学习概览 来源:ApacheCN《Sklearn 与 TensorFlow 机器学习实用指南》翻译项目...

  • TensorFlow On Windows

    概览 TensorFlow 是 Google 推出的机器学习开源平台,支持 CNN、RNN 和 LSTM 等深度神...

  • 第一天、Win10环境安装Pycharm+Anaconda+Te

    概览:环境为:Win10 重点讲解TensorFlow的安装以及测试安装是否成功。 一、安装Pycharm并配置A...

  • 第一讲:TensorFlow概览

    人生苦短,我用Python文:郑元春 之前在搜索TF的各种学习资料的时候,搜索的大部分结果都是直接开始上代码。虽然...

  • 在美团云上配置GPU服务器

    概览:0.GPU来源:美团云1.配置: 2.选择自带tensorflow(很关键)3.ubuntu系统4.其他:t...

  • 性能测试概览目录

    性能测试概览Ⅰ 性能测试概览Ⅱ 性能测试概览Ⅲ 性能测试概览Ⅳ

  • TensorFlow准备

    1、关于TensorFlow 相关网站:TensorFlow、TensorFlow中文社区 TensorFlow™...

  • Tensorflow初识及安装

    本章节目录 Tensorflow简介Tensorflow相关网站 Tensorflow的安装Tensorflow学...

网友评论

      本文标题:TensorFlow 概览

      本文链接:https://www.haomeiwen.com/subject/paanuftx.html