Code Monkey home page Code Monkey logo

cam2's People

Contributors

ratsgo avatar

Watchers

 avatar

cam2's Issues

Implementation of CAM with LSTM for multi-label classification

Hi @ratsgo ,

I am referring to your paper titled 'Sentiment Classification with Word Attention based on
Weakly Supervised Learning with a Convolutional Neural Network'. I have a problem as below:

Problem:

I am working on a project for extraction Drug-target Interaction in the biomedical domain. I am solving it as a multi-label text classification problem using LSTM.
Now, I want to know how much each word in the sentence is contributing to the prediction of a particular class.
Could please let me know how can I implement the CAM approach with LSTM having the attention layer? Please find below the model architecture:

class CustomLayer (tf.keras.layers.Layer):
    def __init__(self , units , **kwargs):
        super (CustomLayer , self).__init__ (**kwargs)
        self.units = units
        self.W1 = tf.keras.layers.Dense (units)
        self.W2 = tf.keras.layers.Dense (units)
        self.V = tf.keras.layers.Dense (1)

    def call(self , features , hidden):
            hidden_with_time_axis = tf.expand_dims (hidden , 1)

        score = tf.nn.tanh (self.W1 (features) + self.W2 (hidden_with_time_axis))

        attention_weights = tf.nn.softmax (self.V (score) , axis=1)

        context_vector = attention_weights * features
        context_vector = tf.reduce_sum (context_vector , axis=1) ##attention adjusted ouput state

        return context_vector , attention_weights

    def get_config(self):
        config = {"units": self.units}
        base_config = super (CustomLayer , self).get_config ()

        return dict(list(base_config.items())+ list(config.items()))




def build_att_lstm(MAX_LEN, vocab_size, EMBED_SIZE, embed_matrix, LSTM_Unit, dropouts_input, dropouts):
    sequence_input = Input(shape=(MAX_LEN,), dtype="int32")
    embedded_sequences = Embedding(vocab_size +1, EMBED_SIZE, weights=[embed_matrix], trainable= False)(sequence_input)
    embedded_sequences_do = Dropout (dropouts_input) (embedded_sequences)
    (lstm, forward_h, forward_c) =LSTM(LSTM_Unit, return_sequences=True, return_state=True, name="lstm_1", dropout=dropouts)(embedded_sequences_do)

    context_vector, attention_weights = CustomLayer(10, name= 'CustomLayer')(lstm, forward_h)

    output = Dense(6, activation="softmax")(context_vector)

    model = tf.keras.Model(inputs=sequence_input, outputs=output)

    #config = model.get_config ()

    METRICS = [
        tf.keras.metrics.BinaryAccuracy (name='accuracy') ,
        tf.keras.metrics.Precision (name='precision') ,
        tf.keras.metrics.Recall (name='recall') ,
        tf.keras.metrics.AUC (name='auc') ,
    ]

    model.compile (loss='categorical_crossentropy' , optimizer='adam' , metrics=METRICS)

    return model

Could you please help me with the solutions to get the importance of each word in the sentence?

Looking forward to your reply.

Best Regards,
Meghna Goyal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.