create_pickle(value=None, filename=None)
Serializes and saves a Python object to a file using pickle.
This function takes a Python object and a filename, and serializes the object to a file with the specified name. If either the value or the filename is not provided, the function raises an exception.
Parameters: - value: The Python object to serialize. Must not be None for the operation to proceed. - filename: The name of the file where the serialized object should be saved. Must not be None for the operation to proceed.
Raises:
- Exception: If either value or filename is None, indicating incomplete arguments for the operation.
Source code in utils.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
device_init(device=None)
Initializes and returns a PyTorch device based on the specified preference and availability.
This function is designed to facilitate the dynamic selection of the computational device for PyTorch operations, allowing the user to preferentially select 'mps' (Apple Silicon GPU) or 'cuda' (NVIDIA GPU) devices. If the preferred device is not available, or if no preference is specified, it defaults to using the CPU.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
device = device_init("cuda") print(device) device(type='cuda') # This output depends on the availability of CUDA.
Note
- The 'mps' device option is specifically for machines with Apple Silicon GPUs.
- It's recommended to explicitly specify your device preference when calling this function to ensure your computations are performed on the desired hardware, when available.
Source code in utils.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
total_params(model)
Calculates and prints the total number of parameters in a PyTorch model.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
model = torch.nn.Linear(10, 5) total_params(model) 55
Source code in utils.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
weight_init(m)
Applies custom weight initialization to convolutional and batch normalization layers of a neural network.
This function initializes the weights of convolutional layers (Conv2d, ConvTranspose2d, etc.) with a normal distribution
centered at 0 with a standard deviation of 0.02. For batch normalization layers (BatchNorm2d, etc.), it initializes the weights
with a normal distribution centered at 1 with a standard deviation of 0.02, and sets the bias to 0.
This initialization can help in stabilizing the learning process in generative adversarial networks (GANs) or other deep learning models.
| Parameters: |
|
|---|
Example
model = MyModel() model.apply(weight_init)
Note
The function directly modifies the input module m and does not return any value. It is designed to be used with the apply
method of torch.nn.Module, which applies a function recursively to every submodule.
Source code in utils.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |