From 120f6a372af488e56fb99422b11bc0805f0c7c64 Mon Sep 17 00:00:00 2001 From: caozhou Date: Wed, 21 Oct 2020 21:53:14 +0800 Subject: [PATCH] add api example --- mindspore/train/callback/_checkpoint.py | 35 ++++++++++++++----------- mindspore/train/serialization.py | 4 +++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/mindspore/train/callback/_checkpoint.py b/mindspore/train/callback/_checkpoint.py index 35cc198068b..34b1bd0624c 100644 --- a/mindspore/train/callback/_checkpoint.py +++ b/mindspore/train/callback/_checkpoint.py @@ -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, diff --git a/mindspore/train/serialization.py b/mindspore/train/serialization.py index 2d1f7818c7a..d5d8b45b017 100644 --- a/mindspore/train/serialization.py +++ b/mindspore/train/serialization.py @@ -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.")