# -*- coding: utf-8 -*-
"""Solving XOR problem using deep feed forward network

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1Cef6TY4UreI4bjAEovxQ6zb6GHHvCzAr
"""

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

X = np.array([
    [0,0],
    [0,1],
    [1,0],
    [1,1]
])

y = np.array([
    [0],
    [1],
    [1],
    [0]
])

model = Sequential()

model.add(Dense(2, input_dim=2, activation='relu'))

model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])

model.fit(X, y, epochs=500, verbose=0)

predictions = model.predict(X)
print("Predictions:")
for i in range(len(X)):
    print(X[i], "->", predictions[i])

"""OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOORRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

"""

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.initializers import GlorotUniform

np.random.seed(42)
tf.random.set_seed(42)

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

model = Sequential([
    Dense(8, activation='tanh', input_shape=(2,), kernel_initializer=GlorotUniform()),
    Dense(4, activation='tanh', kernel_initializer=GlorotUniform()),
    Dense(1, activation='sigmoid', kernel_initializer=GlorotUniform())
])

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(X, y, epochs=2000, verbose=0)

loss, accuracy = model.evaluate(X, y, verbose=0)
print(f"Model Evaluation - Loss: {loss:.4f}, Accuracy: {accuracy:.4f}")

predictions = model.predict(X)
binary_predictions = (predictions > 0.5).astype(int)

print("\nXOR Problem Predictions:")
print(f"{'Input':<10} | {'Expected':<8} | {'Predicted'}")
print("-" * 35)
for i in range(len(X)):
    print(f"{str(X[i]):<10} | {y[i][0]:<8} | {binary_predictions[i][0]}")


