Skip to content

Instantly share code, notes, and snippets.

@heolin
Last active June 26, 2019 14:57
Show Gist options
  • Save heolin/58930bca179506e86c2438b635b3732a to your computer and use it in GitHub Desktop.
Save heolin/58930bca179506e86c2438b635b3732a to your computer and use it in GitHub Desktop.
import tensorflow as tf
import numpy as np
import random
# neural network predicting if input hour is 21:37
# network created with layers of size: [73 21]
# because of small number of neurons if may take few tries to read good results (around 0.98 accuracy)
X, y = [], []
for _ in range(10000):
h, m = random.randint(0, 23), random.randint(0, 59)
if h == 21 and m == 37:
y.append([1, 0])
else:
y.append([0, 1])
X.append([h, m])
for _ in range(10000):
X.append([21, 37])
y.append([1, 0])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
model = tf.keras.Sequential([
tf.keras.layers.Dense(7, activation='relu', input_shape=(2,)),
tf.keras.layers.Dense(3, activation='relu'),
tf.keras.layers.Dense(1, activation='relu'),
tf.keras.layers.Dense(2, activation='softmax')
])
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
n = model.fit(np.array(X_train), np.array(y_train),
batch_size=100, epochs=10,
validation_data=(np.array(X_test), np.array(y_test)))
y_pred = model.predict(np.array([
[10, 20], # 0.07371756
[21, 20], # 0.07371756
[20, 37], # 0.89896834
[21, 38], # 0.96462727
[21, 39], # 0.92450625
[21, 37], # 0.9837998
]))
for _y in y_pred:
print(_y[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment