TF - 卷积神经网络实现

卷积神经网络设计主要是tensorflow网络层的定义和编写,本部分可能较多的涉及到相关函数的说明。

  • 卷积模块
tf.nn.conv2d(x, w, strides=[1,1,1,1], padding='SAME')

 * x 对应一个batch_size的图像,[ batch_size, img_height, img_width, channel ]   (channel_last 模式)
 * w 对应卷积核,[kernel_height, kernel_width, input_channels, output_channels]
 * strides 对应步长,即卷积核每一步前进的长度,[1, stride_height, stride_width, 1]
 * padding 对应same和valid模式

卷积封装模块

def conv2d(x, W, bias):
    conv = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME")
    return tf.nn.bias_add(conv, bias)
  • 池化模块
tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

 * ksize 池化核的大小
 * strides 池化核的步长
 * padding 周围是否补全  

池化封装模块

def max_pool(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
  • 激活层
tf.nn.relu(x)
  • softmax层
tf.nn.softmax(x, dim)

 * dim指定维度来进行softmax变换
 * default dim = -1 表示最后一个维度
**************** 举例说明 ****************

a = tf.ones([3,2], name="test", dtype=tf.float32)

b = tf.nn.softmax(a, dim=0)
# 第一个维度上的变换
# b= [[0.33, 0.33][0.33 0.33][0.33 0.33]]

b = tf.nn.softmax(a, dim=1)
# 第二个维度上的变换
# b =  [[ 0.5  0.5] [ 0.5  0.5] [0.5 0.5]]
#b = tf.nn.softmax(a, dim=-1)

常见函数的封装

  • tf.reduce_sum & tf.reduce_mean
tf.reduce_sum(x, axis = 0)
* 计算一个张量的各个维度上元素的总和

**************** 举例说明 ****************

x = tf.constant([[1, 1, 1], [1, 1, 1]])
b = tf.reduce_sum(x)
# 6
# 如果axis没有条目,则缩小所有维度,并返回具有单个元素的张量

b = tf.reduce_sum(x, axis = 0)
# [2 , 2, 2]
# shape [2,3] 把第一个维度衰减了 [1 3]
  • tf.argmax
tf.argmax(input, axis=None, name=None, dimension=None)
* 对矩阵按行或列计算最大值
* 返回行或列的最大值下标向量

**************** 举例说明 ****************

a=tf.get_variable(name='a',
                  shape=[3,4],
                  dtype=tf.float32,
initializer=tf.random_uniform_initializer(minval=-1,maxval=1))
b=tf.argmax(input=a,axis=0)
c=tf.argmax(input=a,dimension=1)  

print(sess.run(a))
#[[ 0.04261756 -0.34297419 -0.87816691 -0.15430689]
# [ 0.18663144  0.86972666 -0.06103253  0.38307118]
# [ 0.84588599 -0.45432305 -0.39736366  0.38526249]]
print(sess.run(b))
#[2 1 1 2]
print(sess.run(c))
#[0 1 0]
  • tf.cast
tf.cast(x, dtype, name=None)
* 类型转换函数
* dtype:转换目标类型
* name:名称

**************** 举例说明 ****************
# tensor `a` is [1.8, 2.2], dtype=tf.float
tf.cast(a, tf.int32) ==> [1, 2]  # dtype=tf.int32
  • tf.equal
A = [[1,3,4,5,6]]
B = [[1,3,4,3,2]]
print(sess.run(tf.equal(A, B)))
# [[ True  True  True False False]]

实际中的运用

correct_prediction=tf.equal(tf.argmax(y_out,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
  • tf.concat
# reference : https://blog.csdn.net/loseinvain/article/details/79638183
a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
ab1 = tf.concat([a,b], axis=0) # shape(4,3)
ab2 = tf.concat([a,b], axis=1) # shape(2,6)
  • tf.stack
a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
ab = tf.stack([a,b], axis=0) # shape (2,2,3)

猜你喜欢

转载自blog.csdn.net/weixin_34212189/article/details/87687607
今日推荐