Skip to content

Instantly share code, notes, and snippets.

@data-hound
Last active January 18, 2021 20:18
Show Gist options
  • Save data-hound/ad4717217485cf09c8f4e8a404b078e9 to your computer and use it in GitHub Desktop.
Save data-hound/ad4717217485cf09c8f4e8a404b078e9 to your computer and use it in GitHub Desktop.
Scikeras Tutorial -1
import numpy as np
from sklearn.datasets import make_classification
from tensorflow import keras
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
# Make a dummy dataset
X, y = make_classification(1000, 20, n_informative=10, random_state=0)
X = X.astype(np.float32)
y = y.astype(np.int64)
print("Features Shape: ",X.shape)
print("Targets Shape: ",y.shape)
# get_model returns a compiled model
def get_model(n_features_in_, X_shape, n_classes_, hidden_layer_dim=100):
# Define a sequential model
model = keras.models.Sequential()
model.add(keras.layers.Dense(n_features_in_, input_shape=X_shape[1:]))
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.Dense(hidden_layer_dim))
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.Dense(n_classes_))
model.add(keras.layers.Activation("softmax"))
# Compile the model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
return model
clf = KerasClassifier(
build_fn=get_model,
n_features_in_=1000,
X_shape=X.shape,
n_classes_=2,
hidden_layer_dim=100,
)
clf.fit(X, y)
print(clf.score(X[-100:,:],y[-100:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment