!29013 modify cn api comment

Merge pull request !29013 from changzherui/code_docs_comment2
This commit is contained in:
i-robot 2022-01-14 06:11:33 +00:00 committed by Gitee
commit d20985284b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 102 additions and 11 deletions

View File

@ -28,8 +28,8 @@
>>> from mindspore import Model, nn
>>> from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
>>>
>>> class LeNet5(nn.Cell)
... def __init__(self, num_class=10, num_channel=1)
>>> class LeNet5(nn.Cell):
... def __init__(self):
... super(LeNet5, self).__init__()
... self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
... self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
@ -40,7 +40,7 @@
... self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
... self.flatten = nn.Flatten()
...
... def construct(self, x)
... def construct(self, x):
... x = self.max_pool2d(self.relu(self.conv1(x)))
... x = self.max_pool2d(self.relu(self.conv2(x)))
... x = self.flatten(x)

View File

@ -15,6 +15,20 @@
- **ValueError** - 当 `per_print_times` 不是整数或小于零。
**样例:**
>>> from mindspore import Model, nn
>>>
>>> net = LeNet5()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> optim = nn.Momentum(net.trainable_params(), 0.01, 0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> time_monitor = TimeMonitor()
>>> model.train(10, dataset, callbacks=time_monitor)
.. py:method:: step_end(run_context)
step结束时打印训练loss。

View File

@ -2,7 +2,7 @@
checkpoint的回调函数。
在训练过程中调用该方法可以保存训练后的网络参数。
在训练过程中调用该方法可以保存网络参数。
.. note::
在分布式训练场景下请为每个训练进程指定不同的目录来保存checkpoint文件。否则可能会训练失败。
@ -15,9 +15,47 @@
**异常:**
- **ValueError** - 如果前缀无效。
- **ValueError** - 如果prefix参数不是str类型或包含'/'字符。
- **ValueError** - 如果directory参数不是str类型。
- **TypeError** - config不是CheckpointConfig类型。
**样例:**
>>> from mindspore import Model, nn
>>> from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
>>>
>>> class LeNet5(nn.Cell):
... def __init__(self):
... super(LeNet5, self).__init__()
... self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
... self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
... self.fc1 = nn.Dense(16 * 5 * 5, 120, weight_init=Normal(0.02))
... self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))
... self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))
... self.relu = nn.ReLU()
... self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
... self.flatten = nn.Flatten()
...
... def construct(self, x)
... x = self.max_pool2d(self.relu(self.conv1(x)))
... x = self.max_pool2d(self.relu(self.conv2(x)))
... x = self.flatten(x)
... x = self.relu(self.fc1(x))
... x = self.relu(self.fc2(x))
... x = self.fc3(x)
... return x
>>>
>>> net = LeNet5()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> optim = nn.Momentum(net.trainable_params(), 0.01, 0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> config = CheckpointConfig(saved_network=net)
>>> ckpoint_cb = ModelCheckpoint(prefix='LeNet5', directory='./checkpoint', config=config)
>>> model.train(10, dataset, callbacks=ckpoint_cb)
.. py:method:: end(run_context)
在训练结束后会保存最后一个step的checkpoint。

View File

@ -10,13 +10,27 @@
- **ValueError** - `data_size` 不是正整数。
**样例:**
>>> from mindspore import Model, nn
>>>
>>> net = LeNet5()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> optim = nn.Momentum(net.trainable_params(), 0.01, 0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> time_monitor = TimeMonitor()
>>> model.train(10, dataset, callbacks=time_monitor)
.. py:method:: epoch_begin(run_context)
在epoch开始时记录时间。
**参数:**
- **run_context** (RunContext) - 包含模型的一些基本信息。
- **run_context** (RunContext) - 包含模型的相关信息。
.. py:method:: epoch_end(run_context)
@ -24,4 +38,4 @@
**参数:**
- **run_context** (RunContext) - 包含模型的一些基本信息。
- **run_context** (RunContext) - 包含模型的相关信息。

View File

@ -355,7 +355,8 @@ class ModelCheckpoint(Callback):
config (CheckpointConfig): Checkpoint strategy configuration. Default: None.
Raises:
ValueError: If the prefix is invalid.
ValueError: If `prefix` is not str or contains the '/' character.
ValueError: If `directory` is not str.
TypeError: If the config is not CheckpointConfig type.
"""

View File

@ -237,7 +237,7 @@ class FederatedLearningManager(Callback):
adaptively adjusted here.
Args:
run_context (RunContext): Context of the train running.
run_context (RunContext): Include some information of the model.
"""
self._global_step += 1
cb_params = run_context.original_args()

View File

@ -35,6 +35,18 @@ class LossMonitor(Callback):
Raises:
ValueError: If per_print_times is not an integer or less than zero.
Examples:
>>> from mindspore import Model, nn
>>>
>>> net = LeNet5()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> optim = nn.Momentum(net.trainable_params(), 0.01, 0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> loss_monitor = LossMonitor()
>>> model.train(10, dataset, callbacks=loss_monitor)
"""
def __init__(self, per_print_times=1):
@ -50,7 +62,7 @@ class LossMonitor(Callback):
Print training loss at the end of step.
Args:
run_context (RunContext): Context of the train running.
run_context (RunContext): Include some information of the model.
"""
cb_params = run_context.original_args()
loss = cb_params.net_outputs

View File

@ -62,7 +62,7 @@ class LearningRateScheduler(Callback):
Change the learning_rate at the end of step.
Args:
run_context (RunContext): Context of the train running.
run_context (RunContext): Include some information of the model.
"""
cb_params = run_context.original_args()
arr_lr = cb_params.optimizer.learning_rate.asnumpy()

View File

@ -30,6 +30,18 @@ class TimeMonitor(Callback):
Raises:
ValueError: If data_size is not positive int.
Examples:
>>> from mindspore import Model, nn
>>>
>>> net = LeNet5()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> optim = nn.Momentum(net.trainable_params(), 0.01, 0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> time_monitor = TimeMonitor()
>>> model.train(10, dataset, callbacks=time_monitor)
"""
def __init__(self, data_size=None):