optim/optim_nn_mnist.py

188 lines
5.5 KiB
Python
Raw Normal View History

2017-02-27 14:52:39 -08:00
#!/usr/bin/env python3
from optim_nn import *
2017-02-27 16:36:04 -08:00
from optim_nn_core import _f
2017-02-27 14:52:39 -08:00
#np.random.seed(42069)
2017-03-12 17:41:18 -07:00
use_emnist = False
2017-02-27 16:36:04 -08:00
measure_every_epoch = True
2017-03-12 17:41:18 -07:00
if use_emnist:
lr = 0.01
epochs = 48
starts = 2
bs = 200
sgdr = True
restart_decay = 0.5
n_dense = 0
n_denses = 2
new_dims = (28, 28)
activation = GeluApprox
log_fn = 'emnist_losses.npz'
fn = 'emnist-balanced.npz'
mnist_dim = 28
mnist_classes = 47
else:
lr = 0.0032
epochs = 125
starts = 5
bs = 200
activation = Relu
sgdr = False
restart_decay = 0.5
n_dense = 1
n_denses = 1
new_dims = (4, 12)
log_fn = 'mnist_losses.npz'
fn = 'mnist.npz'
mnist_dim = 28
mnist_classes = 10
2017-02-27 14:52:39 -08:00
def get_mnist(fn='mnist.npz'):
import os.path
2017-03-12 17:41:18 -07:00
if fn == 'mnist.npz' and not os.path.exists(fn):
2017-02-27 14:52:39 -08:00
from keras.datasets import mnist
from keras.utils.np_utils import to_categorical
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, mnist_dim, mnist_dim)
X_test = X_test.reshape(X_test.shape[0], 1, mnist_dim, mnist_dim)
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
Y_train = to_categorical(y_train, mnist_classes)
Y_test = to_categorical(y_test, mnist_classes)
np.savez_compressed(fn,
X_train=X_train,
Y_train=Y_train,
X_test=X_test,
Y_test=Y_test)
lament("mnist successfully saved to", fn)
lament("please re-run this program to continue")
sys.exit(1)
with np.load(fn) as f:
return f['X_train'], f['Y_train'], f['X_test'], f['Y_test']
2017-03-12 17:41:18 -07:00
inputs, outputs, valid_inputs, valid_outputs = get_mnist(fn)
2017-02-27 14:52:39 -08:00
x = Input(shape=inputs.shape[1:])
y = x
2017-03-12 17:41:18 -07:00
y = y.feed(Reshape(new_shape=(mnist_dim, mnist_dim)))
for i in range(n_denses):
if i > 0:
y = y.feed(activation())
y = y.feed(Denses(new_dims[0], axis=0, init=init_he_normal))
y = y.feed(Denses(new_dims[1], axis=1, init=init_he_normal))
2017-02-27 14:52:39 -08:00
y = y.feed(Flatten())
2017-03-12 17:41:18 -07:00
for i in range(n_dense):
if i > 0:
y = y.feed(activation())
y = y.feed(Dense(y.output_shape[0], init=init_he_normal))
y = y.feed(activation())
2017-02-27 14:52:39 -08:00
y = y.feed(Dense(mnist_classes, init=init_glorot_uniform))
y = y.feed(Softmax())
model = Model(x, y, unsafe=True)
optim = Adam()
2017-03-12 17:41:18 -07:00
if sgdr:
2017-02-28 17:12:56 -08:00
learner = SGDR(optim, epochs=epochs//starts, rate=lr,
restarts=starts-1, restart_decay=restart_decay,
expando=lambda i:0)
else:
# learner = TriangularCLR(optim, epochs=epochs, lower_rate=0, upper_rate=lr,
# frequency=epochs//starts)
learner = SineCLR(optim, epochs=epochs, lower_rate=0, upper_rate=lr,
frequency=epochs//starts)
2017-02-27 14:52:39 -08:00
loss = CategoricalCrossentropy()
mloss = Accuracy()
ritual = Ritual(learner=learner, loss=loss, mloss=mloss)
2017-03-12 17:41:18 -07:00
#ritual = NoisyRitual(learner=learner, loss=loss, mloss=mloss,
# input_noise=1e-1, output_noise=3.2e-2, gradient_noise=1e-1)
2017-02-27 14:52:39 -08:00
log('parameters', model.param_count)
ritual.prepare(model)
2017-02-27 16:36:04 -08:00
batch_losses, batch_mlosses = [], []
train_losses, train_mlosses = [], []
valid_losses, valid_mlosses = [], []
2017-03-12 17:41:18 -07:00
train_confid, valid_confid = [], []
2017-02-27 16:36:04 -08:00
def measure_error(quiet=False):
2017-02-27 14:52:39 -08:00
def print_error(name, inputs, outputs, comparison=None):
loss, mloss, _, _ = ritual.test_batched(inputs, outputs, bs, return_losses='both')
2017-03-12 17:41:18 -07:00
c = Confidence()
predicted = ritual.model.forward(inputs)
confid = c.forward(predicted)
2017-02-27 16:36:04 -08:00
if not quiet:
log(name + " loss", "{:12.6e}".format(loss))
log(name + " accuracy", "{:6.2f}%".format(mloss * 100))
2017-03-12 17:41:18 -07:00
log(name + " confidence", "{:6.2f}%".format(confid * 100))
return loss, mloss, confid
2017-02-27 14:52:39 -08:00
2017-03-12 17:41:18 -07:00
#if not quiet:
loss, mloss, confid = print_error("train", inputs, outputs)
train_losses.append(loss)
train_mlosses.append(mloss)
train_confid.append(confid)
loss, mloss, confid = print_error("valid", valid_inputs, valid_outputs)
2017-02-27 16:36:04 -08:00
valid_losses.append(loss)
valid_mlosses.append(mloss)
2017-03-12 17:41:18 -07:00
valid_confid.append(confid)
2017-02-27 14:52:39 -08:00
measure_error()
while learner.next():
indices = np.arange(inputs.shape[0])
np.random.shuffle(indices)
shuffled_inputs = inputs[indices]
shuffled_outputs = outputs[indices]
2017-02-27 16:36:04 -08:00
avg_loss, avg_mloss, losses, mlosses = ritual.train_batched(
2017-02-27 14:52:39 -08:00
shuffled_inputs, shuffled_outputs,
batch_size=bs,
return_losses='both')
fmt = "rate {:10.8f}, loss {:12.6e}, accuracy {:6.2f}%"
2017-03-22 14:41:24 -07:00
log("epoch {}".format(learner.epoch),
2017-02-27 14:52:39 -08:00
fmt.format(learner.rate, avg_loss, avg_mloss * 100))
2017-02-27 16:36:04 -08:00
batch_losses += losses
batch_mlosses += mlosses
if measure_every_epoch:
2017-03-22 14:41:24 -07:00
quiet = learner.epoch != learner.epochs
2017-02-27 16:36:04 -08:00
measure_error(quiet=quiet)
if not measure_every_epoch:
measure_error()
if log_fn:
log('saving losses', log_fn)
np.savez_compressed(log_fn,
batch_losses =np.array(batch_losses, dtype=_f),
batch_mlosses=np.array(batch_mlosses, dtype=_f),
train_losses =np.array(train_losses, dtype=_f),
train_mlosses=np.array(train_mlosses, dtype=_f),
valid_losses =np.array(valid_losses, dtype=_f),
2017-03-12 17:41:18 -07:00
valid_mlosses=np.array(valid_mlosses, dtype=_f),
train_confid =np.array(train_confid, dtype=_f),
valid_confid =np.array(valid_confid, dtype=_f))