Detailed TensorFlow 2 (tf ecosystem, installation, housekeeping, basic operations)

        This article starts from the theoretical basis of TensorFlow, simple code examples, and comprehensively organizes the introductory operations of TensorFlow.

Table of contents

About TensorFlow 2

TensorFlow Ecosystem

Dynamic graph mechanism

tf.data

Estimator

TensorFlow Lite

TensorFlow Hub 

TensorFlow Extended

TensorBoard

TensorFlow installation

TensorFlow Basic Operations

Import TensorFlow and view

Dynamic graph inspection GPU

Declare Eager variables 

Declare TensorFlow constants

Create tensor tensor

Tensor shape reshaping

Transformation of tensors and python variables

Calculate tensor size

View tensor data types

Tensor basic operation rules

Compute tensor squared difference 

calculate average

Randomly initialize tensors 

Restoring tensors with checkpoint saving


About TensorFlow 2

        TensorFlow was born in 2011 as an internal and closed-source project of Google, and was named DisBelief at the time. DisBelief is a machine learning system that applies a deep learning neural network. The system was released to the developer community under the Apache 2.0 open source license on November 2015 and evolved into TensorFlow. TensorFlow version 1.0.0 was released on February 11, 2017. The TensorFlow 2.0.0 Alpha release was released at the TensorFlow Developer Summit on March 6, 2019. During and after, a large number of versions were released and included a large number of new features.


        TensorFlow gets its name from tensor. Tensors are a higher-dimensional generalization of vectors and matrices. The rank of a tensor is the number of indices required to specify each element of the tensor. A scalar (a number) is a rank 1 tensor, a vector is a rank 1 tensor, a matrix is ​​a rank 2 tensor, and a 3D array is a rank 3 tensor. Tensors have a data type and shape (all data items in a tensor must have the same type). 

        An example of a four-dimensional tensor (i.e., rank 4) is an image , for example, these four dimensions can be batch (batch), height (height), width ( width) and color (color) channels.

imagel = tf. zeros([15, 500, 300, 3])  # 批次、高度、宽度、颜色通道

        Although TensorFlow can be used in the field of numerical computing in general, and machine learning in particular, its main research and development area is usually the application of Deep Neural Networks (DNN). Its application areas mainly include speech recognition, such as voice assistants that are widely used now, text-based applications, language translators, image recognition, alien planet search, cancer detection and diagnosis, time series applications, etc.

TensorFlow Ecosystem

Dynamic graph mechanism

        First introduce the dynamic graph mechanism (Eager Execution) in TensorFlow. TensorFlow initially requires the construction of a computation graph consisting of operations and tensors, which must then be computed in what Google calls a session (known as declarative programming). This is a common way of writing TensorFlow programs. However, starting with the 1.5 release in research form, the dynamic graph mechanism came into effect and was officially integrated into TensorFlow starting with the 1.7 release. Its features include immediate execution of operations, allowing tensors to be handled like NumPy arrays (known as imperative programming).

tf.data

        tf.data is an API that allows users to build complex data input pipelines from simpler, reusable parts. Its highest-level abstraction is a Dataset, which contains both the elements of the tensor nested structure and the transformation plans that act on these elements. The following are common classes related to Dataset:

  • FixedLengthRecordDataset: Used to get a dataset with fixed-length records, which comes from one or more binary files.
  • TFRecordDataset: Represents a dataset containing records from one or more TFRecord files.
  • TextLineDataset: Represents a dataset containing lines from one or more text files.
  • tf.data.Iterator: Indicates the Dataset iteration status.

Estimator

        Estimator is a high-level API that allows users to build very simplified machine learning programs and is responsible for training, evaluation, prediction and serving export. TensorFlow.jis is a set of APIs that allow users to build and train models using the underlying JavaScript linear algebra library or other high-level APIs. With this API, models can be trained and run in the browser.

TensorFlow Lite

        TensorFlow Lite is a lightweight version of TensorFlow for mobile and embedded devices. It consists of a runtime interpreter and a set of utilities. The idea is to train the model on a more powerful machine, then use a utility to convert the model to .tflite format, which can then be loaded to the device of choice. As of this writing, TensorFlow Lite supports Android and i0s with a C++ API, and has a Java wrapper for Android.

TensorFlow Hub 

        TensorFlow Hub is a library designed to facilitate the publishing, discovery, and consumption of reusable modules for machine learning models. Here, modules consist of TensorFlow network structures and their weights. This module can be reused in different tasks by means of transfer learning. Its original intention is to train a model on a large dataset and then reuse appropriate modules for different but related tasks. This approach has many advantages, not only allowing the model to be trained with a smaller dataset, but also improving generalization and significantly speeding up training.

TensorFlow Extended

        TensorFlow Extended (TFX) is a general machine learning platform based on TensorFlow. Open source libraries released so far include TensorFlow Transform, TensorFlowModel Analysis, and TensorFlow Serving.

        tf.keras  is a high-level neural network API written in Python that serves as an interface to Tensor Flow and various other tensor tools. tf.keras supports rapid prototyping, is user-friendly, modular, and extensible. It supports convolutional and recurrent networks, and can run on both CPU and GPU. Keras is the API of choice for developing in TensorFlow 2.

TensorBoard

        TensorBoard is a set of visualization tools that supports the understanding, debugging and optimization of TensorFlow programs. It is compatible with both dynamic graph and computational graph execution environments. During model training, TensorBoard can be used for various visualization operations.

TensorFlow installation

(Linux) Use conda to configure an env environment compatible with TensorFlow and PyTorch_Haohao's Blog-CSDN Blog_linux Entering the tensorflow environment If you want to use Tensorflow and pytorch at the same time, you need to install them in the same environment. After trying many times, I configured a compatible environment. In this environment, models using TensorFlow and pytorch can be deployed on a unified system architecture https://blog.csdn.net/qq_52213943/article/details/124617468

TensorFlow Basic Operations

Import TensorFlow and view

import tensorflow as tf
print("TensorFlow version: {}".format(tf.__version__))
print("Eager execution is: {}".format(tf.executing_eagerly()))
print("Keras version: {}".format(tf.keras.__version__))

operation result:

Dynamic graph inspection GPU

var = tf.Variable([3, 3])

if tf.test.is_gpu_available():
    print('Running on GPU')
    print('GPU #0?')
    print(var.device.endswith('GPU:0'))
else:
    print('CPU')

 operation result:

Declare Eager variables 

t0 = 24 
# python变量

t1 = tf.Variable(42) 
# 0维张量

t2 = tf.Variable([ [ [0., 1., 2.], [3., 4., 5.] ], [ [6., 7., 8.], [9., 10., 11.] ] ])
# 三维张量

 operation result:

Declare TensorFlow constants

mol = tf.constant(42)
mol

 operation result:

 

Create tensor tensor

The shape of the tensor is accessible through the attribute

t2 = tf.Variable([ [ [0., 1., 2.], [3., 4., 5.] ], [ [6., 7., 8.], [9., 10., 11.] ] ])
# tensor variable
print(t2.shape)

Output shape result:

Tensor shape reshaping

r1 = tf.reshape(t2,[2,6]) # 2 行 6 列
r2 = tf.reshape(t2,[1,12]) # 1 行 12 列

#取值不能变

operation result:

Transformation of tensors and python variables

print(t2.numpy())
#或者
print(t2[1, 0, 2].numpy())

 

Calculate tensor size

s = tf.size(input=t2).numpy()

View tensor data types

t3.dtype

#输出
tf.float32

Tensor basic operation rules

Tensor operations can be implemented by overloading operators +, -, *, /

t2*t2

t4 = t2*4

Compute tensor squared difference 

x = [1,3,5,7,11]
y = 5
s = tf.math.squared_difference(x, y)

calculate average

tf.reduce_mean(input_tensor=numbers) 
#( 4. + 5. + 7. + 3.)/4 = 4.75

Randomly initialize tensors 

# tf.random.normal()输出给定形状的张量 正态分布填充
tf.random.normal(shape = (3,2), mean=10, stddev=2, dtype=tf.float32, seed=None,  name=None)

# tf.random.uniform()输出给定形状的张量 上下边界最大最小值范围填充
tf.random.uniform(shape = (2,4),  minval=0, maxval=None, dtype=tf.float32, seed=None,  name=None)

Restoring tensors with checkpoint saving

        Explained in detail later, this method can save Zhang Liang

variable = tf.Variable([[1,3,5,7],[11,13,17,19]])
checkpoint= tf.train.Checkpoint(var=variable)
save_path = checkpoint.save('./vars') #存在本路径下
variable.assign([[0,0,0,0],[0,0,0,0]])
variable

Welcome to like, collect, and comment in the comment area, and reprint to indicate the source.

-----------------------------

Guess you like

Origin blog.csdn.net/qq_52213943/article/details/125837930