简体   繁体   中英

AttributeError: 'TFSequenceClassifierOutput' object has no attribute 'argmax'

I wrote the text classification code with two classes using the Roberta model and now I want to draw the confusion matrix. How to go about plotting the confusion matrix based of a Roberta model?

    RobertaTokenizer = RobertaTokenizer.from_pretrained('roberta-base',do_lower_case=False)
    roberta_model = TFRobertaForSequenceClassification.from_pretrained('roberta-base',num_labels=2)
    
    input_ids=[]
    attention_masks=[]
    
    for sent in sentences:
        bert_inp=RobertaTokenizer.encode_plus(sent,add_special_tokens = True,max_length =128,pad_to_max_length = True,return_attention_mask = True)
        input_ids.append(bert_inp['input_ids'])
        attention_masks.append(bert_inp['attention_mask'])
    input_ids=np.asarray(input_ids)
    attention_masks=np.array(attention_masks)
    labels=np.array(labels)
    #split
train_inp,val_inp,train_label,val_label,train_mask,val_mask=train_test_split(input_ids,labels,attention_masks,test_size=0.5)
    print('Train inp shape {} Val input shape {}\nTrain label shape {} Val label shape {}\nTrain attention mask shape {} Val attention mask shape {}'.format(train_inp.shape,val_inp.shape,train_label.shape,val_label.shape,train_mask.shape,val_mask.shape))
  
    log_dir='tensorboard_data/tb_roberta'
    model_save_path='/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py'
    
    callbacks = [tf.keras.callbacks.ModelCheckpoint(filepath=model_save_path,save_weights_only=True,monitor='val_loss',mode='min',save_best_only=True),keras.callbacks.TensorBoard(log_dir=log_dir)]
    
    print('\nBert Model',roberta_model.summary())
    
    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
    metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
    optimizer = tf.keras.optimizers.Adam(learning_rate=2e-5,epsilon=1e-08)
    
    roberta_model.compile(loss=loss,optimizer=optimizer,metrics=[metric]) history=roberta_model.fit([train_inp,train_mask],train_label,batch_size=16,epochs=2,validation_data=([val_inp,val_mask],val_label),callbacks=callbacks)
    
    preds = roberta_model.predict([val_inp,val_mask],batch_size=16)
    pred_labels = preds.argmax(axis=1)
    f1 = f1_score(val_label,pred_labels)
    print('F1 score',f1)
    print('Classification Report')
    print(classification_report(val_label,pred_labels,target_names=target_names)) 
    c1 = confusion_matrix(val_label,pred_labels)
    print('confusion_matrix ',c1)

I now have the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-13-abcbb1d223b8> in <module>()
    106 
    107 preds = trained_model.predict([val_inp,val_mask],batch_size=16)
--> 108 pred_labels = preds.argmax(axis=1)
    109 f1 = f1_score(val_label,pred_labels)
    110 print('F1 score',f1)

AttributeError: 'TFSequenceClassifierOutput' object has no attribute 'argmax'

代替pred_labels = preds.argmax (axis = 1) ,替换以下代码:

pred_labels = np.argmax(preds.logits, axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM