예제 #1
0
def test_training_continuation():
    # Make model and data for the test.
    X = np.random.random((10, 2))
    X, = theano_floatx(X)
    optimizer = 'gd'
    m = autoencoder.AutoEncoder(2, [2], ['tanh'],
                                'identity',
                                'squared',
                                tied_weights=True,
                                max_iter=10,
                                optimizer=optimizer)

    # Train the mdoel with a trainer for 2 epochs.
    stopper = climin.stops.OnSignal()
    print stopper.sig
    stops = climin.stops.Any([stopper, climin.stops.AfterNIterations(5)])
    t = Trainer(m, stop=stops, pause=climin.stops.always)

    t.val_key = 'val'
    t.eval_data = {'val': (X, )}
    killed = False
    for info in t.iter_fit(X):
        os.kill(os.getpid(), stopper.sig)

    assert info['n_iter'] == 1
예제 #2
0
def test_autoencoder():
    X = np.random.random((100, 10))
    X, = theano_floatx(X)

    m = autoencoder.AutoEncoder(10, [100], ['tanh'],
                                'identity',
                                'squared',
                                tied_weights=True,
                                max_iter=10)
    m.fit(X)
    m.score(X)
    m.transform(X)
예제 #3
0
def test_checkpoint_trainer():
    # Make model and data for the test.
    X = np.random.random((10, 2))
    X, = theano_floatx(X)
    optimizer = 'rmsprop', {'step_rate': 0.0001}
    m = autoencoder.AutoEncoder(2, [2], ['tanh'],
                                'identity',
                                'squared',
                                tied_weights=True,
                                max_iter=10,
                                optimizer=optimizer)

    # Train the mdoel with a trainer for 2 epochs.
    t = Trainer(m,
                stop=climin.stops.AfterNIterations(2),
                pause=climin.stops.always)
    t.val_key = 'val'
    t.eval_data = {'val': (X, )}
    t.fit(X)

    # Make a copy of the trainer.
    t2 = copy.deepcopy(t)
    intermediate_pars = t2.model.parameters.data.copy()
    intermediate_info = t2.current_info.copy()

    # Train original for 2 more epochs.
    t.stop = climin.stops.AfterNIterations(4)
    t.fit(X)

    # Check that the snapshot has not changed
    assert np.all(t2.model.parameters.data == intermediate_pars)

    final_pars = t.model.parameters.data.copy()
    final_info = t.current_info.copy()

    check_infos(intermediate_info, t2.current_info)

    t2.stop = climin.stops.AfterNIterations(4)
    t2.fit(X)
    check_infos(final_info, t2.current_info)

    assert np.allclose(final_pars, t2.model.parameters.data)

    t_pickled = cPickle.dumps(t2)
    t_unpickled = cPickle.loads(t_pickled)
    t.stop = climin.stops.AfterNIterations(4)

    t_unpickled.fit(X)

    assert np.allclose(final_pars,
                       t_unpickled.model.parameters.data,
                       atol=5.e-3)