您的位置:首页 > 其它

深度学习学习笔记——部分API解析

2017-07-06 10:49 561 查看

持续更新

tf.slice(input_, begin, size, name=None)

作用:提取Tensor中的一片

参数解释:

- input_:表示输入的Tensor数据

- begin:n维列表,begin[i] 表示从inputs中第i维抽取数据时,相对0的起始偏移量,也就是从第i维的begin[i]开始抽取数据

- size:n维列表,size[i]表示要抽取的第i维元素的数目

- 关系式如下:

- i in [0,n]

- tf.shape(inputs)[0] = len(begin) = len(size)

- begin[i]>=0 抽取第i维原始的起始位置要大于等于0

- begin[i]+size[i]<=tf.shape(inputs)[i]

import tensorflow as tf
input = [[[1,1,1],[2,2,2]],
[[3,3,3],[4,4,4]],
[[5,5,5],[6,6,6]]]
begin = [1,0,0]
size = [2,1,3]
tf.slice(input,begin,size)==>[[[3,3,3]]
[[5,5,5]]]


解析如下:

- begin[0] = 1,表示的是第1维的起始位置是第2行(因为是以0位基准),也就是下面加粗的那行

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

【[3,3,3],[4,4,4]】,

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

- size[0] = 2,表示从第一维的起始位置开始取一维中的两个元素,也就是下面加粗的部分:

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

【[3,3,3],[4,4,4]】,

【[5,5,5],[6,6,6]】]

- begin[1] = 0,表示的是第2维的起始位置是下面加粗的位置:

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

[【3,3,3】,[4,4,4]],

[【5,5,5】,[6,6,6]]]

- size[1] = 1表示的是从第2维的起始位置开始取一个元素,也就是下面加粗的部分

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

[【3,3,3】,[4,4,4]],

[【5,5,5】,[6,6,6]]]

- begin[2] = 0,表示的是第3维的起始位置从下面加粗位置开始:

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

[[3,3,3],[4,4,4]],

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

- size[2] = 3,表示的是从第3维的起始位置开始取第3维中的3个元素,也就是下面加粗的几个元素

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

[[3,3,3],[4,4,4]],

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

tf.train.string_input_producer(string_tensor, num_epochs=None, shuffle=True, seed=None, capacity=32, shared_name=None, name=None, cancel_op=None)

作用:输出为输入管道输出一个字符串队列(例如:filenames)

注意:如果num_epochs不为空,则这个函数将创建一个本地计数器epochs,通过local_variable_initializer()去初始化这个本地变量。

输入参数:

1. string_tensor:一维字符串张量,就是一个filename。

2. num_epochs:一个可选整数,如果不为空,则表示最大训练迭代数,如果为空,则会循环变量string_tensor中的字符串无限次。

3. shuffle:默认为True类型,如果为True,就表示每次迭代字符串都是乱序的。

4. seed:可选整数,当shuffle==True时,seed才会起作用

5. capacity:一个整数,设置队列的最大容量

6. shared_name:可选,如果设置了,这个队列将在多个会话之间的给定名称下共享。

7. name:这个操作的名字(可选)

8. cancel_op:取消队列操作

返回值:输出具有字符串的队列。QueueRunner将添加到当前Graph的QUEUE_RUNNER集合中。

tf.nn.local_respons_normalization

参考博客:【TensorFlow】tf.nn.local_response_normalization详解,lrn正则法如何计算?

local response normalization最早是由Krizhevsky和Hinton在关于ImageNet的论文里面使用的一种数据标准化方法,即使现在,也依然会有不少CNN网络会使用到这种正则手段,现在记录一下lrn方法的计算流程以及tensorflow的实现,方便后面查阅



以上是这种归一手段的公式,其中a的上标指该层的第几个feature map,a的下标x,y表示feature map的像素位置,N指feature map的总数量,公式里的其它参数都是超参,需要自己指定的。

这种方法是受到神经科学的启发,激活的神经元会抑制其邻近神经元的活动(侧抑制现象),至于为什么使用这种正则手段,以及它为什么有效,查阅了很多文献似乎也没有详细的解释,可能是由于后来提出的batch normalization手段太过火热,渐渐的就把local response normalization掩盖了吧

tf.nn.local_response_normalization(input, depth_radius=None, bias=None, alpha=None, beta=None, name=None)

除去name参数用以指定该操作的name,与方法有关的一共五个参数:

第一个参数input:这个输入就是feature map了,既然是feature map,那么它就具有[batch, height, width, channels]这样的shape

第二个参数depth_radius:这个值需要自己指定,就是上述公式中的n/2

第三个参数bias:上述公式中的k

第四个参数alpha:上述公式中的α

第五个参数beta:上述公式中的β

返回值是新的feature map,它应该具有和原feature map相同的shape

我们可以写一个示例程序演示TensorFlow中tf.nn.local_response_normalization方法的计算过程

import tensorflow as tf

a = tf.constant([
[[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[8.0, 7.0, 6.0, 5.0],
[4.0, 3.0, 2.0, 1.0]],
[[4.0, 3.0, 2.0, 1.0],
[8.0, 7.0, 6.0, 5.0],
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0]]
])
#reshape a,get the feature map [batch:1 height:2 width:2 channels:8]
a = tf.reshape(a, [1, 2, 2, 8])

normal_a=tf.nn.local_response_normalization(a,2,0,1,1)
with tf.Session() as sess:
print("feature map:")
image = sess.run(a)
print (image)
print("normalized feature map:")
normal = sess.run(normal_a)
print (normal)


输出结果:

feature map:
[[[[ 1.  2.  3.  4.  5.  6.  7.  8.]
[ 8.  7.  6.  5.  4.  3.  2.  1.]]

[[ 4.  3.  2.  1.  8.  7.  6.  5.]
[ 1.  2.  3.  4.  5.  6.  7.  8.]]]]
normalized feature map:
[[[[ 0.07142857  0.06666667  0.05454545  0.04444445  0.03703704  0.03157895
0.04022989  0.05369128]
[ 0.05369128  0.04022989  0.03157895  0.03703704  0.04444445  0.05454545
0.06666667  0.07142857]]

[[ 0.13793103  0.10000001  0.0212766   0.00787402  0.05194805  0.04
0.03448276  0.04545454]
[ 0.07142857  0.06666667  0.05454545  0.04444445  0.03703704  0.03157895
0.04022989  0.05369128]]]]


这里我取了n/2=2,k=0,α=1,β=1,举个例子,比如对于一通道的第一个像素“1”来说,我们把参数代人公式就是1/(1^2+2^2+3^2)=0.07142857,对于四通道的第一个像素“4”来说,公式就是4/(2^2+3^2+4^2+5^2+6^2)=0.04444445,以此类推

tf.reduce_sum()

主要想介绍一下reduction_indices,以tf.reduce_sum(y_*tf.log(y),reduction_indices=[1])为例,用 tf.log 计算 y 的每个元素的对数。接下来,我们把 y_ 的每一个元素和 tf.log(y) 的对应元素相乘。最后,由于其reduction_indices = [1]我们将相乘后的值进行加法操作。

#This is something about what I understand of reduce_sum
import tensorflow as tf
x = [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]
sess = tf.Session()
print("0:")
print(sess.run(tf.reduce_sum(x,reduction_indices=0)))
print("1:")
print(sess.run(tf.reduce_sum(x,reduction_indices=1)))
print("2:")
print(sess.run(tf.reduce_sum(x,reduction_indices=2)))
sess.close()
=>
#0:
#[[ 8 10 12]
# [14 16 18]]
#1:
#[[ 5  7  9]
# [17 19 21]]
#2:
#[[ 6 15]
# [24 33]]


当reduction_indices=0时,我们将在0为的内部进行处理

【[[1,2,3],[4,5,6]]

[[7,8,9],[10,11,12]]】

第一维有一层,第二维有两层,第三维有四层,我们将第二维的两层相对加起来,也就是

1+7=8 2+8=10 3+9=12

4+10=14 5+11=16 6+12=18

当reduction_indices=1时,我们将在1维的内部进行处理

[【[1,2,3],[4,5,6]】

【[7,8,9],[10,11,12]】]

第三维的四层对应加起来,也就是

1+4=5 2+5=7 3+6=9

7+10=17 8+11=19 9+12=21

当reduction_indices=2时,我们将在2为的内部进行处理

[[【1,2,3】,【4,5,6】]

[【7,8,9】,【10,11,12】]]

三层内部,逗号之间的数字加起来,也就是

1+2+3=6 4+5+6=15

7+8+9=24 10+11+12=33

Tf.shape(x)

其中x可以是tensor,也可不是tensor,返回是一个tensor。

shape=tf.placeholder(tf.float32,shape=[None,227,227,3])
#在这个书中,如果在运行时想知道None是多少,这时候只能通过tf.shape(x)[0]来获得


tensor.get_shape()

只有tensor有这个方法,返回的是一个tuple
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  深度学习 api