学习笔记(1):第一章:深度学习框架搭建-案例: Fashion Mnist衣服识别-01

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是基于TensorFlow深度学习框架,使用Python搭建LeNet-5卷积神经网络模型并使用MNIST手写数字识别数据集进行训练和测试的代码: ``` # 导入所需要的库 import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers import numpy as np import matplotlib.pyplot as plt # 加载MNIST数据集 (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() # 将像素值归一化到[0, 1]之间 x_train = x_train.astype('float32') / 255 x_test = x_test.astype('float32') / 255 # 将标签转换成one-hot编码 y_train = keras.utils.to_categorical(y_train, 10) y_test = keras.utils.to_categorical(y_test, 10) # 定义LeNet-5模型 model = keras.Sequential([ layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dense(units=120, activation='relu'), layers.Dense(units=84, activation='relu'), layers.Dense(units=10, activation='softmax') ]) # 编译模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # 训练模型 history = model.fit(x_train.reshape(-1, 28, 28, 1), y_train, batch_size=128, epochs=20, validation_split=0.2) # 评估模型 score = model.evaluate(x_test.reshape(-1, 28, 28, 1), y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) # 绘制训练曲线 acc = history.history['accuracy'] val_acc = history.history['val_accuracy'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(acc) + 1) plt.plot(epochs, acc, 'bo', label='Training acc') plt.plot(epochs, val_acc, 'b', label='Validation acc') plt.title('Training and validation accuracy') plt.legend() plt.figure() plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and validation loss') plt.legend() plt.show() # 使用模型进行预测 predictions = model.predict(x_test.reshape(-1, 28, 28, 1)) for i in range(10): print('Prediction:', np.argmax(predictions[i])) print('True label:', np.argmax(y_test[i])) plt.imshow(x_test[i], cmap='gray') plt.show() ``` 运行以上代码,将会得到一个基于LeNet-5模型的手写数字识别软件。该软件可以通过图形界面或命令行方式输入手写数字图片,自动识别出数字并显示在界面上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值