Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Temporal Score functions #72

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b75a406
Added Character perturbations along with a test
harsh4799 May 12, 2020
1e589a2
Merge branch 'master' of https://github.com/blackrosedragon2/deceptic…
harsh4799 May 12, 2020
c116e2e
Testing and python3 black
harsh4799 May 12, 2020
d65e27b
Update perturbations.py
harsh4799 May 12, 2020
d596ec1
Changed the name of the class from SpaceCharacterPerturbations to Ins…
May 12, 2020
2edbede
Black Formatting
May 12, 2020
4025c24
Merge branch 'master' of https://github.com/SforAiDl/decepticonlp
May 14, 2020
55651b4
modifed ts
harsh4799 May 15, 2020
5196834
Merge branch 'master' of https://github.com/SforAiDl/decepticonlp
May 15, 2020
a20fee1
completed fns
May 16, 2020
e074820
Merge branch 'master' of https://github.com/SforAiDl/decepticonlp
May 19, 2020
53cc3fc
updated temporal score
May 19, 2020
d89dace
added files to right directory
harsh4799 May 20, 2020
05861c1
added torch to requirements
May 20, 2020
e5e08ad
added word level temporal score
May 20, 2020
6174468
Merge branch 'master' of https://github.com/SforAiDl/decepticonlp
May 21, 2020
fbcd5f5
added temporal score
May 21, 2020
400229c
Update test_temporal_score.py
harsh4799 May 22, 2020
96eba69
removed faulty testcase
May 23, 2020
ad89afb
removed faulty test and formatted
May 23, 2020
abe26c3
Merge branch 'master' of https://github.com/blackrosedragon2/deceptic…
May 23, 2020
a1b7961
removed faulty test and formatted
May 23, 2020
6139fe3
removed faulty test and formatted
May 23, 2020
0f53026
added name to authors
May 23, 2020
f1f9c40
Added docstrings
May 25, 2020
2a00a8e
User defined Models
May 27, 2020
8cb7762
removed the unexpected failing test case
May 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Contributors
* Rohit Patil <[email protected]>
* Parantak Singh <[email protected]>
* Abheesht Sharma <[email protected]>
* Harshit Pandey <[email protected]>

2 changes: 1 addition & 1 deletion decepticonlp/metrics/char_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CharacterMetrics(metaclass=abc.ABCMeta):
An abstract class used to represent the character metrics. Subclasses implement the calculate method.
Methods
-------
apply(self, text1: str, text2: str, **kwargs)
calculate(self, text1: str, text2: str, **kwargs)
- calculates the similarity/distance between two strings using the appropriate metric.
"""

Expand Down
109 changes: 109 additions & 0 deletions decepticonlp/metrics/temporal_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import torch
import torch.nn as nn


class RankWords:
"""
Accepts a feature vector torch tensor and outputs a temporal ranking of each word
inputs of shape (number of words,feature_vector_size)
Methods
--------
temporal_score(self, model, sentence)
- calculates the temporal score each word in a sentence(considering the first i words)
temportal_tail_score(self, model, sentence)
- calculates the temporal score each word in a sentence(considering n-i words)
combined_score(self, model, sentence, lambda_)
- calculates the combined score taking into consideration temporal score and tailed temporal score with lambda as the weight
"""

def temporal_score(self, model, sentence):
"""
Considering a input sequence x1,x2,...,xn
we calculate T(xi) = F(x1,x2,...,xi) - F(x1,x2,...,xi-1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstrings in our preferred format needed here too? Including information about params and return values?

where F is one pass through a RNN cell

Example:

>> import temporal_metrics
>> rw = temporal_metrics.RankWords()
>> embedded_word = embed("i like dogs") #embed should convert the sentence to a (sent_length * embedding_dim) shape tensor
>> print(rw.temporal_score(model,embedded_word)) #seed = 99
tensor([[1.1921e-07],[2.2054e-05],[8.4886e-01]])

:params
:model : The model must output torch tensor of dimension (1,1,1) as its output
:sentence : An tensor of shape (sent_length * embedding_dim) where each word has been embedded

returns temporal score of each word
:return type: torch.Tensor
"""
inputs = sentence.reshape(len(sentence), 1, -1)
with torch.no_grad():
pred = model(inputs)
losses = torch.zeros(inputs.shape[0:2])
for i in range(inputs.size()[0]):
tempinputs = inputs.clone()
tempinputs[i, :, :].zero_()
tempoutput = model(tempinputs)
losses[i] = torch.dist(
tempoutput.squeeze(1), pred.squeeze(1)
) # L2 Norm
return losses

def temportal_tail_score(self, model, sentence):
"""
Considering a input sequence x1,x2,...,xn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring needed

we calculate T(xi) = F(xi,xi+1,...,xn) - F(xi+1,xi+2...,xn)
where F is one pass through a RNN cell

Example:

>> import temporal_metrics
>> rw = temporal_metrics.RankWords()
>> embedded_word = embed("i like dogs") #embed should convert the sentence to a (sent_length * embedding_dim) shape tensor
>> print(rw.temportal_tail_score(model,embedded_word)) #seed = 99

tensor([[5.9605e-08],[1.9789e-05],[0.0000e+00]])

:params
:model : The model must output torch tensor of dimension (1,1,1) as its output
:sentence : An tensor of shape (sent_length * embedding_dim) where each word has been embedded

returns temporal score of each word
:return type: torch.Tensor
"""
inputs = sentence.reshape(len(sentence), 1, -1)
losses = torch.zeros(inputs.shape[0:2])
with torch.no_grad():
for i in range(inputs.size()[0] - 1):
pred = model(inputs[i:, :, :])
tempinputs = inputs[i + 1 :, :,].clone()
tempoutput = model(tempinputs)
losses[i] = torch.dist(tempoutput, pred) # L2 Norm
return losses

def combined_score(self, model, sentence, lambda_):
"""
Computes combined temporal_score, temportal_tail_score
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper docstrings needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add all of the docstrings

Combined Score = temporal_score + λ(temportal_tail_score)

Example:

>> import temporal_metrics
>> rw = temporal_metrics.RankWords()
>> embedded_word = embed("i like dogs") #embed should convert the sentence to a (sent_length * embedding_dim) shape tensor
>> print(rw.combined_score(model,embedded_word,0.5)) #seed = 99

tensor([[6.8545e-07], [3.8507e-03], [8.4886e-01]])

:params
:model : The model must output torch tensor of dimension (1,1,1) as its output
:sentence : An tensor of shape (sent_length * embedding_dim) where each word has been embedded
:lambda_ : (float) , 0 <= lambda_ <= 1

returns temporal score of each word
:return type: torch.Tensor
"""
return self.temporal_score(
model, sentence
) + lambda_ * self.temportal_tail_score(model, sentence)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Sphinx==1.8.5
twine==1.14.0
tensorflow==2.2.0
tensorflow-hub==0.8.0

torch==1.5.0
setuptools
pytest==5.4.2
pytest-runner==5.2
Expand Down
Loading