00-1 开发环境介绍
2026/6/30 17:02:29 网站建设 项目流程

🥳欢迎体验Jupyter云上开发环境

此开发环境是使用的是Jupyter Lab 3.x,并集成了实用的插件。您可以在Jupyter Lab上灵活运行调试代码和编写文档,Jupyter Lab集成了很多编辑器,例如Jupyter笔记本、文本编辑器、终端和自定义组件。更多的详细介绍你可以查看 JupyterLab官网文档介绍。

接下来跟我来一起沉浸式地探索Jupyter Lab吧❗

若您不熟悉Jupyter Lab, 您可以先阅读Jupyter-Document.ipynb文件熟悉Jupyter的基本操作

昇思MindSpore介绍

昇思MindSpore是一个全场景深度学习框架,旨在实现易开发、高效执行、全场景统一部署三大目标。其中,易开发表现为API友好,调试难度低;高效执行包括计算效率、数据预处理效率和分布式训练效率;全场景则指框架同时支持云、边缘以及端侧场景。

MindSpore整体架构如下:

  1. 模型套件:为开发者提供开箱即用的模型和开发套件,如大模型套件MindSpore Transformers、MindSpore ONE以及科学计算库等热点研究领域拓展库;
  2. 深度学习+科学计算:为开发者提供AI模型开发所需各类Python接口,最大化保持开发者在Python生态开发的使用习惯;
  3. 核心:作为AI框架的核心,构建Tensor数据结构、基础运算算子Operator、自动求导autograd模块、并行计算Parallel模块、编译compile能力以及runtime运行时管理模块。

下面以MindSpore Transformers为例,快速体验大模型的使用。

快速体验平台操作

快速体验推理

MindSpore MindFormers套件的目标是构建一个大模型预训练、微调、评测、推理、部署的全流程开发套件,提供业内主流的Transformer类大语言模型(Large Language Models, LLMs)和多模态理解模型(Multimodal Models, MMs)。期望帮助用户轻松地实现大模型全流程开发。

MindSpore Transformers套件基于MindSpore内置的多维混合并行技术和组件化设计,具备如下特点:

  • 一键启动模型单卡或多卡预训练、微调、评测、推理、部署流程;
  • 提供丰富的多维混合并行能力可供灵活易用地进行个性化配置;
  • 大模型训推系统级深度优化,原生支持超大规模集群高效训推,故障快速恢复;
  • 支持任务组件配置化开发。任意模块可通过统一配置进行使能,包括模型网络、优化器、学习率策略等;
  • 提供训练精度/性能监控指标实时可视化能力等。

MindSpore Transformers-版本1.5.0支持的模型列表如下:

模型名支持规格模型类型
CodeLlama34B稠密LLM
CogVLM2-Image19BMM
CogVLM2-Video13BMM
DeepSeek-V3671B稀疏LLM
DeepSeek-V2236B稀疏LLM
DeepSeek-Coder-V1.57B稠密LLM
DeepSeek-Coder33B稠密LLM
GLM49B稠密LLM
GLM3-32K6B稠密LLM
GLM36B稠密LLM
InternLM27B/20B稠密LLM
Llama3.23B稠密LLM
Llama3.2-Vision11BMM
Llama3.18B/70B稠密LLM
Llama38B/70B稠密LLM
Llama27B/13B/70B稠密LLM
Mixtral8x7B稀疏LLM
Qwen2.50.5B/1.5B/7B/14B/32B/72B稠密LLM
Qwen20.5B/1.5B/7B/57B/57B-A14B/72B稠密/稀疏LLM
Qwen1.50.5B/1.8B/4B/7B/14B/72B稠密LLM
Qwen-VL9.6BMM
TeleChat27B/35B/115B稠密LLM
TeleChat7B/12B/52B稠密LLM
Whisper1.5BMM
Yi6B/34B稠密LLM
YiZhao12B稠密LLM

如果感兴趣可以进入下面代码仓,一键三连哦

  • MindSpore Transformers仓
  • MindSpore仓

你可以Ctrl+Enter运行下面代码,或者使用菜单栏的三角形运行下面的Cell,注意一定要按顺序执行。

使用 vit 进行图像分类

%%capture captured_output# 实验环境已经预装了mindspore==2.6.0,如需更换mindspore版本,可更改下面 MINDSPORE_VERSION 变量!pip uninstall mindspore-y%env MINDSPORE_VERSION=2.6.0!pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MINDSPORE_VERSION}/MindSpore/unified/aarch64/mindspore-${MINDSPORE_VERSION}-cp39-cp39-linux_aarch64.whl--trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com-i https://pypi.tuna.tsinghua.edu.cn/simple
# 查看当前 mindspore 版本!pip show mindspore
Name: mindspore Version: 2.8.0 Summary: MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios. Home-page: https://www.mindspore.cn Author: The MindSpore Authors Author-email: contact@mindspore.cn License: Apache 2.0 Location: /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages Requires: asttokens, astunparse, dill, numpy, packaging, pillow, protobuf, psutil, safetensors, scipy Required-by:
# 控制MindSpore日志的级别, 0-DEBUG、1-INFO、2-WARNING、3-ERROR、4-EXCEPTIONimportos os.environ['GLOG_v']='3'
%%capture captured_output# 安装 mindformers 包!pip install-i https://repo.huaweicloud.com/repository/pypi/simple/mindformers==1.2.0

MindFormers大模型套件面向任务设计pipeline推理接口,旨在让用户可以便捷地体验不同AI领域的大模型在线推理服务,当前已集成10+任务的推理流程。
使用MindFormers预置任务和模型开发一个推理流:

# 若出现ValueError: Failed to read the checkpoint file . May not have permission to read it, please check the correct of the file. 请将下面注释放开#!rm -rf checkpoint_download/vit/frommindformers.pipelineimportpipelinefrommindformers.tools.image_toolsimportload_imagefromPILimportImageimportmatplotlib.pyplotasplt pipeline_task=pipeline("image_classification",model="vit_base_p16")
/home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for <class 'numpy.float64'> type is zero. setattr(self, word, getattr(machar, word).flat[0]) /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for <class 'numpy.float64'> type is zero. return self._float_to_str(self.smallest_subnormal) /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:549: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero. setattr(self, word, getattr(machar, word).flat[0]) /home/mindspore/miniconda/envs/jupyter/lib/python3.9/site-packages/numpy/core/getlimits.py:89: UserWarning: The value of the smallest subnormal for <class 'numpy.float32'> type is zero. return self._float_to_str(self.smallest_subnormal) 2025-02-22 16:46:50,265 - mindformers[mindformers/tools/download_tools.py:97] - INFO - Start download /home/mindspore/work/checkpoint_download/vit/vit_base_p16.ckpt Downloading: 346MB [00:22, 15.1MB/s] 2025-02-22 16:47:13,254 - mindformers[mindformers/tools/download_tools.py:110] - INFO - Download completed!, times: 23.27s 2025-02-22 16:47:13,258 - mindformers[mindformers/models/modeling_utils.py:1524] - INFO - start to read the ckpt file: 346282549 2025-02-22 16:47:14,124 - mindformers[mindformers/models/modeling_utils.py:1529] - INFO - weights in ./checkpoint_download/vit/vit_base_p16.ckpt are loaded
fromdownloadimportdownload# 下载测试图片url="https://obs-xihe-beijing4.obs.cn-north-4.myhuaweicloud.com/jupyter/images/daisy.jpg"download(url,"daisy.jpg",replace=True)
Downloading data from https://obs-xihe-beijing4.obs.cn-north-4.myhuaweicloud.com/jupyter/images/daisy.jpg (314 kB) file_sizes: 100%|████████████████████████████| 321k/321k [00:00<00:00, 7.10MB/s] Successfully downloaded file to daisy.jpg 'daisy.jpg'
fromPILimportImageimportmatplotlib.pyplotasplt test_png="daisy.jpg"picture=Image.open(test_png)plt.figure('image')plt.imshow(picture)plt.show()

img=load_image(test_png)pipeline_result=pipeline_task(img,top_k=3)pipeline_result
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ----> 1 img = load_image(test_png) 2 pipeline_result = pipeline_task(img, top_k=3) 3 pipeline_result NameError: name 'load_image' is not defined

使用 BERT 进行中文命名实体识别

# pipeline接口开启快速推理frommindformers.pipelineimportTokenClassificationPipelinefrommindformersimportAutoTokenizer,BertForTokenClassification,AutoConfigfrommindformers.dataset.labelsimportcluener_labels id2label={label_id:labelforlabel_id,labelinenumerate(cluener_labels)}tokenizer=AutoTokenizer.from_pretrained("tokcls_bert_base_chinese_cluener",)tokcls_cluener_config=AutoConfig.from_pretrained("tokcls_bert_base_chinese_cluener",)# This is a known issue, you need to specify batch size equal to 1 when creating bert model.tokcls_cluener_config.batch_size=1model=BertForTokenClassification(tokcls_cluener_config)tokcls_pipeline=TokenClassificationPipeline(task='token_classification',model=model,id2label=id2label,tokenizer=tokenizer,max_length=model.config.seq_length,padding="max_length")
2025-02-22 16:47:29,371 - mindformers[mindformers/models/auto/tokenization_auto.py:396] - INFO - default yaml config in ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.yaml is used. 2025-02-22 16:47:29,372 - mindformers[mindformers/models/auto/tokenization_auto.py:328] - INFO - Config in the yaml file ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.yaml are used for tokenizer building. 2025-02-22 16:47:29,411 - mindformers[mindformers/models/auto/tokenization_auto.py:335] - INFO - Load the tokenizer name BertTokenizer from the ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.yaml 2025-02-22 16:47:29,412 - mindformers[mindformers/models/tokenization_utils_base.py:1949] - INFO - Download the vocab from the url https://ascend-repo-modelzoo.obs.cn-east-2.myhuaweicloud.com/XFormer_for_mindspore/bert/bert_base_chinese/vocab.txt to ./checkpoint_download/tokcls/vocab.txt. 2025-02-22 16:47:29,671 - mindformers[mindformers/tools/download_tools.py:97] - INFO - Start download /home/mindspore/work/checkpoint_download/tokcls/vocab.txt Downloading: 110kB [00:00, 2.01MB/s] 2025-02-22 16:47:29,729 - mindformers[mindformers/tools/download_tools.py:110] - INFO - Download completed!, times: 0.32s 2025-02-22 16:47:29,768 - mindformers[mindformers/models/tokenization_utils_base.py:1886] - WARNING - Can't find the tokenizer_config.json in the file_dict. The content of file_dict is : {} 2025-02-22 16:47:29,769 - mindformers[mindformers/models/tokenization_utils_base.py:1893] - INFO - build tokenizer class name is: BertTokenizer using args {'cls_token': '[CLS]', 'mask_token': '[MASK]', 'pad_token': '[PAD]', 'sep_token': '[SEP]', 'unk_token': '[UNK]', 'is_tokenize_char': True, 'do_lower_case': False, 'checkpoint_name_or_path': 'tokcls_bert_base_chinese', 'vocab_file': './checkpoint_download/tokcls/vocab.txt'}. 2025-02-22 16:47:29,813 - mindformers[mindformers/models/auto/tokenization_auto.py:413] - INFO - BertTokenizer Tokenizer built successfully! 2025-02-22 16:47:32,208 - mindformers[mindformers/tools/download_tools.py:97] - INFO - Start download /home/mindspore/work/checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.ckpt Downloading: 1.23GB [01:30, 13.5MB/s] 2025-02-22 16:49:03,003 - mindformers[mindformers/tools/download_tools.py:110] - INFO - Download completed!, times: 91.10s 2025-02-22 16:49:03,006 - mindformers[mindformers/models/modeling_utils.py:1524] - INFO - start to read the ckpt file: 1227549758 2025-02-22 16:49:05,831 - mindformers[mindformers/models/modeling_utils.py:1529] - INFO - weights in ./checkpoint_download/tokcls/tokcls_bert_base_chinese_cluener.ckpt are loaded
input_data=["小明作为开发人员去北京参加华为开发者大会。"]results=tokcls_pipeline(input_data)print(results)
.[[{'entity_group': 'position', 'start': 4, 'end': 6, 'score': 0.72438, 'word': '开发人'}, {'entity_group': 'address', 'start': 9, 'end': 10, 'score': 0.93827, 'word': '北京'}, {'entity_group': 'company', 'start': 13, 'end': 14, 'score': 0.54733, 'word': '华为'}]]

快速体验训练

这里以Lenet5实现手写数字体为案例

基本介绍

LeNet描述
LeNet是1998年提出的一种典型的卷积神经网络。它被用于数字识别并取得了巨大的成功。

论文: Y.Lecun, L.Bottou, Y.Bengio, P.Haffner.Gradient-Based Learning Applied to Document Recognition.Proceedings of the IEEE.1998.

模型架构

按照LeNet的网络结构,LeNet除去输入层共有7层,其中有2个卷积层,2个子采样层,3个全连接层。

下载MNIST数据集

MNIST数据集是由10类28∗28的灰度图片组成,训练数据集包含60000张图片,测试数据集包含10000张图片。
下载完成后,下载完成后的目录结构如下:

└── MNIST_Data ├── test # 测试集 │ ├── t10k-images-idx3-ubyte (10000个测试图片) │ └── t10k-labels-idx1-ubyte (10000个测试标签) │── train # 训练集 │ ├── train-images-idx3-ubyte (60000个训练图片) │ └── train-lables-idx1-ubyte (60000个训练标签) └── README.md
fromdownloadimportdownload url=("https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/""notebook/datasets/MNIST_Data.zip")path=download(url,"./",kind="zip",replace=True)
Downloading data from https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/datasets/MNIST_Data.zip (10.3 MB) file_sizes: 100%|███████████████████████████| 10.8M/10.8M [00:00<00:00, 120MB/s] Extracting zip file... Successfully downloaded / unzipped to ./

创建模型

MindSpore Vision套件提供了LeNet网络模型接口lenet, 定义网络模型如下:

frommindvision.classification.modelsimportlenet network=lenet(num_classes=10,pretrained=False)

定义损失函数和优化器

要训练神经网络模型,需要定义损失函数和优化器函数。

损失函数这里使用交叉熵损失函数SoftmaxCrossEntropyWithLogits。

优化器这里使用Momentum。

# 超参设置learning_rate=0.01momentum=0.9batch_size=32num_epochs=5epoch_size=10
importmindspore.nnasnn# 定义损失函数net_loss=nn.SoftmaxCrossEntropyWithLogits(sparse=True,reduction='mean')# 定义优化器函数net_opt=nn.Momentum(network.trainable_params(),learning_rate=learning_rate,momentum=momentum)

模型训练

frommindspore.trainimportModelfrommindvision.classification.datasetimportMnistfrommindspore.train.callbackimportModelCheckpoint,CheckpointConfigfrommindvision.engine.callbackimportLossMonitor data_url="./MNIST_Data"output_path="./lenet"# 初始化模型参数model=Model(network,loss_fn=net_loss,optimizer=net_opt,metrics={'accuracy'})deftrain():# 定义训练数据集download_train=Mnist(path=data_url,split="train",batch_size=batch_size,repeat_num=1,shuffle=True,resize=32,download=True,)dataset_train=download_train.run()# 设置模型保存参数,模型训练保存参数的step为1875config_ck=CheckpointConfig(save_checkpoint_steps=1875,keep_checkpoint_max=10)# 应用模型保存参数ckpoint=ModelCheckpoint(prefix="lenet",directory=output_path,config=config_ck)# 训练网络模型,并保存为lenet-1_1875.ckpt文件model.train(epoch_size,dataset_train,callbacks=[ckpoint,LossMonitor(0.01,1875)])# 执行训练train()
....Epoch:[ 0/ 10], step:[ 1875/ 1875], loss:[0.055/0.361], time:19.343 ms, lr:0.01000 Epoch time: 54393.399 ms, per step time: 29.010 ms, avg loss: 0.361 Epoch:[ 1/ 10], step:[ 1875/ 1875], loss:[0.021/0.061], time:18.822 ms, lr:0.01000 Epoch time: 18903.794 ms, per step time: 10.082 ms, avg loss: 0.061 Epoch:[ 2/ 10], step:[ 1875/ 1875], loss:[0.022/0.044], time:17.664 ms, lr:0.01000 Epoch time: 18859.462 ms, per step time: 10.058 ms, avg loss: 0.044 Epoch:[ 3/ 10], step:[ 1875/ 1875], loss:[0.001/0.035], time:18.250 ms, lr:0.01000 Epoch time: 19054.932 ms, per step time: 10.163 ms, avg loss: 0.035 Epoch:[ 4/ 10], step:[ 1875/ 1875], loss:[0.009/0.029], time:18.280 ms, lr:0.01000 Epoch time: 19356.754 ms, per step time: 10.324 ms, avg loss: 0.029 Epoch:[ 5/ 10], step:[ 1875/ 1875], loss:[0.101/0.023], time:18.708 ms, lr:0.01000 Epoch time: 19428.140 ms, per step time: 10.362 ms, avg loss: 0.023 Epoch:[ 6/ 10], step:[ 1875/ 1875], loss:[0.098/0.020], time:17.957 ms, lr:0.01000 Epoch time: 19325.511 ms, per step time: 10.307 ms, avg loss: 0.020 Epoch:[ 7/ 10], step:[ 1875/ 1875], loss:[0.000/0.016], time:18.525 ms, lr:0.01000 Epoch time: 19252.076 ms, per step time: 10.268 ms, avg loss: 0.016 Epoch:[ 8/ 10], step:[ 1875/ 1875], loss:[0.005/0.015], time:18.509 ms, lr:0.01000 Epoch time: 19266.987 ms, per step time: 10.276 ms, avg loss: 0.015 Epoch:[ 9/ 10], step:[ 1875/ 1875], loss:[0.005/0.012], time:17.811 ms, lr:0.01000 Epoch time: 19322.312 ms, per step time: 10.305 ms, avg loss: 0.012

训练过程中会打印loss值,loss值会波动,但总体来说loss值会逐步减小,精度逐步提高。每个人运行的loss值有一定随机性,不一定完全相同。

模型验证及推理

通过模型运行测试数据集得到的结果,验证模型的泛化能力:

1.使用model.eval接口读入测试数据集。

2.使用保存后的模型参数进行推理。

测试集验证精确度
defevaluate():# 定义训练数据集download_test=Mnist(path=data_url,split="test",batch_size=batch_size,repeat_num=1,shuffle=True,resize=32,download=True,)dataset_test=download_test.run()acc=model.eval(dataset_test)print("{}".format(acc))evaluate()
{'accuracy': 0.9904847756410257}

可以在打印信息中看出模型精度数据,示例中精度数据达到95%以上,模型质量良好。随着网络迭代次数增加,模型精度会进一步提高。

模型推理

我们使用生成的模型进行单个图片数据的分类预测,具体步骤如下:

importnumpyasnpfrommindsporeimportTensorimportmatplotlib.pyplotasplt mnist=Mnist(data_url,split="train",batch_size=6,resize=32)dataset_infer=mnist.run()ds_test=dataset_infer.create_dict_iterator()data=next(ds_test)images=data["image"].asnumpy()labels=data["label"].asnumpy()plt.figure()foriinrange(1,7):plt.subplot(2,3,i)plt.imshow(images[i-1][0],interpolation="None",cmap="gray")plt.show()# 使用函数model.predict预测image对应分类output=model.predict(Tensor(data['image']))predicted=np.argmax(output.asnumpy(),axis=1)# 输出预测分类与实际分类print(f'Predicted: "{predicted}", Actual: "{labels}"')

Predicted: "[3 7 9 8 6 1]", Actual: "[3 7 9 8 6 1]"

更多体验持续更新中…

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询