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 # its jth input output_neuron[j] -= output_deltas[i] * hidden_output # back-propagate errors to hidden layers hidden_deltas = [hidden_output * (1 - hidden_output) * 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 nn_cost_function(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmd): # Useful value m = y.size theta1, theta2 = deserialize_t(nn_params, input_layer_size, hidden_layer_size, num_labels) _, _, _, _, h = feed_forward(theta1, theta2, X) ys = extend_y(y, num_labels) cost = np.sum(-ys * np.log(h) - (1 - ys) * np.log(1 - h)) return cost / m + nn_cost_function_reg(theta1, theta2, m, lmd)
def nn_grad_function(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmd): # Reshape nn_params back into the parameters theta1 and theta2, the weight 2-D arrays # for our two layer neural network theta1, theta2 = deserialize_t(nn_params, input_layer_size, hidden_layer_size, num_labels) a1, z2, a2, z3, h = feed_forward(theta1, theta2, X) # a1(5000, 401) a2(5000, 26), h(5000, 10) # Useful value m = y.size ys = extend_y(y, num_labels) d3 = h - ys # (5000, 10) d2 = d3 @ theta2 * sigmoid_gradient_a(a2) # (5000, 26) theta2_grad = d3.T.dot(a2) / m + nn_grad_function_reg(theta2, lmd, m) theta1_grad = d2.T.dot(a1)[1:, :] / m + nn_grad_function_reg(theta1, lmd, m) grad = np.concatenate([theta1_grad.flatten(), theta2_grad.flatten()]) return grad
def cost_and_gradient(thetas): theta1 = np.zeros(thetas[0].shape) theta2 = np.zeros(thetas[1].shape) theta3 = np.zeros(thetas[2].shape) theta4 = np.zeros(thetas[3].shape) gradient = np.array([theta1, theta2, theta3, theta4]) with open('../csvFiles/2000/trainresults.csv') as results_file: with open('../csvFiles/2000/trainimages.csv') as images_file: results_lines=results_file.readlines() image_lines=images_file.readlines() for i in range(0,len(image_lines)): spected_result = np.fromstring(results_lines[i], dtype=float, sep=',') image = np.fromstring(image_lines[i], dtype=float, sep=',') A = feed_forward(image.reshape(-1,1), thetas) deltas = calculate_phi(A, spected_result.reshape(-1,1),thetas) gradient += deltas return gradient/len(image_lines)
def guess(): with open('../csvFiles/thetas.csv') as thetas_file: thetas_lines = thetas_file.readlines() theta1 = np.fromstring(thetas_lines[ITERATION * 4 + 0], dtype=float, sep=',').reshape(100, 785) theta2 = np.fromstring(thetas_lines[ITERATION * 4 + 1], dtype=float, sep=',').reshape(50, 101) theta3 = np.fromstring(thetas_lines[ITERATION * 4 + 2], dtype=float, sep=',').reshape(10, 51) theta4 = np.fromstring(thetas_lines[ITERATION * 4 + 3], dtype=float, sep=',').reshape(10, 11) thetas = np.array([theta1, theta2, theta3, theta4]) content = Image.open('../testimg/image.bmp') img = tobinary(np.array(content)[:, :, 0].reshape(-1, 1)) A = feed_forward(img, thetas) print(A[4])
def create_net(self): if self.name == 'feed_forward': self.model = feed_forward(self.input_dim, self.output_dim, self.n_hid) self.J = nn.MSELoss(size_average=True, reduce=True) elif self.name == 'autoencoder': self.model = Autoencoder(self.input_dim, self.output_dim, self.n_hid, self.n_bottleneck) self.J = nn.MSELoss(size_average=True, reduce=True) elif self.name == 'ff_mlpg': self.model = ff_mlpg(self.input_dim, self.output_dim, self.n_hid) self.J = Nloss_GD(self.input_dim) else: pass if self.cuda: self.model = self.model.cuda() self.J = self.J.cuda() print(' Total params: %.2fM' % (sum(p.numel() for p in self.model.parameters()) / 1000000.0))
from feed_forward import feed_forward xor_network = [ # hidden layer [ [20, 20, -30], # 'and' neuron [20, 20, -10] ], # 'or' neuron # output layer [[-60, 60, -30]] ] # '2nd input but not 1st input' neuron for x in [0, 1]: for y in [0, 1]: # feed_forward produces the output of every neuron # feed_forward[-1] is the outputs of the output-layer neurons print x, y, feed_forward(xor_network, [x, y])[-1]
dtype=float, sep=',').reshape(10, 11) thetas = np.array([theta1, theta2, theta3, theta4]) goods = 0 bads = 0 with open('../csvFiles/twickresults.csv') as results_file: with open('../csvFiles/twickimages.csv') as images_file: results_lines = results_file.readlines() image_lines = images_file.readlines() for i in range(0, len(image_lines)): spected_result = np.fromstring(results_lines[i], dtype=float, sep=',') image = np.fromstring(image_lines[i], dtype=float, sep=',') A = feed_forward(image.reshape(-1, 1), thetas) if (np.argmax(A[3]) == np.argmax(spected_result)): goods += 1 else: bads += 1 print(goods) print(bads) total = bads + goods print(goods / total * 100) ''' ITERACION 0: sigmoide normalizado: 10% ~235 iteraciones ITERACION : sigmoide normalizado * 3: >235 iteraciones ITERACION 1:
from feed_forward import feed_forward xor_network = [# hidden layer [[20, 20, -30], # 'and' neuron [20, 20, -10]], # 'or' neuron # output layer [[-60, 60, -30]]] # '2nd input but not 1st input' neuron for x in [0, 1]: for y in [0, 1]: # feed_forward produces the output of every neuron # feed_forward[-1] is the outputs of the output-layer neurons print x, y, feed_forward(xor_network, [x, y])[-1]