Python and AI Development Frameworks
What this domain covers
The exam tests practical Python plus the tensor, dataset, and model APIs used in MindSpore and comparable frameworks.
Core rules and concepts
- NumPy broadcasting aligns shapes from the right; a dimension of size 1 is stretched, otherwise the shapes must match.
- Pandas:
.locis label-based,.ilocis position-based. - MindSpore tensors are created with
mindspore.Tensor; dtype and device are explicit. - Execution mode is set with
context.set_context(mode=context.GRAPH_MODE or PYNATIVE_MODE, device_target="Ascend"). GRAPH_MODE compiles the graph; PYNATIVE_MODE runs op-by-op for debugging. - A model subclasses
nn.Cell, defines layers in__init__, and implementsconstruct(), which is the forward function. PyTorch usesforward(); MindSpore callsconstruct()itself. - Datasets come from
mindspore.dataset, withmap,batch, andshuffleoperations.
Worked example
class Net(nn.Cell):
def __init__(self):
super().__init__()
self.fc1 = nn.Dense(784, 128)
self.relu = nn.ReLU()
self.fc2 = nn.Dense(128, 10)
def construct(self, x):
return self.fc2(self.relu(self.fc1(x)))
Reasoning: nn.Dense(in_channels, out_channels) is the fully connected layer; layers must be assigned as attributes so parameters register; construct is invoked by the framework, so never call net.construct(x) manually in training code.
Traps
Writing forward() in a MindSpore Cell; forgetting super().__init__(); mixing torch.nn and mindspore.nn imports; creating tensors before setting device_target; assuming GRAPH_MODE supports arbitrary Python side effects.