学习目录
1.tensorflow相关函数理解
(1)tf.nn.conv2d
(2)tf.nn.relu
(3)tf.nn.max_pool
(4)tf.nn.dropout
(5)tf.nn.sigmoid_cross_entropy_with_logits
(6)tf.nn.truncated_normal
(7)tf.nn.constant
(8)tf.nn.placeholder
(9)tf.nn.reduce_mean
(10)tf.nn.squared_difference
(1)tf.nn.square
2.tensorflow相关类的理解
(1)tf.Variavle
tf.nn.conv2d(tf.nn.conv2d是TensorFlow里面实现卷积的函数)
参数列表
conv2d(
input,
filter,
strides,
padding,
use_cudnn_on_gpu=True,
data_format='NHWC',
name=None
)
| 参数名 | 必选 | 类型 | 说明 |
|---|---|---|---|
| input | 是 | tensor | 是一个 4 维的 tensor,即 [ batch, in_height, in_width, in_channels ](若 input 是图像,[ 训练时一个 batch 的图片数量, 图片高度, 图片宽度, 图像通道数 ]) |
| filter | 是 | tensor | 是一个 4 维的 tensor,即 [ filter_height, filter_width, in_channels, out_channels ](若 input 是图像,[ 卷积核的高度,卷积核的宽度,图像通道数,卷积核个数 ]),filter 的 in_channels 必须和 input 的 in_channels 相等 |
| strides | 是 | 列表 | 长度为 4 的 list,卷积时候在 input 上每一维的步长,一般 strides[0] = strides[3] = 1 |
| padding | 是 | string | 只能为 " VALID "," SAME " 中之一,这个值决定了不同的卷积方式。VALID 丢弃方式;SAME:补全方式 |
| use_cudnn_on_gpu | 否 | bool | 是否使用 cudnn 加速,默认为 true |
| data_format | 否 | string | 只能是 " NHWC ", " NCHW ",默认 " NHWC " |
| name | 否 | string | 运算名称 |
以下的动画是我们以一张彩色的图片为例子,彩色图的通道数为3(黑白照的通道数为1),所以每张彩色的图片就需要3个filter,每个filter中的权重都不同,最后输出的值为各自所对应相乘相加即可。
实例:
import tensorflow as tf a = tf.constant([1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,1,1,0,0],dtype=tf.float32,shape=[1,5,5,1]) b = tf.constant([1,0,1,0,1,0,1,0,1],dtype=tf.float32,shape=[3,3,1,1]) c = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='VALID') d = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='SAME') with tf.Session() as sess: print ("c shape:") print (c.shape) print ("c value:") print (sess.run(c)) print ("d shape:") print (d.shape) print ("d value:") print (sess.run(d))
运行结果如下:
1 c shape: 2 (1, 3, 3, 1) 3 c value: 4 [[[[ 4.] 5 [ 3.] 6 [ 4.]] 7 8 [[ 2.] 9 [ 4.] 10 [ 3.]] 11 12 [[ 2.] 13 [ 3.] 14 [ 4.]]]] 15 d shape: 16 (1, 5, 5, 1) 17 d value: 18 [[[[ 2.] 19 [ 2.] 20 [ 3.] 21 [ 1.] 22 [ 1.]] 23 24 [[ 1.] 25 [ 4.] 26 [ 3.] 27 [ 4.] 28 [ 1.]] 29 30 [[ 1.] 31 [ 2.] 32 [ 4.] 33 [ 3.] 34 [ 3.]] 35 36 [[ 1.] 37 [ 2.] 38 [ 3.] 39 [ 4.] 40 [ 1.]] 41 42 [[ 0.] 43 [ 2.] 44 [ 2.] 45 [ 1.] 46 [ 1.]]]]