def trainFwdVision(self, curr2DPos): self.trainEstVision = utils.sigmoid( np.dot(self.fwdVisionW.T, self.prvFwdVisionState)) self.errorTrainEstVision = utils.change_range(curr2DPos, -1., 1., 0., 1.) - self.trainEstVision self.fwdVisionW += self.cbETA * np.outer(self.prvFwdVisionState, self.errorTrainEstVision)
def postprocess(preds_ailia, anchor_path='anchors.npy', back=False, min_score_thresh=DEFAULT_MIN_SCORE_THRESH): raw_box = preds_ailia[0] # (1, 896, 16) raw_score = preds_ailia[1] # (1, 896, 1) anchors = np.load(anchor_path).astype(np.float32) score_thresh = 100.0 detection_boxes = decode_boxes(raw_box, back, anchors) # (1, 896, 16) raw_score = np.clip(raw_score, -score_thresh, score_thresh) # (1, 896, 1) detection_scores = np.squeeze(sigmoid(raw_score), axis=-1) # (1, 896) # Note: we stripped off the last dimension from the scores tensor # because there is only has one class. Now we can simply use a mask # to filter out the boxes with too low confidence. mask = detection_scores >= min_score_thresh # (1, 896) # Because each image from the batch can have a different number of # detections, process them one at a time using a loop. detections = [] for i in range(raw_box.shape[0]): boxes = detection_boxes[i, mask[i]] scores = np.expand_dims(detection_scores[i, mask[i]], axis=-1) detections.append(np.concatenate((boxes, scores), axis=-1)) # Non-maximum suppression to remove overlapping detections: filtered_detections = [] for i in range(len(detections)): faces = weighted_non_max_suppression(detections[i]) faces = np.stack(faces) if len(faces) > 0 else np.zeros((0, 17)) filtered_detections.append(faces) return filtered_detections
def get_mask(depthimg, model): depth = np.zeros((1, 480, 640, 1)) depth[0, :, :, 0] = depthimg / 2000.0 mask = model.predict(x=depth, batch_size=1) # out =np.zeros(mask.shape,dtype='uint8') mask = math_utils.sigmoid(mask[0, :, :, 0]) # out[np.where(mask>0.5)]= return mask
def write_predictions(biases, weights, test_data, test_predictions_filename): with open(test_predictions_filename, 'w', encoding='utf-8') as f: for data in test_data: activation = data[0] for i in range(len(biases)): bias = biases[i] weight = weights[i] z = dot(weight, activation) + bias activation = math_utils.sigmoid(z) actual_result = argmax(activation) f.write(str(actual_result) + '\n')
def forward_step(A_prev, W, b, activation): Z = np.dot(W, A_prev) + b if activation == 'relu': A = mu.relu(Z) elif activation == 'tanh': A = mu.tanh(Z) elif activation == 'sigmoid': A = mu.sigmoid(Z) else: raise ValueError('Unknown %s' % (activation)) cache = (A_prev, W, b, Z) return A, cache
def get_gradients(self, inputs, expected_outputs): bias_gradients = [np.zeros(bias.shape) for bias in self.biases] weight_gradients = [np.zeros(weight.shape) for weight in self.weights] # forward pass activations = [inputs] activation = activations[0] sigmoid_z_caches = [] for i in range(len(self.biases)): bias = self.biases[i] weight = self.weights[i] z = np.dot(weight, activation) + bias if i == len(self.biases) - 1: # output layer activation = math_utils.softmax(z) else: # hidden layers activation = math_utils.sigmoid(z) activations.append(activation) sigmoid_z_caches.append(z) # backward propagation # err for the output layer err = math_utils.cross_entropy(activations[-1], expected_outputs) bias_gradients[-1] = err weight_gradients[-1] = np.dot(err, activations[-2].T) # err for the hidden layers for i in range(len(self.layer_sizes) - 1, 1, -1): err = np.dot(self.weights[i-1].T, err) * \ math_utils.sigmoid_derivative(sigmoid_z_caches[i-2]) bias_gradients[i - 2] = err weight_gradients[i - 2] = np.dot(err, activations[i - 2].T) return { 'bias_gradients': bias_gradients, 'weight_gradients': weight_gradients }
def print_accuracy(biases, weights, test_data, epoch_i): nums_correct_result = 0 for data in test_data: expected_result = data[1] activation = data[0] for i in range(len(biases)): bias = biases[i] weight = weights[i] z = dot(weight, activation) + bias activation = math_utils.sigmoid(z) actual_result = argmax(activation) if actual_result == expected_result: nums_correct_result += 1 print('epoch', epoch_i, ' accuracy:', nums_correct_result / len(test_data))
def forward_step(A_prev, W, b, activation, keep_prob): Z = np.dot(W, A_prev) + b if activation == 'relu': A = mu.relu(Z) elif activation == 'tanh': A = mu.tanh(Z) elif activation == 'sigmoid': A = mu.sigmoid(Z) else: raise ValueError('Unknown %s' % (activation)) # drop-out D = np.random.rand(A.shape[0], A.shape[1]) D = D < keep_prob A = A * D A = A / keep_prob assert (D.shape == A.shape) cache = (A_prev, W, b, Z, D) return A, cache
bg.compSurprise() maze.posBuff[:, step] = maze.curr2DPos.copy() wrist.posBuff[:, step] = wrist.curr3DPos.copy() if CEREBELLUM == True: if step % ACTOR_CEREBELLUM_RATIO == 0: bg.spreadAct() if GANGLIA_NOISE == True: bg.computate_noise(T) cb.spreading(bg.currState) netOut = (K * bg.currActOut) + ((1 - K) * cb.currOut) wrist.ep = netOut + bg.currNoise if INTRALAMINAR_NUCLEI == True: bg.trainOut = utils.sigmoid( np.dot(bg.actW.T, bg.currState)) else: if step % ACTOR_CEREBELLUM_RATIO == 0: bg.spreadAct() if GANGLIA_NOISE == True: bg.computate_noise(T) netOut = K * bg.currActOut wrist.ep = netOut + bg.currNoise # print bg.currNoise if CEREBELLUM == True: bg.compEpState(wrist.ep) cb.fwdVisionState = np.hstack([bg.visionState, bg.epState]) cb.fwdVision()
if trial > startPlotting: agent.set_data(wrist.actual_2dposition[0], wrist.actual_2dposition[1]) plt.pause(0.05) # compute actual state wrist.position3d_state = np.reshape( utils.gaussian(wrist.actual_3dposition, bg.posX, bg.std_dev), bg.gaussN * 3) # wrist.position2d_state = np.reshape(utils.gaussian(wrist.actual_2dposition, bg.rewX, bg.std_dev * 10), bg.gaussN * 2) bg.actState = np.hstack( [wrist.position3d_state, wrist.reward_state]) # compute actor output bg.actOut = utils.sigmoid(bg.spreading(bg.actW, bg.actState)) # storage old noise and compute new noise bg.prvNoise = bg.actNoise.copy() bg.actNoise = utils.computate_noise(bg.prvNoise, bg.delT, bg.tau) * T #♠ bg.actual_noise = utils.Cut_range(bg.actNoise, -0.5 , 0.5) # compute 3D EPs bg.ep3D = utils.Cut_range(bg.actOut + bg.actNoise, 0., 1.) if movement > 0: wrist.saveMov3d() # compute 3d movement wrist.actual_3dposition = wrist.move3d(bg.ep3D)
def forward(self, layer_input, *args, **kwargs): self.current_layer_input = layer_input self.current_layer_output = sigmoid(layer_input) return self.current_layer_output
def output_gate(self, x, h_prev, cell_prev): out_x = np.dot(self.U_o, x) + self.B_ox out_h_prev = np.dot(self.W_o, h_prev) + self.B_oh return sigmoid(out_x + out_h_prev)
def input_gate(self, x, h_prev, cell_prev): input_x = np.dot(self.U_i, x) + self.B_ix input_h_prev = np.dot(self.W_i, h_prev) + self.B_ih return sigmoid(input_x + input_h_prev)
def trainCb(self, state, ep, T): self.trainOut = utils.sigmoid(np.dot(self.w.T, state)) self.errorOut = ep - self.trainOut self.w += self.cbETA * np.outer( state, self.errorOut) * self.trainOut * (1. - self.trainOut) * T
def fwdVision(self): self.estVision = utils.sigmoid( np.dot(self.fwdVisionW.T, self.fwdVisionState))
def spreading(self, state): self.currOut = utils.sigmoid(np.dot(self.w.T, state))
def spreadAct(self): self.currActOut = utils.sigmoid(np.dot(self.actW.T, self.currState))
def forget_gate(self, x, h_prev, cell_prev): forget_x = np.dot(self.U_f, x) + self.B_fx forget_h_prev = np.dot(self.W_f, h_prev) + self.B_fh return sigmoid(forget_x + forget_h_prev)
def fitness_spp_evol(pp): """Fitness function for the spp evol algorithm. The exact form of fitness function: Elements of the fitness function are the following: * Average of velocity correlation * delta (Ratio of collisions) * A "smooth Heaviside-theta" of the average velocity magnitude (should be centered at V_Flock) * delta (Average of distance from arena) * delta (StDev of distance from arena) where "delta" is an approximation of Dirac-delta function (can be Gauss, or step-function, or anything). Note that this "delta" should not be normalized, its maximum value should be 1.0. fitness is in the interval [0, 1]. Smooth version of the Heaviside function can be e.g. the Fermi-Dirac distribution f(E). """ # initialize empty fitness dictionary fitnesses = {} # get interesting data elements j_p = pp.header.index("p") j_vel_corr_avg = pp.header.index("cluster_vel_corr_avg") j_collisions_avg = pp.header.index("ratio_of_collisions_avg") j_velocity_magn_avg = pp.header.index("vel_abs_(cm/s)_avg") j_cluster_minsize = pp.header.index("min_cluster_size_avg") j_agents_not_in_cluster = pp.header.index("agents_not_in_cluster_avg") j_distance_from_arena_avg = pp.header.index("distance_from_arena_(cm)_avg") #j_distance_between_neighbours = pp.header.index("distance_between_neighbours_(cm)_avg") j_V_Flock = pp.header.index("v_flock_(cm/s)_avg") j_R_0 = pp.header.index("R_0_(cm)_avg") for i in xrange(len(pp.data)): # Below, we used sigma = 0.00003 for the characteristic maximal collision risk # TODO: Maybe sigma has to be defined as a function of the number of agents... a = 0.00003 delta_coll = math_utils.peak(pp.data[i][j_collisions_avg], a) #(a*a / (math.pow(pp.data[i][j_collisions_avg] + a, 2.0))) # sigmoid curve for calculating 'velocity magnitude' fitness V_flock = pp.data[i][j_V_Flock] vel_tolerance = 150.0/400.0 * V_flock vel_magn_coeff = 1.0 - math_utils.sigmoid( pp.data[i][j_velocity_magn_avg], vel_tolerance, V_flock, 1.0) #vel_magn_coeff = 1.0 - 1.0 / (math.exp ((pp.data[i][j_velocity_magn_avg] - (V_flock - 150.0)) / 50.0) + 1.0) # Below, we used sigma = 200.0 cm for the stdev of the Gauss-function. sigmasquare = 40000.0 delta_distance_from_arena = math_utils.gauss( pp.data[i][j_distance_from_arena_avg], sigmasquare, 0.0, 1.0) #delta_distance_from_arena = (math.exp ( - (math.pow (pp.data[i][j_distance_from_arena_avg], 2.0)) / sigmasquare)) # Velocity correlation should be saturated at 0 correlation_coeff = 0 if pp.data[i][j_vel_corr_avg] > 0: correlation_coeff = pp.data[i][j_vel_corr_avg] # Cluster size should be at least N / 5 N = 10.0 min_size = N / 5.0 if pp.data[i][j_cluster_minsize] > min_size: min_cluster_size_fitness = 1.0 else: min_cluster_size_fitness = pp.data[i][j_cluster_minsize] * 2.0 / N # Independent agents should not exist agents_not_in_cluster_fitness = math_utils.peak( pp.data[i][j_agents_not_in_cluster], min_size) #agents_not_in_cluster_fitness = ((min_size) * (min_size) / (math.pow(pp.data[i][j_agents_not_in_cluster] + min_size, 2))) # Distance between nearest neighbours should be around R_0 # tolerance = 300.0 # R_0 = float (pp.data[i][j_R_0]) # temp_distance = math.fabs(pp.data[i][j_distance_between_neighbours] - R_0) # distance_between_neighbours_fitness = math_utils.gauss (temp_distance, tolerance, 0.0, 1.0) # Save everything fitnesses[int(pp.data[i][j_p])] = { "vel_corr": correlation_coeff, "collision": delta_coll, "vel_magn": vel_magn_coeff, "distance_from_arena_avg": delta_distance_from_arena, "min_cluster_size": min_cluster_size_fitness, "agents_not_in_cluster": agents_not_in_cluster_fitness, #"distance_between_neighbours": distance_between_neighbours_fitness } return fitnesses