Skip to content

Commit 5c59cae

Browse files
v0.2.4
mapk
1 parent c66158d commit 5c59cae

5 files changed

Lines changed: 159 additions & 1 deletion

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,11 @@ notebooks/data/VBDP/test.csv
218218
notebooks/data/VBDP/testt.csv
219219
notebooks/data/VBDP/train.csv
220220
notebooks/data/VBDP/trainn.csv
221+
notebooks/data/MNIST/raw/t10k-images-idx3-ubyte
222+
notebooks/data/MNIST/raw/t10k-images-idx3-ubyte.gz
223+
notebooks/data/MNIST/raw/t10k-labels-idx1-ubyte
224+
notebooks/data/MNIST/raw/t10k-labels-idx1-ubyte.gz
225+
notebooks/data/MNIST/raw/train-images-idx3-ubyte
226+
notebooks/data/MNIST/raw/train-images-idx3-ubyte.gz
227+
notebooks/data/MNIST/raw/train-labels-idx1-ubyte
228+
notebooks/data/MNIST/raw/train-labels-idx1-ubyte.gz

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.2.3"
10+
version = "0.2.4"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/data/torch_hyper_dict.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,65 @@
11
{
2+
"Net_vbdp":
3+
{
4+
"l1": {
5+
"type": "int",
6+
"default": 5,
7+
"transform": "transform_power_2_int",
8+
"lower": 2,
9+
"upper": 9},
10+
"l2": {
11+
"type": "int",
12+
"default": 5,
13+
"transform": "transform_power_2_int",
14+
"lower": 2,
15+
"upper": 9},
16+
"lr_mult": {
17+
"type": "float",
18+
"default": 1.0,
19+
"transform": "None",
20+
"lower": 0.1,
21+
"upper": 10.0},
22+
"batch_size": {
23+
"type": "int",
24+
"default": 4,
25+
"transform": "transform_power_2_int",
26+
"lower": 1,
27+
"upper": 4},
28+
"epochs": {
29+
"type": "int",
30+
"default": 3,
31+
"transform": "transform_power_2_int",
32+
"lower": 3,
33+
"upper": 4},
34+
"k_folds": {
35+
"type": "int",
36+
"default": 1,
37+
"transform": "None",
38+
"lower": 1,
39+
"upper": 1},
40+
"patience": {
41+
"type": "int",
42+
"default": 5,
43+
"transform": "None",
44+
"lower": 2,
45+
"upper": 10
46+
},
47+
"optimizer": {
48+
"levels": ["Adadelta", "Adagrad", "Adam", "AdamW", "SparseAdam", "Adamax", "ASGD", "NAdam", "RAdam", "RMSprop", "Rprop", "SGD"],
49+
"type": "factor",
50+
"default": "SGD",
51+
"transform": "None",
52+
"class_name": "torch.optim",
53+
"core_model_parameter_type": "str",
54+
"lower": 0,
55+
"upper": 12},
56+
"sgd_momentum": {
57+
"type": "float",
58+
"default": 0.0,
59+
"transform": "None",
60+
"lower": 0.0,
61+
"upper": 1.0}
62+
},
263
"Net_fashionMNIST":
364
{
465
"l1": {

src/spotPython/torch/mapk.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import torch
2+
from torchmetrics import Metric
3+
import numpy as np
4+
5+
6+
class MAPK(Metric):
7+
"""Computes the mean average precision at k.
8+
Args:
9+
k: Number of predictions to consider
10+
dist_sync_on_step: Whether to sync the output across all GPUs
11+
Example:
12+
>>> from torchmetrics import MAPK
13+
>>> target = torch.tensor([0, 1, 2, 3])
14+
>>> preds = torch.tensor([[0, 1, 2, 3],
15+
... [0, 2, 1, 3],
16+
... [0, 1, 3, 2],
17+
... [0, 3, 1, 2]])
18+
>>> mapk = MAPK(k=3)
19+
>>> mapk(preds, target)
20+
tensor(0.3333)
21+
22+
>>> y_pred = torch.tensor([[0.5, 0.2, 0.2], # 0 is in top 2
23+
[0.3, 0.4, 0.2], # 1 is in top 2
24+
[0.2, 0.4, 0.3], # 2 is in top 2
25+
[0.7, 0.2, 0.1]]) # 2 isn't in top 2
26+
>>> y_true = torch.tensor([0, 1, 2, 2])
27+
>>> mapk_metric = MAPK(k=2)
28+
>>> mapk_metric.update(y_pred, y_true)
29+
>>> result = mapk_metric.compute()
30+
>>> print(result) # tensor(0.37500)
31+
"""
32+
33+
def __init__(self, k=3, dist_sync_on_step=False):
34+
super().__init__(dist_sync_on_step=dist_sync_on_step)
35+
self.k = k
36+
self.add_state("actual", default=[], dist_reduce_fx="cat")
37+
self.add_state("predicted", default=[], dist_reduce_fx="cat")
38+
39+
def update(self, y_pred: torch.Tensor, y: torch.Tensor):
40+
sorted_prediction_ids = np.argsort(-y_pred.cpu().numpy(), axis=1)
41+
top_k_prediction_ids = sorted_prediction_ids[:, : self.k]
42+
self.actual.append(y.cpu().numpy().reshape(-1, 1))
43+
self.predicted.append(top_k_prediction_ids)
44+
45+
def compute(self):
46+
actual = np.concatenate(self.actual)
47+
predicted = np.concatenate(self.predicted)
48+
return self.mapk(actual, predicted)
49+
50+
@staticmethod
51+
def apk(actual, predicted, k=10):
52+
if len(predicted) > k:
53+
predicted = predicted[:k]
54+
score = 0.0
55+
num_hits = 0.0
56+
for i, p in enumerate(predicted):
57+
if p in actual and p not in predicted[:i]:
58+
num_hits += 1.0
59+
score += num_hits / (i + 1.0)
60+
if not actual:
61+
return 0.0
62+
return score / min(len(actual), k)
63+
64+
def mapk(self, actual, predicted):
65+
return np.mean([self.apk(a, p, self.k) for a, p in zip(actual, predicted)])

src/spotPython/torch/netvbdp.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from torch import nn
2+
import spotPython.torch.netcore as netcore
3+
4+
5+
class Net_vbdp(netcore.Net_Core):
6+
def __init__(self, l1, l2, lr_mult, batch_size, epochs, k_folds, patience, optimizer, sgd_momentum):
7+
super(Net_vbdp, self).__init__(
8+
lr_mult=lr_mult,
9+
batch_size=batch_size,
10+
epochs=epochs,
11+
k_folds=k_folds,
12+
patience=patience,
13+
optimizer=optimizer,
14+
sgd_momentum=sgd_momentum,
15+
)
16+
self.flatten = nn.Flatten()
17+
self.linear_relu_stack = nn.Sequential(
18+
nn.Linear(64, l1), nn.ReLU(), nn.Linear(l1, l2), nn.ReLU(), nn.Linear(l2, 11)
19+
)
20+
21+
def forward(self, x):
22+
x = self.flatten(x)
23+
logits = self.linear_relu_stack(x)
24+
return logits

0 commit comments

Comments
 (0)