ltnjax ¶
The folder ltn contains the modified logictensornetworks framework from logictensornetworks/logictensornetworks and tommasocarraro/LTNtorch.
Modules:
| Name | Description |
|---|---|
core | |
fuzzy_ops | |
Classes:
| Name | Description |
|---|---|
Connective | Class representing an LTN connective. |
Constant | The class representing constants. |
Function | Class representing LTN functions. |
Predicate | Class representing an LTN predicate. |
Quantifier | Class representing an LTN quantifier. |
Variable | Class representing an LTN variable. |
Functions:
| Name | Description |
|---|---|
diag | Diagonalizes |
undiag | Resets the |
Attributes¶
__all__ module-attribute ¶
__all__ = [
"Connective",
"Constant",
"Function",
"Predicate",
"Quantifier",
"Variable",
"diag",
"undiag",
]
Classes¶
Connective ¶
Class representing an LTN connective.
Wrapper for connectives that aggregates given LTNObject objects according a given aggregator operation connective_op and also broadcasts variables, see _broadcast_exprs.
Attributes:
| Name | Type | Description |
|---|---|---|
connective_op | Aggregation function. |
Methods:
| Name | Description |
|---|---|
__call__ | Applies the connective using the given |
__init__ | Constructor. |
Source code in src/ltnjax/core.py
Attributes¶
Methods:¶
__call__ ¶
Applies the connective using the given connective_op.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| LTNObject | Tuple of LTN objects. | () |
| Any | Further arguments to pass to | {} |
Returns:
| Type | Description |
|---|---|
LTNObject | The resulting LTNObject object that |
LTNObject | combines the given |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If number of |
Source code in src/ltnjax/core.py
__init__ ¶
__init__(connective_op: ConnectiveOperator) -> None
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| ConnectiveOperator | Aggregation function. | required |
Constant ¶
Bases: LTNObject
flowchart TD
ltnjax.Constant[Constant]
ltnjax.core.LTNObject[LTNObject]
ltnjax.core.LTNObject --> ltnjax.Constant
click ltnjax.Constant href "" "ltnjax.Constant"
click ltnjax.core.LTNObject href "" "ltnjax.core.LTNObject"
The class representing constants.
A constant can be a tensor of any rank.
Attributes:
| Name | Type | Description |
|---|---|---|
value | Value of the constant that is a array of an arbitrary rank. | |
free_vars | list[VarLabel] | The free variables that are contained in the expression. |
trainable | list[VarLabel] | Flag indicating whether the LTN constant is trainable (embedding) or not. |
Methods:
| Name | Description |
|---|---|
__init__ | Constructor. |
__repr__ | Representation function. |
Source code in src/ltnjax/core.py
Methods:¶
__init__ ¶
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Any | An object that is convertible to an array. This includes JAX arrays, NumPy arrays, Python scalars, Python collections like lists and tuples, objects with an | required |
| bool | Flag indicating whether the LTN constant is trainable (embedding) or not. | False |
Source code in src/ltnjax/core.py
Function ¶
Bases: Module
flowchart TD
ltnjax.Function[Function]
click ltnjax.Function href "" "ltnjax.Function"
Class representing LTN functions.
A function that maps \(n\) tensors of any rank to one single tensor of any rank.
Attributes:
| Name | Type | Description |
|---|---|---|
model | A |
Note
modelwill be called with a tensor that has a batch dimension and optionally feature dimensions. The batch dimension is the axis that results from flattening the axes of the free variables. See _flatten_free_dims.
Methods:
| Name | Description |
|---|---|
__call__ | Evaluates the |
__init__ | Constructor. |
Source code in src/ltnjax/core.py
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 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
Attributes¶
Methods:¶
__call__ ¶
Evaluates the model of the given inputs and kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| LTNObject | tuple of LTNObject to apply on | () |
| Any | Further arguments to pass to | {} |
Returns:
| Type | Description |
|---|---|
LTNObject |
|
Raises:
| Type | Description |
|---|---|
TypeError | If |
Source code in src/ltnjax/core.py
__init__ ¶
__init__(model: Module | None = None, func: LambdaType | None = None) -> None
Constructor.
Initializes the LTN predicate in two different ways: 1. if model is not None, it initializes the predicate with the given nnx.Module; 2. if model is None, it uses the func as a function to define the LTN predicate. Note that, in this case, the LTN predicate is not learnable. So, the lambda function has to be used only for simple predicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Module | None | (default=None) A | None |
| LambdaType | None | (default=None) A lambda_expression. | None |
Raises:
| Type | Description |
|---|---|
ValueError | If either both |
TypeError | If |
Source code in src/ltnjax/core.py
Predicate ¶
Bases: Module
flowchart TD
ltnjax.Predicate[Predicate]
click ltnjax.Predicate href "" "ltnjax.Predicate"
Class representing an LTN predicate.
An LTN predicate is grounded as a mathematical function (either pre-defined or learnable) that maps from some n-ary domain of individuals to a real number in [0,1] (fuzzy), which can be interpreted as a truth value.
In LTNtorch, the inputs of a predicate are automatically broadcasted before the computation of the predicate, if necessary. Moreover, the output is organized in a tensor where each dimension is related to one variable given in input.
Attributes:
| Name | Type | Description |
|---|---|---|
model | A |
Note
modelwill be called with a tensor that has a batch dimension and optionally feature dimensions. The batch dimension is the axis that results from flattening the axes of the free variables. See _flatten_free_dims.
Methods:
| Name | Description |
|---|---|
__call__ | Evaluates the |
__init__ | Constructor. |
Source code in src/ltnjax/core.py
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 | |
Attributes¶
Methods:¶
__call__ ¶
Evaluates the model of the given inputs and kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| LTNObject | tuple of LTNObject to apply on | () |
| Any | Further arguments to pass to | {} |
Returns:
| Type | Description |
|---|---|
LTNObject |
|
Raises:
| Type | Description |
|---|---|
TypeError | If |
Source code in src/ltnjax/core.py
__init__ ¶
Constructor.
Initializes the LTN predicate in two different ways: 1. if model is not None, it initializes the predicate with the given nnx.Module; 2. if model is None, it uses the func as a function to define the LTN predicate. Note that, in this case, the LTN predicate is not learnable. So, the lambda function has to be used only for simple predicates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Module | None | (default=None) A | None |
| Callable | None | (default=None) A lambda_expression. | None |
Raises:
| Type | Description |
|---|---|
ValueError | If either both |
TypeError | If |
Source code in src/ltnjax/core.py
Quantifier ¶
Class representing an LTN quantifier.
Wrapper for Quantifiers. This evaluates a given LTN object wff for all variable combinations for that the condition mask is true. Then, the results will be aggregated with the aggregation operator aggreg_op.
Attributes:
| Name | Type | Description |
|---|---|---|
aggreg_op | Aggregation operator. | |
quantifier | (str = "f" | "e" |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If |
Note
It is possible that the variable-combinations are empty or that the condition mask will mask every variable-combination. In both cases, a "forall"-statement will always be true while an "exists"-statement will always be false.
- API Reference
ltnjax
Methods:
| Name | Description |
|---|---|
__call__ | Applies the quantification and outputs the resulting LTN object. |
__init__ | Constructor. |
Source code in src/ltnjax/core.py
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 | |
Attributes¶
Methods:¶
__call__ ¶
__call__(
variables: Variable | list[Variable],
wff: LTNObject,
mask: LTNObject | None = None,
**kwargs: Any,
) -> LTNObject
Applies the quantification and outputs the resulting LTN object.
As a side-effect, this removes the diagonal quantification from the given variables. Refer to undiag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Variable | list[Variable] | Variable or list of variables. | required |
| LTNObject | LTN object. | required |
| LTNObject | None | (default=None) Condition operation. | None |
| Any | Further arguments to pass to | {} |
Returns:
| Type | Description |
|---|---|
LTNObject | The resulting LTN object. |
Raises:
| Type | Description |
|---|---|
TypeError |
Source code in src/ltnjax/core.py
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 | |
__init__ ¶
__init__(aggreg_op: ConnectiveOperator, quantifier: str) -> None
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| ConnectiveOperator | Aggregation operator. | required |
| str | (str = "f" | "e" | required |
Source code in src/ltnjax/core.py
_broadcast_wff_and_mask staticmethod ¶
Broadcasts the free variables from mask to wff.
The variables of mask are put in the first axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| LTNObject | LTN object. | required |
| LTNObject | LTN object. | required |
| bool | (default=False) Boolean that decides whether we perform the operation on | False |
Returns:
| Type | Description |
|---|---|
LTNObject | The LTN object |
Source code in src/ltnjax/core.py
_transpose_free_vars staticmethod ¶
_transpose_free_vars(
expr: LTNObject, new_var_order: list[VarLabel], in_place: bool = False
) -> LTNObject
Transposes free variables.
This changes the order of variables in expr.free_vars and the axes of expr.value will be transposed accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| LTNObject | The LTN object whose variables will be transposed. | required |
| list[VarLabel] | List of variables that defines the new order. | required |
| bool | (default=False) Boolean that decides whether we perform the operation on a new copy or on the same LTN objects. | False |
Returns:
| Type | Description |
|---|---|
LTNObject | The transposed LTN object. |
Source code in src/ltnjax/core.py
Variable ¶
Bases: LTNObject
flowchart TD
ltnjax.Variable[Variable]
ltnjax.core.LTNObject[LTNObject]
ltnjax.core.LTNObject --> ltnjax.Variable
click ltnjax.Variable href "" "ltnjax.Variable"
click ltnjax.core.LTNObject href "" "ltnjax.core.LTNObject"
Class representing an LTN variable.
A variable \(x\) that can take only a finite number \(n\) of tensors of any rank, that means with or without feature dimensions.
Without feature dimensions: \(x \in \mathcal{R}^n\).
With feature dimensions \(d_1 \times \dotsc \times d_m\): \(x \in \mathcal{R}^(n \times d_1 \times \dotsc \times d_m)\).
Attributes:
| Name | Type | Description |
|---|---|---|
var_label | The name of the variable. | |
value | The array describes a batch of individuals; The first axis describes the number of individuals and the optional remaining axes are the | |
free_vars | list[VarLabel] | The free variables that are contained in the expression. |
trainable | list[VarLabel] | Flag indicating whether the LTN constant is trainable (embedding) or not. |
Raises:
| Type | Description |
|---|---|
ValueError | If |
Note
- The first dimension \(n\) of an LTN variable is associated with the number of individuals in the variable, while the other \(d\) exes are associated with the features of the individuals;
- If the Variable should contain truth values in
[0., 1.]that are updated during training, ensure that they will stay in that interval. This could be done by using sigmoid or tanh operations on the values. Avoid usingjax.numpy.clip(x, 0., 1.)during training as this will yield to gradient issues.
- API Reference
ltnjax
- API Reference
ltnjax
- API Reference
ltnjax
Methods:
| Name | Description |
|---|---|
__init__ | Constructor. |
__repr__ | Representation function. |
Source code in src/ltnjax/core.py
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 | |
Attributes¶
Methods:¶
__init__ ¶
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| VarLabel | The name of the variable. | required |
| Any | An object that is convertible to an array. This includes JAX arrays, NumPy arrays, Python scalars, Python collections like lists and tuples, objects with an The array describes a batch of individuals; The first axis describes the number of individuals and the optional remaining axes are the | required |
| bool | Flag indicating whether the LTN constant is trainable (embedding) or not. | False |
Source code in src/ltnjax/core.py
Functions:¶
diag ¶
Diagonalizes variables.
This diagonalizes a list of given Variable objects, i.e. this prepares the variables for the use of Quantifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Variable | Tuple of Variable objects to diagonalize. | () |
Returns:
| Type | Description |
|---|---|
list[Variable] | Tuple of Variable objects, but in diagonalized |
list[Variable] | form. |
Raises:
| Type | Description |
|---|---|
TypeError | If |
ValueError | If a variable in |
Source code in src/ltnjax/core.py
undiag ¶
Resets the LTN broadcasting for the given LTN variables.
In other words, it removes the diagonal quantification setting from the given variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Variable | () |
Returns:
| Type | Description |
|---|---|
list[Variable] | List of the same LTN Variable objects given in |
list[Variable] | input, with the |
Raises:
| Type | Description |
|---|---|
TypeError | If |
- API Reference
ltnjax