Python chainer.links.Classifier() Examples
The following are 29
code examples of chainer.links.Classifier().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
chainer.links
, or try the search function
.
Example #1
Source File: test_chainer.py From optuna with MIT License | 6 votes |
def test_chainer_pruning_extension() -> None: def objective(trial: optuna.trial.Trial) -> float: model = L.Classifier(chainer.Sequential(L.Linear(None, 2))) optimizer = chainer.optimizers.Adam() optimizer.setup(model) train_iter = chainer.iterators.SerialIterator(FixedValueDataset(), 16) updater = chainer.training.StandardUpdater(train_iter, optimizer) trainer = chainer.training.Trainer(updater, (1, "epoch")) trainer.extend( optuna.integration.chainer.ChainerPruningExtension(trial, "main/loss", (1, "epoch")) ) trainer.run(show_loop_exception_msg=False) return 1.0 study = optuna.create_study(pruner=DeterministicPruner(True)) study.optimize(objective, n_trials=1) assert study.trials[0].state == optuna.trial.TrialState.PRUNED study = optuna.create_study(pruner=DeterministicPruner(False)) study.optimize(objective, n_trials=1) assert study.trials[0].state == optuna.trial.TrialState.COMPLETE assert study.trials[0].value == 1.0
Example #2
Source File: test_multi_node_chain_list.py From chainer with MIT License | 6 votes |
def check_crossing_model(gpu, param): communicator, rank_next, rank_prev = create_communicator(gpu) n, d = 100, 10 X = np.random.randn(n, d).astype(param.dtype) Y = (np.random.rand(n) * 2).astype(np.int32) with chainer.using_config('dtype', param.dtype): if communicator.rank == 0: model = L.Classifier(Cross0( d, communicator, rank_next, rank_prev)) else: model = L.Classifier(Cross1( d, communicator, rank_next, rank_prev)) if gpu: model.to_device(cupy.cuda.Device()) X = chainer.backends.cuda.to_gpu(X) Y = chainer.backends.cuda.to_gpu(Y) for i in range(n): err = model(X[i:i + 1], Y[i:i + 1]) err.backward()
Example #3
Source File: test_multi_node_chain_list.py From chainer with MIT License | 5 votes |
def check_cycle_model(gpu, param): communicator, rank_next, rank_prev = create_communicator(gpu) n, d = 100, 10 with chainer.using_config('dtype', param.dtype): if communicator.rank == 0: X = np.random.randn(n, d).astype(param.dtype) Y = (np.random.rand(n) * 2).astype(np.int32) model = L.Classifier( Cycle0(d, communicator, rank_next, rank_prev)) if gpu: model.to_device(cupy.cuda.Device()) X = chainer.backends.cuda.to_gpu(X) Y = chainer.backends.cuda.to_gpu(Y) for i in range(n): err = model(X[i:i + 1], Y[i:i + 1]) err.backward() else: model = Cycle1( d, communicator, rank_next, rank_prev) if gpu: model.to_device(cupy.cuda.Device()) for i in range(n): err = model() err.backward()
Example #4
Source File: mnist.py From sagemaker-chainer-container with Apache License 2.0 | 5 votes |
def model_fn(model_dir): model = L.Classifier(MLP(1000, 10)) serializers.load_npz(os.path.join(model_dir, "model.npz"), model) return model.predictor
Example #5
Source File: single_machine_customer_script.py From sagemaker-chainer-container with Apache License 2.0 | 5 votes |
def model_fn(model_dir): model = L.Classifier(MLP(1000, 10)) serializers.load_npz(os.path.join(model_dir, 'model.npz'), model) return model.predictor
Example #6
Source File: distributed_customer_script_with_env_vars.py From sagemaker-chainer-container with Apache License 2.0 | 5 votes |
def model_fn(model_dir): model = L.Classifier(MLP(1000, 10)) serializers.load_npz(os.path.join(model_dir, 'model.npz'), model) return model.predictor
Example #7
Source File: single_machine_custom_loop.py From sagemaker-chainer-container with Apache License 2.0 | 5 votes |
def model_fn(model_dir): model = L.Classifier(MLP(1000, 10)) serializers.load_npz(os.path.join(model_dir, 'model.npz'), model) return model.predictor
Example #8
Source File: distributed_customer_script.py From sagemaker-chainer-container with Apache License 2.0 | 5 votes |
def model_fn(model_dir): model = L.Classifier(MLP(1000, 10)) serializers.load_npz(os.path.join(model_dir, 'model.npz'), model) return model.predictor
Example #9
Source File: test_multi_node_chain_list.py From chainer with MIT License | 5 votes |
def check_tuple_data_model(gpu, param): # This test only uses pairs (0, 1), (2, 3), ... (2m, 2m+1) communicator, rank_next, rank_prev = create_communicator(gpu) n, d = 100, 10 X = np.random.randn(n, d).astype(param.dtype) Y = (np.random.rand(n) * 2).astype(np.int32) with chainer.using_config('dtype', param.dtype): if communicator.rank % 2 == 0: if communicator.rank == communicator.size - 1: # in case 2m is the right end with odd number of nodes return model = L.Classifier( TupleDataParent(communicator, d, rank_next)) elif communicator.rank % 2 == 1: model = TupleDataChild(communicator, d, rank_prev) assert model is not None if gpu: model.to_device(cupy.cuda.Device()) X = chainer.backends.cuda.to_gpu(X) Y = chainer.backends.cuda.to_gpu(Y) for i in range(n): if communicator.rank % 2 == 0: err = model(X[i:i + 1], Y[i:i + 1]) elif communicator.rank % 2 == 1: err = model() assert err is not None err.backward()
Example #10
Source File: test_multi_node_chain_list.py From chainer with MIT License | 5 votes |
def check_branching_model(gpu, communicator, rank_next, rank_prev, parent_model, param): n, d = 100, 10 X = np.random.randn(n, d).astype(param.dtype) Y = (np.random.rand(n) * 2).astype(np.int32) with chainer.using_config('dtype', param.dtype): if communicator.rank == 0: rank_children = [rank for rank in range(1, communicator.size)] model = L.Classifier(parent_model( d, communicator, rank_children)) if gpu: model.to_device(cupy.cuda.Device()) X = chainer.backends.cuda.to_gpu(X) Y = chainer.backends.cuda.to_gpu(Y) for i in range(n): err = model(X[i:i + 1], Y[i:i + 1]) err.backward() else: model = BranchChild(d, communicator, 0) if gpu: model.to_device(cupy.cuda.Device()) for i in range(n): err = model() err.backward()
Example #11
Source File: test_checkpoint.py From chainer with MIT License | 5 votes |
def setup_mnist_trainer(self, display_log=False, use_chx=False): batchsize = 100 n_units = 100 comm = self.communicator model = L.Classifier(MLP(n_units, 10)) model.to_device(get_device(None, use_chx)) optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.Adam(), comm) optimizer.setup(model) if comm.rank == 0: train, test = chainer.datasets.get_mnist() else: train, test = None, None train = chainermn.scatter_dataset(train, comm, shuffle=True) test = chainermn.scatter_dataset(test, comm, shuffle=True) train_iter = chainer.iterators.SerialIterator(train, batchsize) test_iter = chainer.iterators.SerialIterator(test, batchsize, repeat=False, shuffle=False) updater = training.StandardUpdater( train_iter, optimizer ) return updater, optimizer, train_iter, test_iter, model
Example #12
Source File: test_multi_node_snapshot.py From chainer with MIT License | 5 votes |
def _prepare_multinode_snapshot(n, result): n_units = 100 batchsize = 10 comm = create_communicator('naive') model = L.Classifier(MLP(n_units, 10)) optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.Adam(), comm) optimizer.setup(model) if comm.rank == 0: train, _ = chainer.datasets.get_mnist() else: train, _ = None, None train = chainermn.scatter_dataset(train, comm, shuffle=True) train_iter = chainer.iterators.SerialIterator(train, batchsize) updater = StandardUpdater(train_iter, optimizer) trainer = Trainer(updater, out=result) snapshot = extensions.snapshot(target=updater, autoload=True) replica_sets = [] mn_snapshot = multi_node_snapshot(comm, snapshot, replica_sets) mn_snapshot.initialize(trainer) for _ in range(n): updater.update() return updater, mn_snapshot, trainer
Example #13
Source File: test_classifier.py From chainer with MIT License | 5 votes |
def setUp(self): self.link = links.Classifier(links.Linear(10, 3)) self.x = numpy.random.uniform(-1, 1, (5, 10)).astype(numpy.float32)
Example #14
Source File: test_classifier.py From chainer with MIT License | 5 votes |
def test_invalid_label_key_type(self): with six.assertRaisesRegex( self, TypeError, 'label_key must be int or str'): links.Classifier(links.Linear(10, 3), label_key=None)
Example #15
Source File: test_classifier.py From chainer with MIT License | 5 votes |
def check_call( self, gpu, label_key, args, kwargs, model_args, model_kwargs): init_kwargs = {'label_key': label_key} if self.accfun is not None: init_kwargs['accfun'] = self.accfun link = links.Classifier(chainer.Link(), **init_kwargs) if gpu: xp = cuda.cupy with testing.assert_warns(DeprecationWarning): link.to_gpu() else: xp = numpy link.compute_accuracy = self.compute_accuracy y = chainer.Variable(self.y) link.predictor = mock.MagicMock(return_value=y) loss = link(*args, **kwargs) link.predictor.assert_called_with(*model_args, **model_kwargs) self.assertTrue(hasattr(link, 'y')) self.assertIsNotNone(link.y) self.assertTrue(hasattr(link, 'loss')) xp.testing.assert_allclose(link.loss.data, loss.data) self.assertTrue(hasattr(link, 'accuracy')) if self.compute_accuracy: self.assertIsNotNone(link.accuracy) else: self.assertIsNone(link.accuracy)
Example #16
Source File: test_fail_on_nonnumber.py From chainer with MIT License | 5 votes |
def setUp(self): self.n_data = 4 self.n_epochs = 3 self.model = Model() self.classifier = links.Classifier(self.model) self.optimizer = chainer.optimizers.Adam() self.optimizer.setup(self.classifier) self.dataset = Dataset([i for i in range(self.n_data)]) self.iterator = chainer.iterators.SerialIterator( self.dataset, 1, shuffle=False) self.temp_dir = tempfile.mkdtemp()
Example #17
Source File: test_computational_graph.py From chainer with MIT License | 5 votes |
def _run_test(self, tempdir, initial_flag): n_data = 4 n_epochs = 3 outdir = os.path.join(tempdir, 'testresult') # Prepare model = Model() classifier = links.Classifier(model) optimizer = chainer.optimizers.Adam() optimizer.setup(classifier) dataset = Dataset([i for i in range(n_data)]) iterator = chainer.iterators.SerialIterator(dataset, 1, shuffle=False) updater = training.updaters.StandardUpdater(iterator, optimizer) trainer = training.Trainer(updater, (n_epochs, 'epoch'), out=outdir) extension = c.DumpGraph('main/loss', filename='test.dot') trainer.extend(extension) # Run with chainer.using_config('keep_graph_on_report', initial_flag): trainer.run() # Check flag history self.assertEqual(model.flag_history, [True] + [initial_flag] * (n_data * n_epochs - 1)) # Check the dumped graph graph_path = os.path.join(outdir, 'test.dot') with open(graph_path) as f: graph_dot = f.read() # Check that only the first iteration is dumped self.assertIn('Function1', graph_dot) self.assertNotIn('Function2', graph_dot) if c.is_graphviz_available(): self.assertTrue(os.path.exists(os.path.join(outdir, 'test.png')))
Example #18
Source File: chainer_compiler_test.py From chainer-compiler with MIT License | 5 votes |
def test_bn(device_name, translator, computation_order): if skip_check(device_name, translator, computation_order): pytest.skip() np.random.seed(40) if has_cupy: cupy.random.seed(40) batch_size = 3 in_size = 5 n_out = 10 device = chainer.get_device(device_name) device.use() bn = BN(in_size, n_out) bn.to_device(device) input = np.random.rand(batch_size, in_size, 1, 1).astype(np.float32) input = device.xp.array(input) target = device.xp.array(np.random.randint(n_out, size=batch_size)) bn_compiled = chainer_compiler.compile( bn, [input], translator=translator, computation_order=computation_order) model = L.Classifier(bn_compiled) model.to_device(device) old_avg_mean = CpuDevice().send(model.predictor.mc.bn.avg_mean.copy()) old_avg_var = CpuDevice().send(model.predictor.mc.bn.avg_var.copy()) loss, grads = _run_fwd_bwd(model, [input, target]) new_avg_mean = CpuDevice().send(model.predictor.mc.bn.avg_mean.copy()) new_avg_var = CpuDevice().send(model.predictor.mc.bn.avg_var.copy()) # running_mean and running_var should be updated assert not np.allclose(old_avg_mean, new_avg_mean) assert not np.allclose(old_avg_var, new_avg_var)
Example #19
Source File: distributed_mnist.py From sagemaker-python-sdk with Apache License 2.0 | 5 votes |
def model_fn(model_dir): model = L.Classifier(MLP(1000, 10)) serializers.load_npz(os.path.join(model_dir, "model.npz"), model) return model.predictor
Example #20
Source File: mnist.py From sagemaker-python-sdk with Apache License 2.0 | 5 votes |
def model_fn(model_dir): model = L.Classifier(MLP(1000, 10)) serializers.load_npz(os.path.join(model_dir, "model.npz"), model) return model.predictor
Example #21
Source File: chainer_compiler_test.py From chainer-compiler with MIT License | 4 votes |
def test_mnist(device_name, translator, computation_order): if skip_check(device_name, translator, computation_order): pytest.skip() np.random.seed(40) if has_cupy: cupy.random.seed(40) batch_size = 3 in_size = 5 n_units = 4 n_out = 10 device = chainer.get_device(device_name) device.use() mlp = MLP(n_units, n_out) model = L.Classifier(mlp) model.to_device(device) input = np.random.rand(batch_size, in_size).astype(np.float32) input = device.xp.array(input) target = device.xp.array(np.random.randint(n_out, size=batch_size)) def run_model(model): model.cleargrads() loss = model(input, target) loss.grad = device.xp.ones(loss.shape, loss.dtype) loss.backward() grads = [] for name, param in sorted(model.namedparams()): name = name.replace('/mc', '') grads.append((name, chainer.backend.to_chx(param.grad))) loss = chainer.backend.to_chx(loss.array) return loss, grads expected_loss, expected_grads = _run_fwd_bwd(model, [input, target]) mlp_compiled = chainer_compiler.compile( mlp, [input], translator=translator, computation_order=computation_order) model = L.Classifier(mlp_compiled) model.to_device(device) actual_loss, actual_grads = _run_fwd_bwd(model, [input, target]) _assert_allclose(expected_loss, actual_loss) assert len(expected_grads) == len(actual_grads) for (e_name, e_grad), (a_name, a_grad) in zip( expected_grads, actual_grads): assert e_name == a_name assert e_grad is not None, e_name assert a_grad is not None, a_name chainerx.testing.assert_allclose(e_grad, a_grad, rtol=1e-4)
Example #22
Source File: gen_mnist_mlp.py From chainer-compiler with MIT License | 4 votes |
def main_impl(args): # Set up a neural network to train # Classifier reports softmax cross entropy loss and accuracy at every # iteration, which will be used by the PrintReport extension below. model = MLP(args.unit, 10) # classifier = L.Classifier(model) classifier = MyClassifier(model, compute_accuracy=args.run_training) model = classifier if args.gpu >= 0: # Make a specified GPU current chainer.backends.cuda.get_device_from_id(args.gpu).use() model.to_gpu() # Copy the model to the GPU if args.run_training: run_training(args, model) return out_dir = 'out/backprop_test_mnist_mlp' x = np.random.random((args.batchsize, 784)).astype(np.float32) y = (np.random.random(args.batchsize) * 10).astype(np.int32) onehot = np.eye(10, dtype=x.dtype)[y] x = chainer.Variable(x, name='input') onehot = chainer.Variable(onehot, name='onehot') chainer.disable_experimental_feature_warning = True shutil.rmtree(out_dir, ignore_errors=True) onnx_chainer.export_testcase(model, (x, onehot), out_dir, output_grad=True, output_names='loss') # Revive this code if we need to test parameter update. # # trainer = create_trainer(args, model) # for step in range(2): # trainer.updater.update() # npz_filename = '%s/params_%d.npz' % (out_dir, step) # params_dir = '%s/params_%d' % (out_dir, step) # chainer.serializers.save_npz(npz_filename, model) # makedirs(params_dir) # npz_to_onnx.npz_to_onnx(npz_filename, os.path.join(params_dir, 'param'))
Example #23
Source File: gen_mnist_mlp.py From chainer-compiler with MIT License | 4 votes |
def run_training(args, model): trainer = create_trainer(args, model) # Dump a computational graph from 'loss' variable at the first iteration # The "main" refers to the target link of the "main" optimizer. trainer.extend(extensions.dump_graph('main/loss')) # Take a snapshot for each specified epoch frequency = args.epoch if args.frequency == -1 else max(1, args.frequency) trainer.extend(extensions.snapshot(), trigger=(frequency, 'epoch')) # Write a log of evaluation statistics for each epoch trainer.extend(extensions.LogReport()) # Save two plot images to the result dir if args.plot and extensions.PlotReport.available(): trainer.extend( extensions.PlotReport(['main/loss', 'validation/main/loss'], 'epoch', file_name='loss.png')) trainer.extend( extensions.PlotReport( ['main/accuracy', 'validation/main/accuracy'], 'epoch', file_name='accuracy.png')) # Print selected entries of the log to stdout # Here "main" refers to the target link of the "main" optimizer again, and # "validation" refers to the default name of the Evaluator extension. # Entries other than 'epoch' are reported by the Classifier link, called by # either the updater or the evaluator. trainer.extend(extensions.PrintReport( ['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', 'elapsed_time'])) # Print a progress bar to stdout trainer.extend(extensions.ProgressBar()) if args.resume: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
Example #24
Source File: chainer_simple.py From optuna with MIT License | 4 votes |
def objective(trial): # Model and optimizer model = L.Classifier(create_model(trial)) optimizer = create_optimizer(trial, model) # Dataset rng = np.random.RandomState(0) train, valid = chainer.datasets.get_mnist() train = chainer.datasets.SubDataset( train, 0, N_TRAIN_EXAMPLES, order=rng.permutation(len(train)) ) valid = chainer.datasets.SubDataset( valid, 0, N_VALID_EXAMPLES, order=rng.permutation(len(valid)) ) train_iter = chainer.iterators.SerialIterator(train, BATCHSIZE) valid_iter = chainer.iterators.SerialIterator(valid, BATCHSIZE, repeat=False, shuffle=False) # Trainer updater = chainer.training.StandardUpdater(train_iter, optimizer) trainer = chainer.training.Trainer(updater, (EPOCH, "epoch")) trainer.extend(chainer.training.extensions.Evaluator(valid_iter, model)) log_report_extension = chainer.training.extensions.LogReport(log_name=None) trainer.extend( chainer.training.extensions.PrintReport( [ "epoch", "main/loss", "validation/main/loss", "main/accuracy", "validation/main/accuracy", ] ) ) trainer.extend(log_report_extension) trainer.extend(ChainerPruningExtension(trial, "validation/main/accuracy", (1, "epoch"))) # Run! trainer.run(show_loop_exception_msg=False) # Set the user attributes such as loss and accuracy for train and validation sets log_last = log_report_extension.log[-1] for key, value in log_last.items(): trial.set_user_attr(key, value) # Return the validation accuracy return log_report_extension.log[-1]["validation/main/accuracy"]
Example #25
Source File: chainermn_simple.py From optuna with MIT License | 4 votes |
def objective(trial, comm): # Sample an architecture. model = L.Classifier(create_model(trial)) # Setup optimizer. optimizer = chainer.optimizers.MomentumSGD() optimizer.setup(model) optimizer = chainermn.create_multi_node_optimizer(optimizer, comm) # Setup dataset and iterator. Only worker 0 loads the whole dataset. # The dataset of worker 0 is evenly split and distributed to all workers. if comm.rank == 0: train, valid = chainer.datasets.get_mnist() rng = np.random.RandomState(0) train = chainer.datasets.SubDataset( train, 0, N_TRAIN_EXAMPLES, order=rng.permutation(len(train)) ) valid = chainer.datasets.SubDataset( valid, 0, N_VALID_EXAMPLES, order=rng.permutation(len(valid)) ) else: train, valid = None, None train = chainermn.scatter_dataset(train, comm, shuffle=True) valid = chainermn.scatter_dataset(valid, comm) train_iter = chainer.iterators.SerialIterator(train, BATCHSIZE, shuffle=True) valid_iter = chainer.iterators.SerialIterator(valid, BATCHSIZE, repeat=False, shuffle=False) # Setup trainer. updater = chainer.training.StandardUpdater(train_iter, optimizer) trainer = chainer.training.Trainer(updater, (EPOCH, "epoch")) if comm.rank == 0: trainer.extend(chainer.training.extensions.ProgressBar()) # Run training. trainer.run() # Evaluate. evaluator = chainer.training.extensions.Evaluator(valid_iter, model) evaluator = chainermn.create_multi_node_evaluator(evaluator, comm) report = evaluator() return report["main/accuracy"]
Example #26
Source File: chainermn_integration.py From optuna with MIT License | 4 votes |
def objective(trial, comm): # Sample an architecture. model = L.Classifier(create_model(trial)) # Setup optimizer. optimizer = chainer.optimizers.MomentumSGD() optimizer.setup(model) optimizer = chainermn.create_multi_node_optimizer(optimizer, comm) # Setup dataset and iterator. Only worker 0 loads the whole dataset. # The dataset of worker 0 is evenly split and distributed to all workers. if comm.rank == 0: train, valid = chainer.datasets.get_mnist() rng = np.random.RandomState(0) train = chainer.datasets.SubDataset( train, 0, N_TRAIN_EXAMPLES, order=rng.permutation(len(train)) ) valid = chainer.datasets.SubDataset( valid, 0, N_VALID_EXAMPLES, order=rng.permutation(len(valid)) ) else: train, valid = None, None train = chainermn.scatter_dataset(train, comm, shuffle=True) valid = chainermn.scatter_dataset(valid, comm) train_iter = chainer.iterators.SerialIterator(train, BATCHSIZE, shuffle=True) valid_iter = chainer.iterators.SerialIterator(valid, BATCHSIZE, repeat=False, shuffle=False) # Setup trainer. updater = chainer.training.StandardUpdater(train_iter, optimizer) trainer = chainer.training.Trainer(updater, (EPOCH, "epoch")) # Add Chainer extension for pruners. trainer.extend( optuna.integration.ChainerPruningExtension( trial, "validation/main/accuracy", (PRUNER_INTERVAL, "epoch") ) ) evaluator = chainer.training.extensions.Evaluator(valid_iter, model) trainer.extend(chainermn.create_multi_node_evaluator(evaluator, comm)) log_report_extension = chainer.training.extensions.LogReport(log_name=None) trainer.extend(log_report_extension) if comm.rank == 0: trainer.extend(chainer.training.extensions.ProgressBar()) # Run training. # Please set show_loop_exception_msg False to inhibit messages about TrialPruned exception. # ChainerPruningExtension raises TrialPruned exception to stop training, and # trainer shows some messages every time it receive TrialPruned. trainer.run(show_loop_exception_msg=False) # Evaluate. evaluator = chainer.training.extensions.Evaluator(valid_iter, model) evaluator = chainermn.create_multi_node_evaluator(evaluator, comm) report = evaluator() return report["main/accuracy"]
Example #27
Source File: chainer_integration.py From optuna with MIT License | 4 votes |
def objective(trial): model = L.Classifier(create_model(trial)) optimizer = chainer.optimizers.Adam() optimizer.setup(model) rng = np.random.RandomState(0) train, valid = chainer.datasets.get_mnist() train = chainer.datasets.SubDataset( train, 0, N_TRAIN_EXAMPLES, order=rng.permutation(len(train)) ) valid = chainer.datasets.SubDataset( valid, 0, N_VALID_EXAMPLES, order=rng.permutation(len(valid)) ) train_iter = chainer.iterators.SerialIterator(train, BATCHSIZE) valid_iter = chainer.iterators.SerialIterator(valid, BATCHSIZE, repeat=False, shuffle=False) # Setup trainer. updater = chainer.training.StandardUpdater(train_iter, optimizer) trainer = chainer.training.Trainer(updater, (EPOCH, "epoch")) # Add Chainer extension for pruners. trainer.extend( optuna.integration.ChainerPruningExtension( trial, "validation/main/accuracy", (PRUNER_INTERVAL, "epoch") ) ) trainer.extend(chainer.training.extensions.Evaluator(valid_iter, model)) trainer.extend( chainer.training.extensions.PrintReport( [ "epoch", "main/loss", "validation/main/loss", "main/accuracy", "validation/main/accuracy", ] ) ) log_report_extension = chainer.training.extensions.LogReport(log_name=None) trainer.extend(log_report_extension) # Run training. # Please set show_loop_exception_msg False to inhibit messages about TrialPruned exception. # ChainerPruningExtension raises TrialPruned exception to stop training, and # trainer shows some messages every time it receive TrialPruned. trainer.run(show_loop_exception_msg=False) # Save loss and accuracy to user attributes. log_last = log_report_extension.log[-1] for key, value in log_last.items(): trial.set_user_attr(key, value) return log_report_extension.log[-1]["validation/main/accuracy"]
Example #28
Source File: demo_nnpu_chainer.py From pywsl with MIT License | 4 votes |
def main(): gpu, out = -1, "result" stepsize = 0.001 batchsize, epoch = 10000, 10 beta, gamma = 0., 1. data_id, prior = 0, .5 n_p, n_n, n_u, n_t, n_vp, n_vn, n_vu = 100, 0, 10000, 100, 20, 20, 100 data_name, x_p, x_n, x_u, y_u, x_t, y_t, x_vp, x_vn, x_vu, y_vu \ = load_dataset(data_id, n_p, n_n, n_u, prior, n_t, n_vp=n_vp, n_vn=n_vn, n_vu=n_vu) x_p, x_n, x_u, x_t, x_vp, x_vn, x_vu = x_p.astype(np.float32), x_n.astype(np.float32), \ x_u.astype(np.float32), x_t.astype(np.float32), x_vp.astype(np.float32), \ x_vn.astype(np.float32), x_vu.astype(np.float32), XYtrain = TupleDataset(np.r_[x_p, x_u], np.r_[np.ones(100), np.zeros(10000)].astype(np.int32)) XYtest = TupleDataset(np.r_[x_vp, x_vu], np.r_[np.ones(20), np.zeros(100)].astype(np.int32)) train_iter = chainer.iterators.SerialIterator(XYtrain, batchsize) test_iter = chainer.iterators.SerialIterator(XYtest, batchsize, repeat=False, shuffle=False) loss_type = lambda x: F.sigmoid(-x) nnpu_risk = PU_Risk(prior, loss=loss_type, nnPU=True, gamma=gamma, beta=beta) pu_acc = PU_Accuracy(prior) model = L.Classifier(MLP(), lossfun=nnpu_risk, accfun=pu_acc) if gpu >= 0: chainer.backends.cuda.get_device_from_id(gpu).use() model.to_gpu(gpu) optimizer = chainer.optimizers.Adam(alpha=stepsize) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(0.005)) updater = chainer.training.StandardUpdater(train_iter, optimizer, device=gpu) trainer = chainer.training.Trainer(updater, (epoch, 'epoch'), out=out) trainer.extend(extensions.LogReport(trigger=(1, 'epoch'))) trainer.extend(extensions.Evaluator(test_iter, model, device=gpu)) trainer.extend(extensions.ProgressBar()) trainer.extend(extensions.PrintReport( ['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', 'elapsed_time'])) key = 'validation/main/accuracy' model_name = 'model' trainer.extend(extensions.snapshot_object(model, model_name), trigger=chainer.training.triggers.MaxValueTrigger(key)) if extensions.PlotReport.available(): trainer.extend( extensions.PlotReport(['main/loss', 'validation/main/loss'], 'epoch', file_name=f'loss_curve.png')) trainer.extend( extensions.PlotReport(['main/accuracy', 'validation/main/accuracy'], 'epoch', file_name=f'accuracy_curve.png')) trainer.run() yh = pred(model, x_t, batchsize, gpu) mr = prior*np.mean(yh[y_t == +1] <= 0) + (1-prior)*np.mean(yh[y_t == -1] >= 0) print("mr: {}".format(mr))
Example #29
Source File: mnist.py From cloudml-samples with Apache License 2.0 | 4 votes |
def main(): # Training settings args = get_args() # Set up a neural network to train model = L.Classifier(Net()) if args.gpu >= 0: # Make a specified GPU current chainer.backends.cuda.get_device_from_id(args.gpu).use() model.to_gpu() # Copy the model to the GPU # Setup an optimizer optimizer = chainer.optimizers.MomentumSGD(lr=args.lr, momentum=args.momentum) optimizer.setup(model) # Load the MNIST dataset train, test = chainer.datasets.get_mnist(ndim=3) train_iter = chainer.iterators.SerialIterator(train, args.batch_size) test_iter = chainer.iterators.SerialIterator(test, args.test_batch_size, repeat=False, shuffle=False) # Set up a trainer updater = training.updaters.StandardUpdater( train_iter, optimizer, device=args.gpu) trainer = training.Trainer(updater, (args.epochs, 'epoch')) # Evaluate the model with the test dataset for each epoch trainer.extend(extensions.Evaluator(test_iter, model, device=args.gpu)) # Write a log of evaluation statistics for each epoch trainer.extend(extensions.LogReport()) # Print selected entries of the log to stdout trainer.extend(extensions.PrintReport( ['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', 'elapsed_time'])) # Send selected entries of the log to CMLE HP tuning system trainer.extend( HpReport(hp_metric_val='validation/main/loss', hp_metric_tag='my_loss')) if args.resume: # Resume from a snapshot tmp_model_file = os.path.join('/tmp', MODEL_FILE_NAME) if not os.path.exists(tmp_model_file): subprocess.check_call([ 'gsutil', 'cp', os.path.join(args.model_dir, MODEL_FILE_NAME), tmp_model_file]) if os.path.exists(tmp_model_file): chainer.serializers.load_npz(tmp_model_file, trainer) trainer.run() if args.model_dir: tmp_model_file = os.path.join('/tmp', MODEL_FILE_NAME) serializers.save_npz(tmp_model_file, model) subprocess.check_call([ 'gsutil', 'cp', tmp_model_file, os.path.join(args.model_dir, MODEL_FILE_NAME)])