动手学深度学习学习笔记tf2.0版(5.3 多输入通道和多输出通道)

卷积神经网络之多输入多输出通道 学习笔记

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值