當前位置: 首頁>>代碼示例>>Python>>正文


Python backend.l2_normalize方法代碼示例

本文整理匯總了Python中keras.backend.l2_normalize方法的典型用法代碼示例。如果您正苦於以下問題:Python backend.l2_normalize方法的具體用法?Python backend.l2_normalize怎麽用?Python backend.l2_normalize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在keras.backend的用法示例。


在下文中一共展示了backend.l2_normalize方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_model_41

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def get_model_41(params):
    embedding_weights = pickle.load(open("../data/datasets/train_data/embedding_weights_w2v-google_MSD-AG.pk","rb"))
    # main sequential model
    model = Sequential()
    model.add(Embedding(len(embedding_weights[0]), params['embedding_dim'], input_length=params['sequence_length'],
                        weights=embedding_weights))
    #model.add(Dropout(params['dropout_prob'][0], input_shape=(params['sequence_length'], params['embedding_dim'])))
    model.add(LSTM(2048))
    #model.add(Dropout(params['dropout_prob'][1]))
    model.add(Dense(output_dim=params["n_out"], init="uniform"))
    model.add(Activation(params['final_activation']))
    logging.debug("Output CNN: %s" % str(model.output_shape))

    if params['final_activation'] == 'linear':
        model.add(Lambda(lambda x :K.l2_normalize(x, axis=1)))

    return model


# CRNN Arch for audio 
開發者ID:sergiooramas,項目名稱:tartarus,代碼行數:22,代碼來源:models.py

示例2: call

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def call(self, input_tensor, mask=None):
        z_s = input_tensor[0]
        z_n = input_tensor[1]
        r_s = input_tensor[2]

        z_s = K.l2_normalize(z_s, axis=-1)
        z_n = K.l2_normalize(z_n, axis=-1)
        r_s = K.l2_normalize(r_s, axis=-1)

        steps = z_n.shape[1]

        pos = K.sum(z_s * r_s, axis=-1, keepdims=True)
        pos = K.repeat_elements(pos, steps, axis=1)
        r_s = K.expand_dims(r_s, axis=-2)
        r_s = K.repeat_elements(r_s, steps, axis=1)
        neg = K.sum(z_n * r_s, axis=-1)

        loss = K.cast(K.sum(K.maximum(0., (1. - pos + neg)), axis=-1, keepdims=True), K.floatx())
        return loss 
開發者ID:madrugado,項目名稱:Attention-Based-Aspect-Extraction,代碼行數:21,代碼來源:my_layers.py

示例3: _compute_energy

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def _compute_energy(self, stm):
        # "concat" energy function
        # energy_i = g * V / |V| * tanh([stm, h_i] * W + b) + r
        _stm = K.dot(stm, self.W_a)

        V_a = self.V_a
        if self.normalize_energy:
            V_a = self.Energy_g * K.l2_normalize(self.V_a)

        et = K.dot(activations.tanh(K.expand_dims(_stm, axis=1) + self._uxpb),
                   K.expand_dims(V_a))

        if self.is_monotonic:
            et += self.Energy_r

        return et 
開發者ID:asmekal,項目名稱:keras-monotonic-attention,代碼行數:18,代碼來源:attention_decoder.py

示例4: call

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def call(self, inputs: list, **kwargs) -> typing.Any:
        """
        The computation logic of MatchingTensorLayer.

        :param inputs: two input tensors.
        """
        x1 = inputs[0]
        x2 = inputs[1]
        # Normalize x1 and x2
        if self._normalize:
            x1 = K.l2_normalize(x1, axis=2)
            x2 = K.l2_normalize(x2, axis=2)

        # b = batch size
        # l = length of `x1`
        # r = length of `x2`
        # d, e = embedding size
        # c = number of channels
        # output = [b, c, l, r]
        output = tf.einsum(
            'bld,cde,bre->bclr',
            x1, self.interaction_matrix, x2
        )
        return output 
開發者ID:NTMC-Community,項目名稱:MatchZoo,代碼行數:26,代碼來源:matching_tensor_layer.py

示例5: call

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def call(self, inputs):
        x1 = inputs[0]
        x2 = inputs[1]
        if self.match_type in ['dot']:
            if self.normalize:
                x1 = K.l2_normalize(x1, axis=2)
                x2 = K.l2_normalize(x2, axis=2)
            output = K.tf.einsum('abd,acd->abc', x1, x2)
            output = K.tf.expand_dims(output, 3)
        elif self.match_type in ['mul', 'plus', 'minus']:
            x1_exp = K.tf.stack([x1] * self.shape2[1], 2)
            x2_exp = K.tf.stack([x2] * self.shape1[1], 1)
            if self.match_type == 'mul':
                output = x1_exp * x2_exp
            elif self.match_type == 'plus':
                output = x1_exp + x2_exp
            elif self.match_type == 'minus':
                output = x1_exp - x2_exp
        elif self.match_type in ['concat']:
            x1_exp = K.tf.stack([x1] * self.shape2[1], axis=2)
            x2_exp = K.tf.stack([x2] * self.shape1[1], axis=1)
            output = K.tf.concat([x1_exp, x2_exp], axis=3)

        return output 
開發者ID:yangliuy,項目名稱:NeuralResponseRanking,代碼行數:26,代碼來源:Match.py

示例6: call

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def call(self, x, mask=None):
        uit = K.tanh(K.dot(x, self.Ws1))
        ait = K.dot(uit, self.Ws2)
        ait = K.permute_dimensions(ait, (0, 2, 1))
        A = K.softmax(ait, axis=1)
        M = K.batch_dot(A, x)
        if self.punish:
            A_T = K.permute_dimensions(A, (0, 2, 1))
            tile_eye = K.tile(K.eye(self.weight_ws2), [self.batch_size, 1])
            tile_eye = K.reshape(
                tile_eye, shape=[-1, self.weight_ws2, self.weight_ws2])
            AA_T = K.batch_dot(A, A_T) - tile_eye
            P = K.l2_normalize(AA_T, axis=(1, 2))
            return M, P
        else:
            return M 
開發者ID:stevewyl,項目名稱:nlp_toolkit,代碼行數:18,代碼來源:multi_dim_attention.py

示例7: call

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def call(self, inputs):  # pylint:disable=arguments-differ
        """This is where the layer's logic lives.

        Parameters
        ----------
        inputs: tensor
            Input tensor, or list/tuple of input tensors
        kwargs: dict
            Additional keyword arguments

        Returns
        -------
        tensor
            A tensor or list/tuple of tensors
        """
        return K.l2_normalize(inputs, self.axis) 
開發者ID:deepfakes,項目名稱:faceswap,代碼行數:18,代碼來源:layers.py

示例8: build_siamese_resnet_18

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def build_siamese_resnet_18(input_shape, num_outputs):
        channels, height, width = input_shape
        branch_channels = 3 #channels / 2
        branch_input_shape = (branch_channels, height, width)
        branch = ResnetBuilder.build_resnet_18(branch_input_shape, NUM_EMBEDDING, False)
        input = Input(shape=(height, width, channels))
        first_branch = branch(Lambda(lambda x: x[:, :, :, :3])(input))
        second_branch = branch(Lambda(lambda x: x[:, :, :, 3:])(input))
        if NORMALIZATION_ON:
            first_branch = Lambda(lambda x: K.l2_normalize(x, axis=1))(first_branch)
            second_branch = Lambda(lambda x: K.l2_normalize(x, axis=1))(second_branch) 
        
        raw_result = concatenate([first_branch, second_branch])
        output = _top_network(raw_result)
        
        # raw_result = dot([first_branch, second_branch], axes=1)
        # result = Lambda(lambda x: (K.clip(x, 0.5, 1) - 0.5) * 2.0)(raw_result)
        # negated_result = Lambda(lambda x: 1 - x)(result)
        # output = concatenate([negated_result, result])
        
        return Model(inputs=input, outputs=output) 
開發者ID:nsavinov,項目名稱:SPTM,代碼行數:23,代碼來源:resnet.py

示例9: call

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def call(self, x):
        # feat : bz x W x H x D, cluster_score: bz X W x H x clusters.
        feat, cluster_score = x
        num_features = feat.shape[-1]

        # softmax normalization to get soft-assignment.
        # A : bz x W x H x clusters
        max_cluster_score = K.max(cluster_score, -1, keepdims=True)
        exp_cluster_score = K.exp(cluster_score - max_cluster_score)
        A = exp_cluster_score / K.sum(exp_cluster_score, axis=-1, keepdims = True)

        # Now, need to compute the residual, self.cluster: clusters x D
        A = K.expand_dims(A, -1)    # A : bz x W x H x clusters x 1
        feat_broadcast = K.expand_dims(feat, -2)    # feat_broadcast : bz x W x H x 1 x D
        feat_res = feat_broadcast - self.cluster    # feat_res : bz x W x H x clusters x D
        weighted_res = tf.multiply(A, feat_res)     # weighted_res : bz x W x H x clusters x D
        cluster_res = K.sum(weighted_res, [1, 2])

        if self.mode == 'gvlad':
            cluster_res = cluster_res[:, :self.k_centers, :]

        cluster_l2 = K.l2_normalize(cluster_res, -1)
        outputs = K.reshape(cluster_l2, [-1, int(self.k_centers) * int(num_features)])
        return outputs 
開發者ID:taylorlu,項目名稱:Speaker-Diarization,代碼行數:26,代碼來源:model.py

示例10: cosine

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def cosine(a, b):
    """Cosine similarity. Maximum is 1 (a == b), minimum is -1 (a == -b)."""

    a = K.l2_normalize(a)
    b = K.l2_normalize(b)
    return 1 - K.mean(a * b, axis=-1) 
開發者ID:codekansas,項目名稱:gandlf,代碼行數:8,代碼來源:similarities.py

示例11: get_model_3

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def get_model_3(params):

    # metadata
    inputs2 = Input(shape=(params["n_metafeatures"],))
    x2 = Dropout(params["dropout_factor"])(inputs2)

    if params["n_dense"] > 0:
        dense2 = Dense(output_dim=params["n_dense"], init="uniform", activation='relu')
        x2 = dense2(x2)
        logging.debug("Output CNN: %s" % str(dense2.output_shape))

        x2 = Dropout(params["dropout_factor"])(x2)

    if params["n_dense_2"] > 0:
        dense3 = Dense(output_dim=params["n_dense_2"], init="uniform", activation='relu')
        x2 = dense3(x2)
        logging.debug("Output CNN: %s" % str(dense3.output_shape))

        x2 = Dropout(params["dropout_factor"])(x2)

    dense4 = Dense(output_dim=params["n_out"], init="uniform", activation=params['final_activation'])
    xout = dense4(x2)
    logging.debug("Output CNN: %s" % str(dense4.output_shape))

    if params['final_activation'] == 'linear':
        reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
        xout = reg(xout)

    model = Model(input=inputs2, output=xout)

    return model


# Metadata 2 inputs, post-merge with dense layers 
開發者ID:sergiooramas,項目名稱:tartarus,代碼行數:36,代碼來源:models.py

示例12: get_model_32

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def get_model_32(params):

    # metadata
    inputs = Input(shape=(params["n_metafeatures"],))
    reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x1 = reg(inputs)

    inputs2 = Input(shape=(params["n_metafeatures2"],))
    reg2 = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x2 = reg2(inputs2)

    # merge
    x = merge([x1, x2], mode='concat', concat_axis=1)

    x = Dropout(params["dropout_factor"])(x)

    if params['n_dense'] > 0:
        dense2 = Dense(output_dim=params["n_dense"], init="uniform", activation='relu')
        x = dense2(x)
        logging.debug("Output CNN: %s" % str(dense2.output_shape))

    dense4 = Dense(output_dim=params["n_out"], init="uniform", activation=params['final_activation'])
    xout = dense4(x)
    logging.debug("Output CNN: %s" % str(dense4.output_shape))

    if params['final_activation'] == 'linear':
        reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
        xout = reg(xout)

    model = Model(input=[inputs,inputs2], output=xout)

    return model

# Metadata 3 inputs, pre-merge and l2 
開發者ID:sergiooramas,項目名稱:tartarus,代碼行數:36,代碼來源:models.py

示例13: get_model_33

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def get_model_33(params):

    # metadata
    inputs = Input(shape=(params["n_metafeatures"],))
    reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x1 = reg(inputs)

    inputs2 = Input(shape=(params["n_metafeatures2"],))
    reg2 = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x2 = reg2(inputs2)

    inputs3 = Input(shape=(params["n_metafeatures3"],))
    reg3 = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x3 = reg3(inputs3)

    # merge
    x = merge([x1, x2, x3], mode='concat', concat_axis=1)

    x = Dropout(params["dropout_factor"])(x)

    if params['n_dense'] > 0:
        dense2 = Dense(output_dim=params["n_dense"], init="uniform", activation='relu')
        x = dense2(x)
        logging.debug("Output CNN: %s" % str(dense2.output_shape))

    dense4 = Dense(output_dim=params["n_out"], init="uniform", activation=params['final_activation'])
    xout = dense4(x)
    logging.debug("Output CNN: %s" % str(dense4.output_shape))

    if params['final_activation'] == 'linear':
        reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
        xout = reg(xout)

    model = Model(input=[inputs,inputs2,inputs3], output=xout)

    return model


# Metadata 4 inputs, pre-merge and l2 
開發者ID:sergiooramas,項目名稱:tartarus,代碼行數:41,代碼來源:models.py

示例14: get_model_34

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def get_model_34(params):

    # metadata
    inputs = Input(shape=(params["n_metafeatures"],))
    reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x1 = reg(inputs)

    inputs2 = Input(shape=(params["n_metafeatures2"],))
    reg2 = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x2 = reg2(inputs2)

    inputs3 = Input(shape=(params["n_metafeatures3"],))
    reg3 = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x3 = reg3(inputs3)

    inputs4 = Input(shape=(params["n_metafeatures4"],))
    reg4 = Lambda(lambda x :K.l2_normalize(x, axis=1))
    x4 = reg4(inputs4)

    # merge
    x = merge([x1, x2, x3, x4], mode='concat', concat_axis=1)

    x = Dropout(params["dropout_factor"])(x)

    if params['n_dense'] > 0:
        dense2 = Dense(output_dim=params["n_dense"], init="uniform", activation='relu')
        x = dense2(x)
        logging.debug("Output CNN: %s" % str(dense2.output_shape))

    dense4 = Dense(output_dim=params["n_out"], init="uniform", activation=params['final_activation'])
    xout = dense4(x)
    logging.debug("Output CNN: %s" % str(dense4.output_shape))

    if params['final_activation'] == 'linear':
        reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
        xout = reg(xout)

    model = Model(input=[inputs,inputs2,inputs3,inputs4], output=xout)

    return model 
開發者ID:sergiooramas,項目名稱:tartarus,代碼行數:42,代碼來源:models.py

示例15: get_model_6

# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import l2_normalize [as 別名]
def get_model_6(params):

    # metadata
    inputs2 = Input(shape=(params["n_metafeatures"],))
    #x2 = Dropout(params["dropout_factor"])(inputs2)

    if params["n_dense"] > 0:
        dense21 = Dense(output_dim=params["n_dense"], init="uniform", activation='relu')
        x21 = dense21(inputs2)
        logging.debug("Output CNN: %s" % str(dense21.output_shape))

        dense22 = Dense(output_dim=params["n_dense"], init="uniform", activation='tanh')
        x22 = dense22(inputs2)
        logging.debug("Output CNN: %s" % str(dense22.output_shape))

        dense23 = Dense(output_dim=params["n_dense"], init="uniform", activation='sigmoid')
        x23 = dense23(inputs2)
        logging.debug("Output CNN: %s" % str(dense23.output_shape))

        # merge
        x = merge([x21, x22, x23], mode='concat', concat_axis=1)
        x2 = Dropout(params["dropout_factor"])(x)

    if params["n_dense_2"] > 0:
        dense3 = Dense(output_dim=params["n_dense_2"], init="uniform", activation='relu')
        x2 = dense3(x2)
        logging.debug("Output CNN: %s" % str(dense3.output_shape))

        x2 = Dropout(params["dropout_factor"])(x2)

    dense4 = Dense(output_dim=params["n_out"], init="uniform", activation=params['final_activation'])
    xout = dense4(x2)
    logging.debug("Output CNN: %s" % str(dense4.output_shape))

    if params['final_activation'] == 'linear':
        reg = Lambda(lambda x :K.l2_normalize(x, axis=1))
        xout = reg(xout)

    model = Model(input=inputs2, output=xout)

    return model 
開發者ID:sergiooramas,項目名稱:tartarus,代碼行數:43,代碼來源:models.py


注:本文中的keras.backend.l2_normalize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。