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

about seq2seq(attention)-Torch multiple sample training question #54

Open
wmathor opened this issue Jun 30, 2020 · 0 comments
Open

about seq2seq(attention)-Torch multiple sample training question #54

wmathor opened this issue Jun 30, 2020 · 0 comments

Comments

@wmathor
Copy link

wmathor commented Jun 30, 2020

hello, first thank your code, but i want to know if batch_size is more than 1, i should how to modify the code, thank you

    def get_att_weight(self, output, enc_output):  # get attention weight one 'output' with 'enc_output'
        '''
        output: [1, batch_size, num_directions(=1) * n_hidden]
        enc_output: [n_step+1, batch_size, num_directions(=1) * n_hidden]
        '''
        length = len(enc_output)
        attn_scores = torch.zeros(length)  # attn_scores : [batch_size, n_step+1]
        for i in range(length):
            attn_scores[i] = self.get_att_score(output, enc_output[i])

        # Normalize scores to weights in range 0 to 1
        # return [batch_size, 1, n_step+1]
        return F.softmax(attn_scores).view(batch_size, 1, -1)

    def get_att_score(self, output, enc_output):
        '''
        output: [batch_size, num_directions(=1) * n_hidden]
        enc_output: [batch_size, num_directions(=1) * n_hidden]
        '''
        score = self.attn(enc_output)  # score : [1, n_hidden]
        return torch.dot(output.view(-1), score.view(-1))  # inner product make scalar value, get a real number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant