当前位置:网站首页>Realization of MNIST handwritten numeral recognition
Realization of MNIST handwritten numeral recognition
2022-06-25 00:23:00 【Know the cold and the warm*】
Catalog
Preface
Realization mnist Handwritten digit recognitionOne 、 Code implementation
import tensorflow as tf
from tensorflow.keras.datasets import mnist
import matplotlib as plt
from tensorflow.keras import models
from tensorflow.keras import layers
(train_images,train_labels), (test_images, test_labels) = mnist.load_data()
# train_images.shape: (60000,28,28) 6 10000 images , Every image is 28*28 Pixel image of .
# Building neural network
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
# Just write a few categories , Here is 10 classification .
network.add(layers.Dense(10, activation='softmax'))
# compile( compile ): Loss function 、 Optimizer 、 Indicators to be monitored during training and testing
# metrics: The index list , For the classification problem , We usually set the list to metrics=['accuracy'], The mean square error regression loss is mse
# For multi category losses 'categorical_crossentropy', II. For classified losses 'binary_crossentropy'
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Data processing : Transform it into the shape required by the network , And normalized
train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32')/255
from tensorflow.keras.utils import to_categorical
# to_categorical: Convert the category vector to binary ( Only 0 and 1) Matrix type representation of . That is to say, the original category vector is transformed into the form of single hot coding .
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# Start training
network.fit(train_images, train_labels, epochs=20, batch_size=128)
# assessment
test_loss, test_acc = network.evaluate(test_images, test_labels)
print(test_loss, test_acc)
Two 、 Some things to pay attention to
2-1、 Network construction mode
It can be ( Create... Through builder ):
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))
It can also be ( adopt add Methods build ):
network = models.Sequential([
layers.Dense(512, activation='relu', input_shape=(28*28,)),
layers.Dense(10, activation='softmax'),
])
2-2、 Determine the specification of model input data
The first layer needs to inform the model data specification through parameter transfer , The back layer does not need , Because it can be deduced automatically according to the output of the first layer .
adopt input_shape Parameters :
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
It can also be done through input_dim Parameter setting , Similar to the above meaning :
network.add(layers.Dense(512, activation='relu', input_dim=28*28))
Be careful :input_shape=(2828,) It means that the input data is 2828 First order vector of dimension .input_shape The format of is tuple , So it must be written as (28*28,) This form .
2-3、 Tensor operation inside the full connection layer
Example :
keras.layers.Dense(512, activation='relu')
annotation : Enter a 2D tensor , Go back to the other 2D tensor . The function is shown below
The formula says :output = relu(dot(w, input) + b)
namely : Input tensor and tensor w( A random tensor of a given shape ) Dot product operation between (dot), Got 2D Tensor and vector b The addition between , Last pass relu Activation function ( namely max(x,0)),relu Both operations and addition operations are element by element operations .
2-4、 There are some understandings about dot products
keras.layers.Dense(512, activation='relu')
Be careful : The dot product between two vectors is a scalar , And only vectors with the same number of elements can do dot product , Multiply element by element and add .
import numpy as np
np.dot([1, 2],[3,4])
# Output
# 11
commonly : Dot product between two matrices , For two matrices x and y, If and only if x.shape[1] == y.shape[0] when , You can plot them , The result is a shape of (x.shape[0], y.shape[1]) Matrix , namely x The line and the way of life y The sum of the multiplied columns of .
np.dot([[1, 2],[1,2]], [[3, 4],[3,4]])
# Output
# array([[ 9, 12],
# [ 9, 12]])
Reference article :
adopt Sequential Quickly build tensorflow Model .
Input_shape Parameters .
Keras Chinese document .
Optimizer optimizers.
Objective function objectives.
Sequential Model method .
summary
Just try to do something , Although the result was terrible ...
边栏推荐
- Adding, deleting, modifying and checking in low build code
- Use of navigation and navigationui
- Paint rounded rectangle
- Analysis report on development mode and investment direction of sodium lauriminodipropionate in the world and China 2022 ~ 2028
- JDBC - database connection
- JS dynamically generates variable names and assigns values
- Intensive reading of thinking about markdown
- Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
- What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
- MySQL log management
猜你喜欢

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181

Ott marketing is booming. How should businesses invest?

Wallpaper applet wechat applet

不重要的token可以提前停止计算!英伟达提出自适应token的高效视觉Transformer网络A-ViT,提高模型的吞吐量!...
More pictures | explain the Nacos parameters in detail!
Design and practice of vivo server monitoring architecture

传输层 以字节为单位的滑动窗口技术

VNC viewer remote connection raspberry pie without display

U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
随机推荐
Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)
In the past 5 years, from "Diandian" to the current test development, my success is worth learning from.
浅析大型IM即时通讯系统开发难度
JS dynamically generates variable names and assigns values
Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
Scrollview height cannot fill full screen
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
Creative SVG ring clock JS effect
从数字化过渡到智能制造
How to use promise Race() and promise any() ?
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
Svg+js keyboard control path
在滴滴和字节跳动干了 5年软件测试,太真实…
Approaching harvest moon:moonbeam DFI Carnival
Go crawler framework -colly actual combat (I)
Use and click of multitypeadapter in recycleview
Use of JMeter easynmon
Virtual machine - network configuration
ServerSocket and socket connection
Overview of medium and low speed aerospace electronic bus