Skip to content

ding.utils.loader.norm

ding.utils.loader.norm

INormClass

Overview

The norm class.

Interfaces: __call__, __add__, __radd__, __sub__, __rsub__, __mul__, __rmul__, __matmul__, __rmatmul__, __truediv__, __rtruediv__, __floordiv__, __rfloordiv__, __mod__, __rmod__, __pow__, __rpow__, __lshift__, __rlshift__, __rshift__, __rrshift__, __and__, __rand__, __or__, __ror__, __xor__, __rxor__, __invert__, __pos__, __neg__, __eq__, __ne__, __lt__, __le__, __gt__, __ge__

__call__(value)

Overview

Call the norm.

Arguments: - value (:obj:Any): The value to be normalized.

__add__(other)

Overview

Add the norm.

Arguments: - other (:obj:Any): The other norm.

__radd__(other)

Overview

Add the norm.

Arguments: - other (:obj:Any): The other norm.

__sub__(other)

Overview

Subtract the norm.

Arguments: - other (:obj:Any): The other norm.

__rsub__(other)

Overview

Subtract the norm.

Arguments: - other (:obj:Any): The other norm.

__mul__(other)

Overview

Multiply the norm.

Arguments: - other (:obj:Any): The other norm.

__rmul__(other)

Overview

Multiply the norm.

Arguments: - other (:obj:Any): The other norm.

__matmul__(other)

Overview

Matrix multiply the norm.

Arguments: - other (:obj:Any): The other norm.

__rmatmul__(other)

Overview

Matrix multiply the norm.

Arguments: - other (:obj:Any): The other norm.

__truediv__(other)

Overview

Divide the norm.

Arguments: - other (:obj:Any): The other norm.

__rtruediv__(other)

Overview

Divide the norm.

Arguments: - other (:obj:Any): The other norm.

__floordiv__(other)

Overview

Floor divide the norm.

Arguments: - other (:obj:Any): The other norm.

__rfloordiv__(other)

Overview

Floor divide the norm.

Arguments: - other (:obj:Any): The other norm.

__mod__(other)

Overview

Mod the norm.

Arguments: - other (:obj:Any): The other norm.

__rmod__(other)

Overview

Mod the norm.

Arguments: - other (:obj:Any): The other norm.

__pow__(power, modulo=None)

Overview

Power the norm.

Arguments: - power (:obj:Any): The power. - modulo (:obj:Any): The modulo.

__rpow__(other)

Overview

Power the norm.

Arguments: - other (:obj:Any): The other norm.

__lshift__(other)

Overview

Lshift the norm.

Arguments: - other (:obj:Any): The other norm.

__rlshift__(other)

Overview

Lshift the norm.

Arguments: - other (:obj:Any): The other norm.

__rshift__(other)

Overview

Rshift the norm.

Arguments: - other (:obj:Any): The other norm.

__rrshift__(other)

Overview

Rshift the norm.

Arguments: - other (:obj:Any): The other norm.

__and__(other)

Overview

And operation the norm.

Arguments: - other (:obj:Any): The other norm.

__rand__(other)

Overview

And operation the norm.

Arguments: - other (:obj:Any): The other norm.

__or__(other)

Overview

Or operation the norm.

Arguments: - other (:obj:Any): The other norm.

__ror__(other)

Overview

Or operation the norm.

Arguments: - other (:obj:Any): The other norm.

__xor__(other)

Overview

Xor operation the norm.

Arguments: - other (:obj:Any): The other norm.

__rxor__(other)

Overview

Xor operation the norm.

Arguments: - other (:obj:Any): The other norm.

__invert__()

Overview

Invert the norm.

__pos__()

Overview

Positive the norm.

__neg__()

Overview

Negative the norm.

__eq__(other)

Overview

Compare the norm if they are equal.

Arguments: - other (:obj:Any): The other norm.

__ne__(other)

Overview

Compare the norm if they are not equal.

Arguments: - other (:obj:Any): The other norm.

__lt__(other)

Overview

Compare the norm if it is less than the other norm.

Arguments: - other (:obj:Any): The other norm.

__le__(other)

Overview

Compare the norm if it is less than or equal to the other norm.

Arguments: - other (:obj:Any): The other norm.

__gt__(other)

Overview

Compare the norm if it is greater than the other norm.

Arguments: - other (:obj:Any): The other norm.

__ge__(other)

Overview

Compare the norm if it is greater than or equal to the other norm.

Arguments: - other (:obj:Any): The other norm.

norm(value)

Overview

Convert value to norm.

Arguments: - value (:obj:Any): The value to be converted.

normfunc(func)

Overview

Convert function to norm function.

Arguments: - func (:obj:Callable[[Any], Any]): The function to be converted.

lcmp(first, *items)

Overview

Compare the items.

Arguments: - first (:obj:Any): The first item. - items (:obj:Any): The other items.

Full Source Code

../ding/utils/loader/norm.py

1import operator 2from abc import abstractmethod 3from functools import wraps 4from typing import Callable, Any 5 6from .base import ILoaderClass 7 8 9def _callable_to_norm(func: Callable[[Any], Any]) -> 'INormClass': 10 """ 11 Overview: 12 Convert callable to norm. 13 Arguments: 14 - func (:obj:`Callable[[Any], Any]`): The callable to be converted. 15 """ 16 17 class _Norm(INormClass): 18 19 def _call(self, value): 20 return func(value) 21 22 return _Norm() 23 24 25def norm(value) -> 'INormClass': 26 """ 27 Overview: 28 Convert value to norm. 29 Arguments: 30 - value (:obj:`Any`): The value to be converted. 31 """ 32 33 if isinstance(value, INormClass): 34 return value 35 elif isinstance(value, ILoaderClass): 36 return _callable_to_norm(value) 37 else: 38 return _callable_to_norm(lambda v: value) 39 40 41def normfunc(func): 42 """ 43 Overview: 44 Convert function to norm function. 45 Arguments: 46 - func (:obj:`Callable[[Any], Any]`): The function to be converted. 47 """ 48 49 @wraps(func) 50 def _new_func(*args_norm, **kwargs_norm): 51 args_norm = [norm(item) for item in args_norm] 52 kwargs_norm = {key: norm(value) for key, value in kwargs_norm.items()} 53 54 def _callable(v): 55 args = [item(v) for item in args_norm] 56 kwargs = {key: value(v) for key, value in kwargs_norm.items()} 57 return func(*args, **kwargs) 58 59 return _callable_to_norm(_callable) 60 61 return _new_func 62 63 64UNARY_FUNC = Callable[[Any], Any] 65BINARY_FUNC = Callable[[Any, Any], Any] 66 67 68def _unary(a: 'INormClass', func: UNARY_FUNC) -> 'INormClass': 69 """ 70 Overview: 71 Create a unary norm. 72 Arguments: 73 - a (:obj:`INormClass`): The norm. 74 - func (:obj:`UNARY_FUNC`): The function. 75 """ 76 77 return _callable_to_norm(lambda v: func(a(v))) 78 79 80def _binary(a: 'INormClass', b: 'INormClass', func: BINARY_FUNC) -> 'INormClass': 81 """ 82 Overview: 83 Create a binary norm. 84 Arguments: 85 - a (:obj:`INormClass`): The first norm. 86 - b (:obj:`INormClass`): The second norm. 87 - func (:obj:`BINARY_FUNC`): The function. 88 """ 89 return _callable_to_norm(lambda v: func(a(v), b(v))) 90 91 92def _binary_reducing(func: BINARY_FUNC, zero): 93 """ 94 Overview: 95 Create a binary reducing norm. 96 Arguments: 97 - func (:obj:`BINARY_FUNC`): The function. 98 - zero (:obj:`Any`): The zero value. 99 """ 100 101 @wraps(func) 102 def _new_func(*args) -> 'INormClass': 103 _sum = norm(zero) 104 for item in args: 105 _sum = _binary(_sum, norm(item), func) 106 return _sum 107 108 return _new_func 109 110 111class INormClass: 112 """ 113 Overview: 114 The norm class. 115 Interfaces: 116 ``__call__``, ``__add__``, ``__radd__``, ``__sub__``, ``__rsub__``, ``__mul__``, ``__rmul__``, ``__matmul__``, 117 ``__rmatmul__``, ``__truediv__``, ``__rtruediv__``, ``__floordiv__``, ``__rfloordiv__``, ``__mod__``, 118 ``__rmod__``, ``__pow__``, ``__rpow__``, ``__lshift__``, ``__rlshift__``, ``__rshift__``, ``__rrshift__``, 119 ``__and__``, ``__rand__``, ``__or__``, ``__ror__``, ``__xor__``, ``__rxor__``, ``__invert__``, ``__pos__``, 120 ``__neg__``, ``__eq__``, ``__ne__``, ``__lt__``, ``__le__``, ``__gt__``, ``__ge__`` 121 """ 122 123 @abstractmethod 124 def _call(self, value): 125 """ 126 Overview: 127 Call the norm. 128 Arguments: 129 - value (:obj:`Any`): The value to be normalized. 130 """ 131 132 raise NotImplementedError 133 134 def __call__(self, value): 135 """ 136 Overview: 137 Call the norm. 138 Arguments: 139 - value (:obj:`Any`): The value to be normalized. 140 """ 141 142 return self._call(value) 143 144 def __add__(self, other): 145 """ 146 Overview: 147 Add the norm. 148 Arguments: 149 - other (:obj:`Any`): The other norm. 150 """ 151 152 return _binary(self, norm(other), operator.__add__) 153 154 def __radd__(self, other): 155 """ 156 Overview: 157 Add the norm. 158 Arguments: 159 - other (:obj:`Any`): The other norm. 160 """ 161 162 return norm(other) + self 163 164 def __sub__(self, other): 165 """ 166 Overview: 167 Subtract the norm. 168 Arguments: 169 - other (:obj:`Any`): The other norm. 170 """ 171 172 return _binary(self, norm(other), operator.__sub__) 173 174 def __rsub__(self, other): 175 """ 176 Overview: 177 Subtract the norm. 178 Arguments: 179 - other (:obj:`Any`): The other norm. 180 """ 181 182 return norm(other) - self 183 184 def __mul__(self, other): 185 """ 186 Overview: 187 Multiply the norm. 188 Arguments: 189 - other (:obj:`Any`): The other norm. 190 """ 191 192 return _binary(self, norm(other), operator.__mul__) 193 194 def __rmul__(self, other): 195 """ 196 Overview: 197 Multiply the norm. 198 Arguments: 199 - other (:obj:`Any`): The other norm. 200 """ 201 202 return norm(other) * self 203 204 def __matmul__(self, other): 205 """ 206 Overview: 207 Matrix multiply the norm. 208 Arguments: 209 - other (:obj:`Any`): The other norm. 210 """ 211 212 return _binary(self, norm(other), operator.__matmul__) 213 214 def __rmatmul__(self, other): 215 """ 216 Overview: 217 Matrix multiply the norm. 218 Arguments: 219 - other (:obj:`Any`): The other norm. 220 """ 221 222 return norm(other) @ self 223 224 def __truediv__(self, other): 225 """ 226 Overview: 227 Divide the norm. 228 Arguments: 229 - other (:obj:`Any`): The other norm. 230 """ 231 232 return _binary(self, norm(other), operator.__truediv__) 233 234 def __rtruediv__(self, other): 235 """ 236 Overview: 237 Divide the norm. 238 Arguments: 239 - other (:obj:`Any`): The other norm. 240 """ 241 242 return norm(other) / self 243 244 def __floordiv__(self, other): 245 """ 246 Overview: 247 Floor divide the norm. 248 Arguments: 249 - other (:obj:`Any`): The other norm. 250 """ 251 252 return _binary(self, norm(other), operator.__floordiv__) 253 254 def __rfloordiv__(self, other): 255 """ 256 Overview: 257 Floor divide the norm. 258 Arguments: 259 - other (:obj:`Any`): The other norm. 260 """ 261 262 return norm(other) // self 263 264 def __mod__(self, other): 265 """ 266 Overview: 267 Mod the norm. 268 Arguments: 269 - other (:obj:`Any`): The other norm. 270 """ 271 272 return _binary(self, norm(other), operator.__mod__) 273 274 def __rmod__(self, other): 275 """ 276 Overview: 277 Mod the norm. 278 Arguments: 279 - other (:obj:`Any`): The other norm. 280 """ 281 282 return norm(other) % self 283 284 def __pow__(self, power, modulo=None): 285 """ 286 Overview: 287 Power the norm. 288 Arguments: 289 - power (:obj:`Any`): The power. 290 - modulo (:obj:`Any`): The modulo. 291 """ 292 293 return _binary(self, norm(power), operator.__pow__) 294 295 def __rpow__(self, other): 296 """ 297 Overview: 298 Power the norm. 299 Arguments: 300 - other (:obj:`Any`): The other norm. 301 """ 302 303 return norm(other) ** self 304 305 def __lshift__(self, other): 306 """ 307 Overview: 308 Lshift the norm. 309 Arguments: 310 - other (:obj:`Any`): The other norm. 311 """ 312 313 return _binary(self, norm(other), operator.__lshift__) 314 315 def __rlshift__(self, other): 316 """ 317 Overview: 318 Lshift the norm. 319 Arguments: 320 - other (:obj:`Any`): The other norm. 321 """ 322 323 return norm(other) << self 324 325 def __rshift__(self, other): 326 """ 327 Overview: 328 Rshift the norm. 329 Arguments: 330 - other (:obj:`Any`): The other norm. 331 """ 332 333 return _binary(self, norm(other), operator.__rshift__) 334 335 def __rrshift__(self, other): 336 """ 337 Overview: 338 Rshift the norm. 339 Arguments: 340 - other (:obj:`Any`): The other norm. 341 """ 342 343 return norm(other) >> self 344 345 def __and__(self, other): 346 """ 347 Overview: 348 And operation the norm. 349 Arguments: 350 - other (:obj:`Any`): The other norm. 351 """ 352 353 return _binary(self, norm(other), operator.__and__) 354 355 def __rand__(self, other): 356 """ 357 Overview: 358 And operation the norm. 359 Arguments: 360 - other (:obj:`Any`): The other norm. 361 """ 362 363 return norm(other) & self 364 365 def __or__(self, other): 366 """ 367 Overview: 368 Or operation the norm. 369 Arguments: 370 - other (:obj:`Any`): The other norm. 371 """ 372 373 return _binary(self, norm(other), operator.__or__) 374 375 def __ror__(self, other): 376 """ 377 Overview: 378 Or operation the norm. 379 Arguments: 380 - other (:obj:`Any`): The other norm. 381 """ 382 383 return norm(other) | self 384 385 def __xor__(self, other): 386 """ 387 Overview: 388 Xor operation the norm. 389 Arguments: 390 - other (:obj:`Any`): The other norm. 391 """ 392 393 return _binary(self, norm(other), operator.__xor__) 394 395 def __rxor__(self, other): 396 """ 397 Overview: 398 Xor operation the norm. 399 Arguments: 400 - other (:obj:`Any`): The other norm. 401 """ 402 403 return norm(other) ^ self 404 405 def __invert__(self): 406 """ 407 Overview: 408 Invert the norm. 409 """ 410 411 return _unary(self, operator.__invert__) 412 413 def __pos__(self): 414 """ 415 Overview: 416 Positive the norm. 417 """ 418 419 return _unary(self, operator.__pos__) 420 421 def __neg__(self): 422 """ 423 Overview: 424 Negative the norm. 425 """ 426 427 return _unary(self, operator.__neg__) 428 429 # Attention: DO NOT USE LINKING COMPARE OPERATORS, IT WILL CAUSE ERROR. 430 def __eq__(self, other): 431 """ 432 Overview: 433 Compare the norm if they are equal. 434 Arguments: 435 - other (:obj:`Any`): The other norm. 436 """ 437 438 return _binary(self, norm(other), operator.__eq__) 439 440 def __ne__(self, other): 441 """ 442 Overview: 443 Compare the norm if they are not equal. 444 Arguments: 445 - other (:obj:`Any`): The other norm. 446 """ 447 448 return _binary(self, norm(other), operator.__ne__) 449 450 def __lt__(self, other): 451 """ 452 Overview: 453 Compare the norm if it is less than the other norm. 454 Arguments: 455 - other (:obj:`Any`): The other norm. 456 """ 457 458 return _binary(self, norm(other), operator.__lt__) 459 460 def __le__(self, other): 461 """ 462 Overview: 463 Compare the norm if it is less than or equal to the other norm. 464 Arguments: 465 - other (:obj:`Any`): The other norm. 466 """ 467 468 return _binary(self, norm(other), operator.__le__) 469 470 def __gt__(self, other): 471 """ 472 Overview: 473 Compare the norm if it is greater than the other norm. 474 Arguments: 475 - other (:obj:`Any`): The other norm. 476 """ 477 478 return _binary(self, norm(other), operator.__gt__) 479 480 def __ge__(self, other): 481 """ 482 Overview: 483 Compare the norm if it is greater than or equal to the other norm. 484 Arguments: 485 - other (:obj:`Any`): The other norm. 486 """ 487 488 return _binary(self, norm(other), operator.__ge__) 489 490 491lnot = normfunc(lambda x: not x) 492land = _binary_reducing(lambda x, y: x and y, True) 493lor = _binary_reducing(lambda x, y: x or y, True) 494 495lin = normfunc(operator.__contains__) 496lis = normfunc(operator.is_) 497lisnot = normfunc(operator.is_not) 498 499lsum = _binary_reducing(lambda x, y: x + y, 0) 500 501_COMPARE_OPERATORS = { 502 '!=': operator.__ne__, 503 '==': operator.__eq__, 504 '<': operator.__lt__, 505 '<=': operator.__le__, 506 '>': operator.__gt__, 507 '>=': operator.__ge__, 508} 509 510 511@normfunc 512def lcmp(first, *items): 513 """ 514 Overview: 515 Compare the items. 516 Arguments: 517 - first (:obj:`Any`): The first item. 518 - items (:obj:`Any`): The other items. 519 """ 520 521 if len(items) % 2 == 1: 522 raise ValueError('Count of items should be odd number but {number} found.'.format(number=len(items) + 1)) 523 524 ops, items = items[0::2], items[1::2] 525 for op in ops: 526 if op not in _COMPARE_OPERATORS.keys(): 527 raise KeyError('Invalid compare operator - {op}.'.format(op=repr(op))) 528 529 _last = first 530 for op, item in zip(ops, items): 531 if not _COMPARE_OPERATORS[op](_last, item): 532 return False 533 _last = item 534 535 return True