-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass_fail.py
68 lines (54 loc) · 2.05 KB
/
pass_fail.py
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
import random
# Sigmoid activation function
def sigmoid(x):
return 1 / (1 + pow(2.718, -x))
# Forward propagation function
def forward_propagation(input_data, weights):
hidden_layer_activation = 0
for i in range(len(input_data)):
hidden_layer_activation += input_data[i] * weights[0][i]
hidden_layer_output = sigmoid(hidden_layer_activation)
output_layer_activation = hidden_layer_output * weights[1][0]
output = sigmoid(output_layer_activation)
return output
# Initialize the weights
def initialize_weights(input_dim):
weights = []
weights.append([random.uniform(-1, 1) for i in range(input_dim)])
weights.append([random.uniform(-1, 1)])
return weights
# Train the neural network
def train(X, y, epochs, lr):
weights = initialize_weights(1)
for i in range(epochs):
for j in range(len(X)):
input_data = X[j]
target_output = y[j]
# Forward propagation
output = forward_propagation(input_data, weights)
# Backward propagation
error = target_output - output
output_error = error * output * (1 - output)
hidden_error = output_error * weights[1][0] * sigmoid(input_data[0]) * (1 - sigmoid(input_data[0]))
# Update weights
weights[1][0] += lr * output_error * sigmoid(hidden_error)
for k in range(len(input_data)):
weights[0][k] += lr * hidden_error * input_data[k]
return weights
# Predict pass/fail
def predict(X, weights):
predictions = []
for i in range(len(X)):
input_data = X[i]
output = forward_propagation(input_data, weights)
predictions.append(int(round(output)))
return predictions
# Input data
X = [[50], [60], [70], [80], [90], [95], [100]]
y = [0, 0, 1, 1, 1, 1, 1]
# Train the neural network
weights = train(X, y, epochs=1000, lr=0.1)
# Predict pass/fail
new_X = [[55], [65], [75], [85], [95], [100], [40]]
predictions = predict(new_X, weights)
print(predictions)