def backpropagate(network, input_vector, targets): hidden_outputs, outputs = feed_forward(network, input_vector) # the output * (1 - output) is from the derivative of sigmoid output_deltas = [ output * (1 - output) * (output - target) for output, target in zip(outputs, targets) ] # adjust weights for output layer, one neuron at a time for i, output_neuron in enumerate(network[-1]): # focus on the ith output layer neuron for j, hidden_output in enumerate(hidden_outputs + [1]): # adjust the jth weight based on both this neuron's delta and jth input output_neuron[j] -= output_deltas[i] * hidden_output #back-propagate errors to hidden layer hidden_deltas = [ hidden_output * (1 - hidden_output) * fnc.dot(output_deltas, [n[i] for n in output_layer]) for i, hidden_output in enumerate(hidden_outputs) ] #adjust weights for hidden layer, one neuron at a time for i, hidden_neuron in enumerate(network[0]): for j, input in enumerate(input_vector + [1]): hidden_neuron[j] -= hidden_deltas[i] * input
def ridge_penalty(beta, alpha): return alpha * fnc.dot(beta[1:], beta[1:])
def predict(x_i, beta): """assumes that the first element of each x_i is 1""" return fnc.dot(x_i, beta)
return alpha * fnc.dot(beta[1:], beta[1:]) def squared_error_ridge(x_i, y_i, beta, alpha): """estimate error plus ridge penalty on beta""" return error(x_i, y_i, beta) ** 2 + ridge_penalty(beta, alpha) def ridge_penalty_gradient(beta, alpha): """gradient of just the ridge penalty""" return [0] + [2 * alpha * beta_j for beta_j in beta[1:]] def squared_error_ridge_gradient(x_i, y_i, beta, alpha): """the gradient corresponding to the ith squared error term including the ridge penalty""" return fnc.vector_add(squared_error_gradient(x_i, y_i, beta), ridge_penalty_gradient(beta, alpha)) def estimate_beta_ridge(x, y, alpha): """use gradient descent to fit a ridge regression with penalty alpha""" beta_initial = [random.random() for x_i in x[0]] return fnc.minimize_stochastic(partial(squared_error_ridge, alpha=alpha), partial(squared_error_ridge_gradient, alpha=alpha), x, y, beta_initial, 0.001) random.seed(0) beta_0 = estimate_beta_ridge(x, daily_minutes_good, alpha=0.1) fnc.dot(beta_0[1:], beta_0[1:]) print(beta_0, multiple_r_squared(x, daily_minutes_good, beta_0))
def logistic_log_partial_ij(x_i, y_i, beta, j): """here i is the index of the data point, j is the index of the derivative""" return (y_i - logistic(fnc.dot(x_i, beta))) * x_i[j]
def logistic_log_likelihood_i(x_i, y_i, beta): if y_i == 1: return math.log(logistic(fnc.dot(x_i, beta))) else: return math.log(1 - logistic(fnc.dot(x_i, beta)))
#maximize log likelihood on the training data fn = partial(logistic_log_likelihood, x_train, y_train) gradient_fin = partial(logistic_log_gradient, x_train, y_train) beta_0 = [random.random() for _ in range(3)] #maximize using gradient descent beta_hat = fnc.maximize_stochastic(logistic_log_likelihood_i, logistic_log_gradient_i, x_train, y_train, beta_0) true_positives = false_positives = true_negatives = false_negatives = 0 for x_i, y_i in zip(x_test, y_test): predict = logistic(fnc.dot(beta_hat, x_i)) if y_i == 1 and predict >= 0.5: true_positives += 1 elif y_i == 1: false_negatives += 1 elif predict >= 0.5: false_positives += 1 else: true_negatives += 1 precision = true_positives / (true_positives + false_positives) recall = true_positives / (true_positives + false_negatives) #print(precision, recall)
def matrix_product_entry(A, B, i, j): return dot(get_row(A, i), get_column(B, j))
def update_road(self, road): self.cur_road = road if functions.dot((self.vx, self.vy), road.vector) > 0: self.next_junction = road.end_junction else: self.next_junction = road.start_junction
def cosine_similarity(v, w): return dot(v, w) / math.sqrt(dot(v, v) * dot(w, w))
def neuron_output(weights, inputs): return sigmoid(fnc.dot(weights, inputs))
def perceptron_output(weights, bias, x): """returns 1 if the perceptron 'fires', 0 if not""" calculation = fnc.dot(weights, x) + bias return step_function(calculation)