1. tf
Commonly used APIs below (the following are more commonly used, mainly to define processing data)
1. tf.constant, tf.variable (it has more attributes than constant, such as .assign, .assign_add,
.assign_sub, these attributes directly return the value to the var variable itself, that is, directly change its value ),
tf.string,tf.range,tf.ones,tf.zeros,tf.linespace;tf.meshgrid.tf.RaggedTensor; definition data
2、tf.fill,tf.shape,tf.one_hot,tf.sort,tf.split,tf.stack,tf.concat,tf.reshape,tf.squeeze,tf.reverse,
tf.tanspose,tf.unique;操作数据
3. tf.random.normal, tf.random.shuffle, tf.random.uniform; define random numbers
4. tf.function; operation function
5. tf.device; specify which device (GPU or CPU) to compute data on
6. tf.GradientTape, tf.gradients, tf.stop_gradient; Explicit differentiation when used in custom training
Second, tf.audio
the commonly used API
1. tf.audio.decode_wav; Parse the WAV format audio file into a float tensor, shape=(length, channels)
2. tf.audio.encode_wav; encode the above float type tensor into an audio file in WAV format
Three, tf.autograph
the commonly used API
1. tf.autograph.to_graph; convert ordinary python programs into tf calculation graphs, so the function efficiency will be improved
2. tf.autograph.to_code; similar to to_graph, but will return the program code in the form of a string
Fourth, tf.compat
the commonly used APIs under (compat itself means compatibility)
1. tf.compat.as_bytes; convert unicode or bytesarray, bytes into bytes
2. tf.compat.as_text; convert string-like data into Unicode
3. tf.compat.as_str_any; convert the input to str type
4. tf.compat.v1, tf.compat.v2; these two are used to be compatible with the code of tf1.x and tf2.x, that is, you can call this function of your own version directly under v1 or v2 . v1.=tf1.; v2.=tf2.; they are the root space
Five, tf.cofig
the commonly used APIs (mainly used for GPU or CPU settings)
Six, tf.data
the commonly used APIs under (mainly used to generate and manipulate datasets)
1. Commonly used APIs under tf.data.Dataset, that is, the methods that the generated dataset can execute
apply, batch, concatenate, enumerate, filter, from_generator, from_tensor_slices,
from_tensors, Interval, map, range, repeat, shuffle, take, skip
2. Commonly used API under tf.data.TextLineDataset, which is used to parse each line in the file to generate a dataset. The function after generating the dataset is exactly the same as in 1.
3. Commonly used APIs under tf.data.TFRecordDataset, which are used to parse TFRecord files to generate datasets. The functions after generating datasets are exactly the same as those in 1.
7. tf.distribute
Commonly used APIs under (mainly include some distributed strategies for distributed training)
1、tf.distribute.MirroredStrategy
2、tf.distribute.experimental.CentralStorageStrategy
3、tf.distribute.experimental.MultiWorkerMirroredStrategy
4、tf.distribute.experimental.ParameterServerStrategy
5、tf.distribute.experimental.TPUStrategy
8. tf.dtypes
Commonly used APIs under (mainly used for data type conversion)
1. tf.dtypes.as_dtype(type_value); Convert the given type value to DType; the main values of DType are as follows:
2. tf.dtypes.cast; perform data type conversion
3. tf.dtypes.complex; Combine the given two numbers into a complex number (one real part, one imaginary part)
Nine, tf.estimator
the commonly used API under (this is a high-level API, you don’t need to build your own model, you only need to input data, specify the classification category, etc.; then call it .train(),.evaluate(),.predict()
; mainly for the convenience of model training, evaluation and prediction; there are many under Estimator)
1、tf.estimator.add_metrics(estimator,metric_fn);往estimator中添加metric
2、tf.estimator.BaselineClassifier,tf.estimator.BaselineRegressor,tf.estimator.BaselineEstimator
3、tf.estimator.DNNClassifier,tf.estimator.DNNEstimator,tf.estimator.DNNRegressor
4、tf.estimator.LinearClassifier,tf.estimator.LinearEstimator,tf.estimator.LinearRegressor
10. tf.feature_column
Commonly used APIs under (mainly to process those features with a lot of discrete values, and turn them into 0,1 codes)
1、tf.feature_column.categorical_column_with_vocabulary
2、tf.feature_column.indicator_column
3、tf.feature_column.crossed_column
4、tf.feature_column.numeric_column
11. tf.image
Commonly used APIs under (mainly processing image data or pictures)
1. tf.image.adjust_brightness, tf.image.adjust_contrast, tf.image.adjust_gamma; adjust brightness, etc.
2. tf.image.central_crop, tf.image.crop_and_resize, tf.image.crop_to_bounding_box; crop
3. tf.image. draw_bounding_boxes; can draw multiple anchor boxes
4, tf.image.flip_left_right, tf.image.flip_up_down; flip image
5, tf.image.grayscale_to_rgb, tf.image.hsv_to_rgb, tf.image.rgb_to_grayscale; color domain conversion
6, tf .image.random_flip_left_right, tf.image.random_crop; random operation
7, tf.image.resize, tf.image.ResizeMethod.BILINEAR; there are many resize methods under ResizeMethod
Twelve, tf.io
the commonly used APIs under (mainly to parse or encode or serialize files (pictures, texts, etc.) or tenor)
1. tf.io.decode_gif, tf.io.decode_jpeg, tf.io.decode_image...; decode the picture
2. tf.io.decode_csv, tf.io.read_file, tf.io.write_file; read, write and parse files
3. tf.io.serialize_sparse, tf.io.serialize_tensor; serialization operation
4. tf.io.TFRecordOptions, tf.io.TFRecordWriter; operate on TFRecord files
5. tf.io.gfile.(listdir, makedirs, remove , Rename, exists, copy) (easy to use)
Thirteen, tf.keras
the commonly used APIs under (emphasis, mainly and model,layers
related)
1. tf.keras.Input; initialize a keras tensor, used to input data into the model, and specify the shape, data type and other information of the data; of course, it can also be used for general calculations, but it is not used much. The main usage is to build a model, as shown in the first example in 2.
2. tf.keras.Model; mainly used to build a model, there are two ways to build a model:
①inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
②import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
model = MyModel()
Then you can call various attributes of the model:
model.layers, model.metrics_names
各种方法:model.compile,evaluate,evaluate_generator,fit,fit_generator,summary
predict,predict_generator,save,save_weights,load_weights,reset_states,reset_metrics
history=model.fit(),history=model.fit_generator(), history.history[loss or acc or val_loss or val_acc], it is a dictionary
predict=model.predict(), predict=model.predict_generator(), it is the prediction result, such as the shape, you can check its shape: predict.shape=(num_samples,num_class), this is the output of the classification problem
3. tf.keras.Sequential; is a linear stack that stores the model, and returns a model object, but it has two more methods than the model in 2 for adding and deleting layers: add, pop
4. tf.keras.activations.softmax, tf.keras.activations.relu, tf.keras.activations.tanh;
tf.nn.softmax, tf.nn.sigmoid, tf.nn.relu, tf.nn.tanh... ; Define activation function
5. tf.keras.applications.VGG16, tf.keras.applications.ResNet50...; directly call the defined model
6. tf.keras.backend; is the backend API provided for keras. The so-called backend is some basic operations, that is, mathematical calculation methods. Because keras is a high-level API, it does not have these basic operations. This is closer to the mathematical operation method under tf.math.
7. tf.keras.callbacks.EarlyStopping, tf.keras.callbacks.ModelCheckpoint,
tf.keras.callbacks.TensorBoard; tf.keras.callbacks.LearningRateScheduler
callback function is a set of functions that are called in a specific stage of training, you You can use the callback function to observe the internal state and statistical information of the network during the training process. By passing the callback function list to the model's .fit(), the functions in the function set can be called in a given training stage. Here is an example of LearningRateScheduler, which is still very vivid:
def scheduler(epoch):
if epoch < 10:
return 0.001
else:
return 0.001 * tf.math.exp(0.1 * (10 - epoch))
callback = tf.keras.callbacks.LearningRateScheduler(scheduler)
model.fit(data, labels, epochs=100, callbacks=[callback],
validation_data=(val_data, val_labels))
8. tf.keras.datasets; used to download some data sets, mainly including fashion_minist, cifa10, minist, cifa100, the calling format is as follows:
fashion_minist=keras.datasets.fashion_minist, fashion_minist.load_data()
9. tf.keras.estimator.model_to_estimator; used to convert keras model to estimator, note that there is no such function in tf.estimator, only this place has
10. tf.keras.initializers; mainly used for data initialization, creating an initialization or serialization/deserialization instance; the calling format is as follows: tf.keras.layers.Dense(32, activation='relu' ,kernel_initializer=tf.keras.initializers.glorot_normal))
11. tf.keras.layers.Dense, tf.keras.layers.Conv2D, tf.keras.layers.MaxPool2D, tf.keras.layers.Flatten;
tf.nn.avg_pool, tf.nn.conv2d, tf.nn. dropout, tf.nn.max_pool...;
there are various network layers, such as convolutional layer, fully connected, flattening layer, pooling layer, etc.
12. tf.keras.losses.MSE, tf.keras.losses.sparse_categorical_crossentropy; define the loss function
tf.nn.nce_loss, tf.nn.l2_loss, tf.nn.sampled_softmax_loss...;
13. tf.keras.metrics; there are also various functions for calculating error and accuracy below. The error function is similar to that of tf.keras.losses, but the usage is different. The object returned by the former has many attributes available, while the latter returns Is just a value, (note that the function under metircs will automatically add up each updated data before reset. The understanding of this addition is equivalent to the last calculation result not counting, and then adding all the current data Get up and recalculate according to the method you specify. Therefore, there is such an effect that if you use Mean, then the final calculation is the average of all the data; if it is Sum, then the sum of all the data is calculated ).
In compile, the parameters added in metrics are used to monitor the model during training and testing, that is, the measurement results are saved in history, and loss is the function that is really used for training, such as updating the gradient.
Mainly include: Accuracy, CategoricalAccuracy, CategoricalAccuracy, Mean, Sum
model.compile('sgd', loss=tf.keras.losses.SparseCategoricalCrossentropy())
model.compile('sgd', loss='mse',metrics=[tf.keras.metrics.SparseCategoricalCrossentropy()])
Note that the APIs under tf.losses, tf.metrics, and tf.optimizers are no longer used, and they are all moved to tf.keras.losses, tf.keras.metrics, and tf.keras.optimizers.
14. tf.keras.models; mainly including save, load, clone model;
and tf.keras.models.model_from_config, json, yaml
15. tf.keras.optimizers.SGD, tf.keras.optimizers.Adam, define the optimizer;
tf.keras.optimizers.schedules.LearningRateSchedule, define the learning rate lr attenuator
16. tf.keras.preprocessing; data preprocessing, mainly using the API for reading pictures, as follows the first three lines:
including reading pictures from a directory or dataframe, random brightness cropping, rotation and scaling, etc.
tf.keras.preprocessing.image.ImageDataGenerator ,
tf.keras.preprocessing.image.load_img,
tf.keras.preprocessing.image.random_brightness,
tf.keras.preprocessing.image.random_rotation
tf.keras.preprocessing.image.random_shear,
tf.keras.preprocessing.image.random_zoom
tf .keras.preprocessing.sequence,
tf.keras.preprocessing.text;
train_datagen = keras.preprocessing.image.ImageDataGenerator(
rescale = 1./255,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
fill_mode = 'nearest',)
train_generator = train_datagen.flow_from_dataframe(
train_df,
directory = './',
x_col = 'filepath',
y_col = 'class',
classes = class_names,
target_size = (height, width),
batch_size = batch_size,
seed = 7,
shuffle = True,
class_mode = 'sparse',)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size = (height, width),
batch_size = batch_size,
seed = 7,
shuffle = True,
class_mode = "categorical")
##ImageDataGenerator only defines the preprocessing of the read image, but at this time he is not sure whether to read from the folder or from the dataframe, if it is read from the dataframe, then the structure of the dataframe must be two One feature_column, one is the path of each picture, and the other is the category of the picture, as shown below:
[(’./cifar10/train/1.png’, ‘frog’),
(’./cifar10/train/2.png’, ‘truck’),
(’./cifar10/train/3.png’, ‘truck’),
(’./cifar10/train/4.png’, ‘deer’),
(’./cifar10/train/5.png’, ‘automobile’)]
[(’./cifar10/test/1.png’, ‘cat’),
(’./cifar10/test/2.png’, ‘cat’),
(’./cifar10/test/3.png’, ‘cat’),
(’./cifar10/test/4.png’, ‘cat’),
(’./cifar10/test/5.png’, ‘cat’)]
17. tf.keras.regularizers; Mainly used to achieve regularization, regularization is to add an extra item after the objective function, so that it affects the selection of the best advantage of the objective function, and can prevent overfitting. There are mainly the following APIs:
tf.keras.regularizers.l1,tf.keras.regularizers.l2,tf.keras.regularizers.l1_l2,tf.keras.regularizers.L1L2
18 、 tf.keras.utils: tf.keras.utils.get_file, tf.keras.utils.normalize
15. tf.nn
Commonly used APIs under
It is very close to some layer and loss under tf.keras, it has been written in tf.keras for comparison
16. tf.ragged
Commonly used APIs under
tf.ragged.constant, tf.ragged.stack, tf.ragged.range; the so-called ragged tensor is, for example, a two-dimensional matrix, then it allows each row to have a different number of elements.
17. tf.random
Commonly used APIs under
tf.random.categorical, tf.random.normal, tf.random.gamma, tf.random.shuffle,
tf.random.uniform; used to define some random numbers, including normal distribution
18. tf.saved_model
Commonly used APIs under
tf.saved_model.save, tf.saved_model.load; used to save and load models, there are also under tf.keras.models
19. tf.signal
Commonly used APIs under (digital signal processing related functions)
fft、fft2d、fft3d、ifft、ifft2d、ifft3d;fftshift、ifftshift;hamming_window、hann_window;
irfft、irfft2d、irfft3d、rfft、rfft2d、rfft3d等等
Twenty. tf.sparse
Commonly used APIs (mainly used for processing sparsetensor
)
1、定义sparsetensor:tf.sparse.SparseTensor
tf.sparse.SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
2、操作sparsetensor:to_dense、to_indicator、split、transpose、reshape、maximum、expand_dims、reduce_sum、reduce_max、add、concat等等
21. tf.string
Commonly used APIs under (mainly used to define and manipulate strings)
as_string, bytes_split (divided by bytes), format, join, split, strip, lower, upper, to_number, Substr
22. tf.test
Commonly used APIs under (mainly used to view GPU related information)
tf.test.is_gpu_available、tf.test.gpu_device_name、tf.test.is_built_with_cuda