43 lines
922 B
Python
43 lines
922 B
Python
import numpy as np
|
|
from tensorflow.keras import Sequential, Input, layers, optimizers
|
|
|
|
x = np.array([
|
|
[0, 0, 0, 0],
|
|
[0, 0, 0, 1],
|
|
[0, 0, 1, 0],
|
|
[0, 0, 1, 1],
|
|
|
|
[0, 1, 0, 0],
|
|
[0, 1, 0, 1],
|
|
[0, 1, 1, 0],
|
|
[0, 1, 1, 1],
|
|
|
|
[1, 0, 0, 0],
|
|
[1, 0, 0, 1],
|
|
[1, 0, 1, 0],
|
|
[1, 0, 1, 1],
|
|
|
|
[1, 1, 0, 0],
|
|
[1, 1, 0, 1],
|
|
[1, 1, 1, 0],
|
|
[1, 1, 1, 1],
|
|
])
|
|
y = np.array([0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0])
|
|
|
|
model = Sequential([
|
|
Input(shape=(4,)),
|
|
layers.Dense(3, activation="tanh"),
|
|
layers.Dense(1, activation="sigmoid"),
|
|
])
|
|
|
|
model.compile(optimizer=optimizers.Adam(learning_rate=0.05), loss="binary_crossentropy", metrics=["accuracy"])
|
|
model.fit(x, y, epochs=200)
|
|
|
|
loss, accuracy = model.evaluate(x, y)
|
|
print(f"Loss: {loss}")
|
|
print(f"Accuracy: {accuracy}")
|
|
|
|
prediction = model.predict(x)
|
|
for inp, pred in zip(x, prediction):
|
|
print(inp, round(pred[0]))
|