Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Practice: Neural Network Calculation
Consolidate your understanding of neural networks with a hands-on practice exercise.
10 min•By Priygop Team•Updated 2026
Practice: Build and Test a Neuron
Practice: Build and Test a Neuron
import math
# Build your own neuron and test it on different inputs
def sigmoid(z):
return 1 / (1 + math.exp(-z))
def relu(z):
return max(0.0, z)
class SimpleNeuron:
def __init__(self, weights, bias, activation="relu"):
self.weights = weights
self.bias = bias
self.activation = activation
def forward(self, inputs):
if len(inputs) != len(self.weights):
raise ValueError("Input and weight count must match")
z = sum(x * w for x, w in zip(inputs, self.weights)) + self.bias
if self.activation == "relu":
return relu(z)
elif self.activation == "sigmoid":
return sigmoid(z)
else:
return z
def predict_class(self, inputs):
output = self.forward(inputs)
return 1 if output >= 0.5 else 0
# Create a neuron
neuron = SimpleNeuron(
weights=[0.7, -0.4, 0.9],
bias=-0.2,
activation="sigmoid"
)
# Test with different inputs
test_cases = [
([1.0, 0.0, 1.0], "All positives"),
([0.0, 1.0, 0.0], "Middle input only"),
([0.5, 0.5, 0.5], "Equal inputs"),
([0.1, 0.9, 0.1], "Mostly negative-weighted"),
]
print("Neuron Output Test:")
print(f"Weights: {neuron.weights}, Bias: {neuron.bias}")
print()
for inputs, description in test_cases:
output = neuron.forward(inputs)
cls = neuron.predict_class(inputs)
print(f" {description}: inputs={inputs} -> output={output:.3f}, class={cls}")Diagram
Loading diagram…
Educational visual guide for practice neural network calculation.