当前位置:网站首页>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
TensorFlow The ecological system
Tensor and python Installation and replacement of variables
Calculate the square difference of 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
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)
molRunning 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.float32Tensor basic operation rules
Tensor operations can be performed by overloading operators +、-、*、/ Realization
t2*t2
t4 = t2*4Calculate 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.75Random 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 .
-----------------------------
边栏推荐
- 开发还没联调,任务就要上线
- [oops framework] random number generation management
- Redis (VIII) - redis enterprises' actual coupons spike
- Trial division -- power of 3
- YOLOV3
- Leetcode notes 20. valid parentheses
- Verilog grammar basics HDL bits training 05
- [untitled] how to realize pluggable configuration?
- [IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
- JVM 三色标记法与读写屏障
猜你喜欢

Nodejs learning

HCIP 第十一天

Distributed transactions: the final consistency scheme of reliable messages

Verilog grammar basics HDL bits training 05

Hnoi2012 mine construction

Hcip day 12

找出单身狗(力扣260)

Azure Synapse Analytics 性能优化指南(1)——使用有序聚集列存储索引优化性能

Redis夺命十二问,你能扛到第几问?

Preparation of bovine erythrocyte superoxide dismutase sod/ folic acid coupled 2-ME albumin nanoparticles modified by bovine serum albumin
随机推荐
Leetcode 笔记 121. 买卖股票的最佳时机
向左旋转k个字符串(细节)
[redis] ① introduction and installation of redis
[calculate the number of times that one string is equal to another string]
SereTOD2022 Track1代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
P4047 [JSOI2010]部落划分
The way to understand JS: six common inheritance methods of JS
Preparation of bovine serum albumin modified by low molecular weight protamine lmwp/peg-1900 on the surface of albumin nanoparticles
Leetcode 笔记 20. 有效的括号
letfaw
Research on visualization method of technology topic map based on clustering information
【NumPy中数组相关方法】
[array creation in numpy]
分布式事务和Seata的AT模式原理
Leetcode notes 350. Intersection of two arrays II
The way of understanding JS: write a perfect combination inheritance (Es5)
[redis] ③ data elimination strategy, pipeline command, publish and subscribe
The way to understand JS: the principle of object.call and object.create() inheritance
Four characteristics and isolation level of MySQL transactions
Practical exercise | find customers who buy more than n products within a given time range

