Exemple #1
0
def print_word_error(infile, outfile, X_test, y_test, l):
    lines = []
    f_vals = []
    #just to keep the file open for as short a time as possible
    file = open(infile, 'r')
    for line in file:
        lines.append(line)
    file.close()
    for i, line in enumerate(lines):
        split = line.split()
        print(str(i) + ": ", end = '')
        params = np.array(split[1:]).astype(np.float)
        w = gc.w_matrix(params)
        t = gc.t_matrix(params)


        predictions = fg.predict(X_test, w, t)
        
        accuracy = fg.accuracy(predictions, y_test)
        print(accuracy[0])
        #only word accuracy so just accuracy[0]
        f_vals.append(str(1 - accuracy[0]) + "\n")
        
    file = open(outfile, 'w')
    file.writelines(f_vals)
    file.close()
Exemple #2
0
def func_to_minimize_word(params, X_train, y_train, word_num, l):
    w = gc.w_matrix(params)
    t = gc.t_matrix(params)
    reg = 1 / 2 * np.sum(params**2)
    w_x = np.inner(X_train[word_num], w)
    prob = gc.log_p_y_given_x(w_x, y_train[word_num], t, word_num)

    return -prob + l * reg
    def train_with_samples(self, call_func):
        # parameter tuning using ADAM

        epoch = 0
        m_t = 0  # first order moment
        v_t = 0  # second order moment

        # init params
        theta = np.zeros(129 * 26 + 26**2)

        # init gradient
        grad = np.ones_like(theta)
        while True:
            epoch += 1

            theta_prev = theta
            #print("Iterations : " + str(epoch))

            w = gc.w_matrix(theta)
            t = gc.t_matrix(theta)
            samples, sampled_word_index = self.generate_samples(
                call_func.X_train, w, t, 100)  #theta[:3354], theta[3354:], 5)
            # calculate gradient
            x_train_array = []
            x_train_array.append(call_func.X_train[sampled_word_index])

            y_train_array = []
            y_train_array.append(samples[-1])

            grad = fg.grad_func_word(theta, x_train_array, y_train_array, 0,
                                     self.lambda_param)
            #   grad = fg.grad_func_word(theta, x_train_array, samples, 0, 0.01)
            #   grad = gc.gradient_avg(theta, (X_train[sampled_word_index], len(samples), 1), samples, len(samples) - 1)

            # biased moments
            m_t = self.beta_1 * m_t + (1 - self.beta_1) * grad
            v_t = self.beta_2 * v_t + (1 - self.beta_2) * np.square(grad)

            # bias corrected first and second order moments
            m_t_hat = m_t / (1 - self.beta_1**epoch)
            v_t_hat = v_t / (1 - self.beta_2**epoch)

            # update params
            theta = theta_prev - (self.learning_rate *
                                  m_t_hat) / (np.sqrt(v_t_hat) + self.epsilon)

            #print(theta)
            # termination condition
            if sum(abs(theta - theta_prev)
                   ) <= self.epsilon or epoch == self.iterations:
                break

            if (epoch % 100 == 0 and epoch > 0):
                call_func.call_back(theta)

        return theta
Exemple #4
0
def print_accuracies(params, X_train, y_train, X_test, y_test):
    w = gc.w_matrix(params)
    t = gc.t_matrix(params)
    predictions = predict(X_train, w, t)
    train_accuracy = accuracy(predictions, y_train)
    print("%.3f, " % train_accuracy[0], end='')
    print("%.3f, " % train_accuracy[1], end='')

    predictions = predict(X_test, w, t)
    test_accuracy = accuracy(predictions, y_test)
    print("%.3f, " % test_accuracy[0], end='')
    print("%.3f " % test_accuracy[1])
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  6 19:30:05 2018

@author: Erik
"""

import get_data as gd
import part2b_code as p2b
import part1c_code as p1c
import gradient_calculation as gc

X_test, y_test = gd.read_data_formatted('test_struct.txt')
X_train, y_train = gd.read_data_formatted('train_struct_1000.txt')
params = gd.get_params()

p2b.optimize(params, X_train, y_train, 1000, 'solution_1000_distortion')

params = p2b.get_optimal_params('solution_1000_distortion')
w = gc.w_matrix(params)
t = gc.t_matrix(params)

print("Function value: ")
print(p2b.func_to_minimize(params, X_train, y_train, 1000))

y_pred = p2b.predict(X_test, w, t)

print(p2b.accuracy(y_pred, y_test))
Exemple #6
0
def grad_func_word(params, X_train, y_train, word_num, l):
    w = gc.w_matrix(params)
    t = gc.t_matrix(params)
    grad_avg = gc.gradient_word(X_train, y_train, w, t, word_num)
    grad_reg = params
    return -grad_avg + l * grad_reg