本文整理汇总了Python中chainer.training.extensions.LogReport方法的典型用法代码示例。如果您正苦于以下问题:Python extensions.LogReport方法的具体用法?Python extensions.LogReport怎么用?Python extensions.LogReport使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.training.extensions
的用法示例。
在下文中一共展示了extensions.LogReport方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def __call__(self, trainer):
log_report = self._log_report
if isinstance(log_report, str):
log_report = trainer.get_extension(log_report)
elif isinstance(log_report, log_report_module.LogReport):
log_report(trainer) # update the log report
else:
raise TypeError('log report has a wrong type %s' %
type(log_report))
log = log_report.log
log_len = self._log_len
hpt = hypertune.HyperTune()
while len(log) > log_len:
target_log = log[log_len]
hpt.report_hyperparameter_tuning_metric(
hyperparameter_metric_tag=self._hp_metric_tag,
metric_value=target_log[self._hp_metric_val],
global_step=target_log[self._hp_global_step])
log_len += 1
self.log_len = log_len
示例2: _setup
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def _setup(self, stream=None, delete_flush=False):
self.logreport = mock.MagicMock(spec=extensions.LogReport(
['epoch'], trigger=(1, 'iteration'), log_name=None))
if stream is None:
self.stream = mock.MagicMock()
if delete_flush:
del self.stream.flush
else:
self.stream = stream
self.report = extensions.PrintReport(
['epoch'], log_report=self.logreport, out=self.stream)
self.trainer = testing.get_trainer_with_mock_updater(
stop_trigger=(1, 'iteration'))
self.trainer.extend(self.logreport)
self.trainer.extend(self.report)
self.logreport.log = [{'epoch': 0}]
示例3: main
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def main(model):
train = read_data(fold=BUZZER_TRAIN_FOLD)
valid = read_data(fold=BUZZER_DEV_FOLD)
print('# train data: {}'.format(len(train)))
print('# valid data: {}'.format(len(valid)))
train_iter = chainer.iterators.SerialIterator(train, 64)
valid_iter = chainer.iterators.SerialIterator(valid, 64, repeat=False, shuffle=False)
optimizer = chainer.optimizers.Adam()
optimizer.setup(model)
optimizer.add_hook(chainer.optimizer.WeightDecay(1e-4))
updater = training.updaters.StandardUpdater(train_iter, optimizer, converter=convert_seq, device=0)
trainer = training.Trainer(updater, (20, 'epoch'), out=model.model_dir)
trainer.extend(extensions.Evaluator(valid_iter, model, converter=convert_seq, device=0))
record_trigger = training.triggers.MaxValueTrigger('validation/main/accuracy', (1, 'epoch'))
trainer.extend(extensions.snapshot_object(model, 'buzzer.npz'), trigger=record_trigger)
trainer.extend(extensions.LogReport())
trainer.extend(extensions.ProgressBar())
trainer.extend(extensions.PrintReport([
'epoch', 'main/loss', 'validation/main/loss',
'main/accuracy', 'validation/main/accuracy', 'elapsed_time'
]))
if not os.path.isdir(model.model_dir):
os.mkdir(model.model_dir)
trainer.run()
示例4: __init__
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def __init__(self,
log_report='LogReport',
hp_global_step='epoch',
hp_metric_val='validation/main/loss',
hp_metric_tag='loss'):
self._log_report = log_report
self._log_len = 0 # number of observations already done
self._hp_global_step = hp_global_step
self._hp_metric_val = hp_metric_val
self._hp_metric_tag = hp_metric_tag
示例5: update_core
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def update_core(self):
loss = 0
# When we pass one iterator and optimizer to StandardUpdater.__init__,
# they are automatically named 'main'.
train_iter = self.get_iterator('main')
optimizer = self.get_optimizer('main')
# Progress the dataset iterator for bprop_len words at each iteration.
for i in range(self.bprop_len):
# Get the next batch (a list of tuples of two word IDs)
batch = train_iter.__next__()
# Concatenate the word IDs to matrices and send them to the device
# self.converter does this job
# (it is chainer.dataset.concat_examples by default)
x, t = self.converter(batch, self.device)
# Compute the loss at this time step and accumulate it
# loss += optimizer.target(chainer.Variable(x), chainer.Variable(t))
loss += optimizer.target(x, t)
optimizer.target.cleargrads() # Clear the parameter gradients
loss.backward() # Backprop
loss.unchain_backward() # Truncate the graph
optimizer.update() # Update the parameters
# Routine to rewrite the result dictionary of LogReport to add perplexity
# values
示例6: update_core
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def update_core(self):
loss = 0
# When we pass one iterator and optimizer to StandardUpdater.__init__,
# they are automatically named 'main'.
train_iter = self.get_iterator('main')
optimizer = self.get_optimizer('main')
# Progress the dataset iterator for bprop_len words at each iteration.
for i in range(self.bprop_len):
# Get the next batch (a list of tuples of two word IDs)
batch = train_iter.__next__()
# Concatenate the word IDs to matrices and send them to the device
# self.converter does this job
# (it is chainer.dataset.concat_examples by default)
x, t = self.converter(batch, self.device)
# Compute the loss at this time step and accumulate it
loss += optimizer.target(x, t)
optimizer.target.cleargrads() # Clear the parameter gradients
loss.backward() # Backprop
loss.unchain_backward() # Truncate the graph
optimizer.update() # Update the parameters
# Routine to rewrite the result dictionary of LogReport to add perplexity
# values
示例7: compute_perplexity
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def compute_perplexity(result):
"""Compute and add the perplexity to the LogReport.
:param dict result: The current observations
"""
# Routine to rewrite the result dictionary of LogReport to add perplexity values
result["perplexity"] = np.exp(result["main/nll"] / result["main/count"])
if "validation/main/nll" in result:
result["val_perplexity"] = np.exp(
result["validation/main/nll"] / result["validation/main/count"]
)
示例8: train
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def train(args):
nz = args.nz
batch_size = args.batch_size
epochs = args.epochs
gpu = args.gpu
# CIFAR-10 images in range [-1, 1] (tanh generator outputs)
train, _ = datasets.get_cifar10(withlabel=False, ndim=3, scale=2)
train -= 1.0
train_iter = iterators.SerialIterator(train, batch_size)
z_iter = RandomNoiseIterator(GaussianNoiseGenerator(0, 1, args.nz),
batch_size)
optimizer_generator = optimizers.RMSprop(lr=0.00005)
optimizer_critic = optimizers.RMSprop(lr=0.00005)
optimizer_generator.setup(Generator())
optimizer_critic.setup(Critic())
updater = WassersteinGANUpdater(
iterator=train_iter,
noise_iterator=z_iter,
optimizer_generator=optimizer_generator,
optimizer_critic=optimizer_critic,
device=gpu)
trainer = training.Trainer(updater, stop_trigger=(epochs, 'epoch'))
trainer.extend(extensions.ProgressBar())
trainer.extend(extensions.LogReport(trigger=(1, 'iteration')))
trainer.extend(GeneratorSample(), trigger=(1, 'epoch'))
trainer.extend(extensions.PrintReport(['epoch', 'iteration', 'critic/loss',
'critic/loss/real', 'critic/loss/fake', 'generator/loss']))
trainer.run()
示例9: check_train
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def check_train(self, gpu):
outdir = tempfile.mkdtemp()
print("outdir: {}".format(outdir))
n_classes = 2
batch_size = 32
devices = {'main': gpu}
A = np.array([
[0, 1, 1, 0],
[1, 0, 0, 1],
[1, 0, 0, 0],
[0, 1, 0, 0],
]).astype(np.float32)
model = graph_cnn.GraphCNN(A, n_out=n_classes)
optimizer = optimizers.Adam(alpha=1e-4)
optimizer.setup(model)
train_dataset = EasyDataset(train=True, n_classes=n_classes)
train_iter = chainer.iterators.MultiprocessIterator(
train_dataset, batch_size)
updater = ParallelUpdater(train_iter, optimizer, devices=devices)
trainer = chainer.training.Trainer(updater, (10, 'epoch'), out=outdir)
trainer.extend(extensions.LogReport(trigger=(1, 'epoch')))
trainer.extend(extensions.PrintReport(
['epoch', 'iteration', 'main/loss', 'main/accuracy']))
trainer.extend(extensions.ProgressBar())
trainer.run()
示例10: _train_trainer
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def _train_trainer(self, examples):
"""Training with chainer trainer module"""
train_iter = SerialIterator(examples, args.batch_size)
optimizer = optimizers.Adam(alpha=args.lr)
optimizer.setup(self.nnet)
def loss_func(boards, target_pis, target_vs):
out_pi, out_v = self.nnet(boards)
l_pi = self.loss_pi(target_pis, out_pi)
l_v = self.loss_v(target_vs, out_v)
total_loss = l_pi + l_v
chainer.reporter.report({
'loss': total_loss,
'loss_pi': l_pi,
'loss_v': l_v,
}, observer=self.nnet)
return total_loss
updater = training.StandardUpdater(
train_iter, optimizer, device=args.device, loss_func=loss_func, converter=converter)
# Set up the trainer.
trainer = training.Trainer(updater, (args.epochs, 'epoch'), out=args.out)
# trainer.extend(extensions.snapshot(), trigger=(args.epochs, 'epoch'))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport([
'epoch', 'main/loss', 'main/loss_pi', 'main/loss_v', 'elapsed_time']))
trainer.extend(extensions.ProgressBar(update_interval=10))
trainer.run()
示例11: main
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
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)])
示例12: run_training
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
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()
示例13: prepare_trainer
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def prepare_trainer(net,
optimizer_name,
lr,
momentum,
num_epochs,
train_data,
val_data,
logging_dir_path,
use_gpus):
if optimizer_name == "sgd":
optimizer = chainer.optimizers.MomentumSGD(lr=lr, momentum=momentum)
elif optimizer_name == "nag":
optimizer = chainer.optimizers.NesterovAG(lr=lr, momentum=momentum)
else:
raise Exception("Unsupported optimizer: {}".format(optimizer_name))
optimizer.setup(net)
# devices = tuple(range(num_gpus)) if num_gpus > 0 else (-1, )
devices = (0,) if use_gpus else (-1,)
updater = training.updaters.StandardUpdater(
iterator=train_data["iterator"],
optimizer=optimizer,
device=devices[0])
trainer = training.Trainer(
updater=updater,
stop_trigger=(num_epochs, "epoch"),
out=logging_dir_path)
val_interval = 100000, "iteration"
log_interval = 1000, "iteration"
trainer.extend(
extension=extensions.Evaluator(
iterator=val_data["iterator"],
target=net,
device=devices[0]),
trigger=val_interval)
trainer.extend(extensions.dump_graph("main/loss"))
trainer.extend(extensions.snapshot(), trigger=val_interval)
trainer.extend(
extensions.snapshot_object(
net,
"model_iter_{.updater.iteration}"),
trigger=val_interval)
trainer.extend(extensions.LogReport(trigger=log_interval))
trainer.extend(extensions.observe_lr(), trigger=log_interval)
trainer.extend(
extensions.PrintReport([
"epoch", "iteration", "main/loss", "validation/main/loss", "main/accuracy", "validation/main/accuracy",
"lr"]),
trigger=log_interval)
trainer.extend(extensions.ProgressBar(update_interval=10))
return trainer
示例14: prepare_trainer
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def prepare_trainer(net,
optimizer_name,
lr,
momentum,
num_epochs,
train_iter,
val_iter,
logging_dir_path,
num_gpus=0):
if optimizer_name == "sgd":
optimizer = chainer.optimizers.MomentumSGD(lr=lr, momentum=momentum)
elif optimizer_name == "nag":
optimizer = chainer.optimizers.NesterovAG(lr=lr, momentum=momentum)
else:
raise Exception('Unsupported optimizer: {}'.format(optimizer_name))
optimizer.setup(net)
# devices = tuple(range(num_gpus)) if num_gpus > 0 else (-1, )
devices = (0,) if num_gpus > 0 else (-1,)
updater = training.updaters.StandardUpdater(
iterator=train_iter,
optimizer=optimizer,
device=devices[0])
trainer = training.Trainer(
updater=updater,
stop_trigger=(num_epochs, 'epoch'),
out=logging_dir_path)
val_interval = 100000, 'iteration'
log_interval = 1000, 'iteration'
trainer.extend(
extension=extensions.Evaluator(
val_iter,
net,
device=devices[0]),
trigger=val_interval)
trainer.extend(extensions.dump_graph('main/loss'))
trainer.extend(extensions.snapshot(), trigger=val_interval)
trainer.extend(
extensions.snapshot_object(
net,
'model_iter_{.updater.iteration}'),
trigger=val_interval)
trainer.extend(extensions.LogReport(trigger=log_interval))
trainer.extend(extensions.observe_lr(), trigger=log_interval)
trainer.extend(
extensions.PrintReport([
'epoch', 'iteration', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy',
'lr']),
trigger=log_interval)
trainer.extend(extensions.ProgressBar(update_interval=10))
return trainer
示例15: train_main
# 需要导入模块: from chainer.training import extensions [as 别名]
# 或者: from chainer.training.extensions import LogReport [as 别名]
def train_main(args):
"""
trains model specfied in args.
main method for train subcommand.
"""
# load text
with open(args.text_path) as f:
text = f.read()
logger.info("corpus length: %s.", len(text))
# data iterator
data_iter = DataIterator(text, args.batch_size, args.seq_len)
# load or build model
if args.restore:
logger.info("restoring model.")
load_path = args.checkpoint_path if args.restore is True else args.restore
model = load_model(load_path)
else:
net = Network(vocab_size=VOCAB_SIZE,
embedding_size=args.embedding_size,
rnn_size=args.rnn_size,
num_layers=args.num_layers,
drop_rate=args.drop_rate)
model = L.Classifier(net)
# make checkpoint directory
log_dir = make_dirs(args.checkpoint_path)
with open("{}.json".format(args.checkpoint_path), "w") as f:
json.dump(model.predictor.args, f, indent=2)
chainer.serializers.save_npz(args.checkpoint_path, model)
logger.info("model saved: %s.", args.checkpoint_path)
# optimizer
optimizer = chainer.optimizers.Adam(alpha=args.learning_rate)
optimizer.setup(model)
# clip gradient norm
optimizer.add_hook(chainer.optimizer.GradientClipping(args.clip_norm))
# trainer
updater = BpttUpdater(data_iter, optimizer)
trainer = chainer.training.Trainer(updater, (args.num_epochs, 'epoch'), out=log_dir)
trainer.extend(extensions.snapshot_object(model, filename=os.path.basename(args.checkpoint_path)))
trainer.extend(extensions.ProgressBar(update_interval=1))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PlotReport(y_keys=["main/loss"]))
trainer.extend(LoggerExtension(text))
# training start
model.predictor.reset_state()
logger.info("start of training.")
time_train = time.time()
trainer.run()
# training end
duration_train = time.time() - time_train
logger.info("end of training, duration: %ds.", duration_train)
# generate text
seed = generate_seed(text)
generate_text(model, seed, 1024, 3)
return model