Tensorflow_2.6 official demo - detailed interpretation

1. Basic details of tensorflow

  1. Channel sorting of tensorflow Tensor: [batch, height, width, channel]

  2. tf.keras module: after version 2.0, the official main push is to use the keras module to build a network

    • You can use keras functional API (the style of building models in TF1) and model subclassing API (similar to the style of building models in Python) to build models.
  3. Build a model demo using the Model Subclassing API

    from tensorflow.keras.layers import Dense, Flatten, Conv2D
    from tensorflow.keras import Model
    
    
    class MyModel(Model):
        def __init__(self):
            super(MyModel, self).__init__()
            self.conv1 = Conv2D(32, 3, activation='relu')
            self.flatten = Flatten()
            self.d1 = Dense(128, activation='relu')
            self.d2 = Dense(10, activation='softmax')
    
        def call(self, x, **kwargs):
            x = self.conv1(x)      # input[batch, 28, 28, 1] output[batch, 26, 26, 32]
            x = self.flatten(x)    # output [batch, 21632]
            x = self.d1(x)         # output [batch, 128]
            return self.d2(x)      # output [batch, 10]
    

    explain:

    a. __init__(self) to define the modules needed in building the model. call() defines the process of network forward propagation.

    b. super(MyModel, self).__init__ (): solve a series of problems that may occur in the process of multi inheritance.

  4. The calculation formula of matrix size after convolution is: N = ( W − F + 2 P ) / s (rounded down to smaller) + 1 N = (W - F + 2P)/s (rounded down to smaller) + 1 N=(W − F+2P)/s (rounded down to smaller) + 1

    a. Enter picture length / width size W W W.

    b. Filter size is F × F F\times F F×F.

    c. Step size S S S

    d. Number of pixels padded P P P.

    Note: in tensorflow, user-defined padding is not required, but two parameters are given: VALID, SAME Output feature of both parameters_ The map size is as follows:

2. demo

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
from model import MyModel


def main():
    mnist = tf.keras.datasets.mnist

    # download and load data
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0

    # Add a channels dimension
    # Add a depth information
    x_train = x_train[..., tf.newaxis]
    x_test = x_test[..., tf.newaxis]

    # create data generator
    train_ds = tf.data.Dataset.from_tensor_slices(
        (x_train, y_train)).shuffle(10000).batch(32)
    test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)

    # create model
    model = MyModel()

    # define loss
    loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
    # define optimizer
    optimizer = tf.keras.optimizers.Adam()

    # define train_loss and train_accuracy
    train_loss = tf.keras.metrics.Mean(name='train_loss')
    train_accuracy = 					     tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')

    # define train_loss and train_accuracy
    test_loss = tf.keras.metrics.Mean(name='test_loss')
    test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')

    # define train function including calculating loss, applying gradient and calculating accuracy
    @tf.function
    def train_step(images, labels):
        with tf.GradientTape() as tape:
            predictions = model(images)
            loss = loss_object(labels, predictions)
        gradients = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        train_loss(loss)
        train_accuracy(labels, predictions)

    # define test function including calculating loss and calculating accuracy
    @tf.function
    def test_step(images, labels):
        predictions = model(images)
        t_loss = loss_object(labels, predictions)

        test_loss(t_loss)
        test_accuracy(labels, predictions)

    EPOCHS = 5

    for epoch in range(EPOCHS):
        train_loss.reset_states()        # clear history info
        train_accuracy.reset_states()    # clear history info
        test_loss.reset_states()         # clear history info
        test_accuracy.reset_states()     # clear history info

        for images, labels in train_ds:
            train_step(images, labels)

        for test_images, test_labels in test_ds:
            test_step(test_images, test_labels)

        template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'
        print(template.format(epoch + 1,
                              train_loss.result(),
                              train_accuracy.result() * 100,
                              test_loss.result(),
                              test_accuracy.result() * 100))


if __name__ == '__main__':
    main()

explain:

  1. Show the first three training sets and corresponding labels

    imgs = x_test[:3]
    labels = y_test[:3]
    print(labels)
    plot_img = np.hstack(imgs) # Horizontal splicing
    plt.imshow(plot_imgs, camp='gray') # Gray scale image: camp='gray '
    plt.show()
    
  2. train_ds = tf.data.Dataset.from_tensor_slices((x_train, 	y_train)).shuffle(10000).batch(32)
    

    Where, shuffle(10000) means 10000 pictures are read into the memory at one time, and random sampling with batch of 32 is performed on the 10000 pictures. In general, the larger the corresponding value in shuffle, the more it can reflect the random sampling process of the overall data. However, it is generally limited by memory and cannot be set too large.

​ 3. tf.keras.losses. The sparsecategoricalcrossentropy() loss function is used to process tag values of non one hot type. While CategoricalCrossentropy() handles tag values of type one hot.

4. Unlike pytoch, tensorflow does not automatically track the gradient of each trainable parameter, so you need to use tf.GradientTape() in conjunction with the with context manager.

Overview of usage of with: with will automatically call the subsequent object to instantiate, and assign the object to the variable name after as. With will call the object first__ enter()__ Method, and then execute the statement body under with. After execution, with will call the__ exit()__ method.

The advantage of with is that it will be called even if an exception occurs during the execution of the statement body__ exit()__ Methods, such as in the open() object, can ensure the normal closing of the file. with Detailed reference.

  1. **@tf.function * * decorator

    Function: build efficient python code, convert python code into tensorflow graph structure, and can operate on GPU and TPU. Add @ TF The python code part after function cannot be debugged by setting breakpoints inside the function, but the training speed will be greatly improved.

  2. Train as defined below_ loss``train_ Accuracy and the like can be regarded as accumulators. Therefore, in the next epoch training, the accumulator value needs to be cleared to zero.

    # define train_loss and train_accuracy
    train_loss = tf.keras.metrics.Mean(name='train_loss')
    train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
    
    # define train_loss and train_accuracy
    test_loss = tf.keras.metrics.Mean(name='test_loss')
    test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
    
    train_loss.reset_states()        # clear history info
    train_accuracy.reset_states()    # clear history info
    test_loss.reset_states()         # clear history info
    test_accuracy.reset_states()     # clear history info
    

``

Tags: Deep Learning TensorFlow AI

Posted by stringman on Mon, 29 Aug 2022 04:28:12 +0930