mindspore.nn.CellDict

class mindspore.nn.CellDict(*args, **kwargs)[source]

Holds Cells in a dictionary. For more details about Cell , please refer to mindspore.nn.Cell .

CellDict can be used like a regular Python dictionary.

Parameters
  • args (iterable, optional) – An iterable of key-value pairs of (key, Cell), the type of key-value pairs is (string, Cell); Or a mapping(dictionary) from string to Cell. The type of Cell can not be CellDict, CellList or SequentialCell. The key can not be same with the attributes of class Cell, can not contain '.', can not be an empty string. The key of type string is used to search corresponding Cell in the CellDict.

  • kwargs (dict) – Reserved for keyword argument to be expanded.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import collections
>>> from collections import OrderedDict
>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor, nn
>>>
>>> cell_dict = nn.CellDict({'conv': nn.Conv2d(10, 6, 5),
...                          'relu': nn.ReLU(),
...                          'max_pool2d': nn.MaxPool2d(kernel_size=4, stride=4)})
>>> print(len(cell_dict))
3
>>> cell_dict.clear()
>>> print(len(cell_dict))
0
>>> ordered_cells = OrderedDict([('conv', nn.Conv2d(10, 6, 5, pad_mode='valid')),
...                              ('relu', nn.ReLU()),
...                              ('max_pool2d', nn.MaxPool2d(kernel_size=2, stride=2))])
>>> cell_dict.update(ordered_cells)
>>> x = Tensor(np.ones([1, 10, 6, 10]), ms.float32)
>>> for cell in cell_dict.values():
...     x = cell(x)
>>> print(x.shape)
(1, 6, 1, 3)
>>> x = Tensor(np.ones([1, 10, 6, 10]), ms.float32)
>>> for item in cell_dict.items():
...     x = item[1](x)
>>> print(x.shape)
(1, 6, 1, 3)
>>> print(cell_dict.keys())
odict_keys(['conv', 'relu', 'max_pool2d'])
>>> pop_cell = cell_dict.pop('conv')
>>> x = Tensor(np.ones([1, 10, 6, 5]), ms.float32)
>>> x = pop_cell(x)
>>> print(x.shape)
(1, 6, 2, 1)
>>> print(len(cell_dict))
2
clear()[source]

Remove all Cells from the CellDict.

items()[source]

Return an iterable of the CellDict key-value pairs.

Returns

An iterable object.

keys()[source]

Return an iterable of the CellDict keys.

Returns

An iterable object.

pop(key)[source]

Remove key from the CellDict and return its cell.

Parameters

key (string) – key to pop from the CellDict.

Raises

KeyError – If key not exist in CellDict when attempt to access cell.

update(cells)[source]

Update the CellDict by overwriting the existing keys with the key-value pairs from a mapping or an iterable.

Parameters

cells (iterable) – An iterable of key-value pairs of (key, Cell), the type of key-value pairs is (string, Cell); Or a mapping(dictionary) from string to Cell. The type of Cell can not be CellDict, CellList or SequentialCell. The key can not be same with the attributes of class Cell, can not contain '.', can not be an empty string.

Note

If the cells is a CellDict, an OrderedDict or an iterable containing key-value pairs, the order of newly added elements is maintained.

Raises
  • TypeError – If cells is not an iterable object.

  • TypeError – If key-value pairs in cells are not iterable objects.

  • ValueError – If the length of key-value pairs in cells is not 2.

  • TypeError – If the cell in cells is None.

  • TypeError – If the type of cell in cells is not Cell.

  • TypeError – If the type of cell in cells is CellDict, CellList or SequentialCell.

  • TypeError – If the type of key in cells is not string.

  • KeyError – If the key in cells is same with the attributes of class Cell.

  • KeyError – If the key in cells contain ".".

  • KeyError – If the key in cells is an empty string.

values()[source]

Return an iterable of the CellDict values.

Returns

An iterable object.