我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用keras.backend.gather()。
def KerasCost(self,y_true, y_pred): #create a random subsampling of the target instances for the test set #This is rarely going to hit the last entry sample = K.cast(K.round(K.random_uniform_variable(shape=tuple([self.MMDTargetSampleSize]), low=0, high=self.MMDTargetTrainSize-1)),IntType) #this is a subset operation (not a very pretty way to do it) MMDTargetSampleTrain = K.gather(self.MMDTargetTrain,sample) #do the same for the validation set sample = K.cast(K.round(K.random_uniform_variable(shape=tuple([self.MMDTargetSampleSize]), low=0, high=self.MMDTargetValidationSize-1)),IntType) #and the subset operation MMDTargetSampleValidation = K.gather(self.MMDTargetValidation,sample) #create the sample based on whether we are in training or validation steps MMDtargetSample = K.in_train_phase(MMDTargetSampleTrain, MMDTargetSampleValidation) #return the MMD cost for this subset ret= self.cost(self.MMDLayer,MMDtargetSample) #pretty dumb but y_treu has to be in the cost for keras to not barf when cleaning up ret = ret + 0*K.sum(y_pred)+0*K.sum(y_true) return ret
def _process_input(self, x): """Apply logistic and softmax activations to input tensor """ logistic_activate = lambda x: 1.0/(1.0 + K.exp(-x)) (batch, w, h, channels) = x.get_shape() x_temp = K.permute_dimensions(x, (3, 0, 1, 2)) x_t = [] for i in range(self.num): k = self._entry_index(i, 0) x_t.extend([ logistic_activate(K.gather(x_temp, (k, k + 1))), # 0 K.gather(x_temp, (k + 2, k + 3))]) if self.background: x_t.append(K.gather(x_temp, (k + 4,))) else: x_t.append(logistic_activate(K.gather(x_temp, (k + 4,)))) x_t.append( softmax( K.gather(x_temp, tuple(range(k + 5, k + self.coords + self.classes + 1))), axis=0)) x_t = K.concatenate(x_t, axis=0) return K.permute_dimensions(x_t, (1, 2, 3, 0))
def yolo_eval(yolo_outputs, image_shape, max_boxes=10, score_threshold=.6, iou_threshold=.5): """Evaluate YOLO model on given input batch and return filtered boxes.""" box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs boxes = yolo_boxes_to_corners(box_xy, box_wh) boxes, scores, classes = yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold=score_threshold) # Scale boxes back to original image shape. height = image_shape[0] width = image_shape[1] image_dims = K.stack([height, width, height, width]) image_dims = K.reshape(image_dims, [1, 4]) boxes = boxes * image_dims max_boxes_tensor = K.variable(max_boxes, dtype='int32') K.get_session().run(tf.variables_initializer([max_boxes_tensor])) nms_index = tf.image.non_max_suppression(boxes, scores, max_boxes_tensor, iou_threshold=iou_threshold) boxes = K.gather(boxes, nms_index) scores = K.gather(scores, nms_index) classes = K.gather(classes, nms_index) return boxes, scores, classes
def sample_noise_layer_input(self): if self._sample_noise_layer_input is None: if self.data is None: raise Exception("data attribute not initialized") if K._BACKEND == 'tensorflow': import tensorflow as tf c_input = tf.constant(self.data) else: c_input = K.variable(self.data) input_ndxs = K_n_choose_k(len(self.data), self.miN) noise_layer_input = K.gather(c_input, input_ndxs) for layerndx, layer in enumerate(self.model_layers): noise_layer_input = layer.call(noise_layer_input) self._sample_noise_layer_input = noise_layer_input return self._sample_noise_layer_input
def _max_attentive_vectors(self, x2, cosine_matrix): """Max attentive vectors. Calculate max attentive vector for the entire sentence by picking the contextual embedding with the highest cosine similarity as the attentive vector. # Arguments x2: sequence vectors, (batch_size, x2_timesteps, embedding_size) cosine_matrix: cosine similarities matrix of x1 and x2, (batch_size, x1_timesteps, x2_timesteps) # Output shape (batch_size, x1_timesteps, embedding_size) """ # (batch_size, x1_timesteps) max_x2_step = K.argmax(cosine_matrix, axis=-1) embedding_size = K.int_shape(x2)[-1] timesteps = K.int_shape(max_x2_step)[-1] if timesteps is None: timesteps = K.shape(max_x2_step)[-1] # collapse time dimension and batch dimension together # collapse x2 to (batch_size * x2_timestep, embedding_size) x2 = K.reshape(x2, (-1, embedding_size)) # collapse max_x2_step to (batch_size * h1_timesteps) max_x2_step = K.reshape(max_x2_step, (-1,)) # (batch_size * x1_timesteps, embedding_size) max_x2 = K.gather(x2, max_x2_step) # reshape max_x2, (batch_size, x1_timesteps, embedding_size) attentive_vector = K.reshape(max_x2, K.stack([-1, timesteps, embedding_size])) return attentive_vector
def construct_perturbed_input(perturb_mapping, onehot_vectors): """ :param perturb_mapping: :param onehot_vectors: :return: """ return K.gather(perturb_mapping, onehot_vectors)
def path_energy0(y, x, U, mask=None): '''Path energy without boundary potential handling.''' n_classes = K.shape(x)[2] y_one_hot = K.one_hot(y, n_classes) # Tag path energy energy = K.sum(x * y_one_hot, 2) energy = K.sum(energy, 1) # Transition energy y_t = y[:, :-1] y_tp1 = y[:, 1:] U_flat = K.reshape(U, [-1]) # Convert 2-dim indices (y_t, y_tp1) of U to 1-dim indices of U_flat: flat_indices = y_t * n_classes + y_tp1 U_y_t_tp1 = K.gather(U_flat, flat_indices) if mask is not None: mask = K.cast(mask, K.floatx()) y_t_mask = mask[:, :-1] y_tp1_mask = mask[:, 1:] U_y_t_tp1 *= y_t_mask * y_tp1_mask energy += K.sum(U_y_t_tp1, axis=1) return energy
def call(self, x, mask=None): sims = [] for n, sim in zip(self.n, self.similarities): for _ in range(n): batch_size = K.shape(x)[0] idx = K.random_uniform((batch_size,), low=0, high=batch_size, dtype='int32') x_shuffled = K.gather(x, idx) pair_sim = sim(x, x_shuffled) for _ in range(K.ndim(x) - 1): pair_sim = K.expand_dims(pair_sim, dim=1) sims.append(pair_sim) return K.concatenate(sims, axis=-1)
def gen_gp_loss(gp): """Generate an internal objective, `dlik_dh * H`, for a given GP layer. """ def loss(_, H): dlik_dh_times_H = H * K.gather(gp.dlik_dh, gp.batch_ids[:gp.batch_sz]) return K.sum(dlik_dh_times_H, axis=1, keepdims=True) return loss # Aliases
def get_output(self, train=False): X = self.get_input(train) if self.dropout: raise NotImplementedError() # TODO out = K.gather(self.W, X) return out
def get_output(self, train=False): X = self.get_input(train) out = K.gather(self.W, X) return out
def path_energy0(y, x, U, mask=None): """Path energy without boundary potential handling.""" n_classes = K.shape(x)[2] y_one_hot = K.one_hot(y, n_classes) # Tag path energy energy = K.sum(x * y_one_hot, 2) energy = K.sum(energy, 1) # Transition energy y_t = y[:, :-1] y_tp1 = y[:, 1:] U_flat = K.reshape(U, [-1]) # Convert 2-dim indices (y_t, y_tp1) of U to 1-dim indices of U_flat: flat_indices = y_t * n_classes + y_tp1 U_y_t_tp1 = K.gather(U_flat, flat_indices) if mask is not None: mask = K.cast(mask, K.floatx()) y_t_mask = mask[:, :-1] y_tp1_mask = mask[:, 1:] U_y_t_tp1 *= y_t_mask * y_tp1_mask energy += K.sum(U_y_t_tp1, axis=1) return energy
def batch_gather(reference, indices): ref_shape = K.shape(reference) batch_size = ref_shape[0] n_classes = ref_shape[1] flat_indices = K.arange(0, batch_size) * n_classes + K.flatten(indices) return K.gather(K.flatten(reference), flat_indices)
def yolo_eval(yolo_outputs, image_shape, max_boxes=10, score_threshold=.6, iou_threshold=.5): """Evaluate YOLO model on given input batch and return filtered boxes.""" box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs boxes = yolo_boxes_to_corners(box_xy, box_wh) boxes, scores, classes = yolo_filter_boxes( boxes, box_confidence, box_class_probs, threshold=score_threshold) # Scale boxes back to original image shape. height = image_shape[0] width = image_shape[1] image_dims = K.stack([height, width, height, width]) image_dims = K.reshape(image_dims, [1, 4]) boxes = boxes * image_dims # TODO: Something must be done about this ugly hack! max_boxes_tensor = K.variable(max_boxes, dtype='int32') K.get_session().run(tf.variables_initializer([max_boxes_tensor])) nms_index = tf.image.non_max_suppression( boxes, scores, max_boxes_tensor, iou_threshold=iou_threshold) boxes = K.gather(boxes, nms_index) scores = K.gather(scores, nms_index) classes = K.gather(classes, nms_index) return boxes, scores, classes
def get_output(self, train=False): X = train out = K.gather(self.W, X) return out
def neighbour_lookup(atoms, edges, maskvalue=0, include_self=False): ''' Looks up the features of an all atoms neighbours, for a batch of molecules. # Arguments: atoms (K.tensor): of shape (batch_n, max_atoms, num_atom_features) edges (K.tensor): of shape (batch_n, max_atoms, max_degree) with neighbour indices and -1 as padding value maskvalue (numerical): the maskingvalue that should be used for empty atoms or atoms that have no neighbours (does not affect the input maskvalue which should always be -1!) include_self (bool): if True, the featurevector of each atom will be added to the list feature vectors of its neighbours # Returns: neigbour_features (K.tensor): of shape (batch_n, max_atoms(+1), max_degree, num_atom_features) depending on the value of include_self # Todo: - make this function compatible with Tensorflow, it should be quite trivial because there is an equivalent of `T.arange` in tensorflow. ''' # The lookup masking trick: We add 1 to all indices, converting the # masking value of -1 to a valid 0 index. masked_edges = edges + 1 # We then add a padding vector at index 0 by padding to the left of the # lookup matrix with the value that the new mask should get masked_atoms = temporal_padding(atoms, (1,0), padvalue=maskvalue) # Import dimensions atoms_shape = K.shape(masked_atoms) batch_n = atoms_shape[0] lookup_size = atoms_shape[1] num_atom_features = atoms_shape[2] edges_shape = K.shape(masked_edges) max_atoms = edges_shape[1] max_degree = edges_shape[2] # create broadcastable offset offset_shape = (batch_n, 1, 1) offset = K.reshape(T.arange(batch_n, dtype=K.dtype(masked_edges)), offset_shape) offset *= lookup_size # apply offset to account for the fact that after reshape, all individual # batch_n indices will be combined into a single big index flattened_atoms = K.reshape(masked_atoms, (-1, num_atom_features)) flattened_edges = K.reshape(masked_edges + offset, (batch_n, -1)) # Gather flattened flattened_result = K.gather(flattened_atoms, flattened_edges) # Unflatten result output_shape = (batch_n, max_atoms, max_degree, num_atom_features) output = T.reshape(flattened_result, output_shape) if include_self: return K.concatenate([K.expand_dims(atoms, dim=2), output], axis=2) return output
def call(self, x, mask=None): input_vector = x[0] target_classes = x[1] nb_req_classes = self.input_spec[1].shape[1] if nb_req_classes is None: nb_req_classes = K.shape(target_classes) if K.dtype(target_classes) != 'int32': target_classes = K.cast(target_classes, 'int32') if self.mode == 0: # One giant matrix mul input_dim = self.input_spec[0].shape[1] nb_req_classes = self.input_spec[1].shape[1] path_lengths = map(len, self.paths) huffman_codes = K.variable(np.array(self.huffman_codes)) req_nodes = K.gather(self.class_path_map, target_classes) req_W = K.gather(self.W, req_nodes) y = K.batch_dot(input_vector, req_W, axes=(1, 3)) if self.bias: req_b = K.gather(self.b, req_nodes) y += req_b y = K.sigmoid(y[:, :, :, 0]) req_huffman_codes = K.gather(huffman_codes, target_classes) return K.prod(req_huffman_codes + y - 2 * req_huffman_codes * y, axis=-1) # Thug life elif self.mode == 1: # Many tiny matrix muls probs = [] for i in range(len(self.paths)): huffman_code = self.huffman_codes[i] path = self.paths[i] prob = 1. for j in range(len(path)): node = path[j] node_index = self.node_indices[node] p = K.dot(input_vector, self.W[node_index, :, :])[:, 0] if self.bias: p += self.b[node_index, :][0] h = huffman_code[j] p = K.sigmoid(p) prob *= h + p - 2 * p * h probs += [prob] probs = K.pack(probs) req_probs = K.gather(probs, target_classes) req_probs = K.permute_dimensions(req_probs, (0, 2, 1)) req_probs = K.reshape(req_probs, (-1, nb_req_classes)) batch_size = K.shape(input_vector)[0] indices = arange(batch_size * batch_size, batch_size + 1) req_probs = K.gather(req_probs, indices) return req_probs
def call(self, x, mask=None): if isinstance(x, list): x,_ = x if mask is not None and isinstance(mask, list): mask,_ = mask if 0. < self.dropout < 1.: retain_p = 1. - self.dropout dims = self.W._keras_shape[:-1] B = K.random_binomial(dims, p=retain_p) * (1. / retain_p) B = K.expand_dims(B) W = K.in_train_phase(self.W * B, self.W) else: W = self.W if self.mode == 'matrix': return K.gather(W,x) elif self.mode == 'tensor': # quick and dirty: only allowing for 3dim inputs when it's tensor mode assert K.ndim(x) == 3 # put sequence on first; gather; take diagonal across shared batch dimension # in other words, W is (B, S, F) # incoming x is (B, S, A) inds = K.arange(self.W._keras_shape[0]) #out = K.gather(K.permute_dimensions(W, (1,0,2)), x).diagonal(axis1=0, axis2=3) #return K.permute_dimensions(out, (3,0,1,2)) ### method above doesn't do grads =.= # tensor abc goes to bac, indexed onto with xyz, goes to xyzac, # x == a, so shape to xayzc == xxyzc # take diagonal on first two: xyzc #out = K.colgather() out = K.gather(K.permute_dimensions(W, (1,0,2)), x) out = K.permute_dimensions(out, (0,3,1,2,4)) out = K.gather(out, (inds, inds)) return out else: raise Exception('sanity check. should not be here.') #all_dims = T.arange(len(self.W._keras_shape)) #first_shuffle = [all_dims[self.embed_dim]] + all_dims[:self.embed_dim] + all_dims[self.embed_dim+1:] ## 1. take diagonal from 0th to ## chang eof tactics ## embed on time or embed on batch. that's all I'm supporting. ## if it's embed on time, then, x.ndim+1 is where batch will be, and is what ## i need to take the diagonal over. ## now dim shuffle the xdims + 1 to the front. #todo: get second shuffle or maybe find diagonal calculations #out = K.gather(W, x) #return out ### reference #A = S(np.arange(60).reshape(3,4,5)) #x = S(np.random.randint(0, 4, (3,4,10))) #x_emb = A.dimshuffle(1,0,2)[x].dimshuffle(0,3,1,2,4)[T.arange(A.shape[0]), T.arange(A.shape[0])]