Advertisement
Guest User

bert_gluemrpc_rocm_benchmark

a guest
Nov 1st, 2021
3,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. #https://huggingface.co/transformers/v4.4.2/training.html#fine-tuning-in-native-tensorflow-2
  2. import tensorflow as tf
  3. import numpy as np
  4. gpus = tf.config.experimental.list_physical_devices('GPU')
  5. if gpus:
  6.   try:
  7.     for gpu in gpus:
  8.       tf.config.experimental.set_memory_growth(gpu, True)
  9.   except RuntimeError as e:
  10.     print(e)
  11.    
  12. import tensorflow_datasets as tfds
  13. from transformers import TFBertForSequenceClassification, BertTokenizer, glue_convert_examples_to_features, AutoConfig
  14.  
  15. tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
  16.  
  17. config = AutoConfig.from_pretrained('bert-base-cased')
  18. model = TFBertForSequenceClassification(config)
  19.  
  20. n_epochs = 5
  21. data = tfds.load('glue/mrpc')
  22. train_dataset = glue_convert_examples_to_features(data['train'], tokenizer, max_length=192, task='mrpc')
  23. train_dataset = train_dataset.shuffle(100).batch(32).repeat(n_epochs)
  24. valid_dataset = glue_convert_examples_to_features(data['validation'], tokenizer, max_length=192, task='mrpc')
  25. valid_dataset = valid_dataset.shuffle(100).batch(32).repeat(n_epochs)
  26.  
  27. optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
  28. loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
  29. model.compile(optimizer=optimizer, loss=loss)
  30. model.fit(train_dataset, validation_data = valid_dataset, epochs=5, steps_per_epoch=115)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement