Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import exp
- from random import seed
- from random import random
- # Initialize a network
- # Put fixed weigths to 0.5 on code.finki.ukim.mk if there is a problem with random()
- def initialize_network(n_inputs, n_hidden, n_outputs):
- network = list()
- hidden_layer = [{'weights':[random() for i in range(n_inputs + 1)]} for i in range(n_hidden)]
- network.append(hidden_layer)
- output_layer = [{'weights':[random() for i in range(n_hidden + 1)]} for i in range(n_outputs)]
- network.append(output_layer)
- return network
- # Calculate neuron activation for an input
- def activate(weights, inputs):
- activation = weights[-1]
- for i in range(len(weights)-1):
- activation += weights[i] * inputs[i]
- return activation
- # Transfer neuron activation
- def transfer(activation):
- return 1.0 / (1.0 + exp(-activation))
- # Forward propagate input to a network output
- def forward_propagate(network, row):
- inputs = row
- for layer in network:
- new_inputs = []
- for neuron in layer:
- activation = activate(neuron['weights'], inputs)
- neuron['output'] = transfer(activation)
- new_inputs.append(neuron['output'])
- inputs = new_inputs
- return inputs
- # Calculate the derivative of an neuron output
- def transfer_derivative(output):
- return output * (1.0 - output)
- # Backpropagate error and store in neurons
- def backward_propagate_error(network, expected):
- for i in reversed(range(len(network))):
- layer = network[i]
- errors = list()
- if i != len(network)-1:
- for j in range(len(layer)):
- error = 0.0
- for neuron in network[i + 1]:
- error += (neuron['weights'][j] * neuron['delta'])
- errors.append(error)
- else:
- for j in range(len(layer)):
- neuron = layer[j]
- errors.append(expected[j] - neuron['output'])
- for j in range(len(layer)):
- neuron = layer[j]
- neuron['delta'] = errors[j] * transfer_derivative(neuron['output'])
- # Update network weights with error
- def update_weights(network, row, l_rate):
- for i in range(len(network)):
- inputs = row[:-1]
- if i != 0:
- inputs = [neuron['output'] for neuron in network[i - 1]]
- for neuron in network[i]:
- for j in range(len(inputs)):
- neuron['weights'][j] += l_rate * neuron['delta'] * inputs[j]
- neuron['weights'][-1] += l_rate * neuron['delta']
- # Train a network for a fixed number of epochs
- def train_network(network, train, l_rate, n_epoch, n_outputs):
- for epoch in range(n_epoch):
- sum_error = 0
- for row in train:
- outputs = forward_propagate(network, row)
- expected = [0 for i in range(n_outputs)]
- expected[row[-1]] = 1
- sum_error += sum([(expected[i]-outputs[i])**2 for i in range(len(expected))])
- backward_propagate_error(network, expected)
- update_weights(network, row, l_rate)
- # Test training backprop algorithm
- dataset = [
- [6.3, 2.9, 5.6, 1.8, 0],
- [6.5, 3.0, 5.8, 2.2, 0],
- [7.6, 3.0, 6.6, 2.1, 0],
- [4.9, 2.5, 4.5, 1.7, 0],
- [7.3, 2.9, 6.3, 1.8, 0],
- [6.7, 2.5, 5.8, 1.8, 0],
- [7.2, 3.6, 6.1, 2.5, 0],
- [6.5, 3.2, 5.1, 2.0, 0],
- [6.4, 2.7, 5.3, 1.9, 0],
- [6.8, 3.0, 5.5, 2.1, 0],
- [5.7, 2.5, 5.0, 2.0, 0],
- [5.8, 2.8, 5.1, 2.4, 0],
- [6.4, 3.2, 5.3, 2.3, 0],
- [6.5, 3.0, 5.5, 1.8, 0],
- [7.7, 3.8, 6.7, 2.2, 0],
- [7.7, 2.6, 6.9, 2.3, 0],
- [6.0, 2.2, 5.0, 1.5, 0],
- [6.9, 3.2, 5.7, 2.3, 0],
- [5.6, 2.8, 4.9, 2.0, 0],
- [7.7, 2.8, 6.7, 2.0, 0],
- [6.3, 2.7, 4.9, 1.8, 0],
- [6.7, 3.3, 5.7, 2.1, 0],
- [7.2, 3.2, 6.0, 1.8, 0],
- [6.2, 2.8, 4.8, 1.8, 0],
- [6.1, 3.0, 4.9, 1.8, 0],
- [6.4, 2.8, 5.6, 2.1, 0],
- [7.2, 3.0, 5.8, 1.6, 0],
- [7.4, 2.8, 6.1, 1.9, 0],
- [7.9, 3.8, 6.4, 2.0, 0],
- [6.4, 2.8, 5.6, 2.2, 0],
- [6.3, 2.8, 5.1, 1.5, 0],
- [6.1, 2.6, 5.6, 1.4, 0],
- [7.7, 3.0, 6.1, 2.3, 0],
- [6.3, 3.4, 5.6, 2.4, 0],
- [5.1, 3.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3.2, 1.6, 0.2, 1],
- [4.8, 3.1, 1.6, 0.2, 1],
- [5.4, 3.4, 1.5, 0.4, 1],
- [5.2, 4.1, 1.5, 0.1, 1],
- [5.5, 4.2, 1.4, 0.2, 1],
- [4.9, 3.1, 1.5, 0.2, 1],
- [5.0, 3.2, 1.2, 0.2, 1],
- [5.5, 3.5, 1.3, 0.2, 1],
- [4.9, 3.6, 1.4, 0.1, 1],
- [4.4, 3.0, 1.3, 0.2, 1],
- [5.1, 3.4, 1.5, 0.2, 1],
- [5.0, 3.5, 1.3, 0.3, 1],
- [4.5, 2.3, 1.3, 0.3, 1],
- [4.4, 3.2, 1.3, 0.2, 1],
- [5.0, 3.5, 1.6, 0.6, 1],
- [5.9, 3.0, 5.1, 1.8, 0],
- [5.1, 3.8, 1.9, 0.4, 1],
- [4.8, 3.0, 1.4, 0.3, 1],
- [5.1, 3.8, 1.6, 0.2, 1],
- [5.5, 2.4, 3.7, 1.0, 2],
- [5.8, 2.7, 3.9, 1.2, 2],
- [6.0, 2.7, 5.1, 1.6, 2],
- [6.7, 3.1, 5.6, 2.4, 0],
- [6.9, 3.1, 5.1, 2.3, 0],
- [5.8, 2.7, 5.1, 1.9, 0],
- ]
- def predict(network,row):
- outputs = forward_propagate(network,row)
- return outputs.index(max(outputs))
- def getScore(network,data):
- score = 0
- for row in data:
- prediction = predict(network,row)
- if (prediction == row[-1]):
- score += 1
- return score
- def getDataset(dataset):
- list = []
- validation = []
- for i in range(0, len(dataset) - 10):
- list.append(dataset[i])
- for i in range(len(dataset) - 10, len(dataset)):
- validation.append(dataset[i])
- return (list,validation)
- if __name__ == "__main__":
- # ne menuvaj
- seed(1)
- att1 = input()
- att2 = input()
- att3 = input()
- att4 = input()
- planttype = input()
- testCase = [att1, att2, att3, att4, planttype]
- data = getDataset(dataset)[0]
- validation = getDataset(dataset)[1]
- n_inputs=len(data[0]) -1
- n_outputs=len(set(row[-1] for row in data))
- network1=initialize_network(n_inputs,3,n_outputs)
- network2=initialize_network(n_inputs,3,n_outputs)
- network3=initialize_network(n_inputs,3,n_outputs)
- train_network(network1,data,0.3,20,n_outputs)
- train_network(network2, data, 0.5, 20, n_outputs)
- train_network(network3, data, 0.7, 20, n_outputs)
- score_network1 = getScore(network1,validation)
- score_network2 = getScore(network2,validation)
- score_network3 = getScore(network3,validation)
- MAX = max(score_network1,score_network2,score_network3)
- if (MAX == score_network1):
- prediction = predict(network1, testCase)
- print(prediction)
- elif (MAX == score_network2):
- prediction = predict(network2, testCase)
- print(prediction)
- else :
- prediction = predict(network3, testCase)
- print(prediction)
- # vasiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement