-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathops.py
90 lines (72 loc) · 2.92 KB
/
ops.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Fri May 12 22:54:50 2017
@author: Chin-Wei
"""
import cPickle as pickle
import gzip
import numpy as np
floatX = 'float32'
def onehot(labels, n_classes=10):
rval = np.zeros((len(labels), n_classes))
for n, label in enumerate(labels):
rval[n, label] == 1
return rval
def load_mnist(filename):
try:
tr,va,te = pickle.load(gzip.open('mnist.pkl.gz','r'))
except:
tr,va,te = pickle.load(gzip.open(filename,'r'))
tr_x,tr_y = tr
va_x,va_y = va
te_x,te_y = te
print tr_y.shape
# doesn't work on hades :/
try:
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(10)
tr_y = enc.fit_transform(tr_y.reshape((-1,1))).toarray().reshape(50000,10).astype(int)
va_y = enc.fit_transform(va_y.reshape((-1,1))).toarray().reshape(10000,10).astype(int)
te_y = enc.fit_transform(te_y.reshape((-1,1))).toarray().reshape(10000,10).astype(int)
except:
tr_y = onehot(tr_y)
va_y = onehot(va_y)
te_y = onehot(te_y)
f = lambda d:d.astype(floatX)
return (f(d) for d in [tr_x, tr_y, va_x, va_y, te_x, te_y])
def load_cifar10(filename,val=0.1,seed=1000):
tr_x, tr_y, te_x, te_y = pickle.load(open(filename,'r'))
enc = OneHotEncoder(10)
tr_y = enc.fit_transform(tr_y).toarray().reshape(50000,10).astype(int)
te_y = enc.fit_transform(te_y).toarray().reshape(10000,10).astype(int)
n = tr_x.shape[0]
trn_ind = set(range(n))
rng = np.random.RandomState(seed)
val_ind = rng.choice(n,int(n*val),False)
trn_ind = np.array(list(trn_ind.difference(val_ind)))
tr_x, tr_y, va_x, va_y = tr_x[trn_ind], tr_y[trn_ind], \
tr_x[val_ind], tr_y[val_ind]
f = lambda d:d.astype(floatX)
return (f(d) for d in [tr_x, tr_y, va_x, va_y, te_x, te_y])
def get_index(vec,key):
return np.arange(vec.shape[0])[key(vec)]
def load_cifar5(filename,val=0.1,seed=1000):
tr_x, tr_y, te_x, te_y = pickle.load(open(filename,'r'))
tr_ind = get_index(tr_y.flatten(),lambda y:y<=4)
te_ind = get_index(te_y.flatten(),lambda y:y<=4)
tr_x, tr_y = tr_x[tr_ind], tr_y[tr_ind]
te_x, te_y = te_x[te_ind], te_y[te_ind]
enc = OneHotEncoder(5)
tr_n, te_n = tr_x.shape[0], te_x.shape[0]
tr_y = enc.fit_transform(tr_y).toarray().reshape(tr_n,5).astype(int)
te_y = enc.fit_transform(te_y).toarray().reshape(te_n,5).astype(int)
n = tr_x.shape[0]
trn_ind = set(range(n))
rng = np.random.RandomState(seed)
val_ind = rng.choice(n,int(n*val),False)
trn_ind = np.array(list(trn_ind.difference(val_ind)))
tr_x, tr_y, va_x, va_y = tr_x[trn_ind], tr_y[trn_ind], \
tr_x[val_ind], tr_y[val_ind]
f = lambda d:d.astype(floatX)
return (f(d) for d in [tr_x, tr_y, va_x, va_y, te_x, te_y])