You are on page 1of 32

BERT Fine-Tuning 세부 설명

안재학 프로
BertForSequenceClassi cation
• Metric

• f1_score

• roc_auc_score

• Accuracy

• Loss

• BCEWithLogitsLoss

fi
Metric

• f1_score

• roc_auc_score

• Accuracy
Confusion Matrix

• 분류결과표 (혼돈 행렬)

• Accuracy (정확도)

• Recall (재현률)

• Precision (정밀도)
Confusion Matrix
• Accuracy (정확도)

• ( TP + TN ) / ( TP +
TN + FP + FN )

• Recall (재현률)

• TP / ( TP + FN )

• Precision (정밀도)

• TP / ( TP + FP )
Accuracy의 문제점

• 6월에 눈이 내릴지 여부 예측

• 무조건 Negative 예측하면 99.9% 정확도

• Negative가 압도적으로 많은 데이터에 대해서는 좋지 않은 metric

• Recall을 사용해보자
Recall

• 6월에 눈이 내릴지 여부 예측

• 실제 눈이 내린 날( TP + FN ) 중 몇
번이나 맞췄는가( TP )?

• Negative만 예측할 경우 TP는 작아


지고 FN는 커져 Recall 점수 0에 가
까워 짐
Recall의 문제

• 전부 Positive 로 예측

• 정확도 99.9%

• 그럼 Precision 사용하자
Precision

• 6월에 눈이 내릴지 여부 예측

• 눈이 내릴 것이라고 한 날( TP + FP )
중 실제로 몇 일이나 내렸나( TP )?

• 언제나 Positive라고 할 경우 FP가 커


지기 때문에 Precision은 0에 가까워

F1

• 두 개를 평균내자

• Recall, Precision의 조화 평균

• 2 * Recall * Precision / (Precision


+ Recall)
ROC curve
• ROC = Receiver Operating
Characteristic

• True Positive Rate(TPR)

• Recall = Sensitivity

• False Positive Rate(FPR)

• 1 - Speci city
fi
ROC curve
• True Positive Rate(TPR)

• Recall = Sensitivity

• TP / ( TP + FN)

• False Positive Rate(FPR)

• 1 - Speci city

• 1 - ( TN / (TN + FP) )

• FP / ( FP + TN )
fi
Draw ROC
• T = [ 1, 0, 0, 1, 1, 0 ]

• P = [ 0.8, 0.6, 0.2, 0.4, 0.3, 0.5 ]

• Threshold = 0.5

• P = [ 1, 1, 0, 0, 0, 1 ]

(0.6, 0.3)

• TPR = TP / ( TP + FN ) = 1 / 3

• FPR = FP / ( FP + TN ) = 2 / 3
Draw ROC
(0.6, 1.0)
• T = [ 1, 0, 0, 1, 1, 0 ]

• P = [ 0.8, 0.6, 0.2, 0.4, 0.3, 0.5 ]

• Threshold = 0.3

• P = [ 1, 1, 0, 1, 1, 1 ]

(0.6, 0.3)
• TPR = TP / ( TP + FN ) = 3 / 3

• FPR = FP / ( FP + TN ) = 2 / 3
Draw ROC
(0.6, 1.0) (1.0, 1.0)
• T = [ 1, 0, 0, 1, 1, 0 ]

• P = [ 0.8, 0.6, 0.2, 0.4, 0.3, 0.5 ]

• Threshold = 0.1

• P = [ 1, 1, 1, 1, 1, 1 ]

(0.6, 0.3)
• TPR = TP / ( TP + FN ) = 3 / 3

• FPR = FP / ( FP + TN ) = 3 / 3
Draw ROC
(0.6, 1.0) (1.0, 1.0)
• T = [ 1, 0, 0, 1, 1, 0 ]

• P = [ 0.8, 0.6, 0.2, 0.4, 0.3, 0.5 ]

(0.6, 0.6) • Threshold = 0.4

• P = [ 1, 1, 0, 1, 0, 1 ]

(0.6, 0.3)
• TPR = TP / ( TP + FN ) = 2 / 3

• FPR = FP / ( FP + TN ) = 2 / 3
Draw ROC

• Model 1의 ROC curve


Draw ROC

• Model 1의 ROC curve

• Model 2의 ROC curve

• Model 3의 ROC curve

• 어떻게 비교..?
AUC

• Area Under the Curve

• Curve 아래의 면적을 계산해 비교


BCEWithLogitsLoss()

• 코드 : transformers/modeling_bert.py
BCEWithLogitsLoss()

..?
BCEWithLogitsLoss()

• Wn 무시

• Yn = True

• Xn = Prediction

• Log = 자연로그

• = sigmoid
BCEWithLogitsLoss()

• Y = [ 1, 0, 0, 1 ]

• X = [ -3.9, -3.7, 5.8, 3 ]


BCEWithLogitsLoss()

• Y = [ 1, 0, 0, 1 ]

• X = [ -3.9, -3.7, 5.8, 3 ]

• = [0.0198, 0.0241, 0.9970, 0.9526]


BCEWithLogitsLoss()

• Y = [ 1, 0, 0, 1 ]

• X = [ -3.9, -3.7, 5.8, 3 ]

• = [ 0.0198, 0.0241, 0.9970, 0.9526 ]

• = [ -3.92, -3.724, -0.003, -0.048 ]


BCEWithLogitsLoss()

• = [ -3.92, -3.724, -0.003, -0.048 ]

• Sum = - [ -3.92 -0.048 ] = 3.968

• Y = 1일 때의 loss : 3.968
BCEWithLogitsLoss()

• Y = [ 1, 0, 0, 1 ]

• X = [ -3.9, -3.7, 5.8, 3 ]


BCEWithLogitsLoss()

• Y = [ 1, 0, 0, 1 ]

• X = [ -3.9, -3.7, 5.8, 3 ]

• = [ 0.9802, 0.9759, 0.0030, 0.0474]


BCEWithLogitsLoss()

• Y = [ 1, 0, 0, 1 ]

• X = [ -3.9, -3.7, 5.8, 3 ]

• = [ 0.9802, 0.9759, 0.0030, 0.0474 ]

• = [ -0.0200, -0.0244, -5.8030, -3.0486 ]


BCEWithLogitsLoss()

• = [ -0.0200, -0.0244, -5.8030, -3.0486 ]

• Sum = - [ -0.0244 -5.8030 ] = 5.8274

• Y=0 일 때, loss : 5.8274


BCEWithLogitsLoss()

• Y = 1일 때의 loss : 3.968

• Y = 0일 때의 loss : 5.8274

• model의 하나의 train data에 대한 BCEWithLogitsLoss = 9.7954


BCEWithLogitsLoss()

True = [ 1, 0, 0, 1 ]

BERT
• “이 카페 디저트 진짜 맛있 Multi-Label
Predict = [ -3.9, -3.7, 5.8, 3 ]

구요, 깨끗하기도 합니다.” Classi cation


Model
BCEWithLogitsLoss = 9.7954

Back Propagation
fi

You might also like