-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22ug2_0013lab_sheet_2.py
More file actions
102 lines (66 loc) · 1.83 KB
/
22ug2_0013lab_sheet_2.py
File metadata and controls
102 lines (66 loc) · 1.83 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""22UG2-0013Lab Sheet 2.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1L8wrp1mrowfWkhYCTQiIdO2NdkC9PPD7
Lab Sheet 2 – Introduction to Neural Networks
22UG2-0013
Single Neuron Calculation
Step 1: Import Libraries
"""
import math # For the exponential function in sigmoid
"""Step 2: Define Sigmoid Function"""
def sigmoid(z):
return 1 / (1 + math.exp(-z))
"""Step 3: Define Inputs, Weights, and Bias"""
# Inputs
x1 = 1
x2 = 2
# Weights
w1 = 0.5
w2 = 0.3
# Bias
b = 0.1
"""Step 4: Calculate Neuron Output"""
# Step 1: Weighted sum
z = x1 * w1 + x2 * w2 + b
# Step 2: Apply sigmoid activation
output = sigmoid(z)
print("Neuron output:", output)
"""Step 5: Make It a Function"""
def simple_neuron(x1, x2, w1, w2, b):
z = x1 * w1 + x2 * w2 + b
return sigmoid(z)
# Test
result = simple_neuron(1, 2, 0.5, 0.3, 0.1)
print("Neuron output:", result)
"""Simple Neural Network
Step 1: Import Libraries
"""
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import numpy as np
"""Step 2: Define the Model"""
# Create a Sequential model
model = Sequential()
# Input layer + Hidden layer
model.add(Dense(units=3, input_dim=2, activation='sigmoid'))
# Output layer
model.add(Dense(units=1, activation='sigmoid'))
"""Step 3: View Model Summary"""
model.summary()
"""Step 4: Test Forward Pass"""
# Example input data (2 features)
X_test = np.array([[1, 2],
[2, 3],
[3, 5]])
# Predict (forward pass)
output = model.predict(X_test)
print("Network output:\n", output)
"""Step 5: Make It a Function"""
def simple_neuron(x1, x2, w1, w2, b):
z = x1 * w1 + x2 * w2 + b
return sigmoid(z)
# Test
result = simple_neuron(1, 2, 0.5, 0.3, 0.1)
print("Neuron output:", result)