initial commit
This commit is contained in:
commit
c256f2913f
39
train.py
Normal file
39
train.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import tensorflow as tf
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
x = np.array([
|
||||||
|
[0, 0, 0],
|
||||||
|
[0, 0, 1],
|
||||||
|
[0, 1, 0],
|
||||||
|
[0, 1, 1],
|
||||||
|
[1, 0, 0],
|
||||||
|
[1, 0, 1],
|
||||||
|
[1, 1, 0],
|
||||||
|
[1, 1, 1],
|
||||||
|
])
|
||||||
|
|
||||||
|
y = np.array([sum(i) % 2 for i in x])
|
||||||
|
|
||||||
|
model = tf.keras.Sequential([
|
||||||
|
tf.keras.layers.Input(shape = (3,)),
|
||||||
|
tf.keras.layers.Dense(3, activation = "tanh"),
|
||||||
|
tf.keras.layers.Dense(1, activation = "sigmoid")
|
||||||
|
])
|
||||||
|
|
||||||
|
model.compile(
|
||||||
|
optimizer = tf.keras.optimizers.Adam(learning_rate = 0.05),
|
||||||
|
loss = "binary_crossentropy",
|
||||||
|
metrics = ["accuracy"]
|
||||||
|
)
|
||||||
|
|
||||||
|
for _ in range(100):
|
||||||
|
model.fit(x, y, epochs = 10, verbose = 0)
|
||||||
|
loss, accuracy = model.evaluate(x, y, verbose = 0)
|
||||||
|
|
||||||
|
print(f"\rAchieved accuracy={accuracy}, loss={loss}", end = '')
|
||||||
|
|
||||||
|
if accuracy == 1.0:
|
||||||
|
print()
|
||||||
|
break
|
||||||
|
|
||||||
|
model.save_weights("mod1_final.weights.h5")
|
34
verify.py
Normal file
34
verify.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import tensorflow as tf
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
x = np.array([
|
||||||
|
[0, 0, 0],
|
||||||
|
[0, 0, 1],
|
||||||
|
[0, 1, 0],
|
||||||
|
[0, 1, 1],
|
||||||
|
[1, 0, 0],
|
||||||
|
[1, 0, 1],
|
||||||
|
[1, 1, 0],
|
||||||
|
[1, 1, 1],
|
||||||
|
])
|
||||||
|
|
||||||
|
y = np.array([sum(i) % 2 for i in x])
|
||||||
|
|
||||||
|
model = tf.keras.Sequential([
|
||||||
|
tf.keras.layers.Input(shape = (3,)),
|
||||||
|
tf.keras.layers.Dense(3, activation = "tanh"),
|
||||||
|
tf.keras.layers.Dense(1, activation = "sigmoid")
|
||||||
|
])
|
||||||
|
|
||||||
|
model.compile(
|
||||||
|
optimizer = tf.keras.optimizers.Adam(learning_rate = 0.05),
|
||||||
|
loss = "binary_crossentropy",
|
||||||
|
metrics = ["accuracy"]
|
||||||
|
)
|
||||||
|
|
||||||
|
model.load_weights("mod1_final.weights.h5")
|
||||||
|
|
||||||
|
prediction = model.predict(x)
|
||||||
|
|
||||||
|
for i, o, t in zip(x, prediction, y):
|
||||||
|
print(f"i = {i}, o = {round(o[0])}, t = {t} : {'Good' if t == round(o[0]) else 'Bad'}")
|
Loading…
x
Reference in New Issue
Block a user