|
32 | 32 |
|
33 | 33 | # Construct the set of unique labels |
34 | 34 | targets = [x.item() for x in labels] |
35 | | - unique_labels = set(targets) |
| 35 | + unique_labels = list(set(targets)) |
| 36 | + |
| 37 | + model_format = { |
| 38 | + "num_layers": 4, |
| 39 | + "layers": [ |
| 40 | + { |
| 41 | + "layer_type": "Linear_LRP", |
| 42 | + "in_dim": 50, |
| 43 | + "out_dim": 64, |
| 44 | + "act": "ReLU", |
| 45 | + "dropout": 0.1, |
| 46 | + "batch_norm": False |
| 47 | + }, |
| 48 | + { |
| 49 | + "layer_type": "Linear_LRP", |
| 50 | + "in_dim": 64, |
| 51 | + "out_dim": 128, |
| 52 | + "act": "ReLU", |
| 53 | + "dropout": 0.0, |
| 54 | + "batch_norm": False |
| 55 | + }, |
| 56 | + { |
| 57 | + "layer_type": "Linear_LRP", |
| 58 | + "in_dim": 128, |
| 59 | + "out_dim": 32, |
| 60 | + "act": "ReLU", |
| 61 | + "dropout": 0.0, |
| 62 | + "batch_norm": False |
| 63 | + }, |
| 64 | + { |
| 65 | + "layer_type": "Linear_LRP", |
| 66 | + "in_dim": 32, |
| 67 | + "out_dim": 1, |
| 68 | + "act": "Sigmoid", |
| 69 | + "dropout": 0.0, |
| 70 | + "batch_norm": False |
| 71 | + }, |
| 72 | + ] |
| 73 | + } |
| 74 | + |
| 75 | + model_config = ModelConfig(model_format) |
36 | 76 |
|
37 | 77 | # Check if the models have been saved |
38 | 78 | if os.path.exists("models.pkl"): |
39 | 79 | models = pickle.load(open("models.pkl", "rb")) |
40 | 80 | print("Models loaded") |
41 | 81 | else: |
42 | 82 | print("Models not found") |
43 | | - model_format = { |
44 | | - "num_layers": 4, |
45 | | - "layers": [ |
46 | | - { |
47 | | - "layer_type": "Linear_LRP", |
48 | | - "in_dim": 50, |
49 | | - "out_dim": 64, |
50 | | - "act": "ReLU", |
51 | | - "dropout": 0.1, |
52 | | - "batch_norm": False |
53 | | - }, |
54 | | - { |
55 | | - "layer_type": "Linear_LRP", |
56 | | - "in_dim": 64, |
57 | | - "out_dim": 128, |
58 | | - "act": "ReLU", |
59 | | - "dropout": 0.0, |
60 | | - "batch_norm": False |
61 | | - }, |
62 | | - { |
63 | | - "layer_type": "Linear_LRP", |
64 | | - "in_dim": 128, |
65 | | - "out_dim": 32, |
66 | | - "act": "ReLU", |
67 | | - "dropout": 0.0, |
68 | | - "batch_norm": False |
69 | | - }, |
70 | | - { |
71 | | - "layer_type": "Linear_LRP", |
72 | | - "in_dim": 32, |
73 | | - "out_dim": 1, |
74 | | - "act": "Sigmoid", |
75 | | - "dropout": 0.0, |
76 | | - "batch_norm": False |
77 | | - }, |
78 | | - ] |
79 | | - } |
80 | | - |
81 | | - model_config = ModelConfig(model_format) |
82 | 83 |
|
83 | 84 | training_config = TrainingConfig(0.01, 1, batch_size) |
84 | 85 |
|
85 | 86 | models = {} |
86 | 87 |
|
| 88 | + # k = unique_labels[0] |
87 | 89 | for k in unique_labels: |
88 | 90 | print("Training model for class", k) |
89 | 91 | # Create a copy of X |
|
107 | 109 | explanations = pickle.load(open("explanations.pkl", "rb")) |
108 | 110 | print("Explanations loaded") |
109 | 111 | else: |
| 112 | + X.requires_grad = True |
| 113 | + |
110 | 114 | explanations = {} |
111 | 115 |
|
| 116 | + if model_config.layers[-1]["act"] == "Sigmoid": |
| 117 | + criterion = nn.BCELoss() |
| 118 | + else: |
| 119 | + criterion = nn.BCEWithLogitsLoss() |
| 120 | + |
| 121 | + # k = unique_labels[0] |
112 | 122 | for k in unique_labels: |
113 | 123 | print("Explaining model for class", k) |
114 | 124 | model = models[k] |
115 | 125 |
|
116 | 126 | predictions = model.forward(X, explain=True, rule="alpha2beta1") |
117 | 127 |
|
118 | | - predictions = predictions[torch.arange(batch_size), predictions.max(1)[1]] # Choose maximizing output neuron |
119 | | - |
120 | | - predictions = predictions.sum() |
| 128 | + pred = predictions.sum() |
121 | 129 |
|
122 | | - predictions.backward() |
| 130 | + loss = criterion(predictions, torch.reshape(torch.tensor([1 if x == k else 0 for x in labels]), (-1, 1)).float()) |
123 | 131 |
|
124 | | - explanation = X.grad |
| 132 | + pred.backward() |
125 | 133 |
|
126 | | - explanations[k] = explanation |
| 134 | + explanations[k] = X.grad |
127 | 135 |
|
128 | 136 | # Save the explanations |
129 | 137 | pickle.dump(explanations, open("explanations.pkl", "wb")) |
0 commit comments