The National Library of Sweden / KBLab releases three pretrained language models based on BERT and ALBERT. The models are trained on aproximately 15-20GB of text (200M sentences, 3000M tokens) from various sources (books, news, government publications, swedish wikipedia and internet forums) aiming to provide a representative BERT model for Swedish text. A more complete description will be published later on.
The following three models are currently available:
bert-base-swedish-cased
(
v1
) - A BERT trained with the same hyperparameters as first published by Google.
bert-base-swedish-cased-ner
(
experimental
) - a BERT fine-tuned for NER using SUC 3.0.
albert-base-swedish-cased-alpha
(
alpha
) - A first attempt at an ALBERT for Swedish.
All models are cased and trained with whole word masking.
The examples below require Huggingface Transformers 2.4.1 and Pytorch 1.3.1 or greater. For Transformers<2.4.0 the tokenizer must be instantiated manually and the
do_lower_case
flag parameter set to
False
and
keep_accents
to
True
(for ALBERT).
To create an environment where the examples can be run, run the following in an terminal on your OS of choice.
A standard BERT base for Swedish trained on a variety of sources. Vocabulary size is ~50k. Using Huggingface Transformers the model can be loaded in Python as follows:
from transformers import AutoModel,AutoTokenizer
tok = AutoTokenizer.from_pretrained('KBLab/bert-base-swedish-cased')
model = AutoModel.from_pretrained('KBLab/bert-base-swedish-cased')
BERT base fine-tuned for Swedish NER
This model is fine-tuned on the SUC 3.0 dataset. Using the Huggingface pipeline the model can be easily instantiated. For Transformer<2.4.1 it seems the tokenizer must be loaded separately to disable lower-casing of input strings:
from transformers import pipeline
nlp = pipeline('ner', model='KB/bert-base-swedish-cased-ner', tokenizer='KB/bert-base-swedish-cased-ner')
nlp('Idag släpper KB tre språkmodeller.')
Running the Python code above should produce in something like the result below. Entity types used are
TME
for time,
PRS
for personal names,
LOC
for locations,
EVN
for events and
ORG
for organisations. These labels are subject to change.
The BERT tokenizer often splits words into multiple tokens, with the subparts starting with
##
, for example the string
Engelbert kör Volvo till Herrängens fotbollsklubb
gets tokenized as
Engel ##bert kör Volvo till Herr ##ängens fotbolls ##klubb
. To glue parts back together one can use something like this:
text = 'Engelbert tar Volvon till Tele2 Arena för att titta på Djurgården IF ' +\
'som spelar fotboll i VM klockan två på kvällen.'
l = []
for token in nlp(text):
if token['word'].startswith('##'):
l[-1]['word'] += token['word'][2:]
else:
l += [ token ]
print(l)
Which should result in the following (though less cleanly formated):
The easisest way to do this is, again, using Huggingface Transformers:
from transformers import AutoModel,AutoTokenizer
tok = AutoTokenizer.from_pretrained('KBLab/albert-base-swedish-cased-alpha'),
model = AutoModel.from_pretrained('KBLab/albert-base-swedish-cased-alpha')
Acknowledgements ❤️
Resources from Stockholms University, Umeå University and Swedish Language Bank at Gothenburg University were used when fine-tuning BERT for NER.
Model pretraining was made partly in-house at the KBLab and partly (for material without active copyright) with the support of Cloud TPUs from Google's TensorFlow Research Cloud (TFRC).
@misc{malmsten2020playing,
title={Playing with Words at the National Library of Sweden -- Making a Swedish BERT},
author={Martin Malmsten and Love Börjeson and Chris Haffenden},
year={2020},
eprint={2007.01658},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
Runs of KBLab bert-base-swedish-cased on huggingface.co
5.5K
Total runs
37
24-hour runs
128
3-day runs
185
7-day runs
632
30-day runs
More Information About bert-base-swedish-cased huggingface.co Model
bert-base-swedish-cased huggingface.co is an AI model on huggingface.co that provides bert-base-swedish-cased's model effect (), which can be used instantly with this KBLab bert-base-swedish-cased model. huggingface.co supports a free trial of the bert-base-swedish-cased model, and also provides paid use of the bert-base-swedish-cased. Support call bert-base-swedish-cased model through api, including Node.js, Python, http.
bert-base-swedish-cased huggingface.co is an online trial and call api platform, which integrates bert-base-swedish-cased's modeling effects, including api services, and provides a free online trial of bert-base-swedish-cased, you can try bert-base-swedish-cased online for free by clicking the link below.
KBLab bert-base-swedish-cased online free url in huggingface.co:
bert-base-swedish-cased is an open source model from GitHub that offers a free installation service, and any user can find bert-base-swedish-cased on GitHub to install. At the same time, huggingface.co provides the effect of bert-base-swedish-cased install, users can directly use bert-base-swedish-cased installed effect in huggingface.co for debugging and trial. It also supports api for free installation.
bert-base-swedish-cased install url in huggingface.co: