RNN识别PTB数据代码精解

1.Python中with用法

  with是一种集成的处理异常方法,比如:打开文件时,有可能出现异常,就像c中需要try catch排除问题一样

       http://linbo.github.io/2013/01/08/python-with

  因为有例子,很便于理解。但是最后一个例子没看懂

2.基本概念解释

  2.1 Tensor 张量的含义

      张量就是一种维度的表示

      张量英语:tensor)是一个可用来表示在一些矢量标量和其他张量之间的线性关系的多线性函数

      在同构的意义下,第零阶张量({\displaystyle r=0}r=0)为标量,第一阶张量({\displaystyle r=1}r=1)为矢量, 第二阶张量{\displaystyle r=2}r=2)则成为矩阵。例如,对于3维空间,{\displaystyle r=1}r=1时的张量为此矢量:{\displaystyle \left(x,y,z\right)^{\mathrm {T} }}\left( x,y,z \right)^\mathrm{T}

  2.2 对 TensorFlow本身的理解

        https://zhuanlan.zhihu.com/p/45476929

  没有细看,但是它进行了类比,感觉挺好的

3.Python代码解析

          TensorFlow是一个开源软件库,用于各种感知和语言理解任务的机器学习,所以本身相当于函数的封装集合。

  函数简介:
  一般都会有这句话,所以tf代表了TensorFlow的函数,TensorFlow本身

  import tensorflow as tf

  3.1 tf.convert_to_tensor

  用于将不同数据变成张量:比如可以让数组变成张量、也可以让列表变成张量。

  代码中用处:raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32)

 

 

 

  3.2  tf.name_scope

通过 with tf.name_scope(‘conv1’) as scope: 可以将scope 内生成的Variable 自动命名为 conv1/xxx,便于区分不同卷积层之间的组件。

  代码中用处:with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]):对下面的循环体进行命名

  3.3  tf.size(input, name=None)

  返回数据的元素数量

  # ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]

  size(t) ==> 12

 

  3.4 /  &  //

  /是除法,不管除数和被除数是不是整数,最后结果都是float

  //是地板除法,先做除法,再向下取整,两个都是int结果也是int,至少一方是float,结果是float

 

  3.5 tf.reshape(tensor, shape, name=None)

  改变tensor的形状

  # tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]

  # tensor ‘t’ has shape [9]

  reshape(t, [3, 3]) ==>

  [[1, 2, 3],

  [4, 5, 6],

  [7, 8, 9]]

  #如果shape有元素[-1],表示在该维度打平至一维

  # -1 将自动推导得为 9:

  reshape(t, [2, -1]) ==>

  [[1, 1, 1, 2, 2, 2, 3, 3, 3],

  [4, 4, 4, 5, 5, 5, 6, 6, 6]]

  关于-1的说法,没看懂

1.Python中with用法

  with是一种集成的处理异常方法,比如:打开文件时,有可能出现异常,就像c中需要try catch排除问题一样

       http://linbo.github.io/2013/01/08/python-with

  因为有例子,很便于理解。但是最后一个例子没看懂

2.基本概念解释

  2.1 Tensor 张量的含义

      张量就是一种维度的表示

      张量(英语:tensor)是一个可用来表示在一些矢量标量和其他张量之间的线性关系的多线性函数

      在同构的意义下,第零阶张量({\displaystyle r=0})为标量,第一阶张量({\displaystyle r=1})为矢量 二阶张量{\displaystyle r=2})则成为矩阵。例如,对于3维空间,{\displaystyle r=1}时的张量为此矢量:{\displaystyle \left(x,y,z\right)^{\mathrm {T} }}

  2.2 对 TensorFlow本身的理解

        https://zhuanlan.zhihu.com/p/45476929

  没有细看,但是它进行了类比,感觉挺好的

 

3.Python代码解析

          TensorFlow是一个开源软件库,用于各种感知和语言理解任务的机器学习,所以本身相当于函数的封装集合。

  函数简介:
  一般都会有这句话,所以tf代表了TensorFlow的函数,TensorFlow本身

  import tensorflow as tf

  3.1 tf.convert_to_tensor

  用于将不同数据变成张量:比如可以让数组变成张量、也可以让列表变成张量。

  代码中用处:raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32)

 

 

 

  3.2  tf.name_scope

通过 with tf.name_scope(‘conv1’) as scope: 可以将scope 内生成的Variable 自动命名为 conv1/xxx,便于区分不同卷积层之间的组件。

  代码中用处:with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]):对下面的循环体进行命名

3.3  tf.size(input, name=None)

返回数据的元素数量

# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]

size(t) ==> 12

3.4 /  &  //

/是除法,不管除数和被除数是不是整数,最后结果都是float

//是地板除法,先做除法,再向下取整,两个都是int结果也是int,至少一方是float,结果是float

3.5 tf.reshape(tensor, shape, name=None)

改变tensor的形状

# tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]

# tensor ‘t’ has shape [9]

reshape(t, [3, 3]) ==>

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

#如果shape有元素[-1],表示在该维度打平至一维

# -1 将自动推导得为 9:

reshape(t, [2, -1]) ==>

[[1, 1, 1, 2, 2, 2, 3, 3, 3],

[4, 4, 4, 5, 5, 5, 6, 6, 6]]

关于-1的说法,没看懂

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/Marigolci/p/10923631.html
今日推荐