深度学习(一)

1、线性回归的概念

(一)假设均值函数为mean(values),它可以表述为:

mean(values) = sum(values)  /  count(values)

1、上编写tensorflow可以总结为两步.
(1一个graph;
(2用session去执行graph中的operation
下面是 graph , session , operation , tensor 四个概念的简介。
Tensor:类型化的多维数组,图的边;
Operation:执行计算的单元,图的节点;
Graph:一张有边与点的图,其表示了需要进行计算的任务;
Session:称之为会话的上下文,用于执行图。
Graph仅仅定义了所有 operation 与 tensor 流向,没有进行任何计算。而session根据 graph 的定义分配资源,计算 operation,得出结果。既然是图就会有点与边,在图计算中 operation 就是点而 tensor 就是边。Operation 可以是加减乘除等数学运算,也可以是各种各样的优化算法。每个 operation 都会有零个或多个输入,零个或多个输出。 tensor 就是其输入与输出,其可以表示一维二维多维向量或者常量。而且除了Variables指向的 tensor 外所有的 tensor 在流入下一个节点后都不再保存。

 

2、数据结构
Tensorflow的数据结构有着rank,shape,data types的概念
(1)rank
Rank一般是指数据的维度,其与线性代数中的rank不是一个概念
Rank Math entity Python example
0 Scalar(magnitude only) s = 483
1 Vector(magnitude and direction) v = [1.1, 2.2, 3.3]
2 Matrix(table of numbers) m = [[1, 2, 3], [4, 5, 6], [7, 8, 9,]]
3 3-Tensor(cube of numbers) t = [[[2], [4], [6]], [[8], [10], [12]],]
n n-Tensor(you get the idea) ....

总结:[代表一维数据,[[代表二维数据,[[[代表三维数据
(2)shape
Shape指tensor每个维度数据的个数,可以用python的list/tuple表示。下面表示了rank,shape的关系。
Rank Shape Dimension number Example
0 [] 0-D A 0-D tensor A scalar
1 [D0] 1-D A 1-D tensor with shape[5]
2 [D0,D1] 2-D A 2-D tensor with shape[3,4]
3 [D0,D1,D2] 3-D A 3-D tensor with shape[1,4,3]
n [D0,D1,...DN-1] n-D A tensor with shape[D0,D1,....Dn-1]
(3) data type
Dta type,是指单个数据的类型。常用DT_FLOAT,也就是32位的浮点数。下面有的types
Data type Python type Description
DT_FLOAT tf.float32 32 bits floating point
DT_INT32 tf.int32 32 bits signed integer
DT_QIT32 tf.qint32 32 bits signed integer used in quantized Ops

3、Variables
当训练模型时,需要使用Variables保存与更新参数。Variables会保存在内存当中,所有tensor一旦拥有Variables的指向就不会在session中丢失。其必须明确的初始化而且可以通过Saver保存到磁盘上。Variables可以通过Variables初始化。
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),name="weights")
biases = tf.Variable(tf.zeros([200]), name="biases")
其中,tf.random_normal是随机生成一个正态分布的tensor,其shape是第一个参数,stddev是其标准差。tf.zeros是生成一个全零的tensor。之后将这个tensor的值赋值给Variable。


4、在TensorFlow中有14种不同的类型
类型 描述符
实数 tf.float32 : 32-bit single-precision floating-point.
tf.float64: 64-bit double-precision floating-point.
tf.bfloat16: 16-bit truncated floating-point.
整数 tf.int8: 8-bit signed integer.
tf.uint8: 8-bit unsigned integer
tf.int32: 32-bit signed integer
tf.int64: 64-bit signed integer.
tf.qint8: Quantized 8-bit signed integer.
tf.quint8: Quantized 8-bit unsigned integer
tf.qint32: Quantized 32-bit signed integer
布尔 tf.bool: Boolean.
复数 tf.complex64: 64-bit single-precision complex.

5、会话模式–通过Python上下文管理器
with tf.Session() as sess:
sess.run(...)

下载数据到mnist_data目录下面
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/',one_hot=True)

 

posted @ 2018-08-22 21:36  python坚持者  阅读(246)  评论(0编辑  收藏  举报