当前位置:网站首页>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 .
-----------------------------
边栏推荐
- Flask send verification code logic
- GOM and GEE engine black screen does not display the interface, and the solution of equipping map monsters
- 前缀异或和,异或差分数组
- 测试左移和测试右移的概念
- 2022/7/19 exam summary
- JMeter/IDEA中引用jar包json-path.jar的坎坷之路
- 以数据驱动管理转型,元年科技正当时
- SereTOD2022 Track1代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
- Semaphore
- Verilog grammar basics HDL bits training 06
猜你喜欢

以数据驱动管理转型,元年科技正当时

YOLOV2 YOLO9000

快速入门顺序表链表

Practical exercise | find customers who buy more than n products within a given time range

数据库工具对决:HeidiSQL 与 Navicat

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

C # from entry to mastery (III)
![[calculate the number of times that one string is equal to another string]](/img/82/db8ed70464df46c7a700c65d208fef.png)
[calculate the number of times that one string is equal to another string]

SQL time splicing problem, splicing recovery automatically truncated by the system

进程与线程
随机推荐
快速入门顺序表链表
Data driven DDT for automated testing
【无标题】如何实现可插拔配置?
MySQL - master-slave replication
Hcip day 12
HCIP第十二天
@The difference between requestparam and @pathvariable annotations
JVM Tri Color marking and read-write barrier
8 tips - database performance optimization, yyds~
【计算一个字符串和另一个字符串相等的次数】
[paper notes] - target attitude estimation Epro PNP 2022 CVPR
生物JC UVSSA复合物缓解MYC驱动的转录压⼒ English
【oops-framework】界面管理
8个小妙招调整数据库性能优化,yyds
Pikachu target clearance and source code analysis
为了拿捏 Redis 数据结构,我画了 40 张图(完整版)
LCA three postures (multiplication, tarjan+ joint search set, tree chain dissection)
SereTOD2022 Track1代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
Day06 MySql知识点总结
P4047 [jsoi2010] tribal Division

