0% found this document useful (0 votes)
22 views3 pages

EXE 1 DL

The document outlines the steps to solve the XOR problem using a Deep Neural Network (DNN), including understanding the XOR logic, setting up the environment, defining the dataset, designing the neural network, compiling the model, training, evaluating, making predictions, and saving the model. It provides example Python code using TensorFlow/Keras for each step. The XOR problem requires a multi-layer neural network due to its non-linear nature.

Uploaded by

sarangopi2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

EXE 1 DL

The document outlines the steps to solve the XOR problem using a Deep Neural Network (DNN), including understanding the XOR logic, setting up the environment, defining the dataset, designing the neural network, compiling the model, training, evaluating, making predictions, and saving the model. It provides example Python code using TensorFlow/Keras for each step. The XOR problem requires a multi-layer neural network due to its non-linear nature.

Uploaded by

sarangopi2019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DEEP LEARNING EXERCISE 1 EXPLANATION

Solving the XOR problem using a Deep Neural Network (DNN) typically involves
the following steps. These steps can be translated into a lab program depending on the
specific programming framework you are using (e.g., TensorFlow, PyTorch, etc.).

1. Understanding the XOR Problem

 XOR (exclusive OR) is a logical operation that outputs true or 1 only when the
inputs are different.

The truth table for XOR is:

Input 1 | Input 2 | Output


--------|---------|-------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0

XOR is a non-linear problem, meaning it cannot be solved using a single layer of


neurons. This is why it requires a multi-layer neural network.

2. Setting Up the Environment

 Import necessary libraries like TensorFlow, Keras, or PyTorch.


 Ensure that you have a suitable environment with Python installed.

3. Define the Dataset

Define the XOR inputs and corresponding outputs as your dataset:

python
import numpy as np
# XOR inputs
X = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
# XOR outputs
y = np.array([[0],
[1],
[1],
[0]])

4. Design the Neural Network

 Create a neural network with at least one hidden layer. A typical architecture
for solving XOR would be:
o Input layer with 2 neurons (corresponding to the two inputs of XOR)
o 1 hidden layer with a few neurons (typically 2-4 neurons)
o Output layer with 1 neuron (for the XOR output)

 Define the activation functions. Typically, sigmoid or tanh is used for the
hidden layers, and sigmoid for the output layer.

python
Copy code
from tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import
Dense

model = Sequential()
model.add(Dense(4, input_dim=2, activation='tanh')) # Hidden layer
model.add(Dense(1, activation='sigmoid')) # Output layer

5. Compile the Model

 Define the loss function and optimizer. For binary classification problems like
XOR, use binary cross-entropy loss.
 An optimizer like stochastic gradient descent (SGD) or Adam can be used.

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

6. Train the Model

 Fit the model to the XOR dataset.


 Define the number of epochs (iterations over the entire dataset) and the batch
size (number of samples per gradient update).

python
Copy code
model.fit(X, y, epochs=1000, verbose=1)

7. Evaluate the Model

 After training, evaluate the model's performance on the training data or new
data.
 Check the accuracy to see if the model has learned the XOR function.

python
Copy code
_, accuracy = model.evaluate(X, y)print(f'Accuracy: {accuracy * 100:.2f}%')

8. Make Predictions

 Use the trained model to make predictions on the XOR inputs.


python
Copy code
predictions = model.predict(X)print(f'Predictions:\n{predictions}')

9. Visualize or Interpret Results

 Optionally, visualize the decision boundary or interpret the weights to


understand how the network is solving the XOR problem.

10. Conclude and Save the Model

 Draw conclusions about the model's performance.


 Save the trained model for future use if needed.

python
Copy code
model.save('xor_model.h5')

Example Lab Program (Python with TensorFlow/Keras)

python
Copy code
import numpy as npfrom tensorflow.keras.models import Sequentialfrom
tensorflow.keras.layers import Dense
# XOR inputs and outputs
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='tanh')) # Hidden layer with 4 neurons
model.add(Dense(1, activation='sigmoid')) # Output layer
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(X, y, epochs=1000, verbose=1)
# Evaluate the model
_, accuracy = model.evaluate(X, y)print(f'Accuracy: {accuracy * 100:.2f}%')
# Make predictions
predictions = model.predict(X)print(f'Predictions:\n{predictions}')
# Save the model
model.save('xor_model.h5')

This code can be used in a lab environment to solve the XOR problem using a Deep
Neural Network.

You might also like