简体   繁体   English

AttributeError: 'TFSequenceClassifierOutput' 对象没有属性 'argmax'

[英]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.我使用 Roberta 模型编写了两个类的文本分类代码,现在我想绘制混淆矩阵。 How to go about plotting the confusion matrix based of a Roberta model?如何绘制基于 Roberta 模型的混淆矩阵?

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 混淆矩阵错误“列表”object 没有属性“argmax” - confusion_matrix error 'list' object has no attribute 'argmax' AttributeError:“ NoneType”对象没有属性“ dtype” - AttributeError: 'NoneType' object has no attribute 'dtype' AttributeError:'module'对象没有属性'MutableMapping' - AttributeError: 'module' object has no attribute 'MutableMapping' AttributeError:&#39;Tensor&#39;对象没有属性&#39;reshape&#39; - AttributeError: 'Tensor' object has no attribute 'reshape' AttributeError: &#39;Tensor&#39; 对象没有属性 &#39;numpy - AttributeError: 'Tensor' object has no attribute 'numpy Keras AttributeError:&#39;list&#39;对象没有属性&#39;ndim&#39; - Keras AttributeError: 'list' object has no attribute 'ndim' AttributeError: &#39;module&#39; 对象没有属性 &#39;ceil&#39; - AttributeError: 'module' object has no attribute 'ceil' AttributeError:&#39;Tensor&#39;对象没有属性&#39;shape&#39; - AttributeError: 'Tensor' object has no attribute 'shape' AttributeError:&#39;Model&#39;对象没有属性&#39;name&#39; - AttributeError: 'Model' object has no attribute 'name' AttributeError: &#39;ShuffleDataset&#39; 对象没有属性 &#39;features&#39; - AttributeError: 'ShuffleDataset' object has no attribute 'features'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM