Delete utils
Browse files- utils/misc.py +0 -72
utils/misc.py
DELETED
@@ -1,72 +0,0 @@
|
|
1 |
-
# Copyright 2023 solo-learn development team.
|
2 |
-
|
3 |
-
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4 |
-
# this software and associated documentation files (the "Software"), to deal in
|
5 |
-
# the Software without restriction, including without limitation the rights to use,
|
6 |
-
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
7 |
-
# Software, and to permit persons to whom the Software is furnished to do so,
|
8 |
-
# subject to the following conditions:
|
9 |
-
|
10 |
-
# The above copyright notice and this permission notice shall be included in all copies
|
11 |
-
# or substantial portions of the Software.
|
12 |
-
|
13 |
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
14 |
-
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
15 |
-
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
16 |
-
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
17 |
-
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
18 |
-
# DEALINGS IN THE SOFTWARE.
|
19 |
-
|
20 |
-
import logging
|
21 |
-
import math
|
22 |
-
|
23 |
-
import torch
|
24 |
-
|
25 |
-
|
26 |
-
def _no_grad_trunc_normal_(tensor, mean, std, a, b):
|
27 |
-
"""Copy & paste from PyTorch official master until it's in a few official releases - RW
|
28 |
-
Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
|
29 |
-
"""
|
30 |
-
|
31 |
-
def norm_cdf(x):
|
32 |
-
"""Computes standard normal cumulative distribution function"""
|
33 |
-
|
34 |
-
return (1.0 + math.erf(x / math.sqrt(2.0))) / 2.0
|
35 |
-
|
36 |
-
if (mean < a - 2 * std) or (mean > b + 2 * std):
|
37 |
-
logging.warn(
|
38 |
-
"mean is more than 2 std from [a, b] in nn.init.trunc_normal_. "
|
39 |
-
"The distribution of values may be incorrect.",
|
40 |
-
stacklevel=2,
|
41 |
-
)
|
42 |
-
|
43 |
-
with torch.no_grad():
|
44 |
-
# Values are generated by using a truncated uniform distribution and
|
45 |
-
# then using the inverse CDF for the normal distribution.
|
46 |
-
# Get upper and lower cdf values
|
47 |
-
l = norm_cdf((a - mean) / std)
|
48 |
-
u = norm_cdf((b - mean) / std)
|
49 |
-
|
50 |
-
# Uniformly fill tensor with values from [l, u], then translate to
|
51 |
-
# [2l-1, 2u-1].
|
52 |
-
tensor.uniform_(2 * l - 1, 2 * u - 1)
|
53 |
-
|
54 |
-
# Use inverse cdf transform for normal distribution to get truncated
|
55 |
-
# standard normal
|
56 |
-
tensor.erfinv_()
|
57 |
-
|
58 |
-
# Transform to proper mean, std
|
59 |
-
tensor.mul_(std * math.sqrt(2.0))
|
60 |
-
tensor.add_(mean)
|
61 |
-
|
62 |
-
# Clamp to ensure it's in the proper range
|
63 |
-
tensor.clamp_(min=a, max=b)
|
64 |
-
return tensor
|
65 |
-
|
66 |
-
|
67 |
-
def trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0):
|
68 |
-
"""Copy & paste from PyTorch official master until it's in a few official releases - RW
|
69 |
-
Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
|
70 |
-
"""
|
71 |
-
|
72 |
-
return _no_grad_trunc_normal_(tensor, mean, std, a, b)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|