!7589 add api example

Merge pull request !7589 from caozhou/add_api_example
This commit is contained in:
mindspore-ci-bot 2020-10-22 10:20:56 +08:00 committed by Gitee
commit 84f70c8cf0
2 changed files with 24 additions and 15 deletions

View File

@ -96,30 +96,35 @@ class CheckpointConfig:
ValueError: If the input_param is None or 0.
Examples:
>>> class Net(nn.Cell):
>>> def __init__(self):
>>> super(Net, self).__init__()
>>> self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal')
>>> self.bn = nn.BatchNorm2d(64)
>>> class LeNet5(nn.Cell):
>>> def __init__(self, num_class=10, num_channel=1):
>>> 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()
>>> self.fc = nn.Dense(64*224*224, 12)
>>>
>>> def construct(self, x):
>>> x = self.conv(x)
>>> x = self.bn(x)
>>> x = self.relu(x)
>>> x = self.max_pool2d(self.relu(self.conv1(x)))
>>> x = self.max_pool2d(self.relu(self.conv2(x)))
>>> x = self.flatten(x)
>>> out = self.fc(x)
>>> return out
>>> x = self.relu(self.fc1(x))
>>> x = self.relu(self.fc2(x))
>>> x = self.fc3(x)
>>> return x
>>>
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
>>> 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)
>>> dataset = get_dataset()
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> config = CheckpointConfig(saved_network=net)
>>> ckpoint_cb = ModelCheckpoint(prefix="ck_prefix", directory='./', config=config)
>>> ckpoint_cb = ModelCheckpoint(prefix='LeNet5', directory='./checkpoint', config=config)
>>> model.train(10, dataset, callbacks=ckpoint_cb)
"""
def __init__(self,

View File

@ -251,6 +251,10 @@ def load_checkpoint(ckpt_file_name, net=None, strict_load=False, filter_prefix=N
Raises:
ValueError: Checkpoint file is incorrect.
Examples:
>>> ckpt_file_name = "./checkpoint/LeNet5-2_1875.ckpt"
>>> param_dict = load_checkpoint(ckpt_file_name, filter_prefix="conv1")
"""
if not isinstance(ckpt_file_name, str):
raise ValueError("The ckpt_file_name must be string.")