前言
计算机视觉系列之学习笔记主要是本人进行学习人工智能(计算机视觉方向)的代码整理。本系列所有代码是用python3编写,在平台Anaconda中运行实现,在使用代码时,默认你已经安装相关的python库,这方面不做多余的说明。本系列所涉及的所有代码和资料可在我的github上下载到,gitbub地址:https://github.com/mcyJacky/DeepLearning-CV,如有问题,欢迎指出。
一、MNIST数据集
本篇MNIST数据集不做介绍,详细请参考第7篇文章https://blog.csdn.net/mcyJacky/article/details/86443206。在keras中MNIST数据集的载入方法如下,:
from keras.datasets import mnist
# 载入数据,会在c盘本地账号下的.keras/datasets中下载mnist.npz文件
(x_train,y_train),(x_test,y_test) = mnist.load_data()
print('x_shape:', x_train.shape) # (60000,28,28)
print('y_shape:', y_train.shape) # (60000)
print(y_train[0]) # 5 是非one-hot
二、使用简单的神经网络进行MNIST数据集简单分类
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils # keras中的numpy工具包
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
# 载入数据
(x_train,y_train), (x_test,y_test) = mnist.load_data()
# (6000,28,28)
print('x_shape:', x_train.shape)
# (6000)
print('y_shape:', y_train.shape)
# 进行数据转换,并归一化
# (60000,28,28) -> (60000, 784)
x_train = x_train.reshape(x_train.shape[0], -1) / 255.0
x_test = x_test.reshape(x_test.shape[0], -1) / 255.0
# 转换为one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
# 创建模型:输入784个神经元,输出10个神经元 784-10
model = Sequential([
Dense(units=10, input_dim=784, bias_initializer='one', activation='softmax')
])
# 定义优化
sgd = SGD(lr=0.2)
# 定义优化器,loss function, 训练过程中的准确率
model.compile(
optimizer = sgd,
loss = 'mse',
metrics = ['accuracy']
)
# 进行模型训练
model.fit(x_train, y_train, batch_size=32, epochs=10)
# 评估模型
loss, accuracy = model.evaluate(x_test, y_test)
print('\ntest loss:', loss)
print('accuracy:', accuracy)
#输出结果如下:
# x_shape: (60000, 28, 28)
# y_shape: (60000,)
# Epoch 1/10
# 60000/60000 [==============================] - 2s 37us/step - loss: 0.0376 - acc: 0.7804
# Epoch 2/10
# 60000/60000 [==============================] - 2s 35us/step - loss: 0.0203 - acc: 0.8823
# Epoch 3/10
# 60000/60000 [==============================] - 2s 35us/step - loss: 0.0177 - acc: 0.8947
# Epoch 4/10
# 60000/60000 [==============================] - 2s 35us/step - loss: 0.0164 - acc: 0.8996
# Epoch 5/10
# 60000/60000 [==============================] - 2s 37us/step - loss: 0.0156 - acc: 0.9041
# Epoch 6/10
# 60000/60000 [==============================] - 2s 37us/step - loss: 0.0150 - acc: 0.9071
# Epoch 7/10
# 60000/60000 [==============================] - 2s 38us/step - loss: 0.0146 - acc: 0.9095
# Epoch 8/10
# 60000/60000 [==============================] - 2s 39us/step - loss: 0.0143 - acc: 0.9111
# Epoch 9/10
# 60000/60000 [==============================] - 2s 37us/step - loss: 0.0140 - acc: 0.9130
# Epoch 10/10
# 60000/60000 [==============================] - 2s 37us/step - loss: 0.0137 - acc: 0.9145
# 10000/10000 [==============================] - 0s 20us/step
# test loss: 0.01298950129081495
# accuracy: 0.9196
由上述定义简单的神经网络进行MNIST数据集进行手写识别数字分类,它定义的是一个单层的神经网络,且训练周期为10个,其测试集的准确率也能达到90%以上。
【参考】:
1. 城市数据团课程《AI工程师》计算机视觉方向
2. deeplearning.ai 吴恩达《深度学习工程师》
3. 《机器学习》作者:周志华
4. 《深度学习》作者:Ian Goodfellow
转载声明:
版权声明:非商用自由转载-保持署名-注明出处
署名 :mcyJacky
文章出处:https://blog.csdn.net/mcyJacky