卷积神经网络之多输入多输出通道 学习笔记
github代码地址:https://github.com/taichuai/d2l_zh_tensorflow2.0
import tensorflow as tf
import os
# 一、多输入通道,单输出通道
# 输入形状为 h * w * channel
def corr2d(X, K):
c,h,w = K.shape
Y = tf.Variable(tf.zeros((X.shape[0] - h + 1, X.shape[1] - w +1, X.shape[2])))
Y_out = tf.Variable(tf.zeros((X.shape[0] - h + 1, X.shape[1] - w +1)))
for i in range(Y.shape[2]):
for j in range(Y.shape[1]):
for k in range(Y.shape[0]):
print(X[:,:,i])
# 注意这里切片方式,以 channel 通道方向切片,这里输出的 Y 也是 h * w * channel形式
Y[j, k,i].assign(tf.cast(tf.reduce_sum(X[j:j+h, k:k+w, i] * K[i,: ,:]), dtype=tf.float32))
# 得到的Y仍然是三个通道,然后求和合并成一个通道
Y_out = tf.reduce_sum(Y,axis=2)
return Y_out
X = tf.random.uniform(shape=(4,4,2),maxval=10, minval=1, dtype='int32')
print('X',X)
K = tf.constant([[[1, 0],[0,1]],[[1, 1],[1,0]]])
print('K',K)
Y = corr2d(X, K)
Y
多通道输出即:
# K.shape = c0 * c1 * h * w
def corr2d_multi_in_out(X, K):
return tf.stack([corr2d(X, k) for k in K],axis=0)
def corr2d_multi_in_out_1x1(X, K):
h, w, c_i = X.shape
c_o = K.shape[0]
# X展开
X = tf.reshape(X,(h * w, c_i))
# K 展开
K = tf.reshape(K,(c_o, c_i))
Y = tf.matmul(K, tf.transpose(X))
return tf.reshape(Y, (c_o, h, w))
经验证,做 1×1 卷积时,以上函数与之前实现的互相关运算函数corr2d_multi_in_out等价。在之后的模型里我们将会看到1×11×1卷积层被当作保持高和宽维度形状不变的全连接层使用。于是,我们可以通过调整网络层之间的通道数来控制模型复杂度。
X = tf.random.uniform((3,3,3))
K = tf.random.uniform((2,3,1,1))
Y1 = corr2d_multi_in_out_1x1(X, K)
Y2 = corr2d_multi_in_out(X, K)
print('Y1',Y1)
print('Y2',Y2)
tf.norm(Y1-Y2) < 1e-6