我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.backend.ones_like()。
def get_constants(self, inputs, training=None): constants = self.recurrent_layer.get_constants( inputs=inputs, training=training ) if 0 < self.dense_dropout < 1: ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.recurrent_layer.units)) def dropped_inputs(): return K.dropout(ones, self.dense_dropout) out_dp_mask = [K.in_train_phase(dropped_inputs, ones, training=training)] constants.append(out_dp_mask) else: constants.append([K.cast_to_floatx(1.)]) return constants
def call(self, x, mask=None): mean = super(IntraAttention, self).call(x, mask) # x: (batch_size, input_length, input_dim) # mean: (batch_size, input_dim) ones = K.expand_dims(K.mean(K.ones_like(x), axis=(0, 2)), dim=0) # (1, input_length) # (batch_size, input_length, input_dim) tiled_mean = K.permute_dimensions(K.dot(K.expand_dims(mean), ones), (0, 2, 1)) if mask is not None: if K.ndim(mask) > K.ndim(x): # Assuming this is because of the bug in Bidirectional. Temporary fix follows. # TODO: Fix Bidirectional. mask = K.any(mask, axis=(-2, -1)) if K.ndim(mask) < K.ndim(x): mask = K.expand_dims(mask) x = switch(mask, x, K.zeros_like(x)) # (batch_size, input_length, proj_dim) projected_combination = K.tanh(K.dot(x, self.vector_projector) + K.dot(tiled_mean, self.mean_projector)) scores = K.dot(projected_combination, self.scorer) # (batch_size, input_length) weights = K.softmax(scores) # (batch_size, input_length) attended_x = K.sum(K.expand_dims(weights) * x, axis=1) # (batch_size, input_dim) return attended_x
def get_constants(self, inputs, training=None): constants = [] '''if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.units)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(3)]) if 0 < self.dropout_W < 1: input_shape = K.int_shape(x) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(3)] constants.append(B_W) else:''' constants.append([K.cast_to_floatx(1.) for _ in range(3)]) return constants
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.output_dim)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(3)]) if 0 < self.dropout_W < 1: input_shape = K.int_shape(x) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(3)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(3)]) return constants
def _ternarize(W, H=1): '''The weights' ternarization function, # References: - [Recurrent Neural Networks with Limited Numerical Precision](http://arxiv.org/abs/1608.06902) - [Ternary Weight Networks](http://arxiv.org/abs/1605.04711) ''' W /= H ones = K.ones_like(W) zeros = K.zeros_like(W) Wt = switch(W > 0.5, ones, switch(W <= -0.5, -ones, zeros)) Wt *= H return Wt
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.output_dim)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) if 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) return constants
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.output_dim)) B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones) constants.append(B_U) else: constants.append(K.cast_to_floatx(1.)) if self.consume_less == 'cpu' and 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, input_dim)) B_W = K.in_train_phase(K.dropout(ones, self.dropout_W), ones) constants.append(B_W) else: constants.append(K.cast_to_floatx(1.)) return constants
def contingency_table(y, z): """Compute contingency table.""" y = K.round(y) z = K.round(z) def count_matches(a, b): tmp = K.concatenate([a, b]) return K.sum(K.cast(K.all(tmp, -1), K.floatx())) ones = K.ones_like(y) zeros = K.zeros_like(y) y_ones = K.equal(y, ones) y_zeros = K.equal(y, zeros) z_ones = K.equal(z, ones) z_zeros = K.equal(z, zeros) tp = count_matches(y_ones, z_ones) tn = count_matches(y_zeros, z_zeros) fp = count_matches(y_zeros, z_ones) fn = count_matches(y_ones, z_zeros) return (tp, tn, fp, fn)
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.concatenate([ones] * self.output_dim, 1) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) if 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.concatenate([ones] * input_dim, 1) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) return constants
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.output_dim)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(3)]) if 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, input_dim)) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(3)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(3)]) return constants
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.output_dim)) B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones) constants.append(B_U) else: constants.append(K.cast_to_floatx(1.0)) if self.consume_less == 'cpu' and 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, input_dim)) B_W = K.in_train_phase(K.dropout(ones, self.dropout_W), ones) constants.append(B_W) else: constants.append(K.cast_to_floatx(1.0)) return constants
def yoloconfidloss(y_true, y_pred, t): real_y_true = tf.select(t, y_true, K.zeros_like(y_true)) pobj = K.sigmoid(y_pred) lo = K.square(real_y_true-pobj) value_if_true = lamda_confid_obj*(lo) value_if_false = lamda_confid_noobj*(lo) loss1 = tf.select(t, value_if_true, value_if_false) loss = K.mean(loss1) # noobj = tf.select(t, K.zeros_like(y_pred), pobj) noobjcount = tf.select(t, K.zeros_like(y_pred), K.ones_like(y_pred)) ave_anyobj = K.sum(noobj) / K.sum(noobjcount) #ave_anyobj = K.mean(pobj) obj = tf.select(t, pobj, K.zeros_like(y_pred)) objcount = tf.select(t, K.ones_like(y_pred), K.zeros_like(y_pred)) #ave_obj = K.mean( K.sum(obj, axis=1) / (K.sum(objcount, axis=1)+0.000001) ) # prevent div 0 ave_obj = K.sum(obj) / (K.sum(objcount)+0.000001) # prevent div 0 return loss, ave_anyobj, ave_obj # shape is (gridcells*2,)
def weighted_dice_loss(y_true, y_pred): y_true = K.cast(y_true, 'float32') y_pred = K.cast(y_pred, 'float32') # if we want to get same size of output, kernel size must be odd number if K.int_shape(y_pred)[1] == 128: kernel_size = 11 elif K.int_shape(y_pred)[1] == 256: kernel_size = 21 elif K.int_shape(y_pred)[1] == 512: kernel_size = 21 elif K.int_shape(y_pred)[1] == 1024: kernel_size = 41 else: raise ValueError('Unexpected image size') averaged_mask = K.pool2d( y_true, pool_size=(kernel_size, kernel_size), strides=(1, 1), padding='same', pool_mode='avg') border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(K.less(averaged_mask, 0.995), 'float32') weight = K.ones_like(averaged_mask) w0 = K.sum(weight) weight += border * 2 w1 = K.sum(weight) weight *= (w0 / w1) loss = 1 - weighted_dice_coeff(y_true, y_pred, weight) return loss
def weighted_bce_dice_loss(y_true, y_pred): y_true = K.cast(y_true, 'float32') y_pred = K.cast(y_pred, 'float32') # if we want to get same size of output, kernel size must be odd number if K.int_shape(y_pred)[1] == 128: kernel_size = 11 elif K.int_shape(y_pred)[1] == 256: kernel_size = 21 elif K.int_shape(y_pred)[1] == 512: kernel_size = 21 elif K.int_shape(y_pred)[1] == 1024: kernel_size = 41 else: raise ValueError('Unexpected image size') averaged_mask = K.pool2d( y_true, pool_size=(kernel_size, kernel_size), strides=(1, 1), padding='same', pool_mode='avg') border = K.cast(K.greater(averaged_mask, 0.005), 'float32') * K.cast(K.less(averaged_mask, 0.995), 'float32') weight = K.ones_like(averaged_mask) w0 = K.sum(weight) weight += border * 2 w1 = K.sum(weight) weight *= (w0 / w1) loss = weighted_bce_loss(y_true, y_pred, weight) + (1 - weighted_dice_coeff(y_true, y_pred, weight)) return loss
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.output_dim)) B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones) constants.append(B_U) else: constants.append(K.cast_to_floatx(1.)) if self.consume_less == 'cpu' and 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = K.in_train_phase(K.dropout(ones, self.dropout_W), ones) constants.append(B_W) else: constants.append(K.cast_to_floatx(1.)) return constants
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.concatenate([ones] * self.output_dim, 1) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(3)]) if 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.concatenate([ones] * input_dim, 1) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(3)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(3)]) return constants
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.output_dim)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(2)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(2)]) if 0 < self.dropout_W < 1: input_shape = K.int_shape(x) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(2)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(2)]) return constants
def call(self, inputs, mask=None): assert(isinstance(inputs, list) and len(inputs) == 5) uQ, WQ_u, WQ_v, v, VQ_r = inputs uQ_mask = mask[0] if mask is not None else None ones = K.ones_like(K.sum(uQ, axis=1, keepdims=True)) # (B, 1, 2H) s_hat = K.dot(uQ, WQ_u) s_hat += K.dot(ones, K.dot(WQ_v, VQ_r)) s_hat = K.tanh(s_hat) s = K.dot(s_hat, v) s = K.batch_flatten(s) a = softmax(s, mask=uQ_mask, axis=1) rQ = K.batch_dot(uQ, a, axes=[1, 1]) return rQ
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.hidden_recurrent_dim)) B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones) constants.append(B_U) else: constants.append(K.cast_to_floatx(1.)) if self.consume_less == 'cpu' and 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, input_dim)) B_W = K.in_train_phase(K.dropout(ones, self.dropout_W), ones) constants.append(B_W) else: constants.append(K.cast_to_floatx(1.)) return constants
def get_constants(self, x): constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.input_dim)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) if 0 < self.dropout_W < 1: input_shape = K.int_shape(x) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) return constants
def discriminator_loss(y_true,y_pred): BATCH_SIZE=10 return K.mean(K.binary_crossentropy(K.flatten(y_pred), K.concatenate([K.ones_like(K.flatten(y_pred[:BATCH_SIZE,:,:,:])),K.zeros_like(K.flatten(y_pred[:BATCH_SIZE,:,:,:])) ]) ), axis=-1)
def discriminator_on_generator_loss(y_true,y_pred): BATCH_SIZE=10 return K.mean(K.binary_crossentropy(K.flatten(y_pred), K.ones_like(K.flatten(y_pred))), axis=-1)
def complementary_mask(x): return K.ones_like(x) - x
def fill_background_mask(x): tensor, mask = x rep = K.int_shape(tensor)[4] - 1 full_mask = K.ones_like(mask) - mask K.repeat_elements(mask, rep, axis=4) return tensor + full_mask
def dice_whole_mod(y_true, y_pred): """ Computes the Sorensen-Dice metric, where P come from class 1,2,3,4,0 TP Dice = 2 ------- T + P Parameters ---------- y_true : keras.placeholder Placeholder that contains the ground truth labels of the classes y_pred : keras.placeholder Placeholder that contains the class prediction Returns ------- scalar Dice metric """ # mask = K.expand_dims(K.sum(y_true,axis=4),axis=4) # cmp_mask = K.concatenate([K.ones_like(mask) - mask,K.zeros_like(mask), K.zeros_like(mask)],axis=4) # y_pred = y_pred + cmp_mask y_true = y_true[:,:,:,:,:3] y_pred_decision = tf.floor((y_pred + K.epsilon()) / K.max(y_pred, axis=4, keepdims=True)) mask_true = K.sum(y_true, axis=4) mask_pred = K.sum(y_pred_decision, axis=4) * K.sum(y_true, axis=4) y_sum = K.sum(mask_true * mask_pred) return (2. * y_sum + K.epsilon()) / (K.sum(mask_true) + K.sum(mask_pred) + K.epsilon())
def compute_mask(self, inputs, mask): output_mask = self.layer.compute_mask( inputs=inputs, mask=mask, ) if self.time_steps is None: return output_mask else: output_mask = K.ones_like(output_mask) output_mask = K.any(output_mask, axis=1, keepdims=True) return K.tile(output_mask, [1, self.time_steps])
def time_distributed_dense(x, w, b=None, dropout=None, input_dim=None, units=None, timesteps=None): """Apply `y . w + b` for every temporal slice y of x. # Arguments x: input tensor. w: weight matrix. b: optional bias vector. dropout: wether to apply dropout (same dropout mask for every temporal slice of the input). input_dim: integer; optional dimensionality of the input. units: integer; optional dimensionality of the output. timesteps: integer; optional number of timesteps. # Returns Output tensor. """ if not input_dim: input_dim = K.shape(x)[2] if not timesteps: timesteps = K.shape(x)[1] if not units: units = K.shape(w)[1] if dropout is not None and 0. < dropout < 1.: # apply the same dropout pattern at every timestep ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim))) dropout_matrix = K.dropout(ones, dropout) expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps) x = K.in_train_phase(x * expanded_dropout_matrix, x) # collapse time dimension and batch dimension together x = K.reshape(x, (-1, input_dim)) x = K.dot(x, w) if b: x += b # reshape to 3D tensor if K.backend() == 'tensorflow': x = K.reshape(x, K.stack([-1, timesteps, units])) x.set_shape([None, None, units]) else: x = K.reshape(x, (-1, timesteps, units)) return x
def get_constants(self, inputs, training=None): constants = [] if 0 < self.dropout < 1: input_shape = K.int_shape(inputs) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) def dropped_inputs(): return K.dropout(ones, self.dropout) dp_mask = K.in_train_phase(dropped_inputs, ones, training=training) constants.append(dp_mask) else: constants.append(K.cast_to_floatx(1.)) if 0 < self.recurrent_dropout < 1: ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.units)) def dropped_inputs(): return K.dropout(ones, self.recurrent_dropout) rec_dp_mask = K.in_train_phase(dropped_inputs, ones, training=training) constants.append(rec_dp_mask) else: constants.append(K.cast_to_floatx(1.)) return constants # Aliases
def bp_mll_loss(y_true, y_pred): # get true and false labels y_i = K.equal(y_true, K.ones_like(y_true)) y_i_bar = K.not_equal(y_true, K.ones_like(y_true)) # cast to float as keras backend has no logical and y_i = K.cast(y_i, dtype='float32') y_i_bar = K.cast(y_i_bar, dtype='float32') # get indices to check truth_matrix = pairwise_and(y_i, y_i_bar) # calculate all exp'd differences sub_matrix = pairwise_sub(y_pred, y_pred) exp_matrix = K.exp(-sub_matrix) # check which differences to consider and sum them sparse_matrix = exp_matrix * truth_matrix sums = K.sum(sparse_matrix, axis=[1,2]) # get normalizing terms and apply them y_i_sizes = K.sum(y_i, axis=1) y_i_bar_sizes = K.sum(y_i_bar, axis=1) normalizers = y_i_sizes * y_i_bar_sizes results = sums / normalizers # sum over samples return K.sum(results) # compute pairwise differences between elements of the tensors a and b
def get_constants(self, inputs, training=None): constants = [] if self.implementation != 0 and 0 < self.dropout < 1: input_shape = K.int_shape(inputs) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) def dropped_inputs(): return K.dropout(ones, self.dropout) dp_mask = [K.in_train_phase(dropped_inputs, ones, training=training) for _ in range(5)] constants.append(dp_mask) else: constants.append([K.cast_to_floatx(1.) for _ in range(5)]) if 0 < self.recurrent_dropout < 1: ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.units)) def dropped_inputs(): return K.dropout(ones, self.recurrent_dropout) rec_dp_mask = [K.in_train_phase(dropped_inputs, ones, training=training) for _ in range(5)] constants.append(rec_dp_mask) else: constants.append([K.cast_to_floatx(1.) for _ in range(5)]) return constants
def call(self, inputs, output_shape=None): """ Seen on https://github.com/tensorflow/tensorflow/issues/2169 Replace with unpool op when/if issue merged Add theano backend """ updates, mask = inputs[0], inputs[1] with K.tf.variable_scope(self.name): mask = K.cast(mask, 'int32') input_shape = K.tf.shape(updates, out_type='int32') # calculation new shape if output_shape is None: output_shape = (input_shape[0], input_shape[1] * self.size[0], input_shape[2] * self.size[1], input_shape[3]) self.output_shape1 = output_shape # calculation indices for batch, height, width and feature maps one_like_mask = K.ones_like(mask, dtype='int32') batch_shape = K.concatenate([[input_shape[0]], [1], [1], [1]], axis=0) batch_range = K.reshape(K.tf.range(output_shape[0], dtype='int32'), shape=batch_shape) b = one_like_mask * batch_range y = mask // (output_shape[2] * output_shape[3]) x = (mask // output_shape[3]) % output_shape[2] feature_range = K.tf.range(output_shape[3], dtype='int32') f = one_like_mask * feature_range # transpose indices & reshape update values to one dimension updates_size = K.tf.size(updates) indices = K.transpose(K.reshape(K.stack([b, y, x, f]), [4, updates_size])) values = K.reshape(updates, [updates_size]) ret = K.tf.scatter_nd(indices, values, output_shape) return ret
def get_constants(self, x): print("begin get_constants(self, x)") constants = [] if 0 < self.dropout_U < 1: ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.controller_output_dim)) B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)] constants.append(B_U) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) if 0 < self.dropout_W < 1: input_shape = self.input_spec[0].shape input_dim = input_shape[-1] ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)] constants.append(B_W) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) # if 0 < self.dropout_R < 1: # input_shape = self.input_spec[0].shape # input_dim = input_shape[-1] # ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1))) # ones = K.tile(ones, (1, int(input_dim))) # B_R = [K.in_train_phase(K.dropout(ones, self.dropout_R), ones) for _ in range(4)] # constants.append(B_R) # else: # constants.append([K.cast_to_floatx(1.) for _ in range(4)]) print("end get_constants(self, x)") return constants
def discriminator_loss(discrim_output_prior, discrim_output_posterior, from_logits=False): if from_logits: discrim_output_posterior = ker.sigmoid(discrim_output_posterior) discrim_output_prior = ker.sigmoid(discrim_output_prior) # The dicriminator loss is the GAN loss with input from the prior and posterior distributions discriminator_loss = ker.mean(binary_crossentropy(y_pred=discrim_output_posterior, y_true=ker.ones_like(discrim_output_posterior)) + binary_crossentropy(y_pred=discrim_output_prior, y_true=ker.zeros_like(discrim_output_prior))) return discriminator_loss
def _sample_weights(y, mask=None): """Compute sample weights.""" if mask is None: weights = K.ones_like(y) else: weights = 1 - K.cast(K.equal(y, mask), K.floatx()) return weights
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.arange(y_true.shape[0]) indices_y = K.argmax(y_pred, axis=-1) indices = K.concatenate(indices_x, indices_y) values = K.ones_like(indices_x) shape = y_pred_ones.shape delta = tf.SparseTensor(indices, values, shape) y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1 # 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 = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt) # recall for each class recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred / gold_cnt) # f1 for each class f1_class = K.switch(K.equal(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall)) # return average f1 score over all classes return f1_class[1]
def limit(x): y = tf.select(K.greater(x,100000), 1000000.*K.ones_like(x), x) z = tf.select(K.lesser(y,-100000), -1000000.*K.ones_like(x), y) return z
def yoloconfidloss(y_true, y_pred, t): pobj = K.sigmoid(y_pred) lo = K.square(y_true-pobj) value_if_true = lamda_confid_obj*(lo) value_if_false = lamda_confid_noobj*(lo) loss1 = tf.select(t, value_if_true, value_if_false) loss = K.mean(loss1) #,axis=0) # ave_anyobj = K.mean(pobj) obj = tf.select(t, pobj, K.zeros_like(y_pred)) objcount = tf.select(t, K.ones_like(y_pred), K.zeros_like(y_pred)) ave_obj = K.mean( K.sum(obj, axis=1) / (K.sum(objcount, axis=1)+0.000001) ) # prevent div 0 return loss, ave_anyobj, ave_obj # shape is (gridcells*2,)
def iou(x_true,y_true,w_true,h_true,x_pred,y_pred,w_pred,h_pred,t): xoffset = K.cast_to_floatx((np.tile(np.arange(side),side))) yoffset = K.cast_to_floatx((np.repeat(np.arange(side),side))) x = tf.select(t, K.sigmoid(x_pred), K.zeros_like(x_pred)) y = tf.select(t, K.sigmoid(y_pred), K.zeros_like(y_pred)) w = tf.select(t, K.sigmoid(w_pred), K.zeros_like(w_pred)) h = tf.select(t, K.sigmoid(h_pred), K.zeros_like(h_pred)) ow = overlap(x+xoffset, w*side, x_true+xoffset, w_true*side) oh = overlap(y+yoffset, h*side, y_true+yoffset, h_true*side) ow = tf.select(K.greater(ow,0), ow, K.zeros_like(ow)) oh = tf.select(K.greater(oh,0), oh, K.zeros_like(oh)) intersection = ow*oh union = w*h*(side**2) + w_true*h_true*(side**2) - intersection + K.epsilon() # prevent div 0 # recall_iou = intersection / union recall_t = K.greater(recall_iou, 0.5) recall_count = K.sum(tf.select(recall_t, K.ones_like(recall_iou), K.zeros_like(recall_iou))) # iou = K.sum(intersection / union, axis=1) obj_count = K.sum(tf.select(t, K.ones_like(x_true), K.zeros_like(x_true)) ) ave_iou = K.sum(iou) / (obj_count) recall = recall_count / (obj_count) return ave_iou, recall, obj_count, intersection, union,ow,oh,x,y,w,h # shape is (gridcells*(5+classes), )
def get_constants(self, inputs, training=None): constants = [] if self.implementation == 0 and 0 < self.dropout < 1: input_shape = K.int_shape(inputs) input_dim = input_shape[-1] ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, int(input_dim))) def dropped_inputs(): return K.dropout(ones, self.dropout) dp_mask = [K.in_train_phase(dropped_inputs, ones, training=training) for _ in range(4)] constants.append(dp_mask) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) if 0 < self.recurrent_dropout < 1: ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1))) ones = K.tile(ones, (1, self.units)) def dropped_inputs(): return K.dropout(ones, self.recurrent_dropout) rec_dp_mask = [K.in_train_phase(dropped_inputs, ones, training=training) for _ in range(4)] constants.append(rec_dp_mask) else: constants.append([K.cast_to_floatx(1.) for _ in range(4)]) return constants
def compute_loss(self, y_true, y_pred): class_loss = self.cross_entropy(y_true[:, :, 4:], y_pred[:, :, 4:]) """ class_loss = K.categorical_crossentropy(y_true[:, :, 4:], y_pred[:, :, 4:]) """ # return K.concatenate([class_loss, class_loss_old], axis=0) local_loss = self.smooth_l1(y_true[:, :, :4], y_pred[:, :, :4]) negative_mask = y_true[:, :, 4 + self.background_id] positive_mask = 1 - negative_mask # calculating the positive loss positive_local_losses = local_loss * positive_mask positive_class_losses = class_loss * positive_mask positive_class_loss = K.sum(positive_class_losses, axis=-1) positive_local_loss = K.sum(positive_local_losses, axis=-1) # obtaining the number of negatives in the batch num_positives_per_sample = K.cast(K.sum(positive_mask, -1), 'int32') num_negatives_per_sample = K.cast(K.sum(negative_mask, -1), 'int32') num_negatives_in_batch = K.sum(num_negatives_per_sample) num_hard_negatives = self.neg_pos_ratio * num_positives_per_sample num_negatives = K.minimum(num_hard_negatives, num_negatives_in_batch) all_negative_class_losses = class_loss * negative_mask negative_class_loss = [] for batch_arg in range(self.batch_size): sample_num_negatives = num_negatives[batch_arg] all_negative_sample_loss = all_negative_class_losses[batch_arg] negative_sample_losses = tf.nn.top_k(all_negative_sample_loss, k=sample_num_negatives, sorted=True)[0] negative_sample_loss = K.sum(negative_sample_losses) negative_sample_loss = K.expand_dims(negative_sample_loss, -1) negative_class_loss.append(negative_sample_loss) negative_class_loss = K.concatenate(negative_class_loss) return negative_class_loss class_loss = positive_class_loss + negative_class_loss total_loss = class_loss + (self.alpha * positive_local_loss) batch_mask = K.not_equal(num_positives_per_sample, 0) total_loss = tf.where(batch_mask, total_loss, K.zeros_like(total_loss)) num_positives_per_sample = tf.where( batch_mask, num_positives_per_sample, K.ones_like(num_positives_per_sample)) num_positives_per_sample = K.cast(num_positives_per_sample, 'float32') total_loss = total_loss / num_positives_per_sample return total_loss
def attributes_update(self, attributes, depth, graph, original_graph, bonds): '''Given the current attributes, the current depth, and the graph that the attributes are based on, this function will update the 2D attributes tensor''' ############# GET NEW ATTRIBUTE MATRIX ######################### # New pre-activated attribute matrix v = M_i,j,: x ones((N_atom, 1)) -> (N_atom, N_features) # as long as dimensions are appropriately shuffled shuffled_graph = graph.copy().dimshuffle((2, 0, 1)) # (N_feature x N_atom x N_atom) shuffled_graph.name = 'shuffled_graph' ones_vec = K.ones_like(attributes[:, 0]) # (N_atom x 1) ones_vec.name = 'ones_vec' (new_preactivated_attributes, updates) = theano.scan(lambda x: K.dot(x, ones_vec), sequences = shuffled_graph) # (N_features x N_atom) # Need to pass through an activation function still # Final attribute = bond flag = is not part of W_inner or b_inner (new_attributes, updates) = theano.scan(lambda x: self.activation_inner( K.dot(x, self.W_inner[depth, :, :]) + self.b_inner[depth, 0, :]), sequences = new_preactivated_attributes[:-1, :].T) # (N_atom x N_features -1) # Append last feature (bond flag) after the loop new_attributes = K.concatenate((new_attributes, attributes[:, -1:]), axis = 1) new_attributes.name = 'new_attributes' ############ UPDATE GRAPH TENSOR WITH NEW ATOM ATTRIBUTES ################### ### Node attribute contribution is located in every entry of graph[i,j,:] where ### there is a bond @ ij or when i = j (self) # Get atoms matrix (identity) atoms = T.identity_like(bonds) # (N_atom x N_atom) atoms.name = 'atoms_identity' # Combine bonds_or_atoms = bonds + atoms # (N_atom x N_atom) bonds_or_atoms.name = 'bonds_or_atoms' atom_indeces = T.arange(ones_vec.shape[0]) # 0 to N_atoms - 1 (indeces) atom_indeces.name = 'atom_indeces vector' ### Subtract previous node attribute contribution # Multiply each entry in bonds_or_atoms by the previous atom features for that column (old_features_to_sub, updates) = theano.scan(lambda i: T.outer(bonds_or_atoms[:, i], attributes[i, :]), sequences = T.arange(ones_vec.shape[0])) old_features_to_sub.name = 'old_features_to_sub' ### Add new node attribute contribution # Multiply each entry in bonds_or_atoms by the previous atom features for that column (new_features_to_add, updates) = theano.scan(lambda i: T.outer(bonds_or_atoms[:, i], new_attributes[i, :]), sequences = T.arange(ones_vec.shape[0])) new_features_to_add.name = 'new_features_to_add' # Update new graph new_graph = graph - old_features_to_sub + new_features_to_add new_graph.name = 'new_graph' return (new_attributes, new_graph)
def attributes_update(self, attributes, depth, graph, original_graph, bonds): '''Given the current attributes, the current depth, and the graph that the attributes are based on, this function will update the 2D attributes tensor''' ############# GET NEW ATTRIBUTE MATRIX ######################### # New pre-activated attribute matrix v = M_i,j,: x ones((N_atom, 1)) -> (N_atom, N_features) # as long as dimensions are appropriately shuffled shuffled_graph = graph.copy().dimshuffle((2, 0, 1)) # (N_feature x N_atom x N_atom) shuffled_graph.name = 'shuffled_graph' ones_vec = K.ones_like(attributes[:, 0]) # (N_atom x 1) ones_vec.name = 'ones_vec' # Embed individually # (scan sequences iterates over the FIRST dimension) # (flatten(ndim) keeps the first ndim-1 dimensions the same, then expands the rest to fill) flattened_graph = shuffled_graph.flatten(ndim = 2).T # (N_atom^2 x N_feature) # Embed each possible atom-atom interaction (new_presummed_attributes_flat, updates) = theano.scan(lambda x: self.activation_inner( K.dot(x[:-1], self.W_inner[depth, :, :]) + self.b_inner[depth, 0, :]), sequences = flattened_graph) # still (N_atom^2 x N_feature) # Reshape into #(N_feature-1 x N_atom x N_atom) new_presummed_attributes = new_presummed_attributes_flat.T.reshape(shuffled_graph[:-1,:,:].shape) # Now sum activated self+neighbors (new_attributes, updates) = theano.scan(lambda x: K.dot(x, ones_vec), sequences = new_presummed_attributes) # (N_features x N_atom) # Append last feature (bond flag) after the loop new_attributes = K.concatenate((new_attributes.T, attributes[:, -1:]), axis = 1) new_attributes.name = 'new_attributes' ############ UPDATE GRAPH TENSOR WITH NEW ATOM ATTRIBUTES ################### ### Node attribute contribution is located in every entry of graph[i,j,:] where ### there is a bond @ ij or when i = j (self) # Get atoms matrix (identity) atoms = T.identity_like(bonds) # (N_atom x N_atom) atoms.name = 'atoms_identity' # Combine bonds_or_atoms = bonds + atoms # (N_atom x N_atom) bonds_or_atoms.name = 'bonds_or_atoms' atom_indeces = T.arange(ones_vec.shape[0]) # 0 to N_atoms - 1 (indeces) atom_indeces.name = 'atom_indeces vector' ### Subtract previous node attribute contribution # Multiply each entry in bonds_or_atoms by the previous atom features for that column (old_features_to_sub, updates) = theano.scan(lambda i: T.outer(bonds_or_atoms[:, i], attributes[i, :]), sequences = T.arange(ones_vec.shape[0])) old_features_to_sub.name = 'old_features_to_sub' ### Add new node attribute contribution # Multiply each entry in bonds_or_atoms by the previous atom features for that column (new_features_to_add, updates) = theano.scan(lambda i: T.outer(bonds_or_atoms[:, i], new_attributes[i, :]), sequences = T.arange(ones_vec.shape[0])) new_features_to_add.name = 'new_features_to_add' # Update new graph new_graph = graph - old_features_to_sub + new_features_to_add new_graph.name = 'new_graph' return (new_attributes, new_graph)