ratsgo / cam2 Goto Github PK
View Code? Open in Web Editor NEWSentiment classification with word localization based on weakly supervised learning with a convolutional neural network
License: MIT License
Sentiment classification with word localization based on weakly supervised learning with a convolutional neural network
License: MIT License
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.