我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用keras.backend.arange()。
def to_configs(states, verbose=True, **kwargs): base = setting['base'] width = states.shape[1] // base height = states.shape[1] // base load(width,height) def build(): P = len(setting['panels']) states = Input(shape=(height*base,width*base)) error = build_error(states, height, width, base) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], height * width, -1]) # a, pos, panel matches = K.permute_dimensions(matches, [0,2,1]) # a, panel, pos config = matches * K.arange(height*width,dtype='float') config = K.sum(config, axis=-1) return Model(states, wrap(states, config)) model = build() return model.predict(states, **kwargs)
def tensor_swirl(image, center=None, strength=1, radius=100, rotation=0, cval=0.0, **kwargs): # **kwargs is for unsupported options (ignored) cval = tf.fill(K.shape(image)[0:1], cval) shape = K.int_shape(image)[1:3] if center is None: center = np.array(shape) / 2 ys = np.expand_dims(np.repeat(np.arange(shape[0]), shape[1]),-1) xs = np.expand_dims(np.tile (np.arange(shape[1]), shape[0]),-1) map_xs, map_ys = swirl_mapping(xs, ys, center, rotation, strength, radius) mapping = np.zeros((*shape, *shape)) for map_x, map_y, x, y in zip(map_xs, map_ys, xs, ys): results = tensor_linear_interpolation(image, map_x, map_y, cval) for _y, _x, w in results: # mapping[int(y),int(x),int(_y),int(_x),] = w mapping[int(_y),int(_x),int(y),int(x),] = w results = tf.tensordot(image, K.variable(mapping), [[1,2],[0,1]]) # results = K.reshape(results, K.shape(image)) return results
def to_configs(states, verbose=True, **kwargs): base = panels.shape[1] dim = states.shape[1] - pad*2 size = dim // base def build(): states = Input(shape=(dim+2*pad,dim+2*pad)) s = tensor_swirl(states, radius=dim+2*pad * relative_swirl_radius, **unswirl_args) error = build_errors(s,base,pad,dim,size) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], size * size, -1]) # a, pos, panel config = matches * K.arange(2,dtype='float') config = K.sum(config, axis=-1) # this is 0,1 configs; for compatibility, we need -1 and 1 config = - (config - 0.5)*2 return Model(states, wrap(states, K.round(config))) return build().predict(states, **kwargs)
def to_configs(states, verbose=True, **kwargs): base = panels.shape[1] size = states.shape[1]//base dim = states.shape[1] def build(): states = Input(shape=(dim,dim)) error = build_errors(states,base,dim,size) matches = 1 - K.clip(K.sign(error - threshold),0,1) # a, h, w, panel matches = K.reshape(matches, [K.shape(states)[0], size * size, -1]) # a, pos, panel config = matches * K.arange(2,dtype='float') config = K.sum(config, axis=-1) # this is 0,1 configs; for compatibility, we need -1 and 1 config = - (config - 0.5)*2 return Model(states, wrap(states, K.round(config))) model = build() return model.predict(states, **kwargs)
def call(self, x, mask=None): # x should be an output and a target assert len(x) == 2 losses = _per_sample_loss(self.loss, mask, x) if self.fast: grads = K.sqrt(sum([ K.sum(K.square(g), axis=1) for g in K.gradients(losses, self.parameter_list) ])) else: nb_samples = K.shape(losses)[0] grads = K.map_fn( lambda i: self._grad_norm(losses[i]), K.arange(0, nb_samples), dtype=K.floatx() ) return K.reshape(grads, (-1, 1))
def get_split_averages(input_tensor, input_mask, indices): # Splits input tensor into three parts based on the indices and # returns average of values prior to index, values at the index and # average of values after the index. # input_tensor: (batch_size, input_length, input_dim) # input_mask: (batch_size, input_length) # indices: (batch_size, 1) # (1, input_length) length_range = K.expand_dims(K.arange(K.shape(input_tensor)[1]), dim=0) # (batch_size, input_length) batched_range = K.repeat_elements(length_range, K.shape(input_tensor)[0], 0) tiled_indices = K.repeat_elements(indices, K.shape(input_tensor)[1], 1) # (batch_size, input_length) greater_mask = K.greater(batched_range, tiled_indices) # (batch_size, input_length) lesser_mask = K.lesser(batched_range, tiled_indices) # (batch_size, input_length) equal_mask = K.equal(batched_range, tiled_indices) # (batch_size, input_length) # We also need to mask these masks using the input mask. # (batch_size, input_length) if input_mask is not None: greater_mask = switch(input_mask, greater_mask, K.zeros_like(greater_mask)) lesser_mask = switch(input_mask, lesser_mask, K.zeros_like(lesser_mask)) post_sum = K.sum(switch(K.expand_dims(greater_mask), input_tensor, K.zeros_like(input_tensor)), axis=1) # (batch_size, input_dim) pre_sum = K.sum(switch(K.expand_dims(lesser_mask), input_tensor, K.zeros_like(input_tensor)), axis=1) # (batch_size, input_dim) values_at_indices = K.sum(switch(K.expand_dims(equal_mask), input_tensor, K.zeros_like(input_tensor)), axis=1) # (batch_size, input_dim) post_normalizer = K.expand_dims(K.sum(greater_mask, axis=1) + K.epsilon(), dim=1) # (batch_size, 1) pre_normalizer = K.expand_dims(K.sum(lesser_mask, axis=1) + K.epsilon(), dim=1) # (batch_size, 1) return K.cast(pre_sum / pre_normalizer, 'float32'), values_at_indices, K.cast(post_sum / post_normalizer, 'float32')
def f1_score_keras(y_true, y_pred): # convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) # y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 # indices_x = K.arange(start=0, stop=y_true.get_shape()[0]) indices_x = K.expand_dims(K.arange(start=0, stop=tf.shape(y_true)[0], dtype='int64'), dim=-1) indices_y = K.expand_dims(K.argmax(y_pred, axis=-1), dim=-1) indices = K.concatenate((indices_x, indices_y)) values = K.sum(K.ones_like(indices_x, dtype='float32'), axis=-1) shape = K.cast(tf.shape(y_pred_ones), dtype='int64') delta = tf.SparseTensor(indices, values, shape) y_pred_ones = y_pred_ones + tf.sparse_tensor_to_dense(delta) # where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true * y_pred_ones, axis=0) # for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) # for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) # precision for each class precision = tf.select(K.equal(pred_cnt, 0), K.zeros_like(y_true_pred), y_true_pred / pred_cnt, name='precision_f1_semeval') # recall for each class recall = tf.select(K.equal(gold_cnt, 0), K.zeros_like(y_true_pred), y_true_pred / gold_cnt, name='racall_f1_semeval') # f1 for each class f1_class = tf.select(K.equal(precision + recall, 0), K.zeros_like(y_true_pred), 2 * (precision * recall) / (precision + recall), name='precision_f1_semeval') # return average f1 score over all classes return K.mean(f1_class)
def f1_score_semeval(y_true, y_pred): # convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) # y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 # indices_x = K.arange(start=0, stop=y_true.get_shape()[0]) indices_x = K.expand_dims(K.arange(start=0, stop=tf.shape(y_true, name='get_indicec_x_shape')[0], dtype='int64'), dim=-1) indices_y = K.expand_dims(K.argmax(y_pred, axis=-1), dim=-1) indices = K.concatenate((indices_x, indices_y)) values = K.sum(K.ones_like(indices_x, dtype='float32'), axis=-1) shape = K.cast(tf.shape(y_pred_ones), dtype='int64') delta = tf.SparseTensor(indices, values, shape) y_pred_ones = y_pred_ones + tf.sparse_tensor_to_dense(delta) # where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true * y_pred_ones, axis=0) # for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) # for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) # precision for each class precision = tf.select(K.equal(pred_cnt, 0), K.zeros_like(y_true_pred), y_true_pred / pred_cnt, name='precision_f1_semeval') # recall for each class recall = tf.select(K.equal(gold_cnt, 0), K.zeros_like(y_true_pred), y_true_pred / gold_cnt, name='racall_f1_semeval') # f1 for each class f1_class = tf.select(K.equal(precision + recall, 0), K.zeros_like(y_true_pred), 2 * (precision * recall) / (precision + recall), name='precision_f1_semeval') # return average f1 score over all classes return (f1_class[0] + f1_class[2]) / 2.0
def f1_score_task3(y_true, y_pred): # convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) # y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), K.argmax(y_pred, axis=-1)], 1) indices_x = K.expand_dims(K.arange(start=0, stop=tf.shape(y_true, name='get_indicec_x_shape')[0], dtype='int64'), dim=-1) indices_y = K.expand_dims(K.argmax(y_pred, axis=-1), dim=-1) indices = K.concatenate((indices_x, indices_y)) values = K.sum(K.ones_like(indices_x, dtype='float32'), axis=-1) shape = K.cast(tf.shape(y_pred_ones), dtype='int64') delta = tf.SparseTensor(indices, values, shape) y_pred_ones = y_pred_ones + tf.sparse_tensor_to_dense(delta) # where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true * y_pred_ones, axis=0) # for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) # for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) # precision for each class precision = tf.select(K.equal(pred_cnt, 0), K.zeros_like(y_true_pred), y_true_pred / pred_cnt, name='precision_f1_semeval') # recall for each class recall = tf.select(K.equal(gold_cnt, 0), K.zeros_like(y_true_pred), y_true_pred / gold_cnt, name='racall_f1_semeval') # f1 for each class f1_class = tf.select(K.equal(precision + recall, 0), K.zeros_like(y_true_pred), 2 * (precision * recall) / (precision + recall), name='precision_f1_semeval') # return average f1 score over all classes return f1_class[1]
def f1_score_semeval(y_true, y_pred): #convert probas to 0,1 y_pred_ones = K.zeros_like(y_true) #y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 #indices_x = K.arange(start=0, stop=y_true.get_shape()[0]) indices_x = K.expand_dims(K.arange(start=0, stop=tf.shape(y_true, name='get_indicec_x_shape')[0], dtype='int64'), dim=-1) indices_y = K.expand_dims(K.argmax(y_pred, axis=-1), dim=-1) indices = K.concatenate((indices_x, indices_y)) values = K.sum(K.ones_like(indices_x, dtype='float32'), axis=-1) shape = K.cast(tf.shape(y_pred_ones), dtype='int64') delta = tf.SparseTensor(indices, values, shape) y_pred_ones = y_pred_ones + tf.sparse_tensor_to_dense(delta) #where y_ture=1 and y_pred=1 -> true positive y_true_pred = K.sum(y_true*y_pred_ones, axis=0) #for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) #for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) #precision for each class precision = tf.select(K.equal(pred_cnt, 0), K.zeros_like(y_true_pred), y_true_pred/pred_cnt, name='precision_f1_semeval') #recall for each class recall = tf.select(K.equal(gold_cnt, 0), K.zeros_like(y_true_pred), y_true_pred/gold_cnt, name='racall_f1_semeval') #f1 for each class f1_class = tf.select(K.equal(precision + recall, 0), K.zeros_like(y_true_pred), 2*(precision*recall)/(precision+recall), name='precision_f1_semeval') #return average f1 score over all classes return (f1_class[0] + f1_class[2])/2.0
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 call(self, x, mask=None): # x[0]: (batch_size, input_length, input_dim) # x[1]: (batch_size, 1) indices of prepositions # Optional: x[2]: (batch_size, input_length - 2) assert isinstance(x, list) or isinstance(x, tuple) encoded_sentence = x[0] prep_indices = K.squeeze(x[1], axis=-1) #(batch_size,) batch_indices = K.arange(K.shape(encoded_sentence)[0]) # (batch_size,) if self.with_attachment_probs: # We're essentially doing K.argmax(x[2]) here, but argmax is not differentiable! head_probs = x[2] head_probs_padding = K.zeros_like(x[2])[:, :2] # (batch_size, 2) # (batch_size, input_length) padded_head_probs = K.concatenate([head_probs, head_probs_padding]) # (batch_size, 1) max_head_probs = K.expand_dims(K.max(padded_head_probs, axis=1)) # (batch_size, input_length, 1) max_head_prob_indices = K.expand_dims(K.equal(padded_head_probs, max_head_probs)) # (batch_size, input_length, input_dim) masked_head_encoding = K.switch(max_head_prob_indices, encoded_sentence, K.zeros_like(encoded_sentence)) # (batch_size, input_dim) head_encoding = K.sum(masked_head_encoding, axis=1) else: head_indices = prep_indices - 1 # (batch_size,) head_encoding = encoded_sentence[batch_indices, head_indices, :] # (batch_size, input_dim) prep_encoding = encoded_sentence[batch_indices, prep_indices, :] # (batch_size, input_dim) child_encoding = encoded_sentence[batch_indices, prep_indices+1, :] # (batch_size, input_dim) ''' prep_indices = x[1] sentence_mask = mask[0] if sentence_mask is not None: if K.ndim(sentence_mask) > 2: # This means this layer came after a Bidirectional layer. Keras has this bug which # concatenates input masks instead of output masks. # TODO: Fix Bidirectional instead. sentence_mask = K.any(sentence_mask, axis=(-2, -1)) head_encoding, prep_encoding, child_encoding = self.get_split_averages(encoded_sentence, sentence_mask, prep_indices) ''' head_projection = K.dot(head_encoding, self.proj_head) # (batch_size, proj_dim) prep_projection = K.dot(prep_encoding, self.proj_prep) # (batch_size, proj_dim) child_projection = K.dot(child_encoding, self.proj_child) # (batch_size, proj_dim) #(batch_size, proj_dim) if self.composition_type == 'HPCT': composed_projection = K.tanh(head_projection + prep_projection + child_projection) elif self.composition_type == 'HPC': prep_child_projection = K.tanh(prep_projection + child_projection) # (batch_size, proj_dim) composed_projection = K.tanh(head_projection + prep_child_projection) else: # Composition type in HC composed_projection = K.tanh(head_projection + child_projection) for hidden_layer in self.hidden_layers: composed_projection = K.tanh(K.dot(composed_projection, hidden_layer)) # (batch_size, proj_dim) # (batch_size, num_classes) class_scores = K.dot(composed_projection, self.scorer) label_probabilities = K.softmax(class_scores) return label_probabilities
def to_configs(states,verbose=True, **kwargs): tower_height = states.shape[1] disks = tower_height // disk_height tower_width = disks * (2*disk_inc) + base_disk_width + border towers = states.shape[2] // tower_width panels = get_panels(disks, tower_width) def build(): states = Input(shape=(tower_height, tower_width*towers)) error = build_error(states, disks, towers, tower_width, panels) matches = 1 - K.clip(K.sign(error - threshold),0,1) # assume disks=4, towers=3 # matches: a h w p # [[[00001][00001][00001]] --- all panel 4 (empty panel) # [[10000][00001][00001]] --- h,w=1,0 is panel 0, others are panel 4 (empty panel) # [[01000][00001][00001]] --- h,w=2,0 is panel 1, others are panel 4 (empty panel) # [[00010][00100][00001]]] --- h,w=3,0 is panel 3, h,w=3,1 is panel 2, h,w=3,2 is panel 4 # # target config is [0,0,1,0] # you don't need the last panel (empty panel) # a h w p # [[[0000][0000][0000]] --- all panel 4 (empty panel) # [[1000][0000][0000]] --- h,w=1,0 is panel 0, others are panel 4 (empty panel) # [[0100][0000][0000]] --- h,w=2,0 is panel 1, others are panel 4 (empty panel) # [[0001][0010][0000]]] --- h,w=3,0 is panel 3, h,w=3,1 is panel 2, h,w=3,2 is panel 4 config = matches[:, :, :, 0:-1] # you don't need the height info # a w p # [[1101][0010][0000]] config = K.sum(config, 1) # reorder to a p w # [[100][100][010][100]] config = K.permute_dimensions(config, [0,2,1]) # convert one-hot width into width position config = config * K.arange(0,towers,dtype='float32') # 1-4 # [[000][000][010][000]] config = K.sum(config, -1) # [0 0 1 0] config = K.cast(config, 'int32') return Model(states, wrap(states, config)) return build().predict(states, **kwargs)
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])]
def accumulate(attend_function, inputs, input_length, mask=None, return_probabilities=False): '''get the running attention over a sequence. given a 3dim tensor where the 1st dim is time (or not. whatever.), calculating the running attended sum. in other words, at the first time step, you only have that item. at the second time step, attend over the first two items. at the third.. the third. so on. this basically a mod on keras' rnn implementation author: bcm ''' ndim = inputs.ndim assert ndim >= 3, 'inputs should be at least 3d' axes = [1,0] + list(range(2, ndim)) inputs = inputs.dimshuffle(axes) indices = list(range(input_length)) successive_outputs = [] if mask is not None: if mask.ndim == ndim-1: mask = K.expand_dims(mask) assert mask.ndim == ndim mask = mask.dimshuffle(axes) prev_output = None successive_outputs = [] successive_pvecs = [] uncover_mask = K.zeros_like(inputs) uncover_indices = K.arange(input_length) for _ in range(ndim-1): uncover_indices = K.expand_dims(uncover_indices) make_subset = lambda i,X: K.switch(uncover_indices <= i, X, uncover_mask) for i in indices: inputs_i = make_subset(i,inputs) mask_i = make_subset(i,mask) if mask is not None: output = attend_function(inputs_i, mask_i) # this should not output the time dimension; it should be marginalized over. else: output = attend_function(inputs_i) # this should not output the time dimension; it should be marginalized over. if return_probabilities: output, p_vectors = output successive_pvecs.append(p_vectors) assert output.ndim == 2, "Your attention function is malfunctioning; the attention accumulator should return 2 dimensional tensors" successive_outputs.append(output) outputs = K.pack(successive_outputs) K.squeeze(outputs, -1) axes = [1, 0] + list(range(2, outputs.ndim)) outputs = outputs.dimshuffle(axes) if return_probabilities: out_pvecs = K.pack(successive_pvecs) K.squeeze(out_pvecs, -1) out_pvecs = out_pvecs.dimshuffle(axes) outputs = [outputs, out_pvecs] return outputs
def yolo_head(feats, anchors, num_classes): """Convert final layer features to bounding box parameters. Parameters ---------- feats : tensor Final convolutional layer features. anchors : array-like Anchor box widths and heights. num_classes : int Number of target classes. Returns ------- box_xy : tensor x, y box predictions adjusted by spatial location in conv layer. box_wh : tensor w, h box predictions adjusted by anchors and conv spatial resolution. box_conf : tensor Probability estimate for whether each box contains any object. box_class_pred : tensor Probability distribution estimate for each box over class labels. """ num_anchors = len(anchors) # Reshape to batch, height, width, num_anchors, box_params. anchors_tensor = K.reshape(K.variable(anchors), [1, 1, 1, num_anchors, 2]) # Dynamic implementation of conv dims for fully convolutional model. conv_dims = K.shape(feats)[1:3] # assuming channels last # In YOLO the height index is the inner most iteration. conv_height_index = K.arange(0, stop=conv_dims[0]) conv_width_index = K.arange(0, stop=conv_dims[1]) conv_height_index = K.tile(conv_height_index, [conv_dims[1]]) conv_width_index = K.tile(K.expand_dims(conv_width_index, 0), [conv_dims[0], 1]) conv_width_index = K.flatten(K.transpose(conv_width_index)) conv_index = K.transpose(K.stack([conv_height_index, conv_width_index])) conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2]) conv_index = K.cast(conv_index, K.dtype(feats)) feats = K.reshape(feats, [-1, conv_dims[0], conv_dims[1], num_anchors, num_classes + 5]) conv_dims = K.cast(K.reshape(conv_dims, [1, 1, 1, 1, 2]), K.dtype(feats)) box_xy = K.sigmoid(feats[..., :2]) box_wh = K.exp(feats[..., 2:4]) box_confidence = K.sigmoid(feats[..., 4:5]) box_class_probs = K.softmax(feats[..., 5:]) # Adjust preditions to each spatial grid point and anchor size. # Note: YOLO iterates over height index before width index. box_xy = (box_xy + conv_index) / conv_dims box_wh = box_wh * anchors_tensor / conv_dims return box_xy, box_wh, box_confidence, box_class_probs