下面是一个没有池化层的卷积神经网络的示例代码,第一层卷积使用 2x2 卷积核,用来识别 MNIST 数据集。

import tensorflow as tf

# 定义输入层
inputs = tf.keras.Input(shape=(28, 28, 1))

# 定义第一层卷积
x = tf.keras.layers.Conv2D(filters=32, kernel_size=(2, 2), activation='relu', padding='same')(inputs)

# 定义第二层卷积
x = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x)

# 定义全连接层
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(units=128, activation='relu')(x)

# 定义输出层
outputs = tf.keras.layers.Dense(units=10, activation='softmax')(x)

# 创建模型
model = tf.keras.Model(inputs=inputs, outputs=outputs)

# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# 打印模型结构
model.summary()

该模型的输入是 28x28 的灰度图像,输出是 10 个类别的概率分布。第一层卷积使用了 2x2 的卷积核,过滤器数为 32。第二层卷积使用了 3x3 的卷积核,过滤器数为 64。全连接层包含 128 个神经元。模型使用了 softmax 激活函数和交叉熵损失函数。

无池化层卷积神经网络识别MNIST数据集

原文地址: https://www.cveoy.top/t/topic/ncpA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录