Skip to content

Commit 7303e16

Browse files
committed
added pytest for 2 runs (yes and no expose val data model 1) of uspantek data classical1
1 parent e083906 commit 7303e16

File tree

2 files changed

+146
-145
lines changed

2 files changed

+146
-145
lines changed

classify.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,18 @@ def read_glue_data(dataset_downloaded,split,lines_to_read=0):
528528

529529

530530

531-
def read_data(filename):
531+
def read_data(filename,lines_to_read):
532532
labels, sentences = [], []
533+
line_counter=0
533534
with open(filename) as f:
534535
for line in f:
535536
t = float(line[0])
536537
labels.append([t, 1-t])
537538
sentences.append(line[1:].strip())
539+
line_counter+=1
540+
if (line_counter> lines_to_read):
541+
break
542+
538543
return labels, sentences
539544

540545

@@ -918,9 +923,9 @@ def perform_task(args):
918923

919924
else:
920925
#read the base data, i.e plain text english.
921-
train_labels, train_data = read_data(os.path.join(args.data_base_folder,TRAIN))
922-
val_labels, val_data = read_data(os.path.join(args.data_base_folder,DEV))
923-
test_labels, test_data = read_data(os.path.join(args.data_base_folder,TEST))
926+
train_labels, train_data = read_data(os.path.join(args.data_base_folder,TRAIN),lines_to_read= args.no_of_training_data_points_to_use)
927+
val_labels, val_data = read_data(os.path.join(args.data_base_folder,DEV),lines_to_read= args.no_of_training_data_points_to_use)
928+
test_labels, test_data = read_data(os.path.join(args.data_base_folder,TEST),lines_to_read= args.no_of_training_data_points_to_use)
924929

925930

926931

test_oov_no_pair.py

Lines changed: 137 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -3,168 +3,164 @@
33
import pytest
44
import time
55

6-
"""
7-
some definitions
8-
Classical 1= the combination of (Spider parser, spider ansatz, pytorch model, pytorchtrainer)
9-
Classical 2= the combination of (bobCatParser , spider ansatz, pytorch model, pytorchtrainer)
10-
Quantum1= (IQPansatz+TKetmodel+Quantum Trainer+ bob cat parser)- this runs on a simulation f a quantum computer
11-
Quantum2 = **Quantum2-simulation-actual quantum computer**=(penny lane model, bob cat parser, iqp ansatz, pytorchtrainer):
12-
"""
13-
# def test_food_it_classical1(monkeypatch):
6+
# """
7+
# some definitions
8+
# Classical 1= the combination of (Spider parser, spider ansatz, pytorch model, pytorchtrainer)
9+
# Classical 2= the combination of (bobCatParser , spider ansatz, pytorch model, pytorchtrainer)
10+
# Quantum1= (IQPansatz+TKetmodel+Quantum Trainer+ bob cat parser)- this runs on a simulation f a quantum computer
11+
# Quantum2 = **Quantum2-simulation-actual quantum computer**=(penny lane model, bob cat parser, iqp ansatz, pytorchtrainer):
12+
# """
13+
# # def test_food_it_classical1(monkeypatch):
14+
# # monkeypatch.setattr(sys, 'argv',
15+
# # ['classify.py',
16+
# # '--dataset', 'food_it',
17+
# # '--parser', 'Spider'
18+
# # ])
19+
# # smart_loss, smart_acc=classify.main()
20+
# # assert round(smart_loss,2) == 0.32
21+
# # assert round(smart_acc,1) >= 0.8
22+
# # assert round(smart_acc,1) <= 0.9
23+
24+
25+
# #for a combination of train=20,val=10,test=10+exposing val data during initialization of model 1
26+
# def test_sst2_classical1_yes_expose_val(monkeypatch):
1427
# monkeypatch.setattr(sys, 'argv',
1528
# ['classify.py',
16-
# '--dataset', 'food_it',
17-
# '--parser', 'Spider'
29+
# '--dataset', 'sst2',
30+
# '--parser', 'Spider',
31+
# '--ansatz', 'SpiderAnsatz',
32+
# '--model14type', 'PytorchModel',
33+
# '--trainer', 'PytorchTrainer',
34+
# '--epochs_train_model1', '7',
35+
# '--no_of_training_data_points_to_use', '20',
36+
# '--no_of_val_data_points_to_use', '10',
37+
# '--expose_model1_val_during_model_initialization',
38+
# '--max_tokens_per_sent', '10'
39+
# ])
40+
# model4_loss, model4_acc=classify.main()
41+
# assert round(model4_loss,2) >= 0.6
42+
# assert round(model4_loss,2) <= 0.7
43+
# assert round(model4_acc,1) >= 0.5
44+
# assert round(model4_acc,1) <= 0.6
45+
46+
47+
# #for a combination of train=20,val=10,test=10 +not exposing val data during initialization of model 1
48+
# """
49+
# python classify.py --dataset sst2 --parser Spider --ansatz SpiderAnsatz --model14type PytorchModel --trainer PytorchTrainer --epochs_train_model1 7 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10
50+
# """
51+
# def test_sst2_classical1_no_expose_val(monkeypatch):
52+
# monkeypatch.setattr(sys, 'argv',
53+
# ['classify.py',
54+
# '--dataset', 'sst2',
55+
# '--parser', 'Spider',
56+
# '--ansatz', 'SpiderAnsatz',
57+
# '--model14type', 'PytorchModel',
58+
# '--trainer', 'PytorchTrainer',
59+
# '--epochs_train_model1', '7',
60+
# '--no_of_training_data_points_to_use', '20',
61+
# '--no_of_val_data_points_to_use', '10',
62+
# '--max_tokens_per_sent', '10'
63+
# ])
64+
# model4_loss, model4_acc=classify.main()
65+
# assert round(model4_loss,2) >= 0.6
66+
# assert round(model4_loss,2) <= 0.7
67+
# assert round(model4_acc,1) >= 0.5
68+
# assert round(model4_acc,1) <= 0.65
69+
70+
71+
# """#for a combination of all classical 1 components+train=20,val=10,test=10 + yes exposing val data during initialization of model 1
72+
# the actual command used to run this will be:
73+
# python classify.py --dataset sst2 --parser BobCatParser --ansatz SpiderAnsatz --model14type PytorchModel --trainer PytorchTrainer --epochs_train_model1 7 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10 --expose_model1_val_during_model_initialization
74+
# """
75+
# def test_sst2_classical2_yes_expose_val(monkeypatch):
76+
# monkeypatch.setattr(sys, 'argv',
77+
# ['classify.py',
78+
# '--dataset', 'sst2',
79+
# '--parser', 'Spider',
80+
# '--ansatz', 'SpiderAnsatz',
81+
# '--model14type', 'PytorchModel',
82+
# '--trainer', 'PytorchTrainer',
83+
# '--epochs_train_model1', '7',
84+
# '--no_of_training_data_points_to_use', '20',
85+
# '--no_of_val_data_points_to_use', '10',
86+
# '--max_tokens_per_sent', '10' ,
87+
# '--expose_model1_val_during_model_initialization'
1888
# ])
19-
# smart_loss, smart_acc=classify.main()
20-
# assert round(smart_loss,2) == 0.32
21-
# assert round(smart_acc,1) >= 0.8
22-
# assert round(smart_acc,1) <= 0.9
89+
# try:
2390

91+
# model4_loss, model4_acc=classify.main()
92+
# except Exception as ex:
93+
# print(ex)
94+
# assert type(ex) == RuntimeError
95+
2496

25-
#for a combination of train=20,val=10,test=10+exposing val data during initialization of model 1
26-
def test_sst2_classical1_yes_expose_val(monkeypatch):
27-
monkeypatch.setattr(sys, 'argv',
28-
['classify.py',
29-
'--dataset', 'sst2',
30-
'--parser', 'Spider',
31-
'--ansatz', 'SpiderAnsatz',
32-
'--model14type', 'PytorchModel',
33-
'--trainer', 'PytorchTrainer',
34-
'--epochs_train_model1', '7',
35-
'--no_of_training_data_points_to_use', '20',
36-
'--no_of_val_data_points_to_use', '10',
37-
'--expose_model1_val_during_model_initialization',
38-
'--max_tokens_per_sent', '10'
39-
])
40-
model4_loss, model4_acc=classify.main()
41-
assert round(model4_loss,2) >= 0.6
42-
assert round(model4_loss,2) <= 0.7
43-
assert round(model4_acc,1) >= 0.5
44-
assert round(model4_acc,1) <= 0.6
4597

4698

47-
#for a combination of train=20,val=10,test=10 +not exposing val data during initialization of model 1
48-
"""
49-
python classify.py --dataset sst2 --parser Spider --ansatz SpiderAnsatz --model14type PytorchModel --trainer PytorchTrainer --epochs_train_model1 7 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10
50-
"""
51-
def test_sst2_classical1_no_expose_val(monkeypatch):
52-
monkeypatch.setattr(sys, 'argv',
53-
['classify.py',
54-
'--dataset', 'sst2',
55-
'--parser', 'Spider',
56-
'--ansatz', 'SpiderAnsatz',
57-
'--model14type', 'PytorchModel',
58-
'--trainer', 'PytorchTrainer',
59-
'--epochs_train_model1', '7',
60-
'--no_of_training_data_points_to_use', '20',
61-
'--no_of_val_data_points_to_use', '10',
62-
'--max_tokens_per_sent', '10'
63-
])
64-
model4_loss, model4_acc=classify.main()
65-
assert round(model4_loss,2) >= 0.6
66-
assert round(model4_loss,2) <= 0.7
67-
assert round(model4_acc,1) >= 0.5
68-
assert round(model4_acc,1) <= 0.65
99+
# """#for a combination of all classical 1 components+train=20,val=10,test=10 + not exposing val data during initialization of model 1
100+
# the actual command used to run this will be:
101+
# python classify.py --dataset sst2 --parser BobCatParser --ansatz SpiderAnsatz --model14type PytorchModel --trainer PytorchTrainer --epochs_train_model1 7 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10
102+
# """
103+
104+
# def test_sst2_classical2_no_expose_val(monkeypatch):
105+
# monkeypatch.setattr(sys, 'argv',
106+
# ['classify.py',
107+
# '--dataset', 'sst2',
108+
# '--parser', 'Spider',
109+
# '--ansatz', 'SpiderAnsatz',
110+
# '--model14type', 'PytorchModel',
111+
# '--trainer', 'PytorchTrainer',
112+
# '--epochs_train_model1', '7',
113+
# '--no_of_training_data_points_to_use', '20',
114+
# '--no_of_val_data_points_to_use', '10',
115+
# '--max_tokens_per_sent', '10'
116+
# ])
117+
# try:
69118

119+
# model4_loss, model4_acc=classify.main()
120+
# except Exception as ex:
121+
# print(ex)
122+
# assert type(ex) == RuntimeError
70123

71-
"""#for a combination of all classical 1 components+train=20,val=10,test=10 + yes exposing val data during initialization of model 1
72-
the actual command used to run this will be:
73-
python classify.py --dataset sst2 --parser BobCatParser --ansatz SpiderAnsatz --model14type PytorchModel --trainer PytorchTrainer --epochs_train_model1 7 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10 --expose_model1_val_during_model_initialization
74-
"""
75-
def test_sst2_classical2_yes_expose_val(monkeypatch):
76-
monkeypatch.setattr(sys, 'argv',
77-
['classify.py',
78-
'--dataset', 'sst2',
79-
'--parser', 'Spider',
80-
'--ansatz', 'SpiderAnsatz',
81-
'--model14type', 'PytorchModel',
82-
'--trainer', 'PytorchTrainer',
83-
'--epochs_train_model1', '7',
84-
'--no_of_training_data_points_to_use', '20',
85-
'--no_of_val_data_points_to_use', '10',
86-
'--max_tokens_per_sent', '10' ,
87-
'--expose_model1_val_during_model_initialization'
88-
])
89-
try:
90124

91-
model4_loss, model4_acc=classify.main()
92-
except Exception as ex:
93-
print(ex)
94-
assert type(ex) == RuntimeError
95-
125+
# # python classify.py --dataset uspantek --parser Spider --ansatz SpiderAnsatz --model14type PytorchModel --trainer PytorchTrainer --epochs_train_model1 7 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10
96126

127+
# def test_uspantek_classical1_no_expose_val(monkeypatch):
128+
# monkeypatch.setattr(sys, 'argv',
129+
# ['classify.py',
130+
# '--dataset', 'uspantek',
131+
# '--parser', 'Spider',
132+
# '--ansatz', 'SpiderAnsatz',
133+
# '--model14type', 'PytorchModel',
134+
# '--trainer', 'PytorchTrainer',
135+
# '--epochs_train_model1', '7',
136+
# '--no_of_training_data_points_to_use', '20',
137+
# '--no_of_val_data_points_to_use', '10',
138+
# '--max_tokens_per_sent', '10'
139+
# ])
140+
# model4_loss, model4_acc=classify.main()
141+
# assert round(model4_loss,2) >= 0.6
142+
# assert round(model4_loss,2) <= 0.75
143+
# assert round(model4_acc,1) >= 0.4
144+
# assert round(model4_acc,1) <= 0.5
97145

98146

99-
"""#for a combination of all classical 1 components+train=20,val=10,test=10 + not exposing val data during initialization of model 1
100-
the actual command used to run this will be:
101-
python classify.py --dataset sst2 --parser BobCatParser --ansatz SpiderAnsatz --model14type PytorchModel --trainer PytorchTrainer --epochs_train_model1 7 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10
102-
"""
103147

104-
def test_sst2_classical2_no_expose_val(monkeypatch):
148+
def test_uspantek_classical1_yes_expose_val(monkeypatch):
105149
monkeypatch.setattr(sys, 'argv',
106150
['classify.py',
107-
'--dataset', 'sst2',
151+
'--dataset', 'uspantek',
108152
'--parser', 'Spider',
109153
'--ansatz', 'SpiderAnsatz',
110154
'--model14type', 'PytorchModel',
111155
'--trainer', 'PytorchTrainer',
112156
'--epochs_train_model1', '7',
113157
'--no_of_training_data_points_to_use', '20',
114158
'--no_of_val_data_points_to_use', '10',
115-
'--max_tokens_per_sent', '10'
159+
'--max_tokens_per_sent', '10' ,
160+
'--expose_model1_val_during_model_initialization'
116161
])
117-
try:
118-
119-
model4_loss, model4_acc=classify.main()
120-
except Exception as ex:
121-
print(ex)
122-
assert type(ex) == RuntimeError
123-
124-
"""# Quantum1= (IQPansatz+TKetmodel+Quantum Trainer+ bob cat parser)- this runs on a simulation f a quantum computer
125-
126-
python classify.py --dataset sst2 --parser BobCatParser --ansatz IQPAnsatz --model14type TketModel --trainer QuantumTrainer --epochs_train_model1 30 --no_of_training_data_points_to_use 20 --no_of_val_data_points_to_use 10 --max_tokens_per_sent 10
127-
note: this combination is just stuck in the first .fit(). - most likely due to less
128-
memory to handle all the circuits.. so for now calling
129-
it time out error, just to move on with ife
130-
"""
131-
132-
# @pytest.mark.timeout(30)
133-
# def test_sst2_quantum1_no_expose_val(monkeypatch):
134-
# try:
135-
# monkeypatch.setattr(sys, 'argv',
136-
# ['classify.py',
137-
# '--dataset', 'sst2',
138-
# '--parser', 'BobCatParser',
139-
# '--ansatz', 'IQPAnsatz',
140-
# '--model14type', 'TketModel',
141-
# '--trainer', 'QuantumTrainer',
142-
# '--epochs_train_model1', '30',
143-
# '--no_of_training_data_points_to_use', '20',
144-
# '--no_of_val_data_points_to_use', '10',
145-
# '--max_tokens_per_sent', '10'
146-
# ])
147-
148-
# model4_loss, model4_acc=classify.main()
149-
# except Exception as ex:
150-
# assert type(ex) == TypeError
151-
152-
153-
154-
# def test_sst2_quantum1_no_expose_val_with_timeout(monkeypatch):
155-
# try:
156-
# sst2_quantum1_no_expose_val(monkeypatch)
157-
# except Exception as ex:
158-
# assert type(ex) == TypeError
159-
160-
# def main(monkeypatch):
161-
# test_sst2_quantum1_no_expose_val_with_timeout(monkeypatch)
162-
163-
164-
# if __name__=="__main__":
165-
# main()
166-
167-
168-
169-
170-
162+
model4_loss, model4_acc=classify.main()
163+
assert round(model4_loss,2) >= 0.6
164+
assert round(model4_loss,2) <= 0.7
165+
assert round(model4_acc,1) >= 0.5
166+
assert round(model4_acc,1) <= 0.6

0 commit comments

Comments
 (0)