tensorflow实现卷积神经网络训练mnist手写数据集

使用tensorflow实现使用卷积神经网络训练mnist手写数据集并符合以下要求:1.画出训练过程中训练集正确率表现图。2.画出训练过程中训练集损失函数表现图。3.训练正确率要求高于80%

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7416182
  • 这篇博客也不错, 你可以看下基于TensorFlow卷积神经网络与MNIST数据集设计手写数字识别算法
  • 您还可以看一下 陆永剑老师的TensorFlow入门学习教程课程中的 Mnist数据集介绍与获取小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    以下是具体的解决方案:

    1. 导入MNIST数据集
    import tensorflow as tf
    mnist = tf.keras.datasets.mnist
    (train_x, train_y), (test_x, test_y) = mnist.load_data()
    
    1. 数据预处理
    train_x = train_x.reshape(-1, 28, 28, 1)/255.0
    test_x = test_x.reshape(-1, 28, 28, 1)/255.0
    train_y = tf.keras.utils.to_categorical(train_y, 10)
    test_y = tf.keras.utils.to_categorical(test_y, 10)
    
    1. 定义模型
    model = tf.keras.Sequential([
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
        tf.keras.layers.MaxPooling2D(),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    
    1. 编译模型并训练
    model.compile(optimizer=tf.optimizers.Adam(),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    
    history = model.fit(train_x, train_y, epochs=20, batch_size=32, validation_data=(test_x, test_y))
    
    1. 绘制训练过程中训练集正确率和损失函数的表现图
    import matplotlib.pyplot as plt
    
    # 准确率的变化
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.title('Model Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
    
    # 损失函数的变化
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
    

    完整的代码如下:

    import tensorflow as tf
    
    # 导入MNIST数据集
    mnist = tf.keras.datasets.mnist
    (train_x, train_y), (test_x, test_y) = mnist.load_data()
    
    # 数据预处理
    train_x = train_x.reshape(-1, 28, 28, 1)/255.0
    test_x = test_x.reshape(-1, 28, 28, 1)/255.0
    train_y = tf.keras.utils.to_categorical(train_y, 10)
    test_y = tf.keras.utils.to_categorical(test_y, 10)
    
    # 定义模型
    model = tf.keras.Sequential([
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
        tf.keras.layers.MaxPooling2D(),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    
    # 编译模型并训练
    model.compile(optimizer=tf.optimizers.Adam(),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    
    history = model.fit(train_x, train_y, epochs=20, batch_size=32, validation_data=(test_x, test_y))
    
    # 绘制训练过程中训练集正确率和损失函数的表现图
    import matplotlib.pyplot as plt
    
    # 准确率的变化
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.title('Model Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
    
    # 损失函数的变化
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()