flgo.utils.fmodule
FModule
Bases: nn.Module
This module implements commonly used model-level operators like add, sub, and so on.
Example:
>>> class TestModel(FModule):
... def __init__(self):
... self.mlp = torch.nn.Linear(2,2, bias=False)
>>> m1 = TestModel()
>>> m2 = TestModel()
>>> m3 = m1+m2
>>> (m1.mlp.weight+m2.mlp.weight)==m3.mlp.weight
Source code in flgo\utils\fmodule.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 79 80 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
cos_sim(other)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Fmodule
|
the model with the same architecture of self |
required |
Returns:
Type | Description |
---|---|
the cosine similarity value of the two vectorized models |
Source code in flgo\utils\fmodule.py
93 94 95 96 97 98 99 100 101 |
|
count_parameters(output=True)
Count the parameters for this model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
bool
|
whether to output the information to the stdin (i.e. console) |
True
|
Returns:
Type | Description |
---|---|
the number of all the parameters in this model |
Source code in flgo\utils\fmodule.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
dot(other)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Fmodule
|
the model with the same architecture of self |
required |
Returns:
Type | Description |
---|---|
the dot value of the two vectorized models |
Source code in flgo\utils\fmodule.py
83 84 85 86 87 88 89 90 91 |
|
enable_grad()
All the gradients of the model parameters will be computed after calling this method
Source code in flgo\utils\fmodule.py
126 127 128 129 130 131 |
|
freeze_grad()
All the gradients of the model parameters won't be computed after calling this method
Source code in flgo\utils\fmodule.py
119 120 121 122 123 124 |
|
get_device()
Returns:
Type | Description |
---|---|
the device of the tensors of this model |
Source code in flgo\utils\fmodule.py
159 160 161 162 163 164 |
|
has_nan()
Check whether there is nan value in model's parameters
Returns:
Name | Type | Description |
---|---|---|
res |
bool
|
True if there is nan value |
Source code in flgo\utils\fmodule.py
148 149 150 151 152 153 154 155 156 157 |
|
load(other)
Set the values of model parameters the same as the values of another model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Fmodule
|
the model with the same architecture of self |
required |
Source code in flgo\utils\fmodule.py
109 110 111 112 113 114 115 116 117 |
|
norm(p=2)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p |
float
|
p-norm |
2
|
Returns:
Type | Description |
---|---|
the scale value of the p-norm of vectorized model parameters |
Source code in flgo\utils\fmodule.py
66 67 68 69 70 71 72 73 74 |
|
normalize()
Normalize the parameters of self to enable self.norm(2)=1
Source code in flgo\utils\fmodule.py
141 142 143 144 145 146 |
|
zero_dict()
Set all the values of model parameters to be zero
Source code in flgo\utils\fmodule.py
133 134 135 136 137 138 139 |
|
zeros_like()
Returns:
Type | Description |
---|---|
a new model with the same architecture and all the parameters being set zero |
Source code in flgo\utils\fmodule.py
76 77 78 79 80 81 |
|
cos_sim(m1, m2)
The cosine similarity value of the two models res=m1·m2/(||m1||*||m2||)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m1 |
FModule
|
model 1 |
required |
m2 |
FModule
|
model 2 |
required |
Returns:
Type | Description |
---|---|
The cosine similarity value of the two models |
Source code in flgo\utils\fmodule.py
214 215 216 217 218 219 220 221 222 223 224 225 |
|
dot(m1, m2)
The dot value of the two models res = m1·m2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m1 |
FModule
|
model 1 |
required |
m2 |
FModule
|
model 2 |
required |
Returns:
Type | Description |
---|---|
The dot value of the two models |
Source code in flgo\utils\fmodule.py
201 202 203 204 205 206 207 208 209 210 211 212 |
|
element_wise_func(m, func)
The element-wise function on this model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m |
FModule
|
the model |
required |
func |
element-wise function |
required |
Returns:
Type | Description |
---|---|
The new model whose parameters satisfy mi=func(mi) |
Source code in flgo\utils\fmodule.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
exp(m)
The element-wise res=exp(m) where all the model parameters satisfy mi=exp(mi)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m |
FModule
|
the model |
required |
Returns:
Type | Description |
---|---|
The new model whose parameters satisfy mi=exp(mi) |
Source code in flgo\utils\fmodule.py
227 228 229 230 231 232 233 234 235 236 237 |
|
get_module_from_model(model, res=None)
Walk through all the sub modules of a model and return them as a list
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
FModule
|
model |
required |
res |
None
|
should be remained None |
None
|
Returns:
Type | Description |
---|---|
The list of all the sub-modules of a model |
Source code in flgo\utils\fmodule.py
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
|
log(m)
The element-wise res=log(m) where all the model parameters satisfy mi=log(mi)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m |
FModule
|
the model |
required |
Returns:
Type | Description |
---|---|
The new model whose parameters satisfy mi=log(mi) |
Source code in flgo\utils\fmodule.py
239 240 241 242 243 244 245 246 247 248 249 |
|
normalize(m)
The new model that is the normalized version of the input model m=m/||m||_2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m |
FModule
|
the model |
required |
Returns:
Type | Description |
---|---|
The new model that is the normalized version of the input model |
Source code in flgo\utils\fmodule.py
189 190 191 192 193 194 195 196 197 198 199 |
|
with_multi_gpus(func)
Decorate functions whose first parameter is model to carry out all the operations on the same device
Source code in flgo\utils\fmodule.py
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 |
|