人工智能实践:Tensorflow笔记14:卷积神经网络搭建

搭建如下的卷积神经网络:一层卷积和两层全连接的网络。使用6个5X5的卷积核,过2x2的池化和,池化的步长为2.最后一层是10,是因为输出的是10分类。

在这里插入图片描述

代码:

class Baseline(Model):
    def __init__(self):
        super(Baseline, self).__init__()
        self.c1 = Conv2D(filters=6, kernel_size=(5, 5), padding='same')  # 卷积层  6个卷积核,每个都是5*5的尺寸。 最后一个参数表示全零填充。
        self.b1 = BatchNormalization()  # BN层   批标准化
        self.a1 = Activation('relu')  # 激活层   激活函数
        self.p1 = MaxPool2D(pool_size=(2, 2), strides=2, padding='same')  # 池化层   2*2的池化,步长为2,使用全零填充。
        self.d1 = Dropout(0.2)  # dropout层   以0.2的比例休眠神经元。

        self.flatten = Flatten() # 拉直
        self.f1 = Dense(128, activation='relu')
        self.d2 = Dropout(0.2)
        self.f2 = Dense(10, activation='softmax')

    def call(self, x):
        #调用    输入和输出都过一次前向传播
        x = self.c1(x)
        x = self.b1(x)
        x = self.a1(x)
        x = self.p1(x)
        x = self.d1(x)

        x = self.flatten(x)
        x = self.f1(x)
        x = self.d2(x)
        y = self.f2(x)
        return y
原创文章 397 获赞 284 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/105674155