-
Notifications
You must be signed in to change notification settings - Fork 446
/
logic.py
1535 lines (1246 loc) · 50.1 KB
/
logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
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
##
# Copyright 2021 IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
##
from enum import auto
from importlib import import_module
from typing import Optional, Union, Tuple, Iterator, Set, List, Dict, TypeVar
import torch
import numpy as np
from . import _gm, _trace
from .. import _utils, _exceptions, utils
from ..constants import Fact, World, AutoName, Direction, Join
class Variable:
r"""Creates free variables to quantify first-order logic formulae
**Parameters**
name : str
name of the free variable
ctype : str, optional
constant of the type associated with the free variable
**Example**
```python
x = Variable('x', 'person')
x, y, z = map(Variable, ['x', 'y', 'z'])
```
"""
def __init__(self, name: str, ctype: Optional[str] = None):
self.name = name
self.ctype = ctype
def __str__(self) -> str:
r"""Returns the name of the free variable"""
return self.name
# declaration to use hinting within the class definition
_Grounding = TypeVar("_Grounding")
class _Grounding(_utils.MultiInstance, _utils.UniqueNameAssumption):
r"""Propositionalises constants for first-order logic
Returns a container for a string or a tuple of strings.
Follows the unique name assumption so that given constant(s) return the
same object
Decomposes multiple constants (from the tuple) by storing each str as a
separate grounding object but returns only the compound container.
This decomposition is used in grounding management to ensure that all
partial strings also follow the unique name assumption by returning the
same container
**Parameters**
constants : str or tuple-of-str
**Example**
```python
_Grounding('person1')
_Grounding(('person1', 'date1'))
```
**Attributes**
name : str
conversion of 'constants' param to str form
grounding_arity : int
length of the 'constants' param
partial_grounding : tuple(_Grounding)
tuple of groundings for decomposition when constants given as tuple
"""
def __init__(self, constants: Union[str, Tuple[str, ...]]):
super().__init__(constants)
self.name = str(constants)
if isinstance(constants, tuple):
self.grounding_arity = len(constants)
self.partial_grounding = tuple(
map(self._partial_grounding_from_str, constants)
)
else:
self.grounding_arity = 1
self.partial_grounding = (self,)
@classmethod
def _partial_grounding_from_str(cls, constant: str) -> _Grounding:
r"""Returns partial Grounding given grounding str"""
return _Grounding.instances[constant]
@classmethod
def ground_by_groundings(cls, *grounding: _Grounding):
r"""Reduce a tuple of groundings to a single grounding"""
return (
grounding[0]
if len(grounding) == 1
else cls.__class__(tuple(str(g) for g in grounding))
)
def __len__(self) -> int:
r"""Returns the length of the grounding arity"""
return self.grounding_arity
def __str__(self) -> str:
r"""Returns the name of the grounding"""
return self.name
@staticmethod
def eval(grounding: _Grounding) -> Union[str, Tuple[str, ...]]:
r"""Returns the original constant(s) in str or tuple-of-str form"""
return eval(grounding.name)
# declaration to use hinting within the class definition
_Formula = TypeVar("_Formula")
class _Formula:
r"""_Formula(*formula: _Formula,
name: Optional[str] = '',
world: World = World.OPEN, **kwds)
**Warnings**
Formula should not be directly instantiated
"""
unique_num: Dict[str, int] = dict()
@property
def class_name(self) -> str:
_Formula.unique_num.setdefault(self.__class__.__name__, 0)
return self.__class__.__name__
def __init__(
self,
*formula: _Formula,
name: Optional[str] = "",
arity: int = None,
world: World = World.OPEN,
**kwds,
):
# formula naming
self.name = (
name
if name
else (f"{self.class_name}_{_Formula.unique_num[self.class_name]}")
)
_Formula.unique_num[self.class_name] += 1
self.join_method = kwds.get("join", Join.INNER)
# construct edge and operand list for each formula
self.edge_list = list()
self.operands = list()
# formula arity
self.arity = arity
# inherit propositional, variables, and graphs
self.propositional = kwds.get("propositional")
self._inherit_from_subformulae(*formula)
# formula truth world assumption
_exceptions.AssertWorld(world)
self.world = world
# formula grounding table maps grounded objects to table rows
self.grounding_table = None if self.propositional else dict()
# placeholder for neural variables and functions
self.variables = list([None] * arity)
self.neuron = None
self.func = None
self.func_inv = None
def set_propositional(self, propositional: bool) -> None:
self.propositional = propositional
def _inherit_from_subformulae(self, *subformula: Union[_Formula, Tuple]) -> None:
r"""
_inherit_from_subformula(*Union[_Formulae, _Formula(*Variable)])
provides manual manual variable remapping if given with variables:
i.e. _Formula used as a function via `__call__`
alternatively inherits remapping from operands if formula is not called
"""
# inheritance variables
self.operand_vars = [None] * self.arity
self.var_remap = [None] * self.arity
self.bindings = [None] * self.arity
self.binding_str = [None] * self.arity
self.has_remapping_vars = True
subformulae = list()
for slot, f in enumerate(subformula):
# manual variable remapping from`__called__` operands
# And(Predicate(x, y)) ... Or(Predicate(x, y)) ...
if isinstance(f, tuple):
self.operand_vars[slot] = f[1]
self.var_remap[slot] = f[2]
self.bindings[slot] = f[3]
self.binding_str[slot] = f[4]
subformulae.append(f[0])
self.edge_list.append((self, f[0]))
self.operands.append(f[0])
# inherit variable remapping from 'uncalled' operands
# for higher level connective formulae
else:
if not f.propositional:
self.var_remap[slot] = f.unique_vars
self.operand_vars[slot] = f.unique_vars
self.bindings[slot] = tuple([[None]])
subformulae.append(f)
self.edge_list.append((self, f))
self.operands.append(f)
# inherit propositional flag from children
if self.propositional is None:
self.set_propositional(
False if any([not f.propositional for f in subformulae]) else True
)
# inherit variables from children
if not self.propositional and not isinstance(self, _LeafFormula):
self._update_variables(*self.var_remap)
self._update_map()
# expand formula graph to include all subformulae
if subformulae:
self.edge_list.extend([edge for f in subformulae for edge in f.edge_list])
def _update_variables(self, *variables: Tuple[Variable, ...]) -> None:
r"""
**Notes**
If the formula grounding_arity is not specified, it will be inherited
from the variables. If variables are not specified, both the
`grounding_arity` and variables are inherited from groundings
"""
self.variables = self.unique_vars = _gm.unique_variables(*variables)
self.num_unique_vars = len(self.unique_vars)
def _update_map(self) -> None:
r"""
Update the 'operand_map' to map parent groundings to operand groundings
"""
self.operand_map = [None] * len(self.operand_vars)
for op_index, op_vars in enumerate(self.var_remap):
op_map = [
var_index
for var_op in self.var_remap[op_index]
for var_index, var in enumerate(self.unique_vars)
if var == var_op
]
self.operand_map[op_index] = tuple(op_map)
def _set_world(self, world: World) -> None:
_exceptions.AssertWorld(world)
self.world = world
self.neuron.set_world(world)
@staticmethod
def _is_grounded(groundings: Union[_Grounding, str, Tuple[str, ...]]) -> bool:
return isinstance(groundings, _Grounding)
def _ground(
self,
grounding: Union[_Grounding, str, Tuple[str, ...]],
arity_match: bool = True,
) -> _Grounding:
r"""returns a single grounded object
**Example**
```python
# for arity == 1
self._ground('str_1')
```
```python
# for arity > 1
grounding = ('str_1', 'str_2', ...)
self._ground(grounding)
```
**Warning**
only grounds one object at a time due to tuple confusion
for multiple objects
"""
if _Formula._is_grounded(grounding):
return grounding
if isinstance(grounding, tuple):
if not all([type(g) in [str, None] for g in grounding]):
raise Exception(
"expected groundings as tuple of str for num_unique_vars "
f" > 1, received {type(grounding)} {grounding}"
)
if len(grounding) != self.num_unique_vars and arity_match:
raise Exception(
"expected grounding length to be of "
f"arity {self.num_unique_vars}, received {grounding}"
)
else:
if self.num_unique_vars != 1 and arity_match:
raise Exception(
f"{self} received str as grounding, expected grounding "
f"({grounding}) as a tuple of len {self.num_unique_vars}"
)
return _Grounding(grounding)
def _add_groundings(self, *groundings: _Grounding) -> None:
r"""add missing groundings to `grounding_table`
**Returns**
tuple: groundings in grounded form
**Example**
```python
# for formulae with arity == 1:
model['formula']._add_groundings(_Grounding_1, _Grounding_2)
```
```python
# for formulae with arity > 1:
groundings = ((_Grounding_1, _Grounding_7),
(_Grounding_2, _Grounding_8))
model['formula']._add_groundings(*groundings)
```
**Warning**
groundings must be given in grounded form: `self._ground('grounding')`
_add_groundings should not be directly called
- instead use `model.add_facts('node': Fact)`
"""
missing_groundings = {g for g in groundings if g not in self.grounding_table}
if len(missing_groundings) == 0:
return
table_rows = self.neuron.extend_groundings(len(missing_groundings))
self.grounding_table.update(
{g: table_rows[i] for i, g in enumerate(missing_groundings)}
)
def _add_facts(self, facts: Union[Fact, Tuple, Set, Dict]) -> None:
r"""Populate formula with fact
Facts given in bool, tuple or None, assumes a propositional formula.
Facts given in dict form assume FOL, keyed by the grounding, where the
value also required in bool, tuple or None
**Warning**
Formulae facts should not be modified directly, rather provide facts
on a per model basis
All subsequent models that are instantiated with formulae that have
existing facts will clone facts into the models
**Example**
usage from within a model
```python
# Propositional:
model['proposition']._add_facts(TRUE)
```
```python
# First-order logic
model['predicate']._add_facts({'grounding': TRUE})
```
"""
if self.propositional: # Propositional facts
_exceptions.AssertBounds(facts)
self.neuron.add_facts(facts, update_leaves=True)
else: # FOL facts
if isinstance(facts, dict): # facts given per grounding
# replace fact keys (str -> _Groundings)
groundings = tuple(facts.keys())
for g in groundings:
facts[self._ground(g)] = facts.pop(g)
# add missing groundings to `grounding_table`
groundings = tuple(facts.keys())
self._add_groundings(*groundings)
# set facts for all groundings
table_facts = {self.grounding_table[g]: facts[g] for g in groundings}
elif isinstance(facts, set): # broadcast facts across groundings
table_facts = facts
else:
raise Exception(
"FOL facts should be from [dict, set], "
f'"{self}" received {type(facts)}'
)
self.neuron.add_facts(table_facts, update_leaves=True)
def get_facts(
self, *groundings: Union[str, Tuple[str, ...], _Grounding]
) -> torch.Tensor:
r"""returns bounds_table slices
if groundings is None, the whole table will return
elif a tuple of groundings returns appropriate slices
"""
if self.propositional or len(groundings) == 0:
return self.neuron.get_facts()
table_rows = list(self.grounding_table.get(self._ground(g)) for g in groundings)
result = self.neuron.get_facts(table_rows)
return result
def get_labels(self, *groundings: str) -> torch.Tensor:
r"""returns labels if no groundings else tuple of given bounds"""
if self.propositional or len(groundings) == 0:
return self.labels
result = torch.stack([self.labels.get(self._ground(g)) for g in groundings])
return result[0] if len(groundings) == 1 else result
def _add_labels(self, labels: Union[Fact, Tuple[str, ...], Set, Dict]) -> None:
r"""Populate labels with fact
Facts given in bool, tuple or None, assumes a propositional formula.
Facts given in dict form assume FOL, keyed by the grounding, where the
value also required in bool, tuple or None
**Example**
```python
# Propositional
model.add_labels({'proposition': TRUE})
```
```python
# First-order logic
model.add_labels({'predicate': {'grounding': TRUE}})
```
"""
if self.propositional: # Propositional labels
_exceptions.AssertBounds(labels)
self.labels = _utils.fact_to_bounds(labels, True)
else: # FOL labels
if not hasattr(self, "labels"):
self.labels = dict()
if isinstance(labels, dict): # labels given per grounding
for g in tuple(labels.keys()):
# set labels for groundings, replace str keys -> Groundings
self.labels[self._ground(g)] = _utils.fact_to_bounds(
labels.pop(g), True
)
else:
raise Exception(
"FOL facts should be from [dict, set], "
f'"{self}" received {type(labels)}'
)
@property
def groundings(self) -> Set:
return {g for g in self.grounding_table}
def state(
self,
groundings: Union[
str, Tuple[str, ...], List[str], List[Tuple[str, ...]]
] = None,
to_bool: bool = False,
bounds: torch.Tensor = None,
) -> Union[torch.Tensor, Dict[Union[str, Tuple[str, ...]], torch.Tensor]]:
r"""returns the state of a single grounded fact
if to_bool flag is True, will map classical Facts to bool:
i.e. {Fact.True: True, FALSE': False}
The rest of the node states will return as str
**Notes**
see section (F.2)[https://arxiv.org/abs/2006.13155] for more
information on node states
"""
if self.propositional:
result = self.neuron.state()
else:
if bounds is None:
if groundings is None or isinstance(groundings, list):
result = {
str(g): self.state(g)
for g in (
self.grounding_table if groundings is None else groundings
)
}
return result
groundings = self._ground(groundings)
bounds = self.get_facts(groundings)
if len(bounds) == 0:
raise LookupError(f"grounding {groundings} not found in {str(self)}")
result = self.neuron.state(bounds[None, :])[0]
result = _utils.node_state(result)
return utils.fact_to_bool(result) if to_bool else result
def print(
self,
header_len: int = 50,
roundoff: int = 5,
params: bool = False,
grads: bool = False,
) -> None:
r"""
print the state of the formula
```
OPEN Proposition: A UNKNOWN (L, U)
CLOSED Proposition: B FALSE (L, U)
OPEN Predicate: P1 (x)
'const_1' TRUE (L, U)
'const_2' CONTRADICTION (L, U)
'const_3' FALSE (L, U)
OPEN Predicate: P2 (x, y)
('const_1', 'const_5') TRUE (L, U)
('const_2', 'const_6') CONTRADICTION (L, U)
('const_3', 'const_7') FALSE (L, U)
OPEN And: And_0 (x, y)
Bindings: P1 (x: 'const_1'), P2 (x: 'const_2', y: ['const_3', ...])
('const_1', 'const_3') TRUE (L, U)
('const_2', 'const_3') CONTRADICTION (L, U)
('const_1', 'const_7') FALSE (L, U)
TRUE ForAll: ForAll_0 (y) UNKNOWN (L, U)
```
"""
def state_wrapper(grounding: _Grounding):
return (
f"'{grounding}'" if grounding.grounding_arity == 1 else f"{grounding}"
)
def round_bounds(grounding=None):
return tuple(
[
round(r, roundoff)
for r in (
self.get_facts(grounding)
if self.propositional
else self.get_facts(grounding)[0]
).tolist()
]
)
header = f"{self.world.name:<6} " f"{self.__class__.__name__}: " f"{str(self)}"
if params:
params = dict()
for name, symbol in _utils.param_symbols.items():
if hasattr(self.neuron, name):
val = getattr(self.neuron, name)
params[symbol] = f"{np.around(val.tolist(), roundoff)}" + (
f" grads {np.around(val.grad.tolist(), roundoff)}"
if grads and val.grad is not None
else ""
)
params = "params " + ", ".join([f"{k}: {v}" for k, v in params.items()])
else:
params = ""
# extract variables
var_str = (
""
if not (hasattr(self, "unique_vars") and self.unique_vars)
else (
str(tuple([str(v) for v in self.unique_vars]))
if (len(self.unique_vars) > 1)
else (str(f"({self.unique_vars[0]})"))
)
)
# print propositional node - single bounds
if self.propositional:
states = (
f'{" ".join([header, var_str]):<{header_len}} '
f"{self.state().name:>13} "
f"{round_bounds()}\n"
f"{params}"
)
print(states)
# print FOL node - table of bounds
else:
facts = (
[""]
if self.grounding_table is None
else (
[
(
f"{state_wrapper(g):{header_len}} "
f"{self.state(g).name:>13} "
f"{round_bounds(g)}\n"
)
for g in self.grounding_table
]
)
)
var_str = (
""
if not (hasattr(self, "unique_vars") and self.unique_vars)
else (
str(tuple([str(v) for v in self.unique_vars]))
if (len(self.unique_vars) > 1)
else (str(f"({self.unique_vars[0]})"))
)
)
binder = list()
for i, op in enumerate(self.operands):
if var_str and not self.propositional and self._has_bindings(i):
binder.append(f"{op} ({self.binding_str[i]})")
bind_str = ("\nBindings: " + ", ".join(binder)) if binder else ""
header = f"{header}{var_str} {bind_str}"
params = f"\n{params}" if params else ""
header = f"{header}{params}"
print(f"{header}\n" + "".join(facts))
def flush(self) -> None:
r"""set all facts in formula to 'Unknown'"""
self.neuron.flush()
def is_contradiction(self, bounds: torch.Tensor = None) -> torch.BoolTensor:
r"""check if bounds are in contradiction"""
return self.neuron.is_contradiction(bounds)
def _has_bindings(self, slot: int = None) -> bool:
r"""Returns True if Formula has any bindings"""
if isinstance(slot, int):
return (
True
if any(
[
self.bindings[slot][i][j]
for i in range(len(self.bindings[slot]))
for j in range(len(self.bindings[slot][i]))
]
)
else False
)
return any([self._has_bindings(i) for i in range(len(self.bindings))])
def __call__(
self, *variables: Union[Variable, Tuple[Variable, Union[str, List[str]]]]
) -> Tuple[
_Formula,
List[Union[Variable, None]],
Tuple[List[Union[Variable, None]], ...],
Tuple[Union[_Grounding, None], ...],
str,
]:
r"""variable remapping between operator and operand variables
**Example**
FOL formulae that appear in connectives are callable:
Note `A`, `B` in the `And`
```python
x = Variable('x')
model['A'] = Predicate()
model['B'] = Predicate()
model['AB'] = And(A(x), B(x))
```
**Returns**
_Formula: reference to the child object
variables: tuple of child's Variables
var_remap: tuple of parent-to-child remapping Variables
bindings: tuple of parent-to-child groundings to bind inference to
single groundings are _Grounding
multiple groundings are list(_Grounding)
binding_string: complete bindings given in string form
"""
if len(variables) != self.num_unique_vars:
raise Exception(
f"{self} variables length ({len(variables)}) must be the same "
f"length as `num_unique_vars` ({self.num_unique_vars})"
)
bindings = list()
_variables = list() # variables to return
binding_str = list()
for v in variables:
if isinstance(v, tuple):
if not isinstance(v[0], Variable):
raise Exception(
"expected a Variable for first tuple input, received "
f"{[type(v[0]), v[0]]}"
)
_variables.append(v[0])
binding_str.append(f"{v[0]}: {v[1]}")
if isinstance(v[1], list): # Pred(x, ['str_1', ...]))
bindings.append([self._ground(g, arity_match=False) for g in v[1]])
elif isinstance(v[1], str): # Pred(x, 'str_1')
bindings.append([self._ground(v[1], arity_match=False)])
else:
raise Exception(
"bindings expected from [str, List(str)],"
f" received {type(v[1]), v[1]}"
)
else:
bindings.append([None])
_variables.append(v)
binding_str.append(f"{v}")
binding_str = [", ".join(binding_str)]
binding_str = ", ".join(binding_str)
return (self, self.variables, tuple(_variables), tuple(bindings), binding_str)
def __str__(self) -> str:
return self.name
def rename(self, name: str) -> None:
self.name = name
def parameters(self) -> Iterator[torch.Tensor]:
for name, param in self.neuron.named_parameters():
yield param
def named_parameters(self) -> Iterator[Tuple[str, torch.Tensor]]:
yield from self.neuron.named_parameters()
def params(
self, *params: str, detach: bool = False
) -> Union[torch.Tensor, List[torch.Tensor]]:
result = list()
for param in params:
if param in dict(self.neuron.named_parameters()):
result.append(
getattr(self.neuron, param).clone().detach()
if detach
else getattr(self.neuron, param)
)
else:
raise ValueError(f"{self} has no attribute: {param}")
return result[0] if len(params) == 1 else result
def contradiction_loss(self, coeff: float = None) -> torch.Tensor:
r"""Contradiction loss"""
if coeff is None:
coeff = 1
bounds = self.get_facts()
x = bounds[..., 0] - bounds[..., 1]
return (self.is_contradiction() * coeff * x).sum()
def uncertainty_loss(self, coeff: float = None) -> torch.Tensor:
r"""Uncertainty loss"""
if coeff is None:
coeff = 0
bounds = self.get_facts()
x = bounds[..., 1] - bounds[..., 0]
return (self.is_contradiction().logical_not() * coeff * x).sum()
def supervised_loss(self, coeff: float = None) -> Union[None, torch.Tensor]:
r"""supervised loss"""
if coeff is None:
coeff = 1
loss = torch.nn.MSELoss()
if (
not hasattr(self, "labels")
or (self.propositional and not self.labels.numel())
or (not self.propositional and not self.labels)
):
return
labels = self.get_labels()
if self.propositional:
return coeff * loss(self.get_facts(), labels)
groundings = [g for g in labels if g in self.grounding_table]
if len(groundings) == 0:
return
return coeff * loss(self.get_facts(*groundings), self.get_labels(*groundings))
def reset_bounds(self) -> None:
self.neuron.reset_bounds()
def project_params(self) -> None:
self.neuron.project_params()
increment_param_history = _trace.increment_param_history
def is_unweighted(self) -> bool:
bias, weights = self.params("bias", "weights")
return all([bias == w for w in weights])
@property
def shape(self) -> torch.Size:
return self.neuron.bounds_table.shape
@staticmethod
def _formula_name(*formulae: _Formula, connective: str) -> str:
r"""Come up with a name for input formula(e)"""
return "(" + f" {connective} ".join([f.name for f in formulae]) + ")"
@staticmethod
def _formula_vars(*formulae: _Formula) -> List[Union[_Formula, Tuple]]:
r"""
Returns a list of formulae as wither called (when predicates)
or uncalled as connectives
"""
variables = list(
Variable(f"x{i}") for i in range(max([f.arity for f in formulae]))
)
return [
f(*variables[: f.arity]) if not f.propositional else f for f in formulae
]
# declaration to use hinting within the function definition
Not = TypeVar("Not")
def Not(self, **kwds) -> Not:
if "name" not in kwds:
kwds["name"] = f"¬({self.name})"
return Not(*self._formula_vars(self), **kwds)
# declaration to use hinting within the function definition
And = TypeVar("And")
def And(self, *formulae: _Formula, **kwds) -> And:
if "name" not in kwds:
kwds["name"] = self._formula_name(self, *formulae, connective="∧")
return And(*self._formula_vars(self), **kwds)
# declaration to use hinting within the function definition
Or = TypeVar("Or")
def Or(self, *formulae: _Formula, **kwds) -> Or:
if "name" not in kwds:
kwds["name"] = self._formula_name(self, *formulae, connective="∨")
return Or(*self._formula_vars(self, *formulae), **kwds)
# declaration to use hinting within the function definition
Implies = TypeVar("Implies")
def Implies(self, formula: _Formula, **kwds) -> Implies:
if "name" not in kwds:
kwds["name"] = self._formula_name(self, formula, connective="→")
return Implies(*self._formula_vars(self, formula), **kwds)
# declaration to use hinting within the function definition
Bidirectional = TypeVar("Bidirectional")
def Bidirectional(self, formula: _Formula, **kwds) -> Bidirectional:
if "name" not in kwds:
kwds["name"] = self._formula_name(self, formula, connective="↔")
return Bidirectional(*self._formula_vars(self, formula), **kwds)
class _LeafFormula(_Formula):
r"""Specifies activation functionality as nodes instead of neurons
Assumes that all leaf formulae are propositions or predicates, therefore
uses the _NodeActivation accordingly
"""
def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)
self.neuron = _NodeActivation()(
self.propositional, self.world, **kwds.get("neuron", {})
)
class Proposition(_LeafFormula):
r"""Creates propositional containers
Stores and retrieves single truth bounds instead of tables as in FOL case
**Parameters**
name : str
name of the proposition
**Example**
```python
P = Proposition('Person')
```
"""
def __init__(self, name: Optional[str] = "", **kwds):
super().__init__(name=name, arity=1, propositional=True, **kwds)
def _add_facts(self, fact: Fact) -> None:
"""Populate proposition with facts
Facts required in bool, tuple or None
None fact assumes `Unknown`
tuple fact required in bounds form `(Lower, Upper)`
"""
super()._add_facts(fact)
class Predicate(_LeafFormula):
r"""Creates a container for a predicate
Stores a table of truths, with columns specified by the arity and rows
indexed by the grounding
**Parameters**
name : str
name of the predicate
arity : int, optional
defaults to unary predicates ()
**Example**
```python
P1 = Predicate()
P2 = Predicate(arity=2)
```
"""
def __init__(self, name: Optional[str] = "", arity: int = 1, **kwds):
if arity is None:
raise Exception(f"arity expected as int > 0, received {arity}")
super().__init__(name=name, arity=arity, propositional=False, **kwds)
self._update_variables(tuple(Variable(f"x{i}") for i in range(self.arity)))
def _add_facts(self, facts: Union[dict, set]) -> None:
r"""Populate predicate with facts
Facts required in dict or set
- dict for grounding-based facts
- set for broadcasting facts across all groundings
requires a set of 1 item
dict keys for groundings and values as facts
tuple facts required in bounds form `(Lower, Upper)`
"""
super()._add_facts(facts)
class _ConnectiveFormula(_Formula):
def __init__(self, *formula: _Formula, **kwds):
super().__init__(*formula, **kwds)
class _ConnectiveNeuron(_ConnectiveFormula):
def __init__(self, *formula, **kwds):
super().__init__(*formula, **kwds)
self.neuron = _NeuralActivation(kwds.get("neuron", {}).get("type"))(
self.propositional, self.arity, self.world, **kwds.get("neuron", {})
)
self.func = self.neuron.function(
self.__class__.__name__, direction=Direction.UPWARD
)
self.func_inv = self.neuron.function(
self.__class__.__name__, direction=Direction.DOWNWARD
)
def upward(
self, groundings: Set[Union[str, Tuple[str, ...]]] = None, **kwds
) -> Union[torch.Tensor, None]:
upward_bounds = _gm.upward_bounds(self, self.operands, groundings)
if upward_bounds is None: # contradiction arresting
return
input_bounds, groundings = upward_bounds
grounding_rows = (
None
if self.propositional
else (
self.grounding_table.values()
if groundings is None
else [self.grounding_table.get(g) for g in groundings]
)
)
result = self.neuron.aggregate_bounds(grounding_rows, self.func(input_bounds))
return result
def downward(
self,