-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.py
270 lines (222 loc) · 11.4 KB
/
metric.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
On Class Distributions Induced by Nearest Neighbor Graphs for Node Classification of Tabular Data
File: metric.py
Authors: Federico Errica ([email protected])
NEC Laboratories Europe GmbH, Copyright (c) 2023, All rights reserved.
THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
PROPRIETARY INFORMATION ---
SOFTWARE LICENSE AGREEMENT
ACADEMIC OR NON-PROFIT ORGANIZATION NONCOMMERCIAL RESEARCH USE ONLY
BY USING OR DOWNLOADING THE SOFTWARE, YOU ARE AGREEING TO THE TERMS OF THIS
LICENSE AGREEMENT. IF YOU DO NOT AGREE WITH THESE TERMS, YOU MAY NOT USE OR
DOWNLOAD THE SOFTWARE.
This is a license agreement ("Agreement") between your academic institution
or non-profit organization or self (called "Licensee" or "You" in this
Agreement) and NEC Laboratories Europe GmbH (called "Licensor" in this
Agreement). All rights not specifically granted to you in this Agreement
are reserved for Licensor.
RESERVATION OF OWNERSHIP AND GRANT OF LICENSE: Licensor retains exclusive
ownership of any copy of the Software (as defined below) licensed under this
Agreement and hereby grants to Licensee a personal, non-exclusive,
non-transferable license to use the Software for noncommercial research
purposes, without the right to sublicense, pursuant to the terms and
conditions of this Agreement. NO EXPRESS OR IMPLIED LICENSES TO ANY OF
LICENSOR'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. As used in this
Agreement, the term "Software" means (i) the actual copy of all or any
portion of code for program routines made accessible to Licensee by Licensor
pursuant to this Agreement, inclusive of backups, updates, and/or merged
copies permitted hereunder or subsequently supplied by Licensor, including
all or any file structures, programming instructions, user interfaces and
screen formats and sequences as well as any and all documentation and
instructions related to it, and (ii) all or any derivatives and/or
modifications created or made by You to any of the items specified in (i).
CONFIDENTIALITY/PUBLICATIONS: Licensee acknowledges that the Software is
proprietary to Licensor, and as such, Licensee agrees to receive all such
materials and to use the Software only in accordance with the terms of this
Agreement. Licensee agrees to use reasonable effort to protect the Software
from unauthorized use, reproduction, distribution, or publication. All
publication materials mentioning features or use of this software must
explicitly include an acknowledgement the software was developed by NEC
Laboratories Europe GmbH.
COPYRIGHT: The Software is owned by Licensor.
PERMITTED USES: The Software may be used for your own noncommercial
internal research purposes. You understand and agree that Licensor is not
obligated to implement any suggestions and/or feedback you might provide
regarding the Software, but to the extent Licensor does so, you are not
entitled to any compensation related thereto.
DERIVATIVES: You may create derivatives of or make modifications to the
Software, however, You agree that all and any such derivatives and
modifications will be owned by Licensor and become a part of the Software
licensed to You under this Agreement. You may only use such derivatives and
modifications for your own noncommercial internal research purposes, and you
may not otherwise use, distribute or copy such derivatives and modifications
in violation of this Agreement.
BACKUPS: If Licensee is an organization, it may make that number of copies
of the Software necessary for internal noncommercial use at a single site
within its organization provided that all information appearing in or on the
original labels, including the copyright and trademark notices are copied
onto the labels of the copies.
USES NOT PERMITTED: You may not distribute, copy or use the Software except
as explicitly permitted herein. Licensee has not been granted any trademark
license as part of this Agreement. Neither the name of NEC Laboratories
Europe GmbH nor the names of its contributors may be used to endorse or
promote products derived from this Software without specific prior written
permission.
You may not sell, rent, lease, sublicense, lend, time-share or transfer, in
whole or in part, or provide third parties access to prior or present
versions (or any parts thereof) of the Software.
ASSIGNMENT: You may not assign this Agreement or your rights hereunder
without the prior written consent of Licensor. Any attempted assignment
without such consent shall be null and void.
TERM: The term of the license granted by this Agreement is from Licensee's
acceptance of this Agreement by downloading the Software or by using the
Software until terminated as provided below.
The Agreement automatically terminates without notice if you fail to comply
with any provision of this Agreement. Licensee may terminate this Agreement
by ceasing using the Software. Upon any termination of this Agreement,
Licensee will delete any and all copies of the Software. You agree that all
provisions which operate to protect the proprietary rights of Licensor shall
remain in force should breach occur and that the obligation of
confidentiality described in this Agreement is binding in perpetuity and, as
such, survives the term of the Agreement.
FEE: Provided Licensee abides completely by the terms and conditions of this
Agreement, there is no fee due to Licensor for Licensee's use of the
Software in accordance with this Agreement.
DISCLAIMER OF WARRANTIES: THE SOFTWARE IS PROVIDED "AS-IS" WITHOUT WARRANTY
OF ANY KIND INCLUDING ANY WARRANTIES OF PERFORMANCE OR MERCHANTABILITY OR
FITNESS FOR A PARTICULAR USE OR PURPOSE OR OF NON- INFRINGEMENT. LICENSEE
BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE SOFTWARE AND
RELATED MATERIALS.
SUPPORT AND MAINTENANCE: No Software support or training by the Licensor is
provided as part of this Agreement.
EXCLUSIVE REMEDY AND LIMITATION OF LIABILITY: To the maximum extent
permitted under applicable law, Licensor shall not be liable for direct,
indirect, special, incidental, or consequential damages or lost profits
related to Licensee's use of and/or inability to use the Software, even if
Licensor is advised of the possibility of such damage.
EXPORT REGULATION: Licensee agrees to comply with any and all applicable
export control laws, regulations, and/or other laws related to embargoes and
sanction programs administered by law.
SEVERABILITY: If any provision(s) of this Agreement shall be held to be
invalid, illegal, or unenforceable by a court or other tribunal of competent
jurisdiction, the validity, legality and enforceability of the remaining
provisions shall not in any way be affected or impaired thereby.
NO IMPLIED WAIVERS: No failure or delay by Licensor in enforcing any right
or remedy under this Agreement shall be construed as a waiver of any future
or other exercise of such right or remedy by Licensor.
GOVERNING LAW: This Agreement shall be construed and enforced in accordance
with the laws of Germany without reference to conflict of laws principles.
You consent to the personal jurisdiction of the courts of this country and
waive their rights to venue outside of Germany.
ENTIRE AGREEMENT AND AMENDMENTS: This Agreement constitutes the sole and
entire agreement between Licensee and Licensor as to the matter set forth
herein and supersedes any previous agreements, understandings, and
arrangements between the parties relating hereto.
THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
"""
from typing import Tuple, List
import torch
from pydgn.training.callback.metric import (
MulticlassClassification,
Classification,
MulticlassAccuracy,
)
from sklearn.metrics import f1_score
from torch import softmax
class MulticlassClassification(MulticlassClassification):
def get_predictions_and_targets(
self, targets: torch.Tensor, *outputs: List[torch.Tensor]
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Returns output[0] as predictions and dataset targets.
Squeezes the first dimension of output and targets to get
single vectors.
Args:
targets (:class:`torch.Tensor`): ground truth
outputs (List[:class:`torch.Tensor`]): outputs of the model
Returns:
A tuple of tensors (predicted_values, target_values)
"""
pred = outputs[0]
targets = outputs[2][0]
if len(targets.shape) == 2:
targets = targets.squeeze(dim=1)
return pred, targets
class MulticlassAccuracy(MulticlassAccuracy):
def get_predictions_and_targets(
self, targets: torch.Tensor, *outputs: List[torch.Tensor]
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Returns output[0] as predictions and dataset targets.
Squeezes the first dimension of output and targets to get
single vectors.
Args:
targets (:class:`torch.Tensor`): ground truth
outputs (List[:class:`torch.Tensor`]): outputs of the model
Returns:
A tuple of tensors (predicted_values, target_values)
"""
pred = outputs[0]
correct = self._get_correct(pred)
targets = outputs[2][0]
if len(targets.shape) == 2:
targets = targets.squeeze(dim=1)
return correct, targets
class MicroF1Score(Classification):
@property
def name(self) -> str:
"""
The name of the loss to be used in configuration files and displayed
on Tensorboard
"""
return "Macro F1 Score"
def __init__(
self,
use_as_loss=False,
reduction="mean",
accumulate_over_epoch: bool = True,
force_cpu: bool = True,
device: str = "cpu",
):
super().__init__(
use_as_loss=use_as_loss,
reduction=reduction,
accumulate_over_epoch=accumulate_over_epoch,
force_cpu=force_cpu,
device=device,
)
self.metric = f1_score
def get_predictions_and_targets(
self, targets: torch.Tensor, *outputs: List[torch.Tensor]
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Returns output[0] as predictions and dataset targets.
Squeezes the first dimension of output and targets to get
single vectors.
Args:
targets (:class:`torch.Tensor`): ground truth
outputs (List[:class:`torch.Tensor`]): outputs of the model
Returns:
A tuple of tensors (predicted_values, target_values)
"""
pred = outputs[0].argmax(dim=1)
# override targets because we are taking a subset of the nodes in the graph
targets = outputs[2][0]
if len(targets.shape) == 2:
targets = targets.squeeze(dim=1)
return pred, targets
def compute_metric(
self, targets: torch.Tensor, predictions: torch.Tensor
) -> torch.tensor:
"""
Applies a regression metric
(to be subclassed as it is None in this class)
Args:
targets (:class:`torch.Tensor`): tensor of ground truth values
predictions (:class:`torch.Tensor`):
tensor of predictions of the model
Returns:
A tensor with the metric value
"""
metric = self.metric(targets, predictions, average="macro")
return metric