Code Monkey home page Code Monkey logo

positional-encoding's Introduction

Positional-Encoding

Attention is all you need: https://arxiv.org/abs/1706.03762 does not use any recurrent neural networks to encode the sentence. Instead they make use of positional encodings followed by attention. In the paper, thay use sine and cosine functions of different frequencies:

P E(pos,2i) = sin(pos/10000**(2i/dmodel))
P E(pos,2i+1) = cos(pos/10000**(2i/dmodel))

where pos is the position and i is the dimension. That is, each dimension of the positional encoding corresponds to a sinusoid. The wavelengths form a geometric progression from 2π to 10000 · 2π. We chose this function because we hypothesized it would allow the model to easily learn to attend by relative positions, since for any fixed offset k, P Epos+k can be represented as a linear function of P Epos.

encoder.py provides a class which helps to encode the position/time component along with the word embeddings. Both the position as well as word embeddings are trainiable. Encoding output of this class must be passed through a self attention layer for improved results.

Syntax

import torch
import numpy as np
pe = PositionalEncoder(vocab_size,EMB_DIM,MAXLEN,BATCH_SIZE)
word_seq = Variable(torch.from_numpy(np.array([0,0,34,56,23,1])).type(torch.LongTensor))
pe(word_seq)

Its time to throw away LSTM/RNN's . Attention is all you need.

positional-encoding's People

Contributors

kaushalshetty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.