当前位置:网站首页>Tensorflow 2 detailed explanation (TF ecosystem, installation, housekeeping, basic operation)

Tensorflow 2 detailed explanation (TF ecosystem, installation, housekeeping, basic operation)

2022-07-26 00:41:00 Haohao, it's time to cook

         This article is from TensorFlow Theoretical foundation starts , Simple code example , Sort it out TensorFlow Getting started .

Catalog

About TensorFlow 2

TensorFlow The ecological system

Dynamic graph mechanism

tf.data

Estimator

TensorFlow Lite

TensorFlow Hub 

TensorFlow Extended

TensorBoard

TensorFlow install

TensorFlow Basic operation

Import TensorFlow And look at

  Dynamic graph check GPU

Statement Eager Variable  

Statement TensorFlow Constant

 ​ edit

  establish tensor tensor

  Tensor shape remodeling

Tensor and python Installation and replacement of variables

 ​ edit

Calculate the tensor size

  View tensor data types

Tensor basic operation rules

Calculate the square difference of tensor  

Calculate average

Random initialization tensor  

Save the recovery tensor using checkpoints


About TensorFlow 2

        TensorFlow Born in 2011  year , As Google An internal and closed source project of the company , It was named at that time DisBelief.DisBelief  It is a machine learning system with deep learning neural network . In the system  2015 year 11 On August th  Apache 2.0  Released to the developer community under an open source license , And become  TensorFlow.TensorFlow 1.0.0  Version released at 2017 year 2 month  11 Japan .TensorFlow 2.0.0 Alpha  Version released at  2019 year 3 month 6 Japanese  TensorFlow  Developer Summit . During and after , A large number of versions have been released , And contains a lot of new features .


        TensorFlow  Its name comes from tensor (tensor). Tensors are the generalization of vectors and matrices in higher dimensions . The rank of the tensor is the index number required to specify each element of the tensor . Scalar ( A number ) Is rank 1 Tensor , The vector is rank 1 Tensor , The matrix is rank  2 Tensor , The three-dimensional array is rank 3 Tensor . Tensors have data types and shapes ( All data items in the tensor must have the same type ). 

         Four dimensional tensor ( That is, the rank is 4) An example of this is Images , for instance , These four dimensions can be batch (batch)、 Height (height)、 Width (width) and Color (color) passageway .

imagel = tf. zeros([15, 500, 300, 3])  #  batch 、 Height 、 Width 、 Color channel 

         Even though  TensorFlow  It can be used in general numerical calculation fields , Especially machine learning , But its main research and development field is usually deep neural network  (Deep Neural Networks, DNN) Application . Its application fields mainly include speech recognition , Such as voice assistant, which is widely used now , Text based applications 、 Language translator 、 Image recognition 、 Alien search 、 Cancer detection and diagnosis 、 Time series applications, etc .

TensorFlow The ecological system

Dynamic graph mechanism

         First introduced  TensorFlow  Dynamic graph mechanism in  (Eager Execution). TensorFlow At first, we need to construct a computational graph composed of operations and tensors , Next , These operations and tensors must be in  Google  Calculation in the so-called session ( It's called declarative programming ). This is the writing TensorFlow  A common method of program . However , From the form of research  1.5 Release start , Dynamic graph mechanism comes into effect , And from 1.7 The release is officially integrated into  TensorFlow  in . Its features include the immediate execution of operations , So that the tensor can be like  NumPy Arrays are processed as well ( It is called imperative programming ).

tf.data

        tf.data  It's a  API, It allows users to move from simpler 、 Reusable parts build complex data input pipelines . The highest level of abstraction is Dataset, It contains elements of tensor nested structure , It also contains transformation plans that act on these elements . The following is about  Dataset  Common classes for :

  • FixedLengthRecordDataset: Used to get data sets with fixed length records , The data set comes from one or more binary files .
  • TFRecordDataset: Indicates that it contains one or more TFRecord  The record data set of the file .
  • TextLineDataset: Represents a dataset containing rows from one or more text files .
  • tf. data. Iterator: Express  Dataset  Iteration state .

Estimator

        Estimator It's an advanced  API, Allow users to build very simplified machine learning programs , And be responsible for training 、 assessment 、 Prediction and service export .TensorFlow. jis  It's a group.  API, Allow users to use the underlying  JavaScript  Linear algebra library or other high-level  API Building and training models . With the help of this  API, The model can be trained and run in the browser .

TensorFlow Lite

        TensorFlow Lite yes  TensorFlow  Lightweight version of , Suitable for mobile and embedded equipment . It consists of a runtime interpreter and a set of utilities . The original intention is , Train the model on a more powerful machine , Then use the utility to convert the model into .tflite  Format , The model can then be loaded into the selected device . In writing this book ,Tensor Flow Lite  use  C++ API To support  Android  and  i0s, And it is applicable to  Android  Of Java  Wrappers .

TensorFlow Hub 

        TensorFlow Hub  Is a library , It aims to promote the release of reusable modules of machine learning models 、 Discover and use . ad locum , Module by  TensorFlow  Network structure and its weight composition . This module can be reused in different tasks through migration learning . The original intention is , Train models on large datasets , Then reuse the appropriate modules for different but related tasks . This method has many advantages , Not only can smaller datasets be used to train models , Generalization can also be improved , Significantly speed up training .

TensorFlow Extended

        TensorFlow Extended(TFX) It's based on  TensorFlow  General machine learning platform . The open source libraries released so far include  TensorFlow Transform、Tensor FlowModel Analysis  and  TensorFlow Serving.

        tf.keras  It's a use. Python  Advanced neural network  API, As  Tensor Flow Interface with other tensor tools .tf.keras  Support rapid prototyping , It is user-friendly, modular and extensible . It supports convolution and cyclic Networks , And you can  CPU  and  GPU Up operation .Keras  Is in  TensorFlow 2  The first choice for development in  API.

TensorBoard

        TensorBoard  It is a set of visualization tools , Support for  TensorFlow  Understanding of procedures 、 Debugging and optimization , It is compatible with both dynamic graph and computational graph execution environments . During model training , have access to TensorBoard  Carry out various visual operations .

TensorFlow install

(Linux) Use conda Configuration compatibility TensorFlow and PyTorch Of env Environmental Science _ It's time for Haohao to cook -CSDN Blog _linux Get into tensorflow Environmental Science If you want to use it at the same time Tensorflow and pytorch, It needs to be installed in the same environment . After many attempts, I configured a compatible environment , In this environment , Use TensorFlow The model and pytorch The model of can be deployed on the unified system architecture https://blog.csdn.net/qq_52213943/article/details/124617468

TensorFlow Basic operation

Import TensorFlow And look at

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__))

Running results :

  Dynamic graph check 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')

  Running results :

Statement Eager Variable  

t0 = 24 
# python Variable 

t1 = tf.Variable(42) 
# 0 D tensor 

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

  Running results :

Statement TensorFlow Constant

mol = tf.constant(42)
mol

  Running results :

 

  establish tensor tensor

The shape of the tensor can be accessed through attributes

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

Output shape results :

  Tensor shape remodeling

r1 = tf.reshape(t2,[2,6]) # 2  That's ok  6  Column 
r2 = tf.reshape(t2,[1,12]) # 1  That's ok  12  Column 

# The value cannot be changed 

Running results :

Tensor and python Installation and replacement of variables

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

 

Calculate the tensor size

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

  View tensor data types

t3.dtype

# Output 
tf.float32

Tensor basic operation rules

Tensor operations can be performed by overloading operators +、-、*、/ Realization

t2*t2

t4 = t2*4

Calculate the square difference of tensor  

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

Random initialization tensor  

# tf.random.normal() Output the tensor of a given shape   Normal distribution filling 
tf.random.normal(shape = (3,2), mean=10, stddev=2, dtype=tf.float32, seed=None,  name=None)

# tf.random.uniform() Output the tensor of a given shape   The range of maximum and minimum values of the upper and lower boundaries is filled 
tf.random.uniform(shape = (2,4),  minval=0, maxval=None, dtype=tf.float32, seed=None,  name=None)

Save the recovery tensor using checkpoints

         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') # Exist under this path 
variable.assign([[0,0,0,0],[0,0,0,0]])
variable

 

Welcome to thumb up 、 Collection 、 Comment area exchange , Reprint mark the source of .

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

原网站

版权声明
本文为[Haohao, it's time to cook]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/201/202207190442570575.html