Skip to content

Commit

Permalink
Fix the tests (#1894)
Browse files Browse the repository at this point in the history
* make tests faster

* skip tests that always fail on tf-nightly

* fix tests

* fix tests

* remove nightly tests

* remove nightly
  • Loading branch information
haifeng-jin authored Oct 2, 2023
1 parent 04525a5 commit ec4ebd1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/nightly.yml

This file was deleted.

65 changes: 47 additions & 18 deletions autokeras/integration_tests/task_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,50 @@
import autokeras as ak
from autokeras import test_utils

NUM_INSTANCES = 3
BATCH_SIZE = 2


def test_image_classifier(tmp_path):
train_x = test_utils.generate_data(num_instances=320, shape=(32, 32))
train_x = test_utils.generate_data(
num_instances=NUM_INSTANCES, shape=(32, 32)
)
train_y = test_utils.generate_one_hot_labels(
num_instances=320, num_classes=10
num_instances=NUM_INSTANCES, num_classes=10
)
clf = ak.ImageClassifier(
directory=tmp_path,
max_trials=2,
seed=test_utils.SEED,
distribution_strategy=tf.distribute.MirroredStrategy(),
)
clf.fit(train_x, train_y, epochs=1, validation_split=0.2)
clf.fit(
train_x, train_y, epochs=1, validation_split=0.2, batch_size=BATCH_SIZE
)
keras_model = clf.export_model()
clf.evaluate(train_x, train_y)
assert clf.predict(train_x).shape == (len(train_x), 10)
assert isinstance(keras_model, keras.Model)


def test_image_regressor(tmp_path):
train_x = test_utils.generate_data(num_instances=320, shape=(32, 32, 3))
train_y = test_utils.generate_data(num_instances=320, shape=(1,))
train_x = test_utils.generate_data(
num_instances=NUM_INSTANCES, shape=(32, 32, 3)
)
train_y = test_utils.generate_data(num_instances=NUM_INSTANCES, shape=(1,))
clf = ak.ImageRegressor(
directory=tmp_path, max_trials=2, seed=test_utils.SEED
)
clf.fit(train_x, train_y, epochs=1, validation_split=0.2)
clf.fit(
train_x, train_y, epochs=1, validation_split=0.2, batch_size=BATCH_SIZE
)
clf.export_model()
assert clf.predict(train_x).shape == (len(train_x), 1)


def test_text_classifier(tmp_path):
train_x = test_utils.generate_text_data(num_instances=320)
train_y = np.random.randint(0, 2, 320)
train_x = test_utils.generate_text_data(num_instances=NUM_INSTANCES)
train_y = np.array([0, 1] * ((NUM_INSTANCES + 1) // 2))[:NUM_INSTANCES]
test_x = train_x
test_y = train_y
clf = ak.TextClassifier(
Expand All @@ -67,29 +78,35 @@ def test_text_classifier(tmp_path):
train_y,
epochs=2,
validation_data=(test_x, test_y),
batch_size=6,
batch_size=BATCH_SIZE,
)
clf.export_model()
assert clf.predict(test_x).shape == (len(test_x), 1)
assert clf.tuner._get_best_trial_epochs() <= 2


def test_text_regressor(tmp_path):
train_x = test_utils.generate_text_data(num_instances=300)
train_x = test_utils.generate_text_data(num_instances=NUM_INSTANCES)
test_x = train_x
train_y = test_utils.generate_data(num_instances=300, shape=(1,))
train_y = test_utils.generate_data(num_instances=NUM_INSTANCES, shape=(1,))
test_y = train_y
clf = ak.TextRegressor(
directory=tmp_path, max_trials=2, seed=test_utils.SEED
)
clf.fit(train_x, train_y, epochs=1, validation_data=(test_x, test_y))
clf.fit(
train_x,
train_y,
epochs=1,
validation_data=(test_x, test_y),
batch_size=BATCH_SIZE,
)
clf.export_model()
assert clf.predict(test_x).shape == (len(test_x), 1)


def test_structured_data_regressor(tmp_path):
num_data = 500
num_train = 400
num_data = NUM_INSTANCES * 2
num_train = NUM_INSTANCES
data = (
pd.read_csv(test_utils.TRAIN_CSV_PATH).to_numpy().astype(str)[:num_data]
)
Expand All @@ -99,14 +116,20 @@ def test_structured_data_regressor(tmp_path):
clf = ak.StructuredDataRegressor(
directory=tmp_path, max_trials=2, seed=test_utils.SEED
)
clf.fit(x_train, y_train, epochs=11, validation_data=(x_train, y_train))
clf.fit(
x_train,
y_train,
epochs=11,
validation_data=(x_train, y_train),
batch_size=BATCH_SIZE,
)
clf.export_model()
assert clf.predict(x_test).shape == (len(y_test), 1)


def test_structured_data_classifier(tmp_path):
num_data = 500
num_train = 400
num_data = NUM_INSTANCES * 2
num_train = NUM_INSTANCES
data = (
pd.read_csv(test_utils.TRAIN_CSV_PATH).to_numpy().astype(str)[:num_data]
)
Expand All @@ -118,7 +141,13 @@ def test_structured_data_classifier(tmp_path):
clf = ak.StructuredDataClassifier(
directory=tmp_path, max_trials=1, seed=test_utils.SEED
)
clf.fit(x_train, y_train, epochs=2, validation_data=(x_train, y_train))
clf.fit(
x_train,
y_train,
epochs=2,
validation_data=(x_train, y_train),
batch_size=BATCH_SIZE,
)
clf.export_model()
assert clf.predict(x_test).shape == (len(y_test), 3)

Expand Down
2 changes: 1 addition & 1 deletion autokeras/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def generate_data(num_instances=100, shape=(32, 32, 3), dtype="np"):
def generate_one_hot_labels(num_instances=100, num_classes=10, dtype="np"):
np.random.seed(SEED)
labels = np.random.randint(num_classes, size=num_instances)
data = keras.utils.to_categorical(labels)
data = keras.utils.to_categorical(labels, num_classes=num_classes)
if dtype == "np":
return data
if dtype == "dataset":
Expand Down

0 comments on commit ec4ebd1

Please sign in to comment.