Browse Source

fix the examples to pass the doctest

tags/v1.2.0-rc1
jiangshuqiang jiangshuqiang 5 years ago
parent
commit
517e4697ab
3 changed files with 78 additions and 43 deletions
  1. +17
    -0
      mindspore/ops/operations/debug_ops.py
  2. +24
    -20
      mindspore/train/callback/_summary_collector.py
  3. +37
    -23
      mindspore/train/summary/summary_record.py

+ 17
- 0
mindspore/ops/operations/debug_ops.py View File

@@ -62,6 +62,10 @@ class ScalarSummary(PrimitiveWithInfer):
``Ascend`` ``GPU`` ``CPU``

Examples:
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class SummaryDemo(nn.Cell):
... def __init__(self,):
... super(SummaryDemo, self).__init__()
@@ -105,6 +109,11 @@ class ImageSummary(PrimitiveWithInfer):
``Ascend`` ``GPU`` ``CPU``

Examples:

>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class Net(nn.Cell):
... def __init__(self):
... super(Net, self).__init__()
@@ -147,6 +156,10 @@ class TensorSummary(PrimitiveWithInfer):
``Ascend`` ``GPU`` ``CPU``

Examples:
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class SummaryDemo(nn.Cell):
... def __init__(self,):
... super(SummaryDemo, self).__init__()
@@ -190,6 +203,10 @@ class HistogramSummary(PrimitiveWithInfer):
``Ascend`` ``GPU`` ``CPU``

Examples:
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class SummaryDemo(nn.Cell):
... def __init__(self,):
... super(SummaryDemo, self).__init__()


+ 24
- 20
mindspore/train/callback/_summary_collector.py View File

@@ -146,28 +146,32 @@ class SummaryCollector(Callback):
RuntimeError: If an error occurs during data collection.

Examples:
>>> # Simple usage:
>>> import mindspore.nn as nn
>>> from mindspore import context
>>> from mindspore.train.callback import SummaryCollector
>>> from mindspore.train import Model
>>> summary_collector = SummaryCollector(summary_dir='./summary_dir')
>>> dataset = get_dataset('/path/to/MNIST')
>>> network = LeNet5()
>>> model = Model(network)
>>> model.train(epoch=1, dataset=dataset, callbacks=summary_collector)
>>> from mindspore.nn.metrics import Accuracy
>>>
>>> # Do not collect metric and collect the first layer parameter, others are collected by default
>>> specified={'collect_metric': False, 'histogram_regular': '^conv1.*'}
>>> summary_collector = SummaryCollector(summary_dir='./summary_dir', collect_specified_data=specified)
>>> model.train(epoch=1, dataset=dataset, callbacks=summary_collector)
>>>
>>> # Only collect metric, custom lineage data and record data that collected by the summary operator,
>>> # others are not collected
>>> specified = {'collect_metric': True}
>>> summary_collector = SummaryCollector('./summary_dir',
>>> collect_specified_data=specified,
>>> keep_default_action=False,
>>> custom_lineage_data={'version': 'resnet50_v1'}
>>> )
>>> model.train(epoch=1, dataset=dataset, callbacks=summary_collector)
>>> if __name__ == '__main__':
... # If the device_target is GPU, set the device_target to "GPU"
... context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
... mnist_dataset_dir = '/path/to/mnist_dataset_directory'
... # The detail of create_dataset method shown in model_zoo.official.cv.lenet.src.dataset.py
... ds_train = create_dataset(mnist_dataset_dir, 32)
... # The detail of LeNet5 shown in model_zoo.official.cv.lenet.src.lenet.py
... network = LeNet5(10)
... net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
... net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
... model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}, amp_level="O2")
...
... # Simple usage:
... summary_collector = SummaryCollector(summary_dir='./summary_dir')
... model.train(1, ds_train, callbacks=[summary_collector], dataset_sink_mode=False)
...
... # Do not collect metric and collect the first layer parameter, others are collected by default
... specified={'collect_metric': False, 'histogram_regular': '^conv1.*'}
... summary_collector = SummaryCollector(summary_dir='./summary_dir', collect_specified_data=specified)
... model.train(1, ds_train, callbacks=[summary_collector], dataset_sink_mode=False)
"""

_DEFAULT_SPECIFIED_DATA = {


+ 37
- 23
mindspore/train/summary/summary_record.py View File

@@ -129,16 +129,17 @@ class SummaryRecord:
TypeError: If the parameter type is incorrect.

Examples:
>>> # use in with statement to auto close
>>> from mindspore.train.summary import SummaryRecord
>>> with SummaryRecord(log_dir="./summary_dir") as summary_record:
... pass
>>>
>>> # use in try .. finally .. to ensure closing
>>> try:
... summary_record = SummaryRecord(log_dir="./summary_dir")
... finally:
... summary_record.close()
>>> if __name__ == '__main__':
... # use in with statement to auto close
... with SummaryRecord(log_dir="./summary_dir") as summary_record:
... pass
...
... # use in try .. finally .. to ensure closing
... try:
... summary_record = SummaryRecord(log_dir="./summary_dir")
... finally:
... summary_record.close()
"""

def __init__(self, log_dir, file_prefix="events", file_suffix="_MS",
@@ -212,8 +213,10 @@ class SummaryRecord:
ValueError: When the mode is not recognized.

Examples:
>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
... summary_record.set_mode('eval')
>>> from mindspore.train.summary import SummaryRecord
>>> if __name__ == '__main__':
... with SummaryRecord(log_dir="./summary_dir", file_prefix="xx_", file_suffix="_yy") as summary_record:
... summary_record.set_mode('eval')
"""
mode_spec = 'train', 'eval'
if mode not in mode_spec:
@@ -249,8 +252,11 @@ class SummaryRecord:
TypeError: If the parameter type is error.

Examples:
>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
... summary_record.add_value('scalar', 'loss', Tensor(0.1))
>>> from mindspore import Tensor
>>> from mindspore.train.summary import SummaryRecord
>>> if __name__ == '__main__':
... with SummaryRecord(log_dir="./summary_dir", file_prefix="xx_", file_suffix="_yy") as summary_record:
... summary_record.add_value('scalar', 'loss', Tensor(0.1))
"""
if plugin in ('tensor', 'scalar', 'image', 'histogram'):
if not name or not isinstance(name, str):
@@ -297,8 +303,10 @@ class SummaryRecord:
RuntimeError: If the disk space is insufficient.

Examples:
>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
... summary_record.record(step=2)
>>> from mindspore.train.summary import SummaryRecord
>>> if __name__ == '__main__':
... with SummaryRecord(log_dir="./summary_dir", file_prefix="xx_", file_suffix="_yy") as summary_record:
... summary_record.record(step=2)
...
True
"""
@@ -364,8 +372,10 @@ class SummaryRecord:
str, the full path of log file.

Examples:
>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
... log_dir = summary_record.log_dir
>>> from mindspore.train.summary import SummaryRecord
>>> if __name__ == '__main__':
... with SummaryRecord(log_dir="./summary_dir", file_prefix="xx_", file_suffix="_yy") as summary_record:
... log_dir = summary_record.log_dir
"""
return self.full_file_name

@@ -376,8 +386,10 @@ class SummaryRecord:
Call it to make sure that all pending events have been written to disk.

Examples:
>>> with SummaryRecord(log_dir="./summary_dir", file_prefix="xxx_", file_suffix="_yyy") as summary_record:
... summary_record.flush()
>>> from mindspore.train.summary import SummaryRecord
>>> if __name__ == '__main__':
... with SummaryRecord(log_dir="./summary_dir", file_prefix="xx_", file_suffix="_yy") as summary_record:
... summary_record.flush()
"""
if self._closed:
logger.error("The record writer is closed and can not flush.")
@@ -389,10 +401,12 @@ class SummaryRecord:
Flush all events and close summary records. Please use the statement to autoclose.

Examples:
>>> try:
... summary_record = SummaryRecord(log_dir="./summary_dir")
... finally:
... summary_record.close()
>>> from mindspore.train.summary import SummaryRecord
>>> if __name__ == '__main__':
... try:
... summary_record = SummaryRecord(log_dir="./summary_dir")
... finally:
... summary_record.close()
"""
if not self._closed and self._event_writer:
# event writer flush and close


Loading…
Cancel
Save