首页 > 分享 > 手写数字识别

手写数字识别

#导入包 import numpy as np import matplotlib.pyplot as plt from tensorflow import keras import tensorflow as tf 12345

#载入数据 (x_train,y_train),(x_test,y_test) = tf.keras.datasets.mnist.load_data() print(x_train.shape) print(y_train.shape) #x_train的数据shape为(60000,28,28) #y_train的数据shape为(60000,) #将x_train和x_test的shape转换成(60000,784) #(60000,28,28)->(60000,784) #由于其数值范围从0到255,因此除以255以实现归一化使得数据范围从0到1 x_train = x_train.reshape(x_train.shape[0],-1)/255.0 x_test = x_test.reshape(x_test.shape[0],-1)/255.0 print(x_train.shape) #将y_train和t_test转换成one hot格式 y_train = keras.utils.to_categorical(y_train,num_classes=10) y_test = keras.utils.to_categorical(y_test,num_classes=10)

123456789101112131415161718

(60000, 28, 28)
(60000,)
(60000, 784)

#创建模型,输入784个神经元,输出10个神经元 model = keras.Sequential() model.add(keras.layers.Dense(10,input_dim=784)) model.add(keras.layers.Activation('softmax')) sgd = keras.optimizers.SGD() model.compile(optimizer=sgd,loss='mse',metrics=['accuracy']) 1234567

#开始训练模型 model.fit(x_train,y_train,batch_size=32,epochs=10) 12

Epoch 1/10
1875/1875 [] - 1s 364us/step - loss: 0.0897 - accuracy: 0.1654
Epoch 2/10
1875/1875 [] - 1s 365us/step - loss: 0.0778 - accuracy: 0.4778
Epoch 3/10
1875/1875 [] - 1s 362us/step - loss: 0.0647 - accuracy: 0.6280
Epoch 4/10
1875/1875 [] - 1s 366us/step - loss: 0.0544 - accuracy: 0.7044
Epoch 5/10
1875/1875 [] - 1s 363us/step - loss: 0.0474 - accuracy: 0.7379
Epoch 6/10
1875/1875 [] - 1s 362us/step - loss: 0.0419 - accuracy: 0.7721
Epoch 7/10
1875/1875 [] - 1s 361us/step - loss: 0.0384 - accuracy: 0.7947
Epoch 8/10
1875/1875 [] - 1s 360us/step - loss: 0.0354 - accuracy: 0.8128
Epoch 9/10
1875/1875 [] - 1s 359us/step - loss: 0.0329 - accuracy: 0.8291
Epoch 10/10
1875/1875 [] - 1s 358us/step - loss: 0.0310 - accuracy: 0.8394
<tensorflow.python.keras.callbacks.History at 0x2900506f208>

#评估模型 loss,accuracy = model.evaluate(x_test,y_test) print('loss: ',loss) print('accuracy: ',accuracy) 1234

313/313 [==============================] - 0s 323us/step - loss: 0.0287 - accuracy: 0.8511
loss: 0.02871590293943882
accuracy: 0.8511000275611877

卷积神经网络

#导入包 from keras import layers from keras import models 1234

#构建卷积神经网络模型 model=models.Sequential() model.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1))) model.add(layers.MaxPooling2D((2,2))) model.add(layers.Conv2D(64,(3,3),activation='relu')) model.add(layers.MaxPooling2D((2,2))) model.add(layers.Conv2D(64,(3,3),activation='relu')) 1234567

model.summary() 1

在这里插入图片描述

#在卷积神经网络模型上添加分类器 model.add(layers.Flatten()) model.add(layers.Dense(64,activation='relu')) model.add(layers.Dense(10,activation='softmax')) 1234

model.summary() 1

在这里插入图片描述

#导入数据集 from keras.datasets import mnist from keras.utils import to_categorical (train_images,train_labels),(test_images,test_labels)=mnist.load_data() 1234

#把数据集归一化 train_images=train_images.reshape((60000,28,28,1)) train_images=train_images.astype('float32')/255 test_images=test_images.reshape((10000,28,28,1)) test_images=test_images.astype('float32')/255 #把数据转化成矩阵形式 train_labels=to_categorical(train_labels) test_labels=to_categorical(test_labels) #编译模型 model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(train_images,train_labels,epochs=5,batch_size=64) 123456789101112131415

在这里插入图片描述

#查看精度 test_loss,test_acc=model.evaluate(test_images,test_labels) test_acc 123

在这里插入图片描述

相关知识

pytorch实现简单卷积神经网络(CNN)网络完成手写数字识别
34 花卉识别
逻辑回归分类、决策树分类、朴素贝叶斯分类及手写数字识别
深度学习 花卉识别
看图识花的算法,如何识别植物?
在线识别汉字
基于python编程的五种鲜花识别
花草录植物识别工具app
智能虫情测报灯,虫脸识别技术守护数字果园丰收
怎么看图识别植物?借助软件来识别!

网址: 手写数字识别 https://m.huajiangbk.com/newsview659079.html

所属分类:花卉
上一篇: 黑白花意.5,笔尖下的花姿百态
下一篇: 牡丹花创意实拍摄影图素材下载