top of page
Search

# A Neural Network to Solve a Binary Classification Problem.

Updated: Apr 5

This network has one input layer with two neurons, one hidden layer with three neurons, and one output layer with one neuron.

```import numpy as np

# Define the sigmoid activation function
def sigmoid(x):
return 1 / (1 + np.exp(-x))

# Define the neural network class
class NeuralNetwork:
def __init__(self):
# Initialize the weights with random values
self.weights1 = np.random.rand(2, 3)  # weights connecting input layer to hidden layer
self.weights2 = np.random.rand(3, 1)  # weights connecting hidden layer to output layer

def forward(self, X):
# Propagate inputs through the network
self.hidden = sigmoid(np.dot(X, self.weights1))
self.output = sigmoid(np.dot(self.hidden, self.weights2))
return self.output

def train(self, X, y, epochs):
for _ in range(epochs):
# Forward propagation
output = self.forward(X)
# Backpropagation
error = y - output
output_delta = error * (output * (1 - output))
hidden_delta = output_delta.dot(self.weights2.T) * (self.hidden * (1 - self.hidden))
# Update weights
self.weights2 += self.hidden.T.dot(output_delta)
self.weights1 += X.T.dot(hidden_delta)

# Create a dataset for training
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

# Create an instance of the neural network
nn = NeuralNetwork()

# Train the neural network
nn.train(X, y, epochs=10000)

# Test the neural network
test_input = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
predictions = nn.forward(test_input)

# Print the predictions
for i in range(len(test_input)):
print(f"Input: {test_input[i]} Predicted Output: {predictions[i]}")```

In this example, the neural network is trained to learn the XOR function. It takes binary inputs [0, 0], [0, 1], [1, 0], and [1, 1] and predicts the corresponding outputs [0], [1], [1], and [0]. The network uses the sigmoid activation function for the hidden and output layers and performs forward propagation and backpropagation to update the weights during training.

The output for this problem was:

Input: [0 0] Predicted Output: [0.03265749]

Input: [0 1] Predicted Output: [0.98000362]

Input: [1 0] Predicted Output: [0.98000388]

Input: [1 1] Predicted Output: [0.00711134]

as expected.

I ran several other simple neural net problems in VS Code.