Search code examples
pythonkerasdeep-learningbert-language-modelroberta-language-model

Using roberta model cannot define the model .compile or summary


Using roberta model for sentiment analysis cannot define the model .compile or summary

from transformers import RobertaTokenizer, RobertaForSequenceClassification
from transformers import  BertConfig
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
robertamodel = RobertaForSequenceClassification.from_pretrained('roberta-base',num_labels=7)
print('\nBert Model',robertamodel.summary())
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('0accuracy')
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-5,epsilon=1e-08)

robertamodel.compile(loss=loss,optimizer=optimizer,metrics=[metric])
print(robertamodel.summary())

i got these errors 'RobertaForSequenceClassification' object has no attribute 'summary' 'RobertaForSequenceClassification' object has no attribute 'compile'


Solution

  • Roberta is based on pytorch. Check out the helper function TFRobertaModel to convert it to a tensorflow model.