0% found this document useful (0 votes)
16 views12 pages

Class Neuron Red

The document outlines the implementation of a simple feedforward neural network in Python, consisting of classes for Neuron, Layer, and NeuralNetwork. Each Neuron has attributes for weights, bias, and methods for forward and backward passes, while the Layer class manages multiple neurons. The NeuralNetwork class allows for adding layers, training the network with input data, and saving/loading the model to/from a JSON file.
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)
16 views12 pages

Class Neuron Red

The document outlines the implementation of a simple feedforward neural network in Python, consisting of classes for Neuron, Layer, and NeuralNetwork. Each Neuron has attributes for weights, bias, and methods for forward and backward passes, while the Layer class manages multiple neurons. The NeuralNetwork class allows for adding layers, training the network with input data, and saving/loading the model to/from a JSON file.
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/ 12

class Neuron:

"""

A single neuron in the neural network.

Attributes:

weights (ndarray): Weights associated with the inputs.

bias (float): Bias term added to the neuron's weighted sum.

output (float): Output of the neuron after activation.

input (ndarray): Inputs to the neuron during the forward pass.

dweights (ndarray): Gradient of the loss with respect to the weights.

dbias (float): Gradient of the loss with respect to the bias.

"""

def __init__(self, input_size):

"""

Initializes a Neuron with random weights and bias.

Parameters:

input_size (int): The number of inputs to the neuron.

"""

self.weights = np.random.randn(input_size)

self.bias = np.random.randn()

self.output = 0

self.input = None

self.dweights = np.zeros_like(self.weights)

self.dbias = 0

def activate(self, x):

"""

Applies the sigmoid activation function.


Parameters:

x (float): The input value.

Returns:

float: Activated output value.

"""

return 1 / (1 + np.exp(-x))

def activate_derivative(self, x):

"""

Computes the derivative of the sigmoid function.

Parameters:

x (float): The activated output value.

Returns:

float: Derivative of the sigmoid.

"""

return x * (1 - x)

def forward(self, inputs):

"""

Computes the forward pass for the neuron.

Parameters:

inputs (ndarray): The input values to the neuron.

Returns:

float: The activated output of the neuron.

"""

self.input = inputs
weighted_sum = np.dot(inputs, self.weights) + self.bias

self.output = self.activate(weighted_sum)

return self.output

def backward(self, d_output, learning_rate):

"""

Computes the backward pass and updates weights and bias.

Parameters:

d_output (float): The gradient of the loss with respect to the output.

learning_rate (float): The learning rate for weight updates.

Returns:

ndarray: The gradient of the loss with respect to the input.

"""

d_activation = d_output * self.activate_derivative(self.output)

self.dweights = np.dot(self.input, d_activation)

self.dbias = d_activation

d_input = np.dot(d_activation, self.weights)

# Update weights and biases

self.weights -= learning_rate * self.dweights

self.bias -= learning_rate * self.dbias

return d_input

# Example usage

if __name__ == "__main__":

# Create a neuron with 3 inputs

neuron = Neuron(3)

# Example inputs
inputs = np.array([1.5, 2.0, -1.0])

# Compute the neuron's output

output = neuron.forward(inputs)

print("Neuron output:", output)


import numpy as np

from neuron.neuron import Neuron

class Layer:

"""

A layer of neurons in the neural network.

Attributes:

neurons (list): List of Neuron objects in the layer.

"""

def __init__(self, num_neurons, input_size):

"""

Initializes a layer with a specified number of neurons.

Parameters:

num_neurons (int): Number of neurons in the layer.

input_size (int): Number of inputs to each neuron in the layer.

"""

self.neurons = [Neuron(input_size) for _ in range(num_neurons)]

def forward(self, inputs):

"""
Computes the forward pass for the layer.

Parameters:

inputs (ndarray): The input values to the layer.

Returns:

ndarray: The activated outputs of the neurons in the layer.

"""

outputs = np.array([neuron.forward(inputs) for neuron in self.neurons])

return outputs

def backward(self, d_outputs, learning_rate):

"""

Computes the backward pass and updates the neurons in the layer.

Parameters:

d_outputs (ndarray): The gradients of the loss with respect to the outputs of the layer.

learning_rate (float): The learning rate for weight updates.

Returns:

ndarray: The gradients of the loss with respect to the inputs to the layer.

"""

d_inputs = np.zeros(len(self.neurons[0].input)) # Initialize gradient wrt inputs

for i, neuron in enumerate(self.neurons):

d_inputs += neuron.backward(d_outputs[i], learning_rate)

return d_inputs

# Example usage

if __name__ == "__main__":

# Create a layer with 3 neurons, each receiving 4 inputs


layer = Layer(3, 4)

# Example inputs (4 features)

inputs = np.array([1.0, 0.5, -1.5, 2.0])

# Compute the output of the layer

layer_output = layer.forward(inputs)

print("Layer output:", layer_output)


import json

import numpy as np

from layer.layer import Layer

class NeuralNetwork:

"""

A simple feedforward neural network.

Attributes:

layers (list): List of Layer objects in the neural network.

"""

def __init__(self):

"""

Initializes an empty neural network with no layers.

"""

self.layers = []

self.loss_list = []

def add_layer(self, num_neurons, input_size):

"""
Adds a layer to the neural network.

Parameters:

num_neurons (int): The number of neurons in the new layer.

input_size (int): The number of inputs to the new layer (or neurons).

"""

if not self.layers:

self.layers.append(Layer(num_neurons, input_size))

else:

previous_output_size = len(self.layers[-1].neurons)

self.layers.append(Layer(num_neurons, previous_output_size))

def forward(self, inputs):

"""

Computes the forward pass through the entire network.

Parameters:

inputs (ndarray): The input values to the network.

Returns:

ndarray: The output of the network after the forward pass.

"""

for layer in self.layers:

inputs = layer.forward(inputs)

return inputs

def backward(self, loss_gradient, learning_rate):

"""

Computes the backward pass through the entire network.

Parameters:
loss_gradient (ndarray): The gradient of the loss with respect to the output of the
network.

learning_rate (float): The learning rate for weight updates.

"""

for layer in reversed(self.layers):

loss_gradient = layer.backward(loss_gradient, learning_rate)

def train(self, X, y, epochs=1000, learning_rate=0.01):

"""

Trains the neural network using the given training data.

Parameters:

X (ndarray): The input training data (features).

y (ndarray): The target output values (labels).

epochs (int): The number of training iterations.

learning_rate (float): The step size for weight updates.

"""

for epoch in range(epochs):

loss = 0

for i in range(len(X)):

# Forward pass

output = self.forward(X[i])

# Calculate loss (mean squared error)

loss += np.mean((y[i] - output) ** 2)

# Backward pass (gradient of loss wrt output)

loss_gradient = 2 * (output - y[i])

self.backward(loss_gradient, learning_rate)

loss /= len(X)

self.loss_list.append(loss)

if epoch % 100 == 0:

print(f"Epoch {epoch}, Loss: {loss}")


def predict(self, X):

"""

Generates predictions for the input data.

Parameters:

X (ndarray): The input data for prediction.

Returns:

ndarray: The predicted outputs from the network.

"""

predictions = []

for i in range(len(X)):

predictions.append(self.forward(X[i]))

return np.array(predictions)

def save(self, filename="model.json"):

"""

Saves the neural network's layers, neurons, weights, and biases to a JSON file.

Parameters:

filename (str): The name of the file to save the model parameters.

"""

# Initialize the model data dictionary

model_data = {

"layers": []

# Iterate through each layer and store its configuration and neurons

for layer in self.layers:

layer_data = {
"num_neurons": len(layer.neurons),

"input_size": len(layer.neurons[0].weights) if layer.neurons else 0,

"neurons": layer.to_dict()

model_data["layers"].append(layer_data)

# Save the model data to a file

with open(filename, "w") as f:

json.dump(model_data, f)

print(f"Model saved to {filename}")

def load(self, filename="model.json"):

"""

Loads the neural network's layers, neurons, weights, and biases from a JSON file.

Parameters:

filename (str): The name of the file from which to load the model parameters.

"""

with open(filename, "r") as f:

model_data = json.load(f)

# Clear any existing layers before loading

self.layers = []

# Recreate the architecture based on the saved configuration

for layer_data in model_data["layers"]:

num_neurons = layer_data["num_neurons"]

input_size = layer_data["input_size"]

# Create a new layer with the specified number of neurons and input size

new_layer = Layer(num_neurons, input_size)


# Load neuron parameters from the saved data

new_layer.from_dict(layer_data["neurons"])

# Append the recreated layer to the network

self.layers.append(new_layer)

print(f"Model loaded from {filename}")

if __name__ == "__main__":

# Example of usage:

# Create a neural network with 3 input features, 1 hidden layers, and 1 output layer

nn = NeuralNetwork()

# Add layers: input size for first hidden layer is 3 (e.g., 3 input features)

nn.add_layer(num_neurons=3, input_size=3) # First hidden layer with 5 neurons

nn.add_layer(num_neurons=3, input_size=3) # Second hidden layer with 4 neurons

nn.add_layer(num_neurons=1, input_size=4) # Output layer with 1 neuron

# Dummy training data (X: input, y: target)

X = np.array([[0.5, 0.2, 0.1],

[0.9, 0.7, 0.3],

[0.4, 0.5, 0.8]])

y = np.array([[0.3, 0.6, 0.9]]).T

# Train the network

nn.train(X, y, epochs=3000, learning_rate=0.5)

# Save the model after training

nn.save("../models/model.json")
# Load the model (no need to train)

nn.load("../models/model.json")

# Predict using the trained network

predictions = nn.predict(X)

print(f"Predictions: {predictions}")

You might also like