使用TensorFlow v2.0构建卷积神经网络

使用TensorFlow v2.0构建卷积神经网络。这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制。CNN 概述MNIST 数据集概述此示例使用手写数字的MNIST数据集。该数据集包含60,000个用于训练的示例和10,000个用于测试的示例。这些数字已经过尺寸标准化并位于图像中心,图像是固定大小(28x28像素),值为0到255。在此示例中,每个图像将转换为...
摘要由CSDN通过智能技术生成

使用TensorFlow v2.0构建卷积神经网络。

这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制。

CNN 概述

MNIST 数据集概述

此示例使用手写数字的MNIST数据集。该数据集包含60,000个用于训练的示例和10,000个用于测试的示例。这些数字已经过尺寸标准化并位于图像中心,图像是固定大小(28x28像素),值为0到255。

在此示例中,每个图像将转换为float32并归一化为[0,1]。

更多信息请查看链接: http://yann.lecun.com/exdb/mnist/

from __future__ import absolute_import, division, print_function

import tensorflow as tf
import numpy as np
# MNIST 数据集参数
num_classes = 10 # 所有类别(数字 0-9)

# 训练参数
learning_rate = 0.001
training_steps = 200
batch_size = 128
display_step = 10

# 网络参数
conv1_filters = 32 # 第一层卷积层卷积核的数目
conv2_filters = 64 # 第二层卷积层卷积核的数目
fc1_units = 1024 # 第一层全连接层神经元的数目
# 准备MNIST数据
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 转化为float32
x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
# 将图像值从[0,255]归一化到[0,1]
x_train, x_test = x_train / 255., x_test / 255.
# 使用tf.data API对数据进行随机排序和批处理
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)
# 为简单起见创建一些包装器
def conv2d(x, W, b, strides=1):
    # Conv2D包装器, 带有偏置和relu激活
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME'
  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,关于使用卷积神经网络深度学习CIFAR-100数据集的问题,需要先进行以下数据预处理: 1. 将图像像素值进行归一化,缩放到 [0, 1] 范围内。 2. 对于每张图像,减去均值并除以标准差,对数据进行标准化。 3. 对于每张图像进行数据增强,包括随机水平翻转、随机裁剪等方法。 参考以下 TensorFlow 2.0 代码: ```python import tensorflow as tf from tensorflow.keras.datasets import cifar100 from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten from tensorflow.keras.optimizers import Adam from tensorflow.keras.utils import to_categorical from tensorflow.keras.preprocessing.image import ImageDataGenerator # 数据预处理 def preprocess(x, y): x = x / 255.0 x = (x - tf.reduce_mean(x)) / tf.math.reduce_std(x) y = to_categorical(y, num_classes=100) return x, y # 加载数据集 (x_train, y_train), (x_test, y_test) = cifar100.load_data() # 数据预处理 x_train, y_train = preprocess(x_train, y_train) x_test, y_test = preprocess(x_test, y_test) # 数据增强 datagen_train = ImageDataGenerator( width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, vertical_flip=False, zoom_range=0.3, rotation_range=30) # 构建模型 model = Sequential([ Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(32, 32, 3)), Conv2D(32, (3, 3), activation='relu', padding='same'), MaxPooling2D((2, 2)), Conv2D(64, (3, 3), activation='relu', padding='same'), Conv2D(64, (3, 3), activation='relu', padding='same'), MaxPooling2D((2, 2)), Conv2D(128, (3, 3), activation='relu', padding='same'), Conv2D(128, (3, 3), activation='relu', padding='same'), MaxPooling2D((2, 2)), Flatten(), Dense(256, activation='relu'), Dense(100, activation='softmax') ]) # 编译模型 model.compile(optimizer=Adam(learning_rate=1e-3), loss='categorical_crossentropy', metrics=['accuracy']) # 训练模型 history = model.fit(datagen_train.flow(x_train, y_train, batch_size=128), epochs=100, validation_data=(x_test, y_test)) # 输出准确率 test_loss, test_acc = model.evaluate(x_test, y_test) print('Test accuracy:', test_acc) ``` 使用以上代码后,可以得到大约 75% 的准确率。需要注意的是,由于本地环境的不同以及随机因素的影响,实际结果可能有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值