-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathtest_keras.py
80 lines (67 loc) · 2.31 KB
/
test_keras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
import pandas as pd
import pytest
from distributed import Nanny
from distributed.utils_test import gen_cluster
from packaging import version
from scipy.stats import loguniform
from sklearn.datasets import make_classification
from dask_ml.model_selection import IncrementalSearchCV
try:
import scikeras
import tensorflow as tf
from scikeras.wrappers import KerasClassifier
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
pytestmark = [
pytest.mark.skipif(
version.parse(tf.__version__) < version.parse("2.3.0"),
reason="pickle support",
),
pytest.mark.skipif(
version.parse(scikeras.__version__) < version.parse("0.1.8"),
reason="partial_fit support",
),
]
except ImportError:
pytestmark = pytest.mark.skip(reason="Missing tensorflow or scikeras")
def _keras_build_fn(lr=0.01):
layers = [
Dense(512, input_shape=(784,), activation="relu"),
Dense(10, input_shape=(512,), activation="softmax"),
]
model = Sequential(layers)
opt = tf.keras.optimizers.SGD(learning_rate=lr)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
return model
@gen_cluster(client=True, Worker=Nanny, timeout=20)
def test_keras(c, s, a, b):
# Mirror the mnist dataset
X, y = make_classification(
n_classes=10, n_features=784, n_informative=100, random_state=0
)
X = X.astype("float32")
assert y.dtype == np.dtype("int64")
model = KerasClassifier(
model=_keras_build_fn, lr=0.01, verbose=False, loss="categorical_crossentropy",
)
params = {"lr": loguniform(1e-3, 1e-1), "random_state": [1]}
search = IncrementalSearchCV(
model,
params,
max_iter=3,
n_initial_parameters=5,
decay_rate=None,
random_state=0,
)
yield search.fit(X, y)
# search.fit(X, y)
assert search.best_score_ >= 0
# Make sure the model trains, and scores aren't constant
scores = {
ident: [h["score"] for h in hist]
for ident, hist in search.model_history_.items()
}
assert all(len(hist) == 3 for hist in scores.values())
nuniq_scores = [pd.Series(v).nunique() for v in scores.values()]
assert max(nuniq_scores) > 1