Python tensorflow.keras.backend.shape() Examples
The following are 30
code examples of tensorflow.keras.backend.shape().
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 also want to check out all available functions/classes of the module
tensorflow.keras.backend
, or try the search function
.
Example #1
Source File: vae-cnn-mnist-8.1.2.py From Advanced-Deep-Learning-with-Keras with MIT License | 7 votes |
def sampling(args): """Reparameterization trick by sampling fr an isotropic unit Gaussian. # Arguments: args (tensor): mean and log of variance of Q(z|X) # Returns: z (tensor): sampled latent vector """ z_mean, z_log_var = args batch = K.shape(z_mean)[0] dim = K.int_shape(z_mean)[1] # by default, random_normal has mean=0 and std=1.0 epsilon = K.random_normal(shape=(batch, dim)) return z_mean + K.exp(0.5 * z_log_var) * epsilon
Example #2
Source File: RandomSequenceMask.py From tape-neurips2019 with MIT License | 6 votes |
def _generate_bert_mask(self, inputs): def _numpy_generate_contiguous_mask(array): mask = np.random.random(array.shape) < (1 / self.avg_seq_len) mask = np.cumsum(mask, 1) seqvals = np.max(mask) mask_prob = self.percentage * array.shape[1] / seqvals # increase probability because fewer sequences vals_to_mask = np.arange(seqvals)[np.random.random((seqvals,)) < mask_prob] indices_to_mask = np.isin(mask, vals_to_mask) mask[indices_to_mask] = 1 mask[~indices_to_mask] = 0 return np.asarray(mask, np.bool) bert_mask = tf.py_func(_numpy_generate_contiguous_mask, [inputs], tf.bool) bert_mask.set_shape(inputs.shape) return bert_mask
Example #3
Source File: layers.py From neuron with GNU General Public License v3.0 | 6 votes |
def _single_batch_trf(self, vol): # vol should be vol_shape + [nb_features] # self.trf should be vol_shape + [nb_features] + [ndims] vol_shape = vol.shape.as_list() nb_input_dims = vol_shape[-1] # this is inefficient... new_vols = [None] * self.output_features for j in range(self.output_features): new_vols[j] = tf.zeros(vol_shape[:-1], dtype=tf.float32) for i in range(nb_input_dims): trf_vol = transform(vol[..., i], self.trf[..., i, j, :] * self.trf_mult, interp_method=self.interp_method) trf_vol = tf.reshape(trf_vol, vol_shape[:-1]) new_vols[j] += trf_vol * self.mult[..., i, j] if self.use_bias: new_vols[j] += self.bias[..., j] return tf.stack(new_vols, -1)
Example #4
Source File: cells.py From DeepPavlov with Apache License 2.0 | 6 votes |
def build(self, input_shape): assert len(input_shape) == 3 assert input_shape[0] == input_shape[1] assert input_shape[0][:-1] == input_shape[2][:-1] input_dim, features_dim = input_shape[0][-1], input_shape[2][-1] if self.use_intermediate_layer: self.first_kernel = self.add_weight( shape=(features_dim, self.intermediate_dim), initializer="random_uniform", name='first_kernel') self.first_bias = self.add_weight( shape=(self.intermediate_dim,), initializer="random_uniform", name='first_bias') self.features_kernel = self.add_weight( shape=(features_dim, 1), initializer="random_uniform", name='kernel') self.features_bias = self.add_weight( shape=(1,), initializer=Constant(self.bias_initializer), name='bias') if self.use_dimension_bias: self.dimensions_bias = self.add_weight( shape=(input_dim,), initializer="random_uniform", name='dimension_bias') super(WeightedCombinationLayer, self).build(input_shape)
Example #5
Source File: network.py From DeepPavlov with Apache License 2.0 | 6 votes |
def gather_indexes(A: tf.Tensor, B: tf.Tensor) -> tf.Tensor: """ Args: A: a tensor with data B: an integer tensor with indexes Returns: `answer` a tensor such that ``answer[i, j] = A[i, B[i, j]]``. In case `B` is one-dimensional, the output is ``answer[i] = A[i, B[i]]`` """ are_indexes_one_dim = (kb.ndim(B) == 1) if are_indexes_one_dim: B = tf.expand_dims(B, -1) first_dim_indexes = tf.expand_dims(tf.range(tf.shape(B)[0]), -1) first_dim_indexes = tf.tile(first_dim_indexes, [1, tf.shape(B)[1]]) indexes = tf.stack([first_dim_indexes, B], axis=-1) answer = tf.gather_nd(A, indexes) if are_indexes_one_dim: answer = answer[:,0] return answer
Example #6
Source File: layers.py From neuron with GNU General Public License v3.0 | 6 votes |
def build(self, input_shape): # Create mean and count # These are weights because just maintaining variables don't get saved with the model, and we'd like # to have these numbers saved when we save the model. # But we need to make sure that the weights are untrainable. self.mean = self.add_weight(name='mean', shape=input_shape[1:], initializer='zeros', trainable=False) self.count = self.add_weight(name='count', shape=[1], initializer='zeros', trainable=False) # self.mean = K.zeros(input_shape[1:], name='mean') # self.count = K.variable(0.0, name='count') super(MeanStream, self).build(input_shape) # Be sure to call this somewhere!
Example #7
Source File: efficientnet.py From keras_imagenet with MIT License | 6 votes |
def get_dropout(**kwargs): """Wrapper over custom dropout. Fix problem of ``None`` shape for tf.keras. It is not possible to define FixedDropout class as global object, because we do not have modules for inheritance at first time. Issue: https://github.com/tensorflow/tensorflow/issues/30946 """ class FixedDropout(layers.Dropout): def _get_noise_shape(self, inputs): if self.noise_shape is None: return self.noise_shape symbolic_shape = backend.shape(inputs) noise_shape = [symbolic_shape[axis] if shape is None else shape for axis, shape in enumerate(self.noise_shape)] return tuple(noise_shape) return FixedDropout
Example #8
Source File: layers.py From neuron with GNU General Public License v3.0 | 6 votes |
def _mean_update(pre_mean, pre_count, x, pre_cap=None): # compute this batch stats this_sum = tf.reduce_sum(x, 0) this_bs = tf.cast(K.shape(x)[0], 'float32') # this batch size # increase count and compute weights new_count = pre_count + this_bs alpha = this_bs/K.minimum(new_count, pre_cap) # compute new mean. Note that once we reach self.cap (e.g. 1000), the 'previous mean' matters less new_mean = pre_mean * (1-alpha) + (this_sum/this_bs) * alpha return (new_mean, new_count) ########################################## ## FFT Layers ##########################################
Example #9
Source File: backend.py From DeepPoseKit with Apache License 2.0 | 6 votes |
def _preprocess_conv2d_input(x, data_format): """Transpose and cast the input before the conv2d. # Arguments x: input tensor. data_format: string, `"channels_last"` or `"channels_first"`. # Returns A tensor. """ if dtype(x) == "float64": x = tf.cast(x, "float32") if data_format == "channels_first": # TF uses the last dimension as channel dimension, # instead of the 2nd one. # TH input shape: (samples, input_depth, rows, cols) # TF input shape: (samples, rows, cols, input_depth) x = tf.transpose(x, (0, 2, 3, 1)) return x
Example #10
Source File: metrics.py From neuron with GNU General Public License v3.0 | 6 votes |
def loss(self, y_true, y_pred): # get the value for the true and fake images disc_true = self.disc(y_true) disc_pred = self.disc(y_pred) # sample a x_hat by sampling along the line between true and pred # z = tf.placeholder(tf.float32, shape=[None, 1]) # shp = y_true.get_shape()[0] # WARNING: SHOULD REALLY BE shape=[batch_size, 1] !!! # self.batch_size does not work, since it's not None!!! alpha = K.random_uniform(shape=[K.shape(y_pred)[0], 1, 1, 1]) diff = y_pred - y_true interp = y_true + alpha * diff # take gradient of D(x_hat) gradients = K.gradients(self.disc(interp), [interp])[0] grad_pen = K.mean(K.square(K.sqrt(K.sum(K.square(gradients), axis=1))-1)) # compute loss return (K.mean(disc_pred) - K.mean(disc_true)) + self.lambda_gp * grad_pen
Example #11
Source File: cvae-cnn-mnist-8.2.1.py From Advanced-Deep-Learning-with-Keras with MIT License | 6 votes |
def sampling(args): """Implements reparameterization trick by sampling from a gaussian with zero mean and std=1. Arguments: args (tensor): mean and log of variance of Q(z|X) Returns: sampled latent vector (tensor) """ z_mean, z_log_var = args batch = K.shape(z_mean)[0] dim = K.int_shape(z_mean)[1] # by default, random_normal has mean=0 and std=1.0 epsilon = K.random_normal(shape=(batch, dim)) return z_mean + K.exp(0.5 * z_log_var) * epsilon
Example #12
Source File: diff_pool.py From spektral with MIT License | 6 votes |
def build(self, input_shape): assert isinstance(input_shape, list) F = input_shape[0][-1] if self.channels is None: self.channels = F self.kernel_emb = self.add_weight(shape=(F, self.channels), name='kernel_emb', initializer=self.kernel_initializer, regularizer=self.kernel_regularizer, constraint=self.kernel_constraint) self.kernel_pool = self.add_weight(shape=(F, self.k), name='kernel_pool', initializer=self.kernel_initializer, regularizer=self.kernel_regularizer, constraint=self.kernel_constraint) super().build(input_shape)
Example #13
Source File: BeplerModel.py From tape-neurips2019 with MIT License | 6 votes |
def convert_sequence_vocab(self, sequence): PFAM_TO_BEPLER_ENCODED = {encoding: UNIPROT_BEPLER.get(aa, 20) for aa, encoding in PFAM_VOCAB.items()} PFAM_TO_BEPLER_ENCODED[PFAM_VOCAB['<PAD>']] = 0 def to_uniprot_bepler(seq): new_seq = np.zeros_like(seq) for pfam_encoding, uniprot_encoding in PFAM_TO_BEPLER_ENCODED.items(): new_seq[seq == pfam_encoding] = uniprot_encoding return new_seq new_sequence = tf.py_func(to_uniprot_bepler, [sequence], sequence.dtype) new_sequence.set_shape(sequence.shape) return new_sequence
Example #14
Source File: yolo3_nano.py From keras-YOLOv3-model-set with MIT License | 6 votes |
def _fca_block(inputs, reduct_ratio, block_id): in_channels = inputs.shape.as_list()[-1] #in_shapes = inputs.shape.as_list()[1:3] reduct_channels = int(in_channels // reduct_ratio) prefix = 'fca_block_{}_'.format(block_id) x = GlobalAveragePooling2D(name=prefix + 'average_pooling')(inputs) x = Dense(reduct_channels, activation='relu', name=prefix + 'fc1')(x) x = Dense(in_channels, activation='sigmoid', name=prefix + 'fc2')(x) x = Reshape((1,1,in_channels),name='reshape')(x) x = Multiply(name=prefix + 'multiply')([x, inputs]) return x
Example #15
Source File: vae-mlp-mnist-8.1.1.py From Advanced-Deep-Learning-with-Keras with MIT License | 6 votes |
def sampling(args): """Reparameterization trick by sampling fr an isotropic unit Gaussian. # Arguments: args (tensor): mean and log of variance of Q(z|X) # Returns: z (tensor): sampled latent vector """ z_mean, z_log_var = args # K is the keras backend batch = K.shape(z_mean)[0] dim = K.int_shape(z_mean)[1] # by default, random_normal has mean=0 and std=1.0 epsilon = K.random_normal(shape=(batch, dim)) return z_mean + K.exp(0.5 * z_log_var) * epsilon
Example #16
Source File: attn_augconv.py From keras-attention-augmented-convs with MIT License | 6 votes |
def relative_logits(self, q): shape = K.shape(q) # [batch, num_heads, H, W, depth_v] shape = [shape[i] for i in range(5)] height = shape[2] width = shape[3] rel_logits_w = self.relative_logits_1d(q, self.key_relative_w, height, width, transpose_mask=[0, 1, 2, 4, 3, 5]) rel_logits_h = self.relative_logits_1d( K.permute_dimensions(q, [0, 1, 3, 2, 4]), self.key_relative_h, width, height, transpose_mask=[0, 1, 4, 2, 5, 3]) return rel_logits_h, rel_logits_w
Example #17
Source File: attn_augconv.py From keras-attention-augmented-convs with MIT License | 6 votes |
def split_heads_2d(self, ip): tensor_shape = K.shape(ip) # batch, height, width, channels for axis = -1 tensor_shape = [tensor_shape[i] for i in range(len(self._shape))] batch = tensor_shape[0] height = tensor_shape[1] width = tensor_shape[2] channels = tensor_shape[3] # Save the spatial tensor dimensions self._batch = batch self._height = height self._width = width ret_shape = K.stack([batch, height, width, self.num_heads, channels // self.num_heads]) split = K.reshape(ip, ret_shape) transpose_axes = (0, 3, 1, 2, 4) split = K.permute_dimensions(split, transpose_axes) return split
Example #18
Source File: RandomSequenceMask.py From tape-neurips2019 with MIT License | 6 votes |
def call(self, inputs): """ Args: sequence: tf.Tensor[int32] - Amino acid sequence, a padded tensor with shape [batch_size, MAX_PROTEIN_LENGTH] protein_length: tf.Tensor[int32] - Length of each protein in the sequence, a tensor with shape [batch_size] Output: amino_acid_probs: tf.Tensor[float32] - Probability of each type of amino acid, a tensor with shape [batch_size, MAX_PROTEIN_LENGTH, n_symbols] """ sequence = inputs['primary'] protein_length = inputs['protein_length'] sequence_mask = rk.utils.convert_sequence_length_to_sequence_mask( sequence, protein_length) masked_sequence, bert_mask = self.bert_mask(sequence, sequence_mask) inputs['original_sequence'] = sequence inputs['primary'] = masked_sequence inputs['bert_mask'] = bert_mask return inputs
Example #19
Source File: bilstm_siamese_network.py From DeepPavlov with Apache License 2.0 | 5 votes |
def _pairwise_distances(self, inputs: List[Tensor]) -> Tensor: emb_c, emb_r = inputs bs = K.shape(emb_c)[0] embeddings = K.concatenate([emb_c, emb_r], 0) dot_product = K.dot(embeddings, K.transpose(embeddings)) square_norm = K.batch_dot(embeddings, embeddings, axes=1) distances = K.transpose(square_norm) - 2.0 * dot_product + square_norm distances = distances[0:bs, bs:bs+bs] distances = K.clip(distances, 0.0, None) mask = K.cast(K.equal(distances, 0.0), K.dtype(distances)) distances = distances + mask * 1e-16 distances = K.sqrt(distances) distances = distances * (1.0 - mask) return distances
Example #20
Source File: loss.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def box_iou(b1, b2): """ Return iou tensor Parameters ---------- b1: tensor, shape=(i1,...,iN, 4), xywh b2: tensor, shape=(j, 4), xywh Returns ------- iou: tensor, shape=(i1,...,iN, j) """ # Expand dim to apply broadcasting. b1 = K.expand_dims(b1, -2) b1_xy = b1[..., :2] b1_wh = b1[..., 2:4] b1_wh_half = b1_wh/2. b1_mins = b1_xy - b1_wh_half b1_maxes = b1_xy + b1_wh_half # Expand dim to apply broadcasting. b2 = K.expand_dims(b2, 0) b2_xy = b2[..., :2] b2_wh = b2[..., 2:4] b2_wh_half = b2_wh/2. b2_mins = b2_xy - b2_wh_half b2_maxes = b2_xy + b2_wh_half intersect_mins = K.maximum(b1_mins, b2_mins) intersect_maxes = K.minimum(b1_maxes, b2_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1] b1_area = b1_wh[..., 0] * b1_wh[..., 1] b2_area = b2_wh[..., 0] * b2_wh[..., 1] iou = intersect_area / (b1_area + b2_area - intersect_area) return iou
Example #21
Source File: yolo3_nano.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def _ep_block(inputs, filters, stride, expansion, block_id): #in_channels = backend.int_shape(inputs)[-1] in_channels = inputs.shape.as_list()[-1] pointwise_conv_filters = int(filters) x = inputs prefix = 'ep_block_{}_'.format(block_id) # Expand x = Conv2D(int(expansion * in_channels), kernel_size=1, padding='same', use_bias=False, activation=None, name=prefix + 'expand')(x) x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'expand_BN')(x) x = ReLU(6., name=prefix + 'expand_relu')(x) # Depthwise if stride == 2: x = ZeroPadding2D(padding=correct_pad(K, x, 3), name=prefix + 'pad')(x) x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None, use_bias=False, padding='same' if stride == 1 else 'valid', name=prefix + 'depthwise')(x) x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'depthwise_BN')(x) x = ReLU(6., name=prefix + 'depthwise_relu')(x) # Project x = Conv2D(pointwise_conv_filters, kernel_size=1, padding='same', use_bias=False, activation=None, name=prefix + 'project')(x) x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'project_BN')(x) if in_channels == pointwise_conv_filters and stride == 1: return Add(name=prefix + 'add')([inputs, x]) return x
Example #22
Source File: postprocess.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def batched_yolo3_boxes_and_scores(feats, anchors, num_classes, input_shape, image_shape): '''Process Conv layer output''' box_xy, box_wh, box_confidence, box_class_probs = yolo3_head(feats, anchors, num_classes, input_shape) num_anchors = len(anchors) grid_shape = K.shape(feats)[1:3] # height, width total_anchor_num = grid_shape[0] * grid_shape[1] * num_anchors boxes = yolo3_correct_boxes(box_xy, box_wh, input_shape, image_shape) boxes = K.reshape(boxes, [-1, total_anchor_num, 4]) box_scores = box_confidence * box_class_probs box_scores = K.reshape(box_scores, [-1, total_anchor_num, num_classes]) return boxes, box_scores
Example #23
Source File: postprocess.py From keras-YOLOv3-model-set with MIT License | 5 votes |
def yolo3_head(feats, anchors, num_classes, input_shape, calc_loss=False): """Convert final layer features to bounding box parameters.""" num_anchors = len(anchors) # Reshape to batch, height, width, num_anchors, box_params. anchors_tensor = K.reshape(K.constant(anchors), [1, 1, 1, num_anchors, 2]) grid_shape = K.shape(feats)[1:3] # height, width grid_y = K.tile(K.reshape(K.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]), [1, grid_shape[1], 1, 1]) grid_x = K.tile(K.reshape(K.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]), [grid_shape[0], 1, 1, 1]) grid = K.concatenate([grid_x, grid_y]) grid = K.cast(grid, K.dtype(feats)) feats = K.reshape( feats, [-1, grid_shape[0], grid_shape[1], num_anchors, num_classes + 5]) # Adjust preditions to each spatial grid point and anchor size. box_xy = (K.sigmoid(feats[..., :2]) + grid) / K.cast(grid_shape[..., ::-1], K.dtype(feats)) box_wh = K.exp(feats[..., 2:4]) * anchors_tensor / K.cast(input_shape[..., ::-1], K.dtype(feats)) box_confidence = K.sigmoid(feats[..., 4:5]) box_class_probs = K.sigmoid(feats[..., 5:]) if calc_loss == True: return grid, feats, box_xy, box_wh return box_xy, box_wh, box_confidence, box_class_probs
Example #24
Source File: network.py From DeepPavlov with Apache License 2.0 | 5 votes |
def biaffine_attention(deps: tf.Tensor, heads: tf.Tensor, name="biaffine_attention") -> tf.Tensor: """Implements a trainable matching layer between two families of embeddings. Args: deps: the 3D-tensor of dependency states, heads: the 3D-tensor of head states, name: the name of a layer Returns: `answer` a 3D-tensor of pairwise scores between deps and heads """ deps_dim_int = deps.get_shape().as_list()[-1] heads_dim_int = heads.get_shape().as_list()[-1] assert deps_dim_int == heads_dim_int with tf.variable_scope(name): kernel_shape = (deps_dim_int, heads_dim_int) kernel = tf.get_variable('kernel', shape=kernel_shape, initializer=tf.initializers.identity()) first_bias = tf.get_variable('first_bias', shape=(kernel_shape[0], 1), initializer=xavier_initializer()) second_bias = tf.get_variable('second_bias', shape=(kernel_shape[1], 1), initializer=xavier_initializer()) # deps.shape = (B, L, D) # first.shape = (B, L, D), first_rie = sum_d deps_{rid} kernel_{de} first = tf.tensordot(deps, kernel, axes=[-1, -2]) answer = tf.matmul(first, heads, transpose_b=True) # answer.shape = (B, L, L) # add bias over x axis first_bias_term = tf.tensordot(deps, first_bias, axes=[-1, -2]) answer += first_bias_term # add bias over y axis second_bias_term = tf.tensordot(heads, second_bias, axes=[-1, -2]) # (B, L, 1) second_bias_term = tf.transpose(second_bias_term, [0, 2, 1]) # (B, 1, L) answer += second_bias_term return answer
Example #25
Source File: cells.py From DeepPavlov with Apache License 2.0 | 5 votes |
def build(self, input_shape): assert len(input_shape) >= 2 input_dim = input_shape[-1] self.gate_kernel = self.add_weight( shape=(input_dim, input_dim), initializer='uniform', name='gate_kernel') self.gate_bias = self.add_weight( shape=(input_dim,), initializer=self.bias_initializer, name='gate_bias') self.dense_kernel = self.add_weight( shape=(input_dim, input_dim), initializer='uniform', name='dense_kernel') self.dense_bias = self.add_weight( shape=(input_dim,), initializer=self.bias_initializer, name='dense_bias') self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim}) self.built = True
Example #26
Source File: bilstm_siamese_network.py From DeepPavlov with Apache License 2.0 | 5 votes |
def create_model(self) -> Model: if self.use_matrix: context = Input(shape=(self.max_sequence_length,)) response = Input(shape=(self.max_sequence_length,)) if self.shared_weights: emb_layer_a = self.embedding_layer() emb_layer_b = emb_layer_a else: emb_layer_a = self.embedding_layer() emb_layer_b = self.embedding_layer() emb_c = emb_layer_a(context) emb_r = emb_layer_b(response) else: context = Input(shape=(self.max_sequence_length, self.embedding_dim,)) response = Input(shape=(self.max_sequence_length, self.embedding_dim,)) emb_c = context emb_r = response if self.shared_weights: lstm_layer_a = self.lstm_layer() lstm_layer_b = lstm_layer_a else: lstm_layer_a = self.lstm_layer() lstm_layer_b = self.lstm_layer() lstm_c = lstm_layer_a(emb_c) lstm_r = lstm_layer_b(emb_r) if self.pooling: pooling_layer = GlobalMaxPooling1D(name="sentence_embedding") lstm_c = pooling_layer(lstm_c) lstm_r = pooling_layer(lstm_r) if self.triplet_mode: dist = Lambda(self._pairwise_distances)([lstm_c, lstm_r]) else: dist = Lambda(self._diff_mult_dist)([lstm_c, lstm_r]) dist = Dense(1, activation='sigmoid', name="score_model")(dist) model = Model([context, response], dist) return model
Example #27
Source File: BeplerModel.py From tape-neurips2019 with MIT License | 5 votes |
def _generate_bert_mask(self, inputs): mask_shape = K.shape(inputs) bert_mask = K.random_uniform(mask_shape) < self.percentage return bert_mask
Example #28
Source File: conv_models.py From bootcamp with Apache License 2.0 | 5 votes |
def _train(): # x = np.random.uniform(size=(6, 32, 64, 4)) # 6 is multiple of 3. # y_softmax = np.random.uniform(size=(6, 100)) # dsm = DeepSpeakerModel(batch_input_shape=(None, 32, 64, 4), include_softmax=True, num_speakers_softmax=100) # dsm.m.compile(optimizer=Adam(lr=0.01), loss='categorical_crossentropy') # print(dsm.m.predict(x).shape) # print(dsm.m.evaluate(x, y_softmax)) # w = dsm.get_weights() dsm = DeepSpeakerModel(batch_input_shape=(None, 32, 64, 4), include_softmax=False) # dsm.m.set_weights(w) dsm.m.compile(optimizer=Adam(lr=0.01), loss=deep_speaker_loss) # it works!!!!!!!!!!!!!!!!!!!! # unit_batch_size = 20 # anchor = np.ones(shape=(unit_batch_size, 32, 64, 4)) # positive = np.array(anchor) # negative = np.ones(shape=(unit_batch_size, 32, 64, 4)) * (-1) # batch = np.vstack((anchor, positive, negative)) # x = batch # y = np.zeros(shape=(len(batch), 512)) # not important. # print('Starting to fit...') # while True: # print(dsm.m.train_on_batch(x, y)) # should not work... and it does not work! unit_batch_size = 20 negative = np.ones(shape=(unit_batch_size, 32, 64, 4)) * (-1) batch = np.vstack((negative, negative, negative)) x = batch y = np.zeros(shape=(len(batch), 512)) # not important. print('Starting to fit...') while True: print(dsm.m.train_on_batch(x, y))
Example #29
Source File: conv_models.py From bootcamp with Apache License 2.0 | 5 votes |
def __init__(self, batch_input_shape=(None, NUM_FRAMES, NUM_FBANKS, 1), include_softmax=False, num_speakers_softmax=None): self.include_softmax = include_softmax if self.include_softmax: assert num_speakers_softmax > 0 self.clipped_relu_count = 0 # http://cs231n.github.io/convolutional-networks/ # conv weights # #params = ks * ks * nb_filters * num_channels_input # Conv128-s # 5*5*128*128/2+128 # ks*ks*nb_filters*channels/strides+bias(=nb_filters) # take 100 ms -> 4 frames. # if signal is 3 seconds, then take 100ms per 100ms and average out this network. # 8*8 = 64 features. # used to share all the layers across the inputs # num_frames = K.shape() - do it dynamically after. inputs = Input(batch_shape=batch_input_shape, name='input') x = self.cnn_component(inputs) x = Reshape((-1, 2048))(x) # Temporal average layer. axis=1 is time. x = Lambda(lambda y: K.mean(y, axis=1), name='average')(x) if include_softmax: logger.info('Including a Dropout layer to reduce overfitting.') # used for softmax because the dataset we pre-train on might be too small. easy to overfit. x = Dropout(0.5)(x) x = Dense(512, name='affine')(x) if include_softmax: # Those weights are just when we train on softmax. x = Dense(num_speakers_softmax, activation='softmax')(x) else: # Does not contain any weights. x = Lambda(lambda y: K.l2_normalize(y, axis=1), name='ln')(x) self.m = Model(inputs, x, name='ResCNN')
Example #30
Source File: backend.py From bert4keras with Apache License 2.0 | 5 votes |
def divisible_temporal_padding(x, n): """将一维向量序列右padding到长度能被n整除 """ r_len = K.shape(x)[1] % n p_len = K.switch(r_len > 0, n - r_len, 0) return K.temporal_padding(x, (0, p_len))