tensorflow - 属性错误: 'TFSequenceClassifierOutput' object has no attribute 'argmax'

标签 tensorflow nlp confusion-matrix

我使用 Roberta 模型编写了包含两个类的文本分类代码,现在我想绘制混淆矩阵。如何根据 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)

我现在遇到以下错误:

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)

关于tensorflow - 属性错误: 'TFSequenceClassifierOutput' object has no attribute 'argmax' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68254796/

相关文章:

python - 混淆矩阵取值错误

python - 使用 Tensor Flow 对 MNIST 手写数字进行 CNN 训练的振荡精度

python - TensorFlow/Keras : Why do I get "ValueError: Incompatible conversion from float32 to uint8" when calling fit?

python - 在文档分析中计算句子分数时应该计算重复词吗?

python - 如何从 Spacy NER 模型中获得每个实体的预测概率?

nlp - 计算梯度PyTorch 中嵌入向量的值

r - 为决策树的概率结果设置阈值

python - Keras multi_gpu_model 错误 : "swig/python detected a memory leak of type ' int64_t *', no destructor found"

tensorflow - 比较 TensorFlow 和 PaddlePaddle 之间的深度学习框架

matlab - 有没有使用Matlab计算Precision和Recall的函数?