Skip to content

Instantly share code, notes, and snippets.

@Arham4
Last active October 28, 2021 04:19
Show Gist options
  • Save Arham4/0725bb28c552aa072a35e6f8eea7b17c to your computer and use it in GitHub Desktop.
Save Arham4/0725bb28c552aa072a35e6f8eea7b17c to your computer and use it in GitHub Desktop.
Assignment 4 Part 1 - Gradient Descent
import math
dot_product = lambda X, Y: sum(map(lambda x, y: x * y, X, Y))
def sigma(t):
return 1 / (1 + math.exp(-t))
def sigma_prime(t):
sigma_value = sigma(t)
return sigma_value * (1 - sigma_value)
import sys
from gradient_descent_math import dot_product, sigma, sigma_prime
initial_weight = 0
def read_training_file(training_file_name):
training_file = open(training_file_name, 'r')
attributes = []
examples = []
correct_values = []
raw_data = training_file.read().splitlines()
for i in range(len(raw_data)):
line_split = raw_data[i].split('\t')
if i == 0:
for datum in line_split:
attributes.append(datum)
else:
examples.append([int(line_split[x]) for x in range(len(line_split) - 1)])
correct_values.append(int(line_split[len(line_split) - 1]))
return (attributes, examples, correct_values)
def calculate_weights(weights, examples, correct_values, iteration, learning_rate):
global dot_product
example = examples[iteration]
correct_value = correct_values[iteration]
predicted_value = sigma(dot_product(weights, example))
delta = correct_value - predicted_value
predicted_value_slope = sigma_prime(dot_product(weights, example))
correction = [x * learning_rate * delta * predicted_value_slope for x in example]
return [sum(w) for w in zip(weights, correction)]
def print_iteration(attributes, weights, examples, iteration):
example = examples[iteration]
predicted_value = sigma(dot_product(weights, example))
print('After iteration ' + str(iteration + 1) + ': ', end='')
for i in range(len(weights)):
print('w(' + attributes[i] + ') = %.4f, ' % weights[i], end='')
print('output = %.4f' % predicted_value)
if len(sys.argv) != 5:
print('The program must be run with the training file, the test file, the learning rate, and the number of iterations as input.')
else:
training_file = sys.argv[1]
test_file = sys.argv[2]
learning_rate = float(sys.argv[3])
iterations = int(sys.argv[4])
attributes, examples, correct_values = read_training_file(training_file)
weights = [initial_weight] * len(examples[0])
for iteration in range(iterations):
weights = calculate_weights(weights, examples, correct_values, iteration, learning_rate)
print_iteration(attributes, weights, examples, iteration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment