Skip to content

HohyunH/Bert_GradCAM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bert_GradCAM

  • Bert를 이용한 준지도학습 모델에 GradCAM을 추가한 실험입니다.

How to use


python main.py --max_len 512 --epochs 1 --batch 16 --label_batch 200 --masked True --xai True --test_num 10000 --class_num 100 --gpu_set choose[1,2,3,4]

Requirement

  • python >= 3.8
  • pandas
  • numpy
  • pytorch
  • transformers
  • seaborn
  • nltk
  • cv2

Command

Model Framework

image

  • BERT 이후에 붙혀진 CNN 에서 마지막 Convolution layer에서 Grad-CAM score를 산출한다.

  • 인공 신경망 연산 과정에서 계산되는 gradients 값과 activation 값의 연산으로 Grad-CAM score를 산출할 수 있다.

gradients = grad_cam.get_activations_gradient()    
activations = grad_cam.get_activations(sentence, masks).detach()

# global average pooling : 각 채널별로 평균 구함.
pooled_gradients = torch.mean(gradients, dim=[0, 2])
for k in range(gradients.shape[1]):
    activations[:, k, :] *= pooled_gradients[k]

grad_cam_score = torch.mean(activations, dim=1).squeeze()
grad_cam_score = grad_cam_score.view(1, -1).cpu().numpy()
grad_cam_score = cv2.resize(grad_cam_score, dsize=(512, 1))
grad_cam_score = np.multiply(grad_cam_score, masks.cpu().numpy())

문장내 단어 중요도 추출 예시

image

  • 아래 Text Self training with XAI 실험에서 Bert+CNN 과 GradCAM을 활용한 코드 입니다.

image

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published