The Transformers repository from “Hugging Face” incorporates a whole lot of prepared to make use of, state-of-the-art fashions, that are simple to obtain and fine-tune with Tensorflow & Keras.

For this objective the customers normally must get:

  • The mannequin itself (e.g. Bert, Albert, RoBerta, GPT-2 and and many others.)
  • The tokenizer object
  • The weights of the mannequin

On this submit, we are going to work on a traditional binary classification job and prepare our dataset on 3 fashions:

Nonetheless, readers ought to know that one can work with transformers on a wide range of down-stream duties, comparable to:

  1. characteristic extraction
  2. sentiment evaluation
  3. text classification
  4. question answering
  5. summarization
  6. translation and many more.


Our first job is to put in the transformers package deal through reticulate.

reticulate::py_install('transformers', pip = TRUE)

Then, as standard, load normal ‘Keras’, ‘TensorFlow’ >= 2.0 and a few traditional libraries from R.

Word that if operating TensorFlow on GPU one might specify the next parameters with the intention to keep away from reminiscence points.

physical_devices = tf$config$list_physical_devices('GPU')



We already talked about that to coach a knowledge on the precise mannequin, customers ought to obtain the mannequin, its tokenizer object and weights. For instance, to get a RoBERTa mannequin one has to do the next:

# get Tokenizer
transformer$RobertaTokenizer$from_pretrained('roberta-base', do_lower_case=TRUE)

# get Mannequin with weights

Knowledge preparation

A dataset for binary classification is offered in text2vec package deal. Let’s load the dataset and take a pattern for quick mannequin coaching.

Break up our knowledge into 2 components:

idx_train =*0.8)

prepare = df[idx_train,]
check = df[!idx_train,]

Knowledge enter for Keras

Till now, we’ve simply coated knowledge import and train-test break up. To feed enter to the community we’ve to show our uncooked textual content into indices through the imported tokenizer. After which adapt the mannequin to do binary classification by including a dense layer with a single unit on the finish.

Nonetheless, we wish to prepare our knowledge for 3 fashions GPT-2, RoBERTa, and Electra. We have to write a loop for that.

Word: one mannequin usually requires 500-700 MB

# listing of three fashions
ai_m = list(
  c('TFGPT2Model',       'GPT2Tokenizer',       'gpt2'),
   c('TFRobertaModel',    'RobertaTokenizer',    'roberta-base'),
   c('TFElectraModel',    'ElectraTokenizer',    'google/electra-small-generator')

# parameters
max_len = 50L
epochs = 2
batch_size = 10

# create an inventory for mannequin outcomes
gather_history = list()

for (i in 1:length(ai_m)) {
  # tokenizer
  tokenizer = glue::glue("transformer${ai_m[[i]][2]}$from_pretrained('{ai_m[[i]][3]}',
                         do_lower_case=TRUE)") %>% 
    rlang::parse_expr() %>% eval()
  # mannequin
  model_ = glue::glue("transformer${ai_m[[i]][1]}$from_pretrained('{ai_m[[i]][3]}')") %>% 
    rlang::parse_expr() %>% eval()
  # inputs
  textual content = list()
  # outputs
  label = list()
  data_prep = perform(knowledge) {
    for (i in 1:nrow(knowledge)) {
      txt = tokenizer$encode(knowledge[['comment_text']][i],max_length = max_len, 
                             truncation=T) %>% 
        t() %>% 
        as.matrix() %>% list()
      lbl = knowledge[['target']][i] %>% t()
      textual content = textual content %>% append(txt)
      label = label %>% append(lbl)
    list(,textual content),,label))
  train_ = data_prep(prepare)
  test_ = data_prep(check)
  # slice dataset
  tf_train = tensor_slices_dataset(list(train_[[1]],train_[[2]])) %>% 
    dataset_batch(batch_size = batch_size, drop_remainder = TRUE) %>% 
    dataset_shuffle(128) %>% dataset_repeat(epochs) %>% 
  tf_test = tensor_slices_dataset(list(test_[[1]],test_[[2]])) %>% 
    dataset_batch(batch_size = batch_size)
  # create an enter layer
  enter = layer_input(form=c(max_len), dtype='int32')
  hidden_mean = tf$reduce_mean(model_(enter)[[1]], axis=1L) %>% 
    layer_dense(64,activation = 'relu')
  # create an output layer for binary classification
  output = hidden_mean %>% layer_dense(items=1, activation='sigmoid')
  mannequin = keras_model(inputs=enter, outputs = output)
  # compile with AUC rating
  mannequin %>% compile(optimizer= tf$keras$optimizers$Adam(learning_rate=3e-5, epsilon=1e-08, clipnorm=1.0),
                    loss = tf$losses$BinaryCrossentropy(from_logits=F),
                    metrics = tf$metrics$AUC())
  # prepare the mannequin
  historical past = mannequin %>% keras::match(tf_train, epochs=epochs, #steps_per_epoch=len/batch_size,
  gather_history[[i]]<- historical past
  names(gather_history)[i] = ai_m[[i]][1]

Reproduce in a           Notebook

Extract outcomes to see the benchmarks:

Each the RoBERTa and Electra fashions present some extra enhancements after 2 epochs of coaching, which can’t be stated of GPT-2. On this case, it’s clear that it may be sufficient to coach a state-of-the-art mannequin even for a single epoch.


On this submit, we confirmed methods to use state-of-the-art NLP fashions from R.
To grasp methods to apply them to extra advanced duties, it’s extremely beneficial to overview the transformers tutorial.

We encourage readers to check out these fashions and share their outcomes beneath within the feedback part!


If you happen to see errors or wish to counsel adjustments, please create an issue on the supply repository.


Textual content and figures are licensed below Artistic Commons Attribution CC BY 4.0. Supply code is on the market at, except in any other case famous. The figures which have been reused from different sources do not fall below this license and might be acknowledged by a word of their caption: “Determine from …”.


For attribution, please cite this work as

Abdullayev (2020, July 30). Posit AI Weblog: State-of-the-art NLP fashions from R. Retrieved from

BibTeX quotation

  writer = {Abdullayev, Turgut},
  title = {Posit AI Weblog: State-of-the-art NLP fashions from R},
  url = {},
  12 months = {2020}

Leave a Reply

Your email address will not be published. Required fields are marked *