Neural_Networks/lab_1.py

22 lines
691 B
Python
Raw Normal View History

2025-09-12 18:34:33 +03:00
import numpy as np
from tensorflow.keras import Sequential, Input, layers, optimizers
x = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0], [1, 1, 1]])
y = np.array([0, 1, 1, 1, 0, 0, 0, 1])
model = Sequential([
Input(shape=(3,)),
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=100)
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]))