40 lines
835 B
Python
40 lines
835 B
Python
|
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")
|