-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_experiment.py
More file actions
107 lines (73 loc) · 3.69 KB
/
init_experiment.py
File metadata and controls
107 lines (73 loc) · 3.69 KB
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
from multi_logreg import register_pytree_node_dataclass
import datasets
import numpy as np
import jax.numpy as jnp
import jax
from preprocess_logreg import PreProcessLogReg
from argparse import Namespace
from os import path
from typing import Dict, Any
from zipfile import ZipFile
from typing import Tuple
from dataclasses import dataclass
Dataset = Tuple[jnp.ndarray, jnp.ndarray]
@jax.jit
def normalize(inputs: jnp.ndarray) -> jnp.ndarray:
"""Normalizes feature vectors so that they have unit L2-norm"""
return inputs / jnp.linalg.norm(inputs, ord=2, axis=1, keepdims=True)
@register_pytree_node_dataclass
@dataclass(eq=True, frozen=True)
class MyLogReg(PreProcessLogReg):
"""Multi-class logistic regression, with L2 normalization applied to each input"""
def preprocess(self, inputs: jnp.ndarray) -> jnp.ndarray:
return normalize(inputs)
def mnist_binary(args: Namespace) -> Tuple[Namespace, Dataset, Dataset, MyLogReg]:
"""Initialize an experiment using Binary-MNIST (8 vs 3)"""
X, y, X_test, y_test = datasets.mnist_binary(8, neg_class=3, dtype=np.float64)
train = X, y
test = X_test, y_test
model = MyLogReg(lamb=args.lamb, epsilon=args.epsilon, delta=args.delta, sigma=args.sigma,
classes=np.array([0.0, 1.0]))
args.feature_min = 0.0 if args.feature_min is None else args.feature_min
args.feature_max = 1.0 if args.feature_max is None else args.feature_max
return args, train, test, model
def mnist(args: Namespace) -> Tuple[Namespace, Dataset, Dataset, MyLogReg]:
"""Initialize an experiment using MNIST"""
X, y, X_test, y_test = datasets.mnist(dtype=np.float64)
train = X, y
test = X_test, y_test
model = MyLogReg(lamb=args.lamb, epsilon=args.epsilon, delta=args.delta, sigma=args.sigma,
classes=np.arange(10.0))
args.feature_min = 0.0 if args.feature_min is None else args.feature_min
args.feature_max = 1.0 if args.feature_max is None else args.feature_max
return args, train, test, model
def fashion_mnist(args: Namespace) -> Tuple[Namespace, Dataset, Dataset, MyLogReg]:
"""Initialize an experiment using Fashion-MNIST"""
X, y, X_test, y_test = datasets.fashion_mnist(dtype=np.float64)
train = X, y
test = X_test, y_test
model = MyLogReg(lamb=args.lamb, epsilon=args.epsilon, delta=args.delta, sigma=args.sigma,
classes=np.arange(10.0))
args.feature_min = 0.0 if args.feature_min is None else args.feature_min
args.feature_max = 1.0 if args.feature_max is None else args.feature_max
return args, train, test, model
def har(args: Dict[str, Any]) -> Tuple[Dict[str, Any], Dataset, Dataset, MyLogReg]:
"""Initialize an experiment using HAR"""
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00240/UCI%20HAR%20Dataset.zip"
filename = "UCI_HAR_Dataset.zip"
datasets._download(url, filename)
arrays = {'X': 'UCI HAR Dataset/train/X_train.txt',
'y': 'UCI HAR Dataset/train/y_train.txt',
'X_test': 'UCI HAR Dataset/test/X_test.txt',
'y_test': 'UCI HAR Dataset/test/y_test.txt'}
with ZipFile(path.join(datasets._DATA, filename)) as z:
for a, p in arrays.items():
with z.open(p) as f:
arrays[a] = np.loadtxt(f)
train = arrays['X'], arrays['y']
test = arrays['X_test'], arrays['y_test']
model = MyLogReg(lamb=args.lamb, epsilon=args.epsilon, delta=args.delta, sigma=args.sigma,
classes=np.arange(1.0, 7.0))
args.feature_min = -1.0 if args.feature_min is None else args.feature_min
args.feature_max = 1.0 if args.feature_max is None else args.feature_max
return args, train, test, model