Beispiel #1
0
    def build(self):
        '''
        1. Build Code Representation Model
        '''
        logger.debug('Building Code Representation Model')
        methname = Input(shape=(self.data_params['methname_len'],), dtype='int32', name='methname')
        apiseq= Input(shape=(self.data_params['apiseq_len'],),dtype='int32',name='apiseq')
        tokens=Input(shape=(self.data_params['tokens_len'],),dtype='int32',name='tokens')

        ## method name representation ##
        #1.embedding
        init_emb_weights = np.load(self.config['workdir']+self.model_params['init_embed_weights_methname']) if self.model_params['init_embed_weights_methname'] is not None else None
        init_emb_weights = init_emb_weights if init_emb_weights is None else [init_emb_weights]
        embedding = Embedding(input_dim=self.data_params['n_words'],
                              output_dim=self.model_params.get('n_embed_dims', 100),
                              weights=init_emb_weights,
                              mask_zero=False,#Whether 0 in the input is a special "padding" value that should be masked out. 
                              #If set True, all subsequent layers in the model must support masking, otherwise an exception will be raised.
                              name='embedding_methname')
        methname_embedding = embedding(methname)
        dropout = Dropout(0.25,name='dropout_methname_embed')
        methname_dropout = dropout(methname_embedding)

        methname_conv1 = Conv1D(100,2,padding='valid', activation='relu',strides=1,name='methname_conv1')
        methname_conv2 = Conv1D(100,3,padding='valid', activation='relu',strides=1,name='methname_conv2')
        methname_conv3 = Conv1D(100,4,padding='valid', activation='relu',strides=1,name='methname_conv3')
        methname_conv1_out = methname_conv1(methname_dropout)
        methname_conv2_out = methname_conv2(methname_dropout)
        methname_conv3_out = methname_conv3(methname_dropout)
        dropout = Dropout(0.25,name='dropout_methname_conv')
        methname_conv1_dropout = dropout(methname_conv1_out)
        methname_conv2_dropout = dropout(methname_conv2_out)
        methname_conv3_dropout = dropout(methname_conv3_out)
        merged_methname= Concatenate(name='methname_merge',axis=1)([methname_conv1_dropout,methname_conv2_dropout,methname_conv3_dropout])


        ## API Sequence Representation ##
        #1.embedding
        init_emb_weights = np.load(self.config['workdir']+self.model_params['init_embed_weights_api']) if self.model_params['init_embed_weights_api'] is not None else None
        init_emb_weights = init_emb_weights if init_emb_weights is None else [init_emb_weights]
        embedding = Embedding(input_dim=self.data_params['n_words'],
                              output_dim=self.model_params.get('n_embed_dims', 100),
                              #weights=weights,
                              mask_zero=False,#Whether 0 in the input is a special "padding" value that should be masked out. 
                                         #If set True, all subsequent layers must support masking, otherwise an exception will be raised.
                              name='embedding_apiseq')
        apiseq_embedding = embedding(apiseq)
        dropout = Dropout(0.25,name='dropout_apiseq_embed')
        apiseq_dropout = dropout(apiseq_embedding)

        api_conv1 = Conv1D(100,2,padding='valid', activation='relu',strides=1,name='api_conv1')
        api_conv2 = Conv1D(100,3,padding='valid', activation='relu',strides=1,name='api_conv2')
        api_conv3 = Conv1D(100,4,padding='valid', activation='relu',strides=1,name='api_conv3')
        api_conv1_out = api_conv1(apiseq_dropout)
        api_conv2_out = api_conv2(apiseq_dropout)
        api_conv3_out = api_conv3(apiseq_dropout)
        dropout = Dropout(0.25,name='dropout_api_conv')
        api_conv1_dropout = dropout(api_conv1_out)
        api_conv2_dropout = dropout(api_conv2_out)
        api_conv3_dropout = dropout(api_conv3_out)
        merged_api= Concatenate(name='api_merge',axis=1)([api_conv1_dropout,api_conv2_dropout,api_conv3_dropout])


        ## Tokens Representation ##
        #1.embedding
        init_emb_weights = np.load(self.config['workdir']+self.model_params['init_embed_weights_tokens']) if self.model_params['init_embed_weights_tokens'] is not None else None
        init_emb_weights = init_emb_weights if init_emb_weights is None else [init_emb_weights]
        embedding = Embedding(input_dim=self.data_params['n_words'],
                              output_dim=self.model_params.get('n_embed_dims', 100),
                              weights=init_emb_weights,
                              #mask_zero=True,#Whether 0 in the input is a special "padding" value that should be masked out. 
                              #If set True, all subsequent layers must support masking, otherwise an exception will be raised.
                              name='embedding_tokens')
        tokens_embedding = embedding(tokens)
        dropout = Dropout(0.25,name='dropout_tokens_embed')
        tokens_dropout= dropout(tokens_embedding)
        tokens_conv1 = Conv1D(100,2,padding='valid', activation='relu',strides=1,name='tokens_conv1')
        tokens_conv2 = Conv1D(100,3,padding='valid', activation='relu',strides=1,name='tokens_conv2')
        tokens_conv3 = Conv1D(100,4,padding='valid', activation='relu',strides=1,name='tokens_conv3')
        tokens_conv1_out = tokens_conv1(tokens_dropout)
        tokens_conv2_out = tokens_conv2(tokens_dropout)
        tokens_conv3_out = tokens_conv3(tokens_dropout)
        dropout = Dropout(0.25,name='dropout_tokens_conv')
        tokens_conv1_dropout = dropout(tokens_conv1_out)
        tokens_conv2_dropout = dropout(tokens_conv2_out)
        tokens_conv3_dropout = dropout(tokens_conv3_out)
        merged_tokens= Concatenate(name='tokens_merge',axis=1)([tokens_conv1_dropout,tokens_conv2_dropout,tokens_conv3_dropout])

        # merge code#
        merged_code= Concatenate(name='code_merge',axis=1)([merged_methname,merged_api,merged_tokens])   #(122,200)


        '''
        2. Build Desc Representation Model
        '''
        ## Desc Representation ##
        logger.debug('Building Desc Representation Model')
        desc = Input(shape=(self.data_params['desc_len'],), dtype='int32', name='desc')
        #1.embedding
        init_emb_weights = np.load(self.config['workdir']+self.model_params['init_embed_weights_desc']) if self.model_params['init_embed_weights_desc'] is not None else None
        init_emb_weights = init_emb_weights if init_emb_weights is None else [init_emb_weights]
        embedding = Embedding(input_dim=self.data_params['n_words'],
                              output_dim=self.model_params.get('n_embed_dims', 100),
                              weights=init_emb_weights,
                              mask_zero=False,#Whether 0 in the input is a special "padding" value that should be masked out. 
                                      #If set True, all subsequent layers must support masking, otherwise an exception will be raised.
                              name='embedding_desc')
        desc_embedding = embedding(desc)
        dropout = Dropout(0.25,name='dropout_desc_embed')
        desc_dropout = dropout(desc_embedding)1

        desc_conv1 = Conv1D(100,2,padding='valid', activation='relu',strides=1,name='desc_conv1')
        desc_conv2 = Conv1D(100,3,padding='valid', activation='relu',strides=1,name='desc_conv2')
        desc_conv3 = Conv1D(100,4,padding='valid', activation='relu',strides=1,name='desc_conv3')
        desc_conv1_out = desc_conv1(desc_dropout)
        desc_conv2_out = desc_conv2(desc_dropout)
        desc_conv3_out = desc_conv3(desc_dropout)
        dropout = Dropout(0.25,name='dropout_desc_conv')
        desc_conv1_dropout = dropout(desc_conv1_out)
        desc_conv2_dropout = dropout(desc_conv2_out)
        desc_conv3_dropout = dropout(desc_conv3_out)
        merged_desc= Concatenate(name='desc_merge',axis=1)([desc_conv1_dropout,desc_conv2_dropout,desc_conv3_dropout])


       

        #AP networks#


        attention = AttentionLayer(name='attention_layer') 
        attention_out = attention([merged_code,merged_desc])


        gmp_1=GlobalMaxPooling1D(name='blobalmaxpool_colum')
        att_1=gmp_1(attention_out)
        activ1=Activation('softmax',name='AP_active_colum')
        att_1_next=activ1(att_1)
        dot1=Dot(axes=1,normalize=False,name='column_dot')
        desc_out = dot1([att_1_next, merged_desc])


        attention_trans_layer = Lambda(lambda x: K.permute_dimensions(x, (0,2,1)),name='trans_attention')
        attention_transposed = attention_trans_layer(attention_out)
        gmp_2=GlobalMaxPooling1D(name='blobalmaxpool_row')
        att_2=gmp_2(attention_transposed)
        activ2=Activation('softmax',name='AP_active_row')
        att_2_next=activ2(att_2)
        dot2=Dot(axes=1,normalize=False,name='row_dot')
        code_out = dot2([att_2_next, merged_code])

        self._code_repr_model=Model(inputs=[methname,apiseq,tokens,desc],outputs=[code_out],name='desc_repr_model')

        print('\nsummary of code representation model')
        self._code_repr_model.summary()
        fname=self.config['workdir']+'models/'+self.model_params['model_name']+'/_desc_repr_model.png'

        self._desc_repr_model=Model(inputs=[methname,apiseq,tokens,desc],outputs=[desc_out],name='code_repr_model')

        print('\nsummary of description representation model')
        self._desc_repr_model.summary()
  

        
        
        """
        3: calculate the cosine similarity between code and desc
        """     
        logger.debug('Building similarity model') 
        code_repr=self._code_repr_model([methname,apiseq,tokens,desc])
        desc_repr=self._desc_repr_model([methname,apiseq,tokens,desc])
        cos_sim=Dot(axes=1, normalize=True, name='cos_sim')([code_repr, desc_repr])
        
        sim_model = Model(inputs=[methname,apiseq,tokens,desc], outputs=[cos_sim],name='sim_model')   
        self._sim_model=sim_model  #for model evaluation  
        print ("\nsummary of similarity model")
        self._sim_model.summary() 
        fname=self.config['workdir']+'models/'+self.model_params['model_name']+'/_sim_model.png'

        
        
        '''
        4:Build training model
        '''
        good_sim = sim_model([self.methname,self.apiseq,self.tokens, self.desc_good])# similarity of good output
        bad_sim = sim_model([self.methname,self.apiseq,self.tokens, self.desc_bad])#similarity of bad output
        loss = Lambda(lambda x: K.maximum(1e-6, self.model_params['margin'] - x[0] + x[1]),
                     output_shape=lambda x: x[0], name='loss')([good_sim, bad_sim])

        logger.debug('Building training model')
        self._training_model=Model(inputs=[self.methname,self.apiseq,self.tokens,self.desc_good,self.desc_bad],
                                   outputs=[loss],name='training_model')
        print ('\nsummary of training model')
        self._training_model.summary()      
        fname=self.config['workdir']+'models/'+self.model_params['model_name']+'/_training_model.png'
relative_e2_embedding = Embedding(input_dim=PF_EMBEDDING_LENGTH,
                                  output_dim=PF_EMBEDDING_DIM,
                                  input_length=FIXED_SIZE,
                                  trainable=True)(relative_e2_input)

embedding_output = concatenate([
    word_embedding, pos_embedding, relative_e1_embedding, relative_e2_embedding
],
                               axis=2)

gru_layer = Bidirectional(GRU(units=200, return_sequences=True, dropout=0.6))

# (None, 96, 400)
gru_output = gru_layer(embedding_output)

attention_layer = AttentionLayer(80)

output = attention_layer(gru_output)

# output = Dropout(0.4)(output)

output = Dense(RELATION_COUNT, activation="softmax")(output)

model = Model(inputs=[
    word_input,
    relative_e1_input,
    relative_e2_input,
    pos_tag_input,
],
              outputs=[output])
Beispiel #3
0
    def createModel(self):
        Vi_1 = Input((self.video_height, self.video_width, 3 * self.F),
                     name='Vi_1')
        CNN1 = Conv2D(64,
                      kernel_size=(3, 3),
                      strides=(2, 2),
                      dilation_rate=(1, 1),
                      padding='same',
                      activation='relu')(Vi_1)
        (CNN1) = Dropout(0.7)(CNN1)
        CNN2 = Conv2D(128,
                      kernel_size=(3, 3),
                      strides=(2, 2),
                      dilation_rate=(1, 1),
                      padding='same',
                      activation='relu')(CNN1)
        (CNN2) = Dropout(0.7)(CNN2)
        CNN3 = Conv2D(256,
                      kernel_size=(3, 3),
                      strides=(1, 1),
                      dilation_rate=(2, 2),
                      padding='same',
                      activation='relu')(CNN2)
        (CNN3) = Dropout(0.7)(CNN3)
        CNN = Conv2D(512,
                     kernel_size=(3, 3),
                     strides=(1, 1),
                     dilation_rate=(4, 4),
                     padding='same',
                     activation='relu')(CNN3)

        Ei = Input((100, ), name='Ei')

        # CONCATINATE WITH Embeddings
        repli = RepeatVector(32)(Ei)
        repli = Reshape((32 * 100, ))(repli)
        repli = RepeatVector(32)(repli)
        repli = Reshape((32, 32, 100))(repli)

        merged = concatenate([CNN, repli])
        backbone = Conv2D(2,
                          kernel_size=(1, 1),
                          padding='same',
                          activation='relu')(merged)

        # FULLY CONV. LOCATION MLP
        Ploc1 = Conv2D(256,
                       kernel_size=(1, 1),
                       padding='same',
                       activation='relu')(merged)
        (Ploc1) = Dropout(0.7)(Ploc1)
        Ploc2 = Conv2D(128,
                       kernel_size=(1, 1),
                       padding='same',
                       activation='relu')(Ploc1)
        (Ploc2) = Dropout(0.7)(Ploc2)
        #Ploc3 = Conv2D(128, kernel_size=(1, 1), padding='same', activation='relu')(Ploc2)
        Ploc4 = Conv2D(self.F,
                       kernel_size=(1, 1),
                       padding='same',
                       activation='sigmoid')(Ploc2)
        (Ploc4) = Dropout(0.7)(Ploc4)
        # UPSAMPLING FOR LOCATION OUTPUT
        Ploc = UpSampling2D(size=(4, 4),
                            data_format=None,
                            interpolation='bilinear')(Ploc4)

        # CHANNEL MAXPOOLING AND MERGE WITH CNN
        Max = Lambda(self.channelPool)(Ploc4)
        Avg = Lambda(self.channelAvg)(backbone)
        attention = AttentionLayer()([Avg, Max])

        #attention = Flatten()(attention)
        attention = Reshape((32 * 32, ))(attention)

        # SCALE MLP
        mu1 = Dense(256, activation='relu')(attention)
        (mu1) = Dropout(0.7)(mu1)
        mu2 = Dense(128, activation='relu')(mu1)
        (mu2) = Dropout(0.7)(mu2)
        mu = Dense(150, activation='sigmoid')(mu2)

        # MODEL SUMMARY
        self.model = Model(inputs=[Vi_1, Ei], outputs=[mu, Ploc])
        self.model.summary()
        plot_model(self.model,
                   to_file='layoutcomposer_model.png',
                   show_shapes=True)

        # COMPILE MODEL
        opt = Adam(lr=0.001, decay=0.5, amsgrad=False)  #weight decay 0.0001
        self.model.compile(optimizer=opt,
                           loss={
                               'dense_3': self.loss2,
                               'up_sampling2d_1': 'mse'
                           },
                           loss_weights=[1, 1],
                           metrics=['accuracy'])
Beispiel #4
0
    return (shape1[0], 1)


# input =  Input(shape=(50,100), dtype='float32')

conv_4 = Conv1D(300, 4, padding='same', activation='relu', strides=1)
# shared = Model(input, conv_4)

input_1 = Input(shape=(20, 100), dtype='float32')
input_2 = Input(shape=(15, 100), dtype='float32')

out_1 = conv_4(input_1)
out_2 = conv_4(input_2)
print 'out1 shape...', K.int_shape(out_1)
print 'out2 shape...', K.int_shape(out_2)
attention = AttentionLayer()([out_1, out_2])

# out_1 column wise
att_1 = GlobalMaxPooling1D()(attention)
att_1 = Activation('softmax')(att_1)
print 'attention shape', K.int_shape(att_1)
att_1 = Lambda(lambda x: K.expand_dims(x, 2))(att_1)
out1 = dot([att_1, out_2], axes=1)

# out_2 row wise
attention_transposed = Lambda(lambda x: K.permute_dimensions(x, (0, 2, 1)))(
    attention)
att_2 = GlobalMaxPooling1D()(attention_transposed)
att_2 = Activation('softmax')(att_2)
att_2 = Lambda(lambda x: K.expand_dims(x, 2))(att_2)
out2 = dot([att_2, out_1], axes=1)
Beispiel #5
0
    def build(self):
        source_words = Input(shape=(None, ), dtype='int32')
        target_words = Input(shape=(None, ), dtype='int32')

        target_words_embeddings, encoder_outputs, encoder_state_h, encoder_state_c = \
            self._build_training_encoder(source_words, target_words)

        encoder_states = [encoder_state_h, encoder_state_c]

        # decoder for training
        decoder_lstm = LSTM(self.hidden_size,
                            recurrent_dropout=self.hidden_dropout_rate,
                            return_sequences=True,
                            return_state=True)
        decoder_outputs_train, _, _ = decoder_lstm(
            target_words_embeddings, initial_state=encoder_states)

        if self.use_attention:
            decoder_attention = AttentionLayer()
            decoder_outputs_train = decoder_attention(
                [encoder_outputs, decoder_outputs_train])

        decoder_dense = Dense(self.vocab_target_size, activation='softmax')
        decoder_outputs_train = decoder_dense(decoder_outputs_train)

        adam = Adam(lr=0.01, clipnorm=5.0)
        self.train_model = Model([source_words, target_words],
                                 decoder_outputs_train)
        self.train_model.compile(optimizer=adam,
                                 loss='sparse_categorical_crossentropy',
                                 metrics=['accuracy'])
        self.train_model.summary()

        self.encoder_model = Model(
            source_words, [encoder_outputs, encoder_state_h, encoder_state_c])
        self.encoder_model.summary()

        decoder_state_input_h = Input(shape=(self.hidden_size, ))
        decoder_state_input_c = Input(shape=(self.hidden_size, ))
        encoder_outputs_input = Input(shape=(
            None,
            self.hidden_size,
        ))

        # decoder for inference
        decoder_input_states = [decoder_state_input_h, decoder_state_input_c]
        decoder_outputs_test, decoder_state_output_h, decoder_state_output_c = \
            decoder_lstm(target_words_embeddings, initial_state=decoder_input_states)

        if self.use_attention:
            decoder_outputs_test = decoder_attention(
                [encoder_outputs_input, decoder_outputs_test])

        decoder_outputs_test = decoder_dense(decoder_outputs_test)

        self.decoder_model = Model([
            target_words, decoder_state_input_h, decoder_state_input_c,
            encoder_outputs_input
        ], [
            decoder_outputs_test, decoder_state_output_h,
            decoder_state_output_c
        ])
        self.decoder_model.summary()