-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuron.py
More file actions
57 lines (45 loc) · 1.48 KB
/
Neuron.py
File metadata and controls
57 lines (45 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
class Neuron:
def __init__(self, weights, bias):
"""
Initializes the Neuron with weights and bias.
Args:
weights (list or np.ndarray): The weights for the neuron.
bias (float): The bias for the neuron.
"""
self.weights = np.array(weights)
self.bias = bias
def activation(self, z):
"""
Applies the activation function (ReLU).
Args:
z (float): The input to the activation function.
Returns:
float: The activated output.
"""
return max(0, z)
def forward(self, inputs):
"""
Computes the output of the neuron given the inputs.
Args:
inputs (list or np.ndarray): The inputs to the neuron.
Returns:
float: The output of the neuron.
Raises:
ValueError: If the number of inputs does not match the number of weights.
"""
if len(inputs) != len(self.weights):
raise ValueError("The number of inputs must match the number of weights.")
z = np.dot(self.weights, inputs) + self.bias
return self.activation(z)
if __name__ == "__main__":
print("Script is running...")
weights = [0.2, 0.8]
bias = 0.1
neuron = Neuron(weights, bias)
inputs = [0.5, 1.5]
try:
output = neuron.forward(inputs)
print(f"Neuron output: {output}")
except ValueError as e:
print(f"Error: {e}")