This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machineForVerification.py
executable file
·124 lines (74 loc) · 3.01 KB
/
machineForVerification.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 26 13:33:39 2015
@author: Christian, Marianne, Kathrine & Cecilia
"""
# importer ting
import os
import sys
import numpy as np
# ændr sti så vi kan finde hjælpefunktioner
projectPath = os.path.dirname(os.path.abspath(sys.argv[0]))
os.chdir(projectPath)
# importer hjælpefunktioner
import fileUtils
import logTransform
import sequenceUtils
from forward import forward
import backpropagation
# set seed to be able to reproduce results
np.random.seed(1234)
def train(inputLayer, target, weight1, weight2, hiddenNodes, epochs, learningRate):
print("Starting training.")
for epoch in range(epochs):
# train on training set
error = np.zeros(1)
for index in range(1):
# run the forward function
hiddenLayer, outputLayer = forward(inputLayer, weight1, weight2)
print("Hidden layer:")
print(hiddenLayer)
print("Output layer:")
print(outputLayer)
# save the error
error[index] = 1/2 * (outputLayer - target)**2
print("Error: {:.8f}.".format(error[index]))
errorDelta = outputLayer - target
# backpropagation
outputDelta = backpropagation.backward(outputLayer, 1, errorDelta)
print("Output delta: {:.8f}.".format(outputDelta))
weight2 = backpropagation.updateWeight(hiddenLayer, weight2, outputDelta, learningRate)
print("weight 2:")
print(weight2)
hiddenDelta = backpropagation.backward(hiddenLayer, weight2, outputDelta)
# bias is not a part of calculating the weights for the input
hiddenDelta = hiddenDelta[0,0:hiddenNodes]
print("hidden delta:")
print(hiddenDelta)
weight1 = backpropagation.updateWeight(inputLayer, weight1, hiddenDelta, learningRate)
print("weight 1:")
print(weight1)
print("Training complete.")
return weight1, weight2
def predict(inputLayer, weight1, weight2):
print("Starting prediction.")
error = np.zeros(1)
for index in range(1):
# run the forward function
hiddenLayer, outputLayer = forward(inputLayer, weight1, weight2)
print("Hidden layer:")
print(hiddenLayer)
print("Output layer:")
print(outputLayer)
# save the error
error[index] = 1/2 * (outputLayer - target)**2
print("Error: {:.8f}.".format(error[index]))
print("Prediction complete.")
inputLayer = np.array([1, 1, 1])
target = 0
weight1 = np.array([[1,1,0.5],[-1,1,0.75]])
weight2 = np.array([-1,1,1])
hiddenNodes = 2
learningRate = 0.5
weight1, weight2 = train(inputLayer, target, weight1, weight2, hiddenNodes, 1, learningRate)
predict(inputLayer, weight1, weight2)