declearn.model.api.Vector
Bases: Generic[T]
Abstract class defining an API to manipulate (sets of) data arrays.
A Vector is an abstraction used to wrap a collection of data structures (numpy arrays, tensorflow or torch tensors, etc.). It enables writing algorithms and operations on such structures, agnostic of their actual implementation support.
Use vector.coefs
to access the stored coefficients.
Any concrete Vector subclass should:
- add type checks to
__init__
to control wrapped coefficients' type - opt. override
_op_...
properties to define compatible operators - implement the abstract operators (
sign
,maximum
,minimum
...) - opt. override
pack
andunpack
to enable their serialization - opt. extend
compatible_vector_types
to specify their compatibility with other Vector subclasses - opt. override the
dtypes
andshapes
methods
Source code in declearn/model/api/_vector.py
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
compatible_vector_types: Set[Type[Vector]]
property
Compatible Vector types, that may be combined into this.
If VectorTypeA is listed as compatible with VectorTypeB,
then (VectorTypeB + VectorTypeA) -> VectorTypeB
(both
for addition and any basic operator). In general, such
compatibility should be declared in one way only, hence
(VectorTypeA + VectorTypeB) -> VectorTypeB
as well.
This is for example the case is VectorTypeB stores numpy arrays while VectorTypeA stores tensorflow tensors since tf.add(tensor, array) returns a tensor, not an array.
If two vector types were inter-compatible, the above operations would result in a vector of the left-hand type.
__eq__(other)
abstractmethod
Equality operator for Vector classes.
Two Vectors should be deemed equal if they have the same specs (same keys, shapes and dtypes) and the same values.
Otherwise, this magic method should return False.
Source code in declearn/model/api/_vector.py
446 447 448 449 450 451 452 453 454 455 456 457 |
|
__init__(coefs)
Instantiate the Vector to wrap a collection of data arrays.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coefs |
Dict[str, T]
|
Dict grouping a named collection of data arrays.
The supported types of that dict's values depends
on the concrete |
required |
Source code in declearn/model/api/_vector.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
apply_func(func, *args, **kwargs)
Apply a given function to the wrapped coefficients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable[..., T]
|
Function to be applied to each and every coefficient (data array) wrapped by this Vector, that must return a similar array (same type, shape and dtype). |
required |
Any *args
and **kwargs
to func
may also be passed.
Returns:
Name | Type | Description |
---|---|---|
vector |
Self
|
Vector similar to the present one, wrapping the resulting data. |
Source code in declearn/model/api/_vector.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|
build(coefs)
staticmethod
Instantiate a Vector, inferring its exact subtype from coefs'.
'Vector' is an abstract class. Its subclasses, however, are
expected to be designed for wrapping specific types of data
structures. Using the register_vector_type
decorator, the
implemented Vector subclasses can be made buildable through
this staticmethod, which relies on input coefficients' type
analysis to infer the Vector type to instantiate and return.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coefs |
Dict[str, T]
|
Dict grouping a named collection of data arrays, that all belong to the same framework. |
required |
Returns:
Name | Type | Description |
---|---|---|
vector |
Vector
|
Vector instance, the concrete class of which depends
on that of the values of the |
Source code in declearn/model/api/_vector.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
build_from_specs(values, v_spec)
staticmethod
Unflatten a Vector from a list of float and a metadata dict.
This staticmethod is a more generic version of the unflatten
classmethod, that may be called from the Vector
ABC directly
in order to recreate a Vector from its specifications without
prior knowledge of the output Vector subclass, retrieved from
the v_spec
information rather than from end-user knowledge.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values |
List[float]
|
List of concatenated float (or int) values of the Vector. |
required |
v_spec |
VectorSpec
|
VectorSpec instance storing metadata enabling to convert the flattened values into a Vector instance of a proper type and with proper data shapes and dtypes. |
required |
Returns:
Name | Type | Description |
---|---|---|
vector |
Vector
|
Recovered Vector matching the one that was flattened into the input arguments. |
Raises:
Type | Description |
---|---|
KeyError
|
If the input specifications do not enable retrieving the Vector subclass constructor to use. If the input specifications do not match expectations from that target Vector subclass. |
TypeError
|
If the inputs do not match type expectations. |
ValueError
|
If the input values cannot be turned back into the shapes and dtypes specified by input vector specs. |
Source code in declearn/model/api/_vector.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
dtypes()
Return a dict storing the dtype of each coefficient.
Returns:
Name | Type | Description |
---|---|---|
dtypes |
dict[str, tuple(int, ...)]
|
Dict containing the dtype of each of the wrapped data array, indexed by the coefficient's name. The dtypes are parsed as a string, the values of which may vary depending on the concrete framework of the Vector. |
Source code in declearn/model/api/_vector.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
flatten()
abstractmethod
Flatten this Vector into a list of float and a metadata dict.
If this Vector contains any sparse data structure, it is expected that zero-valued coefficients are part of the output values, as the (un)flattening methods are aimed at enabling SecAgg features, that may involve summing up tensors with distinct sparsity, which cannot be easily anticipated in a decentralized fashin.
Returns:
Name | Type | Description |
---|---|---|
values |
List[float]
|
List of concatenated float (or int) values from this Vector. |
v_spec |
VectorSpec
|
VectorSpec instance storing metadata enabling to convert the flattened values into a Vector instance similar to this one. |
Source code in declearn/model/api/_vector.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
get_vector_specs()
Return a VectorSpec instance storing metadata on this Vector.
This method is mostly meant to be called by the flatten
class
method, and is merely implemented to define some common grounds
across all Vector subclasses.
Source code in declearn/model/api/_vector.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
|
maximum(other)
abstractmethod
Compute coef.-wise, element-wise maximum wrt to another Vector.
Source code in declearn/model/api/_vector.py
472 473 474 475 476 477 |
|
minimum(other)
abstractmethod
Compute coef.-wise, element-wise minimum wrt to another Vector.
Source code in declearn/model/api/_vector.py
465 466 467 468 469 470 |
|
pack()
Return a JSON-serializable dict representation of this Vector.
This method must return a dict that can be serialized to and from
JSON using the JSON-extending declearn hooks (see json_pack
and
json_unpack
functions from the declearn.utils
module).
The counterpart unpack
method may be used to re-create a Vector
from its "packed" dict representation.
Returns:
Name | Type | Description |
---|---|---|
packed |
dict[str, any]
|
Dict with str keys, that may be serialized to and from JSON
using the |
Source code in declearn/model/api/_vector.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
shapes()
Return a dict storing the shape of each coefficient.
Returns:
Name | Type | Description |
---|---|---|
shapes |
dict[str, tuple(int, ...)]
|
Dict containing the shape of each of the wrapped data array, indexed by the coefficient's name. |
Source code in declearn/model/api/_vector.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
sign()
abstractmethod
Return a Vector storing the sign of each coefficient.
Source code in declearn/model/api/_vector.py
459 460 461 462 463 |
|
sum()
abstractmethod
Compute coefficient-wise sum of elements.
Source code in declearn/model/api/_vector.py
479 480 481 482 483 |
|
unflatten(values, v_spec)
abstractmethod
classmethod
Unflatten a Vector from a list of float and a metadata dict.
This is the counterpart method to flatten
and is defined at
the level of each Vector subclass. You may alternatively use
the Vector.build_from_specs
generic method to automate the
identification of the target Vector subclass and pass inputs
to its unflatten
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values |
List[float]
|
List of concatenated float (or int) values of the Vector. |
required |
v_spec |
VectorSpec
|
VectorSpec instance storing metadata enabling to convert the flattened values into an instance of this Vector class, with proper data shapes and dtypes. |
required |
Returns:
Name | Type | Description |
---|---|---|
vector |
Self
|
Recovered Vector matching the one that was flattened into the input arguments. |
Raises:
Type | Description |
---|---|
KeyError
|
If the input specifications do not match expectations from this specific Vector subclass. |
ValueError
|
If the input values cannot be turned back into the shapes and dtypes specified by input vector specs. |
Source code in declearn/model/api/_vector.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
|
unpack(data)
classmethod
Instantiate a Vector from its "packed" dict representation.
This method is the counterpart to the pack
one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, Any]
|
Dict produced by the |
required |
Returns:
Name | Type | Description |
---|---|---|
vector |
Self
|
Instance of this Vector subclass, (re-)created from the inputs. |
Source code in declearn/model/api/_vector.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|