-
Notifications
You must be signed in to change notification settings - Fork 27
/
utils.py
65 lines (51 loc) · 1.8 KB
/
utils.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
# -*- coding: utf-8 -*-
#
# Copyright © dawnranger.
#
# 2018-05-08 10:15 <[email protected]>
#
# Distributed under terms of the MIT license.
from __future__ import division, print_function
import numpy as np
import torch
from torch.utils.data import Dataset
def load_mnist(path='./data/mnist.npz'):
f = np.load(path)
x_train, y_train, x_test, y_test = f['x_train'], f['y_train'], f[
'x_test'], f['y_test']
f.close()
x = np.concatenate((x_train, x_test))
y = np.concatenate((y_train, y_test)).astype(np.int32)
x = x.reshape((x.shape[0], -1)).astype(np.float32)
x = np.divide(x, 255.)
print('MNIST samples', x.shape)
return x, y
class MnistDataset(Dataset):
def __init__(self):
self.x, self.y = load_mnist()
def __len__(self):
return self.x.shape[0]
def __getitem__(self, idx):
return torch.from_numpy(np.array(self.x[idx])), torch.from_numpy(
np.array(self.y[idx])), torch.from_numpy(np.array(idx))
#######################################################
# Evaluate Critiron
#######################################################
def cluster_acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
from sklearn.utils.linear_assignment_ import linear_assignment
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred.size