I am having problem to create Ansatz from diagrams #138
sujit4uwbslg
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
import torch
BATCH_SIZE = 30
EPOCHS = 30
LEARNING_RATE = 3e-2
SEED = 0
#Dataset
def read_data(sentences):
labels, data = [], []
for sentence in sentences:
label = int(sentence[0])
labels.append([1-label,label])
data.append(sentence[3:].strip()) # Adjust index to account for label
return labels, data
train_sentences = [
"0, Achieving my goals gives me a sense of accomplishment and pride.",
"1, The traffic this morning was terrible and stressful.",
"0, I love spending time with my friends and family.",
"1, Experiencing constant setbacks can be discouraging and demoralizing.",
"0, Surrounding myself with positive people uplifts my spirits.",
"1, Failing to meet deadlines fills me with anxiety and disappointment.",
"0, Learning new skills brings me joy and fulfillment.",
"1, Dealing with difficult people can be draining and frustrating.",
"0, I am grateful for the opportunities that come my way.",
"1, Dealing with health issues takes a toll on both my physical and mental well-being.",
"0, Traveling to new places broadens my horizons and enriches my life.",
"1, The constant noise pollution in the city makes it hard to relax and unwind.",
"0, Helping others makes me feel happy and fulfilled.",
"1, Feeling unappreciated at work leads to dissatisfaction and resentment.",
"0, Exercise boosts my energy levels and improves my mood.",
"1, Financial struggles cause me a great deal of stress and worry.",
"0, I enjoy eating healthy and nutritious meals.",
"1, The weather today is beautiful and sunny.",
"0, Achieving my goals gives me a sense of accomplishment and pride.",
"1, Losing touch with loved ones leaves me feeling lonely and isolated.",
"0, Surrounding myself with positive people uplifts my spirits.",
"1, Dealing with difficult people can be draining and frustrating.",
"0, I am grateful for the opportunities that come my way.",
]
dev_sentences=[
"1, Experiencing constant setbacks can be discouraging and demoralizing.",
"0, Learning new skills brings me joy and fulfillment.",
"1, The traffic this morning was terrible and stressful.",
"0, Traveling to new places broadens my horizons and enriches my life.",
"1, Failing to meet deadlines fills me with anxiety and disappointment.",
"0, Helping others makes me feel happy and fulfilled.",
"1, Dealing with health issues takes a toll on both my physical and mental well-being.",
"0, Exercise boosts my energy levels and improves my mood.",
"1, The constant noise pollution in the city makes it hard to relax and unwind.",
"0, I enjoy eating healthy and nutritious meals.",
]
test_sentences=[
"1, Feeling unappreciated at work leads to dissatisfaction and resentment.",
"0, I love spending time with my friends and family.",
"1, Financial struggles cause me a great deal of stress and worry.",
"0, Achieving my goals gives me a sense of accomplishment and pride.",
"1, Losing touch with loved ones leaves me feeling lonely and isolated.",
"0, Surrounding myself with positive people uplifts my spirits.",
"1, Dealing with difficult people can be draining and frustrating.",
"0, I am grateful for the opportunities that come my way.",
"1, Experiencing constant setbacks can be discouraging and demoralizing.",
"0, Learning new skills brings me joy and fulfillment.",
]
train_labels, train_data = read_data(train_sentences)
val_labels,val_data=read_data(dev_sentences)
test_labels, test_data = read_data(test_sentences)
#doing some filtering
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
def filtering(sent):
words=word_tokenize(sent)
import nltk
nltk.download('punkt')
nltk.download('stopwords')
train_data_filtered=[filtering(s) for s in train_data]
val_data_filtered=[filtering(s) for s in val_data]
test_data_filtered=[filtering(s) for s in test_data]
train_data_filtered[:5]
#creating diagrams
from lambeq import BobcatParser
parser = BobcatParser(verbose='text')
train_diagrams = parser.sentences2diagrams(train_data_filtered)
val_diagrams = parser.sentences2diagrams(val_data_filtered)
test_diagrams = parser.sentences2diagrams(test_data_filtered)
#rewriting and creation of circuits
from lambeq.backend.tensor import Dim
from lambeq import AtomicType, SpiderAnsatz,UnifyCodomainRewriter,IQPAnsatz
ansatz = SpiderAnsatz({AtomicType.NOUN: Dim(4),
AtomicType.SENTENCE: Dim(4)})
remove_cups = UnifyCodomainRewriter(output_type=n)
r1=remove_cups(train_diagrams)
r1[0].draw()
ct1=ansatz(r1[0])
#this is giving me following error
TypeError Traceback (most recent call last)
in <cell line: 11>()
9 r1=remove_cups(train_diagrams)
10 r1[0].draw()
---> 11 ct1=ansatz(r1[0])
12 #train_circuits = [ansatz(remove_cups(diagram)) for diagram in train_diagrams]
13 #val_circuits = [ansatz(remove_cups(diagram)) for diagram in val_diagrams]
5 frames
/usr/local/lib/python3.10/dist-packages/lambeq/backend/grammar.py in ar_with_cache(self, ar)
1927 dom_check = self.ob_with_cache(ar.dom)
1928 if ret.cod != cod_check or ret.dom != dom_check:
-> 1929 raise TypeError(f'The arrow is ill-defined. Applying the functor '
1930 f'to a box returns dom = {ret.dom}, cod = '
1931 f'{ret.cod} expected dom = {dom_check}, cod = '
TypeError: The arrow is ill-defined. Applying the functor to a box returns dom = Ty(), cod = n expected dom = s, cod = n
Beta Was this translation helpful? Give feedback.
All reactions