Here I show how to design a signle neuron using numpy library
In [2]:
import numpy as np
import random # generate random numbers
import math
In [ ]:
Preliminary¶
In [3]:
# matrix multiplication
A = np.array([
[2, 4, 1 ],
[7, 2, 5 ]
])
B = np.array([
[4, 5, 2],
[6, 8, 5],
[2, 2, 1]
])
result = A @ B # or A @ B, np.matmul(A, B)
print(result)
[[34 44 25] [50 61 29]]
In [ ]:
In [5]:
# Vectorized operation
vec1 = np.array([3, 2, 7, 9, 0])
vec2 = np.array([4, 1, 6, 8, 4])
result = vec1 * vec2 # vectorized operation - Element-wise Multiplication
print(result)
result_dot = vec1 @ vec2 # Dot Product
print(result_dot)
[12 2 42 72 0] 128
In [ ]:
A simple NN¶
In [12]:
# 1) z = W*X + b
x = np.array([1, 2, 3])
w = np.array([0.2, 0.8, -0.5])
b = 2
z = w @ x + b
print(z)
2.3
In [ ]:
# 2) Activation function
output = np.maximum(0, z) # ReLU
# output = 1 / (1 + np.exp(-z)) # or use sigmoid
print(output)
2.3
In [ ]:
Multiple Neuron Layer:¶
- Hidden layer with 3 neuron
- output layer with 1 neuron
Input (x)
↓
Hidden Layer (Z → A) 3 neuron
↓
Output Neuron (Z2 → output) 1 neuron
In [3]:
# 1)
# Input (3 features)
x = np.array([1, 2, 3])
# 3 neurons → each has 3 weights
W = np.array([
[0.20, 0.80, -0.50],
[0.50, -0.91, 0.26],
[-0.26, -0.27, 0.17]
])
# Bias for each neuron
b = np.array([2, 3, 0.5])
# Layer output
Z = W @ x + b
print("Z (Linear Output):\n", Z)
Z (Linear Output): [2.3 2.46 0.21]
In [4]:
# 2) ReLU activation
A = np.maximum(0, Z)
print("A (After ReLU):\n", A)
A (After ReLU): [2.3 2.46 0.21]
In [5]:
# 2) sigmoid activation
A = 1 / (1 + np.exp(-Z))
print("A (After ReLU):\n", A)
A (After ReLU): [0.90887704 0.92128966 0.55230791]
In [ ]:
In [ ]:
In [ ]:
In [7]:
# 2) Now pass this through the last output layer with 1 neuron
# Weights for output neuron (3 inputs → 1 neuron)
W2 = np.array([0.3, -0.2, 0.5])
# Bias for output neuron
b2 = 1.0
Z2 = W2 @ A + b2
print("Z (Linear Output):\n", Z2)
output = 1 / (1 + np.exp(-Z2))
print("Final Output:\n", output)
Z (Linear Output): 1.303 Final Output: 0.21366055482715413
In [ ]:
In [ ]:
In [ ]:
# OPTONAL
Batch Input¶
In [6]:
# 2 samples
X = np.array([
[1, 2, 3],
[2, 5, -1]
])
Z = X @ (W.T) + b
print(Z)
[[ 2.3 2.46 0.21] [ 6.9 -0.81 -1.54]]
In [7]:
# 2) ReLU activation
A = np.maximum(0, Z)
print("A (After ReLU):\n", A)
A (After ReLU): [[2.3 2.46 0.21] [6.9 0. 0. ]]
In [ ]:
In [ ]:
(OPTIONAL) Single Neuron Ouput With Adjustible Wts and Bias¶
In [9]:
# 1) Single Neuron Ouput With Adjustible Wts and Bias
# Activation function (sigmoid for simplicity)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Simulate a single neuron
def neuron_output(inputs, weights, bias):
# z = np.dot(inputs, weights) + bias
z = inputs @ weights + bias
output = sigmoid(z)
return output
# Input features (e.g., [hours studied, hours slept])
inputs = np.array([2, 3])
weights = np.array([0.1, 0.2])
bias = [-2]
output = neuron_output(inputs, weights, bias)
print("output:", output)
output: [0.23147522]
In [7]:
# 2) Single Neuron Ouput With Adjustible Wts and Bias
# Activation function (sigmoid for simplicity)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Simulate a single neuron
def neuron_output(inputs, weights, bias):
# z = np.dot(inputs, weights) + bias
z = inputs @ weights + bias
output = sigmoid(z)
return output
# Input features (e.g., [hours studied, hours slept])
inputs = np.array([2, 3])
# Try different weights and bias
weights_list = [
np.array([0.1, 0.2]),
np.array([1.0, -1.0]),
np.array([-0.5, 2.0])
]
bias_list = [0, 1, -2]
# Display outputs
print("Inputs:", inputs)
for weights in weights_list:
for bias in bias_list:
output = neuron_output(inputs, weights, bias)
print(f"Weights: {weights}, Bias: {bias}, Output: {output:.4f}")
Inputs: [2 3] Weights: [0.1 0.2], Bias: 0, Output: 0.6900 Weights: [0.1 0.2], Bias: 1, Output: 0.8581 Weights: [0.1 0.2], Bias: -2, Output: 0.2315 Weights: [ 1. -1.], Bias: 0, Output: 0.2689 Weights: [ 1. -1.], Bias: 1, Output: 0.5000 Weights: [ 1. -1.], Bias: -2, Output: 0.0474 Weights: [-0.5 2. ], Bias: 0, Output: 0.9933 Weights: [-0.5 2. ], Bias: 1, Output: 0.9975 Weights: [-0.5 2. ], Bias: -2, Output: 0.9526
In [ ]:
In [ ]:
In [ ]:
In [18]:
# Solution
import numpy as np
# Input (2 samples, 3 features)
X = np.array([
[1, 2, 3],
[4, 5, 6]
])
# Step 1: Initialize random weights (4 neurons, 3 inputs each)
W = np.random.randn(4, 3)
# Step 2: Initialize random bias (1 per neuron)
b = np.random.randn(4)
# Step 3: Compute output
Z = X @ (W.T) + b
print("Weights:\n", W)
print("\nBias:\n", b)
print("\nOutput:\n", Z)
Weights: [[ 1.49845688 -1.36751215 0.31821724] [-0.97340705 -0.77166496 0.01077436] [-0.96718551 -0.51717606 0.93293336] [-0.37610632 1.05705184 0.12159993]] Bias: [-0.34751369 -0.28830612 0.26055088 -1.69342705] Output: [[-0.62942937 -2.77272 1.05781335 0.40937008] [ 0.71805656 -7.97561292 -0.59647126 2.8170064 ]]
In [ ]:
