<jupyter_start><jupyter_text>使用卷积神经网络对CIFAR-10数据集进行分类<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

print(tf.__version__)<jupyter_output>2.7.0<jupyter_text>下载数据集<jupyter_code>datasets = tf.keras.datasets.cifar10
(x_train, y_train), (x_test, y_test) = datasets.load_data()<jupyter_output><empty_output><jupyter_text>查看数据集信息<jupyter_code>print("Training data shape: ", x_train.shape)
print("Training label shape: ", y_train.shape)
print("Test data shape: ", x_test.shape)
print("Test labels shape: ", y_test.shape)<jupyter_output>Training data shape:  (50000, 32, 32, 3)
Training label shape:  (50000, 1)
Test data shape:  (10000, 32, 32, 3)
Test labels shape:  (10000, 1)<jupyter_text>查看单项image和label<jupyter_code>plt.imshow(x_train[6])<jupyter_output><empty_output><jupyter_text>查看对应的标签值<jupyter_code>y_train[6]<jupyter_output><empty_output><jupyter_text>定义标签字典 每一个数字所代表的图像类别的名称<jupyter_code>label_dict = {0: "airplane", 1: "automobile", 2: "bird", 3: "cat", 4: "deer", 5: "dog", 6: "frog", 7: "horse",
              8: "ship", 9: "truck"}
label_dict[y_train[6][0]]<jupyter_output><empty_output><jupyter_text>数据预处理<jupyter_code># 特征数据标准化
x_train[0][0][0]
# 对图像image进行数字标签化
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0<jupyter_output><empty_output><jupyter_text>定义网格结构+ 图像特征提取：通过卷积层1，降采样层1，卷积层2以及降采样层2的处理，提取图像的特征+ 全连接神经网路：全连接层、输出层所组成的网络结构 | 输入层                | 卷积层1                                    | 降采样层1                                      | 卷积层2                                     | 降采样层2                                    | 全连接层                                 | 输出层                      ||--------------------|-----------------------------------------|--------------------------------------------|------------------------------------------|------------------------------------------|--------------------------------------|--------------------------||  32*32图像，通道为3（RGB） | 第1次卷积：输入通道：3，输出通道：32，卷积后图像尺寸不变，依然是32*32 | 第一次降采样：将32*32图像缩小为16*16；池化不改变通道数量，因此依然是32个 | 第2次卷积：输入通道：32，输出通道：64，卷积后图像尺寸不变，依然是16*16 | 第二次降采样：将16*16图像缩小为8*8；池化不改变通道数量，因此依然是64个 | 将64个8*8的图像转换为长度为4096的一维向量，该层有128个神经元 | 输出层共有10个神经元，对应到0-9这10个类别 |<jupyter_code># 建立卷积神经网络CNN模型
model = tf.keras.models.Sequential()

# 第1个卷积层
model.add(tf.keras.layers.Conv2D(filters=32,
                                 kernel_size=(3, 3),
                                 input_shape=(32, 32, 3),
                                 activation="relu",
                                 padding="same"))

# 防止过拟合
model.add(tf.keras.layers.Dropout(rate=0.3))

# 第1个池化层（将32*32的图像缩小为16*16，池化不改变通道数量）
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))

# 第2个卷积层
model.add(tf.keras.layers.Conv2D(filters=64,
                                 kernel_size=(3, 3),
                                 activation="relu",
                                 padding="same"))

# 防止过拟合
model.add(tf.keras.layers.Dropout(rate=0.3))

# 第2个池化层（将16*16的图像缩小为8*8，池化不改变通道数量）
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))

# 平坦层（将64个8*8的图像转化为长度是4096的一维向量）
model.add(tf.keras.layers.Flatten())

# 全连接层
model.add(tf.keras.layers.Dense(128, activation="relu"))
model.add(tf.keras.layers.Dropout(rate=0.3))

model.add(tf.keras.layers.Dense(64, activation="relu"))
model.add(tf.keras.layers.Dropout(rate=0.3))

model.add(tf.keras.layers.Dense(32, activation="relu"))
model.add(tf.keras.layers.Dropout(rate=0.3))

# 添加输出层
model.add(tf.keras.layers.Dense(10, activation="softmax"))
model.summary()<jupyter_output>Model: "sequential_2"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_4 (Conv2D)           (None, 32, 32, 32)        896       
                                                                 
 dropout_4 (Dropout)         (None, 32, 32, 32)        0         
                                                                 
 max_pooling2d_4 (MaxPooling  (None, 16, 16, 32)       0         
 2D)                                                             
                                                                 
 conv2d_5 (Conv2D)           (None, 16, 16, 64)        18496     
                                                                 
 dropout_5 (Dropout)         (None, 16, 16, 64)        0         
                                                                 
 max_pooling2d_5 (MaxPooling  (None, 8, 8, 64)        [...]<jupyter_text>模型训练<jupyter_code># 设置训练参数
train_epochs = 50
batch_size = 100
# 设置训练模式
model.compile(optimizer="adam",
              loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])
# 训练模型
train_history = model.fit(x=x_train,
                          y=y_train,
                          epochs=train_epochs,
                          validation_split=0.2,
                          batch_size=batch_size,
                          verbose=1)
def show_train_history(train_history, train_metric, val_metric):
    plt.plot(train_history.history[train_metric])
    plt.plot(train_history.history[val_metric])

    plt.title("Train History")
    plt.xlabel("Epochs")
    plt.ylabel(train_metric)

    plt.legend(["Train", "Validation"], loc="upper left")

    plt.show()
show_train_history(train_history, "loss", "val_loss")
show_train_history(train_history, "accuracy", "val_accuracy")<jupyter_output><empty_output><jupyter_text>评估模型<jupyter_code>test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)

print("Test accuracy: ", test_acc)
## 应用模型
# 进行预测
test_pred = model.predict(x_test)

test_pred.shape
np.argmax(test_pred[0])
plt.imshow(x_test[0])
y_test[0]
label_dict[y_test[0][0]]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17476231633535005, 'fraction_numerical': 0.047018150388936905, 'mean_word_length': 2.090811965811966, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '533117', 'license': 'MIT', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2334, 'n_words': 252, 'path': 'Course/2.0/Example/7-CIFAR.ipynb', 'repo_name': 'PhoenixNest/Hello-Tensorflow', 'size': '5785'}