Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze convolution Neural Network in TensorFlow Foundation

2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to analyze the convolution neural network in the basis of TensorFlow? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Convolution neural network

Grasp its core idea, that is, reduce the content of the image through the convolution operation, and focus the model attention on the specific and obvious features of the image.

Max pooling-enhance features and reduce data

Realize

In the following code, the accuracy of the model in training data may rise to about 93%, and in verification data may rise to 91%.

This is a remarkable progress in the right direction!

Try running more epochs--, such as 20 epochs, and observe the results! Although the results may look very good, the validation results may actually decline because of "overfitting", which will be discussed later.

(in short, 'overfitting' occurs when the network model learns very well from the training set, but it is so narrow that it can only recognize training data and does not work well when seeing other data. For example, if we only see red shoes all our lives, we may be confused when we see a pair of blue suede shoes. To give another example, examination-oriented education often makes students have a good correct rate for the questions they have done, but a high error rate for real problems)

Import tensorflow as tf

Print (tf.__version__)

Mnist = tf.keras.datasets.fashion_mnist

(training_images, training_labels), (test_images, test_labels) = mnist.load_data ()

Training_images=training_images.reshape (60000, 28, 28, 1)

Training_images=training_images / 255.0

Test_images = test_images.reshape (10000, 28, 28, 1)

Test_images=test_images/255.0

Model = tf.keras.models.Sequential ([

Tf.keras.layers.Conv2D (64, (3P3), activation='relu', input_shape= (28,28,1))

Tf.keras.layers.MaxPooling2D (2,2)

Tf.keras.layers.Conv2D (64, (3pyr3), activation='relu')

Tf.keras.layers.MaxPooling2D (2Jing 2)

Tf.keras.layers.Flatten ()

Tf.keras.layers.Dense (128C, activation='relu')

Tf.keras.layers.Dense (10, activation='softmax')

])

Model.compile (optimizer='adam', loss='sparse_categorical_crossentropy', metrics= ['accuracy'])

Model.summary ()

Model.fit (training_images, training_labels, epochs=5)

Test_loss = model.evaluate (test_images, test_labels)

How to establish the convolution model

The first step is to collect data. We will notice that there is a slight change here and before, and the training data needs to change the dimension (shape). This is because the first convolution expects a single tensor containing all the data, so set the training data to a 4D list of 60000x28x28x1, and the test image is treated the same way. If you do not do so, you will get an error during training because the convolution operation will not recognize the shape of the data.

Next is to define the model. The first step is to add a convolution layer. The parameter is

The number of convolutions we want to generate (number of filters). This number is arbitrary, but preferably a multiple starting at 32. The size of the convolution (the size of the filter), in this case the 3x3 grid. This is the most commonly used size. The activation function to use-in this case, we will use relu, which we may remember is equivalent to returning x when x > 0, otherwise returning 0. On the first floor, set the shape of the input data.

A MaxPooling layer is added after the convolution layer to compress the image while maintaining the feature content emphasized by the convolution. By specifying (2) for MaxPooling, the effect is to reduce the size of the image by 1/4. The idea is to create an array of 2x2 pixels, then select the largest one, turning four pixels into one, and do so repeatedly throughout the image, resulting in halving the number of horizontal pixels, halving the number of vertical pixels, and effectively reducing the image by 25%.

Add another convolution layer and MaxPooling2D.

Now flatten the output. After that, you will have the same DNN structure as the non-convolutional version, that is, a fully connected neural network.

There are 128 neurons in the fully connected layer and 10 neurons in the output layer.

Now compile the model, call the model.fit method for training, and then use the test set to evaluate loss and accuracy.

Network structure

See if you can use only a single convolution layer and a single MaxPooling 2D to increase the MNIST (handwritten digit) recognition rate to 99.8% or higher. Once the accuracy exceeds this value, training should be stopped. There should be no more than 20 Epochs. If the epochs reaches 20 but the precision does not meet the requirements, then the layer structure needs to be redesigned. When reaching 99.8% accuracy, you should print "achieve 99.8% accuracy, so cancel the training!" The string of the.

Import tensorflow as tf

From tensorflow import keras

# # overwrite callback

Class Callbacks (tf.keras.callbacks.Callback):

Def on_epoch_end (self, epoch, logs= {}):

If (logs.get ('accuracy') > = 0.998):

Print ("99.8% accuracy, so cancel training!")

Self.model.stop_training = True

Callbacks = Callbacks ()

# # preparing data

Mnist = tf.keras.datasets.mnist

(training_images, training_labels), (test_images, test_labels) = mnist.load_data ()

# # normalization

Training_images = training_images.reshape (60000, 28, 28, 1)

Training_images = training_images / 255.0

Test_images = test_images.reshape (10000, 28, 28, 1)

Test_images = test_images / 255.0

# # Building a Model

Model = tf.keras.models.Sequential ([

Tf.keras.layers.Conv2D (64, (3P3), activation='relu', input_shape= (28,28,1))

Tf.keras.layers.MaxPooling2D (2,2)

# tf.keras.layers.Conv2D (62, (3,3), activation='relu')

# tf.keras.layers.MaxPooling2D (2pr 2)

Tf.keras.layers.Flatten (), # # flattening

Tf.keras.layers.Dense (128C, activation='relu')

Tf.keras.layers.Dense (10, activation='softmax')

])

# # training

Model.compile (optimizer=tf.keras.optimizers.Adam ()

Loss=tf.keras.losses.SparseCategoricalCrossentropy ()

Metrics= ['accuracy'])

Model.summary ()

Model.fit (training_images, training_labels, epochs=4, callbacks= [callbacks])

# # Forecast and Evaluation

Test_loss = model.evaluate (test_images, test_labels)

Result

This is the answer to the question on how to analyze the convolution neural network in the TensorFlow foundation. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report