深度学习-2:tensorflow 入门以及简单的线性拟合

摘要:

  1. 介绍 tensorflow 入门
  2. 使用 tensorflow 实现简单的线性回归

tensorflow 入门知识

参考: https://www.tensorflow.org/get_started/get_started

载入 tensorflow 的标准语句:

import tensorflow as tf

tensor

tensor 物理上的翻译是 张量。 tensor 是 tensorflow 的基本核心数据单元,tensor 可以理解为由数据组成的多维数组,维度可以是 1 维,2 维, 3 维等等。如下所示:

3 # a rank 0 tensor; this is a scalar with shape []
[1., 2., 3.] # a rank 1 tensor; this is a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

tensorflow 表面意思应该就是 tensor 的流动,实际的计算中,貌似确实蕴含了这种意思。就像 tensor 就像是水流一般,按照人们规定的路线进行流动,在规定的路线上,有很多关卡,用于对 tensor 进行相应的处理,得到最终的答案。

node

tensorflow 程序可以分为两部分:

  1. 构建计算流图
  2. 运行计算流图

一个计算流图(computaional graph)由很多节点(node)组成。


深度学习-2:tensorflow 入门以及简单的线性拟合_第1张图片
图 1

图 1 所示的计算流图用于进行线性拟合。 其中包含好几种 node

常数节点 constant node

如下所示,定义了两种常数节点:

node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # tf.float32 是默认的类型
print(node1, node2)

输出结果为:

Tensor("Const_13:0", shape=(), dtype=float32) Tensor("Const_14:0", shape=(), dtype=float32)

可以看到,两者类型是 Const,输出值为 0。并不是 3.0 和 4.0。 为什么是这样,可以理解为,这两个节点,还没被激活,所以其输出结果默认为 0。

定义了两个结点,相当于我们构建了一个计算流图,如何运行这个计算流图?需要我们在一个 Session 对象中去运行。

sess = 

你可能感兴趣的:(深度学习,tensorflow,深度学习,tensorflow,线性回归)