def call(self, inputs, reverse=False, ddi=False, **kwargs): logscale_factor = 3. x = inputs reduce_axis = list(range(K.ndim(inputs)))[:-1] if not reverse: log_scale = self.log_scale bias = self.bias if ddi: x_var = tf.reduce_mean(x**2, reduce_axis, keepdims=True) init_scale = tf.log(1. / (tf.sqrt(x_var) + 1e-6)) / logscale_factor init_bias = tf.reduce_mean(x, reduce_axis, keepdims=True) log_scale = K.switch(K.all(K.equal(self.log_scale, 0.)), init_scale, self.log_scale) bias = K.switch(K.all(K.equal(self.bias, 0.)), -init_bias, self.bias) self.add_update(K.update_add( self.log_scale, K.switch(K.all(K.equal(self.log_scale, 0.)), init_scale, K.zeros_like(init_scale))), inputs=x) self.add_update(K.update_add( self.bias, K.switch(K.all(K.equal(self.bias, 0.)), -init_bias, K.zeros_like(init_bias))), inputs=x) return (x + bias) * K.exp(log_scale) else: return x / K.exp(self.log_scale) - self.bias
def get_initial_state(self, inputs): initial_states = [] first = True if self._stackedcells: for cell in self.cell.cells: shape = list(cell.kernel_shape) shape[-1] = cell.filters if first: # Make m, h, c states initial_state = K.zeros_like(inputs) initial_state = K.sum(initial_state, axis=1) initial_state = cell.input_conv(initial_state, K.zeros(tuple(shape)), padding=cell.padding) initial_states += [initial_state for _ in range(3)] first = False else: # if not first make h, c states initial_state = K.zeros_like(initial_state) initial_state = cell.input_conv(initial_state, K.zeros(tuple(shape)), padding=cell.padding) initial_states += [initial_state for _ in range(2)] else: # Single cell shape = list(self.cell.kernel_shape) shape[-1] = self.cell.filters initial_state = K.zeros_like(inputs) initial_state = self.cell.inputs_conv(initial_state, K.zeros(tuple(shape)), padding=self.cell.padding) initial_states += [initial_state for _ in range(3)] return initial_states
def _moments(self, inputs, reduction_axes, keep_dims): mean, variance = nn.moments(inputs, reduction_axes, keep_dims=keep_dims) # TODO(b/129279393): Support zero batch input in non DistributionStrategy # code as well. if self._support_zero_size_input(): inputs_size = array_ops.size(inputs) mean = array_ops.where(inputs_size > 0, mean, K.zeros_like(mean)) variance = array_ops.where(inputs_size > 0, variance, K.zeros_like(variance)) return mean, variance
def _moments(self, inputs, reduction_axes, keep_dims): mean, variance = nn.moments(inputs, reduction_axes, keep_dims=keep_dims) # TODO(b/129279393): Support zero batch input in non DistributionStrategy # code as well. # TODO(b/130185866): Support zero batch input in graph mode. if (ops.executing_eagerly_outside_functions() and distribution_strategy_context.has_strategy()): inputs_size = array_ops.size(inputs) mean = array_ops.where(inputs_size > 0, mean, K.zeros_like(mean)) variance = array_ops.where(inputs_size > 0, variance, K.zeros_like(variance)) return mean, variance
def _moments(self, inputs, reduction_axes, keep_dims): mean, variance = nn.moments(inputs, reduction_axes, keep_dims=keep_dims) # TODO(b/129279393): Support zero batch input in non DistributionStrategy # code as well. if distribution_strategy_context.has_strategy(): inputs_size = array_ops.size(inputs) mean = tf_utils.smart_cond(inputs_size > 0, lambda: mean, lambda: K.zeros_like(mean)) variance = tf_utils.smart_cond(inputs_size > 0, lambda: variance, lambda: K.zeros_like(variance)) return mean, variance
def _moments(self, inputs, reduction_axes, keep_dims): mean, variance = nn.moments(inputs, reduction_axes, keep_dims=keep_dims) # TODO(b/129279393): Support zero batch input in non DistributionStrategy # code as well. if distribution_strategy_context.has_strategy( ) and not inputs.shape.is_fully_defined(): inputs_size = array_ops.size(inputs) mean = array_ops.where(inputs_size > 0, mean, K.zeros_like(mean)) variance = array_ops.where(inputs_size > 0, variance, K.zeros_like(variance)) return mean, variance
def get_initial_state(self, inputs): """ INPUTS: shape = (Batch, Time_steps, window_size, numberofSensors, 1 or filters ) This function will be called after build() """ initial_state = K.zeros_like(inputs) # Because of all zeros, sum to delete the timestpes dimension ---->(Batch, Time_steps, window_size, numberofSensors, 1 or filters ) initial_state = K.sum(initial_state, axis=1) # Through the convlution to inference the size of hidden state for index, k_shape in enumerate(self.cell.kernel_shape): shape = list(k_shape) shape[-1] = self.cell.filters[index] initial_state = self.cell.input_conv(initial_state, array_ops.zeros(tuple(shape)), padding=self.cell.padding) #self.len_state = initial_state.shape[1] if hasattr(self.cell.state_size, '__len__'): return [initial_state for _ in self.cell.state_size] else: return [initial_state]
def subdiv_moments(self, inputs, reduction_axes, keep_dims): # mean and variance only for the current batch mean, net_sum, variance, squared_mean, input_batch_size = self._subdiv_calculate_mean_and_var( inputs, reduction_axes, keep_dims) if self._support_zero_size_input(): input_batch_size = 0 if input_batch_size is None else input_batch_size mean = array_ops.where(input_batch_size > 0, mean, K.zeros_like(mean)) net_sum = array_ops.where(input_batch_size > 0, net_sum, K.zeros_like(net_sum)) variance = array_ops.where(input_batch_size > 0, variance, K.zeros_like(variance)) squared_mean = array_ops.where(input_batch_size > 0, squared_mean, K.zeros_like(squared_mean)) return mean, net_sum, variance, squared_mean, input_batch_size
def call(self, u_vecs, **kwargs): if self.share_weights: u_hat_vecs = K.conv1d(u_vecs, self.W) else: u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1]) batch_size = K.shape(u_vecs)[0] input_num_capsule = K.shape(u_vecs)[1] u_hat_vecs = K.reshape(u_hat_vecs, (batch_size, input_num_capsule, self.num_capsule, self.dim_capsule)) u_hat_vecs = K.permute_dimensions(u_hat_vecs, (0, 2, 1, 3)) # final u_hat_vecs.shape = [None, num_capsule, input_num_capsule, dim_capsule] b = K.zeros_like( u_hat_vecs[:, :, :, 0]) # shape = [None, num_capsule, input_num_capsule] for i in range(self.routings): c = softmax(b, 1) o = K.batch_dot(c, u_hat_vecs, [2, 2]) if K.backend() == 'theano': o = K.sum(o, axis=1) if i < self.routings - 1: o = K.l2_normalize(o, -1) b = K.batch_dot(o, u_hat_vecs, [2, 3]) if K.backend() == 'theano': b = K.sum(b, axis=1) return self.activation(o)
def create_inital_state(inputs, hidden_size): # We are not using initial states, but need to pass something to K.rnn funciton fake_state = K.zeros_like(inputs) # <= (batch_size, enc_seq_len, latent_dim fake_state = K.sum(fake_state, axis=[1, 2]) # <= (batch_size) fake_state = K.expand_dims(fake_state) # <= (batch_size, 1) fake_state = K.tile(fake_state, [1, hidden_size]) # <= (batch_size, latent_dim return fake_state
def reshape_connection(connection, shape): if connection.shape[1] != shape[1]: connection = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(connection) if connection.shape[-1] != shape[-1]: connection = Lambda(lambda x: K.concatenate([x, K.zeros_like(x)], axis=-1))(connection) return connection
def create_inital_state(inputs, hidden_size): fake_state = K.zeros_like( inputs) # (batch_size, enc_seq_len, latent_dim) fake_state = K.sum(fake_state, axis=[1, 2]) # (batch_size) fake_state = K.expand_dims(fake_state) # (batch_size, 1) fake_state = K.tile(fake_state, [1, hidden_size]) # (batch_size, latent_dim) return fake_state
def inverse_root_via_eigenvalues(m): ev, v = tf.linalg.eigh(m) epsillon = 1e-8 # for numerical stability - clip ev = tf.where(ev > epsillon, x=ev, y=K.ones_like(ev)) v = tf.where(ev > epsillon, x=v, y=K.zeros_like(v)) u = v ev_inv_root = tf.math.reciprocal(tf.math.sqrt(ev)) res = tf.matmul(tf.matmul(u, tf.diag(ev_inv_root)), tf.transpose(v)) return res
def call(self, inputs, **kwargs): input_shape = K.int_shape(inputs) sequence_length, d_model = input_shape[-2:] # output of the "sigmoid halting unit" (not the probability yet) halting = K.sigmoid( K.reshape( K.bias_add(K.dot(K.reshape(inputs, [-1, d_model]), self.act_weights['halting_kernel']), self.act_weights['halting_biases'], data_format='channels_last'), [-1, sequence_length])) if self.zeros_like_halting is None: self.initialize_control_tensors(halting) # useful flags step_is_active = K.greater(self.halt_budget, 0) no_further_steps = K.less_equal(self.halt_budget - halting, 0) # halting probability is equal to # a. halting output if this isn't the last step (we have some budget) # b. to remainder if it is, # c. and zero for the steps that shouldn't be executed at all # (out of budget for them) halting_prob = K.switch( step_is_active, K.switch(no_further_steps, self.remainder, halting), self.zeros_like_halting) self.active_steps += K.switch(step_is_active, self.ones_like_halting, self.zeros_like_halting) # We don't know which step is the last, so we keep updating # expression for the loss with each call of the layer self.ponder_cost = (self.act_weights['time_penalty_t'] * K.mean(self.remainder + self.active_steps)) # Updating "the remaining probability" and the halt budget self.remainder = K.switch(no_further_steps, self.remainder, self.remainder - halting) self.halt_budget -= halting # OK to become negative # If none of the inputs are active at this step, then instead # of zeroing them out by multiplying to all-zeroes halting_prob, # we can simply use a constant tensor of zeroes, which means that # we won't even calculate the output of those steps, saving # some real computational time. if self.zeros_like_input is None: self.zeros_like_input = K.zeros_like(inputs, name='zeros_like_input') # just because K.any(step_is_active) doesn't work in PlaidML any_step_is_active = K.greater(K.sum(K.cast(step_is_active, 'int32')), 0) step_weighted_output = K.switch( any_step_is_active, K.expand_dims(halting_prob, -1) * inputs, self.zeros_like_input) if self.weighted_output is None: self.weighted_output = step_weighted_output else: self.weighted_output += step_weighted_output return [inputs, self.weighted_output]
def custom_loss(y_true, y_pred, loss_weights = loss_weights): # Verified zero_index = K.zeros_like(y_true[:, 0]) ones_index = K.ones_like(y_true[:, 0]) # Classifier labels = y_true[:, 0] class_preds = y_pred[:, 0] bi_crossentropy_loss = -labels * K.log(class_preds) - (1 - labels) * K.log(1 - class_preds) classify_valid_index = tf.where(K.less(y_true[:, 0], 0), zero_index, ones_index) classify_keep_num = K.cast(tf.cast(tf.reduce_sum(classify_valid_index), tf.float32) * SAMPLE_KEEP_RATIO, dtype = tf.int32) # For classification problem, only pick 70% of the valid samples. classify_loss_sum = bi_crossentropy_loss * tf.cast(classify_valid_index, bi_crossentropy_loss.dtype) classify_loss_sum_filtered, _ = tf.nn.top_k(classify_loss_sum, k = classify_keep_num) classify_loss = tf.where(K.equal(classify_keep_num, 0), tf.constant(0, dtype = tf.float32), K.mean(classify_loss_sum_filtered)) # Bounding box regressor rois = y_true[:, 1: 5] roi_preds = y_pred[:, 1: 5] roi_raw_mean_square_error = K.sum(K.square(rois - roi_preds), axis = 1) # mse # roi_raw_smooth_l1_loss = K.mean(tf.where(K.abs(rois - roi_preds) < 1, 0.5 * K.square(rois - roi_preds), K.abs(rois - roi_preds) - 0.5)) # L1 Smooth Loss roi_valid_index = tf.where(K.equal(K.abs(y_true[:, 0]), 1), ones_index, zero_index) roi_keep_num = K.cast(tf.reduce_sum(roi_valid_index), dtype = tf.int32) roi_valid_mean_square_error = roi_raw_mean_square_error * tf.cast(roi_valid_index, roi_raw_mean_square_error.dtype) roi_filtered_mean_square_error, _ = tf.nn.top_k(roi_valid_mean_square_error, k = roi_keep_num) roi_loss = tf.where(K.equal(roi_keep_num, 0), tf.constant(0, dtype = tf.float32), K.mean(roi_filtered_mean_square_error)) # roi_valid_smooth_l1_loss = roi_raw_smooth_l1_loss * roi_valid_index # roi_filtered_smooth_l1_loss, _ = tf.nn.top_k(roi_valid_smooth_l1_loss, k = roi_keep_num) # roi_loss = K.mean(roi_filtered_smooth_l1_loss) # Landmark regressor pts = y_true[:, 5: 17] pt_preds = y_pred[:, 5: 17] pts_raw_mean_square_error = K.sum(K.square(pts - pt_preds), axis = 1) # mse # pts_raw_smooth_l1_loss = K.mean(tf.where(K.abs(pts - pt_preds) < 1, 0.5 * K.square(pts - pt_preds), K.abs(pts - pt_preds) - 0.5)) # L1 Smooth Loss pts_valid_index = tf.where(K.equal(y_true[:, 0], -2), ones_index, zero_index) pts_keep_num = K.cast(tf.reduce_sum(pts_valid_index), dtype = tf.int32) pts_valid_mean_square_error = pts_raw_mean_square_error * tf.cast(pts_valid_index, tf.float32) pts_filtered_mean_square_error, _ = tf.nn.top_k(pts_valid_mean_square_error, k = pts_keep_num) pts_loss = tf.where(K.equal(pts_keep_num, 0), tf.constant(0, dtype = tf.float32), K.mean(pts_filtered_mean_square_error)) # pts_valid_smooth_l1_loss = pts_raw_smooth_l1_loss * pts_valid_index # pts_filtered_smooth_l1_loss, _ = tf.nn.top_k(pts_valid_smooth_l1_loss, k = pts_keep_num) # pts_loss = K.mean(pts_filtered_smooth_l1_loss) loss = classify_loss * loss_weights[0] + roi_loss * loss_weights[1] + pts_loss * loss_weights[2] return loss
def _assign_moving_average(self, variable, value, momentum, inputs_size): with K.name_scope('AssignMovingAvg') as scope: with ops.colocate_with(variable): decay = ops.convert_to_tensor_v2(1.0 - momentum, name='decay') if decay.dtype != variable.dtype.base_dtype: decay = math_ops.cast(decay, variable.dtype.base_dtype) update_delta = ( variable - math_ops.cast(value, variable.dtype)) * decay if inputs_size is not None: update_delta = array_ops.where(inputs_size > 0, update_delta, K.zeros_like(update_delta)) return state_ops.assign_sub(variable, update_delta, name=scope)
def _assign_moving_average(self, variable, value, momentum, inputs_size): with K.name_scope('AssignMovingAvg') as scope: with ops.colocate_with(variable): # decay = ops.convert_to_tensor(1.0 - momentum, name='decay') # if decay.dtype != variable.dtype.base_dtype: # decay = math_ops.cast(decay, variable.dtype.base_dtype) if 'moving_mean' in variable.name: if self.bn_state == 1: self.agg_mean += value elif self.bn_state == 2: if self.bn_update_cntr > 0: self.agg_mean = self.agg_mean / self.bn_update_cntr update_delta = math_ops.cast( self.agg_mean, variable.dtype) if inputs_size is not None: update_delta = array_ops.where( inputs_size > 0, update_delta, K.zeros_like(update_delta)) return state_ops.assign(variable, update_delta, name=scope) elif 'moving_variance' in variable.name: if self.bn_state == 1: self.agg_var += value self.bn_update_cntr += 1 elif self.bn_state == 2: if self.bn_update_cntr > 0: self.agg_var = self.agg_var / self.bn_update_cntr update_delta = math_ops.cast( self.agg_var, variable.dtype) if inputs_size is not None: update_delta = array_ops.where( inputs_size > 0, update_delta, K.zeros_like(update_delta)) return state_ops.assign(variable, update_delta, name=scope) return state_ops.assign(variable, variable, name=scope)
def get_initial_states(self, inputs): # (samples, timesteps, rows, cols, filters) initial_state = K.zeros_like(inputs) # (samples, rows, cols, filters) initial_state = K.sum(initial_state, axis=1) depthwise_shape = list(self.depthwise_kernel_shape) pointwise_shape = list(self.pointwise_kernel_shape) initial_state = self.input_conv( initial_state, K.zeros(tuple(depthwise_shape)), K.zeros(tuple(pointwise_shape)), padding=self.padding) initial_states = [initial_state for _ in range(2)] return initial_states
def initialize_control_tensors(self, halting): """ Initializes constants and some step-tracking variables during the first call of the layer (since for the Universal Transformer all the following calls are supposed to be with inputs of identical shapes). """ self.zeros_like_halting = K.zeros_like(halting, name='zeros_like_halting') self.ones_like_halting = K.ones_like(halting, name='ones_like_halting') self.remainder = self.ones_like_halting self.active_steps = self.zeros_like_halting self.halt_budget = self.ones_like_halting - self.halt_epsilon
def get_constants(self, inputs, training=None): constants = [] if self.implementation == 0 and 0 < self.dropout < 1: ones = K.zeros_like(inputs) ones = K.sum(ones, axis=1) ones += 1 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: depthwise_shape = list(self.depthwise_kernel_shape) pointwise_shape = list(self.pointwise_kernel_shape) ones = K.zeros_like(inputs) ones = K.sum(ones, axis=1) ones = self.input_conv(ones, K.zeros(depthwise_shape), K.zeros(pointwise_shape), padding=self.padding) ones += 1. def dropped_inputs(): # pylint: disable=function-redefined 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 call(self, u_vecs, scores=None): # if self.share_weights: # u_hat_vecs = K.conv1d(u_vecs, self.W) # else: # u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1]) u_hat_vecs = u_vecs batch_size = K.shape(u_vecs)[0] input_num_capsule = K.shape(u_vecs)[1] if scores is not None: scores = K.permute_dimensions(scores, (0, 2, 1)) u_hat_vecs = u_hat_vecs * scores u_hat_vecs = K.reshape(u_hat_vecs, (batch_size, input_num_capsule, self.num_capsule, self.dim_capsule)) u_hat_vecs = K.permute_dimensions(u_hat_vecs, (0, 2, 1, 3)) b = K.zeros_like( u_hat_vecs[:, :, :, 0]) # shape = [None, num_capsule, input_num_capsule] # biases = self.add_weight(name='capsule_kernel', # shape=(batch_size1, self.num_capsule, self.dim_capsule), # # shape=self.kernel_size, # dtype=tf.float32, # initializer='glorot_uniform', # trainable=True) # biases = tf.get_variable(name='bias', # shape=(self.num_capsule, self.dim_capsule), initializer='glorot_uniform',) for i in range(self.routings): # b = K.permute_dimensions(b, (0, 2, 1)) # shape = [None, input_num_capsule, num_capsule] # c = K.softmax(b) leak = tf.zeros_like(b, optimize=True) leak = tf.reduce_sum(leak, axis=1, keep_dims=True) leaky_logits = tf.concat([leak, b], axis=1) leaky_routing = tf.nn.softmax(leaky_logits, dim=1) c = tf.split(leaky_routing, [1, self.num_capsule], axis=1)[1] # c = K.permute_dimensions(c, (0, 2, 1)) # b = K.permute_dimensions(b, (0, 2, 1)) o = K.batch_dot(c, u_hat_vecs, [2, 2]) # + self.biases outputs = self.activation(o) if i < self.routings - 1: b = K.batch_dot(outputs, u_hat_vecs, [2, 3]) # self.c = scores return outputs
def get_initial_state(self, inputs): # (samples, timesteps, rows, cols, z, filters) initial_state = K.zeros_like(inputs) d = [1,2,1,1,1] if self.cell.data_format == 'channels_first' else [1,1,1,1,2] initial_state = K.tile(initial_state, d) shape = list(self.cell.kernel_shape) shape[-1] = self.cell.filters initial_state = self.cell.input_conv(initial_state, array_ops.zeros(tuple(shape)), padding=self.cell.padding) if hasattr(self.cell.state_size, '__len__'): return [initial_state for _ in self.cell.state_size] else: return [initial_state]
def get_initial_state(self, inputs): # (samples, timesteps, rows, cols, filters) initial_state = K.zeros_like(inputs) # (samples, rows, cols, filters) initial_state = K.sum(initial_state, axis=1) shape = list(self.cell.kernel_shape) shape[-1] = self.cell.filters initial_state = self.cell.input_conv(initial_state, K.zeros(tuple(shape)), padding=self.cell.padding) if hasattr(self.cell.state_size, '__len__'): return [initial_state for _ in self.cell.state_size] else: return [initial_state]
def get_initial_state(self, inputs): initial_state = K.zeros_like(inputs) initial_state = K.sum(initial_state, axis=(1, 2)) initial_state = K.expand_dims(initial_state) initial_state = K.tile(initial_state, [1, self.units]) # (samples, output_dim) n = K.identity(initial_state) d = K.identity(initial_state) h = K.identity(initial_state) dtype = initial_state.dtype.name min_value = np.array([1E38]).astype(dtype).item() a_max = K.identity(initial_state) - min_value h = h + self.cell.recurrent_activation(K.expand_dims(self.cell.initial_attention, axis=0)) return [n, d, h, a_max]
def get_initial_state(self, inputs): # (samples, timesteps, rows, cols, z, filters) initial_state = K.zeros_like(inputs) # (samples, rows, cols, z, filters) initial_state = K.sum(initial_state, axis=1) shape = list(self.cell.kernel_shape) shape[-1] = self.cell.filters initial_state = self.cell.input_conv(initial_state, array_ops.zeros(tuple(shape)), padding=self.cell.padding) if hasattr(self.cell.state_size, '__len__'): return [initial_state for _ in self.cell.state_size] else: return [initial_state]
def check_the_config_valid(para, window_size, feature): initial_state = np.zeros((1, window_size, feature, 1)) initial_state = tf.cast(initial_state, 'float32') initial_state = K.zeros_like(initial_state) channel = 1 try: for i in range(para["preprocessing_layers"]): shape = (para["pre_kernel_width"], 1, channel, para["pre_number_filters"]) channel = para["pre_number_filters"] initial_state = K.conv2d( initial_state, array_ops.zeros(tuple(shape)), (para["pre_strides"], 1)) #,dilation_rate=(para["pre_dilation_rate"],1)) for i in range(1, 4): assert len(para["eclstm_{}_recurrent_activation".format(i)]) == len(para["eclstm_{}_conv_activation".format(i)]) == \ len(para["eclstm_{}_number_filters".format(i)]) == len(para["eclstm_{}_kernel_width".format(i)])== \ len(para["eclstm_{}_fusion".format(i)]), "Archtecture Parameters of {} layer should be in same length".format(i) for j in range( len(para["eclstm_{}_recurrent_activation".format(i)])): if para["eclstm_{}_recurrent_activation".format(i)][0] is None: break if para["eclstm_{}_fusion".format(i)][j] == "early": shape = (para["eclstm_{}_kernel_width".format(i)][j], feature, channel, para["eclstm_{}_number_filters".format(i)][j]) feature = 1 channel = para["eclstm_{}_number_filters".format(i)][j] else: shape = (para["eclstm_{}_kernel_width".format(i)][j], 1, channel, para["eclstm_{}_number_filters".format(i)][j]) channel = para["eclstm_{}_number_filters".format(i)][j] initial_state = K.conv2d( initial_state, array_ops.zeros(tuple(shape)), (para["eclstm_{}_strides".format(i)], 1)) print("valid Configuration!") return True except: print( "Invalid Configuration! Try smaller strides or kernel size or greater window size!" ) return False
def call(self, y): # Sanity Check if isinstance(y, list): raise ValueError('TSG layer has only 1 input') # y = tf_print(y, [y], message='{}: The unconstrained action is:'.format(y.name.split('/')[0]), summarize=-1) y = check_numerics(y, 'Problem with input y') # Calculate A.c Ac = tensordot(self.A_graph, self.c_graph, 1) # Calculate b - Ac bMinusAc = self.b_graph - Ac # Calculate y - c yMinusc = y - self.c_graph # Calculate A.(y - c) ADotyMinusc = K.sum((self.A_graph * expand_dims(yMinusc, -2)), axis=2) # Do elem-wise division intersection_points = bMinusAc / (ADotyMinusc + K.epsilon() ) # Do we need the K.epsilon()? # Enforce 0 <= intersection_points <= 1 because the point must lie between c and y greater_1 = K.greater(intersection_points, K.ones_like(intersection_points)) candidate_alpha = K.switch(greater_1, K.ones_like(intersection_points) + 1, intersection_points) less_0 = K.less(candidate_alpha, K.zeros_like(intersection_points)) candidate_alpha = K.switch(less_0, K.ones_like(intersection_points) + 1, candidate_alpha) # Find farthest intersection point from y to get projection point alpha = K.min(candidate_alpha, axis=-1, keepdims=True) # If it is an interior point, y itself is the projection point interior_point = K.greater(alpha, K.ones_like(alpha)) alpha = K.switch(interior_point, K.ones_like(alpha), alpha) # alpha = tf_print(alpha, [alpha], message="{}: The value of alpha is: ".format(alpha.name.split('/')[0])) # Return \alpha.y + (1 - \alpha).c z = alpha * y + ((1 - alpha) * self.c_graph) # z = tf_print(z, [z], message='{}: The constrained action is:'.format(z.name.split('/')[0]), summarize=-1) return z
def _assign_moving_average(self, variable, value, momentum, inputs_size): with ops.name_scope(None, 'AssignMovingAvg', [variable, value, momentum]) as scope: with ops.colocate_with(variable): decay = ops.convert_to_tensor(1.0 - momentum, name='decay') if decay.dtype != variable.dtype.base_dtype: decay = math_ops.cast(decay, variable.dtype.base_dtype) update_delta = (variable - math_ops.cast(value, variable.dtype)) * decay # TODO(b/129279393): Support zero batch input in non # DistributionStrategy code as well. if distribution_strategy_context.has_strategy(): update_delta = tf_utils.smart_cond( inputs_size > 0, lambda: update_delta, lambda: K.zeros_like(update_delta)) return state_ops.assign_sub(variable, update_delta, name=scope)
def myCrossEntropy(y_true, y_pred, e=0.3): loss = K.sparse_categorical_crossentropy(y_true, y_pred) loss0 = K.sparse_categorical_crossentropy(K.zeros_like(y_true), y_pred) loss1 = K.sparse_categorical_crossentropy(K.ones_like(y_true), y_pred) loss2 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 2, y_pred) loss3 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 3, y_pred) loss4 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 4, y_pred) loss5 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 5, y_pred) loss6 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 6, y_pred) loss7 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 7, y_pred) loss8 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 8, y_pred) loss9 = K.sparse_categorical_crossentropy(K.ones_like(y_true) * 9, y_pred) return ((100.0 - 5.765 - 1.359 - 1.000 - 1.348 - 1.554 - 1.995 - 3.042 - 6.347 - 10.431 - 17.632) * loss + 5.765 * loss0 + 1.359 * loss1 + 1.000 * loss2 + 1.348 * loss3 + 1.553 * loss4 + 1.995 * loss5 + 3.042 * loss6 + 6.347 * loss7 + 10.421 * loss8 + 17.632 * loss9)
def _assign_subdiv_moving_average(self, variable, value, momentum, subdivsions, count): with K.name_scope('AssignSubDivMovingAvg') as scope: with ops.colocate_with(variable): decay = ops.convert_to_tensor_v2_with_dispatch(1.0 - momentum, name='decay') if decay.dtype != variable.dtype.base_dtype: decay = math_ops.cast(decay, variable.dtype.base_dtype) # get the aggregated update update_delta = (variable - math_ops.cast(value, variable.dtype)) * decay # update at the end of last step update_delta = array_ops.where((count + 1) % subdivisions == 0, update_delta, K.zeros_like(update_delta)) return state_ops.assign_sub(variable, update_delta, name=scope)
def _assign_subdiv_rotating_sum(self, variable, value, subdivisions, count, inputs_size): with K.name_scope('AssignSubDivRotatedSum') as scope: with ops.colocate_with(variable): # reduce it for the current update_delta = value #/subdivisions # if the input size is 0 if inputs_size is not None: update_delta = array_ops.where(inputs_size > 0, update_delta, K.zeros_like(update_delta)) # if we are starting a new batch set the variable to 0 by removing it # from update delta then add the delta to the variable to get # rid of the value variable update_delta = array_ops.where(count % subdivisions == 0, update_delta - variable, update_delta) return state_ops.assign_add(variable, update_delta, name=scope)
def multi_inputs_multi_outputs_model(): a = keras.layers.Input(shape=(16,), name='input_a') b = keras.layers.Input(shape=(16,), name='input_b') m = keras.layers.Input(shape=(8,), dtype='bool', name='input_m') dense = keras.layers.Dense(8, name='dense_1') a_2 = dense(a) # Apply a mask s_2 = keras.layers.Lambda(lambda k: K.switch(k[0], k[1], K.zeros_like(k[1])))([m, a_2]) b_2 = dense(b) merged = keras.layers.concatenate([s_2, b_2], name='merge') c = keras.layers.Dense(3, activation='softmax', name='dense_2')(merged) d = keras.layers.Dense(2, activation='softmax', name='dense_3')(merged) model = keras.models.Model(inputs=[a, b, m], outputs=[c, d]) model.compile( loss='categorical_crossentropy', optimizer='rmsprop', metrics={ 'dense_2': 'categorical_accuracy', 'dense_3': 'categorical_accuracy' }) return model