python os.path() module
os. The path module is mainly used to obtain the attributes of the file.
os.path.isfile(path) determines whether the path is a file
os.path.join(path1[, path2[, ...]]) Combine the directory and file name into a path
Save dictionary in python pickle format
Use the dump() function of the pickle module to save the dictionary, and use the load() function to read the dictionary from the saved file. The dump() function of the pickle module requires the dictionary and file object we want to save as parameters in order to save the dictionary as pkl file
import pickle my_dict = { 'Apple': 4, 'Banana': 2, 'Orange': 6, 'Grapes': 11} with open("myDictionary.pkl", "wb") as tf: pickle.dump(my_dict,tf) with open("myDictionary.pkl", "wb") as tf: new_dict = pickle.load(tf) print(new_dict.item())
Python data loading - detailed explanation of Dataset and DataLoader
python map function
The map() function maps the specified sequence according to the provided function.
The first parameter function calls the function function with each element in the parameter sequence and returns a new list containing the return value of each function function.
Syntax: map(function, iterable,...)
- Function – function
- iterable – one or more sequences
- Returns an iterator
GRU(Gate Recurrent Unit)
How to specify the GPU run code by torch or keras
CUDA when using the command line_ VISIBLE_ DEVICES=0,1,2,3 python xxx. Py to set the gpu visible to the program
You can add at the beginning of the code:
import os os.environ['CUDA_VISIBLE_DEVICES']='1'
Others are when writing code, when needed, such as models or input data, followed by
net = Net.cuda(0) torch.cuda.set_device(0) #Execute before generating network objects
keras model.fit() parameter
fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False)
keras model class official website
tf.keras.Model() Model groups layers into objects with training and reasoning characteristics
parameter
- inputs: keras.Input object or keras Input object list
- outputs:
- Name: String, the name of the model
Instantiate Model – use Functional API
Starting from the input, the link layer calls to specify the forward transfer of the model, and finally creates the model according to the input and output:
import tensorflow as tf 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)
Note: only tuples of dictionaries, lists and input tensors are supported. Nested input is not supported
You can also use the intermediate tensor to create a new Functional API, which can quickly extract the sub components of the model
inputs = keras.Input(shape=(None, None, 3)) processed = keras.layers.RandomCrop(width=32, height=32)(inputs) conv = keras.layers.Conv2D(filters=2, kernel_size=3)(processed) pooling = keras.layers.GlobalAveragePooling2D()(conv) feature = keras.layers.Dense(10)(pooling) full_model = keras.Model(inputs, feature) backbone = keras.Model(processed, conv) activations = keras.Model(conv, feature)
backbone and activations do not use keras Instead of using the input object, it is created from the keras The tensor of the inputs object is created.
Behind the scenes, layers and weights will be shared between these models so that users can train full_model, and use backbone and activations for feature extraction.
The input and output of the model can also be the nested structure of tensors. The created model is a standard functional API model, which supports all existing APIs.
Instantiate Model – subclass the Model class
In this case, you should__ init__ Define your layer in () and implement the forward transfer of the model in call().
import tensorflow as tf class MyModel(tf.keras.Model): def __init__(self): super().__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()
If the Model is subclassed, you can choose to have a train parameter (Boolean value) in call(), which can be used to specify different behaviors in training and reasoning:
import tensorflow as tf class MyModel(tf.keras.Model): def __init__(self): super().__init__() self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu) self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax) self.dropout = tf.keras.layers.Dropout(0.5) def call(self, inputs, training=False): x = self.dense1(inputs) if training: x = self.dropout(x, training=training) return self.dense2(x) model = MyModel()
Once the model is created, you can use model Compile () configures the loss and measurement of the model, using model Fit() to train the model, or use model Predict () uses the model to predict.
keras.Input object
tf.keras.Input( shape=None, batch_size=None, name=None, dtype=None, sparse=None, tensor=None, ragged=None, type_spec=None, **kwargs )
summary method
Print a summary of the network
Model.summary(line_length=None, positions=None, print_fn=None, expand_nested=False)
ValueError: if summary() is called before the model is built
get_layer method
The layer is retrieved according to its name (unique) or index. If both name and index are provided, the index takes precedence. The index is based on the order in which the horizontal graph is traversed (bottom-up)
Model.get_layer(name=None, index=None)