<commit_before>#-------------------------------------------------------------------------------
# Name:        homework 5
# Purpose:
#
# Author:      kirbs
#-----------------------
#!/usr/bin/env python

import numpy
import pylab
import random


def q1(variance, d):
    answers = {"a": 10,"b": 25, "c": 100, "d": 500, "e": 1000}

    for key, val in answers.iteritems():
        print key + ": " + str(variance**2 * (1 - ((d+1)/float(val))))


def gradientError(u, v):
    return (u*numpy.exp(v) - 2*v*numpy.exp(-u))**2


def updateWeights(weights, n, error):
    for i in range(len(weights)):
        weights[i] -= n
    return weights


def q5():
    n = .1
    threshold = 10**-14
    weights = [1.0,1.0]
    iterations = 0

    while True:
        error = gradientError(weights[0], weights[1])
        iterations += 1

        if error < threshold or iterations >= 10000:
            print iterations
            break
        else:
            weights = updateWeights(weights, n, error)

    print gradientError(weights[0], weights[1])
    return weights


def q6():
    weights = q5()
    answers = {"a": (1.0, 1.0), "b": (.713, .045), "c": (.016, .112), "d": (-.083, .029), "e": (.045, .024)}<commit_msg>Revise hw5q5 to include partial derivatives of u and v.<commit_after>#-------------------------------------------------------------------------------
# Name:        homework 5
# Purpose:
#
# Author:      kirbs
#-----------------------
#!/usr/bin/env python

import numpy
import pylab
import random


def q1(variance, d):
    answers = {"a": 10,"b": 25, "c": 100, "d": 500, "e": 1000}

    for key, val in answers.iteritems():
        print key + ": " + str(variance**2 * (1 - ((d+1)/float(val))))


def gradientError(u, v):
    return (u*numpy.exp(v) - 2*v*numpy.exp(-u))**2

def dE_du(u, v):
    '''
    returns partial derivative of E with respect to u.
    '''
    return 2 * ( u*numpy.exp(v) - 2*v*numpy.exp(-u) ) * ( numpy.exp(v) + 2*v*numpy.exp(-u) )

def dE_dv(u,v):
    '''
    returns partial derivative of E with respect to v.
    '''
    return  2 * ( u * numpy.exp(v) - 2*v*numpy.exp(-u) ) * ( u*numpy.exp(v) - 2*numpy.exp(-u))

def updateWeights(weights, n):
    '''
    new_weight = old_weight - (learning rate) * (partial derivative w.r.t. u or v)
    '''
    u = weights[0]
    v = weights[1]
    weights[0] -= n * dE_du(u, v)
    weights[1] -= n * dE_dv(u, v)
    return weights


def q5():
    n = .1
    threshold = 10**-14
    weights = [1.0,1.0]
    iterations = 0

    while True:
        error = gradientError(weights[0], weights[1])
        iterations += 1

        if error < threshold or iterations >= 10000:
            print iterations
            break
        else:
            weights = updateWeights(weights, n)

    print gradientError(weights[0], weights[1])
    return weights


def q6():
    weights = q5()
    answers = {"a": (1.0, 1.0), "b": (.713, .045), "c": (.016, .112), "d": (-.083, .029), "e": (.045, .024)}
{'fraction_non_alphanumeric': 0.23894701316233546, 'fraction_numerical': 0.05264934188322646, 'mean_word_length': 1.8917073170731706, 'pattern_counts': {'":': 22, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7638199', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1018, 'n_words': 282}