Practical introduction to deep learning - building a neural network with tensorflow

This article will not go into details about the principles of neural networks, but simply introduces how to use the python3.0 | tensorflow2.0 framework to build a simple neural network for simple image recognition.

This article is divided into three parts. The first part first publishes the overall code , the second part explains each piece of code in detail, and the third part summarizes .

reference:

tf.Keras - Jianshu (jianshu.com)

2 Fashion-MNIST: An image data set that replaces the MNIST handwritten digit set - Zhihu (zhihu.com)

model.evaluate test_kitanoli’s blog-CSDN blog_model.evaluate 

Table of contents

overall code

Code details introduction

1 Import data set     

2 Build a network model

3. Training network model

4 Evaluation model, how to check the training effect

5 models for prediction

Summarize 


overall code

import tensorflow as tf
from tensorflow import keras

#导入数据集
fashion_mnist = keras.datasets.fashion_mnist
#devide the data into training and test:train 60000 and test 10000
(train_images, train_lables),(test_images, test_lables) = fashion_mnist.load_data()


# 定义一个三层结构,输入层,隐藏层,输出层
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)), #图片为28 28格式
    keras.layers.Dense(128, activation= tf.nn.relu),    #隐藏层有128个神经元,这个数字是自己定的
    keras.layers.Dense(10, activation = tf.nn.softmax)  #10指的输出有10个类别,10个神经元
])

# train and evaluate the module
train_images_scaled = train_images/255 #将输入的数据归一化,训练效果更好
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(train_images_scaled, train_lables, epochs=5)

test_images_scaled = test_images/255
model.evaluate(test_images_scaled, test_lables)

#module to predict
print(model.predict(test_images/255)[0])

Code details introduction

  1 Import data set     

from tensorflow import keras

fashion_mnist = keras.datasets.fashion_mnist
#devide the data into training and test:train 60000 and test 10000
(train_images, train_lables),(test_images, test_lables) = fashion_mnist.load_data()
  • This code imports the mnist image data set , which covers a total of 70,000 front-facing images of different products in 10 categories and their corresponding labels. Each image is a 28×18 grayscale image. The training set/test set is divided into 60000/10000 , that is, there are 60000 trian_images and 10000 test_images here.
  • You can use the following code to view the data shape of the training set.
print(train_images.shape) #可以查看训练集的数据形状
print(train_images[0]) #可以查看训练集种具体一张图片的像素
plt.imshow(train_images[0]) #可以查看图片样式,关于imshow函数使用可以查看上一篇博文

        You can see that the corresponding shape is (60000, 28, 28), that is, 60000 28×28 grayscale pictures.

Other additions :

1 Introduction to keras
tf.keras is TensorFlow’s implementation of the Keras API specification . This is a high-level API for building and training models, including top-level support for TensorFlow-specific features such as Eager Execution , tf.data pipelines, and Estimators . tf.keras makes TensorFlow easier to use without sacrificing flexibility or performance.

2 Build a network model

# 定义一个三层结构,输入层,隐藏层,输出层
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)), #图片为28 28格式
    keras.layers.Dense(128, activation= tf.nn.relu),    #隐藏层有128个神经元,这个数字是自己定的
    keras.layers.Dense(10, activation = tf.nn.softmax)  #10指的输出有10个类别,10个神经元
])
  • A three-layer network structure is defined here, input layer, hidden layer, and output layer. The input layer is a flattened 28×28 image pixel input , the hidden layer has 128 neurons, and the activation layer has 10 neurons .

The keras.Sequential function, my personal understanding is: establish the first layer, the second layer, and the third layer of the neural network in order. Sequential means sequential. For example, the keras.layers.Flatten function here establishes the first layer of the neural network, and keras.layers.Dense establishes the second layer of the neural network.

        The keras.layers.Flatten function is responsible for compressing the input layer data into one-dimensional data, which is 28×28=784. It is usually used as a fully connected layer because the fully connected layer can only receive one-dimensional data, while the convolutional layer can process two-dimensional data. 

keras.layers.Dense creates a fully connected layer:

        keras.layers.Dense(units, activation=None, use_bias=True, kernel....)

  •  The units here are the number of neurons, a positive integer
  • activation is to select the activation function. If not specified, the activation function will not be used by default.

        If you are interested, you can check the usage of each activation function yourself. I will not go into details here.

3. Training network model

# train the module
train_images_scaled = train_images/255 #将输入的数据归一化,训练效果更好
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(train_images_scaled, train_lables, epochs=5)

Since the pixel value is between 0-255, compressing it to between 0-1 will have a better training effect.

  • model.compile is used to inform the optimizer, loss function and accuracy evaluation criteria used during training when configuring training.
  • model.fit is used to perform the training process of the model.

        model.fit(input feature X of training set,

                        The label Y of the training set,

                        batch_size, #The size of each batch, not set here

                        epochs, the number of iterations)

 4 Evaluation model, how to check the training effect

test_images_scaled = test_images/255 #因为训练集归一化了,这里也归一化
model.evaluate(test_images_scaled, test_lables) #评价模型效果

model.evaluate inputs data and corresponding real labels, then compares the prediction results with the real standard, obtains the error between the two, and outputs it.

 The figure below shows the loss and accuracy corresponding to the corresponding 5 rounds of training:

It can be seen that the loss is getting smaller and smaller, and the accuracy is getting higher and higher. At the bottom is the output of evaluate, the corresponding error and accuracy. You can see that this is worse than the training results of 0.2938 and 0.8911, which is normal.

5 models for prediction

#module to predict
print(model.predict(test_images/255)[0]) #输入测试集中的第一张图片,进行预测

model.predict inputs a new image and outputs the prediction result.

What is output here is 10 probability values, corresponding to the probability values ​​that this picture belongs to 10 categories , and the maximum probability value corresponds to the prediction result of the model. 

 The corresponding 9.5131058e-01 here is the largest, then the model predicts that this picture belongs to the 10th category.

You can use "print(test_labels[0]" to detect the label of the 0th picture in the test set.

The output is 9 (index values ​​start from 0), so the model predicts correctly.

Summarize 

        This code creates a three-layer neural network to train and test on the mnist image dataset.

        The overall structure of the network is as follows:

 You can use model.summary() to check the shape of the network

100480 is the number of synapses between the input layer and the hidden layer

100480: 784 pixels × 128 neurons = 100352, but because each layer has a bias (automatically added), one is added to the input layer and one is added to the middle layer, so it is (784+1) × 128

1290 is the number of synapses between the hidden layer and the output layer

1290: The output layer has 10 neurons, and the input is 128+1, which is bias. So it is (128+1)×10=1290.

Guess you like

Origin blog.csdn.net/weixin_51286347/article/details/127721042