pull/9279/head
bai-yangfan 4 years ago
parent a09f1e30b6
commit 7bf9f4819f

@ -55,13 +55,13 @@ class Cell(Cell_):
``Ascend`` ``GPU`` ``CPU`` ``Ascend`` ``GPU`` ``CPU``
Examples: Examples:
>>> class MyCell(Cell): >>> class MyCell(nn.Cell):
>>> def __init__(self): ... def __init__(self):
>>> super(MyCell, self).__init__() ... super(MyCell, self).__init__()
>>> self.relu = P.ReLU() ... self.relu = P.ReLU()
>>> ...
>>> def construct(self, x): ... def construct(self, x):
>>> return self.relu(x) ... return self.relu(x)
""" """
IGNORE_LIST = ['_scope', '_cell_init_args', '_auto_prefix', '_cells', '_params', '_construct_inputs_names', IGNORE_LIST = ['_scope', '_cell_init_args', '_auto_prefix', '_cells', '_params', '_construct_inputs_names',
'_construct_inputs_num', '_create_time', '_mindspore_flags', '_parallel_inputs_run', '_construct_inputs_num', '_create_time', '_mindspore_flags', '_parallel_inputs_run',
@ -776,8 +776,9 @@ class Cell(Cell_):
Examples: Examples:
>>> net = Net() >>> net = Net()
>>> parameters = []
>>> for item in net.get_parameters(): >>> for item in net.get_parameters():
>>> print(item) ... parameters.append(item)
""" """
for _, param in self.parameters_and_names(expand=expand): for _, param in self.parameters_and_names(expand=expand):
yield param yield param
@ -805,8 +806,8 @@ class Cell(Cell_):
>>> n = Net() >>> n = Net()
>>> names = [] >>> names = []
>>> for m in n.parameters_and_names(): >>> for m in n.parameters_and_names():
>>> if m[0]: ... if m[0]:
>>> names.append(m[0]) ... names.append(m[0])
""" """
cells = [] cells = []
if expand: if expand:
@ -842,8 +843,8 @@ class Cell(Cell_):
>>> n = Net() >>> n = Net()
>>> names = [] >>> names = []
>>> for m in n.cells_and_names(): >>> for m in n.cells_and_names():
>>> if m[0]: ... if m[0]:
>>> names.append(m[0]) ... names.append(m[0])
""" """
t_cells = cells if cells else set() t_cells = cells if cells else set()
if self in t_cells: if self in t_cells:
@ -1016,7 +1017,7 @@ class Cell(Cell_):
fn must be defined as the following code. `cell_name` is the name of registered cell. fn must be defined as the following code. `cell_name` is the name of registered cell.
`grad_input` is gradient passed to the cell. `grad_output` is the gradient computed and passed to the `grad_input` is gradient passed to the cell. `grad_output` is the gradient computed and passed to the
next cell or primitve, which may be modified and returned. next cell or primitve, which may be modified and returned.
>>> hook_fn(cell_name, grad_input, grad_output) -> Tensor or None hook_fn(cell_name, grad_input, grad_output) -> Tensor or None.
Args: Args:
fn (function): Specifies the hook function with grad as input. fn (function): Specifies the hook function with grad as input.
@ -1051,13 +1052,13 @@ class GraphKernel(Cell):
enable_graph_kernel in context is set to True. enable_graph_kernel in context is set to True.
Examples: Examples:
>>> class Relu(GraphKernel): >>> class Relu(nn.GraphKernel):
>>> def __init__(self): ... def __init__(self):
>>> super(Relu, self).__init__() ... super(Relu, self).__init__()
>>> self.max = P.Maximum() ... self.max = P.Maximum()
>>> ...
>>> def construct(self, x): ... def construct(self, x):
>>> return self.max(P.Fill()(P.DType()(x), P.Shape()(x), 0.0), x) ... return self.max(P.Fill()(P.DType()(x), P.Shape()(x), 0.0), x)
""" """
def __init__(self, auto_prefix=True, pips=None): def __init__(self, auto_prefix=True, pips=None):

@ -88,25 +88,29 @@ class Model:
Examples: Examples:
>>> class Net(nn.Cell): >>> class Net(nn.Cell):
>>> def __init__(self): ... def __init__(self, num_class=10, num_channel=1):
>>> super(Net, self).__init__() ... super(Net, self).__init__()
>>> self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal') ... self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
>>> self.bn = nn.BatchNorm2d(64) ... self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
>>> self.relu = nn.ReLU() ... self.fc1 = nn.Dense(16*5*5, 120, weight_init='ones')
>>> self.flatten = nn.Flatten() ... self.fc2 = nn.Dense(120, 84, weight_init='ones')
>>> self.fc = nn.Dense(64*224*224, 12) # padding=0 ... self.fc3 = nn.Dense(84, num_class, weight_init='ones')
>>> ... self.relu = nn.ReLU()
>>> def construct(self, x): ... self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
>>> x = self.conv(x) ... self.flatten = nn.Flatten()
>>> x = self.bn(x) ...
>>> x = self.relu(x) ... def construct(self, x):
>>> x = self.flatten(x) ... x = self.max_pool2d(self.relu(self.conv1(x)))
>>> out = self.fc(x) ... x = self.max_pool2d(self.relu(self.conv2(x)))
>>> return out ... x = self.flatten(x)
... x = self.relu(self.fc1(x))
... x = self.relu(self.fc2(x))
... x = self.fc3(x)
... return x
>>> >>>
>>> net = Net() >>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) >>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> optim = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9) >>> optim = nn.Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None) >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
>>> # For details about how to build the dataset, please refer to the tutorial document on the official website. >>> # For details about how to build the dataset, please refer to the tutorial document on the official website.
>>> dataset = create_custom_dataset() >>> dataset = create_custom_dataset()
@ -545,9 +549,10 @@ class Model:
If dataset_sink_mode is False, set sink_size as invalid. Default: -1. If dataset_sink_mode is False, set sink_size as invalid. Default: -1.
Examples: Examples:
>>> from mindspore.train.loss_scale_manager import FixedLossScaleManager
>>> dataset = create_custom_dataset() >>> dataset = create_custom_dataset()
>>> net = Net() >>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) >>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> loss_scale_manager = FixedLossScaleManager() >>> loss_scale_manager = FixedLossScaleManager()
>>> optim = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9) >>> optim = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None, loss_scale_manager=loss_scale_manager) >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None, loss_scale_manager=loss_scale_manager)
@ -667,9 +672,9 @@ class Model:
Examples: Examples:
>>> dataset = create_custom_dataset() >>> dataset = create_custom_dataset()
>>> net = Net() >>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) >>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=None, metrics={'acc'}) >>> model = Model(net, loss_fn=loss, optimizer=None, metrics={'acc'})
>>> model.eval(dataset) >>> acc = model.eval(dataset, dataset_sink_mode=False)
""" """
dataset_sink_mode = Validator.check_bool(dataset_sink_mode) dataset_sink_mode = Validator.check_bool(dataset_sink_mode)
_device_number_check(self._parallel_mode, self._device_number) _device_number_check(self._parallel_mode, self._device_number)
@ -713,9 +718,9 @@ class Model:
Tensor, array(s) of predictions. Tensor, array(s) of predictions.
Examples: Examples:
>>> input_data = Tensor(np.random.randint(0, 255, [1, 3, 224, 224]), mindspore.float32) >>> input_data = Tensor(np.random.randint(0, 255, [1, 1, 32, 32]), mindspore.float32)
>>> model = Model(Net()) >>> model = Model(Net())
>>> model.predict(input_data) >>> result = model.predict(input_data)
""" """
self._predict_network.set_train(False) self._predict_network.set_train(False)
check_input_data(*predict_data, data_class=Tensor) check_input_data(*predict_data, data_class=Tensor)

Loading…
Cancel
Save