All Projects → Kyushik → Attention

Kyushik / Attention

Licence: other
Repository for Attention Algorithm

Programming Languages

Jupyter Notebook
11667 projects

Projects that are alternatives of or similar to Attention

Linear Attention Recurrent Neural Network
A recurrent attention module consisting of an LSTM cell which can query its own past cell states by the means of windowed multi-head attention. The formulas are derived from the BN-LSTM and the Transformer Network. The LARNN cell with attention can be easily used inside a loop on the cell state, just like any other RNN. (LARNN)
Stars: ✭ 119 (+205.13%)
Mutual labels:  lstm, attention-model
Image Caption Generator
A neural network to generate captions for an image using CNN and RNN with BEAM Search.
Stars: ✭ 126 (+223.08%)
Mutual labels:  lstm, attention-model
learningspoons
nlp lecture-notes and source code
Stars: ✭ 29 (-25.64%)
Mutual labels:  lstm, attention-model
Recognizing-Textual-Entailment
A pyTorch implementation of models used for Recognizing Textual Entailment using the SNLI corpus
Stars: ✭ 31 (-20.51%)
Mutual labels:  attention-model
bert-squeeze
🛠️ Tools for Transformers compression using PyTorch Lightning ⚡
Stars: ✭ 56 (+43.59%)
Mutual labels:  lstm
SpeakerDiarization RNN CNN LSTM
Speaker Diarization is the problem of separating speakers in an audio. There could be any number of speakers and final result should state when speaker starts and ends. In this project, we analyze given audio file with 2 channels and 2 speakers (on separate channels).
Stars: ✭ 56 (+43.59%)
Mutual labels:  lstm
lstm-electric-load-forecast
Electric load forecast using Long-Short-Term-Memory (LSTM) recurrent neural network
Stars: ✭ 56 (+43.59%)
Mutual labels:  lstm
tensorflow-node-examples
Tensorflow Node.js Examples
Stars: ✭ 21 (-46.15%)
Mutual labels:  lstm
Simple-Tensor
A simplification of Tensorflow Tensor Operations
Stars: ✭ 17 (-56.41%)
Mutual labels:  lstm
deep-char-cnn-lstm
Deep Character CNN LSTM Encoder with Classification and Similarity Models
Stars: ✭ 20 (-48.72%)
Mutual labels:  lstm
extkeras
Playground for implementing custom layers and other components compatible with keras, with the purpose to learn the framework better and perhaps in future offer some utils for others.
Stars: ✭ 18 (-53.85%)
Mutual labels:  lstm
kaspersky hackathon
https://events.kaspersky.com/hackathon/
Stars: ✭ 25 (-35.9%)
Mutual labels:  lstm
voice zaloai
dentifying gender and regional accent from speech
Stars: ✭ 35 (-10.26%)
Mutual labels:  lstm
Show and Tell
Show and Tell : A Neural Image Caption Generator
Stars: ✭ 74 (+89.74%)
Mutual labels:  lstm
AdvancedDeepLearning
Advanced Deep Learning
Stars: ✭ 28 (-28.21%)
Mutual labels:  lstm
HHH-An-Online-Question-Answering-System-for-Medical-Questions
HBAM: Hierarchical Bi-directional Word Attention Model
Stars: ✭ 44 (+12.82%)
Mutual labels:  attention-model
dnn-lstm-word-segment
Chinese Word Segmention Base on the Deep Learning and LSTM Neural Network
Stars: ✭ 24 (-38.46%)
Mutual labels:  lstm
keras-malicious-url-detector
Malicious URL detector using keras recurrent networks and scikit-learn classifiers
Stars: ✭ 24 (-38.46%)
Mutual labels:  lstm
novel writer
Train LSTM to writer novel (HongLouMeng here) in Pytorch.
Stars: ✭ 14 (-64.1%)
Mutual labels:  lstm
air writing
Online Hand Writing Recognition using BLSTM
Stars: ✭ 26 (-33.33%)
Mutual labels:  lstm

Attention

Introduction

This repository is for algorithms of Attention.

The paper I implemented is as follows.

Dataset

This Algorithm will be tested by Modified MNIST dataset Which is made by Jongwon Park.

This modified MNIST dataset is good for verifying attention algorithm.

The example of modified MNIST is as follows.

Combined Image

You can download this modified MNIST data from this link

Training dataset / Testing dataset

Environment

Software

  • Windows7 (64bit)
  • Python 3.5.2
  • Anaconda 4.2.0
  • Tensorflow-gpu 1.4.0

Hardware

  • CPU: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHZ
  • GPU: GeForce GTX 1080
  • Memory: 8GB

Algorithms

Soft Attention

This algorithm is from the paper Show, Attend and Tell: Neural Image Caption Generation with Visual Attention. I studied attention from Heuritech blog.

The attention model for image captioning from paper is as follows. The image is from the Heuritech blog.

Combined Image

For implementing this algorithm, Attention model and LSTM are needed. The code of LSTM is as follows.

# LSTM function
def LSTM_cell(C_prev, h_prev, x_lstm, Wf, Wi, Wc, Wo, bf, bi, bc, bo):
    # C_prev: Cell state from lstm of previous time step (shape: [batch_size, lstm_size])
    # h_prev: output from lstm of previous time step (shape: [batch_size, lstm_size])
    # x_lstm: input of lstm (shape: [batch_size, data_flatten_size])

    input_concat = tf.concat([x_lstm, h_prev], 1)
    f = tf.sigmoid(tf.matmul(input_concat, Wf) + bf)
    i = tf.sigmoid(tf.matmul(input_concat, Wi) + bi)
    c = tf.tanh(tf.matmul(input_concat, Wc) + bc)
    o = tf.sigmoid(tf.matmul(input_concat, Wo) + bo)
    
    C_t = tf.multiply(f, C_prev) + tf.multiply(i, c) 
    h_t = tf.multiply(o, tf.tanh(C_t))
    
    return C_t, h_t # Cell state, Output

Colah's blog post is very good for understanding LSTM and I studied this post to implement LSTM.

Structure image of soft attention model is as follows. Image is from Heuritech blog.

Combined Image

Also, the code of soft attention is as follows.

# Soft Attention function
def soft_attention(h_prev, a, Wa, Wh):
    # h_prev: output from lstm of previous time step (shape: [batch_size, lstm_size])
    # a: Result of CNN [batch_size, conv_size * conv_size, channel_size] 

    m_list = [tf.tanh(tf.matmul(a[i], Wa) + tf.matmul(h_prev, Wh)) for i in range(len(a))] 
    m_concat = tf.concat([m_list[i] for i in range(len(a))], axis = 1)     
    alpha = tf.nn.softmax(m_concat) 
    z_list = [tf.multiply(a[i], tf.slice(alpha, (0, i), (-1, 1))) for i in range(len(a))]
    z_stack = tf.stack(z_list, axis = 2)
    z = tf.reduce_sum(z_stack, axis = 2)

    return alpha, z

After 10 epoch, The training accuracy of LSTM was 94% and validation accuracy was 97%.

Sample images of soft attention result are as follows.

Combined Image

Hard Attention

This algorithm is from the paper Show, Attend and Tell: Neural Image Caption Generation with Visual Attention. Hard Attention architecture image from Heuritech blog is as follows.

Combined Image

The random choice algorithm is Monte-Carlo Sampling. Therefore, I made a code for hard attention as follows.

# Hard Attention function
def hard_attention(h_prev, a, Wa, Wh):
    # h_prev: output from lstm of previous time step (shape: [batch_size, lstm_size])
    # a: Result of CNN [batch_size, conv_size * conv_size, channel_size] 

    m_list = [tf.tanh(tf.matmul(a[i], Wa) + tf.matmul(h_prev, Wh)) for i in range(len(a))] 
    m_concat = tf.concat([m_list[i] for i in range(len(a))], axis = 1)     
    alpha = tf.nn.softmax(m_concat) 
    
    #For Monte-Carlo Sampling
    alpha_cumsum = tf.cumsum(alpha, axis = 1)
    len_batch = tf.shape(alpha_cumsum)[0]
    rand_prob = tf.random_uniform(shape = [len_batch, 1], minval = 0., maxval = 1.)
    alpha_relu = tf.nn.relu(rand_prob - alpha_cumsum)
    alpha_index = tf.count_nonzero(alpha_relu, 1)
    alpha_hard  = tf.one_hot(alpha_index, len(a))

    z_list = [tf.multiply(a[i], tf.slice(alpha_hard, (0, i), (-1, 1))) for i in range(len(a))]
    z_stack = tf.stack(z_list, axis = 2)
    z = tf.reduce_sum(z_stack, axis = 2)

    return alpha, z

After 10 epoch, The training accuracy of LSTM was only 30% and validation accuracy was 33%.

Sample images of hard attention result are as follows.

Combined Image

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].