From beginner concepts to advanced deep learning
Learn Python, TensorFlow, PyTorch, and how to build real-world AI applications with our comprehensive, interactive tutorials and guided learning paths.
Choose your learning journey based on your experience level and goals
Start your AI journey with fundamental concepts and easy-to-follow tutorials.
Deepen your understanding with more complex algorithms and frameworks.
Master cutting-edge AI techniques and research-level implementations.
Understand neural networks by exploring this implementation of a simple XOR classifier
import numpy as np
# Simple Neural Network with one hidden layer
class SimpleNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
# Initialize weights randomly
self.W1 = np.random.randn(input_size, hidden_size) * 0.01
self.b1 = np.zeros((1, hidden_size))
self.W2 = np.random.randn(hidden_size, output_size) * 0.01
self.b2 = np.zeros((1, output_size))
def forward(self, X):
# Forward propagation
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = np.tanh(self.z1) # Hidden layer activation
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.a2 = 1 / (1 + np.exp(-self.z2)) # Output layer activation (sigmoid)
return self.a2
def train(self, X, y, learning_rate=0.01, epochs=1000):
for epoch in range(epochs):
# Forward pass
output = self.forward(X)
# Backpropagation
dZ2 = output - y
dW2 = np.dot(self.a1.T, dZ2)
db2 = np.sum(dZ2, axis=0, keepdims=True)
dZ1 = np.dot(dZ2, self.W2.T) * (1 - np.power(self.a1, 2))
dW1 = np.dot(X.T, dZ1)
db1 = np.sum(dZ1, axis=0, keepdims=True)
# Update weights
self.W2 -= learning_rate * dW2
self.b2 -= learning_rate * db2
self.W1 -= learning_rate * dW1
self.b1 -= learning_rate * db1
# Print loss every 100 epochs
if epoch % 100 == 0:
loss = -np.mean(y * np.log(output) + (1 - y) * np.log(1 - output))
print(f"Epoch {epoch}, Loss: {loss}")
# Example usage
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # XOR inputs
y = np.array([[0], [1], [1], [0]]) # XOR outputs
# Create and train network
nn = SimpleNeuralNetwork(input_size=2, hidden_size=4, output_size=1)
nn.train(X, y, learning_rate=0.1, epochs=10000)
# Test the network
predictions = nn.forward(X)
print("Predictions:")
print(predictions)
Full Tutorial
Advertisement