def test_minibatch_score_trainer(): X = np.random.random((100, 10)) Z = np.random.random((100, 2)) X, Z = theano_floatx(X, Z) data = { 'train': (X, Z), 'val': (X, Z), 'test': (X, Z) } cut_size = 10 class MyModel(mlp.Mlp): def score(self, X, Z): assert X.shape[0] <= cut_size return super(MyModel, self).score(X, Z) m = MyModel(10, [100], 2, ['tanh'], 'identity', 'squared', max_iter=10) score = MinibatchScore(cut_size, [0, 0]) trainer = Trainer( m, data, score=score, pause=lambda info: True, stop=lambda info: False) trainer.val_key = 'val' for _ in trainer.iter_fit(X, Z): break
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
def test_training_continuation(): if sys.platform == 'win32': raise SkipTest() # Make model and data for the test. X = np.random.random((100, 10)) Z = np.random.random((100, 2)) X, Z = theano_floatx(X, Z) data = { 'train': (X, Z), 'val': (X, Z), 'test': (X, Z) } optimizer = 'rmsprop', {'step_rate': 0.0001} m = mlp.Mlp(10, [2], 2, ['tanh'], 'identity', 'squared', max_iter=10, optimizer=optimizer) # Train the mdoel with a trainer for 2 epochs. stopper = climin.stops.OnSignal() stops = climin.stops.Any([stopper, climin.stops.AfterNIterations(5)]) t = Trainer( m, data, stop=stops, pause=climin.stops.always) t.val_key = 'val' for info in t.iter_fit(X, Z): os.kill(os.getpid(), stopper.sig) assert info['n_iter'] == 1
def test_minibatch_score_trainer(): X = np.random.random((100, 10)) X, = theano_floatx(X) cut_size = 10 class MyAutoEncoder(autoencoder.AutoEncoder): def score(self, X): assert X.shape[0] <= cut_size return super(MyAutoEncoder, self).score(X) m = MyAutoEncoder(10, [100], ['tanh'], 'identity', 'squared', tied_weights=True, max_iter=10) score = MinibatchScore(cut_size, [0]) trainer = Trainer(m, score=score, pause=lambda info: True, stop=lambda info: False) trainer.eval_data = {'val': (X, )} trainer.val_key = 'val' for _ in trainer.iter_fit(X): break
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
def test_minibatch_score_trainer(): X = np.random.random((100, 10)) Z = np.random.random((100, 2)) X, Z = theano_floatx(X, Z) data = {'train': (X, Z), 'val': (X, Z), 'test': (X, Z)} cut_size = 10 class MyModel(mlp.Mlp): def score(self, X, Z): assert X.shape[0] <= cut_size return super(MyModel, self).score(X, Z) m = MyModel(10, [100], 2, ['tanh'], 'identity', 'squared', max_iter=10) score = MinibatchScore(cut_size, [0, 0]) trainer = Trainer(m, data, score=score, pause=lambda info: True, stop=lambda info: False) trainer.val_key = 'val' for _ in trainer.iter_fit(X, Z): break
def test_training_continuation(): if sys.platform == 'win32': raise SkipTest() # Make model and data for the test. X = np.random.random((100, 10)) Z = np.random.random((100, 2)) X, Z = theano_floatx(X, Z) data = {'train': (X, Z), 'val': (X, Z), 'test': (X, Z)} optimizer = 'rmsprop', {'step_rate': 0.0001} m = mlp.Mlp(10, [2], 2, ['tanh'], 'identity', 'squared', max_iter=10, optimizer=optimizer) # Train the mdoel with a trainer for 2 epochs. stopper = climin.stops.OnSignal() stops = climin.stops.Any([stopper, climin.stops.AfterNIterations(5)]) t = Trainer(m, data, stop=stops, pause=climin.stops.always) t.val_key = 'val' for info in t.iter_fit(X, Z): os.kill(os.getpid(), stopper.sig) assert info['n_iter'] == 1
def new_trainer(pars, data): X, Z = data m = Mlp(2, [pars['n_hidden']], 1, hidden_transfers=[pars['hidden_transfer']], out_transfer='sigmoid', loss='bern_ces', optimizer=pars['optimizer']) climin.initialize.randomize_normal(m.parameters.data, 0, pars['par_std']) n_report = 100 interrupt = climin.stops.OnSignal() print dir(climin.stops) stop = climin.stops.Any([ climin.stops.AfterNIterations(10000), climin.stops.OnSignal(signal.SIGTERM), climin.stops.BetterThan('train_loss', 1e-4), ]) pause = climin.stops.ModuloNIterations(n_report) reporter = KeyPrinter(['n_iter', 'train_loss']) t = Trainer( m, stop=stop, pause=pause, report=reporter, interrupt=interrupt) t.val_key = 'train' t.eval_data['train'] = (X, Z) return t
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)
def new_trainer(self, pars, data): modules = ['theano', 'breze', 'climin', 'alchemie'] git_log(modules) copy_theanorc() m = Mlp(2, [pars['n_hidden']], 1, hidden_transfers=[pars['hidden_transfer']], out_transfer='sigmoid', loss='bern_ces', optimizer=pars['optimizer']) climin.initialize.randomize_normal(m.parameters.data, 0, pars['par_std']) n_report = 100 t = Trainer( model=m, data=data, stop=climin.stops.Any([ climin.stops.AfterNIterations(10000), climin.stops.NotBetterThanAfter(1e-1, 5000, key='val_loss') ]), pause=climin.stops.ModuloNIterations(n_report), report=OneLinePrinter( keys=['n_iter', 'runtime', 'train_loss', 'val_loss'], spaces=[6, '10.2f', '15.8f', '15.8f']), interrupt=climin.stops.OnSignal(), ) return t
def new_trainer(pars, data): m = Mlp(2, [pars['n_hidden']], 1, hidden_transfers=[pars['hidden_transfer']], out_transfer='sigmoid', loss='bern_ces', optimizer=pars['optimizer']) climin.initialize.randomize_normal(m.parameters.data, 0, pars['par_std']) n_report = 100 interrupt = climin.stops.OnSignal() print dir(climin.stops) stop = climin.stops.Any([ climin.stops.AfterNIterations(10000), climin.stops.OnSignal(signal.SIGTERM), climin.stops.NotBetterThanAfter(1e-1, 500, key='train_loss'), ]) pause = climin.stops.ModuloNIterations(n_report) reporter = KeyPrinter(['n_iter', 'train_loss']) t = Trainer(m, stop=stop, pause=pause, report=reporter, interrupt=interrupt) make_data_dict(t, data) return t
def test_checkpoint_trainer(): # Make model and data for the test. X = np.random.random((100, 10)) Z = np.random.random((100, 2)) X, Z = theano_floatx(X, Z) data = { 'train': (X, Z), 'val': (X, Z), 'test': (X, Z) } optimizer = 'rmsprop', {'step_rate': 0.0001} m = mlp.Mlp(10, [2], 2, ['tanh'], 'identity', 'squared', max_iter=10, optimizer=optimizer) # Train the mdoel with a trainer for 2 epochs. t = Trainer( m, data, stop=climin.stops.AfterNIterations(2), pause=climin.stops.always) t.val_key = 'val' t.fit() # Make a copy of the trainer. t2 = copy.deepcopy(t) t2.data = t.data 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() # 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() 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_unpickled.data = data t.stop = climin.stops.AfterNIterations(4) t_unpickled.fit() assert np.allclose( final_pars, t_unpickled.model.parameters.data, atol=5.e-3)
def test_minibatch_score_trainer(): X = np.random.random((100, 10)) X, = theano_floatx(X) cut_size = 10 class MyAutoEncoder(autoencoder.AutoEncoder): def score(self, X): assert X.shape[0] <= cut_size return super(MyAutoEncoder, self).score(X) m = MyAutoEncoder(10, [100], ['tanh'], 'identity', 'squared', tied_weights=True, max_iter=10) score = MinibatchScore(cut_size, [0]) trainer = Trainer( m, score=score, pause=lambda info: True, stop=lambda info: False) trainer.eval_data = {'val': (X,)} trainer.val_key = 'val' for _ in trainer.iter_fit(X): break