20
20
import autokeras as ak
21
21
from autokeras import test_utils
22
22
23
+ NUM_INSTANCES = 3
24
+ BATCH_SIZE = 2
25
+
23
26
24
27
def test_image_classifier (tmp_path ):
25
- train_x = test_utils .generate_data (num_instances = 320 , shape = (32 , 32 ))
28
+ train_x = test_utils .generate_data (
29
+ num_instances = NUM_INSTANCES , shape = (32 , 32 )
30
+ )
26
31
train_y = test_utils .generate_one_hot_labels (
27
- num_instances = 320 , num_classes = 10
32
+ num_instances = NUM_INSTANCES , num_classes = 10
28
33
)
29
34
clf = ak .ImageClassifier (
30
35
directory = tmp_path ,
31
36
max_trials = 2 ,
32
37
seed = test_utils .SEED ,
33
38
distribution_strategy = tf .distribute .MirroredStrategy (),
34
39
)
35
- clf .fit (train_x , train_y , epochs = 1 , validation_split = 0.2 )
40
+ clf .fit (
41
+ train_x , train_y , epochs = 1 , validation_split = 0.2 , batch_size = BATCH_SIZE
42
+ )
36
43
keras_model = clf .export_model ()
37
44
clf .evaluate (train_x , train_y )
38
45
assert clf .predict (train_x ).shape == (len (train_x ), 10 )
39
46
assert isinstance (keras_model , keras .Model )
40
47
41
48
42
49
def test_image_regressor (tmp_path ):
43
- train_x = test_utils .generate_data (num_instances = 320 , shape = (32 , 32 , 3 ))
44
- train_y = test_utils .generate_data (num_instances = 320 , shape = (1 ,))
50
+ train_x = test_utils .generate_data (
51
+ num_instances = NUM_INSTANCES , shape = (32 , 32 , 3 )
52
+ )
53
+ train_y = test_utils .generate_data (num_instances = NUM_INSTANCES , shape = (1 ,))
45
54
clf = ak .ImageRegressor (
46
55
directory = tmp_path , max_trials = 2 , seed = test_utils .SEED
47
56
)
48
- clf .fit (train_x , train_y , epochs = 1 , validation_split = 0.2 )
57
+ clf .fit (
58
+ train_x , train_y , epochs = 1 , validation_split = 0.2 , batch_size = BATCH_SIZE
59
+ )
49
60
clf .export_model ()
50
61
assert clf .predict (train_x ).shape == (len (train_x ), 1 )
51
62
52
63
53
64
def test_text_classifier (tmp_path ):
54
- train_x = test_utils .generate_text_data (num_instances = 320 )
55
- train_y = np .random . randint ( 0 , 2 , 320 )
65
+ train_x = test_utils .generate_text_data (num_instances = NUM_INSTANCES )
66
+ train_y = np .array ([ 0 , 1 ] * (( NUM_INSTANCES + 1 ) // 2 ))[: NUM_INSTANCES ]
56
67
test_x = train_x
57
68
test_y = train_y
58
69
clf = ak .TextClassifier (
@@ -67,29 +78,35 @@ def test_text_classifier(tmp_path):
67
78
train_y ,
68
79
epochs = 2 ,
69
80
validation_data = (test_x , test_y ),
70
- batch_size = 6 ,
81
+ batch_size = BATCH_SIZE ,
71
82
)
72
83
clf .export_model ()
73
84
assert clf .predict (test_x ).shape == (len (test_x ), 1 )
74
85
assert clf .tuner ._get_best_trial_epochs () <= 2
75
86
76
87
77
88
def test_text_regressor (tmp_path ):
78
- train_x = test_utils .generate_text_data (num_instances = 300 )
89
+ train_x = test_utils .generate_text_data (num_instances = NUM_INSTANCES )
79
90
test_x = train_x
80
- train_y = test_utils .generate_data (num_instances = 300 , shape = (1 ,))
91
+ train_y = test_utils .generate_data (num_instances = NUM_INSTANCES , shape = (1 ,))
81
92
test_y = train_y
82
93
clf = ak .TextRegressor (
83
94
directory = tmp_path , max_trials = 2 , seed = test_utils .SEED
84
95
)
85
- clf .fit (train_x , train_y , epochs = 1 , validation_data = (test_x , test_y ))
96
+ clf .fit (
97
+ train_x ,
98
+ train_y ,
99
+ epochs = 1 ,
100
+ validation_data = (test_x , test_y ),
101
+ batch_size = BATCH_SIZE ,
102
+ )
86
103
clf .export_model ()
87
104
assert clf .predict (test_x ).shape == (len (test_x ), 1 )
88
105
89
106
90
107
def test_structured_data_regressor (tmp_path ):
91
- num_data = 500
92
- num_train = 400
108
+ num_data = NUM_INSTANCES * 2
109
+ num_train = NUM_INSTANCES
93
110
data = (
94
111
pd .read_csv (test_utils .TRAIN_CSV_PATH ).to_numpy ().astype (str )[:num_data ]
95
112
)
@@ -99,14 +116,20 @@ def test_structured_data_regressor(tmp_path):
99
116
clf = ak .StructuredDataRegressor (
100
117
directory = tmp_path , max_trials = 2 , seed = test_utils .SEED
101
118
)
102
- clf .fit (x_train , y_train , epochs = 11 , validation_data = (x_train , y_train ))
119
+ clf .fit (
120
+ x_train ,
121
+ y_train ,
122
+ epochs = 11 ,
123
+ validation_data = (x_train , y_train ),
124
+ batch_size = BATCH_SIZE ,
125
+ )
103
126
clf .export_model ()
104
127
assert clf .predict (x_test ).shape == (len (y_test ), 1 )
105
128
106
129
107
130
def test_structured_data_classifier (tmp_path ):
108
- num_data = 500
109
- num_train = 400
131
+ num_data = NUM_INSTANCES * 2
132
+ num_train = NUM_INSTANCES
110
133
data = (
111
134
pd .read_csv (test_utils .TRAIN_CSV_PATH ).to_numpy ().astype (str )[:num_data ]
112
135
)
@@ -118,7 +141,13 @@ def test_structured_data_classifier(tmp_path):
118
141
clf = ak .StructuredDataClassifier (
119
142
directory = tmp_path , max_trials = 1 , seed = test_utils .SEED
120
143
)
121
- clf .fit (x_train , y_train , epochs = 2 , validation_data = (x_train , y_train ))
144
+ clf .fit (
145
+ x_train ,
146
+ y_train ,
147
+ epochs = 2 ,
148
+ validation_data = (x_train , y_train ),
149
+ batch_size = BATCH_SIZE ,
150
+ )
122
151
clf .export_model ()
123
152
assert clf .predict (x_test ).shape == (len (y_test ), 3 )
124
153
0 commit comments