Skip to content

Instantly share code, notes, and snippets.

@neilslater
Last active August 25, 2019 08:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilslater/6053e3729045e575f40f4d884f4d65dc to your computer and use it in GitHub Desktop.
Save neilslater/6053e3729045e575f40f4d884f4d65dc to your computer and use it in GitHub Desktop.
100% accuracy on example from AI Stack Exchange question
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.utils import to_categorical
def get_data():
X = np.array([[1,2], [1,12], [1,17], [9,33], [48,49], [48,50]])
y = [ 0, 0, 0, 0, 1, 2 ]
X = (X/25) - 1
# This copies indetical data to CV and test data sets, because question is only about fitting training data
# Don't do this for any real data, otherwise you won't know how well your NN is doing!
return (X, y, X, y, X, y)
def build_model(layer_sizes, hidden_activation='relu', learning_rate=0.005, momentum=0.9):
model = Sequential()
model.add( Dense(layer_sizes[1], input_dim=layer_sizes[0]) )
for size in layer_sizes[2:]:
model.add( Activation(hidden_activation) )
model.add( Dense(size) )
model.add(Activation('softmax'))
sgd = SGD(lr=learning_rate, momentum=momentum, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
def train_model(model, X_train, y_train, X_cv, y_cv, epochs=20, batch_size=6):
y_train = to_categorical(y_train)
y_cv = to_categorical(y_cv)
history = model.fit(X_train, y_train, validation_data=(X_cv, y_cv), epochs=epochs, batch_size=batch_size, verbose=0)
return history.history
def test_model(model, X_test, y_test):
y_pred = model.predict_classes(X_test, verbose=0)
accuracy = np.mean( (y_pred == y_test).astype('float') )
print("Final accuracy {}".format(accuracy * 100))
def plot_history(history):
plt.plot(history['acc'])
plt.plot(history['val_acc'])
plt.title('Accuracy')
plt.ylabel('Accuracy vs Epoch')
plt.xlabel('Epoch')
plt.legend(['train', 'val'], loc='lower right')
plt.show()
def main():
np.random.seed(1234)
X_train, y_train, X_cv, y_cv, X_test, y_test = get_data()
model = build_model( layer_sizes=[X_train.shape[1], 20, 10, 3],
hidden_activation='tanh',
learning_rate=0.01,
momentum=0.9 )
history = train_model(model, X_train, y_train, X_cv, y_cv, epochs=500)
test_model(model, X_test, y_test)
plot_history(history)
if __name__ == '__main__':
main()
@neilslater
Copy link
Author

This is just a hacky script, adapted from other code I had lying around. It is in response to this question: https://ai.stackexchange.com/questions/14099/tensorflow-estimator-dnnclassifier-fails-to-fit-simple-data

@datdinhquoc
Copy link

tks a lot neil :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment