示例#1
0
import os
os.environ['ODIN'] = 'float32,gpu,%s,seed=12' % arg['backend']

from odin import backend as K
from odin import nnet as N
from odin import fuel, training
from six.moves import cPickle

# ===========================================================================
# Load data
# ===========================================================================
USE_MNIST_DATA = True if 'mnist' in arg['ds'].lower() else False

if USE_MNIST_DATA:
    ds = fuel.load_mnist()
else:
    ds = fuel.load_cifar10()

X = K.placeholder(shape=(None, ) + ds['X_train'].shape[1:], name='X')
y = K.placeholder(shape=(None, ), name='y', dtype='int32')

# ===========================================================================
# Build network
# ===========================================================================
ops = N.Sequence([
    N.Dimshuffle((0, 1, 2, 'x')) if USE_MNIST_DATA else None,
    N.BatchNorm(axes='auto'),
    N.Conv(32, (3, 3), strides=(1, 1), pad='same', activation=K.relu),
    N.Pool(pool_size=(2, 2), strides=None),
    N.Conv(64, (3, 3), strides=(1, 1), pad='same', activation=K.relu),
示例#2
0
    def test_mnist(self):
        ds = fuel.load_mnist()
        m = model.SequentialClassifier(N.Flatten(outdim=2),
                                       N.Dense(64, activation=K.relu),
                                       N.Dense(10, activation=K.softmax))
        m.set_inputs(
            K.placeholder(shape=(None, 28, 28), name='X',
                          dtype='float32')).set_outputs(
                              K.placeholder(shape=(None, ),
                                            name='y',
                                            dtype='int32'))
        # ====== query info ====== #
        m.path
        self.assertEqual(m.is_initialized, True)
        self.assertEqual(m.input_shape, (None, 28, 28))
        self.assertEqual(m.output_shape, (None, 10))
        # ====== training test ====== #
        m.set_training_info(learning_rate=0.001, n_epoch=3)
        m.fit(X=(ds['X_train'], ds['y_train']),
              X_valid=(ds['X_valid'], ds['y_valid']))
        score = m.score(ds['X_test'][:], ds['y_test'][:])
        self.assertEqual(
            score > 0.8,
            True,
            msg='Test if the model get reasonable results: %f accuracy' %
            score)
        # ====== make prediction and transform test ====== #
        np.random.seed(12)
        _ = np.random.rand(8, 28, 28)
        self.assertEqual(m.transform(_).shape, (8, 10))
        self.assertEqual(
            np.isclose(m.predict_proba(_).sum(-1), 1.).sum() == 8, True)
        self.assertEqual(len(m.predict(_)), 8)
        # ====== pickling test ====== #
        str_old = str(m)
        p_old = m.get_params(True)

        m = cPickle.loads(cPickle.dumps(m, protocol=cPickle.HIGHEST_PROTOCOL))
        str_new = str(m)
        p_new = m.get_params(True)
        # ====== test same configurations ====== #
        self.assertEqual(str_new, str_old)
        # ====== test same params ====== #
        for i, j in p_new.iteritems():
            k = p_old[i]
            for a, b in zip(j, k):
                self.assertEqual(np.array_equal(a, b), True)
        # ====== test set params ====== #
        params = m.get_params(deep=True)
        params_new = {}
        for n, p in params.iteritems():
            params_new[n] = [
                np.random.rand(*i.shape).astype('float32') for i in p
            ]
        m.set_params(**params_new)
        # test if equal new onces
        for i, j in m.get_params(deep=True).iteritems():
            k = params_new[i]
            for a, b in zip(j, k):
                self.assertEqual(np.array_equal(a, b), True)
        # ====== training test ====== #
        print('Re-train the model second time:')
        m.fit(X=(ds['X_train'], ds['y_train']),
              X_valid=(ds['X_valid'], ds['y_valid']))
        score = m.score(ds['X_test'][:], ds['y_test'][:])
        self.assertEqual(
            score > 0.8,
            True,
            msg='Test if the model get reasonable results: %f accuracy' %
            score)
示例#3
0
from __future__ import print_function, division, absolute_import

import os
os.environ['ODIN'] = 'float32,gpu,tensorflow'

import numpy as np

from odin import nnet as N, backend as K, fuel as F


# ===========================================================================
# Data and const
# ===========================================================================
ds = F.load_mnist()
print(ds)

# ===========================================================================
# Model
# ===========================================================================
input_desc = [
    N.VariableDesc(shape=(None, 28, 28), dtype='float32', name='X'),
    N.VariableDesc(shape=(None,), dtype='float32', name='y')
]
model = N.get_model_descriptor('ladder1')
K.set_training(True); y_train, cost = model(input_desc)
K.set_training(False); y_score = model()
示例#4
0
from __future__ import print_function, division, absolute_import

import os
os.environ['ODIN'] = 'float32,gpu,tensorflow'

import numpy as np

from odin import nnet as N, backend as K, fuel as F

# ===========================================================================
# Data and const
# ===========================================================================
ds = F.load_mnist()
print(ds)

# ===========================================================================
# Model
# ===========================================================================
input_desc = [
    N.VariableDesc(shape=(None, 28, 28), dtype='float32', name='X'),
    N.VariableDesc(shape=(None, ), dtype='float32', name='y')
]
model = N.get_model_descriptor('ladder1')
K.set_training(True)
y_train, cost = model(input_desc)
K.set_training(False)
y_score = model()