Python keras.backend.ones_like() Examples
The following are 30 code examples for showing how to use keras.backend.ones_like(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
keras.backend
, or try the search function
.
Example 1
Project: deep-models Author: LaurentMazare File: rhn.py License: Apache License 2.0 | 6 votes |
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
Example 2
Project: keras_bn_library Author: bnsnapper File: rnnrbm.py License: MIT License | 6 votes |
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
Example 3
Project: keras_bn_library Author: bnsnapper File: recurrent.py License: MIT License | 6 votes |
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
Example 4
Project: NTM-Keras Author: SigmaQuan File: lstm2ntm.py License: MIT License | 6 votes |
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
Example 5
Project: research Author: commaai File: layers.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
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) 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.concatenate([ones] * input_dim, 1) 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
Example 6
Project: research Author: commaai File: layers.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
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) 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.concatenate([ones] * input_dim, 1) 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
Example 7
Project: Kaggle-Carvana-Image-Masking-Challenge Author: petrosgk File: losses.py License: MIT License | 6 votes |
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
Example 8
Project: Kaggle-Carvana-Image-Masking-Challenge Author: petrosgk File: losses.py License: MIT License | 6 votes |
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
Example 9
Project: recurrent-attention-for-QA-SQUAD-based-on-keras Author: wentaozhu File: QnA.py License: MIT License | 6 votes |
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
Example 10
Project: recurrent-attention-for-QA-SQUAD-based-on-keras Author: wentaozhu File: rnnlayer.py License: MIT License | 6 votes |
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
Example 11
Project: recurrent-attention-for-QA-SQUAD-based-on-keras Author: wentaozhu File: rnnlayer.py License: MIT License | 6 votes |
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
Example 12
Project: recurrent-attention-for-QA-SQUAD-based-on-keras Author: wentaozhu File: rnnlayer.py License: MIT License | 6 votes |
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
Example 13
Project: onto-lstm Author: pdasigi File: pooling.py License: Apache License 2.0 | 6 votes |
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
Example 14
Project: nn_playground Author: DingKe File: ternary_ops.py License: MIT License | 6 votes |
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
Example 15
Project: R-NET-in-Keras Author: YerevaNN File: QuestionPooling.py License: MIT License | 6 votes |
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
Example 16
Project: deepcpg Author: cangermueller File: metrics.py License: MIT License | 6 votes |
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)
Example 17
Project: ikelos Author: braingineer File: rtn.py License: MIT License | 6 votes |
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
Example 18
Project: ikelos Author: braingineer File: rtn.py License: MIT License | 6 votes |
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
Example 19
Project: deep_qa Author: allenai File: bigru_index_selector.py License: Apache License 2.0 | 6 votes |
def call(self, inputs, mask=None): """ Extract the GRU output for the target document index for the forward and backwards GRU outputs, and then concatenate them. If the target word index is at index l, and there are T total document words, the desired output in the forward pass is at GRU_f[l] (ignoring the batched case) and the desired output of the backwards pass is at GRU_b[T-l]. We need to get these two vectors and concatenate them. To do so, we'll reverse the backwards GRU, which allows us to use the same index/mask for both. """ # TODO(nelson): deal with case where cloze token appears multiple times # in a question. word_indices, gru_f, gru_b = inputs index_mask = K.cast(K.equal((K.ones_like(word_indices) * self.target_index), word_indices), "float32") gru_mask = K.repeat_elements(K.expand_dims(index_mask, -1), K.int_shape(gru_f)[-1], K.ndim(gru_f) - 1) masked_gru_f = switch(gru_mask, gru_f, K.zeros_like(gru_f)) selected_gru_f = K.sum(masked_gru_f, axis=1) masked_gru_b = switch(gru_mask, gru_b, K.zeros_like(gru_b)) selected_gru_b = K.sum(masked_gru_b, axis=1) selected_bigru = K.concatenate([selected_gru_f, selected_gru_b], axis=-1) return selected_bigru
Example 20
Project: View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition Author: microsoft File: transform_rnn.py License: MIT License | 5 votes |
def _trans(theta): tx = theta[:,3:4] ty = theta[:,4:5] tz = theta[:,5:6] zero = K.zeros_like(tx) one = K.ones_like(tx) first = K.reshape(K.concatenate([one,zero,zero,tx],axis=1),(-1,1,4)) second = K.reshape(K.concatenate([zero,one,zero,ty],axis=1),(-1,1,4)) third = K.reshape(K.concatenate([zero,zero,one,tz],axis=1),(-1,1,4)) trans = K.concatenate([first,second,third],axis=1) trans = trans.reshape((trans.shape[0],3,4)) return trans
Example 21
Project: View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition Author: microsoft File: transform_rnn.py License: MIT License | 5 votes |
def _rotation_y(theta): r1 = K.cos(theta[:,0:1]) r2 = K.sin(theta[:,0:1]) zero = K.zeros_like(r1) one = K.ones_like(r1) first = K.reshape(K.concatenate([r1,zero,r2,zero],axis=1),(-1,1,4)) second = K.reshape(K.concatenate([zero,one,zero,zero],axis=1),(-1,1,4)) third = K.reshape(K.concatenate([-r2,zero,r1,zero],axis=1),(-1,1,4)) fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4)) rotation_y = K.concatenate([first,second,third,fourth],axis=1) rotation_y = T.reshape(rotation_y,[-1,4,4]) return rotation_y
Example 22
Project: View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition Author: microsoft File: transform_rnn.py License: MIT License | 5 votes |
def _rotation_x(theta): r1 = K.cos(theta[:,1:2]) r2 = K.sin(theta[:,1:2]) zero = K.zeros_like(r1) one = K.ones_like(r1) first = K.reshape(K.concatenate([one,zero,zero,zero],axis=1),(-1,1,4)) second = K.reshape(K.concatenate([zero,r1,-r2,zero],axis=1),(-1,1,4)) third = K.reshape(K.concatenate([zero,r2,r1,zero],axis=1),(-1,1,4)) fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4)) rotation_x = K.concatenate([first,second,third,fourth],axis=1) rotation_x = T.reshape(rotation_x,[-1,4,4]) return rotation_x
Example 23
Project: View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition Author: microsoft File: transform_rnn.py License: MIT License | 5 votes |
def _rotation_z(theta): r1 = K.cos(theta[:,2:3]) r2 = K.sin(theta[:,2:3]) zero = K.zeros_like(r1) one = K.ones_like(r1) first = K.reshape(K.concatenate([r1,-r2,zero,zero],axis=1),(-1,1,4)) second = K.reshape(K.concatenate([r2,r1,zero,zero],axis=1),(-1,1,4)) third = K.reshape(K.concatenate([zero,zero,one,zero],axis=1),(-1,1,4)) fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4)) rotation_z = K.concatenate([first,second,third,fourth],axis=1) rotation_z = T.reshape(rotation_z,[-1,4,4]) return rotation_z
Example 24
Project: View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition Author: microsoft File: transform_rnn.py License: MIT License | 5 votes |
def _trans_rot_new(theta): tx = theta[:,3:4] ty = theta[:,4:5] tz = theta[:,5:6] zero = K.zeros_like(tx) one = K.ones_like(tx) first = K.reshape(K.concatenate([one,zero,zero,tx],axis=1),(-1,1,4)) second = K.reshape(K.concatenate([zero,one,zero,ty],axis=1),(-1,1,4)) third = K.reshape(K.concatenate([zero,zero,one,tz],axis=1),(-1,1,4)) fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4)) trans = K.concatenate([first,second,third,fourth],axis=1) trans = T.reshape(trans,[-1,4,4]) return trans
Example 25
Project: MCF-3D-CNN Author: xyj77 File: liver_model.py License: MIT License | 5 votes |
def mycrossentropy(self, y_true, y_pred): e = 0.3 # for i in range(y_true.shape[0]): # for j in range(3): # sum += 0.1*(-1**y_true(i,j))*exp(abs(np.argmax(y_true[i,:])-j))*log(y_pred(i,j)) # return sum/len # y = np.argmax(y_true, axis=1) # y_ = np.argmax(y_pred, axis=1) # print '*****************',y_pred # return (1-e)*K.categorical_crossentropy(y_pred,y_true) - e*K.categorical_crossentropy(y_pred, (1-y_true)/(self.config.classes-1)) return (1-e)*K.categorical_crossentropy(y_pred,y_true) + e*K.categorical_crossentropy(y_pred, K.ones_like(y_pred)/2)
Example 26
Project: NTM-Keras Author: SigmaQuan File: ntm.py License: MIT License | 5 votes |
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
Example 27
Project: FSA-Net Author: shamangary File: layers.py License: Apache License 2.0 | 5 votes |
def _make_regular_grids(self, batch_size, height, width): # making a single regular grid x_linspace = K_linspace(-1., 1., width) y_linspace = K_linspace(-1., 1., height) x_coordinates, y_coordinates = K_meshgrid(x_linspace, y_linspace) x_coordinates = K.flatten(x_coordinates) y_coordinates = K.flatten(y_coordinates) ones = K.ones_like(x_coordinates) grid = K.concatenate([x_coordinates, y_coordinates, ones], 0) # repeating grids for each batch grid = K.flatten(grid) grids = K.tile(grid, K.stack([batch_size])) return K.reshape(grids, (batch_size, 3, height * width))
Example 28
Project: stochastic_depth_keras Author: dblN File: train.py License: MIT License | 5 votes |
def residual_drop(x, input_shape, output_shape, strides=(1, 1)): global add_tables nb_filter = output_shape[0] conv = Convolution2D(nb_filter, 3, 3, subsample=strides, border_mode="same", W_regularizer=l2(weight_decay))(x) conv = BatchNormalization(axis=1)(conv) conv = Activation("relu")(conv) conv = Convolution2D(nb_filter, 3, 3, border_mode="same", W_regularizer=l2(weight_decay))(conv) conv = BatchNormalization(axis=1)(conv) if strides[0] >= 2: x = AveragePooling2D(strides)(x) if (output_shape[0] - input_shape[0]) > 0: pad_shape = (1, output_shape[0] - input_shape[0], output_shape[1], output_shape[2]) padding = K.zeros(pad_shape) padding = K.repeat_elements(padding, K.shape(x)[0], axis=0) x = Lambda(lambda y: K.concatenate([y, padding], axis=1), output_shape=output_shape)(x) _death_rate = K.variable(death_rate) scale = K.ones_like(conv) - _death_rate conv = Lambda(lambda c: K.in_test_phase(scale * c, c), output_shape=output_shape)(conv) out = merge([conv, x], mode="sum") out = Activation("relu")(out) gate = K.variable(1, dtype="uint8") add_tables += [{"death_rate": _death_rate, "gate": gate}] return Lambda(lambda tensors: K.switch(gate, tensors[0], tensors[1]), output_shape=output_shape)([out, x])
Example 29
Project: Keras-IndRNN Author: titu1994 File: ind_rnn.py License: MIT License | 5 votes |
def call(self, inputs, states, training=None): if 0 < self.dropout < 1 and self._dropout_mask is None: self._dropout_mask = _generate_dropout_mask( K.ones_like(inputs), self.dropout, training=training, count=1) if (0 < self.recurrent_dropout < 1 and self._recurrent_masks is None): _recurrent_mask = _generate_dropout_mask( K.ones_like(states[0]), self.recurrent_dropout, training=training, count=1) self._recurrent_masks = _recurrent_mask # dropout matrices for input units dp_mask = self._dropout_mask # dropout matrices for recurrent units rec_dp_masks = self._recurrent_masks h_tm1 = states[0] # previous state if 0. < self.dropout < 1.: inputs *= dp_mask[0] if 0. < self.recurrent_dropout < 1.: h_tm1 *= rec_dp_masks[0] h = K.dot(inputs, self.kernel) h = h + (h_tm1 * self.recurrent_kernel) if self.use_bias: h = K.bias_add(h, self.bias) h = self.activation(h) if 0 < self.dropout + self.recurrent_dropout: if training is None: h._uses_learning_phase = True return h, [h]
Example 30
Project: Nested-LSTM Author: titu1994 File: nested_lstm.py License: MIT License | 5 votes |
def call(self, inputs, states, training=None): if 0 < self.dropout < 1 and self._dropout_mask is None: self._dropout_mask = _generate_dropout_mask( K.ones_like(inputs), self.dropout, training=training, count=1) if (0 < self.recurrent_dropout < 1 and self._nested_recurrent_masks is None): _nested_recurrent_mask = _generate_dropout_mask( K.ones_like(states[0]), self.recurrent_dropout, training=training, count=self.depth) self._nested_recurrent_masks = _nested_recurrent_mask # dropout matrices for input units dp_mask = self._dropout_mask # dropout matrices for recurrent units rec_dp_masks = self._nested_recurrent_masks h_tm1 = states[0] # previous memory state c_tm1 = states[1:self.depth + 1] # previous carry states if 0. < self.dropout < 1.: inputs *= dp_mask[0] h, c = self.nested_recurrence(inputs, hidden_state=h_tm1, cell_states=c_tm1, recurrent_masks=rec_dp_masks, current_depth=0) if 0 < self.dropout + self.recurrent_dropout: if training is None: h._uses_learning_phase = True return h, c