本文整理汇总了Python中torch.utils.tensorboard.SummaryWriter方法的典型用法代码示例。如果您正苦于以下问题:Python tensorboard.SummaryWriter方法的具体用法?Python tensorboard.SummaryWriter怎么用?Python tensorboard.SummaryWriter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.utils.tensorboard
的用法示例。
在下文中一共展示了tensorboard.SummaryWriter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def __init__(self, log_dir, use_tb=True, config='rl'):
self._log_dir = log_dir
if use_tb:
tb_dir = os.path.join(log_dir, 'tb')
if os.path.exists(tb_dir):
shutil.rmtree(tb_dir)
self._sw = SummaryWriter(tb_dir)
else:
self._sw = None
self._train_mg = MetersGroup(
os.path.join(log_dir, 'train.log'),
formating=FORMAT_CONFIG[config]['train']
)
self._eval_mg = MetersGroup(
os.path.join(log_dir, 'eval.log'),
formating=FORMAT_CONFIG[config]['eval']
)
示例2: __init__
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def __init__(self, log_dir):
try:
from torch.utils.tensorboard import SummaryWriter
except ImportError:
try:
from tensorboardX import SummaryWriter
except ImportError:
raise RuntimeError("This contrib module requires tensorboardX to be installed. "
"Please install it with command: \n pip install tensorboardX")
try:
self.writer = SummaryWriter(log_dir)
except TypeError as err:
if "type object got multiple values for keyword argument 'logdir'" == str(err):
self.writer = SummaryWriter(log_dir=log_dir)
warnings.warn('tensorboardX version < 1.7 will not be supported '
'after ignite 0.3.0; please upgrade',
DeprecationWarning)
else:
raise err
示例3: add_tensorboard_handler
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def add_tensorboard_handler(tensorboard_folder, engine, every_iteration=False):
"""
Every key in engine.state.epoch_history[-1] is logged to TensorBoard.
Args:
tensorboard_folder (str): Where the tensorboard logs should go.
trainer (ignite.Engine): The engine to log.
every_iteration (bool, optional): Whether to also log the values at every
iteration.
"""
@engine.on(ValidationEvents.VALIDATION_COMPLETED)
def log_to_tensorboard(engine):
writer = SummaryWriter(tensorboard_folder)
for key in engine.state.epoch_history:
writer.add_scalar(
key, engine.state.epoch_history[key][-1], engine.state.epoch)
if every_iteration:
@engine.on(Events.ITERATION_COMPLETED)
def log_iteration_to_tensorboard(engine):
writer = SummaryWriter(tensorboard_folder)
for key in engine.state.iter_history:
writer.add_scalar(
key, engine.state.iter_history[key][-1], engine.state.iteration)
示例4: before_run
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def before_run(self, runner):
if TORCH_VERSION < '1.1' or TORCH_VERSION == 'parrots':
try:
from tensorboardX import SummaryWriter
except ImportError:
raise ImportError('Please install tensorboardX to use '
'TensorboardLoggerHook.')
else:
try:
from torch.utils.tensorboard import SummaryWriter
except ImportError:
raise ImportError(
'Please run "pip install future tensorboard" to install '
'the dependencies to use torch.utils.tensorboard '
'(applicable to PyTorch 1.1 or higher)')
if self.log_dir is None:
self.log_dir = osp.join(runner.work_dir, 'tf_logs')
self.writer = SummaryWriter(self.log_dir)
示例5: graph
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def graph(self, model: Union[torch.nn.Module, torch.nn.DataParallel, Model], name: str, use_gpu: bool,
*input_shape):
if isinstance(model, torch.nn.Module):
proto_model: torch.nn.Module = model
num_params: int = self._count_params(proto_model)
elif isinstance(model, torch.nn.DataParallel):
proto_model: torch.nn.Module = model.module
num_params: int = self._count_params(proto_model)
elif isinstance(model, Model):
proto_model: torch.nn.Module = model.model
num_params: int = model.num_params
else:
raise TypeError("Only `nn.Module`, `nn.DataParallel` and `Model` can be passed!")
model_logdir = os.path.join(self.logdir, name)
self._build_dir(model_logdir)
writer_for_model = SummaryWriter(log_dir=model_logdir)
input_list = tuple(torch.ones(shape).cuda() if use_gpu else torch.ones(shape) for shape in input_shape)
self.scalars({'ParamsNum': num_params}, 0, tag="ParamsNum")
self.scalars({'ParamsNum': num_params}, 1, tag="ParamsNum")
proto_model(*input_list)
writer_for_model.add_graph(proto_model, input_list)
writer_for_model.close()
示例6: __init__
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.use_tf = True
# Base dir is often the dir created to save the model into
base_dir = kwargs.get('base_dir', '.')
log_dir = os.path.expanduser(kwargs.get('log_dir', 'runs'))
if not os.path.isabs(log_dir):
log_dir = os.path.join(base_dir, log_dir)
# Run dir is the name of an individual run
run_dir = kwargs.get('run_dir')
pid = str(os.getpid())
run_dir = '{}-{}'.format(run_dir, pid) if run_dir is not None else pid
log_dir = os.path.join(log_dir, run_dir)
try:
from torch.utils.tensorboard import SummaryWriter
self._log = SummaryWriter(log_dir)
self.use_tf = False
except:
import tensorflow as tf
file_writer = tf.summary.create_file_writer(log_dir)
file_writer.set_as_default()
self._log_scalar = tf.summary.scalar
示例7: test_tensorboard_add_scalar
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def test_tensorboard_add_scalar(tmp_path: Path):
reporter = Reporter()
reporter.set_epoch(1)
key1 = uuid.uuid4().hex
with reporter.observe(key1) as sub:
stats1 = {"aa": 0.6}
sub.register(stats1)
reporter.set_epoch(1)
with reporter.observe(key1) as sub:
# Skip epoch=2
sub.register({})
reporter.set_epoch(3)
with reporter.observe(key1) as sub:
stats1 = {"aa": 0.6}
sub.register(stats1)
if LooseVersion(torch.__version__) >= LooseVersion("1.1.0"):
from torch.utils.tensorboard import SummaryWriter
else:
from tensorboardX import SummaryWriter
writer = SummaryWriter(tmp_path)
reporter.tensorboard_add_scalar(writer)
示例8: __init__
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def __init__(self, writer_kwargs=None,
abort_event: Event = None, queue: Queue = None):
"""
Parameters
----------
writer_kwargs : dict
arguments to initialize a writer
abort_event : :class:`threading.Event`
the abortion event
queue : :class:`queue.Queue`
the queue holding all logging tasks
"""
if writer_kwargs is None:
writer_kwargs = {}
if "logdir" in writer_kwargs:
writer_kwargs[LOGDIR_KWARG] = writer_kwargs.pop("logdir")
elif "log_dir" in writer_kwargs:
writer_kwargs[LOGDIR_KWARG] = writer_kwargs.pop("log_dir")
super().__init__(SummaryWriter, writer_kwargs,
abort_event, queue)
示例9: experiment
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def experiment(self) -> SummaryWriter:
r"""
Actual tensorboard object. To use TensorBoard features in your
:class:`~pytorch_lightning.core.lightning.LightningModule` do the following.
Example::
self.logger.experiment.some_tensorboard_function()
"""
if self._experiment is not None:
return self._experiment
assert rank_zero_only.rank == 0, 'tried to init log dirs in non global_rank=0'
os.makedirs(self.root_dir, exist_ok=True)
self._experiment = SummaryWriter(log_dir=self.log_dir, **self._kwargs)
return self._experiment
示例10: test_writing_stack
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def test_writing_stack(self):
with TemporaryDirectory() as tmp_dir1, TemporaryDirectory() as tmp_dir2:
writer1 = SummaryWriter(tmp_dir1)
writer1.add_scalar = MagicMock()
writer2 = SummaryWriter(tmp_dir2)
writer2.add_scalar = MagicMock()
with summary_writer_context(writer1):
with summary_writer_context(writer2):
SummaryWriterContext.add_scalar("test2", torch.ones(1))
SummaryWriterContext.add_scalar("test1", torch.zeros(1))
writer1.add_scalar.assert_called_once_with(
"test1", torch.zeros(1), global_step=0
)
writer2.add_scalar.assert_called_once_with(
"test2", torch.ones(1), global_step=0
)
示例11: write_to_tensorboard
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def write_to_tensorboard(
self, writer: SummaryWriter, prefix="train", global_step=None
):
for meter in self.meters:
avg = meter.avg
val = meter.val
if meter.write_val:
writer.add_scalar(
f"{prefix}/{meter.name}_val", val, global_step=global_step
)
if meter.write_avg:
writer.add_scalar(
f"{prefix}/{meter.name}_avg", avg, global_step=global_step
)
示例12: __init__
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def __init__(self) -> None:
"""Initialize TensorBoardWriter."""
super().__init__()
# Set up tensorboard summary writer
self.writer = SummaryWriter(Meta.log_path)
示例13: plot_model
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def plot_model(
model: ClassyModel,
size: Tuple[int, ...] = (3, 224, 224),
input_key: Optional[Union[str, List[str]]] = None,
writer: Optional["SummaryWriter"] = None,
folder: str = "",
train: bool = True,
) -> None:
"""Visualizes a model in TensorBoard.
The TensorBoard writer can be either specified directly via `writer` or can
be specified via a `folder`.
The model can be run in training or evaluation model via the `train` argument.
Example usage on devserver:
- Install TensorBoard using: `sudo feature install tensorboard`
- Start TensorBoard using: `tensorboard --port=8098 --logdir <folder>`
"""
assert (
writer is not None or folder != ""
), "must specify SummaryWriter or folder to create SummaryWriter in"
input = get_model_dummy_input(model, size, input_key)
if writer is None:
writer = SummaryWriter(log_dir=folder, comment="Model graph")
with writer:
orig_train = model.training
model.train(train) # visualize model in desired mode
writer.add_graph(model, input_to_model=(input,))
model.train(orig_train)
# function that produces an image map:
示例14: test_constructors
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def test_constructors(self) -> None:
"""
Test that the hooks are constructed correctly.
"""
config = {"summary_writer": {}, "log_period": 5}
invalid_config = copy.deepcopy(config)
invalid_config["log_period"] = "this is not an int"
self.constructor_test_helper(
config=config,
hook_type=TensorboardPlotHook,
hook_registry_name="tensorboard_plot",
hook_kwargs={"tb_writer": SummaryWriter(), "log_period": 5},
invalid_configs=[invalid_config],
)
示例15: test_constructors
# 需要导入模块: from torch.utils import tensorboard [as 别名]
# 或者: from torch.utils.tensorboard import SummaryWriter [as 别名]
def test_constructors(self) -> None:
"""
Test that the hooks are constructed correctly.
"""
config = {"summary_writer": {}}
self.constructor_test_helper(
config=config,
hook_type=ModelTensorboardHook,
hook_registry_name="model_tensorboard",
hook_kwargs={"tb_writer": SummaryWriter()},
)