Tensor¶
from lambeq import BobcatParser
parser = BobcatParser()
diagram = parser.sentence2diagram('Alice gives Claire the flowers')
diagram.draw()
from lambeq import AtomicType, MPSAnsatz, SpiderAnsatz, TensorAnsatz
from lambeq.backend.tensor import Dim
N = AtomicType.NOUN
S = AtomicType.SENTENCE
TensorAnsatz({N: Dim(4), S: Dim(3)})(diagram).draw(figsize=(8, 2))
MPSAnsatz({N: Dim(4), S: Dim(3)}, bond_dim=5)(diagram).draw(figsize=(10, 2))
SpiderAnsatz({N: Dim(4), S: Dim(3)})(diagram).draw(figsize=(10, 2))
import torch as th
from sympy import default_sort_key
d = SpiderAnsatz({N: Dim(4), S: Dim(3)}, max_order=2)(diagram)
syms = sorted(d.free_symbols, key=default_sort_key)
sym_dict = {k: th.ones(k.size) for k in syms}
subbed_diagram = d.lambdify(*syms)(*sym_dict.values())
import tensornetwork as tn
subbed_diagram.eval(contractor=tn.contractors.auto)
array([256., 256., 256.], dtype=float32)