GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — torchTextClassifiers/tests/test_predict_no_grad.py at main · InseeFrLab/torchTextClassifiers

Expand file tree

/

test_predict_no_grad.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
62 lines (49 loc) · 2.37 KB
/

test_predict_no_grad.py

Copy path

File metadata and controls

62 lines (49 loc) · 2.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import pytest
import torch
from torchTextClassifiers import ModelConfig, torchTextClassifiers
from torchTextClassifiers.tokenizers import NGramTokenizer
def _build_ttc(texts):
tokenizer = NGramTokenizer(
min_count=1, min_n=2, max_n=4, num_tokens=50, len_word_ngrams=2, output_dim=20
)
tokenizer.train(list(texts))
model_config = ModelConfig(embedding_dim=8, num_classes=2)
return torchTextClassifiers(tokenizer=tokenizer, model_config=model_config)
def test_predict_does_not_track_gradients(sample_text_data):
"""predict() must run its forward pass without building an autograd graph (issue #111)."""
ttc = _build_ttc(sample_text_data)
recorded = {}
def hook(module, inputs, output):
recorded["grad_enabled"] = torch.is_grad_enabled()
recorded["requires_grad"] = output.requires_grad
handle = ttc.pytorch_model.register_forward_hook(hook)
try:
assert torch.is_grad_enabled()
out = ttc.predict(np.asarray(sample_text_data), raw_categorical_inputs=False)
finally:
handle.remove()
assert recorded["grad_enabled"] is False
assert recorded["requires_grad"] is False
assert out["prediction"].shape[0] == len(sample_text_data)
# Global grad mode is restored after predict()
assert torch.is_grad_enabled()
def test_predict_captum_works_under_user_no_grad(sample_text_data):
"""Captum attributions still work when predict() is called inside torch.no_grad()."""
pytest.importorskip("captum")
torch.manual_seed(0)
ttc = _build_ttc(sample_text_data)
# The classification head is zero-initialized, which would give all-zero
# attributions on an untrained model: randomize it so the check is meaningful.
with torch.no_grad():
ttc.pytorch_model.classification_head.net.weight.normal_()
X = np.asarray(sample_text_data)
ref = ttc.predict(X, raw_categorical_inputs=False, explain_with_captum=True)
with torch.no_grad():
out = ttc.predict(X, raw_categorical_inputs=False, explain_with_captum=True)
assert not torch.is_grad_enabled() # caller's grad mode untouched
attributions = out["captum_attributions"]
assert attributions.shape[:2] == (len(sample_text_data), 1)
assert torch.isfinite(attributions).all()
assert attributions.abs().sum() > 0
torch.testing.assert_close(attributions, ref["captum_attributions"])
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.