j-min / mocha-pytorch Goto Github PK
View Code? Open in Web Editor NEWPyTorch Implementation of "Monotonic Chunkwise Attention" (ICLR 2018)
PyTorch Implementation of "Monotonic Chunkwise Attention" (ICLR 2018)
Is the returned attention by MonotonicAttention.soft() a probability distribution?
Seems to be not, the following code:
from attention import MonotonicAttention
monotonic = MonotonicAttention().cuda()
batch_size = 1
sequence_length= 5
enc_dim, dec_dim = 10, 10
prev_attention = None
for t in range(5):
encoder_outputs = torch.randn(batch_size, sequence_length, enc_dim).cuda()
decoder_h = torch.randn(batch_size, dec_dim).cuda()
attention = monotonic.soft(encoder_outputs, decoder_h, previous_alpha=prev_attention)
prev_attention = attention
# probability distribution ?
print(torch.sum(attention, dim=-1).detach().cpu().numpy())
returns:
[1.]
[0.0550258]
[0.00664481]
[0.00043618]
[4.0174375e-05]
If it was a probability distribution like softmax, every row should return 1 or ?. The consequence is my alignments look like this image:
So my questions are:
cumprod in the MoChA paper is defined to be exclusive, while the safe_cumprod
in this repo does not. Shouldn't it be:
def safe_cumprod(self, x, exclusive=False):
"""Numerically stable cumulative product by cumulative sum in log-space"""
bsz = x.size(0)
logsum = torch.cumsum(torch.log(torch.clamp(x, min=1e-20, max=1)), dim=1)
if exclusive:
logsum = torch.cat([torch.zeros(bsz, 1).to(logsum), logsum], dim=1)[:, :-1]
return torch.exp(logsum)
And in the function soft()
of MonotonicAttention
:
cumprod_1_minus_p = self.safe_cumprod(1 - p_select, exclusive=True)
I tried this MonotonicAttention in my seq2seq model, which works well with vanilla attention, while after training for a while, it still encountered the Nan grad issue. I checked the parameters with Nan grad, which are all params before MonotonicAttention's output. I also deleted the "safe_cumprod" operation, and this works well. So I think there may be some problems. Does anyone tried MonotonicAttention, and what's your situation?
I think
energy = self.tanh(self.W(encoder_outputs) + self.V(decoder_h).repeat(sequence_length, 1) + self.b)
should be writen as
energy = self.tanh(self.W(encoder_outputs) + self.V(decoder_h).repeat(1,sequence_length).reshape(batch_size*sequence_length,-1) + self.b)
Excuse me,is there any trained weights or training code?
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.