SOLVING XOR PROBLEM USING DNN
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# XOR problem data
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# Define the model
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu')) # Hidden layer with 4
neurons and ReLU activation
model.add(Dense(1, activation='sigmoid')) # Output layer with 1 neuron and
sigmoid activation
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
# Train the model
model.fit(X, y, epochs=500, verbose=0)
# Evaluate the model (optional)
loss, accuracy = model.evaluate(X, y)
print(f"Loss: {loss:.4f}, Accuracy: {accuracy:.4f}")
# Make predictions
predictions = model.predict(X)
print("\nPredictions:")
print(np.round(predictions)) # Round predictions to get binary output