- Bert를 이용한 준지도학습 모델에 GradCAM을 추가한 실험입니다.
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]
- python >= 3.8
- pandas
- numpy
- pytorch
- transformers
- seaborn
- nltk
- cv2
- pip install pandas
- pip install numpy
- pip install seaborn
- pip intsall nltk
- pip intsall transformers
- pip3 install torch==1.10.0+cu102 torchvision==0.11.1+cu102 torchaudio===0.10.0+cu102 -f https://download.pytorch.org/whl/cu102/torch_stable.html
-
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())
- 아래 Text Self training with XAI 실험에서 Bert+CNN 과 GradCAM을 활용한 코드 입니다.