lucid-dl 2.7.6__py3-none-any.whl → 2.7.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lucid/nn/__init__.py +1 -0
- lucid/nn/util.py +60 -0
- {lucid_dl-2.7.6.dist-info → lucid_dl-2.7.7.dist-info}/METADATA +1 -1
- {lucid_dl-2.7.6.dist-info → lucid_dl-2.7.7.dist-info}/RECORD +7 -6
- {lucid_dl-2.7.6.dist-info → lucid_dl-2.7.7.dist-info}/WHEEL +0 -0
- {lucid_dl-2.7.6.dist-info → lucid_dl-2.7.7.dist-info}/licenses/LICENSE +0 -0
- {lucid_dl-2.7.6.dist-info → lucid_dl-2.7.7.dist-info}/top_level.txt +0 -0
lucid/nn/__init__.py
CHANGED
lucid/nn/util.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from typing import Iterable
|
|
2
|
+
|
|
3
|
+
import lucid
|
|
4
|
+
|
|
5
|
+
from lucid._tensor import Tensor
|
|
6
|
+
from lucid.types import _Scalar
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
__all__ = ["grad_norm", "clip_grad_norm", "clip_grad_value"]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _as_iter(parameters: Iterable[Tensor] | Tensor) -> list[Tensor]:
|
|
13
|
+
if isinstance(parameters, Tensor):
|
|
14
|
+
return [parameters]
|
|
15
|
+
return list(parameters)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def grad_norm(parameters: Iterable[Tensor] | Tensor, norm_type: int = 2) -> Tensor:
|
|
19
|
+
parameters = _as_iter(parameters)
|
|
20
|
+
device = parameters[0].device
|
|
21
|
+
|
|
22
|
+
params: list[Tensor] = [p for p in parameters if p.grad is not None]
|
|
23
|
+
if not params:
|
|
24
|
+
return Tensor(0.0, device=device)
|
|
25
|
+
|
|
26
|
+
norm_pow_sum = 0.0
|
|
27
|
+
for p in params:
|
|
28
|
+
param_norm = lucid.linalg.norm(lucid.ravel(p.grad), ord=norm_type).item()
|
|
29
|
+
norm_pow_sum += param_norm**norm_type
|
|
30
|
+
|
|
31
|
+
total_norm = norm_pow_sum ** (1.0 / norm_type)
|
|
32
|
+
return Tensor(total_norm, device=device)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def clip_grad_norm(
|
|
36
|
+
parameters: Iterable[Tensor] | Tensor,
|
|
37
|
+
max_norm: _Scalar,
|
|
38
|
+
norm_type: int = 2,
|
|
39
|
+
eps: float = 1e-7,
|
|
40
|
+
) -> float:
|
|
41
|
+
params: list[Tensor] = [p for p in _as_iter(parameters) if p.grad is not None]
|
|
42
|
+
total_norm = grad_norm(params, norm_type=norm_type)
|
|
43
|
+
|
|
44
|
+
clip_coef = float(max_norm) / (total_norm.item() + eps)
|
|
45
|
+
if clip_coef < 1.0:
|
|
46
|
+
for p in params:
|
|
47
|
+
p.grad = p.grad * clip_coef
|
|
48
|
+
|
|
49
|
+
return total_norm
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def clip_grad_value(parameters: Iterable[Tensor] | Tensor, clip_value: _Scalar) -> None:
|
|
53
|
+
params = [p for p in _as_iter(parameters) if p.grad is not None]
|
|
54
|
+
if not params:
|
|
55
|
+
return
|
|
56
|
+
|
|
57
|
+
lo, hi = -float(clip_value), float(clip_value)
|
|
58
|
+
for p in params:
|
|
59
|
+
g_clip = lucid.clip(p.grad, lo, hi).data
|
|
60
|
+
p.grad = g_clip
|
|
@@ -71,10 +71,11 @@ lucid/models/objdet/yolo/yolo_v3.py,sha256=B5U42Npwfg8nSgU9E261zf0cbQS9RVYrX1ADD
|
|
|
71
71
|
lucid/models/objdet/yolo/yolo_v4.py,sha256=RFbBumreXmy6s8IYZvUuhW0893ss8sx_8Vgi6KbBKWo,21467
|
|
72
72
|
lucid/models/seq2seq/__init__.py,sha256=wjsrhj4H_AcqwwbebAN8b68QBA8L6p1_12dkG2995-w,27
|
|
73
73
|
lucid/models/seq2seq/transformer.py,sha256=y5rerCs1s6jXTsVvbgscWScKpQKuSu1fezsBe7PNTRA,3513
|
|
74
|
-
lucid/nn/__init__.py,sha256=
|
|
74
|
+
lucid/nn/__init__.py,sha256=_hk6KltQIJuWXowXstMSu3TjiaTP8zMLNvGpjnA9Mpw,182
|
|
75
75
|
lucid/nn/fused.py,sha256=ZGOQmDThaGNQLC59y3M7s993K_K09ce6IZP8cFX8FUE,5498
|
|
76
76
|
lucid/nn/module.py,sha256=XvFWJ8NqXeZpr3RmKBQBz5eqT535Oi_7DaPN1Zi9gJc,21971
|
|
77
77
|
lucid/nn/parameter.py,sha256=jDaWukWecCcH9ri65SefNls66MmyTyucFolWbzSjapc,856
|
|
78
|
+
lucid/nn/util.py,sha256=Yw1iBSPrGV_r_F51qpqLYdafNE_hyaA0DPWYP-rjaig,1699
|
|
78
79
|
lucid/nn/functional/__init__.py,sha256=90Zi7jClPOiiSYx-Qkg0QTideKD6GigbWON9eFCoxzg,13869
|
|
79
80
|
lucid/nn/functional/_activation.py,sha256=nQVwArvPuwkUpLMLCNABTw96Zgw9VsPB8SyXCL6t2LM,1331
|
|
80
81
|
lucid/nn/functional/_attention.py,sha256=nrZF3-2AR03kNo1PGNszujhWlAVcab_FNQwOCWZT47I,946
|
|
@@ -119,8 +120,8 @@ lucid/visual/__init__.py,sha256=6TuFDfmXTwpLyHl7_KqBfdzW6zqHjGzIFvymjFPlvjI,21
|
|
|
119
120
|
lucid/visual/graph.py,sha256=YjpIDM_lloZARw3sCBiXPl_hT5A2gTk2fEHvwvJWXTk,4599
|
|
120
121
|
lucid/weights/__init__.py,sha256=z1AikA3rOEeckWGkYWlcZkxNlJo9Xwa39PL6ly3hWnc,8801
|
|
121
122
|
lucid/weights/__init__.pyi,sha256=lFonYC3cUx2Idolf3AEPnjFcyqcn3UDU84oJlZafqLY,3013
|
|
122
|
-
lucid_dl-2.7.
|
|
123
|
-
lucid_dl-2.7.
|
|
124
|
-
lucid_dl-2.7.
|
|
125
|
-
lucid_dl-2.7.
|
|
126
|
-
lucid_dl-2.7.
|
|
123
|
+
lucid_dl-2.7.7.dist-info/licenses/LICENSE,sha256=vxRFYnVD1IeYtsvw-KmoElfqrjxKHv1h9YTvsG54loQ,1065
|
|
124
|
+
lucid_dl-2.7.7.dist-info/METADATA,sha256=UVi3p93MdqSoYHvUkdil5C12wx5sfZy5Obw4Hkd7pxs,11260
|
|
125
|
+
lucid_dl-2.7.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
126
|
+
lucid_dl-2.7.7.dist-info/top_level.txt,sha256=uzP_qBx9iNWIHKJRlElYcBLYVqMpdm9Q1Ma63QPYbFc,6
|
|
127
|
+
lucid_dl-2.7.7.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|