riccorl / transformers-embedder Goto Github PK
View Code? Open in Web Editor NEWA Word Level Transformer layer based on PyTorch and ๐ค Transformers.
Home Page: https://riccorl.github.io/transformers-embedder
A Word Level Transformer layer based on PyTorch and ๐ค Transformers.
Home Page: https://riccorl.github.io/transformers-embedder
When building the word_ids
mask for text pairs, we can look for the last index of the BPE tokens in the first sentence and update the second part accordingly. As for now, the implementation is a bit slow due to the use of for loops. It can be performed more efficiently if we vectorize the function so that it looks for the offsets batch-wise (e.g. I used NumPy for the purpose, but I'm confident it can be implemented with PyTorch too).
from transformers import AutoTokenizer
import numpy as np
model_name = "bert-base-cased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
sents = [
("Today I'm using BERT embeddings.", "What's the wheather like in London?"),
("My Name is Luke.", "What's your name?")
]
# Tokenizer the text pairs
inputs = tokenizer(sents, return_special_tokens_mask=True)
# Select the offset indices
idxs = np.argwhere(np.diff(np.concatenate(inputs.special_tokens_mask)) == 1)[::2].squeeze()
# Obtain the batch word_ids
word_ids = np.concatenate([inputs.word_ids(i) for i in range(len(inputs.input_ids))])
offsets = word_ids[idxs].astype(int)
print(offsets)
# [SEP] and [CLS] are encoded as `1s` with the special_token_mask
>>> inputs.special_tokens_mask
[
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1]
]
# Look for differences of contiguous elements as to find the offset:
>>> np.diff(np.concatenate(inputs.special_tokens_mask))
array([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, <- "1s" at indexes: 13, 24, 31, 38
0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, -1, 0,
0, 0, 0, 0, 1])
# Alternate sequence of [SEP], [CLS], [SEP], [CLS], ... select [SEP]s only (i.e. the even indices)
>>> idxs = np.argwhere(np.diff(np.concatenate(inputs.special_tokens_mask)) == 1)[::2].squeeze()
array([13, 31]) <- offset indices (i.e. lengths of the first text in a text pair when concatenated)
i.e. BPE lengths are: 13 - 0 = 13 for the first sentence and 31 - 26 for the second
# Get the word_ids, unfortunately the transformers library doesn't provide an attribute as for the `special_tokens_mask`
>>> word_ids = np.concatenate([inputs[i].word_ids for i in range(len(inputs.input_ids))])
>>> word_ids
array([None, 0, 1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 6, 7, None, 0, 1, 2, 3, 4, <- offsets are: 7 and 4
4, 5, 6, 7, 8, None, None, 0, 1, 2, 3, 4, None, 0, 1, 2, 3, 4, 5,
None], dtype=object)
# Select the sentence offsets:
>>> word_ids[idxs].astype(int)
array([7, 4])
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.