Skip to content

Instantly share code, notes, and snippets.

@danzeeeman
Last active December 5, 2022 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danzeeeman/3acdd8e3a977ee60eb2fa81f7e406a80 to your computer and use it in GitHub Desktop.
Save danzeeeman/3acdd8e3a977ee60eb2fa81f7e406a80 to your computer and use it in GitHub Desktop.
write a gradient descent neural network in numpy
# Import NumPy
import numpy as np
# Define the architecture of the network
n_inputs = 2
n_hidden = 3
n_outputs = 1
# Initialize the weights and biases
weights = {
'hidden': np.random.randn(n_inputs, n_hidden),
'output': np.random.randn(n_hidden, n_outputs),
}
biases = {
'hidden': np.random.randn(n_hidden),
'output': np.random.randn(n_outputs),
}
# Define the activation function
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Define the derivative of the activation function
def sigmoid_prime(x):
return sigmoid(x) * (1 - sigmoid(x))
# Set the learning rate
learning_rate = 0.5
# Implement the forward propagation step
def forward_propagate(inputs):
hidden_inputs = np.dot(inputs, weights['hidden']) + biases['hidden']
hidden_outputs = sigmoid(hidden_inputs)
output_inputs = np.dot(hidden_outputs, weights['output']) + biases['output']
outputs = sigmoid(output_inputs)
return outputs
# Implement the backpropagation step
def backpropagate(inputs, targets, outputs):
# Calculate the error in the output
output_errors = targets - outputs
# Calculate the error in the hidden layer
hidden_errors = np.dot(output_errors, weights['output'].T) * sigmoid_prime(hidden_outputs)
# Update the weights and biases
weights['output'] += learning_rate * np.dot(hidden_outputs.T, output_errors)
biases['output'] += learning_rate * output_errors
weights['hidden'] += learning_rate * np.dot(inputs.T, hidden_errors)
biases['hidden'] += learning_rate * hidden_errors
# Train the network using the gradient descent algorithm
for epoch in range(10000):
# Generate some random input data
inputs = np.random.randn(n_inputs)
targets = np
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment