Skip to content

Instantly share code, notes, and snippets.

@danieldaeschle
Created July 27, 2018 18:13
Show Gist options
  • Save danieldaeschle/f9e7c9518e0ad0381b8fa92c1c4dff8c to your computer and use it in GitHub Desktop.
Save danieldaeschle/f9e7c9518e0ad0381b8fa92c1c4dff8c to your computer and use it in GitHub Desktop.
This is my first deep learning network which detects if the number is smaller or bigger than 5 :)
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense
from random import sample
import numpy as np
x_train = np.array(sample(5 * [[x] for x in range(11)], 5 * 11))
y_train = np.array([int(x > 4) for x in x_train])
y_binary = to_categorical(y_train)
x_test = np.array([[7], [2], [8], [6], [2], [9], [8], [4], [1]])
y_test = np.array([1, 0, 1, 1, 0, 1, 1, 0, 0])
y_test_binary = to_categorical(y_test)
model = Sequential()
model.add(Dense(units=2, activation="relu", input_dim=1))
model.add(Dense(units=2, activation="softmax"))
model.compile(optimizer="sgd", loss="binary_crossentropy")
model.fit(x_train, y_binary, epochs=500, batch_size=1)
loss = model.evaluate(x_test, y_test_binary, batch_size=1)
print("Loss:", loss)
while True:
x_predict = input("Give me a number: ")
x_predict = np.array([[x_predict]])
result = model.predict(x_predict)
print("The number is smaller than 5" if result[0][0] > result[0][1] else "The number is equal to 5 or bigger")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment