Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
1 contributor

Users who have contributed to this file

from random import *
inputList = []
assignedWeights = []
tempList = []
outputList = []
tempVar = 0
calcList = []
hiddenValues = []
newWeight = []
hiddenWeights = []
tempList2 = []
allInputWeights = []
allHiddenWeights = []
def Parameters(noInputs,noHidden,noOutput,noEpoch,hiddenLayers,learningRate,target):
#This is the function that defines the characteristics of the MLP
Input(noInputs)
Initialise(noInputs, noHidden)
Introduced(noHidden, noOutput)
print("These are the starting weights: ", assignedWeights)
for j in range(noEpoch):
tempList2.clear()
inputList.clear()
tempList.clear()
outputList.clear()
calcList.clear()
hiddenValues.clear()
newWeight.clear()
for i in range(len(assignedWeights)):
allInputWeights.append([j,assignedWeights[i][1]])
for i in range(len(hiddenWeights)):
allHiddenWeights.append([j,hiddenWeights[i]])
Structure(noInputs,noHidden)
Output(hiddenLayers, noHidden)
WeightUpdate(learningRate,target,noHidden)
Print()
#This function asks the user what values they would like to input initially into the MLP
def Input(a):
for i in range(a):
inputVar = int(input("What is the value of this input?: "))
inputList.append(inputVar)
return inputList
#This function is used to combine the outputs from the initial inputs into the appropriate hidden neurons
def Structure(noInputs,noHidden):
for j in range(noInputs * noHidden):
tempList.append(assignedWeights[j][0] * assignedWeights[j][1])
#print(tempList)
for i in range(0,len(tempList)-1,2):
tempVar = tempList[i] + tempList[i + 1]
calcList.append(tempVar)
#Create the actual neural network structure(Inputs, hidden units, outputs)
#This function is used to create random weight values and are assigned to their respective inputs(only for first loop)
def Initialise(inp, out):
for j in range(inp):
for i in range(out):
assignedWeights.append([inputList[j], round(random(),1)])
return(assignedWeights)
#This function creates random weights for the hidden neurons travelling towards the output
def Introduced(noHidden, noOutput):
for i in range(noHidden * noOutput):
hiddenWeights.append(round(random(),1))
#Repeatedly introduce one training sample at a time to the input layer. The full training set should be introduced in a random order
#for the number of times represented by the number of epochs
#Could also just re enter input values each time it loops
#This function sums up the values the output has recieved and prints it to the terminal
def Output(hiddenLayers, noHidden):
for i in range(hiddenLayers):
for i in range(noHidden):
hiddenValues.append(calcList[i] * hiddenWeights[i])
tempVar = 0
for i in range(len(hiddenValues)):
tempVar = tempVar + hiddenValues[i]
outputList.append(tempVar)
print("answer is",round(tempVar,2))
#Calculate the outputs from the hidden units and the overall outputs from the network for each input training sample using the equation 1
#round(random(),1) *
#Creates slight changes to the existing weight to make end result closer to target value
def WeightUpdate(learningRate, target,noHidden):
#Uses the difference between the predicted output and the target output after each input training sample to update the weights in the neural network using equation 2
for i in range(len(outputList)):
tempVar = outputList[0]
for i in range(len(assignedWeights)):
newWeight.append(assignedWeights[i][1] + (learningRate*(target - tempVar)*assignedWeights[i][0]))
for i in range(len(newWeight)):
assignedWeights[i][1] = round(newWeight[i],2)
for i in range(noHidden):
tempList2.append(hiddenWeights[i] + (learningRate*(target - tempVar)*calcList[i]))
hiddenWeights.clear()
for i in range(len(tempList2)):
hiddenWeights.append(round(tempList2[i],2))
#print("the new input weights are", assignedWeights)
#print("the new hidden weights are", hiddenWeights)
#prints every weight that has existed within every epoch loop and which run it is relevant to
def Print():
print("-----------------------------------------------------------------------------------------------------------")
print(allInputWeights)
print("")
print(allHiddenWeights)
#Once training is complete prints the weights associated with the neurons to the screen in an appropriate manner.
def main():
Parameters(2,2,1,20,1,0.1,1)
#Parameters(noInputs,noHidden,noOutput,noEpoch,hiddenLayers,learningRate,target)
#Original values: 2,2,1,20,1,0.1,1
#Values for input 1,1 and target 0: 2,2,1,20,1,0.1,0
#Values for input 1,0 and target 1: 2,2,1,20,1,0.1,1
#Values for input 0,1 and target 1: 2,2,1,20,1,0.1,1
#Values for input 0,0 and target 0: 2,2,1,20,1,0.1,0
main()