Welcome to TinyMS’s documentation!

什么是TinyMS

TinyMS项目

TinyMS简介

  • TinyMS是一款主要用PyThon语言编写的开源深度学习开发工具包,基于以MindSpore为代表的新型开源深度学习框架,提供面向从数据准备到模型部署全流程的极简易用的高阶API封装,并通过易于扩展的模块化设计,提供覆盖多种业务场景的能力。

  • 主要由data, model, serving等模块组成,分场景分领域提供transform数据预处理算子,复用MindSpore原生数据集提供常用数据集,如:cifar-10等。data提供部分自定义数据集和常用的数据下载和解压等常用工具集,model提供常用的预置模型,并提供模型构建,模型编译,模型训练、验证与推理。serving通过搭建服务器来提供AI模型应用服务,为新手提供快速推理的体验。

  • TinyMS面向的主要用户群体为深度学习初学者、研究领域涉及深度学习结合的科研人员、以及深度学习相关业务应用开发的企业开发人员。

  • 通过搭配完整的在线课程教学,TinyMS提供目前业界最佳的深度学习入门与开发体验。

TinyMS vs Keras

Keras 是一个用 Python 编写的高级神经网络 API,将把用户体验放在首要位置,支持短时间内出实验。

Keras项目可以说是“大而全”,主要由dataset, layer, model和backend模块构成,提供较多常用的预置数据集,并分场景分领域提供数据预处理函数,layer网络层提供较完善,如:convolution卷积层,embedding嵌入层,pooling池化层等。backend支持多个后端(TensorFlow、CNTK和Theano),与TensorFlow版本不强耦合。Model提供模型选择(sequential)、网络层构建(输入层、输出层和池化层等),模型编译,模型训练、验证与推理。

TinyMS在高阶API方面会更为简单抽象,较Keras来说复杂度更低,比如提供了只需一行代码即可完成数据集的预处理,而且在设计中重点考虑到了Keras尚未提供单独好用的工具库,以及尚未提供的快速部署推理模块等。

TinyMS vs Fastai

Fastai是为了帮助新手快速轻松出结果的高阶API项目, 其基于PyTorch的深度学习库,利用底层PyTorch库的灵活性,分领域分场景地提供包括对vision,text,tabular和collab模型的“开箱即用”的支持,后端对PyTorch的版本要求紧耦合。

fastai 深度学习库项目较轻便,目录清晰易理解,可以说是“小而美”,主要由data, models和learner三大模块构成,其中,data提供了transform类方便开发者进行数据预处理操作。models按应用领域,提供部分预置网络,如:unet,快速实现模型构建。learner实现数据和模型的关联,并定义了一系列回调函数,帮助开发者快速厘清深度学习的架构,提供模型训练、模型评估、模型保存与加载和模型推理。除此之外,还拥有较丰富好用的工具集,如:数据下载,解压,图片验证和文件处理等工具函数。

TinyMS在高阶API方面理念与Fastai相近,不同点在于TinyMS提供了常用的MindSpore预置数据集,方便开发者简化对数据集的调用,而且提供了Fastai尚未提供的快速部署推理模块等。

TinyMS开源社区

TinyMS开源社区中除了TinyMS项目外,还有如下一些项目和活动:

  • Specification项目:主要用来协作制定面向模型训练脚本的格式规范。由于TinyMS提供了较为高阶的API抽象,因此诞生了ModelZoo脚本规范性和标准化的需求,便于高阶封装的持续迭代

  • https://tinyms-ai.github.io:开源实现的简单官方网站搭建,基于Github Page

  • RustedAI Team:目前只有组织成员可见,RustedAI是TinyMS旨在推动利用Rust语言编写更多的低运行时开销的深度学习组件

  • 社区活动:我们会不定期的组织TinyMS模型拉力赛,以及多种多样的Meetup活动

TinyMS与开发者

TinyMS是一个新生的开源项目,我们站在Keras、Fastai等巨人的肩膀上,虽然在设计理念上有所创新,但依然需要社区开发者一起持续的协作,才能达到可以更好的服务学术界、产业界和开发者的深度和广度。

快速安装TinyMS

新用户上手安装

Pypi

对于拥有干净环境的用户,在满足以下环境要求后,推荐使用 pypi 安装TinyMS。安装python环境,可以使用 Anaconda

环境要求

  • OS: Ubuntu 18.04 or Windows 10

  • Python: 3.7.5

中国国内用户可以运行以下代码配置国内镜像源,解决下载速度慢的问题

mkdir -pv /root/.pip \
&& echo "[global]" > /root/.pip/pip.conf \
&& echo "trusted-host=mirrors.aliyun.com" >> /root/.pip/pip.conf \
&& echo "index-url=http://mirrors.aliyun.com/pypi/simple/" >> /root/.pip/pip.conf
pip install tinyms==0.3.1

注:在执行过程中可能会出现一些问题,以下可能情况仅供参考。在安装过程中你可能会碰到其它问题,我们欢迎你在我们的社区,提出您的Issues和Pull requests,我们会及时回复您。

  1. Error 1:若使用镜像源执行安装命令可能会报 Could not find a version that satisfies the requirement tinyms==0.3.1

    解决方案:

    • 可以试试使用默认官方源,直接在末尾追加-i https://pypi.python.org/simple/,采用默认官方源下载速度可能较慢,请耐心等待:smile:

  2. Error 2:如果是windows用户请确保是否安装了Microsoft VC++ 14.0,若没有,安装过程中可能会报ERROR:Microsoft Visual C++ 14.0 or greater is required.Get it with “Microsoft C++ Build Tools”: https://visualstudio.microsoft.com/visual-cpp-build-tools/

    解决方案:

    • 因为TinyMS是对Python3.7.5环境依赖的,而Python3是通过VC++ 14.0编译的。我们可以根据错误提示,在提供的链接下载Microsoft C++ Build Tools,注意在安装过程中需在使用C++桌面的桌面开发模块勾选windows 10 SDK用于Windows的C++ CMake工具俩个组件,相关安装详情可以参考Visual studio 生成工具安装

Docker

对于不想改变本地环境设置的用户,推荐使用 docker 进行安装

  • docker: v18.06.1-ce

如果想要体验本教程中的.ipynb教程,建议拉取TinyMS jupyter版本的镜像,该镜像中除了tinyms外还内置了jupyter组件

如果想要在WEB界面体验图片可视化推理,建议拉取TinyMS nginx版本的镜像,该镜像中除了tinyms外还内置了nginx组件

  • 普通版本

docker pull tinyms/tinyms:0.3.1
docker run -it tinyms/tinyms:0.3.1
  • Jupyter版本

如果想体验jupyter教程,运行下列命令行

docker pull tinyms/tinyms:0.3.1-jupyter
docker run -it --net=host tinyms/tinyms:0.3.1-jupyter

在本地打开浏览器,输入

<公网IP地址>:8888

例如 188.8.8.88:8888,之后在弹出的页面中,密码输入tinyms,就可以远程登录jupyter

  • Nginx版本

如果想在可视化WEB界面体验图片推理服务,运行下列命令行

docker pull tinyms/tinyms:0.3.1-nginx
docker run -itd --name=tinyms-nginx -p 80:80 tinyms/tinyms:0.3.1-nginx /bin/bash

docker exec -it tinyms-nginx /bin/bash
entrypoint.sh <容器所在宿主机的公网IP地址>

在本地打开浏览器,输入

<容器所在宿主机的公网IP地址>:80

进阶用户源码安装

想针对TinyMS进行开发的开发者,可以通过源码安装

sudo apt-get install -y libssl-dev
git clone https://github.com/tinyms-ai/tinyms.git
cd tinyms
pip install -r requirements.txt
python setup.py install

验证

进入 pythonjupyternginx 环境,输入以下代码验证安装

import tinyms as ts
from tinyms.primitives import tensor_add

x = ts.ones([2, 3])
y = ts.ones([2, 3])
print(tensor_add(x, y))

如果可以看到如下输出,则证明安装成功

[[2. 2. 2.]
 [2. 2. 2.]]

Notes

在我们使用TinyMS 0.3.1的时候可能会报以下错误

错误详情:

[ERROR] ME(24148:23792,MainProcess):2022-01-25-21:59:25.562.448 [mindspore\_extends\parse\parser.py:565] When eval 'P.tensor_add(identity, x)' by using Fallback feature, an error occurred: name 'identity' is not defined. You can try to turn off the Fallback feature by 'export MS_DEV_ENABLE_FALLBACK=0'.

解决方案:

根据错误提示,我们可以通过以下命令将Fallback feature关掉

对于一般用户,在命令行工具执行以下命令:

export MS_DEV_ENABLE_FALLBACK=0

对于使用jupyter的用户,在cell执行以下命令:

!export MS_DEV_ENABLE_FALLBACK=0

如果您在使用TinyMS 0.3.1过程中报其它ERROR,在您尝试解决ERROR过后,依然有问题,欢迎您在我们的社区,提出您的Issues和Pull requests,我们会及时回复您。

在一分钟内实现一个图形分类应用

在本教程中,我们会演示使用TinyMS 0.3.1 API构建LeNet5模型,下载数据集,训练,启动服务器和推理的过程。

环境要求

  • Ubuntu: 18.04

  • docker: v18.06.1-ce

  • Jupyter: 使用TinyMS 0.3.1-Jupyter,可参考 快速安装TinyMS 部署该环境。

介绍

TinyMS是一个高级API,目的是让新手用户能够更加轻松地上手深度学习。TinyMS可以有效地减少用户在构建、训练、验证和推理一个模型过程中的操作次数。TinyMS也提供了教程和文档帮助开发者更好的上手和开发。

本教程包括6部分:构建模型下载数据集训练定义servable json启动服务器推理,其中服务器将在一个子进程中启动。

[1]:
import os
import json
from PIL import Image

from tinyms import context
from tinyms.data import MnistDataset, download_dataset
from tinyms.vision import mnist_transform, ImageViewer
from tinyms.model import Model, lenet5
import tinyms.optimizers as opt
from tinyms.serving import Server, Client
from tinyms.metrics import Accuracy
from tinyms.losses import SoftmaxCrossEntropyWithLogits
from tinyms.callbacks import ModelCheckpoint, CheckpointConfig, LossMonitor
[WARNING] ME(12716:140477914756928,MainProcess):2021-03-19-15:58:38.621.652 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.

1. 构建模型

TinyMS封装了MindSpore LeNet5模型中的init和construct函数,代码行数能够大大减少,原有的大量代码段行数会被极限压缩:

[2]:
# 构建网络
net = lenet5(class_num=10)
model = Model(net)

2. 下载数据集

如果/home/jovyan文件夹下没有创建mnist文件夹则MNIST数据集会被自动下载并存放到/home/jovyan文件夹,如果mnist文件夹已经存在于/home/jovyan文件夹,则此步操作会被跳过。

[3]:
# 下载数据集
mnist_path = '/home/jovyan/mnist'
if not os.path.exists(mnist_path):
    download_dataset('mnist', '/home/jovyan')
    print('************Download complete*************')
else:
    print('************Dataset already exists.**************')
************Dataset already exists.**************

3. 训练模型

数据集中的训练集、验证集都会在此步骤中定义,同时也会定义训练参数。训练后生成的ckpt文件会保存到/home/jovyan/tinyms/serving/lenet5文件夹以便后续使用,训练完成后会进行验证并输出 Accuracy指标。

[ ]:
# 创建mnist路径
ckpt_folder = '/home/jovyan/tinyms/serving/lenet5'
ckpt_path = '/home/jovyan/tinyms/serving/lenet5/lenet5.ckpt'
if not os.path.exists(ckpt_folder):
    !mkdir -p  /home/jovyan/tinyms/serving/lenet5
else:
    print('lenet5 ckpt folder already exists')

# 设置环境参数
device_target = "CPU"
context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
dataset_sink_mode = False

# 创建数据集
train_dataset = MnistDataset(os.path.join(mnist_path, "train"), shuffle=True)
train_dataset = mnist_transform.apply_ds(train_dataset)
eval_dataset = MnistDataset(os.path.join(mnist_path, "test"), shuffle=True)
eval_dataset = mnist_transform.apply_ds(eval_dataset)

# 设置训练参数
lr = 0.01
momentum = 0.9
epoch_size = 1
batch_size = 32

# 定义loss函数
net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')

# 定义optimizer
net_opt = opt.Momentum(net.trainable_params(), lr, momentum)
net_metrics={"Accuracy": Accuracy()}
model.compile(loss_fn=net_loss, optimizer=net_opt, metrics=net_metrics)

print('************************Start training*************************')
ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet", config=CheckpointConfig(save_checkpoint_steps=1875, keep_checkpoint_max=10))
model.train(epoch_size, train_dataset, callbacks=[ckpoint_cb, LossMonitor()],dataset_sink_mode=dataset_sink_mode)
print('************************Finished training*************************')
model.save_checkpoint(ckpt_path)


model.load_checkpoint(ckpt_path)
print('************************Start evaluation*************************')
acc = model.eval(eval_dataset, dataset_sink_mode=dataset_sink_mode)
print("============== Accuracy:{} ==============".format(acc))

4. 定义servable.json

定义lenet5 servable json文件:servable json文件定义了servable名称,模型名称,模型格式和分类数量,以便后续推理使用。

[5]:
servable_json = [{'name': 'lenet5',
                  'description': 'This servable hosts a lenet5 model predicting numbers',
                  'model': {
                      "name": "lenet5",
                      "format": "ckpt",
                      "class_num": 10}}]
os.chdir("/home/jovyan/tinyms/serving")
json_data = json.dumps(servable_json, indent=4)

with open('servable.json', 'w') as json_file:
    json_file.write(json_data)

5. 启动服务器

5.1 介绍

TinyMS推理是C/S(Client/Server)架构。TinyMS使用Flask这个轻量化的网页服务器架构作为C/S通讯的基础架构。为了能够对模型进行推理,用户必须首先启动服务器。如果成功启动,服务器会在子进程中运行并且会监听从地址127.0.0.1,端口号5000发送来的POST请求并且使用MindSpore作为后端来处理这些请求。后端会构建模型,运行推理并且返回结果给客户端。

5.2 启动服务器

运行下列代码以启动服务器:

[ ]:
server = Server(serving_path="/home/jovyan/tinyms/serving/")
server.start_server()
 * Serving Flask app 'tinyms.serving.server.server' (lazy loading)

 * Environment: production

   WARNING: This is a development server. Do not use it in a production deployment.

   Use a production WSGI server instead.

 * Debug mode: off

出现如上提示说明服务器已启动且正在运行。

服务器启动后,我们要到菜单栏点击File=>new Notebook创建一个新的jupyter的文件,然后继续完成客户端的操作。

[ ]:
import os
from PIL import Image

from tinyms.vision import ImageViewer
from tinyms.serving import Server, Client

6. 推理

6.1 上传图片

用户需要上传一张0~9之间的数字图片作为输入。如果使用命令行终端,可以使用scp或者wget命令获取图片,如果使用Jupyter,点击菜单右上方的Upload按钮并且选择上传的图片。本教程中使用的图片可以点击这里进行下载,将图片保存在/home/jovyan文件夹下,重命名为’7.png’(或者任何用户喜欢的名字)。

或者运行如下的代码下载图片:

[7]:
if not os.path.exists('/home/jovyan/7.png'):
    !wget -P /home/jovyan https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/numbers/7.png
else:
    print('7.png already exists')
--2022-01-26 18:42:14--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/numbers/7.png
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.113, 49.4.112.90, 121.36.121.44
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 34970 (34K) [image/png]
Saving to: ‘/home/jovyan/7.png’

7.png               100%[===================>]  34.15K  --.-KB/s    in 0.07s

2022-01-26 18:42:14 (470 KB/s) - ‘/home/jovyan/7.png’ saved [34970/34970]

6.2 List servables

使用list_servables函数检查当前后端的serving模型。

[8]:
client=Client()
client.list_servables()
[8]:
[{'description': 'This servable hosts a lenet5 model predicting numbers',
  'model': {'class_num': 10, 'format': 'ckpt', 'name': 'lenet5'},
  'name': 'lenet5'}]

如果输出的description字段显示这是一个lenet5的模型,则可以继续到下一步发送推理请求。

6.3 发送推理请求

运行predict函数发送推理请求,第4个参数选择TOP1_CLASS或者TOP5_CLASS输出:

[9]:
# 设置图片路径和输出策略(可以在TOP1和TOP5中选择)
image_path = "/home/jovyan/7.png"
strategy = "TOP1_CLASS"

# predict(image_path, servable_name, dataset='mnist', strategy='TOP1_CLASS')
# predict方法的四个参数分别是图片路径、servable名称,数据集名称(默认MNIST)和输出策略(默认输出TOP1,可以选择TOP5)
img_viewer = ImageViewer(Image.open(image_path), image_path)
img_viewer.show()
print(client.predict(image_path,'lenet5', 'mnist', strategy))
_images/quickstart_quickstart_in_one_minute_21_0.png
TOP1: 7, score: 0.9997

如果用户能看到类似如下输出:

TOP1: 7, score: 0.99934917688369750977

那么意味着已经进行了一次成功的推理。

关闭服务器

运行以下代码关闭服务器:

[10]:
server.shutdown()
[10]:
'Server shutting down...'

TinyMS推理可视化体验

结合OpenCV图像视觉库,TinyMS v0.3.1聚焦可视化特性。通过简单直观的图片可视化,帮助用户更快地了解模型推理的效果。

针对不想编写代码的用户,TinyMS提供了WEB UI可视化界面,用户只需在浏览器页面上传待推理的图片即可轻松体验,当前提供了LeNet5CycleGanSSD300模型的支持。

WEB UI推理可视化

用户需要先部署可视化服务器,详情请看Nginx版本的TinyMS安装。服务器部署成功后,访问浏览器呈现的首页和推理效果页(以CycleGan模型为例)如下:

_images/tinyms_web_index.jpg首页

_images/tinyms_web_reasoning.jpg推理页

针对想运行代码的用户,TinyMS提供了模型推理可视化模块,仅需5步骤代码即可快速体验,当前仅提供SSD300对象检测模型的支持。

模型推理可视化模块应用

如果您需要第一时间体验模型推理可视化模块应用,可下载TinyMS官方仓项目代码,执行如下操作:

  • 静态图像检测

  1. 环境准备

    • 有可视化桌面的操作系统(比如Window_x64或者Ubuntu18.04

  2. 验证模块应用

    # 下载tinyms项目
    git clone https://github.com/tinyms-ai/tinyms.git
    cd tinyms/tests/st/app/object_detection/
    # 运行静态图像检测
    python opencv_image_app.py
    

    待检测图片和执行推理后的图片展示如下:

    _images/tinyms_visulization_origin.jpg待检测图片

    _images/tinyms_visulization_reasoning.jpg推理效果图

  • 摄像头采集的动态视频图像检测

  1. 环境准备:

    • 带有可视化桌面的操作系统(Window_x64或者Ubuntu18.04

    • 确保操作系统可以正常访问摄像头

      注:

      对于在主机(比如Window_x64Ubuntu 18.04)下的操作系统,一般来说都可以正常访问摄像头;而对于在虚拟机下的操作系统,请确保开启了相关的虚拟机服务以及连接了摄像头驱动。我们以在window下的虚拟机VMware Workstation为例:

      1. 首先我们在terminal(您的测试系统的terminal,比如挂在VM的Ubuntu 18.04系统下的terminal)下输入命令ls /dev/v*查看是否有/dev/video0驱动程序,若有,则说明是正常的,请忽略以下操作,若没有,则我们执行以下操作。

      2. 开启相关的虚拟机服务,在windows主机中开启服务VMware USB Arbitration Servcie,即键盘Win+R输入services.msc找到相应服务并开启。开启之后需将虚拟机重启。

      3. 连接摄像头驱动,在VMware Workstation的菜单栏上点击虚拟机(M)=>可移动设备=>摄像头名称=>主机连接,并且在虚拟机(M)=>设置(S)=>USB中,选择USB3.0

      4. 你可以通过cheese测试摄像头是不是可以正常访问。

  2. 验证模块应用

    对于不同操作系统的选型以及不同测试方式的选择,我们提供以下具体环境的验证,请您在对应环境进行验证,注意以下环境均已满足环境准备的俩个条件,这很重要

    • 环境一:Windows

      • 若您采用主机测试

        环境需求:

        • 操作系统:Window_64

        • 环境依赖: Git + Python 3. 7.5 + TinyMS 0.3.1 + Microsoft Visual C++ 14.0 or greater

        • 命令行工具:Git Bash

          注:有关vc++ 14.0 的环境依赖详情请参考Pypi安装TinyMS下的注释

        满足环境需求后执行以下命令:

        # 1.在容器内下载tinyms项目
        git clone https://github.com/tinyms-ai/tinyms.git
        cd tinyms/tests/st/app/object_detection/
        # 2.运行摄像头采集的动态视频图像检测
        python opencv_camera_app.py
        
      • 若您采用虚拟机测试

        我们以Window_x64下的虚拟机VMware Workstation为例,VM连接摄像头请参考环境准备中的注释:

        环境需求:

        • 操作系统:Ubuntu18.04 LTS Desktop

        • 环境依赖: Docker 18.06.1-ce

        • 命令行工具:Terminal

        满足环境需求后执行以下命令:

        # 1.在宿主机内安装xserver,并设置权限
        apt install x11-xserver-utils
        # 2.允许所有用户访问显示接口
        xhost +
        # 3.运行容器
        docker run -it --rm --device=/dev/video0 -e DISPLAY=unix$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix tinyms/tinyms:0.3.1 /bin/bash
        # 4.在容器内下载tinyms项目
        git clone https://github.com/tinyms-ai/tinyms.git
        cd tinyms/tests/st/app/object_detection/
        # 5.运行摄像头采集的动态视频图像检测
        python opencv_camera_app.py
        
    • 环境二:Ubuntu

      • 若您采用主机测试:

        环境需求:

        • 操作系统:Ubuntu 18.04 LTS Desktop

        • 环境依赖:Git + Python 3.7.5 + TinyMS 0.3.1

        • 命令行工具:Terminal

        满足环境需求后执行以下命令

        # 1.在容器内下载tinyms项目
        git clone https://github.com/tinyms-ai/tinyms.git
        cd tinyms/tests/st/app/object_detection/
        # 2.运行摄像头采集的动态视频图像检测
        python opencv_camera_app.py
        
      • 若您采用docker访问

        环境需求:

        • 操作系统:Ubuntu 18.04 LTS Desktop

        • 环境依赖: Docker 18.06.1-ce

        • 命令行工具:Terminal

        满足环境需求后执行以下命令:

        # 1.在宿主机内安装xserver,并设置权限
        apt install x11-xserver-utils
        # 2.允许所有用户访问显示接口
        xhost +
        # 3.运行容器
        docker run -it --rm --device=/dev/video0 -e DISPLAY=unix$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix tinyms/tinyms:0.3.1 /bin/bash
        # 4.在容器内下载tinyms项目
        git clone https://github.com/tinyms-ai/tinyms.git
        cd tinyms/tests/st/app/object_detection/
        # 5.运行摄像头采集的动态视频图像检测
        python opencv_camera_app.py
        

    目前文档还在完善中:smile:,如果您的环境不在以上参考中,在您尝试过后,依然有问题,欢迎您在我们的社区,提出您的Issues和Pull requests,我们会及时回复您。

TinyMS ResNet50 教程

在本教程中,我们会演示使用TinyMS API进行训练/推理一个ResNet50模型过程。

环境要求

  • Ubuntu: 18.04

  • Python: 3.7.x

  • Flask: 1.1.2

  • MindSpore: CPU-1.1.1

  • TinyMS: 0.1.0

  • numpy: 1.17.5

  • Pillow: 8.1.0

  • pip: 21.0.1

  • requests: 2.18.4

介绍

TinyMS是一个高级API,目的是让新手用户能够更加轻松地上手深度学习。TinyMS可以有效地减少用户在构建、训练、验证和推理一个模型过程中的操作次数。TinyMS也提供了教程和文档帮助开发者更好的上手和开发。

本教程中,包含6个步骤:构建模型下载数据集训练模型定义servable.json启动服务器推理,其中服务器在子进程中启动。

[1]:
import os
import json

from PIL import Image
from tinyms import context
from tinyms.serving import start_server, predict, list_servables, shutdown, server_started
from tinyms.data import Cifar10Dataset, download_dataset, ImageFolderDataset
from tinyms.vision import cifar10_transform, ImageViewer, imagefolder_transform
from tinyms.model import Model, resnet50
from tinyms.callbacks import ModelCheckpoint, CheckpointConfig, LossMonitor
from tinyms.metrics import Accuracy
from tinyms.optimizers import Momentum
from tinyms.losses import SoftmaxCrossEntropyWithLogits
[WARNING] ME(12653:139827079800640,MainProcess):2021-03-19-15:26:20.878.475 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.

1. 构建模型

TinyMS封装了MindSpore ResNet50模型中的init和construct函数,代码行数能够大大减少,原有的大量代码段行数会被极限压缩:

[2]:
# 构建网络
net = resnet50(class_num=10)
model = Model(net)

2. 下载数据集

本教程演示了使用cifar10数据集进行训练,同时也提供两个预训练好的ckpt文件下载,一个是cifar10数据集训练得来,另一个是由ImageNet2012数据集中的蘑菇图片训练得来.

[3]:
# download the cifar10 dataset
cifar10_path = '/root/cifar10/cifar-10-batches-bin'
if not os.path.exists(cifar10_path):
    download_dataset('cifar10', '/root')
    print('************Download complete*************')
else:
    print('************Dataset already exists.**************')
************** Downloading the Cifar10 dataset **************
[███████████████████████████████████████████████████████████████████████████████████████████████████ ] 99.66%************Download complete*************

3. 训练模型

数据集中的训练集、验证集都会在此步骤中定义,同时也会定义训练参数。训练后生成的ckpt文件会保存到/etc/tinyms/serving/resnet50_cifar10文件夹以便后续使用,训练完成后会进行验证并输出 Accuracy指标。

提示:训练过程非常漫长,建议跳过训练步骤并直接下载、使用本教程提供的ckpt文件进行后续的推理
[ ]:
# 检查ckpt文件和路径
cifar10_ckpt_folder = '/etc/tinyms/serving/resnet50_cifar10'
cifar10_ckpt_path = '/etc/tinyms/serving/resnet50_cifar10/resnet50.ckpt'
if not os.path.exists(cifar10_ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/resnet50_cifar10
else:
    print('resnet50_cifar10 ckpt folder already exists')

# 设置训练参数
epoch_size = 90 # default is 90
batch_size = 32

# 设置环境参数
dataset_sink_mode = False
device_target = "CPU"
context.set_context(mode=context.GRAPH_MODE, device_target=device_target)

# 设置数据集参数
train_dataset = Cifar10Dataset(cifar10_path, num_parallel_workers=4, shuffle=True)
train_dataset = cifar10_transform.apply_ds(train_dataset, repeat_size=1, batch_size=32, is_training=True)
eval_dataset = Cifar10Dataset(cifar10_path, num_parallel_workers=4, shuffle=True)
eval_dataset = cifar10_transform.apply_ds(eval_dataset, repeat_size=1, batch_size=32, is_training=False)
step_size = train_dataset.get_dataset_size()

save_checkpoint_epochs = 5
ckpoint_cb = ModelCheckpoint(prefix="resnet_cifar10", config=CheckpointConfig(
            save_checkpoint_steps=save_checkpoint_epochs * train_dataset.get_dataset_size(),
            keep_checkpoint_max=10))

# 定义loss函数
net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")

# 定义optimizer
net_opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
model.compile(loss_fn=net_loss, optimizer=net_opt, metrics={"Accuracy": Accuracy()})


print('************************Start training*************************')
model.train(epoch_size, train_dataset, callbacks=[ckpoint_cb, LossMonitor()],dataset_sink_mode=dataset_sink_mode)
model.save_checkpoint(cifar10_ckpt_path)
print('************************Finished training*************************')

model.load_checkpoint(cifar10_ckpt_path)
print('************************Start evaluation*************************')
acc = model.eval(eval_dataset, dataset_sink_mode=dataset_sink_mode)
print("============== Accuracy:{} ==============".format(acc))
提示:如果跳过了训练步骤,下载预训练的ckpt文件并继续推理步骤

点击resnet_imagenet下载ResNet50_imagenet ckpt文件,或者点击resnet_cifar下载ResNet50_cifar ckpt文件,将ckpt文件保存到/etc/tinyms/serving/resnet50_<dataset_name>/resnet50.ckpt

或者运行以下代码下载resnet_imagenetresnet_cifar ckpt文件:

[4]:
# check lenet folder exists or not, and download resnet50_imagenet
imagenet2012_ckpt_folder = '/etc/tinyms/serving/resnet50_imagenet2012'
imagenet2012_ckpt_path = '/etc/tinyms/serving/resnet50_imagenet2012/resnet50.ckpt'
if not os.path.exists(imagenet2012_ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/resnet50_imagenet2012
    !wget -P /etc/tinyms/serving/resnet50_imagenet2012 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/imagenet2012/resnet50.ckpt
else:
    print('imagenet2012 ckpt folder already exists')
    if not os.path.exists(imagenet2012_ckpt_path):
        !wget -P /etc/tinyms/serving/resnet50_imagenet2012 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/imagenet2012/resnet50.ckpt
    else:
        print('imagenet2012 ckpt file already exists')


# check lenet folder exists or not
cifar10_ckpt_folder = '/etc/tinyms/serving/resnet50_cifar10'
cifar10_ckpt_path = '/etc/tinyms/serving/resnet50_cifar10/resnet50.ckpt'
if not os.path.exists(cifar10_ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/resnet50_cifar10
    !wget -P /etc/tinyms/serving/resnet50_cifar10 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cifar10/resnet50.ckpt
else:
    print('cifar10 ckpt folder already exists')
    if not os.path.exists(cifar10_ckpt_path):
        !wget -P /etc/tinyms/serving/resnet50_cifar10 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cifar10/resnet50.ckpt
    else:
        print('cifar10 ckpt file already exists')
imagenet2012 ckpt folder already exists
--2021-03-19 15:28:24--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/imagenet2012/resnet50.ckpt
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.113, 49.4.112.90, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 188521005 (180M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/resnet50_imagenet2012/resnet50.ckpt’

resnet50.ckpt       100%[===================>] 179.79M  32.4MB/s    in 6.0s

2021-03-19 15:28:31 (30.0 MB/s) - ‘/etc/tinyms/serving/resnet50_imagenet2012/resnet50.ckpt’ saved [188521005/188521005]

cifar10 ckpt folder already exists
--2021-03-19 15:28:31--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cifar10/resnet50.ckpt
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.113, 49.4.112.90, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 188462121 (180M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/resnet50_cifar10/resnet50.ckpt’

resnet50.ckpt       100%[===================>] 179.73M  4.07MB/s    in 40s

2021-03-19 15:29:11 (4.52 MB/s) - ‘/etc/tinyms/serving/resnet50_cifar10/resnet50.ckpt’ saved [188462121/188462121]

4. 定义servable.json

在下列两段代码中选择其中一段运行以定义servable json文件,该文件会在后续推理中使用 运行下列代码定义ResNet50_imagenet2012模型servable json文件:

[ ]:
servable_json = [{'name': 'resnet50_imagenet2012',
                  'description': 'This servable hosts a resnet50 model predicting mushrooms',
                  'model': {
                      "name": "resnet50",
                      "format": "ckpt",
                      "class_num": 9}}]
os.chdir("/etc/tinyms/serving")
json_data = json.dumps(servable_json, indent=4)

with open('servable.json', 'w') as json_file:
    json_file.write(json_data)

运行下列代码定义ResNet50_cifar10模型servable json文件:

[5]:
servable_json = [{'name': 'resnet50_cifar10',
                  'description': 'This servable hosts a resnet50 model predicting 10 classes of objects',
                  'model': {
                      "name": "resnet50",
                      "format": "ckpt",
                      "class_num": 10}}]
os.chdir("/etc/tinyms/serving")
json_data = json.dumps(servable_json, indent=4)

with open('servable.json', 'w') as json_file:
    json_file.write(json_data)

5. 启动服务器

5.1 介绍

TinyMS推理是C/S(Client/Server)架构。TinyMS使用Flask这个轻量化的网页服务器架构作为C/S通讯的基础架构。为了能够对模型进行推理,用户必须首先启动服务器。如果成功启动,服务器会在子进程中运行并且会监听从地址127.0.0.1,端口号5000发送来的POST请求并且使用MindSpore作为后端来处理这些请求。后端会构建模型,运行推理并且返回结果给客户端

5.2 启动服务器

运行下列代码以启动服务器:

[6]:
start_server()
Server starts at host 127.0.0.1, port 5000

6. 推理

6.1 上传图片

ResNet50_imagenet2012模型需要用户上传一张蘑菇图片作为输入,而ResNet50_cifar10模型需要用户上传一张属于如下10个类别的图片作为输入:

['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

点击蘑菇下载本教程中使用的蘑菇图片以运行ResNet50_imagenet2012,或者点击飞机以运行ResNet50_cifar10。上传图片,如果使用命令行终端,可以使用’scp’或者’wget’获取图片,如果使用Jupyter,点击菜单右上方的’Upload’按钮并且选择上传的图片。将图片保存在根目录下,重命名为’mushroom.jpeg’或者’airplane.jpg’。

或者运行下列代码下载蘑菇图片(推理ResNet_imagenet模型)和飞机(推理ResNet_cifar模型)图片:

[7]:
# 下载蘑菇图片
if not os.path.exists('/root/mushroom.jpeg'):
    !wget -P /root/ https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/mushrooms/mushroom.jpeg
else:
    print('mushroom.jpeg already exists')

# 下载飞机图片
if not os.path.exists('/root/airplane.jpg'):
    !wget -P /root/ https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/objects/airplane.jpg
else:
    print('airplane.jpg already exists')
--2021-03-19 15:29:18--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/mushrooms/mushroom.jpeg
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.113, 49.4.112.90, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 76020 (74K) [image/jpeg]
Saving to: ‘/root/mushroom.jpeg’

mushroom.jpeg       100%[===================>]  74.24K  --.-KB/s    in 0.1s

2021-03-19 15:29:18 (563 KB/s) - ‘/root/mushroom.jpeg’ saved [76020/76020]

--2021-03-19 15:29:18--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/objects/airplane.jpg
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.113, 49.4.112.90, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 151188 (148K) [image/jpeg]
Saving to: ‘/root/airplane.jpg’

airplane.jpg        100%[===================>] 147.64K   568KB/s    in 0.3s

2021-03-19 15:29:19 (568 KB/s) - ‘/root/airplane.jpg’ saved [151188/151188]

6.2 List servables

使用list_servables函数检查当前后端的serving模型

[8]:
list_servables()
[8]:
[{'description': 'This servable hosts a resnet50 model predicting 10 classes of objects',
  'model': {'class_num': 10, 'format': 'ckpt', 'name': 'resnet50'},
  'name': 'resnet50_cifar10'}]

如果输出的description字段显示这是一个resnet50的模型,则可以继续到下一步发送推理请求,后续的代码会自动判断后端模型并运行推理代码

6.3 发送推理请求

运行predict函数发送推理请求,第4个参数选择TOP1_CLASSTOP5_CLASS以指定输出策略:

[9]:
#设置两张图片的路径(对应两个不同的数据集)和输出策略(可以在TOP1和TOP5中选择)
imagenet_image_path = "/root/mushroom.jpeg"
cifar_image_path = "/root/airplane.jpg"
strategy = "TOP1_CLASS"

# predict(image_path, servable_name, dataset, topk_strategy)
# predict方法的4个参数分别是图片路径、servable名称,数据集名称(默认MNIST,此处需手动指定)和输出策略
if server_started() is True:
    servable_name = list_servables()[0]['name']
    if servable_name == 'resnet50_imagenet2012':
        img_viewer = ImageViewer(Image.open(imagenet_image_path), imagenet_image_path)
        img_viewer.show()
        print(predict(imagenet_image_path, "resnet50_imagenet2012", "imagenet2012", strategy))
    else:
        img_viewer = ImageViewer(Image.open(cifar_image_path), cifar_image_path)
        img_viewer.show()
        print(predict(cifar_image_path, "resnet50_cifar10", 'cifar10', strategy))
else:
    print('Server not started')
_images/tutorials_ipynb_TinyMS_ResNet50_tutorial_zh_22_0.png
TOP1: airplane, score: 0.99997282028198242188

检查输出

如果用户运行ResNet50_imagenet2012且能看到类似如下输出:

TOP1: Amanita毒蝇伞,伞菌目,鹅膏菌科,鹅膏菌属,主要分布于我国黑龙江、吉林、四川、西藏、云南等地,有毒, score: 0.99119007587432861328

那么意味着已经进行了一次成功的推理

如果用户运行ResNet50_cifar10,输出应该类似于:

TOP1: airplane, score: 0.99997282028198242188

切换模型

如果想要体验使用ImageNet2012毒蘑菇数据集训练,可以运行下列代码,同时运行对应的servable_json代码段:

[ ]:
# download the imagenet2012 mushroom dataset
imagenet_path = '/root/mushrooms'
if not os.path.exists(imagenet_path):
    !wget -P /root/ https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/resnet-50/mushrooms/mushrooms.zip
    !mkdir /root/mushrooms/
    !unzip /root/mushrooms.zip -d /root/mushrooms/
    print('************Download complete*************')
else:
    print('************Dataset already exists.**************')


# check ckpt folder exists or not
imagenet_ckpt_folder = '/etc/tinyms/serving/resnet50_imagenet2012'
imagenet_ckpt_path = '/etc/tinyms/serving/resnet50_imagenet2012/resnet50.ckpt'
if not os.path.exists(imagenet_ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/resnet50_imagenet2012
else:
    print('resnet50_imagenet2012 ckpt folder already exists')


epoch_size = 90 # default is 90
batch_size = 32

# set environment parameters
dataset_sink_mode = False
device_target = "CPU"
context.set_context(mode=context.GRAPH_MODE, device_target=device_target)

# set dataset parameters
imagenet_train_path = '/root/mushrooms/train'
train_dataset = ImageFolderDataset(imagenet_train_path, num_parallel_workers=4, shuffle=True)
train_dataset = imagefolder_transform.apply_ds(train_dataset, repeat_size=1, batch_size=32, is_training=True)
imagenet_eval_path = '/root/mushrooms/eval'
eval_dataset = ImageFolderDataset(imagenet_eval_path, num_parallel_workers=4, shuffle=True)
eval_dataset = imagefolder_transform.apply_ds(eval_dataset, repeat_size=1, batch_size=32, is_training=False)
step_size = train_dataset.get_dataset_size()

save_checkpoint_epochs = 5
ckpoint_cb = ModelCheckpoint(prefix="resnet_imagenet2012", config=CheckpointConfig(
            save_checkpoint_steps=save_checkpoint_epochs * train_dataset.get_dataset_size(),
            keep_checkpoint_max=10))

# define the loss function
net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")

# define the optimizer
net_opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
model.compile(loss_fn=net_loss, optimizer=net_opt, metrics={"Accuracy": Accuracy()})


print('************************Start training*************************')
model.train(epoch_size, train_dataset, callbacks=[ckpoint_cb, LossMonitor()],dataset_sink_mode=dataset_sink_mode)
model.save_checkpoint(imagenet_ckpt_path)
print('************************Finished training*************************')

model.load_checkpoint(imagenet_ckpt_path)
print('************************Start evaluation*************************')
acc = model.eval(eval_dataset, dataset_sink_mode=dataset_sink_mode)
print("============== Accuracy:{} ==============".format(acc))

关闭服务器

[10]:
shutdown()
[10]:
'Server shutting down...'

TinyMS MobileNetV2 教程

在本教程中,我们会演示使用TinyMS API进行训练/推理一个MobileNetV2模型过程。

环境要求

  • Ubuntu: 18.04

  • Python: 3.7.x

  • Flask: 1.1.2

  • MindSpore: CPU-1.1.1

  • TinyMS: 0.1.0

  • numpy: 1.17.5

  • Pillow: 8.1.0

  • pip: 21.0.1

  • requests: 2.18.4

介绍

TinyMS是一个高级API,目的是让新手用户能够更加轻松地上手深度学习。TinyMS可以有效地减少用户在构建、训练、验证和推理一个模型过程中的操作次数。TinyMS也提供了教程和文档帮助开发者更好的上手和开发。

本教程中,包含6个步骤:构建模型下载数据集训练模型定义servable.json启动服务器推理,其中服务器在子进程中启动。

[1]:
import os
import json

from PIL import Image
from tinyms import context
from tinyms.serving import start_server, predict, list_servables, shutdown, server_started
from tinyms.model import Model, mobilenetv2
from tinyms.data import Cifar10Dataset, download_dataset
from tinyms.vision import cifar10_transform, ImageViewer
from tinyms.metrics import Accuracy
from tinyms.optimizers import Momentum
from tinyms.losses import CrossEntropyWithLabelSmooth
from tinyms.utils.train.loss_manager import FixedLossScaleManager
from tinyms.utils.train.lr_generator import mobilenetv2_lr
from tinyms.utils.train.cb_config import mobilenetv2_cb
[WARNING] ME(10671:140590521530176,MainProcess):2021-03-19-15:08:41.425.705 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.

1. 构建模型

TinyMS封装了MindSpore MobileNetV2模型中的init和construct函数,代码行数能够大大减少,原有的大量代码段行数会被极限压缩:

[2]:
# 构建模型
net = mobilenetv2(class_num=10, is_training=True)
model = Model(net)

2. 下载数据集

如果根目录下没有创建cifar10文件夹则cifar10数据集会被自动下载并存放到根目录,如果cifar10文件夹已经存在于根目录 ,则此步操作会被跳过

[3]:
# download the dataset
cifar10_path = '/root/cifar10/cifar-10-batches-bin'
if not os.path.exists(cifar10_path):
    download_dataset('cifar10', '/root')
    print('************Download complete*************')
else:
    print('************Dataset already exists.**************')
************** Downloading the Cifar10 dataset **************
[██████████████████████████████████████████████████████████████████████████████████████████████████  ] 98.12%************Download complete*************

3. 训练模型

数据集中的训练集、验证集都会在此步骤中定义,同时也会定义训练参数。训练后生成的ckpt文件会保存到/etc/tinyms/serving/mobilenetv2文件夹以便后续使用,训练完成后会进行验证并输出 Accuracy指标。

提示:训练过程非常漫长,建议跳过训练步骤并直接下载、使用本教程提供的ckpt文件进行后续的推理
[ ]:
# 检查mobilenetv2路径
ckpt_folder = '/etc/tinyms/serving/mobilenetv2'
ckpt_path = '/etc/tinyms/serving/mobilenetv2/mobilenetv2.ckpt'
if not os.path.exists(ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/mobilenetv2
else:
    print('mobilenetv2 ckpt folder already exists')

# 设置参数
epoch_size = 60 # default is 60
batch_size = 32
class_num = 10

# 设置环境参数
device_target="CPU"
dataset_sink_mode = False
context.set_context(mode=context.GRAPH_MODE, device_target=device_target)

# 创建数据集
train_dataset = Cifar10Dataset(cifar10_path, num_parallel_workers=4, shuffle=True)
train_dataset = cifar10_transform.apply_ds(train_dataset, repeat_size=1, batch_size=32, is_training=True)
eval_dataset = Cifar10Dataset(cifar10_path, num_parallel_workers=4, shuffle=True)
eval_dataset = cifar10_transform.apply_ds(eval_dataset, repeat_size=1, batch_size=32, is_training=False)
step_size = train_dataset.get_dataset_size()

# 定义loss函数
label_smooth = 0.1
loss = CrossEntropyWithLabelSmooth(smooth_factor=label_smooth,num_classes=class_num)

# 定义学习率
lr_max = 0.001
lr_init_scale = 0.01
lr_end_scale = 0.01
lr = mobilenetv2_lr(global_step=0,
                    lr_init=lr_max*lr_init_scale,
                    lr_end=lr_max*lr_end_scale,
                    lr_max=lr_max,
                    warmup_epochs=2,
                    total_epochs=epoch_size,
                    steps_per_epoch=step_size)

# 定义optimizer
loss_scale = FixedLossScaleManager(1024, drop_overflow_update=False)
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),lr, 0.9, 4e-5, 1024)
model.compile(loss_fn=loss, optimizer=opt, metrics={"Accuracy": Accuracy()},loss_scale_manager=loss_scale)

# configure checkpoint to save weights and do training job
save_checkpoint_epochs = 10
ckpoint_cb = mobilenetv2_cb(device_target=device_target,
                            lr=lr,
                            is_saving_checkpoint=True,
                            save_checkpoint_epochs=save_checkpoint_epochs,
                            step_size=step_size)


print('************************Start training*************************')
model.train(epoch_size, train_dataset, callbacks=ckpoint_cb, dataset_sink_mode=dataset_sink_mode)
model.save_checkpoint(ckpt_path)
print('************************Finished training*************************')

model.load_checkpoint(ckpt_path)
print('************************Start evaluation*************************')
acc = model.eval(eval_dataset, dataset_sink_mode=dataset_sink_mode)
print("============== Accuracy:{} ==============".format(acc))
提示:如果跳过了训练步骤,下载预训练的ckpt文件并继续推理步骤

点击这里进行下载,并将ckpt文件保存到/etc/tinyms/serving/mobilenetv2/mobilenetv2.ckpt

或者运行以下代码下载 mobilenetv2 ckpt文件:

[4]:
mobilenetv2_ckpt_folder = '/etc/tinyms/serving/mobilenetv2'
mobilenetv2_ckpt_path = '/etc/tinyms/serving/mobilenetv2/mobilenetv2.ckpt'

if not os.path.exists(mobilenetv2_ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/mobilenetv2
    !wget -P /etc/tinyms/serving/mobilenetv2 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cifar10/mobilenetv2.ckpt
else:
    print('mobilenetv2 ckpt folder already exists')
    if not os.path.exists(mobilenetv2_ckpt_path):
        !wget -P /etc/tinyms/serving/mobilenetv2 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cifar10/mobilenetv2.ckpt
    else:
        print('mobilenetv2 ckpt file already exists')
mobilenetv2 ckpt folder already exists
--2021-03-19 15:10:50--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cifar10/mobilenetv2.ckpt
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.90, 49.4.112.113, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.90|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 18509001 (18M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/mobilenetv2/mobilenetv2.ckpt’

mobilenetv2.ckpt    100%[===================>]  17.65M  16.3MB/s    in 1.1s

2021-03-19 15:10:51 (16.3 MB/s) - ‘/etc/tinyms/serving/mobilenetv2/mobilenetv2.ckpt’ saved [18509001/18509001]

4. 定义servable.json

运行下列代码定义servable json文件:

[5]:
servable_json = [{'name': 'mobilenetv2',
                  'description': 'This servable hosts a mobilenetv2 model predicting 10 classes of objects',
                  'model': {
                      "name": "mobilenetv2",
                      "format": "ckpt",
                      "class_num": 10}}]
os.chdir("/etc/tinyms/serving")
json_data = json.dumps(servable_json, indent=4)

with open('servable.json', 'w') as json_file:
    json_file.write(json_data)

5. 启动服务器

5.1 介绍

TinyMS推理是C/S(Client/Server)架构。TinyMS使用Flask这个轻量化的网页服务器架构作为C/S通讯的基础架构。为了能够对模型进行推理,用户必须首先启动服务器。如果成功启动,服务器会在子进程中运行并且会监听从地址127.0.0.1,端口号5000发送来的POST请求并且使用MindSpore作为后端来处理这些请求。后端会构建模型,运行推理并且返回结果给客户端

5.2 启动服务器

运行下列代码以启动服务器:

[6]:
start_server()
Server starts at host 127.0.0.1, port 5000

6. 推理

6.1 上传图片

用户需要上传一张图片作为输入,图片中要求含有以下类别的物体以供识别:

['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

点击这里下载本教程中使用的图片。上传图片,如果使用命令行终端,可以使用’scp’或者’wget’获取图片,如果使用Jupyter,点击菜单右上方的’Upload’按钮并且选择上传的图片。将图片保存在根目录下,重命名为’airplane.jpg’(或其他自定义名字)。

或者运行下列代码下载本教程使用的图片:

[7]:
# 下载飞机的图片
if not os.path.exists('/root/airplane.jpg'):
    !wget -P /root/ https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/objects/airplane.jpg
else:
    print('airplane.jpg already exists')
--2021-03-19 15:10:59--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/objects/airplane.jpg
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.90, 49.4.112.113, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.90|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 151188 (148K) [image/jpeg]
Saving to: ‘/root/airplane.jpg’

airplane.jpg        100%[===================>] 147.64K   706KB/s    in 0.2s

2021-03-19 15:11:00 (706 KB/s) - ‘/root/airplane.jpg’ saved [151188/151188]

6.2 List servables

使用list_servables函数检查当前后端的serving模型

[8]:
list_servables()
[8]:
[{'description': 'This servable hosts a mobilenetv2 model predicting 10 classes of objects',
  'model': {'class_num': 10, 'format': 'ckpt', 'name': 'mobilenetv2'},
  'name': 'mobilenetv2'}]

如果输出的description字段显示这是一个mobilenetv2的模型,则可以继续到下一步发送推理请求

6.3 发送推理请求

运行predict函数发送推理请求,目前支持TOP1_CLASSTOP5_CLASS输出策略

[9]:
# 设置图片路径和输出策略(在TOP1_CLASS和TOP5_CLASS中选择一个)
image_path = "/root/airplane.jpg"
strategy = "TOP1_CLASS"

# predict(image_path, servable_name, dataset_name, strategy)
if server_started() is True:
    img_viewer = ImageViewer(Image.open(image_path), image_path)
    img_viewer.show()
    print(predict(image_path, 'mobilenetv2', 'cifar10', strategy))
else:
    print("Server not started")
_images/tutorials_ipynb_TinyMS_MobileNetV2_tutorial_zh_20_0.png
TOP1: airplane, score: 0.22268821299076080322

检查输出

如果看到了类似如下的输出:

TOP1: airplane, score: 0.22268821299076080322

则表明已经成功进行了一次的推理

关闭服务器

[10]:
shutdown()
[10]:
'Server shutting down...'

TinyMS SSD300 教程

在本教程中,我们会演示使用TinyMS API进行训练/推理一个SSD300模型过程。

环境要求

  • Ubuntu: 18.04

  • Python: 3.7.x

  • Flask: 1.1.2

  • MindSpore: CPU-1.1.1

  • TinyMS: 0.1.0

  • numpy: 1.17.5

  • Pillow: 8.1.0

  • pip: 21.0.1

  • requests: 2.18.4

介绍

TinyMS是一个高级API,目的是让新手用户能够更加轻松地上手深度学习。TinyMS可以有效地减少用户在构建、训练、验证和推理一个模型过程中的操作次数。TinyMS也提供了教程和文档帮助开发者更好的上手和开发。

本教程中,包含6个步骤:构建模型下载数据集训练模型定义servable.json启动服务器推理,其中服务器在子进程中启动。

[1]:
import os
import json
import time
import tinyms as ts
import xml.etree.ElementTree as et

from PIL import Image
from tinyms import context, layers, primitives as P, Tensor
from tinyms.serving import start_server, predict, list_servables, shutdown, server_started
from tinyms.data import VOCDataset, download_dataset
from tinyms.vision import voc_transform, coco_eval, ImageViewer
from tinyms.model import Model, ssd300_mobilenetv2, ssd300_infer
from tinyms.losses import net_with_loss
from tinyms.optimizers import Momentum
from tinyms.callbacks import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
from tinyms.utils.train.lr_generator import mobilenetv2_lr as ssd300_lr
from tinyms.initializers import initializer, TruncatedNormal
[WARNING] ME(17758:139967989708608,MainProcess):2021-03-19-15:42:00.473.223 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.

1. 构建模型

[2]:
# 构建网络
net = ssd300_mobilenetv2(class_num=21)

2. 下载数据集

VOC数据集会被自动下载并存放到根目录,如果voc文件夹已经存在于根目录 ,则此步操作会被跳过

[3]:
# download the dataset
voc_path = '/root/voc'

if not os.path.exists(voc_path):
    download_dataset('voc', '/root')
    print('************Download complete*************')
else:
    print('************Dataset already exists.**************')
************** Downloading the VOC2007 dataset **************
[███████████████████████████████████████████████████████████████████████████████████████████████████ ] 100.00%************Download complete*************

3. 训练模型

数据集中的训练集、验证集都会在此步骤中定义,同时也会定义训练参数。训练后生成的ckpt文件会保存到/etc/tinyms/serving/ssd300文件夹以便后续使用,训练完成后会进行验证并输出 Accuracy指标。

提示:训练过程非常漫长,建议跳过训练步骤并直接下载、使用本教程提供的ckpt文件进行后续的推理
[ ]:
class TrainingWrapper(layers.Layer):
    """
    Encapsulation class of SSD300 network training.

    Append an optimizer to the training network after that the construct
    function can be called to create the backward graph.

    Args:
        network (Layer): The training network. Note that loss function should have been added.
        optimizer (Optimizer): Optimizer for updating the weights.
        sens (float): The adjust parameter. Default: 1.0.
    """

    def __init__(self, network, optimizer, sens=1.0):
        super(TrainingWrapper, self).__init__(auto_prefix=False)
        self.network = network
        self.network.set_grad()
        self.weights = ts.ParameterTuple(network.trainable_params())
        self.optimizer = optimizer
        self.grad = P.GradOperation(get_by_list=True, sens_param=True)
        self.sens = sens
        self.hyper_map = P.HyperMap()

    def construct(self, *args):
        weights = self.weights
        loss = self.network(*args)
        sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
        grads = self.grad(self.network, weights)(*args, sens)
        return P.depend(loss, self.optimizer(grads))

def create_voc_label(voc_dir, voc_cls, usage='val'):
    """Get image path and annotation from VOC."""
    if not os.path.isdir(voc_dir):
        raise ValueError(f'Cannot find {voc_dir} dataset path.')
    anno_dir = voc_dir
    if os.path.isdir(os.path.join(voc_dir, 'Annotations')):
        anno_dir = os.path.join(voc_dir, 'Annotations')

    cls_map = {name: i for i, name in enumerate(voc_cls)}
    # Fetch the specific xml files path
    xml_files = []
    with open(os.path.join(voc_dir, 'ImageSets', 'Main', usage+'.txt'), 'r') as f:
        for line in f:
            xml_files.append(line.strip('\n')+'.xml')

    json_dict = {"images": [], "type": "instances", "annotations": [],
                 "categories": []}
    bnd_id = 1
    for xml_file in xml_files:
        img_id = xml_files.index(xml_file)
        tree = et.parse(os.path.join(anno_dir, xml_file))
        root_node = tree.getroot()
        file_name = root_node.find('filename').text

        for obj in root_node.iter('object'):
            cls_name = obj.find('name').text
            if cls_name not in cls_map:
                print(f'Label "{cls_name}" not in "{cls_map}"')
                continue

            bnd_box = obj.find('bndbox')
            x_min = int(float(bnd_box.find('xmin').text)) - 1
            y_min = int(float(bnd_box.find('ymin').text)) - 1
            x_max = int(float(bnd_box.find('xmax').text)) - 1
            y_max = int(float(bnd_box.find('ymax').text)) - 1
            o_width = abs(x_max - x_min)
            o_height = abs(y_max - y_min)
            ann = {'area': o_width * o_height, 'iscrowd': 0,
                   'image_id': img_id,
                   'bbox': [x_min, y_min, o_width, o_height],
                   'category_id': cls_map[cls_name], 'id': bnd_id,
                   'ignore': 0,
                   'segmentation': []}
            json_dict['annotations'].append(ann)
            bnd_id = bnd_id + 1

        size = root_node.find("size")
        width = int(size.find('width').text)
        height = int(size.find('height').text)
        image = {'file_name': file_name, 'height': height, 'width': width,
                 'id': img_id}
        json_dict['images'].append(image)

    for cls_name, cid in cls_map.items():
        cat = {'supercategory': 'none', 'id': cid, 'name': cls_name}
        json_dict['categories'].append(cat)

    anno_file = os.path.join(anno_dir, 'annotation.json')
    with open(anno_file, 'w') as f:
        json.dump(json_dict, f)
    return anno_file


# 检查ckpt文件路径
ckpt_folder = '/etc/tinyms/serving/ssd300'
ckpt_path = '/etc/tinyms/serving/ssd300/ssd300.ckpt'
if not os.path.exists(ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/ssd300
else:
    print('ssd300 ckpt folder already exists')

# 设置训练参数
epoch_size = 800 # default is 800
batch_size = 32
voc_path = '/root/voc/VOCdevkit/VOC2007'

# 设置环境
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
dataset_sink_mode = False

# 创建数据集
train_dataset = VOCDataset(voc_path, task='Detection', usage='trainval', num_parallel_workers=4, shuffle=True, decode=True)
train_dataset = voc_transform.apply_ds(train_dataset, repeat_size=1, batch_size=batch_size, num_parallel_workers=4, is_training=True)
eval_dataset = VOCDataset(voc_path, task='Detection', usage='val', num_parallel_workers=4, shuffle=True, decode=True)
eval_dataset = voc_transform.apply_ds(eval_dataset, repeat_size=1, batch_size=batch_size, num_parallel_workers=4, is_training=False)
dataset_size = train_dataset.get_dataset_size()
total = eval_dataset.get_dataset_size()

# 定义loss函数
net = net_with_loss(net)
params = net.trainable_params()
for p in params:
        if 'beta' not in p.name and 'gamma' not in p.name and 'bias' not in p.name:
            p.set_data(initializer(TruncatedNormal(0.02), p.data.shape, p.data.dtype))


# 定义optimizer
pre_trained_epoch_size = 0
save_checkpoint_epochs = 10
lr = 0.01
lr = ssd300_lr(global_step=pre_trained_epoch_size * dataset_size,
                lr_init=0.001, lr_end=0.001 * lr, lr_max=lr,
                warmup_epochs=2, total_epochs=epoch_size,
                steps_per_epoch=dataset_size)
loss_scale = 1.0
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr,0.9, 1.5e-4, loss_scale)
model = Model(TrainingWrapper(net, opt, loss_scale))
model.compile()
ckpoint_cb = ModelCheckpoint(prefix="ssd300", config=CheckpointConfig(
    save_checkpoint_steps=save_checkpoint_epochs * dataset_size,
    keep_checkpoint_max=10))

print('************************Start training*************************')
model.train(epoch_size, train_dataset, callbacks=[ckpoint_cb, LossMonitor(), TimeMonitor(data_size=dataset_size)],
            dataset_sink_mode=dataset_sink_mode)
model.save_checkpoint(ckpt_path)
print('************************Finished training*************************')

eval_net = ssd300_infer(class_num=21)
model = Model(eval_net)
model.load_checkpoint(ckpt_path)
# perform the model predict operation
print("\n========================================\n")
print("total images num: ", total)
print("Processing, please wait a moment...")
start = time.time()
pred_data = []
id_iter = 0

for data in eval_dataset.create_dict_iterator(output_numpy=True):
    image_np = data['image']
    image_shape = data['image_shape']

    output = model.predict(Tensor(image_np))
    for batch_idx in range(image_np.shape[0]):
        pred_data.append({"boxes": output[0].asnumpy()[batch_idx],
                          "box_scores": output[1].asnumpy()[batch_idx],
                          "img_id": id_iter,
                          "image_shape": image_shape[batch_idx]})
        id_iter += 1
cost_time = int((time.time() - start) * 1000)
print(f'    100% [{total}/{total}] cost {cost_time} ms')

# calculate mAP for the predict data
voc_cls = ['background',
            'aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
            'bus', 'car', 'cat', 'chair', 'cow',
            'diningtable', 'dog', 'horse', 'motorbike', 'person',
            'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
anno_file = create_voc_label(voc_path, voc_cls)
mAP = coco_eval(pred_data, anno_file)
print("\n========================================\n")
print(f"mAP: {mAP}")
提示:如果跳过了训练步骤,下载预训练的ckpt文件并继续推理步骤

点击这里进行下载,并将ckpt文件保存到/etc/tinyms/serving/ssd300/ssd300.ckpt

或者运行以下代码下载 ssd300 ckpt文件:

[4]:
ssd300_ckpt_folder = '/etc/tinyms/serving/ssd300'
ssd300_ckpt_path = '/etc/tinyms/serving/ssd300/ssd300.ckpt'

# 创建路径,下载并存储ssd300 ckpt
if not os.path.exists(ssd300_ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/ssd300
    !wget -P /etc/tinyms/serving/ssd300 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/voc/ssd300.ckpt
else:
    print('ssd300 ckpt folder already exists')
    if not os.path.exists(ssd300_ckpt_path):
        !wget -P /etc/tinyms/serving/ssd300 https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/voc/ssd300.ckpt
    else:
        print('ssd300 ckpt file already exists')
ssd300 ckpt folder already exists
--2021-03-19 15:47:44--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/voc/ssd300.ckpt
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.113, 49.4.112.90, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28056511 (27M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/ssd300/ssd300.ckpt’

ssd300.ckpt         100%[===================>]  26.76M  19.2MB/s    in 1.4s

2021-03-19 15:47:46 (19.2 MB/s) - ‘/etc/tinyms/serving/ssd300/ssd300.ckpt’ saved [28056511/28056511]

4. 定义servable.json

运行下列代码定义servable json文件:

[5]:
servable_json = [{'name': 'ssd300',
                  'description': 'This servable hosts an ssd300 model predicting bounding boxes',
                  'model': {
                      "name": "ssd300",
                      "format": "ckpt",
                      "class_num": 21}}]
os.chdir("/etc/tinyms/serving")
json_data = json.dumps(servable_json, indent=4)

with open('servable.json', 'w') as json_file:
    json_file.write(json_data)

5. 启动服务器

5.1 介绍

TinyMS推理是C/S(Client/Server)架构。TinyMS使用Flask这个轻量化的网页服务器架构作为C/S通讯的基础架构。为了能够对模型进行推理,用户必须首先启动服务器。如果成功启动,服务器会在子进程中运行并且会监听从地址127.0.0.1,端口号5000发送来的POST请求并且使用MindSpore作为后端来处理这些请求。后端会构建模型,运行推理并且返回结果给客户端

5.2 启动服务器

运行下列代码以启动服务器:

[6]:
start_server()
Server starts at host 127.0.0.1, port 5000

6. 推理

6.1 上传图片

用户需要上传一张图片作为输入,图片中要求含有以下类别的物体以供识别:

['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']

点击这里下载本教程中使用的图片。上传图片,如果使用命令行终端,可以使用’scp’或者’wget’获取图片,如果使用Jupyter,点击菜单右上方的’Upload’按钮并且选择上传的图片。将图片保存在根目录下,重命名为’ssd300_test.jpeg’(或其他自定义名字)。

或者运行下列代码下载本教程使用的图片:

[7]:
# 下载图片
if not os.path.exists('/root/ssd300_test.jpeg'):
    !wget -P /root/ https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/ssd300_test/ssd300_test.jpeg
else:
    print('ssd300_test.jpeg already exists')
--2021-03-19 15:47:51--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/tinyms-test-pics/ssd300_test/ssd300_test.jpeg
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 49.4.112.113, 49.4.112.90, 49.4.112.5, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 70412 (69K) [image/jpeg]
Saving to: ‘/root/ssd300_test.jpeg’

ssd300_test.jpeg    100%[===================>]  68.76K  --.-KB/s    in 0.1s

2021-03-19 15:47:52 (560 KB/s) - ‘/root/ssd300_test.jpeg’ saved [70412/70412]

6.2 List servables

使用list_servables函数检查当前后端的serving模型

[8]:
list_servables()
[8]:
[{'description': 'This servable hosts an ssd300 model predicting bounding boxes',
  'model': {'class_num': 21, 'format': 'ckpt', 'name': 'ssd300'},
  'name': 'ssd300'}]

如果输出的description字段显示这是一个ssd300的模型,则可以继续到下一步发送推理请求

6.3 发送推理请求

运行predict函数发送推理请求,目前仅支持TOP1_CLASS输出策略,再运行ImageViewer.draw绘制边框

[9]:
# 设置图片路径和输出策略(目前仅支持'TOP1_CLASS')
image_path = "/root/ssd300_test.jpeg"
strategy = "TOP1_CLASS"

labels = ['background',
          'aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
          'bus', 'car', 'cat', 'chair', 'cow',
          'diningtable', 'dog', 'horse', 'motorbike', 'person',
          'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']

# predict(image_path, servable_name, dataset_name, strategy)
# ImageViewer(img, title)
# ImageViewer.draw(predict_result, labels)
if server_started() is True:
    res = predict(image_path, 'ssd300', 'voc', strategy)
    img_viewer = ImageViewer(Image.open(image_path))
    img_viewer.draw(res, labels)
else:
    print("Server not started")
_images/tutorials_ipynb_TinyMS_SSD300_tutorial_zh_20_0.png

检查输出

如果输出了包含推测边框的图片,则表示已经进行了一次成功的输出,物体的类别和分数也会在图片中显示

关闭服务器

[10]:
shutdown()
[10]:
'Server shutting down...'

TinyMS CycleGAN 教程

在本教程中,我们会演示使用TinyMS API进行训练/推理一个CycleGAN模型过程。

环境要求

  • Ubuntu: 18.04

  • Python: 3.7.x

  • Flask: 1.1.2

  • MindSpore: CPU-1.1.1

  • TinyMS: 0.1.0

  • numpy: 1.17.5

  • Pillow: 8.1.0

  • pip: 21.0.1

  • requests: 2.18.4

  • matplotlib: 3.3.4

介绍

TinyMS是一个高级API,目的是让新手用户能够更加轻松地上手深度学习。TinyMS可以有效地减少用户在构建、训练、验证和推理一个模型过程中的操作次数。TinyMS也提供了教程和文档帮助开发者更好的上手和开发。

本教程中,包含5个步骤:下载数据集训练模型定义servable.json启动服务器推理,其中服务器在子进程中启动。

[1]:
import os
import argparse
import json
import tinyms as ts
import numpy as np
import matplotlib.pyplot as plt

from PIL import Image
from tinyms import context, Tensor
from tinyms.serving import start_server, predict, list_servables, shutdown, server_started
from tinyms.data import GeneratorDataset, UnalignedDataset, GanImageFolderDataset, DistributedSampler
from tinyms.vision import cyclegan_transform
from tinyms.model.cycle_gan.cycle_gan import get_generator_discriminator, cycle_gan, TrainOneStepG, TrainOneStepD
from tinyms.losses import CycleGANDiscriminatorLoss, CycleGANGeneratorLoss
from tinyms.optimizers import Adam
from tinyms.data.utils import save_image, generate_image_list
from tinyms.utils.common_utils import GanReporter, gan_load_ckpt, GanImagePool
from tinyms.utils.train import cyclegan_lr
from tinyms.utils.eval import CityScapes, fast_hist, get_scores
[WARNING] ME(29932:140184175310656,MainProcess):2021-03-21-15:16:53.948.651 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.

1. 下载数据集

在本教程中,会使用到cityscapes数据集,点击链接前往官方网站申请下载和处理数据集

2. 训练模型

设置参数,启动训练和验证过程

提示:训练过程非常漫长,建议跳过训练步骤并直接下载、使用本教程提供的ckpt文件进行后续的推理
[ ]:
def parse_args():
    parser = argparse.ArgumentParser(description='MindSpore Cycle GAN Example')
    parser.add_argument('--device_target', type=str, default="CPU", choices=['Ascend', 'GPU', 'CPU'],
                        help='device where the code will be implemented (default: CPU)')
    parser.add_argument('--dataset_path', type=str, default="/root/dataset/cityscapes", help='cityscape dataset path.')
    parser.add_argument('--phase', type=str, default="train", help='train, eval or predict.')
    parser.add_argument('--model', type=str, default="resnet", choices=("resnet", "unet"),
                        help='generator model, should be in [resnet, unet].')
    parser.add_argument('--max_epoch', type=int, default=200, help='epoch size for training, default is 200.')
    parser.add_argument('--n_epoch', type=int, default=100,
                        help='number of epochs with the initial learning rate, default is 100')
    parser.add_argument('--batch_size', type=int, default=1, help='Batch size.')
    parser.add_argument("--save_checkpoint_epochs", type=int, default=10,
                        help="Save checkpoint epochs, default is 10.")
    parser.add_argument("--G_A_ckpt", type=str, default="/etc/tinyms/serving/cyclegan_cityscape/G_A.ckpt", help="pretrained checkpoint file path of G_A.")
    parser.add_argument("--G_B_ckpt", type=str, default="/etc/tinyms/serving/cyclegan_cityscape/G_B.ckpt", help="pretrained checkpoint file path of G_B.")
    parser.add_argument("--D_A_ckpt", type=str, default=None, help="pretrained checkpoint file path of D_A.")
    parser.add_argument("--D_B_ckpt", type=str, default=None, help="pretrained checkpoint file path of D_B.")
    parser.add_argument('--outputs_dir', type=str, default='/root/',
                        help='models are saved here, default is ./outputs.')
    parser.add_argument('--save_imgs', type=bool, default=True,
                        help='whether save imgs when epoch end, default is True.')
    parser.add_argument("--cityscapes_dir", type=str, default="/root/dataset/cityscapes/testA", help="Path to the original cityscapes dataset")
    parser.add_argument("--result_dir", type=str, default="/root/dataset/cityscapes/testB", help="Path to the generated images to be evaluated")
    args_opt = parser.parse_args(args=[])
    return args_opt


args_opt = parse_args()

context.set_context(mode=context.GRAPH_MODE, device_target="CPU")

dataset_path = args_opt.dataset_path
phase = args_opt.phase
G_A_ckpt = args_opt.G_A_ckpt
G_B_ckpt = args_opt.G_B_ckpt
repeat_size = 1


model = args_opt.model
batch_size = args_opt.batch_size
max_dataset_size = float("inf")
outputs_dir = args_opt.outputs_dir

max_epoch = args_opt.max_epoch
n_epoch = args_opt.n_epoch
n_epoch = min(max_epoch, n_epoch)


def create_dataset(dataset_path, batch_size=1, repeat_size=1, max_dataset_size=None,
                   shuffle=True, num_parallel_workers=1, phase='train', data_dir='testA'):
    """ create Mnist dataset for train or eval.
    Args:
        data_path: Data path
        batch_size: The number of data records in each group
        repeat_size: The number of replicated data records
        num_parallel_workers: The number of parallel workers
    """
    # define dataset and apply the transform func
    if phase == 'train':
        ds = UnalignedDataset(dataset_path, phase, max_dataset_size=max_dataset_size, shuffle=True)

        device_num = 1
        distributed_sampler = DistributedSampler(len(ds), num_replicas=device_num, rank=0, shuffle=shuffle)
        gan_generator_ds = GeneratorDataset(ds, column_names=["image_A", "image_B"], sampler=distributed_sampler,
                                            num_parallel_workers=num_parallel_workers)
    else:
        datadir = os.path.join(dataset_path, data_dir)
        ds = GanImageFolderDataset(datadir, max_dataset_size=max_dataset_size)
        gan_generator_ds = GeneratorDataset(ds, column_names=["image", "image_name"],
                                            num_parallel_workers=num_parallel_workers)

    gan_generator_ds = cyclegan_transform.apply_ds(gan_generator_ds,
                                                   repeat_size=repeat_size,
                                                   batch_size=batch_size,
                                                   num_parallel_workers=num_parallel_workers,
                                                   shuffle=shuffle,
                                                   phase=phase)
    dataset_size = len(ds)
    return gan_generator_ds, dataset_size


# create dataset
dataset, args_opt.dataset_size = create_dataset(dataset_path, batch_size=batch_size, repeat_size=1,
                                                max_dataset_size=max_dataset_size, shuffle=True,
                                                num_parallel_workers=1,
                                                phase="train",
                                                data_dir=None)


G_A, G_B, D_A, D_B = get_generator_discriminator(model)
gan_load_ckpt(args_opt.G_A_ckpt, args_opt.G_B_ckpt, args_opt.D_A_ckpt, args_opt.D_B_ckpt,
              G_A, G_B, D_A, D_B)
generator_net = cycle_gan(G_A, G_B)

# define loss function and optimizer
loss_D = CycleGANDiscriminatorLoss(D_A, D_B)
loss_G = CycleGANGeneratorLoss(generator_net, D_A, D_B)
lr = cyclegan_lr(max_epoch, n_epoch, args_opt.dataset_size)

optimizer_G = Adam(generator_net.trainable_params(),
                   cyclegan_lr(max_epoch, n_epoch, args_opt.dataset_size), beta1=0.5)
optimizer_D = Adam(loss_D.trainable_params(),
                   cyclegan_lr(max_epoch, n_epoch, args_opt.dataset_size), beta1=0.5)

# build two net: generator net and descriminator net
net_G = TrainOneStepG(loss_G, generator_net, optimizer_G)
net_D = TrainOneStepD(loss_D, optimizer_D)

# train process
imgae_pool_A = GanImagePool(pool_size=50)
imgae_pool_B = GanImagePool(pool_size=50)


def train_process(args_opt, data_loader, net_G, net_D, imgae_pool_A, imgae_pool_B):
    reporter = GanReporter(args_opt)
    reporter.info('==========start training===============')
    for _ in range(max_epoch):
        reporter.epoch_start()
        for data in data_loader:
            img_A = data["image_A"]
            img_B = data["image_B"]
            res_G = net_G(img_A, img_B)
            fake_A = res_G[0]
            fake_B = res_G[1]
            res_D = net_D(img_A, img_B, imgae_pool_A.query(fake_A), imgae_pool_B.query(fake_B))
            reporter.step_end(res_G, res_D)
            reporter.visualizer(img_A, img_B, fake_A, fake_B)
        reporter.epoch_end(net_G)

    reporter.info('==========end training===============')


data_loader = dataset.create_dict_iterator()
train_process(args_opt, data_loader, net_G, net_D, imgae_pool_A, imgae_pool_B)

# eval
# original image dir
cityscapes_dir = args_opt.cityscapes_dir

# fake image dir generated after predict
result_dir = args_opt.result_dir


def eval_process(args_opt, cityscapes_dir, result_dir):
    CS = CityScapes()
    hist_perframe = ts.zeros((CS.class_num, CS.class_num)).asnumpy()
    cityscapes = generate_image_list(cityscapes_dir)
    args_opt.dataset_size = len(cityscapes)
    reporter = GanReporter(args_opt)
    reporter.start_eval()
    for i, img_path in enumerate(cityscapes):
        if i % 100 == 0:
            reporter.info('Evaluating: %d/%d' % (i, len(cityscapes)))
        img_name = os.path.split(img_path)[1]
        ids1 = CS.get_id(os.path.join(cityscapes_dir, img_name))
        ids2 = CS.get_id(os.path.join(result_dir, img_name))
        hist_perframe += fast_hist(ids1.flatten(), ids2.flatten(), CS.class_num)

    mean_pixel_acc, mean_class_acc, mean_class_iou, per_class_acc, per_class_iou = get_scores(hist_perframe)
    reporter.info("mean_pixel_acc:{}, mean_class_acc:{}, mean_class_iou: {}".format(mean_pixel_acc,
                                                                                    mean_class_acc,
                                                                                    mean_class_iou))
    reporter.info("************ Per class numbers below ************")
    for i, cl in enumerate(CS.classes):
        while len(cl) < 15:
            cl = cl + ' '
        reporter.info("{}: acc = {}, iou = {}".format(cl, per_class_acc[i], per_class_iou[i]))
    reporter.end_eval()


# Compare the similarity between the original image and the fake image
eval_process(args_opt, cityscapes_dir, result_dir)
提示:如果跳过了训练步骤,下载预训练的ckpt文件并继续推理步骤

点击G_A下载G_A.ckpt, 点击G_B下载G_B.ckpt,并将这两个ckpt文件保存到/etc/tinyms/serving/cyclegan_cityscape

或者运行以下代码下载两个ckpt文件:

[2]:
ckpt_folder = '/etc/tinyms/serving/cyclegan_cityscape'
G_A_ckpt_path = '/etc/tinyms/serving/cyclegan_cityscape/G_A.ckpt'
G_B_ckpt_path = '/etc/tinyms/serving/cyclegan_cityscape/G_B.ckpt'

if not os.path.exists(ckpt_folder):
    !mkdir -p  /etc/tinyms/serving/cyclegan_cityscape
    !wget -P /etc/tinyms/serving/cyclegan_cityscape https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cityscapes/G_A.ckpt
    !wget -P /etc/tinyms/serving/cyclegan_cityscape https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cityscapes/G_B.ckpt
else:
    print('ckpt folder already exists')
    if not os.path.exists(G_A_ckpt_path):
        !wget -P /etc/tinyms/serving/cyclegan_cityscape https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cityscapes/G_A.ckpt
    if not os.path.exists(G_B_ckpt_path):
        !wget -P /etc/tinyms/serving/cyclegan_cityscape https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cityscapes/G_B.ckpt
ckpt folder already exists
--2021-03-21 15:16:58--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cityscapes/G_A.ckpt
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 121.36.121.44, 49.4.112.5, 49.4.112.90, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|121.36.121.44|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7785480 (7.4M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/cyclegan_cityscape/G_A.ckpt’

G_A.ckpt            100%[===================>]   7.42M  9.60MB/s    in 0.8s

2021-03-21 15:16:59 (9.60 MB/s) - ‘/etc/tinyms/serving/cyclegan_cityscape/G_A.ckpt’ saved [7785480/7785480]

--2021-03-21 15:16:59--  https://ascend-tutorials.obs.cn-north-4.myhuaweicloud.com/ckpt_files/cityscapes/G_B.ckpt
Resolving ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)... 121.36.121.44, 49.4.112.5, 49.4.112.90, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|121.36.121.44|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7785480 (7.4M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/cyclegan_cityscape/G_B.ckpt’

G_B.ckpt            100%[===================>]   7.42M  10.6MB/s    in 0.7s

2021-03-21 15:17:01 (10.6 MB/s) - ‘/etc/tinyms/serving/cyclegan_cityscape/G_B.ckpt’ saved [7785480/7785480]

3. 定义servable.json

运行下列代码定义servable json文件:

[3]:
servable_json = [{'name': 'cyclegan_cityscape',
                  'description': 'This servable hosts a Cycle GAN model predicting for cityscape dataset',
                  'model': {
                      "name": "cycle_gan",
                      "format": "ckpt",
                      "g_model": "resnet"}}]
os.chdir("/etc/tinyms/serving")
json_data = json.dumps(servable_json, indent=4)

with open('servable.json', 'w') as json_file:
    json_file.write(json_data)

4. 启动服务器

4.1 介绍

TinyMS推理是C/S(Client/Server)架构。TinyMS使用Flask这个轻量化的网页服务器架构作为C/S通讯的基础架构。为了能够对模型进行推理,用户必须首先启动服务器。如果成功启动,服务器会在子进程中运行并且会监听从地址127.0.0.1,端口号5000发送来的POST请求并且使用MindSpore作为后端来处理这些请求。后端会构建模型,运行推理并且返回结果给客户端

4.2 启动服务器

运行下列代码以启动服务器:

[4]:
start_server()
Server starts at host 127.0.0.1, port 5000

5. 推理

5.1 List servables

使用list_servables函数检查当前后端的serving模型

[5]:
list_servables()
[5]:
[{'description': 'This servable hosts a Cycle GAN model predicting for cityscape dataset',
  'model': {'format': 'ckpt', 'g_model': 'resnet', 'name': 'cycle_gan'},
  'name': 'cyclegan_cityscape'}]

如果输出的description字段显示这是一个CycleGAN的模型,则可以继续到下一步发送推理请求

5.2 发送推理请求

运行predict函数发送推理请求,gray to colorcolor to gray两种策略都会被执行,建议直接使用cityscapes/test数据集中的图片,利于进行风格对比,如果自行上传图片,请resize至 256*256 的图片尺寸

[6]:
servable_name = 'cyclegan_cityscape'
dataset_name = 'cityscape'

if server_started() is True:
    # gray to color
    testA_path = '/root/dataset/cityscapes/testA/1.jpg'
    strategy = 'gray2color'
    fakeB_data = predict(testA_path, servable_name, dataset_name, strategy)

    # color to gray
    testB_path = '/root/dataset/cityscapes/testB/1.jpg'
    strategy = 'color2gray'
    fakeA_data = predict(testB_path, servable_name, dataset_name, strategy)

    # draw the plot
    plt.figure(dpi=160, figsize=(10, 10))

    plt.subplot(221)
    plt.imshow(Image.open(testA_path))
    plt.axis('off')
    plt.title(testA_path)

    plt.subplot(222)
    plt.imshow(Image.fromarray(np.uint8(fakeB_data)))
    plt.axis('off')
    plt.title("fakeB.jpg")

    plt.subplot(223)
    plt.imshow(Image.open(testB_path))
    plt.axis('off')
    plt.title(testB_path)

    plt.subplot(224)
    plt.imshow(Image.fromarray(np.uint8(fakeA_data)))
    plt.axis('off')
    plt.title("fakeA.jpg")

    plt.show()
else:
    print("Server not started")
_images/tutorials_ipynb_TinyMS_CycleGAN_tutorial_zh_15_0.png

检查输出

如果看到了4张图片,表明左列图片是原始数据集中的图片,而右列的图片是生成的图片

关闭服务器

[7]:
shutdown()
[7]:
'Server shutting down...'

TinyMS 视频教程

为了方便小白用户学习,TinyMS提供了全套在线视频教程,随着每个版本的迭代会进行同步更新。我们按照每一集的内容进行更新存档,包含但不限于视频网址、学习文档和Q&A,如果大家在学习的过程中遇到任何疑问,欢迎添加小助手的微信(Wechat ID: mindspore0328,备注:TinyMS),小助手会将您添加至TinyMS课程学习群内,在群内与开发人员及时沟通,当然我们也非常欢迎您遇到问题直接提 Issue,这样也能帮助其他遇到同样的问题的开发者。

课程列表

集数

标题

主题内容

学习文档

状态

更新时间

EP01

最适合小白的深度学习入门课程来了!

老师简介+深度学习科普+课程介绍

已发布

2021.3.30

EP02

计算机是如何识别图像的?

计算机识别图像科普+TinyMS安装(Ubuntu、Win10、Docker安装)

TinyMS快速安装

已发布

2021.3.31

EP03

30min速成Shell脚本命令

shell基本命令、变量与传参、数组、运算符、test命令、使用函数、输入输出重定向、流程控制函数以及常见的文件操作命令

Shell脚本30min速成指南

待发布

2021.3.31

EP04

30min快速入门Python

EP05

30min学会深度学习中的数学知识

30min速成Shell脚本

一、shell是什么?

我们常说的shell脚本,是一种为shell编写的脚本程序,shell编程非常简洁,只需要在有一个能写代码的文本编辑器,比如上文提到的vim,和能解释执行的脚本解释器就行。先看一下你的电脑里装了哪些shell呢?

输入:cat /etc/shells

# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

一般我们常用的是Bash,也就是Bourne Again Shell,也是大多数Linux系统默认的shell,不过我们基本不会可以区分Bourne Shell 和 Bourne Again Shell,所以像 #!/bin/sh,它同样也可以改为 #!/bin/bash

#! 意思是告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序

如何运行shell文件?
  • **1.**作为可执行程序

vi hello.sh

chmod +x ./test.sh #使脚本具有执行权限

./test.sh #执行脚本

注意执行的时候一定要写成 ./test.sh,而不是 test.sh,运行其它二进制的程序也一样,直接写 test.sh,linux 系统会去 PATH 里寻找有没有叫 test.sh 的,而只有 /bin, /sbin, /usr/bin,/usr/sbin 等在 PATH 里,你的当前目录通常不在 PATH 里,所以写成 test.sh 是会找不到命令的,要用 ./test.sh 告诉系统说,就在当前目录找。

  • 2.作为解释器参数

这种运行方式是,直接运行解释器,其参数就是 shell 脚本的文件名,如:

sh test.sh
bash test.sh

二、变量和参数

2.1 变量
#!/bin/bash
# 脚本的第一行叫 shebang,用来告知系统如何执行该脚本:

# 显示 “Hello world!”
echo Hello world!

# 每一句指令以换行或分号隔开:
echo 'This is the Day One Tutorial'; echo 'This is Day Two Tutorial'

# 声明一个变量:
a="Hello TinyMS"

# 下面是错误的做法:
a = "Hello TinyMS"
# Bash 会把 a 当做一个指令,由于找不到该指令,因此这里会报错。

# 也不可以这样:
a= 'Hello TinyMS'
# Bash 会认为 'Hello,TinyMS' 是一条指令,由于找不到该指令,这里再次报错。
# (这个例子中 'a=' 这部分会被当作仅对 'Hello,TinyMS' 起作用的赋值。)

# 使用变量:
echo $a
echo "$a"
echo '$a'
# 当你赋值 (assign) 、导出 (export),或者以其他方式使用变量时,变量名前不加 $。
# 如果要使用变量的值, 则要加 $。
# 注意: ' (单引号) 不会展开变量(即会屏蔽掉变量)。

# 在变量内部进行字符串代换
echo ${a/Hello/Hi}
# 会把 a 中首次出现的 "some" 替换成 “A”。

# 变量的截取
Length=6
echo ${a:0:Length}
# 这样会仅返回变量值的前7个字符

# 变量的默认值
echo ${a:-"DefaultValueIfFooIsMissingOrEmpty"}
# 对 null (Foo=) 和空串 (Foo="") 起作用; 零(Foo=0)时返回0
# 注意这仅返回默认值而不是改变变量的值

几个内置变量

参数处理 说明
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。 如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的ID号
$@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。 如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$- 显示Shell使用的当前选项,与set命令功能相同。
$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
# 内置变量:
echo "Last program return value: $?"
echo "Script's PID: $$"
echo "Number of arguments: $#"
echo "Scripts arguments: $@"
echo "Scripts arguments separated in different as: $1 $2..."

$* 与 $@ 区别:

  • 相同点:都是引用所有参数。

  • 不同点:只有在双引号中体现出来。假设在脚本运行时写了三个参数 1、2、3,,则 “ * “ 等价于 “1 2 3”(传递了一个参数),而 “@” 等价于 “1” “2” “3”(传递了三个参数)。

#!/bin/bash

echo "-- \$* example ---"
for i in "$*"; do
    echo $i
done

echo "-- \$@ example ---"
for i in "$@"; do
    echo $i
done

执行脚本,输出结果如下所示:

$ chmod +x test.sh 
$ ./test.sh 1 2 3
-- $* example ---
1 2 3
-- $@ example ---
1
2
3
2.2 传递参数
  • 读取输入:

echo "What's your name?"
read Name # 这里不需要声明新变量,输入自己的名字
# 在屏幕输入你的名字,这里我输入的是Charlotte
echo Hello, $Name!

输出结果:

Hello,Charlotte
  • 传递参数

#!/bin/bash

echo "Shell 传递参数!";
echo "执行的文件名:$0";
echo "第一个参数为:$1";
echo "第二个参数为:$2";
echo "第三个参数为:$3";

输出结果:

$ sh test.sh 1 2 3
Shell 传递参数实例!
执行的文件名:./test.sh
第一个参数为:1
第二个参数为:2
第三个参数为:3

三、shell数组

Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似)。

  • 创建数组

格式:

array_name=(value1 value2 ... valuen)

举例:

test_array=(this is shell tutorial)
  • 读取数组

格式:

${array_name[index]}

举例:

#!/bin/bash

test_array=(this is shell tutorial)

echo "第一个元素为: ${test_array[0]}"
echo "第二个元素为: ${test_array[1]}"
echo "第三个元素为: ${test_array[2]}"
echo "第四个元素为: ${test_array[3]}"

输出结果:

root@73df419e7539:~# sh array.sh
第一个元素为: this
第二个元素为: is
第三个元素为: shell
第四个元素为: tutorial
  • 获取数组中的所有元素

#!/bin/bash

test_array[0]=this
test_array[1]=is
test_array[2]=shell
test_array[3]=tutorial

echo "数组的元素为: ${test_array[*]}"
echo "数组的元素为: ${test_array[@]}"
  • 获取数组的长度

#!/bin/bash

test_array[0]=this
test_array[1]=is
test_array[2]=shell
test_array[3]=tutorial

echo "数组的元素为: ${#test_array[*]}"
echo "数组的元素为: ${#test_array[@]}"
  • 用for 循环遍历数组

#!/bin/bash
  arr=(1 2 3 4 5 6 7 8 9 10)
  for a in ${arr[*]}
  do
    echo $a
  done
  • 用while循环输出数组

arr=(1 2 3 4 5 6 7 8 9 10)
i=0
while [ $i -lt ${#arr[@]} ]
do
  echo ${arr[$i]}
  let i++
done

四、shell运算符

Shell 和其他编程语言一样,支持多种运算符,包括:

  • 算数运算符

  • 关系运算符

  • 布尔运算符

  • 字符串运算符

  • 文件测试运算符

例如两数相加:

#!/bin/bash

val=`expr 2 + 2` 
val_1=$(expr 10 + 20)
echo "两数之和为 : $val"
echo "两数之和为 : $val_1"

两点注意:

  • 表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2。

  • 完整的表达式要被 `` 包含,注意这个字符不是常用的单引号,在 Esc 键下边。

算术运算符
关系运算符

关系运算符只支持数字,不支持字符串,除非字符串的值是数字。

下表列出了常用的关系运算符,假定变量 a 为 10,变量 b 为 20:

运算符 说明 举例
-eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] 返回 false。
-ne 检测两个数是否不相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
-gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。
-lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。
-ge 检测左边的数是否大于等于右边的,如果是,则返回 true。 [ $a -ge $b ] 返回 false。
-le 检测左边的数是否小于等于右边的,如果是,则返回 true。 [ $a -le $b ] 返回 true。

EQ 就是 EQUAL等于

NE 就是 NOT EQUAL不等于

GT 就是 GREATER THAN大于

LT 就是 LESS THAN小于

GE 就是 GREATER THAN OR EQUAL 大于等于

LE 就是 LESS THAN OR EQUAL 小于等于

示例:

#!/bin/bash

a=10
b=20

if [ $a -eq $b ]
then
   echo "$a -eq $b : a 等于 b"
else
   echo "$a -eq $b: a 不等于 b"
fi
if [ $a -ne $b ]
then
   echo "$a -ne $b: a 不等于 b"
else
   echo "$a -ne $b : a 等于 b"
fi
if [ $a -gt $b ]
then
   echo "$a -gt $b: a 大于 b"
else
   echo "$a -gt $b: a 不大于 b"
fi
if [ $a -lt $b ]
then
   echo "$a -lt $b: a 小于 b"
else
   echo "$a -lt $b: a 不小于 b"
fi
if [ $a -ge $b ]
then
   echo "$a -ge $b: a 大于或等于 b"
else
   echo "$a -ge $b: a 小于 b"
fi
if [ $a -le $b ]
then
   echo "$a -le $b: a 小于或等于 b"
else
   echo "$a -le $b: a 大于 b"
fi

输出结果:

10 -eq 20: a 不等于 b
10 -ne 20: a 不等于 b
10 -gt 20: a 不大于 b
10 -lt 20: a 小于 b
10 -ge 20: a 小于 b
10 -le 20: a 小于或等于 b
布尔运算符

下表列出了常用的布尔运算符,假定变量 a 为 10,变量 b 为 20:

运算符 说明 举例
! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
-o 或运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a 与运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false。

示例

#!/bin/bash

a=10
b=20

if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a == $b: a 等于 b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a 小于 100 且 $b 大于 15 : 返回 true"
else
   echo "$a 小于 100 且 $b 大于 15 : 返回 false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a 小于 100 或 $b 大于 100 : 返回 true"
else
   echo "$a 小于 100 或 $b 大于 100 : 返回 false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a 小于 5 或 $b 大于 100 : 返回 true"
else
   echo "$a 小于 5 或 $b 大于 100 : 返回 false"
fi

输出结果:

10 != 20 : a 不等于 b
10 小于 10020 大于 15 : 返回 true
10 小于 10020 大于 100 : 返回 true
10 小于 520 大于 100 : 返回 false
逻辑运算符
运算符 说明 举例
&& 逻辑的 AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| 逻辑的 OR [[ $a -lt 100 || $b -gt 100 ]] 返回 true

示例

#!/bin/bash

a=10
b=20

if [[ $a -lt 100 && $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

if [[ $a -lt 100 || $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi

输出结果:

返回 false
返回 true
字符串运算符

下表列出了常用的字符串运算符,假定变量 a 为 “abc”,变量 b 为 “efg”:

运算符 说明 举例
= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 检测两个字符串是否不相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
-n 检测字符串长度是否不为 0,不为 0 返回 true。 [ -n "$a" ] 返回 true。
$ 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。

示例:

#!/bin/bash

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a 等于 b"
else
   echo "$a = $b: a 不等于 b"
fi
if [ $a != $b ]
then
   echo "$a != $b : a 不等于 b"
else
   echo "$a != $b: a 等于 b"
fi
if [ -z $a ]
then
   echo "-z $a : 字符串长度为 0"
else
   echo "-z $a : 字符串长度不为 0"
fi
if [ -n "$a" ]
then
   echo "-n $a : 字符串长度不为 0"
else
   echo "-n $a : 字符串长度为 0"
fi
if [ $a ]
then
   echo "$a : 字符串不为空"
else
   echo "$a : 字符串为空"
fi

输出结果:

abc = efg: a 不等于 b
abc != efg : a 不等于 b
-z abc : 字符串长度不为 0
-n abc : 字符串长度不为 0
abc : 字符串不为空

下表列出了常用的算术运算符,假定变量 a 为 10,变量 b 为 20:

运算符 说明 举例
+ 加法 expr $a + $b 结果为 30。
- 减法 expr $a - $b 结果为 -10。
* 乘法 expr $a \* $b 结果为 200。
/ 除法 expr $b / $a 结果为 2。
% 取余 expr $b % $a 结果为 0。
= 赋值 a=$b 将把变量 b 的值赋给 a。
== 相等。用于比较两个数字,相同则返回 true。 [ $a == $b ] 返回 false。
!= 不相等。用于比较两个数字,不相同则返回 true。 [ $a != $b ] 返回 true。

**注意:**条件表达式要放在方括号之间,并且要有空格,例如: [$a==$b] 是错误的,必须写成 [ $a == $b ]

#!/bin/bash

a=10
b=20

val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
   echo "a 等于 b"
fi
if [ $a != $b ]
then
   echo "a 不等于 b"
fi

输出结果:

a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a 不等于 b

五、Shell test命令

Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

数值测试
参数 说明
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真

示例

num1=100
num2=100
if test $[num1] -eq $[num2]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi

输出结果:

两个数相等!
判断文件夹/文件是否存在

文件夹不存在则创建

if [ ! -d "/root/test" ];then
  mkdir /root/test
  else
  echo "文件夹已经存在"
fi

文件存在则删除

if [ ! -f "/root/test1.sh" ];then
  echo "文件不存在"
  else
  rm -f /root/test1.sh
fi

判断文件夹是否存在

if [ -d "/data/" ];then
  echo "文件夹存在"
  else
  echo "文件夹不存在"
fi

判断文件是否存在

if [ -f "/data/filename" ];then
  echo "文件存在"
  else
  echo "文件不存在"
fi

六、使用函数

定义函数

function foo ()
{
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
}

更简单的方法:

# 更简单的方法
bar ()
{
    echo "Another way to declare functions!"
    return 0
}

调用函数

foo "My name is" $Name

输出结果

root@2097f8ce4358:~# foo "My name is" $Name
Arguments work just like script arguments: My name is Charlotte
And: My name is Charlotte...
This is a function

七、输入输出重定向

常用场景:前一个指令的输出可以当作后一个指令的输入,在深度学习中启动训练时通常可以把运行训练的结果输出到log文件中,或者作为下游任务的输入,以下列举了一些常用的命令

# 用下面的指令列出当前目录下所有的 txt 文件:
ls -l | grep "\.txt"

# 重定向输入和输出(标准输入,标准输出,标准错误)。
# 以 ^EOF$ 作为结束标记从标准输入读取数据并覆盖 hello.py :
cat > hello.py << EOF
#!/usr/bin/env python
from __future__ import print_function
import sys
print("#stdout", file=sys.stdout)
print("#stderr", file=sys.stderr)
for line in sys.stdin:
    print(line, file=sys.stdout)
EOF

# 重定向可以到输出,输入和错误输出。
python hello.py < "input.in"
python hello.py > "output.out"
python hello.py 2> "error.err"
python hello.py > "output-and-error.log" 2>&1
python hello.py > /dev/null 2>&1
# > 会覆盖已存在的文件, >> 会以累加的方式输出文件中。
python hello.py >> "output.out" 2>> "error.err"

# 覆盖 output.out , 追加 error.err 并统计行数
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
wc -l output.out error.err

# 运行指令并打印文件描述符 (比如 /dev/fd/123)
# 具体可查看: man fd
echo <(echo "#helloworld")

# 以 "#helloworld" 覆盖output.out,以下三种方式均可:
cat > output.out <(echo "#helloworld")
echo "#helloworld" > output.out
echo "#helloworld" | cat > output.out
echo "#helloworld" | tee output.out >/dev/null

# 清理临时文件并显示详情(增加 '-i' 选项启用交互模式)
rm -v output.out error.err output-and-error.log

# 一个指令可用 $( ) 嵌套在另一个指令内部:
# 以下的指令会打印当前目录下的目录和文件总数
echo "There are $(ls | wc -l) items here."

# 反引号 `` 起相同作用,但不允许嵌套
# 优先使用 $(  ).
echo "There are `ls | wc -l` items here."

八、流程控制函数

# Bash 的 case 语句与 Java 和 C++ 中的 switch 语句类似:
case "$a" in
    # 列出需要匹配的字符串
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;
esac

# 循环遍历给定的参数序列:
# 变量$a 的值会被打印 3 次。
for a in {1..3}
do
    echo "$a"
done

# 或传统的 “for循环” :
for ((a=1; a <= 3; a++))
do
    echo $a
done

# 也可以用于文件
# 用 cat 输出 file1 和 file2 内容
for a in test2.sh test3.sh
do
    cat "$a"
done

# 或作用于其他命令的输出
# 对 ls 输出的文件执行 cat 指令。
for Output in $(ls)
do
    cat "$Output"
done

# while 循环:
while [ true ]
do
    echo "loop body here..."
    break
done

九、常见的文件操作命令

下面列举了一些常见的文件操作,帮助大家在实际开发中提升效率

# 打印 file.txt 的最后 10 行
tail -n 10 hello.txt
# 打印 file.txt 的前 10 行
head -n 10 hello.txt
# 将 file.txt 按行排序
sort hello.txt
# 报告或忽略重复的行,用选项 -d 打印重复的行
uniq -d hello.txt
# 打印每行中 ',' 之前内容
cut -d ',' -f 1 hello.txt
# 将 file.txt 文件所有 'hello' 替换为 'Hi', (兼容正则表达式)
sed -i 's/Hello/Hi/g' hello.txt
# 将 file.txt 中匹配正则的行打印到标准输出
# 这里打印以 "1" 开头, "MS" 结尾的行
grep "^1.*MS$" hello.txt
# 使用选项 "-c" 统计行数
grep -c "^1.*MS$" hello.txt
# 如果只是要按字面形式搜索字符串而不是按正则表达式,使用 fgrep (或 grep -F)
fgrep "^1.*MS$" hello.txt 

参考资料:

1.https://www.runoob.com/linux/linux-shell-io-redirections.html

2.https://jmt33.github.io/mtao/Html/Linux/20141022113029_shell.html

3.https://learnxinyminutes.com/docs/zh-cn/bash-cn/

30min速成Python

30min速成深度学习中的数学知识

TinyMS的设计理念

背景

近些年随着AI技术的蓬勃发展,业界不断涌现出很多优秀的深度学习框架:比如TensorFlow、PyTorch、Apache MXNet、MindSpore等,这些框架的提出满足了各行各业进行AI模型/应用开发的需求。但是由于AI技术的门槛较高,导致原生框架的API并不能满足所有用户/开发者的需求,这也导致后期出现了许多针对AI框架而定制开发的高阶API项目:比如Keras(TF)、fastai(PyTorch)等。TinyMS是此类项目的新成员,旨在提供简单有效的高阶API、低运行开销、模块化开发以及敏捷部署。TinyMS的开发将首要聚焦在对MindSpore框架的支持,同时也欢迎开发者提供对更多框架的支持。

值得注意的是,MindSpore的高阶和中阶Python API已经实现了Keras的大部分功能,不需要再额外封装一层类似Keras的API;与fastai本身专为PyTorch灵活性而生的快速开发验证而提供的能力也不相上下。因此不同于Keras和fastai基于底层框架的特点或不足进行进一步优化的设计初衷,TinyMS着重于提升开发者对已有框架即MindSpore的使用体验,尤其是面向全场景的开发和部署。

在TinyMS的帮助下,我们希望达到如下目标:

  • 一分钟内上手AI应用开发

  • 一小时内掌握AI模型与数据集的自由切换

架构设计

TinyMS的架构目标:

  • 极简易学的高阶API

  • 支持从数据准备到模型训练/推理到最终部署的全流程

  • 模块间解耦,易于扩展

  • 可以应用在手机、边缘、云等全场景的低运行时开销支持

  • 模型训练脚本的格式进行标准化和规范化

_images/tinyms-architecture.pngTinyMS Architecture

流程解析

对于大多数AI模型应用开发场景,基本上都可以归纳为如下几个步骤:

  • 数据获取:包括数据集下载、解压、加载等操作

  • 数据处理:为了能让模型获得更好的性能,一般都会针对原始数据进行数据预处理(增强)操作

  • 模型构建:除了网络主体的构建,还包括Loss损失函数、Optimizer优化器等定义

  • 模型训练:负责模型训练的流程,其中包括callbacks的定义

  • 精度验证:负责模型精度验证的流程,其中包括metrics的定义

  • 模型部署:通过搭建服务器来提供AI模型应用服务

_images/tinyms-workflow.pngTinyMS Workflow

模块划分

针对上述场景提到的步骤,当前TinyMS创建了如下几个模块:

模块名称 功能介绍 样例代码
app 支持OpenCV实现模型推理可视化 from tinyms.app import object_detection
data 数据集一键下载和加载 from tinyms.data import MnistDataset, download_dataset
hub 预训练模型库,适用于模型推理和迁移学习 from tinyms import hub
model Model高阶API以及预置网络 from tinyms.model import Model, lenet5
serving 模型部署模块 from tinyms.serving import predict
vision CV领域相关的数据处理 from tinyms.vision import mnist_transform, Resize
text NLP领域相关的数据处理 from tinyms.text import Lookup
callbacks 模型训练过程的回调处理 from tinyms.callbacks import ModelCheckpoint
common 基础组件,包括Tensor、numpy风格的函数 from tinyms import Tensor, array
context 全局上下文 from tinyms import context
initializers 算子权重初始化 from tinyms.initializers import Normal
layers 构建网络的算子清单 from tinyms.layers import Layer, Conv2d
losses 模型训练的损失函数 from tinyms.losses import SoftmaxCrossEntropyWithLogits
metrics 模型验证的指标收集 from tinyms.metrics import Accuracy
optimizers 模型训练的优化器 from tinyms.optimizers import Momentum
primitives 基础算子 from tinyms.primitives import Add, tensor_add

模块实现

看到这里相信大家对TinyS的整体架构有了初步的了解,接下来我们就每个模块的设计与实现思路逐一讲解。

数据加载(data

数据加载模块主要分为数据集下载和加载两个部分,通过TinyMS的数据加载API仅需两行代码便可完成常见数据集的下载、解压、格式调整、加载等流程。

大部分AI框架不会提供数据集下载的接口,需要用户提前准备好数据集,同时要按照框架本身提供的数据加载API格式进行格式的调整(训练/验证数据集划分等),这对于AI初学者来说门槛还是相当高的。基于该问题TinyMS提供了download_dataset接口,支持用户一键完成数据集的下载、解压和格式调整操作;以Mnist手写数字数据集为例:

from tinyms.data import download_dataset

mnist_path = download_dataset('mnist', local_path='./')

对于数据加载操作,TinyMS完全继承了MindSpore原生的数据加载API,这样用户可以非常方便地使用xxxDataset接口进行不同数据集的实例化;以MnistDataset为例:

from tinyms.data import MnistDataset

mnist_ds = MnistDataset(mnist_path, shuffle=True)

数据处理(visiontext

通常在构建AI模型开发应用时,数据处理是我们面临的第一大挑战:数据量不足、人为标注工作量大、数据格式不规范等问题都又可能导致训练之后的网络精度不达标,因此绝大多数AI框架都会提供数据处理的相关模块。以MindSpore为例,MindSpore当前提供CV和NLP等常用场景的数据处理功能(相关接口定义可以查阅mindspore.dataset.visionmindspore.dataset.text),用户可以直接调用其中预设的数据处理算子对图片或文本进行处理,然后通过构建数据处理pipeline来对海量数据进行高效并行处理(详见此处)。

TinyMS在MindSpore的基础上做了进一步的抽象与封装,通过DatasetTransform接口直接对应到数据集本身的处理,让用户可以一行代码就实现单条数据或者整个数据集的预处理操作;以MnistTransform为例:

from PIL import Image
from tinyms.vision import mnist_transform

# 针对单张图片进行预处理
img = mnist_transform(Image.open('picture.jpg'))
# 针对MnistDataset实例进行预处理
mnist_ds = mnist_transform.apply_ds(mnist_ds)

网络构建(model

网络结构作为深度学习模型开发的核心,AI框架的主要职责就是提供完备的算子表达用来构建不同的网络结构,因此AI框架层面的接口更侧重于功能完备度和灵活性;AI框架会专门提供ModelZoo组件,用来满足用户的使用和商业落地诉求。TinyMS在原生脚本之上封装好相关的网络调用API;以LeNet5网络为例:

from tinyms.model import lenet5

net = lenet5(class_num=10)

除了封装常用的网络结构之外,TinyMS还提供了Model高阶API接口(基于MindSpore Model接口封装),通过借鉴Keras Model接口的设计思想,不仅完善了原生API接口的功能,还为希望尝试TinyMS的Keras用户提供了一致性的开发体验:

from tinyms.model import Model

model = Model(net)
model.compile(loss_fn=net_loss, optimizer=net_opt)

模型训练(lossesoptimizerscallbacks

对于模型训练阶段,最重要的因素就是损失函数、优化器以及回调函数的定义,关于这三者的基本定义这里不做赘述。而对于初学者来说,了解损失函数和优化器的基本原理并不困难,但是想要了解其中的原理实现则需要较强的数学背景。因此TinyMS高阶API针对损失函数和优化器进行网络层面的封装,这样无论是训练简单或复杂的网络,用户都可以用一行代码完成初始化工作;以LeNet5网络为例:

from tinyms.losses import SoftmaxCrossEntropyWithLogits
from tinyms.optimizers import Momentum

lr = 0.01
momentum = 0.9
net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
net_opt = Momentum(net.trainable_params(), lr, momentum)

关于回调函数的定义,除了常用的回调函数外(比如TimeMonitorLossMonitor等),MindSpore本身提供了Callback接口以方便用户自定义回调函数。而TinyMS高阶API同样提供了网络层面的封装,这样用户可以用一行代码完成回调函数的初始化工作;以MobileNetV2网络为例:

from tinyms.callbacks import mobilenetv2_cb

net_cb = mobilenetv2_cb(device_target, lr, is_saving_checkpoint, save_checkpoint_epochs, step_size)

模型精度验证(metrics

模型精度验证是检验模型精度是否达标必不可少的过程,MindSpore原生提供了AccuracyPrecision等指标的度量接口(详见此处),同时为用户提供了Metric自定义度量接口。在指标度量方面TinyMS直接继承了原生MindSpore API,用户可以沿用MindSpore的习惯来进行精度验证:

from tinyms.model import Model
from tinyms.metrics import Accuracy

model = Model(net)
model.compile(metrics={"Accuracy": Accuracy())

预训练模型加载(hub

TinyMS Hub是TinyMS生态的预训练模型应用工具,作为模型开发者和应用开发者的管道:

  • 向模型开发者提供方便快捷的模型发布、提交通道;

  • 向应用开发者提供高质量的预训练模型,结合模型加载以及模型Fine-tune API快速完成模型的迁移到部署的工作。

TinyMS Hub提供的预训练模型主要包括图像分类目标检测语义模型推荐模型等。

当前hub模块为开发者提供了多种加载预训练模型的接口:

  • 加载预训练模型

    from PIL import Image
    from tinyms import hub
    from tinyms.vision import mnist_transform
    from tinyms.model import Model
    
    img = Image.open(img_path)
    img = mnist_transform(img)
    
    # load LeNet5 pre-trained model
    net= hub.load('tinyms/0.2/lenet5_v1_mnist', class_num=10)
    model = Model(net)
    
    res = model.predict(ts.expand_dims(ts.array(img), 0)).asnumpy()
    print("The label is:", mnist_transform.postprocess(res))
    
  • 加载模型ckpt文件

    from tinyms import hub
    from tinyms.model import lenet5
    from tinyms.utils.train import load_checkpoint
    
    ckpt_dist_file = '/tmp/lenet5.ckpt'
    hub.load_checkpoint('tinyms/0.2/lenet5_v1_mnist', ckpt_dist_file)
    net = lenet5()
    load_checkpoint(ckpt_dist_file, net=net)
    
  • 加载模型权重

    from tinyms import hub
    from tinyms.model import lenet5
    from tinyms.utils.train import load_param_into_net
    
    param_dict = hub.load_weights('tinyms/0.2/lenet5_v1_mnist')
    net = lenet5()
    load_param_into_net(net, param_dict)
    

模型部署推理(serving

模型部署推理是指将预训练好的模型服务化,使其快速、高效地对用户输入的数据进行处理,得到结果的过程。MindSpore提供了predict函数用于推理,同样的,TinyMS针对这个函数进行了相应的封装,以同一个接口对接不同的后端网络。为了实现服务化,TinyMS基于Flask提供了整套的启动服务器(start_server)、检查后端(list_servables)、检查是否启动(server_started)和关闭服务器(shutdown)等功能; 以LeNet5网络为例:

from tinyms.serving import Server, Client

server = Server()
# 启动推理服务器
server.start_server()

client = Client()
# 查看当前可用的推理模型
client.list_servables()
# 从客户端调用推理接口
client.predict(image_path, 'lenet5', dataset_name='mnist')
# 关闭推理服务器
server.shutdown()

除此之外,TinyMS还提供了WEB可视化界面,方便使用者在网页上直接上传图片进行推理,目前主要支持LeNet5CycleGanSSD300网络,只需启动后台推理服务器,前端服务器通过Nginx web服务器部署,项目路径存放在当前tinyms项目的tinyms/serving/web目录下。若想快速试用,可查看快速安装TinyMS Nginx版本 一节:

# WEB后端服务器启动
from tinyms.serving import Server

server = Server()
server.start_server()

模型推理可视化应用(app

OpenCV是用于计算机视觉的库,TinyMS是深度学习框架的高阶API库。通常我们在训练后,需要加载预训练模型验证模型的效果时,得到的结果往往是一堆数字。这些数据对于初学者是枯燥的,不直观的,要想理解它们代表的含义是非常困难的。因此,TinyMS在0.3.0版本将模型推理可视化作为主要特性,结合OpenCV实现图像的实时监测和可视化检测,去帮助用户更直观地感受推理的效果。目前,可视化推理模块仅支持目标检测模型SSD300,未来会增加对更多图像处理模型的支持。

下面,我将演示如何使用训练过的模型来检测静态图像和电脑摄像头采集的实时动态的视频图像, 仅需5个步骤即可实现:

  • 静态图像对象检测

import cv2

from tinyms.app.object_detection.utils.config_util import load_and_parse_config
from tinyms.app.object_detection.object_detector import ObjectDetector, object_detection_predict
from tinyms.app.object_detection.utils.view_util import visualize_boxes_on_image

# 1.加载和解析模型配置JSON文件
config_path = '**/tinyms/app/object_detection/configs/tinyms/0.3/ssd300_shanshui.json'
config = load_and_parse_config(config_path=config_path)

# 2.创建ObjectDetector类的实例
detector = ObjectDetector(config=config)

# 3.使用OpenCV读取静态图像
img_path = ('./pic/test.jpeg)
image_np = cv2.imread(img_path)
input = image_np.copy()

# 4.对图像进行检测
detection_bbox_data = object_detection_predict(input, detector, is_training=False)

# 5.调用OpenCV库为图像画检测框并显示检测图像检测效果图
detection_image_np = visualize_boxes_on_image(image_np, detection_bbox_data, box_color=(0, 255, 0),
                                              box_thickness=3, text_font=cv2.FONT_HERSHEY_PLAIN,
                                              font_scale=2, text_color=(0, 0, 255), font_size=3, show_scores=True)
cv2.imshow('object detection image', cv2.resize(detection_image_np, (600, 1000)))
cv2.waitKey(0)
  • 电脑摄像头采集的视频图像实时动态检测

import cv2

from tinyms.app.object_detection.utils.config_util import load_and_parse_config
from tinyms.app.object_detection.object_detector import ObjectDetector, object_detection_predict
from tinyms.app.object_detection.utils.view_util import visualize_boxes_on_image

# 1.加载和解析模型配置JSON文件
config_path = "**/tinyms/app/object_detection/configs/tinyms/0.3/ssd300_voc.json"
config = load_and_parse_config(config_path=config_path)

# 2.创建ObjectDetector类的实例
detector = ObjectDetector(config=config)

cap = cv2.VideoCapture(0)
while True:
    # 3.使用OpenCV读取摄像头每帧图像
    ret, image_np = cap.read()
    input = image_np.copy()

    # 4.对每帧图像进行检测
    detection_bbox_data = object_detection_predict(input, detector, is_training=False)

    # 5.调用OpenCV库为每帧图像画检测框并显示动态视频图像检测效果图
    detection_image_np = visualize_boxes_on_image(image_np, detection_bbox_data, box_color=(0, 255, 0),
                                                  box_thickness=3, text_font=cv2.FONT_HERSHEY_PLAIN,
                                                  font_scale=2, text_color=(0, 0, 255), font_size=3, show_scores=True)
    cv2.imshow('object detection camera', cv2.resize(detection_image_np, (800, 600)))

    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

tinyms

Top-level reference to dtype of common module. This module also provides Numpy-like interfaces in TinyMS.

实际案例

>>> import tinyms as ts
>>>
>>> print(ts.ones([2, 3]))
[[1. 1. 1.]
[1. 1. 1.]]
tinyms.dtype_to_nptype(type_)[源代码]

Convert MindSpore dtype to numpy data type.

参数

type_ (mindspore.dtype) – MindSpore’s dtype.

返回

The data type of numpy.

tinyms.issubclass_(type_, dtype)[源代码]

Determine whether type_ is a subclass of dtype.

参数
  • type_ (mindspore.dtype) – Target MindSpore dtype.

  • dtype (mindspore.dtype) – Compare MindSpore dtype.

返回

bool, True or False.

tinyms.dtype_to_pytype(type_)[源代码]

Convert MindSpore dtype to python data type.

参数

type_ (mindspore.dtype) – MindSpore’s dtype.

返回

Type of python.

tinyms.pytype_to_dtype(obj)[源代码]

Convert python type to MindSpore type.

参数

obj (type) – A python type object.

返回

Type of MindSpore type.

tinyms.get_py_obj_dtype(obj)[源代码]

Get the MindSpore data type, which corresponds to python type or variable.

参数

obj (type) – An object of python type, or a variable of python type.

返回

Type of MindSpore type.

class tinyms.Tensor(input_data=None, dtype=None, shape=None, init=None)[源代码]

Tensor is used for data storage.

Tensor inherits tensor object in C++. Some functions are implemented in C++ and some functions are implemented in Python.

参数
  • input_data (Union[Tensor, float, int, bool, tuple, list, numpy.ndarray]) – Input data of the tensor.

  • dtype (mindspore.dtype) – Input data should be None, bool or numeric type defined in mindspore.dtype. The argument is used to define the data type of the output tensor. If it is None, the data type of the output tensor will be the same as the input_data. Default: None.

  • shape (Union[tuple, list, int]) – A list of integers, a tuple of integers or an integer as the shape of output. If input_data is available, shape doesn’t need to be set. Default: None.

  • init (Initializer) – The information of init data. ‘init’ is used for delayed initialization in parallel mode. Usually, it is not recommended to use ‘init’ interface to initialize parameters in other conditions. If ‘init’ interface is used to initialize parameters, the Tensor.init_data API needs to be called to convert Tensor to the actual data.

Outputs:

Tensor. If dtype and shape are not set, return a tensor with the same dtype and shape as input_data. If dtype or shape is set, the dtype or shape of the output Tensor is consistent with the setting.

实际案例

>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import Tensor
>>> from mindspore.common.initializer import One
>>> # initialize a tensor with input data
>>> t1 = Tensor(np.zeros([1, 2, 3]), ms.float32)
>>> assert isinstance(t1, Tensor)
>>> assert t1.shape == (1, 2, 3)
>>> assert t1.dtype == ms.float32
>>>
>>> # initialize a tensor with a float scalar
>>> t2 = Tensor(0.1)
>>> assert isinstance(t2, Tensor)
>>> assert t2.dtype == ms.float64
...
>>> # initialize a tensor with init
>>> t3 = Tensor(shape = (1, 3), dtype=ms.float32, init=One())
>>> assert isinstance(t3, Tensor)
>>> assert t3.shape == (1, 3)
>>> assert t3.dtype == ms.float32
property T

Return the transposed tensor.

abs()[源代码]

Return absolute value element-wisely.

返回

Tensor, with absolute value element-wisely.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import Tensor
>>> a = Tensor([1.1, -2.1]).astype("float32")
>>> output = a.abs()
>>> print(output)
[1.1 2.1]
all(axis=(), keep_dims=False)[源代码]

Check all array elements along a given axis evaluate to True.

参数
  • axis (Union[None, int, tuple(int)) – Dimensions of reduction, when the axis is None or empty tuple, reduce all dimensions. Default: ().

  • keep_dims (bool) – Whether to keep the reduced dimensions. Default: False.

返回

Tensor, if all array elements along the given axis evaluate to True, its value is True, otherwise its value is False. If the axis is None or empty tuple, reduce all dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import Tensor
>>> a = Tensor([True, True, False])
>>> output = a.all()
>>> print(output)
False
any(axis=(), keep_dims=False)[源代码]

Check any array element along a given axis evaluate to True.

参数
  • axis (Union[None, int, tuple(int)) – Dimensions of reduction, when the axis is None or empty tuple, reduce all dimensions. Default: ().

  • keep_dims (bool) – Whether to keep the reduced dimensions. Default: False.

返回

Tensor, if any array element along the given axis evaluates to True, its value is True, otherwise its value is False. If the axis is None or empty tuple, reduce all dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import Tensor
>>> a = Tensor([True, True, False])
>>> output = a.any()
>>> print(output)
True
argmax(axis=None)[源代码]

Return the indices of the maximum values along an axis.

参数

axis (int, optional) – By default, the index is into the flattened tensor, otherwise along the specified axis.

返回

Tensor, indices into the input tensor. It has the same shape as self.shape with the dimension along axis removed.

引发

ValueError – if the axis is out of range.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.arange(10, 16).reshape(2, 3).astype("float32"))
>>> print(a.argmax())
5
argmin(axis=None)[源代码]

Return the indices of the minimum values along an axis.

参数

axis (int, optional) – By default, the index is into the flattened tensor, otherwise along the specified axis.

返回

Tensor, indices into the input tensor. It has the same shape as self.shape with the dimension along axis removed.

引发

ValueError – if the axis is out of range.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.arange(10, 16).reshape(2, 3).astype("float32"))
>>> print(a.argmin())
0
asnumpy()[源代码]

Convert tensor to numpy array.

assign_value_cpp(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.Tensor) → mindspore._c_expression.Tensor

Assign another tensor value to this.

Arg:

value (mindspore.tensor): The value tensor.

实际案例

>>> data = mindspore.Tensor(np.ones((1, 2), np.float32))
>>> data2 = mindspore.Tensor(np.ones((2, 2), np.float32))
>>> data.assign_value(data2)
>>> data.shape
(2, 2)
astype(dtype, copy=True)[源代码]

Return a copy of the tensor, cast to a specified type.

参数
  • dtype (Union[mindspore.dtype, str]) – Designated tensor dtype, can be in format of mindspore.dtype.float32 or float32. Default: mindspore.dtype.float32.

  • copy (bool, optional) – By default, astype always returns a newly allocated tensor. If this is set to false, the input tensor is returned instead of a copy if possible. Default: True.

返回

Tensor, with the designated dtype.

引发

TypeError – If dtype has types not specified above, or values cannot be understood.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((1,2,2,1), dtype=np.float32))
>>> x = x.astype("int32")
>>> print(x.dtype)
Int32
choose(choices, mode='clip')[源代码]

Construct an array from an index array and a list of arrays to choose from.

参数
  • choices (Union[tuple, list, Tensor]) – Choice arrays. a and all of the choices must be broadcasted to the same shape. If choices is itself an array, then its outermost dimension (i.e., the one corresponding to choices.shape[0]) is taken as defining the “sequence”.

  • mode (‘raise’, ‘wrap’, ‘clip’, optional) –

    Specifies how indices outside [0, n-1] will be treated:

    ‘raise’ – raise an error (default);

    ‘wrap’ – wrap around;

    ‘clip’ – clip to the range. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

返回

Tensor, the merged result.

Supported Platforms:

Ascend GPU CPU

引发

ValueError – if the input tensor and any of the choices cannot be broadcast.

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]]
>>> x = Tensor(np.array([2, 3, 1, 0]))
>>> print(x.choose(choices))
[20 31 12  3]
clip(xmin, xmax, dtype=None)[源代码]

Clips (limits) the values in a Tensor.

Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of \([0, 1]\) is specified, values smaller than 0 become 0, and values larger than 1 become 1.

注解

Currently, clip with xmin=nan or xmax=nan is not supported.

参数
  • xmin (Tensor, scalar, None) – Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of xmin and xmax may be None.

  • xmax (Tensor, scalar, None) – Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of xmin and xmax may be None. If xmin or xmax are tensors, then the three tensors will be broadcasted to match their shapes.

  • dtype (mindspore.dtype, optional) – Overrides the dtype of the output Tensor. Default is None.

返回

Tensor, a tensor with the elements of input tensor, but where values < xmin are replaced with xmin, and those > xmax with xmax.

引发
  • TypeError – If inputs have types not specified above.

  • ValueError – If the shapes of x1 and x2 cannot broadcast, or both xmin and xmax are None.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import Tensor
>>> x = Tensor([1, 2, 3, -4, 0, 3, 2, 0]).astype("float32")
>>> output = x.clip(0, 2)
>>> print(output)
[1. 2. 2. 0. 0. 2. 2. 0.]
copy()[源代码]

Return a copy of the tensor.

注解

The current implementation does not support order argument.

返回

Copied tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.ones((3,3)).astype("float32"))
>>> output = a.copy()
>>> print(output)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
cumsum(axis=None, dtype=None)[源代码]

Return the cumulative sum of the elements along a given axis.

注解

If self.dtype is int8, int16 or bool, the result dtype will be elevated to int32, int64 is not supported.

参数
  • axis (int, optional) – Axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array.

  • dtype (mindspore.dtype, optional) – If not specified, stay the same as original, tensor, unless it has an integer dtype with a precision less than float32. In that case, float32 is used. Default: None.

引发

ValueError – if the axis is out of range.

返回

Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.ones((3,3)).astype("float32"))
>>> output = a.cumsum(axis=0)
>>> print(output)
[[1. 1. 1.]
[2. 2. 2.]
[3. 3. 3.]]
data_sync(self: mindspore._c_expression.Tensor, arg0: bool) → None
diagonal(offset=0, axis1=0, axis2=1)[源代码]

Return specified diagonals.

参数
  • offset (int, optional) – Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal.

  • axis1 (int, optional) – Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0).

  • axis2 (int, optional) – Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis.

返回

Tensor, if a is 2-D, then a 1-D array containing the diagonal.

引发

ValueError – if the input tensor has less than two dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.arange(4).reshape(2, 2))
>>> print(a)
[[0 1]
[2 3]]
>>> output = a.diagonal()
>>> print(output)
[0 3]
dim(self: mindspore._c_expression.Tensor) → int

Get tensor’s data dimension.

返回

int, the dimension of tensor.

实际案例

>>> data = mindspore.Tensor(np.ones((2, 3)))
>>> data.dim()
2
property dtype

Return the dtype of the tensor (mindspore.dtype).

expand_as(x)[源代码]

Expand the dimension of target tensor to the dimension of input tensor.

参数

x (Tensor) – The input tensor. The shape of input tensor must obey the broadcasting rule.

返回

Tensor, has the same dimension as input tensor.

fill(value)[源代码]

Fill the array with a scalar value.

注解

Unlike Numpy, tensor.fill() will always returns a new tensor, instead of filling the original tensor.

参数

value (Union[None, int, float, bool]) – All elements of a will be assigned this value.

返回

Tensor, with the original dtype and shape as input tensor.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.arange(4).reshape((2,2)).astype('float32'))
>>> print(a.fill(1.0))
[[1. 1.]
[1. 1.]]
flatten(order='C')[源代码]

Return a copy of the tensor collapsed into one dimension.

参数

order (str, optional) – Can choose between ‘C’ and ‘F’. ‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran-style) order. Only ‘C’ and ‘F’ are supported. Default: ‘C’.

返回

Tensor, has the same data type as input.

Supported Platforms:

Ascend GPU CPU

引发
  • TypeError – If order is not string type.

  • ValueError – If order is string type, but not ‘C’ or ‘F’.

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((2,3,4), dtype=np.float32))
>>> output = x.flatten()
>>> print(output.shape)
(24,)
flush_from_cache()[源代码]

Flush cache data to host if tensor is cache enable.

static from_numpy(array)[源代码]

Convert numpy array to Tensor without copy data.

参数

array (numpy.array) – The input array.

返回

Tensor, has the same data type as input array.

property has_init

tensor is inited.

init_data(slice_index=None, shape=None, opt_shard_group=None)[源代码]

Get the tensor format data of this Tensor. The init_data function can be called once for the same tensor.

参数
  • slice_index (int) – Slice index of a parameter’s slices. It is used when initialize a slice of a parameter, it guarantees that devices using the same slice can generate the same tensor. Default: None.

  • shape (list[int]) – Shape of the slice, it is used when initialize a slice of the parameter. Default: None.

  • opt_shard_group (str) – Optimizer shard group which is used in auto or semi auto parallel mode to get one shard of a parameter’s slice. Default: None.

返回

Initialized Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore as ms
>>> import mindspore.common.initializer as init
>>> x = init.initializer(init.Constant(1), [2, 2], ms.float32)
>>> out = x.init_data()
>>> print(out)
[[1. 1.]
 [1. 1.]]
is_init(self: mindspore._c_expression.Tensor) → bool

Get tensor init_flag.

返回

bool, whether the tensor init.

实际案例

>>> data = mindspore.Tensor(np.ones((2, 3)))
>>> data.is_init()
False
item(index=None)[源代码]

Getitem from the Tensor with the index.

注解

Tensor.item returns a Tensor scalar instead of a Python scalar.

参数

index (Union[None, int, tuple(int)]) – The index in Tensor. Default: None.

返回

A Tensor scalar, dtype is the same with the original Tensor.

引发

ValueError – If the length of the index is not euqal to self.ndim.

Supported Platforms:

Ascend GPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32))
>>> x = x.item((0,1))
>>> print(x)
2.0
itemset(*args)[源代码]

Insert scalar into a tensor (scalar is cast to tensor’s dtype, if possible).

There must be at least 1 argument, and define the last argument as item. Then, tensor.itemset(*args) is equivalent to \(tensor[args] = item\).

参数

args (Union[(numbers.Number), (int/tuple(int), numbers.Number)]) – The arguments that specify the index and value. If args contain one argument (a scalar), it is only used in case tensor is of size 1. If args contain two arguments, the last argument is the value to be set and must be a scalar, the first argument specifies a single tensor element location. It is either an int or a tuple.

返回

A new Tensor, with value set by \(tensor[args] = item\).

引发
  • ValueError – If the length of the first argument is not euqal to self.ndim.

  • IndexError – If only one argument is provided, and the original Tensor is not scalar.

Supported Platforms:

Ascend GPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32))
>>> x = x.itemset((0,1), 4)
>>> print(x)
[[1. 4. 3.]
[4. 5. 6.]]
property itemsize

Return the length of one tensor element in bytes.

max(axis=None, keepdims=False, initial=None, where=True)[源代码]

Return the maximum of a tensor or maximum along an axis.

参数
  • axis (Union[None, int, tuple of ints], optional) – Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of ints, the maximum is selected over multiple axes, instead of a single axis or all the axes as before. Default: None.

  • keepdims (bool, optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. Default: False.

  • initial (scalar, optional) – The minimum value of an output element. Must be present to allow computation on empty slice. Default: None.

  • where (bool Tensor, optional) – A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided. Default: True.

返回

Tensor or scalar, maximum of input tensor. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension self.ndim - 1.

引发

TypeError – if arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.arange(4).reshape((2, 2)).astype('float32'))
>>> output = a.max()
>>> print(output)
3.0
mean(axis=(), keep_dims=False)[源代码]

Reduce a dimension of a tensor by averaging all elements in the dimension.

参数
  • axis (Union[None, int, tuple(int), list(int)]) – Dimensions of reduction, when the axis is None or empty tuple, reduce all dimensions. Default: ().

  • keep_dims (bool) – Whether to keep the reduced dimensions. Default: False.

返回

Tensor, has the same data type as input tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> input_x = Tensor(np.array([1, 2, 3], dtype=np.float32))
>>> output = input_x.mean()
>>> print(output)
2.0
min(axis=None, keepdims=False, initial=None, where=True)[源代码]

Return the minimum of a tensor or minimum along an axis.

参数
  • axis (Union[None, int, tuple of ints], optional) – Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of ints, the minimum is selected over multiple axes, instead of a single axis or all the axes as before. Default: None.

  • keepdims (bool, optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. Default: False.

  • initial (scalar, optional) – The maximum value of an output element. Must be present to allow computation on empty slice. Default: None.

  • where (bool Tensor, optional) – A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided. Default: True.

返回

Tensor or scalar, minimum of input tensor. If the axis is None, the result is a scalar value. If axis is given, the result is an array of dimension self.ndim - 1.

引发

TypeError – if arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> import mindspore.numpy as np
>>> a = Tensor(np.arange(4).reshape((2,2)).astype('float32'))
>>> output = a.min()
>>> print(output)
0.0
property nbytes

Return the total number of bytes taken by the tensor.

property ndim

Return the number of tensor dimensions.

ptp(axis=None, keepdims=False)[源代码]

The name of the function comes from the acronym for ‘peak to peak’.

注解

Numpy arguments dtype and out are not supported.

参数
  • axis (Union[None, int, tuple(int)]) – Axis or axes along which the range is computed. The default is to compute the variance of the flattened array. Default: None.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array. Default is False.

返回

Tensor.

引发

TypeError – if self is not a tensor, or axis and keepdims have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import Tensor
>>> x = Tensor([[4.0, 9.0, 2.0, 10.0], [6.0, 9.0, 7.0, 12.0]]).astype("float32")
>>> print(x.ptp(axis=1))
[8. 6.]
>>> print(x.ptp(axis=0))
[2. 0. 5. 2.]
ravel()[源代码]

Return a contiguous flattened tensor.

返回

Tensor, a 1-D tensor, containing the same elements of the input.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((2,3,4), dtype=np.float32))
>>> output = x.ravel()
>>> print(output.shape)
(24,)
repeat(repeats, axis=None)[源代码]

Repeat elements of an array.

参数
  • repeats (Union[int, tuple, list]) – The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.

  • axis (int, optional) – The axis along which to repeat values. By default, use the flattened input tensor, and return a flat output tensor.

返回

Tensor, has the same shape as input tensor except along the given axis.

引发
  • ValueError – if the axis is out of range.

  • TypeError – if arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array(3))
>>> print(x.repeat(4))
[3 3 3 3]
>>> x = Tensor(np.array([[1, 2],[3, 4]]))
>>> print(x.repeat(2))
[1 1 2 2 3 3 4 4]
>>> print(x.repeat(3, axis=1))
[[1 1 1 2 2 2]
[3 3 3 4 4 4]]
>>> print(x.repeat([1,2], axis=0))
[[1 2]
[3 4]
[3 4]]
reshape(*shape)[源代码]

Give a new shape to a tensor without changing its data.

参数

shape (Union[int, tuple(int), list(int)]) – The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

返回

Tensor, with new specified shape.

引发
  • TypeError – If new_shape is not integer, list or tuple, or x is not tensor.

  • ValueError – If new_shape is not compatible with the original shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import Tensor
>>> from mindspore import dtype as mstype
>>> x = Tensor([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]], dtype=mstype.float32)
>>> output = x.reshape((3, 2))
>>> print(output)
[[-0.1  0.3]
[ 3.6  0.4]
[ 0.5 -3.2]]
resize(*new_shape)[源代码]

Changes shape and size of array in-place.

注解

Instead of changing the size of the input array and returns nothing as in numpy, this method returns a new Tensor with the input size. Numpy argument refcheck is not supported.

参数

new_shape (Union[ints, tuple of ints]) – Shape of resized array.

返回

Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[0, 1], [2, 3]]))
>>> x = x.resize(2, 3)
>>> print(x)
[[0 1 2]
[3 0 0]]
searchsorted(v, side='left', sorter=None)[源代码]

Finds indices where elements should be inserted to maintain order.

参数
  • v (Union[int, float, bool, list, tuple, Tensor]) – Values to insert into a.

  • side ('left', 'right', optional) – If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of a). Default: left.

  • sorter (Union[int, float, bool, list, tuple, Tensor]) – 1-D optional array of integer indices that sort array a into ascending order. They are typically the result of argsort.

返回

Tensor, array of insertion points with the same shape as v.

引发

ValueError – if argument for side or sorter is invalid.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([1, 2, 3, 4, 5]))
>>> print(x.searchsorted(3))
2
set_cast_dtype(self: mindspore._c_expression.Tensor, dtype: mindspore::Type = None) → None
set_dtype(self: mindspore._c_expression.Tensor, arg0: mindspore::Type) → mindspore::Type

Set the tensor’s data type.

Arg:

dtype (mindspore.dtype): The type of output tensor.

实际案例

>>> data = mindspore.Tensor(np.ones((1, 2), np.float32))
>>> data.set_dtype(mindspore.int32)
mindspore.int32
set_init_flag(self: mindspore._c_expression.Tensor, arg0: bool) → None

Set tensor init_flag.

实际案例

>>> data = mindspore.Tensor(np.ones((2, 3)))
>>> data.set_init_flag(True)
property shape

Returns the shape of the tensor as a tuple.

property size

Returns the total number of elements in tensor.

squeeze(axis=None)[源代码]

Remove single-dimensional entries from the shape of a tensor.

参数

axis (Union[None, int, list(int), tuple(int)], optional) – Selects a subset of the entries of length one in the shape. If an axis is selected with shape entry greater than one, an error is raised. Default is None.

返回

Tensor, with all or a subset of the dimensions of length 1 removed.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If specified axis has shape entry \(> 1\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((1,2,2,1), dtype=np.float32))
>>> x = x.squeeze()
>>> print(x.shape)
(2, 2)
std(axis=None, ddof=0, keepdims=False)[源代码]

Compute the standard deviation along the specified axis. The standard deviation is the square root of the average of the squared deviations from the mean, i.e., \(std = sqrt(mean(abs(x - x.mean())**2))\).

Return the standard deviation, which is computed for the flattened array by default, otherwise over the specified axis.

注解

Numpy arguments dtype, out and where are not supported.

参数
  • axis (Union[None, int, tuple(int)]) –

    Axis or axes along which the standard deviation is computed. Default: None.

    If None, compute the standard deviation of the flattened array.

  • ddof (int) – Means Delta Degrees of Freedom. The divisor used in calculations is \(N - ddof\), where \(N\) represents the number of elements. Default: 0.

  • keepdims – Default: False.

返回

Standard deviation tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> input_x = Tensor(np.array([1, 2, 3, 4], dtype=np.float32))
>>> output = input_x.std()
>>> print(output)
1.118034
property strides

Return the tuple of bytes to step in each dimension when traversing a tensor.

sum(axis=None, dtype=None, keepdims=False, initial=None)[源代码]

Return sum of array elements over a given axis.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • axis (Union[None, int, tuple(int)]) – Axis or axes along which a sum is performed. Default: None. If None, sum all of the elements of the input array. If the axis is negative, it counts from the last to the first axis. If the axis is a tuple of ints, a sum is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised. Default: False.

  • initial (scalar) – Starting value for the sum. Default: None.

返回

Tensor. A tensor with the same shape as input, with the specified axis removed. If input tensor is a 0-d array, or if the axis is None, a scalar is returned.

引发
  • TypeError – If input is not array_like, or axis is not int or tuple of ints, or keepdims is not integer, or initial is not scalar.

  • ValueError – If any axis is out of range or duplicate axes exist.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> input_x = Tensor(np.array([-1, 0, 1]).astype(np.float32))
>>> print(input_x.sum())
0.0
>>> input_x = Tensor(np.arange(10).reshape(2, 5).astype(np.float32))
>>> print(input_x.sum(axis=1))
[10. 35.]
swapaxes(axis1, axis2)[源代码]

Interchange two axes of a tensor.

参数
  • axis1 (int) – First axis.

  • axis2 (int) – Second axis.

返回

Transposed tensor, has the same data type as the input.

引发
  • TypeError – If axis1 or axis2 is not integer.

  • ValueError – If axis1 or axis2 is not in the range of \([-ndim, ndim-1]\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((2,3,4), dtype=np.float32))
>>> output = x.swapaxes(0, 2)
>>> print(output.shape)
(4,3,2)
take(indices, axis=None, mode='clip')[源代码]

Takes elements from an array along an axis.

参数
  • indices (Tensor) – The indices with shape (Nj…) of the values to extract.

  • axis (int, optional) – The axis over which to select values. By default, the flattened input array is used. Default: None.

  • mode (‘raise’, ‘wrap’, ‘clip’, optional) –

    • edge: Pads with the edge values of arr.

    • raise: Raises an error;

    • wrap: Wraps around;

    • clip: Clips to the range. clip mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

    Default: clip.

返回

Tensor, the indexed result.

引发

ValueError – if axis is out of range, or mode has values other than (‘raise’, ‘wrap’, ‘clip’)

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.array([4, 3, 5, 7, 6, 8]))
>>> indices = Tensor(np.array([0, 1, 4]))
>>> output = a.take(indices)
>>> print(output)
[4 3 6]
to_tensor(slice_index=None, shape=None, opt_shard_group=None)[源代码]

Return init_data() and get the tensor format data of this Tensor.

注解

The usage of to_tensor is deprecated. Please use init_data.

参数
  • slice_index (int) – Slice index of a parameter’s slices. It is used when initialize a slice of a parameter, it guarantees that devices using the same slice can generate the same tensor. Default: None.

  • shape (list[int]) – Shape of the slice, it is used when initialize a slice of the parameter. Default: None.

  • opt_shard_group (str) – Optimizer shard group which is used in auto or semi auto parallel mode to get one shard of a parameter’s slice. Default: None.

返回

Initialized Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore as ms
>>> import mindspore.common.initializer as init
>>> x = init.initializer(init.Constant(1), [2, 2], ms.float32)
>>> out = x.to_tensor()
>>> print(out)
[[1. 1.]
 [1. 1.]]
trace(offset=0, axis1=0, axis2=1, dtype=None)[源代码]

Return the sum along diagonals of the array.

参数
  • offset (int, optional) – Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal.

  • axis1 (int, optional) – Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0).

  • axis2 (int, optional) – Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor, sum_along_diagonals.

引发

ValueError – if the input tensor has less than two dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.eye(3, dtype=np.float32))
>>> print(x.trace())
3.0
transpose(*axes)[源代码]

Return a view of the tensor with axes transposed.

  • For a 1-D tensor this has no effect, as a transposed vector is simply the same vector.

  • For a 2-D tensor, this is a standard matrix transpose.

  • For an n-D tensor, if axes are given, their order indicates how the axes are permuted.

If axes are not provided and tensor.shape = (i[0], i[1],...i[n-2], i[n-1]), then tensor.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0]).

参数

axes (Union[None, tuple(int), list(int), int], optional) – If axes is None or blank, the method will reverse the order of the axes. If axes is tuple(int) or list(int), tensor.transpose() will transpose the tensor to the new axes order. If axes is int, this form is simply intended as a convenience alternative to the tuple/list form.

返回

Tensor, has the same dimension as input tensor, with axes suitably permuted.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If the number of axes is not euqal to a.ndim.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((1,2,3), dtype=np.float32))
>>> x = x.transpose()
>>> print(x.shape)
(3, 2, 1)
var(axis=None, ddof=0, keepdims=False)[源代码]

Compute the variance along the specified axis.

The variance is the average of the squared deviations from the mean, i.e., \(var = mean(abs(x - x.mean())**2)\).

Return the variance, which is computed for the flattened array by default, otherwise over the specified axis.

注解

Numpy arguments dtype, out and where are not supported.

参数
  • axis (Union[None, int, tuple(int)]) – Axis or axes along which the variance is computed. The default is to compute the variance of the flattened array. Default: None.

  • ddof (int) – Means Delta Degrees of Freedom. Default: 0. The divisor used in calculations is \(N - ddof\), where \(N\) represents the number of elements.

  • keepdims (bool) – Default: False.

Supported Platforms:

Ascend GPU CPU

返回

Standard deviation tensor.

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> input_x = Tensor(np.array([1., 2., 3., 4.], np.float32))
>>> output = input_x.var()
>>> print(output)
1.25
view(*shape)[源代码]

Reshape the tensor according to the input shape.

参数

shape (Union[tuple(int), int]) – Dimension of the output tensor.

返回

Tensor, has the same dimension as the input shape.

property virtual_flag

Used to mark whether the tensor is virtual. If the tensor is virtual, return True.

class tinyms.RowTensor(indices, values, dense_shape)[源代码]

A sparse representation of a set of tensor slices at given indices.

An RowTensor is typically used to represent a subset of a larger tensor dense of shape [L0, D1, .. , DN] where L0 >> D0.

The values in indices are the indices in the first dimension of the slices that have been extracted from the larger tensor.

The dense tensor dense represented by an RowTensor slices has dense[slices.indices[i], :, :, :, …] = slices.values[i, :, :, :, …].

RowTensor can only be used in the Cell’s construct method.

It is not supported in pynative mode at the moment.

参数
  • indices (Tensor) – A 1-D integer Tensor of shape [D0].

  • values (Tensor) – A Tensor of any dtype of shape [D0, D1, …, Dn].

  • dense_shape (tuple(int)) – An integer tuple which contains the shape of the corresponding dense tensor.

返回

RowTensor, composed of indices, values, and dense_shape.

实际案例

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> from mindspore import RowTensor
>>> class Net(nn.Cell):
...     def __init__(self, dense_shape):
...         super(Net, self).__init__()
...         self.dense_shape = dense_shape
...     def construct(self, indices, values):
...         x = RowTensor(indices, values, self.dense_shape)
...         return x.values, x.indices, x.dense_shape
>>>
>>> indices = Tensor([0])
>>> values = Tensor([[1, 2]], dtype=ms.float32)
>>> out = Net((3, 2))(indices, values)
>>> print(out[0])
[[1. 2.]]
>>> print(out[1])
[0]
>>> print(out[2])
(3, 2)
class tinyms.SparseTensor(indices, values, dense_shape)[源代码]

A sparse representation of a set of nonzero elememts from a tensor at given indices.

SparseTensor can only be used in the Cell’s construct method.

Pynative mode not supported at the moment.

For a tensor dense, its SparseTensor(indices, values, dense_shape) has dense[indices[i]] = values[i].

参数
  • indices (Tensor) – A 2-D integer Tensor of shape [N, ndims], where N and ndims are the number of values and number of dimensions in the SparseTensor, respectively.

  • values (Tensor) – A 1-D tensor of any type and shape [N], which supplies the values for each element in indices.

  • dense_shape (tuple(int)) – A integer tuple of size ndims, which specifies the dense_shape of the sparse tensor.

返回

SparseTensor, composed of indices, values, and dense_shape.

实际案例

>>> import mindspore as ms
>>> import mindspore.nn as nn
>>> from mindspore import SparseTensor
>>> class Net(nn.Cell):
...     def __init__(self, dense_shape):
...         super(Net, self).__init__()
...         self.dense_shape = dense_shape
...     def construct(self, indices, values):
...         x = SparseTensor(indices, values, self.dense_shape)
...         return x.values, x.indices, x.dense_shape
>>>
>>> indices = Tensor([[0, 1], [1, 2]])
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> out = Net((3, 4))(indices, values)
>>> print(out[0])
[1. 2.]
>>> print(out[1])
[[0 1]
 [1 2]]
>>> print(out[2])
(3, 4)
tinyms.ms_function(fn=None, obj=None, input_signature=None)[源代码]

Create a callable MindSpore graph from a Python function.

This allows the MindSpore runtime to apply optimizations based on graph.

参数
  • fn (Function) – The Python function that will be run as a graph. Default: None.

  • obj (Object) – The Python object is used to distinguish the compiled function. Default: None.

  • input_signature (Tensor) – The Tensor which describes the input arguments. The shape and dtype of the Tensor will be supplied to this function. If input_signature is specified, each input to fn must be a Tensor. And the input parameters of fn cannot accept **kwargs. The shape and dtype of actual inputs should keep the same as input_signature. Otherwise, TypeError will be raised. Default: None.

返回

Function, if fn is not None, returns a callable function that will execute the compiled function; If fn is None, returns a decorator and when this decorator invokes with a single fn argument, the callable function is equal to the case when fn is not None.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindspore import ms_function
...
>>> x = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
>>> y = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
...
>>> # create a callable MindSpore graph by calling ms_function
>>> def tensor_add(x, y):
...     z = x + y
...     return z
...
>>> tensor_add_graph = ms_function(fn=tensor_add)
>>> out = tensor_add_graph(x, y)
...
>>> # create a callable MindSpore graph through decorator @ms_function
>>> @ms_function
... def tensor_add_with_dec(x, y):
...     z = x + y
...     return z
...
>>> out = tensor_add_with_dec(x, y)
...
>>> # create a callable MindSpore graph through decorator @ms_function with input_signature parameter
>>> @ms_function(input_signature=(Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)),
...                               Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))))
... def tensor_add_with_sig(x, y):
...     z = x + y
...     return z
...
>>> out = tensor_add_with_sig(x, y)
class tinyms.Parameter(default_input, name=None, requires_grad=True, layerwise_parallel=False, parallel_optimizer=True)[源代码]

An object holding weights of cells, after initialized Parameter is a subtype of Tensor.

注解

In auto_parallel mode of “semi_auto_parallel” and “auto_parallel”, if init Parameter by a Tensor, the type of Parameter will be Tensor. Tensor will save the shape and type info of a tensor with no memory usage. The shape can be changed while compiling for auto-parallel. Call init_data will return a Tensor Parameter with initialized data. If there is an operator in the network that requires part of the inputs to be Parameter, then the Parameters as this part of the inputs are not allowed to be cast. It is recommended to use the default value of name when initialize a parameter as one attribute of a cell, otherwise, the parameter name may be different from expected.

参数
  • default_input (Union[Tensor, int, float, numpy.ndarray, list]) – Parameter data, to initialize the parameter data.

  • name (str) – Name of the child parameter. Default: None.

  • requires_grad (bool) – True if the parameter requires gradient. Default: True.

  • layerwise_parallel (bool) – When layerwise_parallel is true in data/hybrid parallel mode, broadcast and gradients communication would not be applied to parameters. Default: False.

  • parallel_optimizer (bool) – It is used to filter the weight shard operation in semi auto or auto parallel mode. It works only when enable parallel optimizer in mindspore.context.set_auto_parallel_context(). Default: True.

实际案例

>>> import numpy as np
>>> from mindspore import Parameter, Tensor
>>> import mindspore.ops as ops
>>> import mindspore.nn as nn
>>> import mindspore
>>>
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.matmul = ops.MatMul()
...         self.weight = Parameter(Tensor(np.ones((1, 2)), mindspore.float32), name="w", requires_grad=True)
...
...     def construct(self, x):
...         out = self.matmul(self.weight, x)
...         return out
>>> net = Net()
>>> x = Tensor(np.ones((2, 1)), mindspore.float32)
>>> print(net(x))
[[2.]]
>>> net.weight.set_data(Tensor(np.zeros((1, 2)), mindspore.float32))
>>> print(net(x))
[[0.]]
asnumpy(self: mindspore._c_expression.Tensor) → array

Convert tensor to numpy.ndarray.

返回

numpy.ndarray.

实际案例

>>> data = mindspore.Tensor(np.ones((2, 3)))
>>> array = data.asnumpy()
>>> array
array([[1., 1., 1.],
       [1., 1., 1.]])
assign_value_cpp(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.Tensor) → mindspore._c_expression.Tensor

Assign another tensor value to this.

Arg:

value (mindspore.tensor): The value tensor.

实际案例

>>> data = mindspore.Tensor(np.ones((1, 2), np.float32))
>>> data2 = mindspore.Tensor(np.ones((2, 2), np.float32))
>>> data.assign_value(data2)
>>> data.shape
(2, 2)
property cache_enable

Return whether the parameter is cache enable.

property cache_shape

Return the cache shape corresponding to the parameter if use cache.

clone(init='same')[源代码]

Clone the parameter.

参数

init (Union[Tensor, str, numbers.Number]) – Initialize the shape and dtype of the parameter. If init is a Tensor or numbers.Number, clone a new parameter with the same shape and dtype, and the data of the new parameter will be set according to init. If init is a str, the init should be the alias of the class inheriting from Initializer. For example, if init is ‘same’, clone a new parameter with the same data, shape, and dtype. Default: ‘same’.

返回

Parameter, a new parameter.

property comm_fusion

Get and set the fusion type (int) for communication operators corresponding to this parameter.

In AUTO_PARALLEL and SEMI_AUTO_PARALLEL mode, some communication operators used for parameters or gradients aggregation are inserted automatically. Set the fusion type for communication operators generated for this parameter. The value of fusion must be greater than or equal to 0. When the value of fusion is 0, operators will not be fused together.

Only support in Ascend environment with Graph mode.

property data

Return the parameter object.

data_sync(self: mindspore._c_expression.Tensor, arg0: bool) → None
dim(self: mindspore._c_expression.Tensor) → int

Get tensor’s data dimension.

返回

int, the dimension of tensor.

实际案例

>>> data = mindspore.Tensor(np.ones((2, 3)))
>>> data.dim()
2
property dtype

Get the MetaTensor’s dtype.

from_numpy(self: array) → mindspore._c_expression.Tensor

Creates a Tensor from a numpy.ndarray without copy.

Arg:

array (numpy.ndarray): The input ndarray.

返回

Tensor, tensor with shared data to input ndarray.

实际案例

>>> a = np.ones((2, 3))
>>> t = mindspore.Tensor.from_numpy(a)
init_data(layout=None, set_sliced=False)[源代码]

Initialize the parameter’s data.

参数
  • layout (Union[None, tuple(list(int))]) –

    Parameter slice layout [dev_mat, tensor_map, slice_shape]. Default: None.

    • dev_mat (list(int)): Device matrix.

    • tensor_map (list(int)): Tensor map.

    • slice_shape (list(int)): Shape of slice.

  • set_sliced (bool) – True if the parameter is set sliced after initializing the data. Default: False.

引发
  • RuntimeError – If it is from Initializer, and parallel mode has changed after the Initializer created.

  • ValueError – If the length of the layout is less than 3.

  • TypeError – If layout is not tuple.

返回

Parameter, the Parameter after initializing data. If current Parameter was already initialized before, returns the same initialized Parameter.

property inited_param

Get the new parameter after call the init_data.

Default is a None, If self is a Parameter without data, after call the init_data the initialized Parameter with data will be recorded here.

property is_init

Get the initialization status of the parameter.

This flag only work in GE, and it will be set to False in other backend.

property layerwise_parallel

When layerwise_parallel is true in data/hybrid parallel mode, broadcast and gradients communication would not be applied to parameters.

property name

Get the name of the parameter.

property parallel_optimizer

It is used to filter the weight shard operation in semi auto or auto parallel mode. It works only when enable parallel optimizer in mindspore.context.set_auto_parallel_context().

property parallel_optimizer_comm_recompute

Get and Set the whether do recompute for communication operators corresponding to this parameter when applying parallel optimizer.

In AUTO_PARALLEL and SEMI_AUTO_PARALLEL mode, when applying parallel optimizer, some all_gather operators used for parameters gathering are inserted automatically. The interface is used to control the recompute attr for those all_gather operators.

注解

  • Only Ascend and Graph mode is supported.

  • It is recommended to use cell.recompute(parallel_optimizer_comm_recompute=True/False) to configure the all_gather operators introducing by parallel optimizer rather than using this interface directly.

property requires_grad

Return whether the parameter requires gradient.

set_cast_dtype(self: mindspore._c_expression.Tensor, dtype: mindspore::Type = None) → None
set_data(data, slice_shape=False)[源代码]

Set Parameter’s data.

参数
  • data (Union[Tensor, int, float]) – new data.

  • slice_shape (bool) – If slice the parameter is set to true, the shape is not checked for consistency. Default: False.

返回

Parameter, the parameter after set data.

set_dtype(self: mindspore._c_expression.Tensor, arg0: mindspore::Type) → mindspore::Type

Set the tensor’s data type.

Arg:

dtype (mindspore.dtype): The type of output tensor.

实际案例

>>> data = mindspore.Tensor(np.ones((1, 2), np.float32))
>>> data.set_dtype(mindspore.int32)
mindspore.int32
set_init_flag(self: mindspore._c_expression.Tensor, arg0: bool) → None

Set tensor init_flag.

实际案例

>>> data = mindspore.Tensor(np.ones((2, 3)))
>>> data.set_init_flag(True)
set_param_fl(push_to_server=False, pull_from_server=False, requires_aggr=True)[源代码]

Set the way of parameter and server interaction.

参数
  • push_to_server (bool) – Whether the parameter should be pushed to server. Default: False.

  • pull_from_server (bool) – Whether the parameter should be pulled from server. Default: False.

  • requires_aggr (bool) – Whether the parameter should be aggregated in the server. Default: True.

set_param_ps(init_in_server=False)[源代码]

Set whether the trainable parameter is updated by parameter server and whether the trainable parameter is initialized on server.

注解

It only works when a running task is in the parameter server mode.

参数

init_in_server (bool) – Whether trainable parameter updated by parameter server is initialized on server. Default: False.

property shape

Get the MetaTensor’s shape.

property sliced

Get slice status of the parameter.

property unique

whether the parameter is already unique or not.

class tinyms.ParameterTuple[源代码]

Class for storing tuple of parameters.

注解

It is used to store the parameters of the network into the parameter tuple collection.

clone(prefix, init='same')[源代码]

Clone the parameters in ParameterTuple element-wisely to generate a new ParameterTuple.

参数
  • prefix (str) – Namespace of parameter.

  • init (Union[Tensor, str, numbers.Number]) – Initialize the shape and dtype of the parameters. The definition of init is the same as in Parameter API. If init is ‘same’, the parameters in the new parameter tuple are the same as those in the original parameter tuple. Default: ‘same’.

引发

RuntimeError – If parameter’s name is not end with embedding_table.

返回

Tuple, the new Parameter tuple.

count()

Return number of occurrences of value.

index()

Return first index of value.

Raises ValueError if the value is not present.

tinyms.set_seed(seed)[源代码]

Set global seed.

注解

The global seed is used by numpy.random, mindspore.common.Initializer, mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution.

If global seed is not set, these packages will use their own default seed independently, numpy.random and mindspore.common.Initializer will choose a random seed, mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution will use zero.

Seed set by numpy.random.seed() only used by numpy.random, while seed set by this API will also used by numpy.random, so just set all seed by this API is recommended.

参数

seed (int) – The seed to be set.

引发

实际案例

>>> import numpy as np
>>> import mindspore.ops as ops
>>> from mindspore import Tensor, set_seed, Parameter
>>> from mindspore.common.initializer import initializer
>>>
>>> # Note: (1) Please make sure the code is running in PYNATIVE MODE;
>>> # (2) Because Composite-level ops need parameters to be Tensors, for below examples,
>>> # when using ops.uniform operator, minval and maxval are initialised as:
>>> minval = Tensor(1.0, ms.float32)
>>> maxval = Tensor(2.0, ms.float32)
>>>
>>> # 1. If global seed is not set, numpy.random and initializer will choose a random seed:
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2
>>> # Rerun the program will get different results:
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A3
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A4
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W3
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W4
>>>
>>> # 2. If global seed is set, numpy.random and initializer will use it:
>>> set_seed(1234)
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2
>>> # Rerun the program will get the same results:
>>> set_seed(1234)
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1
>>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2
>>>
>>> # 3. If neither global seed nor op seed is set, mindspore.ops.composite.random_ops and
>>> # mindspore.nn.probability.distribution will choose a random seed:
>>> c1 = ops.uniform((1, 4), minval, maxval) # C1
>>> c2 = ops.uniform((1, 4), minval, maxval) # C2
>>> # Rerun the program will get different results:
>>> c1 = ops.uniform((1, 4), minval, maxval) # C3
>>> c2 = ops.uniform((1, 4), minval, maxval) # C4
>>>
>>> # 4. If global seed is set, but op seed is not set, mindspore.ops.composite.random_ops and
>>> # mindspore.nn.probability.distribution will calculate a seed according to global seed and
>>> # default op seed. Each call will change the default op seed, thus each call get different
>>> # results.
>>> set_seed(1234)
>>> c1 = ops.uniform((1, 4), minval, maxval) # C1
>>> c2 = ops.uniform((1, 4), minval, maxval) # C2
>>> # Rerun the program will get the same results:
>>> set_seed(1234)
>>> c1 = ops.uniform((1, 4), minval, maxval) # C1
>>> c2 = ops.uniform((1, 4), minval, maxval) # C2
>>>
>>> # 5. If both global seed and op seed are set, mindspore.ops.composite.random_ops and
>>> # mindspore.nn.probability.distribution will calculate a seed according to global seed and
>>> # op seed counter. Each call will change the op seed counter, thus each call get different
>>> # results.
>>> set_seed(1234)
>>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2
>>> # Rerun the program will get the same results:
>>> set_seed(1234)
>>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2
>>>
>>> # 6. If op seed is set but global seed is not set, 0 will be used as global seed. Then
>>> # mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution act as in
>>> # condition 5.
>>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2
>>> # Rerun the program will get the same results:
>>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1
>>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2
>>>
>>> # 7. Recall set_seed() in the program will reset numpy seed and op seed counter of
>>> # mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution.
>>> set_seed(1234)
>>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1
>>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1
>>> set_seed(1234)
>>> np_2 = np.random.normal(0, 1, [1]).astype(np.float32) # still get A1
>>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # still get C1
tinyms.get_seed()[源代码]

Get global seed.

返回

Integer. The global seed.

tinyms.abs(x, dtype=None)

Calculates the absolute value element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Currently the backend kernel only supports float calculation, if the input is not a float, then it will be casted to mstype.float32 and casted back.

参数
  • x (Tensor) – Tensor to be used for calculation.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, -5], np.float32)
>>> output = np.absolute(x)
>>> print(output)
[1. 2. 3. 4. 5.]
tinyms.absolute(x, dtype=None)[源代码]

Calculates the absolute value element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Currently the backend kernel only supports float calculation, if the input is not a float, then it will be casted to mstype.float32 and casted back.

参数
  • x (Tensor) – Tensor to be used for calculation.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, -5], np.float32)
>>> output = np.absolute(x)
>>> print(output)
[1. 2. 3. 4. 5.]
tinyms.add(x1, x2, dtype=None)[源代码]

Adds arguments element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input to be added.

  • x2 (Tensor) – input to be added.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the sum of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.full((3, 2), [1, 2])
>>> x2 = np.full((3, 2), [3, 4])
>>> output = np.add(x1, x2)
>>> print(output)
[[4 6]
[4 6]
[4 6]]
tinyms.amax(a, axis=None, keepdims=False, initial=None, where=True)[源代码]

Returns the maximum of an array or maximum along an axis.

注解

Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • a (Tensor) – Input data.

  • axis (None or int or tuple of integers, optional) – defaults to None. Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of integers, the maximum is selected over multiple axes, instead of a single axis or all the axes as before.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • initial (scalar, optional) – defaults to None. The minimum value of an output element. Must be present to allow computation on empty slice.

  • where (boolean Tensor, optional) – defaults to True. A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided.

返回

Tensor or scalar, maximum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(4).reshape((2,2)).astype('float32')
>>> output = np.amax(a)
>>> print(output)
3.0
>>> output = np.amax(a, axis=0)
>>> print(output)
[2. 3.]
>>> output = np.amax(a, axis=1)
>>> print(output)
[1. 3.]
>>> output = np.amax(a, where=np.array([False, True]), initial=-1, axis=0)
>>> print(output)
[-1.  3.]
tinyms.amin(a, axis=None, keepdims=False, initial=None, where=True)[源代码]

Returns the minimum of an array or minimum along an axis.

注解

Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • a (Tensor) – Input data.

  • axis (None or int or tuple of integers, optional) – defaults to None. Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of integers, the minimum is selected over multiple axes, instead of a single axis or all the axes as before.

  • keepdims (bool, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • initial (Number, optional) – defaults to None. The maximum value of an output element. Must be present to allow computation on empty slice.

  • where (bool Tensor, optional) – defaults to True. A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided.

返回

Tensor or scalar, minimum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(4).reshape((2,2)).astype('float32')
>>> output = np.amin(a)
>>> print(output)
0.0
>>> output = np.amin(a, axis=0)
>>> print(output)
[0. 1.]
>>> output = np.amin(a, axis=1)
>>> print(output)
[0. 2.]
>>> output = np.amin(a, where=np.array([False, True]), initial=10, axis=0)
>>> print(output)
[10.  1.]
tinyms.append(arr, values, axis=None)[源代码]

Appends values to the end of a tensor.

参数
  • arr (Tensor) – Values are appended to a copy of this tensor.

  • values (Tensor) – These values are appended to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis). If axis is not specified, values can be any shape and will be flattened before use.

  • axis (None, int, optional) – The axis along which values are appended. If axis is not given, both arr and values are flattened before use, default is None.

返回

Tensor, a copy of tensor with values appended to axis.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If specified axis exceeds arr.ndim.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((2, 3))
>>> b = np.ones((2, 1))
>>> print(np.append(a, b, axis=1).shape)
(2, 4)
tinyms.apply_along_axis(func1d, axis, arr, *args, **kwargs)[源代码]

Applies a function to 1-D slices along the given axis. Executes func1d(a, *args, **kwargs) where func1d operates on 1-D arrays and a is a 1-D slice of arr along axis.

参数
  • func1d (function) – Maps (M,) -> (Nj…). This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.

  • axis (int) – Axis along which arr is sliced.

  • arr (Tensor) – Input array with shape (Ni…, M, Nk…).

  • args (any) – Additional arguments to func1d.

  • kwargs (any) – Additional named arguments to func1d.

返回

Tensor with shape (Ni…, Nj…, Nk…), the output array. Its shape is identical to the shape of arr, except along the axis dimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of func1d. So if func1d returns a scalar, the output will have one fewer dimensions than arr.

Supported Platforms:

Ascend GPU CPU

引发

ValueError – if axis is out of the range.

实际案例

>>> import mindspore.numpy as np
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> print(np.apply_along_axis(np.diag, -1, b))
[[[1 0 0]
[0 2 0]
[0 0 3]]
[[4 0 0]
[0 5 0]
[0 0 6]]
[[7 0 0]
[0 8 0]
[0 0 9]]]
tinyms.apply_over_axes(func, a, axes)[源代码]

Applies a function repeatedly over multiple axes.

func is called as res = func(a, axis), where axis is the first element of axes. The result res of the function call must have either the same dimensions as a or one less dimension. If res has one less dimension than a, a dimension is inserted before axis. The call to func is then repeated for each axis in axes, with res as the first argument.

参数
  • func (function) – This function must take two arguments, func(a, axis).

  • a (Union[int, float, bool, list, tuple, Tensor]) – Input tensor.

  • axes (Union[int, list, tuple]) – Axes over which func is applied; the elements must be integers.

返回

Tensor. The number of dimensions is the same as a, but the shape can be different. This depends on whether func changes the shape of its output with respect to its input.

引发
  • TypeError – If input a is not array_like or axes is not int or sequence of ints.

  • ValueError – If any axis is out of range or duplicate axes exist.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(10).reshape(2, 5).astype('float32')
>>> print(x)
[[0. 1. 2. 3. 4.]
 [5. 6. 7. 8. 9.]]
>>> print(np.apply_over_axes(np.sum, x, axes=0))
[[ 5.  7.  9. 11. 13.]]
tinyms.arange(start, stop=None, step=None, dtype=None)[源代码]

Returns evenly spaced values within a given interval.

参数
  • start (Union[int, float]) – Start of interval. The interval includes this value. When stop is provided as a position argument, start must be given, when stop is a normal argument, start can be optional, and default is 0. Please see additional examples below.

  • stop (Union[int, float], optional) – End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.

  • step (Union[int, float], optional) – Spacing between values. For any output out, this is the distance between two adjacent values, \(out[i+1] - out[i]\). The default step size is 1. If step is specified as a position argument, start must also be given.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype. If dtype is None, the data type of the new tensor will be inferred from start, stop and step. Default is None.

返回

Tensor with evenly spaced values.

引发
  • TypeError(PyNative Mode) – If input arguments have types not specified above, or arguments are not given in the correct orders specified above.

  • RuntimeError(Graph Mode) – The inputs that lead to TypeError in Pynative Mode will lead to RuntimeError in Graph Mode.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.arange(0, 5, 1))
[0 1 2 3 4]
>>> print(np.arange(3))
[0 1 2]
>>> print(np.arange(start=0, stop=3))
[0 1 2]
>>> print(np.arange(0, stop=3, step=0.5))
[0.  0.5 1.  1.5 2.  2.5]
>>> print(np.arange(stop=3)) # This will lead to TypeError
tinyms.arccos(x, dtype=None)[源代码]

Trigonometric inverse cosine, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor. x-coordinate on the unit circle. For real arguments, the domain is \([-1, 1]\).

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor.

引发

TypeError – If the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, -1], np.float32)
>>> output = np.arccos(x)
>>> print(output)
[0.        3.1415927]
tinyms.arccosh(x, dtype=None)[源代码]

Inverse hyperbolic cosine, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(1, 5).astype('float32')
>>> print(np.arccosh(x))
[0.        1.316958  1.7627472 2.063437 ]
tinyms.arcsin(x, dtype=None)[源代码]

Inverse sine, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor. y-coordinate on the unit circle.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Output Tensor.

引发

TypeError – If the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, -1], np.float32)
>>> output = np.arcsin(x)
>>> print(output)
[ 1.5707964 -1.5707964]
tinyms.arcsinh(x, dtype=None)[源代码]

Inverse hyperbolic sine element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(5).astype('float32')
>>> print(np.arcsinh(x))
[0.        0.8813736 1.4436355 1.8184465 2.0947125]
tinyms.arctan(x, dtype=None)[源代码]

Trigonometric inverse tangent, element-wise.

The inverse of tan, so that if \(y = tan(x)\) then \(x = arctan(y)\).

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(5).astype('float32')
>>> print(np.arctan(x))
[0.        0.7853982 1.1071488 1.2490457 1.3258177]
tinyms.arctan2(x1, x2, dtype=None)[源代码]

Element-wise arc tangent of \(x1/x2\) choosing the quadrant correctly.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input tensor.

  • x2 (Tensor) – input tensor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the sum of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([-1, +1, +1, -1])
>>> x2 = np.array([-1, -1, +1, +1])
>>> output = np.arctan2(x1, x2)
>>> print(output)
[-2.3561945   2.3561945   0.78539819 -0.78539819]
tinyms.arctanh(x, dtype=None)[源代码]

Inverse hyperbolic tangent element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([-0.99, -0.75, -0.5, 0, 0.5]).astype('float32')
>>> print(np.arctanh(x))
[-2.646653   -0.97295505 -0.54930615  0.          0.54930615]
tinyms.argmax(a, axis=None)[源代码]

Returns the indices of the maximum values along an axis.

注解

Numpy argument out is not supported. On Ascend, in case of multiple occurrences of the maximum values, the return indices may not necessarily correspond to the first occurrence.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input array.

  • axis (int, optional) – By default, the index is into the flattened array, otherwise along the specified axis.

返回

Tensor, array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.

引发

ValueError – if axis is out of range.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(10, 16).reshape(2, 3)
>>> print(np.argmax(a))
5
>>> print(np.argmax(a, axis=0))
[1 1 1]
>>> print(np.argmax(a, axis=1))
[2 2]
tinyms.argmin(a, axis=None)[源代码]

Returns the indices of the minimum values along an axis.

注解

Numpy argument out is not supported.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input array.

  • axis (int, optional) – By default, the index is into the flattened array, otherwise along the specified axis.

返回

Tensor, array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.

引发

ValueError – if axis is out of range.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(10, 16).reshape(2, 3)
>>> print(np.argmin(a))
0
>>> print(np.argmin(a, axis=0))
[0 0 0]
>>> print(np.argmin(a, axis=1))
[0 0]
tinyms.array(obj, dtype=None, copy=True, ndmin=0)[源代码]

Creates a tensor.

This function creates tensors from an array-like object.

参数
  • obj (Union[int, float, bool, list, tuple]) – Input data, in any form that can be converted to a Tensor. This includes Tensor, list, tuple and numbers.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype, can be in format of np.int32, or ‘int32’. If dtype is None, the data type of the new tensor will be inferred from obj. Default is None.

  • copy (bool) – If True, then the object is copied. Otherwise, a copy will only be made if necessary. Default: True.

  • ndmin (int) – Specifies the minimum number of dimensions that the resulting tensor should have. Ones will be pre-pended to the shape as needed to meet this requirement. Default: 0

返回

Tensor, generated tensor with the specified dtype.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If input obj has different sizes at different dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.array([1,2,3]))
[1 2 3]
tinyms.array_equal(a1, a2, equal_nan=False)[源代码]

Returns True if input arrays have same shapes and all elements equal.

注解

In mindspore, a bool tensor is returned instead, since in Graph mode, the value cannot be traced and computed at compile time.

Since on Ascend, nan is treated differently, currently the argument equal_nan is not supported on Ascend.

参数
返回

Scalar bool tensor, value is True if inputs are equal, False otherwise.

引发

TypeError – If inputs have types not specified above.

Supported Platforms:

GPU CPU Ascend

实际案例

>>> import mindspore.numpy as np
>>> a = [0,1,2]
>>> b = [[0,1,2], [0,1,2]]
>>> print(np.array_equal(a,b))
False
tinyms.array_equiv(a1, a2)[源代码]

Returns True if input arrays are shape consistent and all elements equal.

Shape consistent means they are either the same shape, or one input array can be broadcasted to create the same shape as the other one.

注解

In mindspore, a bool tensor is returned instead, since in Graph mode, the value cannot be traced and computed at compile time.

参数

a1/a2 (Union[int, float, bool, list, tuple, Tensor]) – Input arrays.

返回

Scalar bool tensor, value is True if inputs are equivalent, False otherwise.

引发

TypeError – If inputs have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = [0,1,2]
>>> b = [[0,1,2], [0,1,2]]
>>> print(np.array_equiv(a,b))
True
tinyms.array_split(x, indices_or_sections, axis=0)[源代码]

Splits a tensor into multiple sub-tensors.

注解

Currently, array_split only supports mindspore.float32 on CPU.

The only difference between np.split and np.array_split is that np.array_split allows indices_or_sections to be an integer that does not equally divide the axis. For a tensor of length l that should be split into n sections, it returns \(l % n\) sub-arrays of size \(l//n + 1\) and the rest of size \(l//n\).

参数
  • x (Tensor) – A Tensor to be divided.

  • indices_or_sections (Union[int, tuple(int), list(int)]) – If integer, \(N\), the tensor will be divided into \(N\) tensors along axis. If tuple(int), list(int) or of sorted integers, the entries indicate where along axis the array is split. For example, \([2, 3]\) would, for \(axis=0\), result in three sub-tensors \(x[:2]\), \(x[2:3]\). If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.

  • axis (int) – The axis along which to split. Default: 0.

返回

A list of sub-tensors.

引发
  • TypeError – If argument indices_or_sections is not integer, tuple(int) or list(int) or argument axis is not integer.

  • ValueError – If argument axis is out of range of \([-x.ndim, x.ndim)\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.arange(9).astype("float32")
>>> output = np.array_split(input_x, 4)
>>> print(output)
(Tensor(shape=[3], dtype=Float32,
    value= [ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00]),
Tensor(shape=[2], dtype=Float32,
    value= [ 3.00000000e+00,  4.00000000e+00]),
Tensor(shape=[2], dtype=Float32,
    value= [ 5.00000000e+00,  6.00000000e+00]),
Tensor(shape=[2], dtype=Float32,
    value= [ 7.00000000e+00,  8.00000000e+00]))
tinyms.array_str(a)[源代码]

Returns a string representation of the data in an array.

The data in the array is returned as a single string. This function is similar to array_repr, the difference being that array_repr also returns information on the kind of array and its data type.

注解

Numpy argument max_line_width, precision and suppress_small are not supported. Graph mode does not support the function.

参数

a (Tensor) – Input data.

返回

String.

Supported Platforms:

Ascend GPU CPU

引发

TypeError – If input is not tensor.

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(5)
>>> np.array_str(x)
'[0 1 2 3 4]'
tinyms.asarray(a, dtype=None)[源代码]

Converts the input to tensor.

This function converts tensors from an array-like object.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input data, in any form that can be converted to a Tensor. This includes Tensor, list, tuple and numbers.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype, can be in format of np.int32, or ‘int32’. If dtype is None, the data type of the new tensor will be inferred from obj. Default is None.

返回

Tensor, generated tensor with the specified dtype.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If input a has different sizes at different dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.asarray([1,2,3]))
[1 2 3]
tinyms.asfarray(a, dtype=mindspore.float32)[源代码]

Similar to asarray, converts the input to a float tensor.

If non-float dtype is defined, this function will return a float32 tensor instead.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input data, in any form that can be converted to a Tensor. This includes Tensor, list, tuple and numbers.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype, can be in format of np.int32, or ‘int32’. If dtype is None, the data type of the new tensor will be inferred from a. Default is mindspore.float32.

返回

Tensor, generated tensor with the specified float dtype.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If input a has different sizes at different dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.asfarray([1,2,3]))
[1. 2. 3.]
tinyms.atleast_1d(*arys)[源代码]

Converts inputs to arrays with at least one dimension.

Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved.

注解

In graph mode, returns a tuple of tensor instead of a list of tensors.

参数

*arys (Tensor) – one or more input tensors.

返回

Tensor, or list of tensors, each with a.ndim >= 1.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((2, 3))
>>> b = np.ones(())
>>> c = np.ones(5)
>>> output = np.atleast_1d(a, b, c)
>>> print(output)
    [Tensor(shape=[2, 3], dtype=Float32, value=
    [[1.00000000e+00, 1.00000000e+00, 1.00000000e+00],
    [1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]),
    Tensor(shape=[1], dtype=Float32, value= [1.00000000e+00]),
    Tensor(shape=[5], dtype=Float32,
    value= [1.00000000e+00, 1.00000000e+00, 1.00000000e+00,
    1.00000000e+00, 1.00000000e+00])]
tinyms.atleast_2d(*arys)[源代码]

Reshapes inputs as arrays with at least two dimensions.

注解

In graph mode, returns a tuple of tensor instead of a list of tensors.

参数

*arys (Tensor) – one or more input tensors.

返回

Tensor, or list of tensors, each with a.ndim >= 2.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((2, 3))
>>> b = np.ones(())
>>> c = np.ones(5)
>>> output = np.atleast_2d(a, b, c)
>>> print(output)
    [Tensor(shape=[2, 3], dtype=Float32, value=
    [[1.00000000e+00, 1.00000000e+00, 1.00000000e+00],
    [1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]),
    Tensor(shape=[1, 1], dtype=Float32, value= [[1.00000000e+00]]),
    Tensor(shape=[1, 5], dtype=Float32,
    value= [[1.00000000e+00, 1.00000000e+00, 1.00000000e+00,
    1.00000000e+00, 1.00000000e+00]])]
tinyms.atleast_3d(*arys)[源代码]

Reshapes inputs as arrays with at least three dimensions.

注解

In graph mode, returns a tuple of tensor instead of a list of tensors.

参数

*arys (Tensor) – one or more input tensors.

返回

Tensor, or list of tensors, each with a.ndim >= 3. For example, a 1-D array of shape (N,) becomes a tensor of shape (1, N, 1), and a 2-D array of shape (M, N) becomes a tensor of shape (M, N, 1).

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((2, 3))
>>> b = np.ones(())
>>> c = np.ones(5)
>>> output = np.atleast_3d(a, b, c)
>>> print(output)
    [Tensor(shape=[2, 3, 1], dtype=Float32, value=
    [[[1.00000000e+00], [1.00000000e+00], [1.00000000e+00]],
    [[1.00000000e+00], [1.00000000e+00], [1.00000000e+00]]]),
    Tensor(shape=[1, 1, 1], dtype=Float32, value= [[[1.00000000e+00]]]),
    Tensor(shape=[1, 5, 1], dtype=Float32,
    value= [[[1.00000000e+00], [1.00000000e+00], [1.00000000e+00],
    [1.00000000e+00], [1.00000000e+00]]])]
tinyms.average(x, axis=None, weights=None, returned=False)[源代码]

Computes the weighted average along the specified axis.

参数
  • x (Tensor) – A Tensor to be averaged.

  • axis (Union[None, int, tuple(int)]) – Axis along which to average x. Default: None. If the axis is None, it will average over all of the elements of the tensor x. If the axis is negative, it counts from the last to the first axis.

  • weights (Union[None, Tensor]) – Weights associated with the values in x. Default: None. If weights is None, all the data in x are assumed to have a weight equal to one. If weights is 1-D tensor, the length must be the same as the given axis. Otherwise, weights should have the same shape as x.

  • returned (bool) – Default: False. If True, the tuple (average, sum_of_weights) is returned. If False, only the average is returned.

返回

Averaged Tensor. If returned is True, return tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.array([[1., 2.], [3., 4.]])
>>> output = np.average(input_x, axis=0, weights=input_x, returned=True)
>>> print(output)
(Tensor(shape=[2], dtype=Float32, value= [ 2.50000000e+00,  3.33333325e+00]),
 Tensor(shape=[2], dtype=Float32, value= [ 4.00000000e+00,  6.00000000e+00]))
tinyms.bartlett(M)[源代码]

Returns the Bartlett window. The Bartlett window is very similar to a triangular window, except that the end points are at zero. It is often used in signal processing for tapering a signal, without generating too much ripple in the frequency domain.

参数

M (int) – Number of points in the output window. If zero or less, an empty array is returned.

返回

Tensor, the triangular window, with the maximum value normalized to one (the value one appears only if the number of samples is odd), with the first and last samples equal to zero.

引发

TypeError – if M is not an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.bartlett(12))
[0.         0.18181819 0.36363637 0.5454545  0.72727275 0.9090909
0.9090909  0.72727275 0.5454545  0.36363637 0.18181819 0.        ]
tinyms.bincount(x, weights=None, minlength=0, length=None)[源代码]

Count number of occurrences of each value in array of non-negative ints. The number of bins (of size 1) is one larger than the largest value in x. If minlength is specified, there will be at least this number of bins in the output array (though it will be longer if necessary, depending on the contents of x). Each bin gives the number of occurrences of its index value in x. If weights is specified the input array is weighted by it, i.e. if a value n is found at position i, out[n] += weight[i] instead of out[n] += 1.

注解

The additional argument length specifies the number of bins (overriding x.max() + 1), which must be provided in graph mode. If x contains negative values, no error will be raised, and negative values are treated as zeros instead.

参数
  • x (Union[list, tuple, Tensor]) – 1-d input array.

  • weights (Union[int, float, bool, list, tuple, Tensor], optional) – Weights, array of the same shape as x. Defaults to None.

  • minlength (int, optional) – A minimum number of bins for the output array. Defaults to 0.

  • length (int, optional) – Number of bins. Defaults to None.

返回

Tensor, the result of binning the input array. The length of out is equal to np.amax(x)+1.

引发

ValueError – if x is not one-dimensional, or if x and weights do not have the same shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.bincount(np.arange(5)))
[1 1 1 1 1]
>>> print(np.bincount(np.array([0, 1, 1, 3, 2, 1, 7])))
[1 3 1 1 0 0 0 1]
>>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
>>> x = np.array([0, 1, 1, 2, 2, 2])
>>> print(np.bincount(x,  weights=w))
[0.3 0.7 1.1]
tinyms.bitwise_and(x1, x2, dtype=None)[源代码]

Computes the bit-wise AND of two arrays element-wise. Computes the bit-wise AND of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator &.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. Only integer and boolean types are handled. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, this is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend

实际案例

>>> import mindspore.numpy as np
>>> print(np.bitwise_and(13, 17))
1
tinyms.bitwise_not(x, dtype=None)

Computes bit-wise inversion, or bit-wise NOT, element-wise. Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ~. For signed integer inputs, the two’s complement is returned. In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value. This is the most common method of representing signed integers on computers [1]. A N-bit two’s-complement system can represent every integer in the range -2^{N-1} to +2^{N-1}-1.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Supported dtypes on Ascend: np.int16, np.uint16.

参数
  • x (Tensor) – Only integer and boolean types are handled.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar.

Supported Platforms:

Ascend

实际案例

>>> import mindspore.numpy as np
>>> print(np.invert(np.array(13, dtype=np.uint16)))
65522
tinyms.bitwise_or(x1, x2, dtype=None)[源代码]

Computes the bit-wise OR of two arrays element-wise. Computes the bit-wise OR of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator |.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. Only integer and boolean types are handled. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, this is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend

实际案例

>>> import mindspore.numpy as np
>>> print(np.bitwise_or(13, 16))
29
tinyms.bitwise_xor(x1, x2, dtype=None)[源代码]

Computes the bit-wise XOR of two arrays element-wise. Computes the bit-wise XOR of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ^.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. Only integer and boolean types are handled. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, this is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend

实际案例

>>> import mindspore.numpy as np
>>> print(np.bitwise_xor(13, 17))
28
tinyms.blackman(M)[源代码]

Returns the Blackman window. The Blackman window is a taper formed by using the first three terms of a summation of cosines. It was designed to have close to the minimal leakage possible. It is close to optimal, only slightly worse than a Kaiser window.

参数

M (int) – Number of points in the output window. If zero or less, an empty array is returned.

返回

Tensor, the window, with the maximum value normalized to one (the value one appears only if the number of samples is odd).

引发

TypeError – if M is not an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.blackman(12))
[-1.4901161e-08  3.2606430e-02  1.5990365e-01  4.1439798e-01
7.3604518e-01  9.6704674e-01  9.6704674e-01  7.3604518e-01
4.1439798e-01  1.5990365e-01  3.2606430e-02 -1.4901161e-08]
tinyms.broadcast_arrays(*args)[源代码]

Broadcasts any number of arrays against each other.

注解

Numpy argument subok is not supported. In graph mode, returns a tuple of Tensor instead of a list of Tensor.

参数

*args (Tensor) – The arrays to broadcast.

返回

List of Tensor.

引发

ValueError – if arrays cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

示例

>>> import mindspore.numpy as np
>>> x = np.array([[1,2,3]])
>>> y = np.array([[4],[5]])
>>> output = np.broadcast_arrays(x, y)
>>> print(output)
[Tensor(shape=[2, 3], dtype=Int32, value=
[[1, 2, 3],
[1, 2, 3]]), Tensor(shape=[2, 3], dtype=Int32, value=
[[4, 4, 4],
[5, 5, 5]])]
tinyms.broadcast_to(array, shape)[源代码]

Broadcasts an array to a new shape.

参数
  • array (Tensor) – The array to broadcast.

  • shape (tuple) – The shape of the desired array.

返回

Tensor, original array broadcast to the given shape.

引发

ValueError – if array cannot be broadcast to shape.

Supported Platforms:

Ascend GPU CPU

示例

>>> import mindspore.numpy as np
>>> x = np.array([1, 2, 3])
>>> output = np.broadcast_to(x, (3, 3))
>>> print(output)
[[1 2 3]
[1 2 3]
[1 2 3]]
tinyms.cbrt(x, dtype=None)[源代码]

Returns the cube-root of a tensor, element-wise.

注解

Numpy arguments casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.asarray([1, -1, 3, -8, 64])
>>> output = np.cbrt(a)
>>> print(output)
[ 1.        -1.         1.4422495 -2.         4.       ]
tinyms.ceil(x, dtype=None)[源代码]

Returns the ceiling of the input, element-wise.

The ceil of the scalar x is the smallest integer i, such that i >= x.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • x (Tensor) – input values.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the floor of each element in x. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
>>> output = np.ceil(a)
>>> print(output)
[-1. -1. -0.  1.  2.  2.  2.]
tinyms.choose(a, choices, mode='clip')[源代码]

Construct an array from an index array and a list of arrays to choose from. Given an “index” array a of integers and a sequence of n arrays (choices), a and each choice array are first broadcast, as necessary, to arrays of a common shape; calling these Ba and Bchoices[i], i = 0,…,n-1 we have that, necessarily, Ba.shape == Bchoices[i].shape for each i. Then, a new array with shape Ba.shape is created as follows:

  • if mode='raise' (the default), then, first of all, each element of a (and thus Ba) must be in the range [0, n-1]; now, suppose that i (in that range) is the value at the (j0, j1, …, jm) position in Ba - then the value at the same position in the new array is the value in Bchoices[i] at that same position;

  • if mode='wrap', values in a (and thus Ba) may be any (signed) integer; modular arithmetic is used to map integers outside the range [0, n-1] back into that range; and then the new array is constructed as above;

  • if mode='clip', values in a (and thus Ba) may be any (signed) integer; negative integers are mapped to 0; values greater than n-1 are mapped to n-1; and then the new array is constructed as above.

注解

Numpy argument out is not supported. mode = 'raise' is not supported, and the default mode is ‘clip’ instead.

参数
  • a (int array) – This array must contain integers in [0, n-1], where n is the number of choices, unless mode=wrap or mode=clip, in which cases any integers are permissible.

  • choices (sequence of arrays) – Choice arrays. a and all of the choices must be broadcastable to the same shape. If choices is itself an array, then its outermost dimension (i.e., the one corresponding to choices.shape[0]) is taken as defining the “sequence”.

  • mode (‘raise’, ‘wrap’, ‘clip’, optional) –

    Specifies how indices outside [0, n-1] will be treated:

    ‘raise’ – raise an error;

    ‘wrap’ – wrap around;

    ‘clip’ – clip to the range. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

返回

Tensor, the merged result.

引发

ValueError – if a and any of the choices cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]]
>>> print(np.choose([2, 3, 1, 0], choices))
[20 31 12  3]
>>> print(np.choose([2, 4, 1, 0], choices, mode='clip'))
[20 31 12  3]
>>> print(np.choose([2, 4, 1, 0], choices, mode='wrap'))
[20  1 12  3]
>>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
>>> choices = [-10, 10]
>>> print(np.choose(a, choices))
[[ 10 -10  10]
 [-10  10 -10]
 [ 10 -10  10]]
tinyms.clip(x, xmin, xmax, dtype=None)[源代码]

Clips (limits) the values in an array.

Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of \([0, 1]\) is specified, values smaller than 0 become 0, and values larger than 1 become 1.

参数
  • x (Tensor) – Tensor containing elements to clip.

  • xmin (Tensor, scalar, None) – Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of xmin and xmax may be None.

  • xmax (Tensor, scalar, None) – Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of xmin and xmax may be None. If xmin or xmax are tensors, then the three tensors will be broadcasted to match their shapes.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor, a tensor with the elements of x, but where values < xmin are replaced with xmin, and those > xmax with xmax.

引发
  • TypeError – If inputs have types not specified above.

  • ValueError – If the shapes of x1 and x2 cannot broadcast, or both xmin and xmax are None.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, 0, 3, 2, 0])
>>> output = np.clip(x, 0, 2)
>>> print(output)
[1 2 2 0 0 2 2 0]
tinyms.column_stack(tup)[源代码]

Stacks 1-D tensors as columns into a 2-D tensor. 2-D tensors are stacked as-is, like np.hstack.

参数

tup (Union[Tensor, tuple, list]) – A sequence of 1-D or 2-D tensors. All of them must have the same shape except the axis to be concatenated.

返回

2-D Tensor, formed by stacking the given tensors.

Supported Platforms:

Ascend GPU CPU

引发

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([1, 2, 3]).astype('int32')
>>> x2 = np.array([4, 5, 6]).astype('int32')
>>> output = np.column_stack((x1, x2))
>>> print(output)
[[1 4]
 [2 5]
 [3 6]]
tinyms.concatenate(arrays, axis=0)[源代码]

Joins a sequence of tensors along an existing axis.

注解

To match Numpy behaviour, \(axis >= 32\) will not cause value error, the axis will be treated as None instead.

参数
  • arrays (Union[Tensor, tuple(Tensor), list(Tensor)]) – a tensor or a list of tensors to be concatenated.

  • axis (Union[None, int], optional) – The axis along which the tensors will be joined, if axis is None, tensors are flattened before use. Default is 0.

返回

A tensor concatenated from a tensor or a list of tensors.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If axis is not in the range of \([-ndim, ndim-1]\), and less than 32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.ones((1,2,3))
>>> x2 = np.ones((1,2,1))
>>> x = np.concatenate((x1, x2), axis=-1)
>>> print(x.shape)
(1, 2, 4)
tinyms.convolve(a, v, mode='full')[源代码]

Returns the discrete, linear convolution of two one-dimensional sequences.

注解

If v is longer than a, the tensors are swapped before computation.

参数
  • a (Union[list, tuple, Tensor]) – First one-dimensional input tensor.

  • v (Union[list, tuple, Tensor]) – Second one-dimensional input tensor.

  • mode (str, optional) – By default, mode is ‘full’. This returns the convolution at each point of overlap, with an output shape of \((N+M-1,)\). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen. If mode is ‘same’, it returns output of length \(max(M, N)\). Boundary effects are still visible. If mode is ‘valid’, it returns output of length \(max(M, N) - min(M, N) + 1\). The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

返回

Tensor, discrete, linear convolution of a and v.

引发
  • TypeError – if the inputs have types not specified above.

  • ValueError – if a and v are empty or have wrong dimensions

Supported Platforms:

GPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.convolve([1., 2., 3., 4., 5.], [2., 3.], mode="valid")
>>> print(output)
[ 7. 12. 17. 22.]
tinyms.copysign(x1, x2, dtype=None)[源代码]

Changes the sign of x1 to that of x2, element-wise.

If x2 is a scalar, its sign will be copied to all elements of x1.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Complex inputs are not supported now.

参数
  • x1 (Union[int, float, list, tuple, Tensor]) – Values to change the sign of.

  • x2 (Union[int, float, list, tuple, Tensor]) – The sign of x2 is copied to x1. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. The values of x1 with the sign of x2. This is a scalar if both x1 and x2 are scalars.

引发

TypeError – if dtype of the input is not in the given types or the input can not be converted to tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.copysign(np.array([1, -1, -1]), np.array([-1, 1, -1]))
>>> print(output)
[-1  1 -1]
tinyms.corrcoef(x, y=None, rowvar=True, dtype=None)[源代码]

Returns Pearson product-moment correlation coefficients.

Please refer to the documentation for cov for more detail. The relationship between the correlation coefficient matrix, R, and the covariance matrix, C, is \(R_{ij} = \frac{ C_{ij} } { \sqrt{ C_{ii} * C_{jj} } }\) The values of R are between -1 and 1, inclusive.

注解

Currently, complex numbers are not supported.

参数
  • x (Union[int, float, bool, tuple, list, Tensor]) – A 1-D or 2-D array containing multiple variables and observations. Each row of x represents a variable, and each column a single observation of all those variables. Also see rowvar below.

  • y (Union[int, float, bool, tuple, list, Tensor], optional) – An additional set of variables and observations.

  • rowvar (bool, optional) – If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.

  • dtype (mindspore.dtype, optional) – Data-type of the result. By default, the return data-type will have at least float32 precision.

返回

Tensor, The correlation coefficient matrix of the variables.

引发
  • TypeError – if the inputs have types not specified above.

  • ValueError – if x and y have wrong dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.corrcoef([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]])
>>> print(output)
[[1.         0.9827076  1.        ]
[0.9827077  0.99999994 0.9827077 ]
[1.         0.9827076  1.        ]]
tinyms.correlate(a, v, mode='valid')[源代码]

Cross-correlation of two 1-dimensional sequences.

This function computes the correlation as generally defined in signal processing texts:

\(c_{av}[k] = sum_n a[n+k] * conj(v[n])\)

with a and v sequences being zero-padded where necessary and conj being the conjugate.

注解

Currently, complex numbers are not supported.

参数
  • a (Union[list, tuple, Tensor]) – First input sequence.

  • v (Union[list, tuple, Tensor]) – Second input sequence.

  • mode (str, optional) – By default, mode is ‘valid’. If mode is ‘valid’, it returns output of length \(max(M, N) - min(M, N) + 1\). The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect. If mode is ‘full’, it returns the convolution at each point of overlap, with an output shape of \((N + M - 1,)\). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen. If mode is ‘same’, it returns output of length \(max(M, N)\). Boundary effects are still visible.

返回

Tensor. Discrete cross-correlation of a and v.

引发
  • TypeError – if the inputs can not be converted to tensor.

  • ValueError – if a and v are empty or have wrong dimensions

Supported Platforms:

GPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.correlate([1, 2, 3], [0, 1, 0.5])
>>> print(output)
[3.5]
>>> output = np.correlate([1, 2, 3], [0, 1, 0.5], mode="same")
>>> print(output)
[2.  3.5 3. ]
>>> output = np.correlate([1, 2, 3, 4, 5], [1, 2], mode="same")
>>> print(output)
[ 2.  5.  8. 11. 14.]
tinyms.cos(x, dtype=None)[源代码]

Cosine element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(5).astype('float32')
>>> print(np.cos(x))
[ 1.          0.5403023  -0.41614684 -0.9899925  -0.6536436 ]
tinyms.cosh(x, dtype=None)[源代码]

Hyperbolic cosine, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(5).astype('float32')
>>> print(np.cosh(x))
[ 1.         1.5430807  3.7621956 10.067662  27.308233 ]
tinyms.count_nonzero(x, axis=None, keepdims=False)[源代码]

Counts the number of non-zero values in the tensor x.

参数
  • x (Tensor) – The tensor for which to count non-zeros.

  • axis (Union[int,tuple], optional) – Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of x.

  • keepdims (bool, optional) – If this is set to True, the axes that are counted are left in the result as dimensions with size one. With this option, the result will broadcast correctly against x.

返回

Tensor, indicating number of non-zero values in the x along a given axis. Otherwise, the total number of non-zero values in x is returned.

引发
  • TypeError – If axis is not int or tuple.

  • ValueError – If axis is not in range [-x.ndim, x.ndim)

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, 0, 3, 2, 0])
>>> output = np.count_nonzero(x)
>>> print(output)
6
tinyms.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None, dtype=None)[源代码]

Estimates a covariance matrix, given data and weights.

Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples, \(X = [x_1, x_2, ... x_N]^T\), then the covariance matrix element \(C_{ij}\) is the covariance of \(x_i\) and \(x_j\). The element \(C_{ii}\) is the variance of \(x_i\).

注解

fweights and aweights must be all positive, in Numpy if negative values are detected, a value error will be raised, in MindSpore we converts all values to positive instead.

参数
  • m (Union[Tensor, list, tuple]) – A 1-D or 2-D tensor containing multiple variables and observations. Each row of m represents a variable, and each column represents a single observation of all those variables. Also see rowvar below.

  • y (Union[Tensor, list, tuple], optional) – An additional set of variables and observations. y has the same form as that of m, default is None.

  • rowvar (bool, optional) – If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.

  • bias (bool, optional) – Default Normalization (False) is by \((N - 1)\), where \(N\) is the number of observations given (unbiased estimate). If bias is True, then Normalization is by N. These values can be overridden by using the keyword ddof.

  • ddof (int, optional) – If not None, the default value implied by bias is overridden. Note that \(ddof=1\) will return the unbiased estimate, even if both fweights and aweights are specified, and \(ddof=0\) will return the simple average. See the notes for the details. The default value is None.

  • fweights (Union[Tensor, list, tuple], optional) – 1-D tensor of integer frequency weights; the number of times each observation vector should be repeated. The default value is None.

  • aweights (Union[Tensor, list, tuple], optional) – 1-D tensor of observation vector weights. These relative weights are typically larger for observations considered more important and smaller for observations considered less important. If \(ddof=0\) the tensor of weights can be used to assign probabilities to observation vectors. The default value is None.

  • dtype (Union[mindspore.dtype, str], optional) – Data-type of the result. By default, the return data-type will have mstype.float32 precision.

返回

Tensor, the covariance matrix of the variables.

引发
  • TypeError – if the inputs have types not specified above.

  • ValueError – if m and y have wrong dimensions.

  • RuntimeError – if aweights and fweights have dimensions > 2.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.cov([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]])
>>> print(output)
[[1.6666666 2.1666667 1.6666666]
[2.1666667 2.9166667 2.1666667]
[1.6666666 2.1666667 1.6666666]]
tinyms.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[源代码]

Returns the cross product of two (arrays of) vectors.

The cross product of a and b in \(R^3\) is a vector perpendicular to both a and b. If a and b are arrays of vectors, the vectors are defined by the last axis of a and b by default, and these axes can have dimensions 2 or 3. Where the dimension of either a or b is 2, the third component of the input vector is assumed to be zero and the cross product calculated accordingly. In cases where both input vectors have dimension 2, the z-component of the cross product is returned.

参数
  • a (Union[list, tuple, Tensor]) – Components of the first vector(s).

  • b (Union[list, tuple, Tensor]) – Components of the second vector(s).

  • axisa (int, optional) – Axis of a that defines the vector(s). By default, the last axis.

  • axisb (int, optional) – Axis of b that defines the vector(s). By default, the last axis.

  • axisc (int, optional) – Axis of c containing the cross product vector(s). Ignored if both input vectors have dimension 2, as the return is scalar. By default, the last axis.

  • axis (int, optional) – If defined, the axis of a, b and c that defines the vector(s) and cross product(s). Overrides axisa, axisb and axisc.

返回

Tensor, vector cross product(s).

引发

ValueError – when the dimensions of the vector(s) in a and/or b does not equal 2 or 3.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([[1,2,3], [4,5,6]])
>>> y = np.array([[4,5,6], [1,2,3]])
>>> output = np.cross(x, y)
>>> print(output)
[[-3  6 -3]
[ 3 -6  3]]
>>> output = np.cross(x, y, axisc=0)
>>> print(output)
[[-3  3]
[ 6 -6]
[-3  3]]
tinyms.cumprod(a, axis=None, dtype=None)[源代码]

Returns the cumulative product of elements along a given axis.

注解

Numpy argument out is not supported.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input tensor.

  • axis (int, optional) – Axis along which the cumulative product is computed. By default the input is flattened.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor.

引发
  • TypeError – If the input can not be converted to tensor or axis is not integer.

  • ValueError – If axis is out of range.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([1, 2, 3])
>>> print(np.cumprod(x))
[1 2 6]
tinyms.cumsum(a, axis=None, dtype=None)[源代码]

Returns the cumulative sum of the elements along a given axis.

注解

If a.dtype is int8, int16 or bool, the result dtype will be elevated to int32.

参数
  • a (Tensor) – Input tensor.

  • axis (int, optional) – Axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array.

  • dtype (mindspore.dtype, optional) – If not specified, stay the same as a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used.

返回

Tensor.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If axis is out of range.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.cumsum(np.ones((3,3)), axis=0)
>>> print(output)
[[1. 1. 1.]
 [2. 2. 2.]
 [3. 3. 3.]]
tinyms.deg2rad(x, dtype=None)[源代码]

Converts angles from degrees to radians.

参数
  • x (Tensor) – Angles in degrees.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor, the corresponding angle in radians. This is a tensor scalar if x is a tensor scalar.

引发

TypeError – if x is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, -5])
>>> output = np.deg2rad(x)
>>> print(output)
[ 0.01745329  0.03490658  0.05235988 -0.06981317 -0.08726647]
tinyms.diag(v, k=0)[源代码]

Extracts a diagonal or construct a diagonal array.

参数
  • v (Tensor) – If v is a 2-D array, return a copy of its k-th diagonal. If v is a 1-D array, return a 2-D array with v on the k-th diagonal.

  • k (int, optional) – Diagonal in question. The default is 0. Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal.

返回

Tensor, the extracted diagonal or constructed diagonal array.

引发

ValueError – if input is not 1-D or 2-D.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(9).reshape((3,3))
>>> print(x)
[[0 1 2]
[3 4 5]
[6 7 8]]
>>> output = np.diag(x)
>>> print(output)
[0 4 8]
>>> output = np.diag(x, k=1)
>>> print(output)
[1 5]
>>> output = np.diag(x, k=-1)
>>> print(output)
[3 7]
tinyms.diag_indices(n, ndim=2)[源代码]

Returns the indices to access the main diagonal of an array.

This returns a tuple of indices that can be used to access the main diagonal of an array a with a.ndim >= 2 dimensions and shape (n, n, …, n). For a.ndim = 2 this is the usual diagonal, for a.ndim > 2 this is the set of indices to access a[i, i, ..., i] for i = [0..n-1].

参数
  • n (int) – The size, along each dimension, of the arrays for which the returned indices can be used.

  • ndim (int, optional) – The number of dimensions.

返回

Tuple of Tensor.

引发

TypeError – if input are not integers.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.diag_indices(5, 3)
>>> print(output)
(Tensor(shape=[5], dtype=Int32, value= [0, 1, 2, 3, 4]),
Tensor(shape=[5], dtype=Int32, value= [0, 1, 2, 3, 4]),
Tensor(shape=[5], dtype=Int32, value= [0, 1, 2, 3, 4]))
tinyms.diagflat(v, k=0)[源代码]

Creates a two-dimensional array with the flattened input as a diagonal.

注解

On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • v (Tensor) – Input data, which is flattened and set as the k-th diagonal of the output.

  • k (int, optional) – Diagonal to set; 0, the default, corresponds to the “main” diagonal, a positive (negative) k giving the number of the diagonal above (below) the main.

返回

Tensor, The 2-D output array.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.diagflat(np.asarray([[1,2], [3,4]]))
>>> print(output)
[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
>>> output = np.diagflat(np.asarray([1,2]), 1)
>>> print(output)
[[0 1 0]
[0 0 2]
[0 0 0]]
tinyms.diagonal(a, offset=0, axis1=0, axis2=1)[源代码]

Returns specified diagonals.

If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-array whose diagonal is returned. The shape of the resulting array can be determined by removing axis1 and axis2 and appending an index to the right equal to the size of the resulting diagonals.

参数
  • a (Tensor) – Array from which the diagonals are taken.

  • offset (int, optional) – Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal.

  • axis1 (int, optional) – Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0).

  • axis2 (int, optional) – Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis.

返回

Tensor, if a is 2-D, then a 1-D array containing the diagonal. If a.ndim > 2, then the dimensions specified by axis1 and axis2 are removed, and a new axis inserted at the end corresponding to the diagonal.

引发

ValueError – if the input tensor has less than two dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(4).reshape(2,2).astype(np.float32)
>>> print(a)
[[0. 1.]
[2. 3.]]
>>> output = np.diagonal(a)
>>> print(output)
[0. 3.]
>>> output = np.diagonal(a, 1)
>>> print(output)
[1.]
>>> a = np.arange(8).reshape(2, 2, 2).astype(np.float32)
>>> print(a)
[[[0. 1.]
[2. 3.]]
[[4. 5.]
[6. 7.]]]
>>> output = np.diagonal(a, 0, 0, 1)
>>> print(output)
[[0. 6.]
[1. 7.]]
tinyms.diff(a, n=1, axis=-1, prepend=None, append=None)[源代码]

Calculates the n-th discrete difference along the given axis.

The first difference is given by \(out[i] = a[i+1] - a[i]\) along the given axis, higher differences are calculated by using diff iteratively.

注解

Since zero-shaped Tensor is not supported in MindSpore, a value error is raised if an empty Tensor is encountered.

参数
  • a (Tensor) – Input tensor.

  • n (int, optional) – The number of times values are differenced. If zero, the input is returned as-is.

  • axis (int, optional) – The axis along which the difference is taken, default is the last axis.

  • prepend/append (Tensor, optional) – Values to prepend or append to a along axis prior to performing the difference. Scalar values are expanded to arrays with length 1 in the direction of axis and the shape of the input array in along all other axes. Otherwise the dimension and shape must match a except along axis.

返回

The n-th differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. The type of the output is the same as the type of the difference between any two elements of a. This is the same as the type of a in most cases.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> arr = np.array([1, 3, -1, 0, 4])
>>> print(np.diff(arr, n=2))
[-6  5  3]
tinyms.digitize(x, bins, right=False)[源代码]

Returns the indices of the bins to which each value in input array belongs. If values in x are beyond the bounds of bins, 0 or len(bins) is returned as appropriate.

参数
  • x (Union[int, float, bool, list, tuple, Tensor]) – Input array to be binned.

  • bins (Union[list, tuple, Tensor]) – Array of bins. It has to be 1-dimensional and monotonic.

  • right (boolean, optional) – Indicating whether the intervals include the right or the left bin edge. Default behavior is (right==False) indicating that the interval does not include the right edge. The left bin end is open in this case, i.e., bins[i-1] <= x < bins[i] is the default behavior for monotonically increasing bins.

返回

Tensor of ints, output array of indices, of same shape as x.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([1.2, 10.0, 12.4, 15.5, 20.])
>>> bins = np.array([0, 5, 10, 15, 20])
>>> inds = np.digitize(x, bins)
>>> print(inds)
[1 3 3 4 5]
tinyms.divide(x1, x2, dtype=None)[源代码]

Returns a true division of the inputs, element-wise.

Instead of the Python traditional ‘floor division’, this returns a true division.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – the divident.

  • x2 (Tensor) – the divisor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, this is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.full((3, 2), [1, 2])
>>> x2 = np.full((3, 2), [3, 4])
>>> output = np.divide(x1, x2)
>>> print(output)
[[0.33333334 0.5       ]
[0.33333334 0.5       ]
[0.33333334 0.5       ]]
tinyms.divmod(x1, x2, dtype=None)

Returns element-wise quotient and remainder simultaneously.

参数
  • x1 (Union[Tensor]) – Dividend tensor.

  • x2 (Union[Tensor, int, float, bool]) – Divisor. If x1.shape != x2.shape, they must be broadcastable to a common shape.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Element-wise quotient and remainder from floor division, in format of (quotient, remainder)

引发

TypeError – if x1 and x2 are not Tensor or scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([1, 2, 3, 4, 5])
>>> print(np.divmod(a, 1.5))
(Tensor(shape=[5], dtype=Float32,
 value= [ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00,  2.00000000e+00,  3.00000000e+00]),
 Tensor(shape=[5], dtype=Float32,
 value= [ 1.00000000e+00,  5.00000000e-01,  0.00000000e+00,  1.00000000e+00,  5.00000000e-01]))
tinyms.dot(a, b)[源代码]

Returns the dot product of two arrays.

Specifically, If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation). If both a and b are 2-D arrays, it is matrix multiplication. If either a or b is 0-D (scalar), it is equivalent to multiply. If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b. If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

注解

Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32. On CPU, the supported dtypes are np.float16, np.float32, and np.float64.

参数
返回

Tensor or scalar, the dot product of a and b. If a and b are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned

引发

ValueError – If the last dimension of a is not the same size as the second-to-last dimension of b.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.full((1, 3), 7).astype('float32')
>>> b = np.full((2, 3, 4), 5).astype('float32')
>>> output = np.dot(a, b)
>>> print(output)
[[[105. 105. 105. 105.]
[105. 105. 105. 105.]]]
tinyms.dsplit(x, indices_or_sections)[源代码]

Splits a tensor into multiple sub-tensors along the 3rd axis (depth). It is equivalent to split with \(axis=2\) (default), the array is always split along the third axis regardless of the array dimension.

参数
  • x (Tensor) – A Tensor to be divided.

  • indices_or_sections (Union[int, tuple(int), list(int)]) – If integer, \(N\), the tensor will be divided into \(N\) equal tensors along axis. If tuple(int), list(int) or of sorted integers, the entries indicate where along axis the array is split. For example, \([2, 3]\) would, for \(axis=0\), result in three sub-tensors \(x[:2]\), \(x[2:3]\). If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.

返回

A list of sub-tensors.

引发

TypeError – If argument indices_or_sections is not integer.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.arange(6).reshape((1, 2, 3)).astype('float32')
>>> output = np.dsplit(input_x, 3)
>>> print(output)
(Tensor(shape=[1, 2, 1], dtype=Float32,
value=[[[ 0.00000000e+00],
        [ 3.00000000e+00]]]),
Tensor(shape=[1, 2, 1], dtype=Float32,
value=[[[ 1.00000000e+00],
        [ 4.00000000e+00]]]),
Tensor(shape=[1, 2, 1], dtype=Float32,
value=[[[ 2.00000000e+00],
        [ 5.00000000e+00]]]))
tinyms.dstack(tup)[源代码]

Stacks tensors in sequence depth wise (along the third axis). This is equivalent to concatenation along the third axis. 1-D tensors \((N,)\) should be reshaped to \((1,N,1)\). 2-D tensors \((M,N)\) should be reshaped to \((M,N,1)\) before concatenation.

参数

tup (Union[Tensor, tuple, list]) – A sequence of tensors. The tensors must have the same shape along all but the third axis. 1-D or 2-D tensors must have the same shape.

返回

Stacked Tensor, formed by stacking the given tensors.

Supported Platforms:

Ascend GPU CPU

引发

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([1, 2, 3]).astype('float32')
>>> x2 = np.array([4, 5, 6]).astype('float32')
>>> output = np.dstack((x1, x2))
>>> print(output)
[[[1. 4.]
  [2. 5.]
  [3. 6.]]]
tinyms.ediff1d(ary, to_end=None, to_begin=None)[源代码]

The differences between consecutive elements of a tensor.

参数
  • ary (Tensor) – If necessary, will be flattened before the differences are taken.

  • to_end (Tensor or scalar, optional) – Number(s) to append at the end of the returned differences.

  • to_begin (Tensor or scalar, optional) – Number(s) to prepend at the beginning of the returned differences.

返回

The differences.

引发

TypeError – If inputs have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> arr = np.array([1, 3, -1, 0, 4])
>>> print(np.ediff1d(arr))
[ 2 -4  1  4]
tinyms.empty(shape, dtype=mindspore.float32)[源代码]

Returns a new array of given shape and type, without initializing entries.

注解

Numpy argument order is not supported. Object arrays are not supported.

参数
  • shape (Union[int, tuple(int)]) – Shape of the empty array, e.g., (2, 3) or 2.

  • dtype (mindspore.dtype, optional) – Desired output data-type for the array, e.g, mstype.int8. Default is mstype.float32.

返回

Tensor, array of uninitialized (arbitrary) data of the given shape and dtype.

引发

TypeError – if the input shape or dtype is invalid.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.empty((2, 3))
>>> print(output)
# result may vary
Tensor(shape=[2, 3], dtype=Float32, value=
<uninitialized>)
tinyms.empty_like(prototype, dtype=None, shape=None)[源代码]

Returns a new array with the same shape and type as a given array.

注解

Input array must have the same size across a dimension. If prototype is not a Tensor, dtype is float32 by default if not provided.

参数
  • prototype (Union[Tensor, list, tuple]) – The shape and data-type of prototype define these same attributes of the returned array.

  • dtype (mindspore.dtype, optional) – Overrides the data type of the result.

  • shape (int or sequence of ints, optional) – Overrides the shape of the result.

返回

Tensor, array of uninitialized (arbitrary) data with the same shape and type as prototype.

引发

ValueError – if prototype is not a Tensor, list or tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((4,1,2))
>>> output = np.empty_like(a)
>>> print(output)
# result may vary
Tensor(shape=[4, 1, 2], dtype=Float32, value=
<uninitialized>)
tinyms.equal(x1, x2, dtype=None)[源代码]

Returns the truth value of (x1 == x2) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype is passed. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.equal(np.array([0, 1, 3]), np.arange(3))
>>> print(output)
[ True  True False]
tinyms.exp(x, dtype=None)[源代码]

Calculates the exponential of all elements in the input array.

注解

Numpy arguments casting, order, subok, signature, and extobj are not supported. When where is provided, out must have a tensor value. out is not supported for storing the result, however it can be used in combination with where to set the value at indices for which where is set to False. On GPU, the supported dtypes are np.float16, and np.float32. On CPU, the supported dtypes are np.float16, np.float32, np.float64.

参数
  • x (Tensor) – input data.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise exponential of x. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.exp(np.arange(5).astype(np.float32))
>>> print(output)
[ 1.         2.718282   7.3890557 20.085537  54.598145 ]
tinyms.exp2(x, dtype=None)[源代码]

Calculates 2**p for all p in the input array.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • x (Tensor) – input values.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise 2 to the power x.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([2, 3]).astype(np.float32)
>>> output = np.exp2(x)
>>> print(output)
[4. 8.]
tinyms.expand_dims(a, axis)[源代码]

Expands the shape of a tensor.

Inserts a new axis that will appear at the axis position in the expanded tensor shape.

参数
  • a (Tensor) – Input tensor array.

  • axis (Union[int, list(int), tuple(int)]) – Position in the expanded axes where the new axis is placed,

返回

Tensor, with the number of dimensions increased at specified axis.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If axis exceeds a.ndim.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.ones((2,2))
>>> x = np.expand_dims(x,0)
>>> print(x.shape)
(1, 2, 2)
tinyms.expm1(x, dtype=None)[源代码]

Calculates exp(x) - 1 for all elements in the array.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32. On CPU, the supported dtypes are np.float16, and np.float32.

参数
  • x (Tensor) – input data.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise exponential minus one, out = exp(x) - 1. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.expm1(np.arange(5).astype(np.float32))
>>> print(output)
[ 0.         1.7182819  6.389056  19.085537  53.59815  ]
tinyms.eye(N, M=None, k=0, dtype=mindspore.float32)[源代码]

Returns a 2-D tensor with ones on the diagonal and zeros elsewhere.

参数
  • N (int) – Number of rows in the output, must be larger than 0.

  • M (int, optional) – Number of columns in the output. If is None, defaults to N, if defined, must be larger than 0. Deault is None.

  • k (int, optional) – Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. Default is 0.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype. Default is mstype.float32.

返回

A tensor of shape (N, M). A tensor where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.eye(2, 2))
[[1. 0.]
[0. 1.]]
tinyms.fabs(x, dtype=None)

Calculates the absolute value element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Currently the backend kernel only supports float calculation, if the input is not a float, then it will be casted to mstype.float32 and casted back.

参数
  • x (Tensor) – Tensor to be used for calculation.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, -5], np.float32)
>>> output = np.absolute(x)
>>> print(output)
[1. 2. 3. 4. 5.]
tinyms.fix(x)[源代码]

Rounds to nearest integer towards zero.

Rounds an array of floats element-wise to nearest integer towards zero. The rounded values are returned as floats.

注解

Numpy argument out is not supported.

参数

x (Tensor) – An array of floats to be rounded.

返回

Tensor.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.fix(np.array([2.1, 2.9, -2.1, -2.9]))
>>> print(output)
[ 2.  2. -2. -2.]
tinyms.flip(m, axis=None)[源代码]

Reverses the order of elements in an array along the given axis.

The shape of the array is preserved, but the elements are reordered.

参数
  • m (Tensor) – Input array.

  • axis (None or int or tuple of integers, optional) – Axis or axes along which to flip over. The default, axis=None, will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis. If axis is a tuple of integers, flipping is performed on all of the axes specified in the tuple.

返回

Tensor, with the entries of axis reversed.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

GPU CPU

示例

>>> import mindspore.numpy as np
>>> A = np.arange(8.0).reshape((2,2,2))
>>> output = np.flip(A)
>>> print(output)
[[[7. 6]
[5. 4]]
[[3. 2]
[1. 0]]]
>>> output = np.flip(A, (0, 2))
>>> print(output)
[[[5. 4]
[7. 6]]
[[1. 0]
[3. 2]]]
tinyms.fliplr(m)[源代码]

Flips the entries in each row in the left/right direction. Columns are preserved, but appear in a different order than before.

参数

m (Tensor) – Input array.

返回

Tensor.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

GPU CPU

示例

>>> import mindspore.numpy as np
>>> A = np.arange(8.0).reshape((2,2,2))
>>> output = np.fliplr(A)
>>> print(output)
[[[2. 3.]
[0. 1.]]
[[6. 7.]
[4. 5.]]]
tinyms.flipud(m)[源代码]

Flips the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before.

参数

m (Tensor) – Input array.

返回

Tensor.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

GPU CPU

示例

>>> import mindspore.numpy as np
>>> A = np.arange(8.0).reshape((2,2,2))
>>> output = np.flipud(A)
>>> print(output)
[[[4. 5.]
[6. 7.]]
[[0. 1.]
[2. 3.]]]
tinyms.float_power(x1, x2, dtype=None)[源代码]

First array elements raised to powers from second array, element-wise.

Raise each base in x1 to the positionally-corresponding power in x2. x1 and x2 must be broadcastable to the same shape. This differs from the power function in that integers, float16, and float64 are promoted to floats with a minimum precision of float32 so that the result is always inexact. The intent is that the function will return a usable result for negative powers and seldom overflow for positive powers.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Integers and floats are promoted to float32 instead of float64.

参数
  • x1 (Tensor) – the bases.

  • x2 (Tensor) – the exponenets.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.arange(6)
>>> x2 = np.array(3)
>>> output = np.float_power(x1, x2)
>>> print(output)
[  0.   1.   8.  27.  64. 125.]
tinyms.floor(x, dtype=None)[源代码]

Returns the floor of the input, element-wise.

The floor of the scalar x is the largest integer i, such that i <= x.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16 and np.float32. On CPU, the supported dtypes are np.float16, np.float32, and np.float64.

参数
  • x (Tensor) – input data.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the floor of each element in x. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.floor(np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]))
>>> print(output)
[-2. -2. -1.  0.  1.  1.  2.]
tinyms.floor_divide(x1, x2, dtype=None)[源代码]

Returns the largest integer smaller or equal to the division of the inputs. It is equivalent to the Python // operator and pairs with the Python % (remainder), function so that a = a % b + b * (a // b) up to roundoff.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.floor_divide(np.array([1., 2., 3., 4.]), np.array(2.5))
>>> print(output)
[0. 0. 1. 1.]
tinyms.fmod(x1, x2, dtype=None)[源代码]

Returns the element-wise remainder of division.

This is the NumPy implementation of the C library function fmod, the remainder has the same sign as the dividend x1. It is equivalent to the Matlab(TM) rem function and should not be confused with the Python modulus operator x1 % x2.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – the first input arrays.

  • x2 (Tensor) – the second input arrays.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the remainder of the division of x1 by x2. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.fmod(np.array([-3, -2, -1, 1, 2, 3]), np.array(2))
>>> print(output)
[-1  0 -1  1  0  1]
tinyms.full(shape, fill_value, dtype=None)[源代码]

Returns a new tensor of given shape and type, filled with fill_value.

参数
  • shape (Union[int, tuple(int), list(int)]) – Shape of the new tensor, e.g., \((2, 3)\) or \(2\).

  • fill_value (Union[int, float, bool, list, tuple]) – Scalar or array_like fill value.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype, if dtype is None, the data type of the new tensor will be inferred from fill_value. Default is None.

返回

Tensor, with the designated shape and dtype, filled with fill_value.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If shape has entries < 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.full((2,2), True))
[[True True]
[True True]]
tinyms.full_like(a, fill_value, dtype=None, shape=None)[源代码]

Returns a full array with the same shape and type as a given array.

注解

Input array must have the same size across a dimension. If a is not a Tensor, dtype is float32 by default if not provided.

参数
  • a (Union[Tensor, list, tuple]) – The shape and data-type of a define these same attributes of the returned array.

  • fill_value (scalar) – Fill value.

  • dtype (mindspore.dtype, optional) – Overrides the data type of the result.

  • shape (int or sequence of ints, optional) – Overrides the shape of the result.

返回

Tensor, array of fill_value with the same shape and type as a.

引发

ValueError – if a is not a Tensor, list or tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((4,1,2))
>>> output = np.full_like(a, 0.5)
>>> print(output)
[[[0.5 0.5]]
[[0.5 0.5]]
[[0.5 0.5]]
[[0.5 0.5]]]
tinyms.gcd(x1, x2, dtype=None)[源代码]

Returns the greatest common divisor of |x1| and |x2|.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input data.

  • x2 (Tensor) – input data.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the greatest common divisor of the absolute value of the inputs. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.gcd(np.arange(6), np.array(20))
>>> print(output)
[20  1  2  1  4  5]
tinyms.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0)[源代码]

Returns numbers spaced evenly on a log scale (a geometric progression).

This is similar to logspace, but with endpoints specified directly. Each output sample is a constant multiple of the previous.

参数
  • start (Union[int, list(int), tuple(int), tensor]) – The starting value of the sequence.

  • stop (Union[int, list(int), tuple(int), tensor]) – The final value of the sequence, unless endpoint is False. In that case, num + 1 values are spaced over the interval in log-space, of which all but the last (a sequence of length num) are returned.

  • num (int, optional) – Number of samples to generate. Default is 50.

  • endpoint (bool, optional) – If True, stop is the last sample. Otherwise, it is not included. Default is True.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype, can be in format of np.float32, or float32.If dtype is None, infer the data type from other input arguments. Default is None.

  • axis (int, optional) – The axis in the result to store the samples. Relevant only if start or stop is array-like. By default (0), the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end. Default is 0.

返回

Tensor, with samples equally spaced on a log scale.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> output = np.geomspace(1, 256, num=9)
>>> print(output)
[  1.   2.   4.   8.  16.  32.  64. 128. 256.]
>>> output = np.geomspace(1, 256, num=8, endpoint=False)
>>> print(output)
[  1.   2.   4.   8.  16.  32.  64. 128.]
tinyms.gradient(f, *varargs, axis=None, edge_order=1)[源代码]

Returns the gradient of a N-dimensional array. The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array.

注解

Currently we only support edge_order`=1 and uniform spacing of `varargs.

参数
  • f (Union[tuple, list, Tensor]) – An N-dimensional array containing samples of a scalar function.

  • varargs (Union[tuple[number], tuple[tensor scalar]], optional) – Spacing between f values. Default unitary spacing for all dimensions. Spacing can be specified using: 1. single scalar to specify a sample distance for all dimensions. 2. N scalars to specify a constant sample distance for each dimension.

  • edge_order (int) – Gradient is calculated using N-th order accurate differences at the boundaries. Default: 1.

  • axis (Union[None, int, tuple(int), list(int)], optional) – Gradient is calculated only along the given axis or axes. The default (axis = None) is to calculate the gradient for all the axes of the input tensor. axis may be negative, in which case it counts from the last to the first axis.

返回

gradient, a list of tensors (or a single tensor if there is only one dimension to be calculated). Each derivative has the same shape as f.

引发
  • TypeError – if the inputs have types not specified above.

  • ValueError – if axis values out of bounds, or shape of f has entries < 1.

  • NotImplementedError – if edge_order != 1, or varargs contains non-scalar entries.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.gradient([[1, 2, 6], [3, 4, 5]], axis=-1)
>>> print(output)
[[1.  2.5 4. ]
[1.  1.  1. ]]
tinyms.greater(x1, x2, dtype=None)[源代码]

Returns the truth value of (x1 > x2) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype is passed. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.greater(np.array([4, 2]), np.array([2, 2]))
>>> print(output)
[ True False]
tinyms.greater_equal(x1, x2, dtype=None)[源代码]

Returns the truth value of (x1 >= x2) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype is passed. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.greater_equal(np.array([4, 2, 1]), np.array([2, 2, 2]))
>>> print(output)
[ True  True False]
tinyms.hamming(M)[源代码]

Returns the Hamming window. The Hamming window is a taper formed by using a weighted cosine.

参数

M (int) – Number of points in the output window. If zero or less, an empty array is returned.

返回

Tensor, the window, with the maximum value normalized to one (the value one appears only if the number of samples is odd).

引发

TypeError – if M is not an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.hamming(12))
[0.08000001 0.15302339 0.34890914 0.6054648  0.841236   0.9813669
0.9813668  0.8412359  0.6054647  0.34890908 0.15302327 0.08000001]
tinyms.hanning(M)[源代码]

Returns the Hanning window. The Hanning window is a taper formed by using a weighted cosine.

参数

M (int) – Number of points in the output window. If zero or less, an empty array is returned.

返回

Tensor, the window, with the maximum value normalized to one (the value one appears only if the number of samples is odd).

引发

TypeError – if M is not an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.hanning(12))
[0.         0.07937324 0.29229254 0.5711574  0.8274304  0.9797465
0.97974646 0.82743025 0.5711573  0.29229245 0.07937312 0.        ]
tinyms.heaviside(x1, x2, dtype=None)[源代码]

Computes the Heaviside step function.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input values.

  • x2 (Tensor) – The value of the function when x1 is 0. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the output array, element-wise Heaviside step function of x1. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.heaviside(np.array([-1.5, 0, 2.0]), np.array(0.5))
>>> print(output)
[0.  0.5 1. ]
>>> output = np.heaviside(np.array([-1.5, 0, 2.0]), np.array(1))
>>> print(output)
[0. 1. 1.]
tinyms.histogram(a, bins=10, range=None, weights=None, density=False)[源代码]

Computes the histogram of a dataset.

注解

String values for bins is not supported. Deprecated numpy argument normed is not supported.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input data. The histogram is computed over the flattened array.

  • bins (Union[int, tuple, list, Tensor], optional) – If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.

  • range ((float, float), optional) – The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second.

  • weights (Union[int, float, bool, list, tuple, Tensor], optional) – An array of weights, of the same shape as a. If density is True, the weights are normalized, so that the integral of the density over the range remains 1.

  • density (boolean, optional) – If False, the result will contain the number of samples in each bin. If True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function.

返回

(Tensor, Tensor), the values of the histogram and the bin edges.

引发

ValueError – if x and weights do not have the same size.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import numpy as np
>>> print(np.histogram([1, 2, 1], bins=[0, 1, 2, 3]))
(Tensor(shape=[3], dtype=Float32, value= [ 0.00000000e+00,  2.00000000e+00,  1.00000000e+00]),
Tensor(shape=[4], dtype=Int32, value= [0, 1, 2, 3]))
>>> print(np.histogram(np.arange(4), bins=np.arange(5), density=True))
(Tensor(shape=[4], dtype=Float32, value=
[ 2.50000000e-01,  2.50000000e-01,  2.50000000e-01,  2.50000000e-01]),
Tensor(shape=[5], dtype=Int32, value= [0, 1, 2, 3, 4]))
>>> print(np.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3]))
(Tensor(shape=[3], dtype=Float32, value= [ 1.00000000e+00,  4.00000000e+00,  1.00000000e+00]),
Tensor(shape=[4], dtype=Int32, value= [0, 1, 2, 3]))
tinyms.histogram2d(x, y, bins=10, range=None, weights=None, density=False)[源代码]

Computes the multidimensional histogram of some data.

注解

Deprecated numpy argument normed is not supported.

参数
  • x (Union[list, tuple, Tensor]) – An array with shape (N,) containing the x coordinates of the points to be histogrammed.

  • y (Union[list, tuple, Tensor]) – An array with shape (N,) containing the y coordinates of the points to be histogrammed.

  • bins (Union[int, tuple, list], optional) –

    The bin specification:

    If int, the number of bins for the two dimensions (nx=ny=bins).

    If array_like, the bin edges for the two dimensions (x_edges=y_edges=bins).

    If [int, int], the number of bins in each dimension (nx, ny = bins).

    If [array, array], the bin edges in each dimension (x_edges, y_edges = bins).

    A combination [int, array] or [array, int], where int is the number of bins and array is the bin edges.

  • range (Union[list, tuple], optional) – has shape (2, 2), the leftmost and rightmost edges of the bins along each dimension (if not specified explicitly in the bins parameters): [[xmin, xmax], [ymin, ymax]]. All values outside of this range will be considered outliers and not tallied in the histogram.

  • weights (Union[list, tuple, Tensor], optional) – An array with shape (N,) of values w_i weighing each sample (x_i, y_i).

  • density (boolean, optional) – If False, the default, returns the number of samples in each bin. If True, returns the probability density function at the bin, bin_count / sample_count / bin_volume.

返回

(Tensor, Tensor, Tensor), the values of the bi-directional histogram and the bin edges along the first and second dimensions.

引发

ValueError – if range does not have the same size as the number of samples.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import numpy as np
>>> x = np.arange(5)
>>> y = np.arange(2, 7)
>>> print(np.histogram2d(x, y, bins=(2, 3)))
(Tensor(shape=[2, 3], dtype=Float32, value=
[[ 2.00000000e+00,  0.00000000e+00,  0.00000000e+00],
[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00]]),
Tensor(shape=[3], dtype=Float32, value= [ 0.00000000e+00,  2.00000000e+00,  4.00000000e+00]),
Tensor(shape=[4], dtype=Float32, value=
[ 2.00000000e+00,  3.33333349e+00,  4.66666698e+00,  6.00000000e+00]))
tinyms.histogram_bin_edges(a, bins=10, range=None, weights=None)[源代码]

Function to calculate only the edges of the bins used by the histogram function.

注解

String values for bins is not supported.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input data. The histogram is computed over the flattened array.

  • bins ((Union[int, tuple, list, Tensor])) – If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.

  • range ((float, float), optional) – The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. Default is None.

  • weights (Union[int, float, bool, list, tuple, Tensor], optional) – An array of weights, of the same shape as a. Each value in a only contributes its associated weight towards the bin count (instead of 1). This is currently not used by any of the bin estimators, but may be in the future. Default is None.

返回

Tensor, the edges to pass into histogram.

Supported Platforms:

Ascend GPU CPU

引发

TypeError – if bins is an array and not one-dimensional.

实际案例

>>> import mindspore.numpy as np
>>> arr = np.array([0, 0, 0, 1, 2, 3, 3, 4, 5])
>>> print(np.histogram_bin_edges(arr, bins=2))
[0.  2.5 5. ]
tinyms.histogramdd(sample, bins=10, range=None, weights=None, density=False)[源代码]

Computes the multidimensional histogram of some data.

注解

Deprecated numpy argument normed is not supported.

参数
  • sample (Union[list, tuple, Tensor]) –

    The data to be histogrammed, either (N, D) array, or (D, N) array_like. Note the unusual interpretation of sample when an array_like:

    When an array, each row is a coordinate in a D-dimensional space, such as histogramdd(np.array([p1, p2, p3])).

    When an array_like, each element is the list of values for single coordinate, such as histogramdd((X, Y, Z)).

    The first form should be preferred.

  • bins (Union[int, tuple, list], optional) –

    The bin specification:

    A sequence of arrays describing the monotonically increasing bin edges along each dimension.

    The number of bins for each dimension (nx, ny, =bins)

    The number of bins for all dimensions (nx=ny=…=bins).

  • range (Union[list, tuple], optional) – A sequence of length D, each an optional (lower, upper) tuple giving the outer bin edges to be used if the edges are not given explicitly in bins. An entry of None in the sequence results in the minimum and maximum values being used for the corresponding dimension. The default, None, is equivalent to passing a tuple of D None values.

  • weights (Union[list, tuple, Tensor], optional) – An array with shape (N,) of values w_i weighing each sample (x_i, y_i, z_i, …).

  • density (boolean, optional) – If False, the default, returns the number of samples in each bin. If True, returns the probability density function at the bin, bin_count / sample_count / bin_volume.

返回

(Tensor, list of Tensor), the values of the histogram and the bin edges.

引发

ValueError – if range does not have the same size as the number of samples.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import numpy as np
>>> sample = np.arange(15).reshape(5, 3)
>>> print(sample)
[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 9 10 11]
[12 13 14]]
>>> print(np.histogramdd(sample, bins=(2, 3, 4)))
(Tensor(shape=[2, 3, 4], dtype=Float32, value=
[[[ 1.00000000e+00,  1.00000000e+00,  0.00000000e+00,  0.00000000e+00],
[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00]],
[[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
[ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00,  0.00000000e+00],
[ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,  2.00000000e+00]]]),
[Tensor(shape=[3], dtype=Float32, value= [ 0.00000000e+00,  6.00000000e+00,  1.20000000e+01]),
Tensor(shape=[4], dtype=Float32, value=
[ 1.00000000e+00,  5.00000000e+00,  9.00000000e+00,  1.30000000e+01]),
Tensor(shape=[5], dtype=Float32, value=
[ 2.00000000e+00,  5.00000000e+00,  8.00000000e+00,  1.10000000e+01,  1.40000000e+01])])
tinyms.hsplit(x, indices_or_sections)[源代码]

Splits a tensor into multiple sub-tensors horizontally (column-wise). It is equivalent to split with \(axis=1\) (default), the array is always split along the second axis regardless of the array dimension.

参数
  • x (Tensor) – A Tensor to be divided.

  • indices_or_sections (Union[int, tuple(int), list(int)]) – If integer, \(N\), the tensor will be divided into \(N\) equal tensors along axis. If tuple(int), list(int) or of sorted integers, the entries indicate where along axis the array is split. For example, \([2, 3]\) would, for \(axis=0\), result in three sub-tensors \(x[:2]\), \(x[2:3]\). If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.

返回

A list of sub-tensors.

引发

TypeError – If argument indices_or_sections is not integer.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.arange(6).reshape((2, 3)).astype('float32')
>>> output = np.hsplit(input_x, 3)
>>> print(output)
(Tensor(shape=[2, 1], dtype=Float32,
value=[[ 0.00000000e+00],
       [ 3.00000000e+00]]),
Tensor(shape=[2, 1], dtype=Float32,
value=[[ 1.00000000e+00],
       [ 4.00000000e+00]]),
Tensor(shape=[2, 1], dtype=Float32,
value=[[ 2.00000000e+00],
       [ 5.00000000e+00]]))
tinyms.hstack(tup)[源代码]

Stacks tensors in sequence horizontally. This is equivalent to concatenation along the second axis, except for 1-D tensors where it concatenates along the first axis.

参数

tup (Union[Tensor, tuple, list]) – A sequence of 1-D or 2-D tensors. The tensors must have the same shape along all but the second axis, except 1-D tensors which can be any length.

返回

Stacked Tensor, formed by stacking the given tensors.

Supported Platforms:

Ascend GPU CPU

引发

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([1, 2, 3]).astype('float32')
>>> x2 = np.array([4, 5, 6]).astype('float32')
>>> output = np.hstack((x1, x2))
>>> print(output)
[1. 2. 3. 4. 5. 6.]
tinyms.hypot(x1, x2, dtype=None)[源代码]

Given the “legs” of a right triangle, returns its hypotenuse.

Equivalent to sqrt(x1**2 + x2**2), element-wise. If x1 or x2 is scalar_like (i.e., unambiguously cast-able to a scalar type), it is broadcast for use with each element of the other argument. (See Examples)

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16 and np.float32. On CPU, the supported dtypes are np.float16, np.float32, and np.float64.

参数
  • x1 (Tensor) – Leg of the traingle(s).

  • x2 (Tensor) – Leg of the triangle(s). If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the hypotenuse of the triangle(s). This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.hypot(3*np.ones((3, 3)), 4*np.ones((3, 3)))
>>> print(output)
[[5. 5. 5.]
[5. 5. 5.]
[5. 5. 5.]]
>>> output = np.hypot(3*np.ones((3, 3)), np.array([4.0]))
>>> print(output)
[[5. 5. 5.]
[5. 5. 5.]
[5. 5. 5.]]
tinyms.identity(n, dtype=mindspore.float32)[源代码]

Returns the identity tensor.

参数
  • n (int) – Number of rows and columns in the output, must be larger than 0.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype, default is mstype.float32.

返回

A tensor of shape (n, n), where all elements are equal to zero, except for the diagonal, whose values are equal to one.

Supported Platforms:

Ascend GPU CPU

引发

TypeError – If input arguments have types not specified above.

实际案例

>>> import mindspore.numpy as np
>>> print(np.identity(2))
[[1. 0.]
[0. 1.]]
tinyms.in1d(ar1, ar2, invert=False)[源代码]

Tests whether each element of a 1-D array is also present in a second array.

Returns a boolean array the same length as ar1 that is True where an element of ar1 is in ar2 and False otherwise.

注解

Numpy argument assume_unique is not supported since the implementation does not rely on the uniqueness of the input arrays.

参数
  • ar1 (Union[int, float, bool, list, tuple, Tensor]) – Input array with shape (M,).

  • ar2 (Union[int, float, bool, list, tuple, Tensor]) – The values against which to test each value of ar1.

  • invert (boolean, optional) – If True, the values in the returned array are inverted (that is, False where an element of ar1 is in ar2 and True otherwise). Default is False.

返回

Tensor, with shape (M,). The values ar1[in1d] are in ar2.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> test = np.array([0, 1, 2, 5, 0])
>>> states = [0, 2]
>>> mask = np.in1d(test, states)
>>> print(mask)
[ True False  True False  True]
>>> mask = np.in1d(test, states, invert=True)
>>> print(mask)
[False  True False  True False]
tinyms.indices(dimensions, dtype=mindspore.int32, sparse=False)[源代码]

Returns an array representing the indices of a grid.

Computes an array where the subarrays contain index values 0, 1, … varying only along the corresponding axis.

参数
  • dimensions (tuple or list of ints) – The shape of the grid.

  • dtype (mindspore.dtype, optional) – Data type of the result.

  • sparse (boolean, optional) – Defaults to False. Return a sparse representation of the grid instead of a dense representation.

返回

Tensor or tuple of Tensor, If sparse is False, returns one array of grid indices, grid.shape = (len(dimensions),) + tuple(dimensions). If sparse is True, returns a tuple of arrays, with grid[i].shape = (1, ..., 1, dimensions[i], 1, ..., 1) with dimensions[i] in the ith place

引发

TypeError – if input dimensions is not a tuple or list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> grid = np.indices((2, 3))
>>> print(grid)
[Tensor(shape=[2, 3], dtype=Int32, value=
[[0, 0, 0],
[1, 1, 1]]), Tensor(shape=[2, 3], dtype=Int32, value=
[[0, 1, 2],
[0, 1, 2]])]
tinyms.inner(a, b)[源代码]

Returns the inner product of two tensors.

Ordinary inner product of vectors for 1-D tensors (without complex conjugation), in higher dimensions a sum product over the last axes.

注解

Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32. On CPU, the supported dtypes are np.float16, np.float32, and np.float64.

参数
  • a (Tensor) – input tensor. If a and b are nonscalar, their last dimensions must match.

  • b (Tensor) – input tensor. If a and b are nonscalar, their last dimensions must match.

返回

Tensor or scalar.

引发

ValueError – if x1.shape[-1] != x2.shape[-1].

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((5, 3))
>>> b = np.ones((2, 7, 3))
>>> output = np.inner(a, b)
>>> print(output)
[[[3. 3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3. 3.]]
[[3. 3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3. 3.]]
[[3. 3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3. 3.]]
[[3. 3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3. 3.]]
[[3. 3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3. 3.]]]
tinyms.interp(x, xp, fp, left=None, right=None)[源代码]

One-dimensional linear interpolation for monotonically increasing sample points. Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

注解

Numpy argument period is not supported. Complex values are not supported.

参数
  • x (Union[int, float, bool, list, tuple, Tensor]) – The x-coordinates at which to evaluate the interpolated values.

  • xp (Union[int, float, bool, list, tuple, Tensor]) – 1-D sequence of floats, the x-coordinates of the data points, must be increasing.

  • fp (Union[int, float, bool, list, tuple, Tensor]) – 1-D sequence of floats, the y-coordinates of the data points, same length as xp.

  • left (float, optional) – Value to return for x < xp[0], default is fp[0] once obtained.

  • right (float, optional) – Value to return for x > xp[-1], default is fp[-1] once obtained.

返回

Tensor, the interpolated values, same shape as x.

引发

ValueError – if xp or fp is not one-dimensional, or if xp and fp do not have the same length.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> xp = [1, 2, 3]
>>> fp = [3, 2, 0]
>>> print(np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp))
[3.         3.         2.5        0.55999994 0.        ]
>>> UNDEF = -99.0
>>> print(np.interp(3.14, xp, fp, right=UNDEF))
-99.0
tinyms.invert(x, dtype=None)[源代码]

Computes bit-wise inversion, or bit-wise NOT, element-wise. Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ~. For signed integer inputs, the two’s complement is returned. In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value. This is the most common method of representing signed integers on computers [1]. A N-bit two’s-complement system can represent every integer in the range -2^{N-1} to +2^{N-1}-1.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Supported dtypes on Ascend: np.int16, np.uint16.

参数
  • x (Tensor) – Only integer and boolean types are handled.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar.

Supported Platforms:

Ascend

实际案例

>>> import mindspore.numpy as np
>>> print(np.invert(np.array(13, dtype=np.uint16)))
65522
tinyms.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)[源代码]

Returns a boolean tensor where two tensors are element-wise equal within a tolerance.

The tolerance values are positive, typically very small numbers. The relative difference (\(rtol * abs(b)\)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

注解

For finite values, isclose uses the following equation to test whether two floating point values are equivalent. \(absolute(a - b) <= (atol + rtol * absolute(b))\) On Ascend, input arrays containing inf or NaN are not supported.

参数
  • a (Union[Tensor, list, tuple]) – Input first tensor to compare.

  • b (Union[Tensor, list, tuple]) – Input second tensor to compare.

  • rtol (numbers.Number) – The relative tolerance parameter (see Note).

  • atol (numbers.Number) – The absolute tolerance parameter (see Note).

  • equal_nan (bool) – Whether to compare NaN as equal. If True, NaN in a will be considered equal to NaN in b in the output tensor.

返回

A bool tensor of where a and b are equal within the given tolerance.

引发

TypeError – If inputs have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> a = np.array([0,1,2,float('inf'),float('inf'),float('nan')])
>>> b = np.array([0,1,-2,float('-inf'),float('inf'),float('nan')])
>>> print(np.isclose(a, b))
[ True  True False False  True False]
>>> print(np.isclose(a, b, equal_nan=True))
[ True  True False False  True  True]
tinyms.isfinite(x, dtype=None)[源代码]

Tests element-wise for finiteness (not infinity or not Not a Number).

The result is returned as a boolean array.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • x (Tensor) – Input values.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, true where x is not positive infinity, negative infinity, or NaN; false otherwise. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.isfinite(np.array([np.inf, 1., np.nan]).astype('float32'))
>>> print(output)
[False  True False]
tinyms.isin(element, test_elements, invert=False)[源代码]

Calculates element in test_elements, broadcasting over element only. Returns a boolean array of the same shape as element that is True where an element of element is in test_elements and False otherwise.

注解

Numpy argument assume_unique is not supported since the implementation does not rely on the uniqueness of the input arrays.

参数
  • element (Union[int, float, bool, list, tuple, Tensor]) – Input array.

  • test_elements (Union[int, float, bool, list, tuple, Tensor]) – The values against which to test each value of element.

  • invert (boolean, optional) – If True, the values in the returned array are inverted, as if calculating element not in test_elements. Default is False.

返回

Tensor, has the same shape as element. The values element[isin] are in test_elements.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> element = 2*np.arange(4).reshape((2, 2))
>>> test_elements = [1, 2, 4, 8]
>>> mask = np.isin(element, test_elements)
>>> print(mask)
[[False  True]
[ True False]]
>>> mask = np.isin(element, test_elements, invert=True)
>>> print(mask)
[[ True False]
[False  True]]
tinyms.isinf(x, dtype=None)[源代码]

Tests element-wise for positive or negative infinity.

Returns a boolean array of the same shape as x, True where x == +/-inf, otherwise False.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Only np.float32 is currently supported.

参数
  • x (Tensor) – Input values.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, true where x is positive or negative infinity, false otherwise. This is a scalar if x is a scalar.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.isinf(np.array(np.inf, np.float32))
>>> print(output)
True
>>> output = np.isinf(np.array([np.inf, -np.inf, 1.0, np.nan], np.float32))
>>> print(output)
[ True  True False False]
tinyms.isnan(x, dtype=None)[源代码]

Tests element-wise for NaN and return result as a boolean array.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Only np.float32 is currently supported.

参数
  • x (Tensor) – Input values.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, true where x is NaN, false otherwise. This is a scalar if x is a scalar.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.isnan(np.array(np.nan, np.float32))
>>> print(output)
True
>>> output = np.isnan(np.array(np.inf, np.float32))
>>> print(output)
False
tinyms.isneginf(x)[源代码]

Tests element-wise for negative infinity, returns result as bool array.

注解

Numpy argument out is not supported. Only np.float32 is currently supported.

参数

x (Tensor) – Input values.

返回

Tensor or scalar, true where x is negative infinity, false otherwise. This is a scalar if x is a scalar.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.isneginf(np.array([-np.inf, 0., np.inf, np.nan], np.float32))
>>> print(output)
[ True False False False]
tinyms.isposinf(x)[源代码]

Tests element-wise for positive infinity, returns result as bool array.

注解

Numpy argument out is not supported. Only np.float32 is currently supported.

参数

x (Tensor) – Input values.

返回

Tensor or scalar, true where x is positive infinity, false otherwise. This is a scalar if x is a scalar.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.isposinf(np.array([-np.inf, 0., np.inf, np.nan], np.float32))
>>> print(output)
[False False  True False]
tinyms.isscalar(element)[源代码]

Returns True if the type of element is a scalar type.

注解

Only object types recognized by the mindspore parser are supported, which includes objects, types, methods and functions defined within the scope of mindspore. Other built-in types are not supported.

参数

element (any) – Input argument, can be of any type and shape.

返回

Boolean, True if element is a scalar type, False if it is not.

引发

TypeError – if the type of element is not supported by mindspore parser.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.isscalar(3.1)
>>> print(output)
True
>>> output = np.isscalar(np.array(3.1))
>>> print(output)
False
>>> output = np.isscalar(False)
>>> print(output)
True
>>> output = np.isscalar('numpy')
>>> print(output)
True
tinyms.ix_(*args)[源代码]

Constructs an open mesh from multiple sequences.

This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions. Using ix_ one can quickly construct index arrays that will index the cross product. a[np.ix_([1,3],[2,5])] returns the array [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

注解

Boolean masks are not supported.

参数

*args (Tensor) – 1-D sequences.

返回

Tuple of Tensor, N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> ixgrid = np.ix_(np.array([0, 1]), np.array([2, 4]))
>>> print(ixgrid)
(Tensor(shape=[2, 1], dtype=Int32, value=
[[0],
[1]]), Tensor(shape=[1, 2], dtype=Int32, value=
[[2, 4]]))
tinyms.kron(a, b)[源代码]

Kronecker product of two arrays.

Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first.

注解

Booleans are not supported.

参数
返回

Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.kron([1,10,100], [5,6,7])
>>> print(output)
[  5   6   7  50  60  70 500 600 700]
>>> output = np.kron([5,6,7], [1,10,100])
>>> print(output)
[  5  50 500   6  60 600   7  70 700]
>>> output = np.kron(np.eye(2), np.ones((2,2)))
>>> print(output)
[[1. 1. 0. 0.]
[1. 1. 0. 0.]
[0. 0. 1. 1.]
[0. 0. 1. 1.]]
tinyms.lcm(x1, x2, dtype=None)[源代码]

Returns the lowest common multiple of |x1| and |x2|.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input data.

  • x2 (Tensor) – input data.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the lowest common multiple of the absolute value of the inputs. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.lcm(np.arange(6), np.array(20))
>>> print(output)
[ 0 20 20 60 20 20]
tinyms.less(x1, x2, dtype=None)[源代码]

Returns the truth value of (x1 < x2) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input array.

  • x2 (Tensor) – Input array. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype is passed. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.less(np.array([1, 2]), np.array([2, 2]))
>>> print(output)
[ True False]
tinyms.less_equal(x1, x2, dtype=None)[源代码]

Returns the truth value of (x1 <= x2) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype is passed. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.less_equal(np.array([4, 2, 1]), np.array([2, 2, 2]))
>>> print(output)
[False  True  True]
tinyms.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)[源代码]

Returns evenly spaced values within a given interval.

参数
  • start (Union[int, list(int), tuple(int), tensor]) – The starting value of the sequence.

  • stop (Union[int, list(int), tuple(int), tensor]) – The end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False.

  • num (int, optional) – Number of samples to generate. Default is 50.

  • endpoint (bool, optional) – If True, stop is the last sample. Otherwise, it is not included. Default is True.

  • retstep (bool, optional) – If True, return (samples, step), where step is the spacing between samples.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype, If dtype is None, infer the data type from other input arguments. Default is None.

  • axis (int, optional) – The axis in the result to store the samples. Relevant only if start or stop are array-like. By default, the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end. Default is 0.

返回

Tensor, with num equally spaced samples in the closed interval \([start, stop]\) or the half-open interval \([start, stop)\) (depending on whether endpoint is True or False).

Step, the size of spacing between samples, only returned if retstep is True.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.linspace(0, 5, 6))
[0. 1. 2. 3. 4. 5.]
tinyms.log(x, dtype=None)[源代码]

Returns the natural logarithm, element-wise.

The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32. On CPU, the supported dtypes are np.float16, np.float32, and np.float64.

参数
  • x (Tensor) – Input array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the natural logarithm of x, element-wise. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([2, 3, 4]).astype('float32')
>>> output = np.log(x)
>>> print(output)
[0.69314575 1.09861    1.3862929 ]
tinyms.log10(x, dtype=None)[源代码]

Base-10 logarithm of x.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([10, 100, 1000]).astype('float16')
>>> output = np.log10(x)
>>> print(output)
[1. 2. 3.]
tinyms.log1p(x, dtype=None)[源代码]

Returns the natural logarithm of one plus the input array, element-wise.

Calculates log(1 + x).

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input array.

  • dtype (mindspore.dtype) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([1, 2, 3]).astype('float16')
>>> output = np.log1p(x)
>>> print(output)
[0.6934 1.099 1.387 ]
tinyms.log2(x, dtype=None)[源代码]

Base-2 logarithm of x.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([2, 4, 8]).astype('float16')
>>> output = np.log2(x)
>>> print(output)
[1. 2. 3.]
tinyms.logaddexp(x1, x2, dtype=None)[源代码]

Logarithm of the sum of exponentiations of the inputs.

Calculates log(exp(x1) + exp(x2)). This function is useful in statistics where the calculated probabilities of events may be so small as to exceed the range of normal floating point numbers. In such cases the logarithm of the calculated probability is stored. This function allows adding probabilities stored in such a fashion.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input array.

  • x2 (Tensor) – Input array. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([1, 2, 3]).astype('float16')
>>> x2 = np.array(2).astype('float16')
>>> output = np.logaddexp(x1, x2)
>>> print(output)
[2.312 2.693 3.312]
tinyms.logaddexp2(x1, x2, dtype=None)[源代码]

Logarithm of the sum of exponentiations of the inputs in base of 2.

Calculates log2(2**x1 + 2**x2). This function is useful in machine learning when the calculated probabilities of events may be so small as to exceed the range of normal floating point numbers. In such cases the base-2 logarithm of the calculated probability can be used instead. This function allows adding probabilities stored in such a fashion.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input tensor.

  • x2 (Tensor) – Input tensor. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([2, 4, 8]).astype('float16')
>>> x2 = np.array(2).astype('float16')
>>> output = np.logaddexp2(x1, x2)
>>> print(output)
[3. 4.32 8.02]
tinyms.logical_and(x1, x2, dtype=None)[源代码]

Computes the truth value of x1 AND x2 element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input tensor.

  • x2 (Tensor) – Input tensor. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. Boolean result of the logical AND operation applied to the elements of x1 and x2; the shape is determined by broadcasting. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([True, False])
>>> x2 = np.array([False, False])
>>> output = np.logical_and(x1, x2)
>>> print(output)
[False False]
tinyms.logical_not(a, dtype=None)[源代码]

Computes the truth value of NOT a element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • a (Tensor) – The input tensor whose dtype is bool.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. Boolean result with the same shape as a of the NOT operation on elements of a. This is a scalar if a is a scalar.

引发

TypeError – if the input is not a tensor or its dtype is not bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([True, False])
>>> output = np.logical_not(a)
>>> print(output)
[False  True]
tinyms.logical_or(x1, x2, dtype=None)[源代码]

Computes the truth value of x1 OR x2 element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input tensor.

  • x2 (Tensor) – Input tensor. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype=object is passed. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([True, False])
>>> x2 = np.array([False, True])
>>> output = np.logical_or(x1, x2)
>>> print(output)
[ True  True]
tinyms.logical_xor(x1, x2, dtype=None)[源代码]

Computes the truth value of x1 XOR x2, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – Input tensor.

  • x2 (Tensor) – Input tensor. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. Boolean result of the logical AND operation applied to the elements of x1 and x2; the shape is determined by broadcasting. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([True, False])
>>> x2 = np.array([False, False])
>>> output = np.logical_xor(x1, x2)
>>> print(output)
[True False]
tinyms.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)[源代码]

Returns numbers spaced evenly on a log scale.

In linear space, the sequence starts at base ** start (base to the power of start) and ends with base ** stop (see endpoint below).

参数
  • start (Union[int, list(int), tuple(int), tensor]) – base ** start is the starting value of the sequence.

  • stop (Union[int, list(int), tuple(int), tensor]) – base ** stop is the final value of the sequence, unless endpoint is False. In that case, num + 1 values are spaced over the interval in log-space, of which all but the last (a sequence of length num) are returned.

  • num (int, optional) – Number of samples to generate. Default is 50.

  • endpoint (bool, optional) – If True, stop is the last sample. Otherwise, it is not included. Default is True.

  • base (Union[int, float], optional) – The base of the log space. The step size between the elements in \(ln(samples) / ln(base)\) (or \(log_{base}(samples)\)) is uniform. Default is 10.0.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype. If dtype is None, infer the data type from other input arguments. Default is None.

  • axis (int, optional) – The axis in the result to store the samples. Relevant only if start or stop is array-like. By default, the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end. Default is 0.

返回

Tensor, equally spaced on a log scale.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.logspace(0, 5, 6, base=2.0))
[ 1.  2.  4.  8. 16. 32.]
tinyms.matmul(x1, x2, dtype=None)[源代码]

Returns the matrix product of two arrays.

注解

Numpy arguments out, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16 and np.float32. On CPU, the supported dtypes are np.float16 and np.float32.

参数
  • x1 (Tensor) – Input tensor, scalar not allowed.

  • x2 (Tensor) – Input tensor, scalar not allowed.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors.

引发

ValueError – If the last dimension of x1 is not the same size as the second-to-last dimension of x2, or if a scalar value is passed in.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.arange(2*3*4).reshape(2, 3, 4).astype('float32')
>>> x2 = np.arange(4*5).reshape(4, 5).astype('float32')
>>> output = np.matmul(x1, x2)
>>> print(output)
[[[  70.   76.   82.   88.   94.]
[ 190.  212.  234.  256.  278.]
[ 310.  348.  386.  424.  462.]]
[[ 430.  484.  538.  592.  646.]
[ 550.  620.  690.  760.  830.]
[ 670.  756.  842.  928. 1014.]]]
tinyms.max(a, axis=None, keepdims=False, initial=None, where=True)

Returns the maximum of an array or maximum along an axis.

注解

Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • a (Tensor) – Input data.

  • axis (None or int or tuple of integers, optional) – defaults to None. Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of integers, the maximum is selected over multiple axes, instead of a single axis or all the axes as before.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • initial (scalar, optional) – defaults to None. The minimum value of an output element. Must be present to allow computation on empty slice.

  • where (boolean Tensor, optional) – defaults to True. A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided.

返回

Tensor or scalar, maximum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(4).reshape((2,2)).astype('float32')
>>> output = np.amax(a)
>>> print(output)
3.0
>>> output = np.amax(a, axis=0)
>>> print(output)
[2. 3.]
>>> output = np.amax(a, axis=1)
>>> print(output)
[1. 3.]
>>> output = np.amax(a, where=np.array([False, True]), initial=-1, axis=0)
>>> print(output)
[-1.  3.]
tinyms.maximum(x1, x2, dtype=None)[源代码]

Returns the element-wise maximum of array elements.

Compares two arrays and returns a new array containing the element-wise maxima.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On Ascend, input arrays containing inf or NaN are not supported.

参数
  • x1 (Tensor) – Input array

  • x2 (Tensor) – The array holding the elements to be compared. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the maximum of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.maximum(np.array([2, 3, 4]), np.array([1, 5, 2]))
>>> print(output)
[2 5 4]
tinyms.mean(a, axis=None, keepdims=False, dtype=None)[源代码]

Computes the arithmetic mean along the specified axis.

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.

注解

Numpy arguments out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • a (Tensor) – input tensor containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

  • axis (None or int or tuple of integers, optional) – Axis or axes along which the means are computed. The default is to compute the mean of the flattened array. If this is a tuple of ints, a mean is performed over multiple axes.

  • keepdims (bool, optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input tensor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, an array containing the mean values.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(6, dtype='float32')
>>> output = np.mean(a, 0)
>>> print(output)
2.5
tinyms.meshgrid(*xi, sparse=False, indexing='xy')[源代码]

Returns coordinate matrices from coordinate vectors.

Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.

注解

Numpy argument copy is not supported, and a copy is always returned.

参数
  • *xi (Tensor) – 1-D arrays representing the coordinates of a grid.

  • indexing (‘xy’, ‘ij’, optional) – Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. In the 2-D case with inputs of length M and N, the outputs are of shape (N, M) for ‘xy’ indexing and (M, N) for ‘ij’ indexing. In the 3-D case with inputs of length M, N and P, outputs are of shape (N, M, P) for ‘xy’ indexing and (M, N, P) for ‘ij’ indexing.

  • sparse (bool, optional) – If True a sparse grid is returned in order to conserve memory. Default is False.

返回

Tuple of tensors, for vectors x1, x2,…, xn with lengths Ni=len(xi), return (N1, N2, N3,…Nn) shaped arrays if indexing=’ij’ or (N2, N1, N3,…Nn) shaped arrays if indexing=’xy’ with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.

引发

TypeError – if the input is not a tensor, or sparse is not boolean, or indexing is not ‘xy’ or ‘ij’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.linspace(0, 1, 3)
>>> y = np.linspace(0, 1, 2)
>>> xv, yv = np.meshgrid(x, y)
>>> print(xv)
[[0.  0.5 1. ]
[0.  0.5 1. ]]
>>> print(yv)
[[0.  0.  0.]
[1.  1.  1.]]
>>> xv, yv = np.meshgrid(x, y, sparse=True)
>>> print(xv)
[[0.  0.5  1. ]]
>>> print(yv)
[[0.]
[1.]]
tinyms.min(a, axis=None, keepdims=False, initial=None, where=True)

Returns the minimum of an array or minimum along an axis.

注解

Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • a (Tensor) – Input data.

  • axis (None or int or tuple of integers, optional) – defaults to None. Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of integers, the minimum is selected over multiple axes, instead of a single axis or all the axes as before.

  • keepdims (bool, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • initial (Number, optional) – defaults to None. The maximum value of an output element. Must be present to allow computation on empty slice.

  • where (bool Tensor, optional) – defaults to True. A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided.

返回

Tensor or scalar, minimum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(4).reshape((2,2)).astype('float32')
>>> output = np.amin(a)
>>> print(output)
0.0
>>> output = np.amin(a, axis=0)
>>> print(output)
[0. 1.]
>>> output = np.amin(a, axis=1)
>>> print(output)
[0. 2.]
>>> output = np.amin(a, where=np.array([False, True]), initial=10, axis=0)
>>> print(output)
[10.  1.]
tinyms.minimum(x1, x2, dtype=None)[源代码]

Element-wise minimum of tensor elements.

Compares two tensors and returns a new tensor containing the element-wise minima.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On Ascend, input arrays containing inf or NaN are not supported.

参数
  • x1 (Tensor) – first input tensor to be compared.

  • x2 (Tensor) – second input tensor to be compared.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor, element-wise minimum of x1 and x2.

引发
  • TypeError – If inputs have types not specified above.

  • ValueError – If the shapes of x1 and x2 cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.asarray([1, 2])
>>> b = np.asarray([[1, 3],[1, 4]])
>>> print(np.minimum(a, b))
[[1 2]
[1 2]]
tinyms.mod(x1, x2, dtype=None)

Returns element-wise remainder of division.

Computes the remainder complementary to the floor_divide function. It is equivalent to the Python modulus operator x1 % x2 and has the same sign as the divisor x2. The MATLAB function equivalent to np.remainder is mod.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input array.

  • x2 (Tensor) – input array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the element-wise remainder of the quotient floor_divide(x1, x2). This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.remainder(np.array([4, 7]), np.array([2, 3]))
>>> print(output)
[0 1]
>>> output = np.remainder(np.arange(7), np.array(5))
>>> print(output)
[0 1 2 3 4 0 1]
tinyms.moveaxis(a, source, destination)[源代码]

Moves axes of an array to new positions.

Other axes remain in their original order.

参数
  • a (Tensor) – The array whose axes should be reordered.

  • source (int or sequence of ints) – Original positions of the axes to move. These must be unique.

  • destination (int or sequence of ints) – Destination positions for each of the original axes. These must also be unique.

返回

Tensor, array with moved axes.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.zeros((3, 4, 5))
>>> output = np.moveaxis(x, 0, -1)
>>> print(output.shape)
(4, 5, 3)
>>> output = np.moveaxis(x, -1, 0)
>>> print(output.shape)
(5, 3, 4)
>>> output = np.moveaxis(x, [0, 1, 2], [-1, -2, -3])
>>> print(output.shape)
(5, 4, 3)
tinyms.multi_dot(arrays)[源代码]

Computes the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order. multi_dot chains numpy.dot and uses optimal parenthesization of the matrices [1] <en.wikipedia.org/wiki/Matrix_chain_multiplication>. Depending on the shapes of the matrices, this can speed up the multiplication a lot. If the first argument is 1-D it is treated as a row vector. If the last argument is 1-D it is treated as a column vector. The other arguments must be 2-D.

注解

Numpy argument out is not supported.

参数

arrays (sequence of array_like) – If the first argument is 1-D it is treated as row vector. If the last argument is 1-D it is treated as column vector. The other arguments must be 2-D.

返回

Tensor, the dot product of the supplied arrays.

引发

ValueError – arrays are not 2-D.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> A = np.ones((10000, 100))
>>> B = np.ones((100, 1000))
>>> C = np.ones((1000, 5))
>>> D = np.ones((5, 333))
>>> output = np.multi_dot([A, B, C, D])
>>> print(output)
[[500000. 500000. 500000. ... 500000. 500000. 500000.]
[500000. 500000. 500000. ... 500000. 500000. 500000.]
[500000. 500000. 500000. ... 500000. 500000. 500000.]
...
[500000. 500000. 500000. ... 500000. 500000. 500000.]
[500000. 500000. 500000. ... 500000. 500000. 500000.]
[500000. 500000. 500000. ... 500000. 500000. 500000.]]
tinyms.multiply(x1, x2, dtype=None)[源代码]

Multiplies arguments element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input tensor to be multiplied.

  • x2 (Tensor) – input tensor to be multiplied.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the product of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.full((3, 2), [1, 2])
>>> x2 = np.full((3, 2), [3, 4])
>>> output = np.multiply(x1, x2)
>>> print(output)
[[3 8]
[3 8]
[3 8]]
tinyms.nancumsum(a, axis=None, dtype=None)[源代码]

Return the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros.

Zeros are returned for slices that are all-NaN or empty.

注解

If a.dtype is int8, int16 or bool, the result dtype will be elevated to int32.

参数
  • a (Tensor) – Input tensor.

  • axis (int, optional) – Axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array.

  • dtype (mindspore.dtype, optional) – If not specified, stay the same as a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used.

返回

Tensor.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If axis is out of range.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([[1, 2], [3, np.nan]])
>>> output = np.nancumsum(a)
>>> print(output)
[1. 3. 6. 6.]
>>> output = np.nancumsum(a, axis=0)
>>> print(output)
[[1. 2.]
[4. 2.]]
>>> output = np.nancumsum(a, axis=1)
>>> print(output)
[[1. 3.]
[3. 3.]]
tinyms.nanmax(a, axis=None, dtype=None, keepdims=False)[源代码]

Return the maximum of an array or maximum along an axis, ignoring any NaNs.

注解

Numpy arguments out is not supported. For all NaN slices, a very small negative number is returned instead of NaN.

参数
  • a (Union[int, float, list, tuple, Tensor]) – Array containing numbers whose maximum is desired. If a is not an array, a conversion is attempted.

  • axis (Union[int, tuple of int, None], optional) – Axis or axes along which the maximum is computed. The default is to compute the maximum of the flattened array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a.

返回

Tensor.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([[1, 2], [3, np.nan]])
>>> output = np.nanmax(a)
>>> print(output)
3.0
>>> output = np.nanmax(a, axis=0)
>>> print(output)
[3. 2.]
tinyms.nanmean(a, axis=None, dtype=None, keepdims=False)[源代码]

Computes the arithmetic mean along the specified axis, ignoring NaNs.

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float32 intermediate and return values are used for integer inputs.

注解

Numpy arguments out is not supported.

参数
  • a (Union[int, float, list, tuple, Tensor]) – Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

  • axis (Union[int, tuple of int, None], optional) – Axis or axes along which the mean is computed. The default is to compute the mean of the flattened array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a.

返回

Tensor.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([[1, np.nan], [3, 4]])
>>> output = np.nanmean(a)
>>> print(output)
2.6666667
>>> output = np.nanmean(a, axis=0)
>>> print(output)
[2. 4.]
>>> output = np.nanmean(a, axis=1)
>>> print(output)
[1.  3.5]
tinyms.nanmin(a, axis=None, dtype=None, keepdims=False)[源代码]

Returns the minimum of array elements over a given axis, ignoring any NaNs.

注解

Numpy arguments out is not supported. For all-NaN slices, a very large number is returned instead of NaN. On Ascend, since checking for NaN is currently not supported, it is not recommended to use np.nanmin. If the array does not contain NaN, np.min should be used instead.

参数
  • a (Union[int, float, list, tuple, Tensor]) – Array containing numbers whose minimum is desired. If a is not an array, a conversion is attempted.

  • axis (Union[int, tuple of int, None], optional) – Axis or axes along which the minimum is computed. The default is to compute the minimum of the flattened array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a.

返回

Tensor.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([[1, 2], [3, np.nan]])
>>> output = np.nanmin(a)
>>> print(output)
1.0
>>> output = np.nanmin(a, axis=0)
>>> print(output)
[1. 2.]
tinyms.nanstd(a, axis=None, dtype=None, ddof=0, keepdims=False)[源代码]

Computes the standard deviation along the specified axis, while ignoring NaNs.

Returns the standard deviation, a measure of the spread of a distribution, of the non-NaN array elements. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.

注解

Numpy arguments out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • a (Union[int, float, list, tuple, Tensor]) – Calculates the standard deviation of the non-NaN values.

  • axis (Union[int, tuple of int, None], optional) – Axis or axes along which the standard deviation is computed. The default is to compute the standard deviation of the flattened array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • ddof (int, optional) – “Delta Degrees of Freedom”: the divisor used in the calculation is N - ddof, where N represents the number of non-NaN elements. By default ddof is zero.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a.

返回

Tensor.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([[1, np.nan], [3, 4]])
>>> output = np.nanstd(a)
>>> print(output)
1.2472192
>>> output = np.nanstd(a, axis=0)
>>> print(output)
[1. 0.]
>>> output = np.nanstd(a, axis=1)
>>> print(output)
[0.  0.5]
tinyms.nansum(a, axis=None, dtype=None, keepdims=False)[源代码]

Returns the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.

注解

Numpy arguments out is not supported.

参数
  • a (Union[int, float, list, tuple, Tensor]) – Array containing numbers whose sum is desired. If a is not an array, a conversion is attempted.

  • axis (Union[int, tuple of int, None], optional) – Axis or axes along which the sum is computed. The default is to compute the sum of the flattened array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a.

返回

Tensor.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([[1, 1], [1, np.nan]])
>>> output = np.nansum(a)
>>> print(output)
3.0
>>> output = np.nansum(a, axis=0)
>>> print(output)
[2. 1.]
tinyms.nanvar(a, axis=None, dtype=None, ddof=0, keepdims=False)[源代码]

Computes the variance along the specified axis, while ignoring NaNs.

Returns the variance of the array elements, a measure of the spread of a distribution. The variance is computed for the flattened array by default, otherwise over the specified axis.

注解

Numpy arguments out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • a (Union[int, float, list, tuple, Tensor]) – Array containing numbers whose variance is desired. If a is not an array, a conversion is attempted.

  • axis (Union[int, tuple of int, None], optional) – Axis or axes along which the variance is computed. The default is to compute the variance of the flattened array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • ddof (int, optional) – “Delta Degrees of Freedom”: the divisor used in the calculation is N - ddof, where N represents the number of non-NaN elements. By default ddof is zero.

  • keepdims (boolean, optional) – defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original a.

返回

Tensor.

引发

ValueError – if axes are out of the range of [-a.ndim, a.ndim), or if the axes contain duplicates.

Supported Platforms:

GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([[1, np.nan], [3, 4]])
>>> output = np.nanvar(a)
>>> print(output)
1.5555557
>>> output = np.nanvar(a, axis=0)
>>> print(output)
[1. 0.]
>>> output = np.nanvar(a, axis=1)
>>> print(output)
[0.   0.25]
tinyms.negative(a, dtype=None)[源代码]

Numerical negative, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • a (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.asarray([1, -1]).astype('float32')
>>> output = np.negative(a)
>>> print(output)
[-1. 1.]
tinyms.norm(x, ord=None, axis=None, keepdims=False)[源代码]

Matrix or vector norm. This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.

注解

Nuclear norm and 2-norm are not supported for matrices.

参数
  • x (Union[int, float, bool, list, tuple, Tensor]) – Input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned.

  • ord (Union[None, 'fro', 'nuc', inf, -inf, int, float], optional) – Order of the norm. inf means numpy’s inf object. The default is None.

  • axis (Union[None, int, 2-tuple of integers], optional) – If axis is an integer, it specifies the axis of x along which to compute the vector norms. If axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned. The default is None.

  • keepdims (boolean, optional) – If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.

返回

Tensor, norm of the matrix or vector(s).

引发

ValueError – If the norm order is not defined.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.norm(np.arange(9).astype(np.float32)))
14.282857
tinyms.not_equal(x1, x2, dtype=None)[源代码]

Returns (x1 != x2) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – First input tensor to be compared.

  • x2 (Tensor) – Second input tensor to be compared.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype is passed. This is a scalar if both x1 and x2 are scalars.

引发

TypeError – If the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.asarray([1, 2])
>>> b = np.asarray([[1, 3],[1, 4]])
>>> print(np.not_equal(a, b))
[[False  True]
[False  True]]
tinyms.not_equal(x1, x2, dtype=None)[源代码]

Returns (x1 != x2) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – First input tensor to be compared.

  • x2 (Tensor) – Second input tensor to be compared.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise comparison of x1 and x2. Typically of type bool, unless dtype is passed. This is a scalar if both x1 and x2 are scalars.

引发

TypeError – If the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.asarray([1, 2])
>>> b = np.asarray([[1, 3],[1, 4]])
>>> print(np.not_equal(a, b))
[[False  True]
[False  True]]
tinyms.ones(shape, dtype=mindspore.float32)[源代码]

Returns a new tensor of given shape and type, filled with ones.

参数
  • shape (Union[int, tuple, list]) – the shape of the new tensor.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype. Default is mstype.float32.

返回

Tensor, with the designated shape and dtype, filled with ones.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If shape entries have values \(< 0\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.ones((2,2)))
[[1. 1.]
[1. 1.]]
tinyms.ones_like(a, dtype=None, shape=None)[源代码]

Returns an array of ones with the same shape and type as a given array.

注解

Input array must have the same size across a dimension. If a is not a Tensor, dtype is float32 by default if not provided.

参数
  • a (Union[Tensor, list, tuple]) – The shape and data-type of a define these same attributes of the returned array.

  • dtype (mindspore.dtype, optional) – Overrides the data type of the result.

  • shape (int or sequence of ints, optional) – Overrides the shape of the result.

返回

Tensor, array of ones with the same shape and type as a.

引发

ValueError – if a is not a Tensor, list or tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((4,1,2))
>>> output = np.ones_like(a)
>>> print(output)
[[[1. 1.]]
[[1. 1.]]
[[1. 1.]]
[[1. 1.]]]
tinyms.outer(a, b)[源代码]

Computes the outer product of two vectors.

Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product is:

[[a0*b0  a0*b1 ... a0*bN ]

[a1*b0    .              ]

[ ...          .         ]

[aM*b0            aM*bN ]]

注解

Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32. On CPU, the supported dtypes are np.float16, np.float32, and np.float64.

参数
  • a (Tensor) – first input vector. Input is flattened if not already 1-dimensional.

  • b (Tensor) – second input vector. Input is flattened if not already 1-dimensional.

返回

Tensor or scalar, out[i, j] = a[i] * b[j].

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.full(7, 2).astype('float32')
>>> b = np.full(4, 3).astype('float32')
>>> output = np.outer(a, b)
>>> print(output)
[[6. 6. 6. 6.]
[6. 6. 6. 6.]
[6. 6. 6. 6.]
[6. 6. 6. 6.]
[6. 6. 6. 6.]
[6. 6. 6. 6.]
[6. 6. 6. 6.]]
tinyms.pad(arr, pad_width, mode='constant', stat_length=None, constant_values=0, end_values=0, reflect_type='even', **kwargs)[源代码]

Pads an array.

注解

Currently, median mode is not supported. reflect and symmetric mode only supports GPU backend.

参数
  • arr (Union[list, tuple, Tensor]) – The array to pad.

  • pad_width (Union[int, tuple, list]) – Number of values padded to the edges of each axis. ((before_1, after_1), ... (before_N, after_N)) creates unique pad widths for each axis. ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes.

  • mode (string, optional) –

    One of the following string values:

    • constant (default): Pads with a constant value.

    • edge: Pads with the edge values of arr.

    • linear_ramp: Pads with the linear ramp between end_value and the arr edge value.

    • maximum: Pads with the maximum value of all or part of the vector along each axis.

    • mean: Pads with the mean value of all or part of the vector along each axis.

    • median: Pads with the median value of all or part of the vector along each axis.

    • minimum: Pads with the minimum value of all or part of the vector along each axis.

    • reflect: Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.

    • symmetric: Pads with the reflection of the vector mirrored along the edge of the arr.

    • wrap: Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.

    • empty: Pads with undefined values.

    • <function>: The padding function, if used, should modify and return a new 1-d tensor. It has the following signature: padding_func(tensor, iaxis_pad_width, iaxis, kwargs)

  • stat_length (Union[tuple, list, int], optional) – Used in ‘maximum’, ‘mean’, ‘median’, and ‘minimum’. Number of values at edge of each axis used to calculate the statistic value. ((before_1, after_1), ... (before_N, after_N)) creates unique statistic lengths for each axis. ((before, after),) yields same before and after statistic lengths for each axis. (stat_length,) or int is a shortcut for before = after = statistic length for all axes. Default is None, to use the entire axis.

  • constant_values (Union[tuple, list, int], optional) – Used in constant mode. The values to set the padded values for each axis. ((before_1, after_1), ... (before_N, after_N)) creates unique pad constants for each axis. ((before, after),) yields same before and after constants for each axis. (constant,) or constant is a shortcut for before = after = constant for all axes. Default is 0.

  • end_values (Union[tuple, list, int], optional) – Used in ‘linear_ramp’. The values used for the ending value of the linear_ramp and that will form the edge of the padded arr. ((before_1, after_1), ... (before_N, after_N)) unique end values for each axis. ((before, after),) yields same before and after end values for each axis. (constant,) or constant is a shortcut for before = after = constant for all axes. Default is 0.

  • reflect_type (string, optional) – ‘reflect’, and ‘symmetric’. The ‘even’ style is the default with an unaltered reflection around the edge value. For the ‘odd’ style, the extended part of the arr is created by subtracting the reflected values from two times the edge value.

返回

Padded tensor of rank equal to arr with shape increased according to pad_width.

引发
  • TypeError – if arr, pad_width, stat_length, constant_values or end_values have types not specified above.

  • ValueError – if mode cannot be recognized, or if pad_width, stat_length, constant_values, end_values cannot broadcast to (arr.ndim, 2), or if keyword arguments got unexpected inputs.

  • NotImplementedError – if mode is function or ‘/median’/.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> tensor = np.array([1., 2., 3., 4., 5.])
>>> print(np.pad(tensor, (3, 4)))
[0. 0. 0. 1. 2. 3. 4. 5. 0. 0. 0. 0.]
>>> print(np.pad(tensor, (3, 4), mode="wrap"))
[3. 4. 5. 1. 2. 3. 4. 5. 1. 2. 3. 4.]
>>> print(np.pad(tensor, (3, 4), mode="linear_ramp", end_values=(10, 10)))
[10.    7.    4.    1.    2.    3.    4.    5.    6.25  7.5   8.75 10.  ]
tinyms.piecewise(x, condlist, funclist, *args, **kw)[源代码]

Evaluates a piecewise-defined function. Given a set of conditions and corresponding functions, evaluate each function on the input data wherever its condition is true.

参数
  • x (Union[int, float, bool, list, tuple, Tensor]) – The input domain.

  • condlist (Union[bool, list of bool Tensor]) – Each boolean array corresponds to a function in funclist. Wherever condlist[i] is True, funclist[i](x) is used as the output value. Each boolean array in condlist selects a piece of x, and should therefore be of the same shape as x. The length of condlist must correspond to that of funclist. If one extra function is given, i.e. if len(funclist) == len(condlist) + 1, then that extra function is the default value, used wherever all conditions are false.

  • funclist (Union[list of callables, list of scalars]) – Each function is evaluated over x wherever its corresponding condition is True. It should take a 1d array as input and give an 1d array or a scalar value as output. If, instead of a callable, a scalar is provided then a constant function (lambda x: scalar) is assumed.

  • args (any) – Any further arguments given to piecewise are passed to the functions upon execution, i.e., if called piecewise(..., ..., 1, 'a'), then each function is called as f(x, 1, 'a').

  • kw (any) – Keyword arguments used in calling piecewise are passed to the functions upon execution, i.e., if called piecewise(..., ..., alpha=1), then each function is called as f(x, alpha=1).

返回

Tensor, the output is the same shape and type as x and is found by calling the functions in funclist on the appropriate portions of x, as defined by the boolean arrays in condlist. Portions not covered by any condition have a default value of 0.

Supported Platforms:

Ascend GPU CPU

引发

ValueError – if length of funclist is not in (len(condlist), len(condlist) + 1)

实际案例

>>> import mindspore.numpy as np
>>> x = np.linspace(-2.5, 2.5, 6)
>>> print(np.piecewise(x, [x < 0, x >= 0], [-1, 1]))
[-1 -1 -1  1  1  1]
tinyms.polyadd(a1, a2)[源代码]

Finds the sum of two polynomials. Returns the polynomial resulting from the sum of two input polynomials.

注解

Numpy object poly1d is currently not supported.

参数
返回

Tensor, the sum of the inputs.

引发

ValueError – if the input array has more than 1 dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.polyadd([1, 2], [9, 5, 4]))
[9 6 6]
tinyms.polyder(p, m=1)[源代码]

Returns the derivative of the specified order of a polynomial.

注解

Numpy object poly1d is currently not supported.

参数
  • p (Union[int, float, bool, list, tuple, Tensor) – Polynomial to differentiate. A sequence is interpreted as polynomial coefficients.

  • m (int, optional) – Defaults to 1, order of differentiation.

返回

Tensor, a new polynomial representing the derivative.

引发

ValueError – if p has more than 1 dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.polyder([1, 1, 1, 1]))
[3 2 1]
tinyms.polyint(p, m=1, k=None)[源代码]

Returns an antiderivative (indefinite integral) of a polynomial.

注解

Numpy object poly1d is currently not supported.

参数
  • p (Union[int, float, bool, list, tuple, Tensor) – Polynomial to integrate. A sequence is interpreted as polynomial coefficients.

  • m (int, optional) – Defaults to 1, Order of the antiderivative.

  • k (Union[int, list of int]y, optinoal) – Integration constants. They are given in the order of integration: those corresponding to highest-order terms come first. If None (default), all constants are assumed to be zero. If m = 1, a single scalar can be given instead of a list.

返回

Tensor, a new polynomial representing the antiderivative.

引发

ValueError – if p has more than 1 dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.polyint([1, 1, 1]))
[0.33333334 0.5        1.         0.        ]
tinyms.polymul(a1, a2)[源代码]

Finds the product of two polynomials.

注解

Numpy object poly1d is currently not supported.

参数
返回

Tensor, a new polynomial representing the derivative.

引发

ValueError – if the input array has more than 1 dimensions.

Supported Platforms:

GPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.polymul([3, 1, 2], [2, 5]))
[ 6 17  9 10]
tinyms.polysub(a1, a2)[源代码]

Difference (subtraction) of two polynomials. Given two polynomials a1 and a2, returns a1 - a2.

注解

Numpy object poly1d is currently not supported.

参数
返回

Tensor, the difference of the inputs.

引发

ValueError – if the input array has more than 1 dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.polysub([2, 10, -2], [3, 10, -4]))
[-1  0  2]
tinyms.polyval(p, x)[源代码]

Evaluates a polynomial at specific values. If p is of length N, this function returns the value: p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1] If x is a sequence, then p(x) is returned for each element of x. If x is another polynomial then the composite polynomial p(x(t)) is returned.

注解

Numpy object poly1d is currently not supported.

参数
  • p (Union[int, float, bool, list, tuple, Tensor) – 1D array of polynomial coefficients (including coefficients equal to zero) from highest degree to the constant term.

  • x (Union[int, float, bool, list, tuple, Tensor) – A number, an array of numbers, at which to evaluate p.

返回

Tensor.

引发

ValueError – if p has more than 1 dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.polyval([3.,0.,1.], 5.))
76.0
tinyms.positive(a, dtype=None)[源代码]

Numerical positive, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • a (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.asarray([1, -1]).astype('float32')
>>> output = np.positive(a)
>>> print(output)
[1. -1.]
tinyms.power(x1, x2, dtype=None)[源代码]

First array elements raised to powers from second array, element-wise.

Raises each base in x1 to the positionally-corresponding power in x2.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32.

参数
  • x1 (Tensor) – the bases.

  • x2 (Tensor) – the exponents.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.full((3, 2), [1, 2]).astype('float32')
>>> x2 = np.full((3, 2), [3, 4]).astype('float32')
>>> output = np.power(x1, x2)
>>> print(output)
[[ 1. 16.]
[ 1. 16.]
[ 1. 16.]]
tinyms.promote_types(type1, type2)[源代码]

Returns the data type with the smallest size and smallest scalar kind.

注解

The promotion rule is slightly different from original Numpy, but more like jax, due to the preference on 32-bit over 64-bit data types.

参数
  • type1 (Union[mindspore.dtype, str]) – First data type.

  • type2 (Union[mindspore.dtype, str]) – Second data type.

返回

The promoted data type.

引发

TypeError – if the input are not valid mindspore.dtype input.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.promote_types(np.float32, np.float64)
>>> print(output)
Float64
tinyms.ptp(x, axis=None, keepdims=False)[源代码]

Range of values (maximum - minimum) along an axis. The name of the function comes from the acronym for ‘peak to peak’.

注解

Numpy arguments dtype and out are not supported.

参数
  • x (Tensor) – Input tensor.

  • axis (Union[None, int, tuple(int)]) – Axis or axes along which the range is computed. The default is to compute the variance of the flattened array. Default: None.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input tensor. If the default value is passed, then keepdims will not be passed through to the ptp method of sub-classes of tensor, however any non-default value will be. Default is False.

返回

Tensor.

引发

TypeError – if inputs have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([[4.0, 9.0, 2.0, 10.0], [6.0, 9.0, 7.0, 12.0]])
>>> print(np.ptp(x, axis=1))
[8. 6.]
>>> print(np.ptp(x, axis=0))
[2. 0. 5. 2.]
tinyms.rad2deg(x, dtype=None)[源代码]

Converts angles from radians to degrees.

参数
  • x (Tensor) – Angles in radians.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor, the corresponding angle in degrees. This is a tensor scalar if x is a tensor scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, -5])
>>> output = np.rad2deg(x)
>>> print(output)
[  57.295776  114.59155   171.88733  -229.1831   -286.47888 ]
tinyms.radians(x, dtype=None)[源代码]

Converts angles from degrees to radians.

参数
  • x (Tensor) – Angles in degrees.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor, the corresponding radian values. This is a tensor scalar if x is a tensor scalar.

引发

TypeError – if x is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([1, 2, 3, -4, -5])
>>> output = np.radians(x)
>>> print(output)
[ 0.01745329  0.03490658  0.05235988 -0.06981317 -0.08726647]
tinyms.ravel(x)[源代码]

Returns a contiguous flattened tensor.

A 1-D tensor, containing the elements of the input, is returned.

参数

x (Tensor) – A tensor to be flattened.

返回

Flattened tensor, has the same data type as the original tensor x.

引发

TypeError – If x is not tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.ones((2,3,4))
>>> output = np.ravel(x)
>>> print(output.shape)
(24,)
tinyms.ravel_multi_index(multi_index, dims, mode='clip', order='C')[源代码]

Converts a tuple of index arrays into an array of flat indices, applying boundary modes to the multi-index.

注解

raise mode is not supported. Default mode is clip.

参数
  • multi_index (tuple of array_like) – A tuple of integer arrays, one array for each dimension.

  • dims (Union[int, tuple of integers]) – The shape of array into which the indices from multi_index apply.

  • mode ({wrap, clip}) –

    Specifies how out-of-bounds indices are handled. Default: clip.

    • wrap: wrap around

    • clip: clip to the range

    In clip mode, a negative index which would normally wrap will clip to 0 instead.

  • order ({C, F}) – Determines whether the multi-index should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order.

返回

Raveled_indices array. An array of indices into the flattened version of an array of dimensions dims.

引发
  • TypeError – If multi_index or dims can not be converted to tensor or dims is not a sequence of integer values.

  • ValueError – If the length of multi_index and that of dims are not equal.

Supported Platforms:

GPU

实际案例

>>> import mindspore.numpy as np
>>> arr = np.array([[3, 6, 6], [4, 5, 1]])
>>> output = np.ravel_multi_index(arr, (7, 6))
>>> print(output)
[22. 41. 37.]
>>> output = np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9))
>>> print(output)
1621.0
tinyms.reciprocal(x, dtype=None)[源代码]

Returns the reciprocal of the argument, element-wise.

Calculates 1/x.

注解

Numpy arguments casting, order, subok, signature, and extobj are not supported. When where is provided, out must have a tensor value. out is not supported for storing the result, however it can be used in combination with where to set the value at indices for which where is set to False.

参数
  • x (Tensor) – Input array. For integer arguments with absolute value larger than 1 the result is always zero because of the way Python handles integer division. For integer zero the result is an overflow.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, this is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(1, 7).reshape(2, 3).astype('float32')
>>> output = np.reciprocal(x)
>>> print(output)
[[1.         0.5        0.33333334]
[0.25       0.2        0.16666667]]
tinyms.remainder(x1, x2, dtype=None)[源代码]

Returns element-wise remainder of division.

Computes the remainder complementary to the floor_divide function. It is equivalent to the Python modulus operator x1 % x2 and has the same sign as the divisor x2. The MATLAB function equivalent to np.remainder is mod.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – input array.

  • x2 (Tensor) – input array.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the element-wise remainder of the quotient floor_divide(x1, x2). This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.remainder(np.array([4, 7]), np.array([2, 3]))
>>> print(output)
[0 1]
>>> output = np.remainder(np.arange(7), np.array(5))
>>> print(output)
[0 1 2 3 4 0 1]
tinyms.repeat(a, repeats, axis=None)[源代码]

Repeats elements of an array.

参数
  • a (Tensor) – Input array.

  • repeats (int or sequence of ints) – The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.

  • axis (int, optional) – The axis along which to repeat values. By default, use the flattened input array, and return a flat output array.

返回

Tensor, output array which has the same shape as a, except along the given axis.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.repeat(np.array(3), 4)
>>> print(output)
[3 3 3 3]
>>> x = np.array([[1,2],[3,4]])
>>> output = np.repeat(x, 2)
>>> print(output)
[1 1 2 2 3 3 4 4]
>>> output = np.repeat(x, 3, axis=1)
>>> print(output)
[[1 1 1 2 2 2]
[3 3 3 4 4 4]]
>>> output = np.repeat(x, [1, 2], axis=0)
>>> print(output)
[[1 2]
[3 4]
[3 4]]
tinyms.reshape(x, new_shape)[源代码]

Reshapes a tensor without changing its data.

参数
  • x (Tensor) – A tensor to be reshaped.

  • new_shape (Union[int, list(int), tuple(int)]) – The new shape should be compatible with the original shape. If the tuple has only one element, the result will be a 1-D tensor of that length. One shape dimension can be \(-1\). In this case, the value is inferred from the length of the tensor and remaining dimensions.

返回

Reshaped Tensor. Has the same data type as the original tensor x.

引发
  • TypeError – If new_shape is not integer, list or tuple, or x is not tensor.

  • ValueError – If new_shape is not compatible with the original shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.asarray([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]])
>>> output = np.reshape(x, (3, 2))
>>> print(output)
[[-0.1  0.3]
 [ 3.6  0.4]
 [ 0.5 -3.2]]
>>> output = np.reshape(x, (3, -1))
>>> print(output)
[[-0.1  0.3]
 [ 3.6  0.4]
 [ 0.5 -3.2]]
>>> output = np.reshape(x, (6, ))
>>> print(output)
[-0.1  0.3  3.6  0.4  0.5 -3.2]
tinyms.result_type(*arrays_and_dtypes)[源代码]

Returns the type that results from applying the type promotion rules to the arguments.

注解

The promotion rule is slightly different from original Numpy, but more like jax, due to the preference on 32-bit over 64-bit data types. Complex dtypes are not supported.

参数

*arrays_and_dtypes (Union[int, float, bool, list, tuple, Tensor, mindspore.dtype, str]) – The operands of some operation whose result type is needed.

返回

mindspore.dtype, the result type.

引发

TypeError – if the input is not a valid data type.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.result_type('i2', np.float32, True))
Float32
tinyms.rint(x, dtype=None)[源代码]

Rounds elements of the array to the nearest integer.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Ascend does not support dtype float64 currently.

参数
  • x (Union[float, list, tuple, Tensor]) – Input tensor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Output tensor is same shape and type as x. This is a scalar if x is a scalar.

引发

TypeError – If x can not be converted to tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([-1.7, -1.5, 0.2, 1.5, 1.7, 2.0])
>>> print(np.rint(x))
[-2. -2. 0. 2. 2. 2.]
tinyms.roll(a, shift, axis=None)[源代码]

Rolls a tensor along given axes.

Elements that rolls beyond the last position are re-introduced at the first.

参数
  • a (Tensor) – Input tensor.

  • shift (Union[int, tuple(int) – The number of places by which elements are shifted. If a tuple, then axis must be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If shift is an int while axis is a tuple of integers, then the same value is used for all given axes.

  • axis (Union[int, tuple(int)], optional) – Axis or axes along which elements are shifted. By default, the array is flattened before shifting, after which the original shape is restored.

返回

Tensor, with the same shape as a.

Supported Platforms:

Ascend GPU CPU

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If axis exceeds a.ndim, or shift and axis cannot broadcast.

实际案例

>>> import mindspore.numpy as np
>>> a = np.reshape(np.arange(12), (3, 4))
>>> print(np.roll(a, [2,-3], [0,-1]))
    [[ 7  4  5  6]
     [11  8  9 10]
     [ 3  0  1  2]]
tinyms.rollaxis(x, axis, start=0)[源代码]

Rolls the specified axis backwards, until it lies in the given position. The positions of the other axes do not change relative to one another.

参数
  • x (Tensor) – A Tensor to be transposed.

  • axis (int) – The axis to be rolled.

  • start (int) –

    Default: 0. If \(start <= axis\), the axis is rolled back until it lies in this position (start). If \(start > axis\): the axis is rolled until it lies before this position (start). If \(start < 0\), the start will be normalized as a non-negative number (more details can be seen in the source code.)

返回

Transposed Tensor. Has the same data type as the original tensor x.

Supported Platforms:

Ascend GPU CPU

引发
  • TypeError – If axis or start is not integer, or x is not tensor.

  • ValueError – If axis is not in the range of \([-ndim, ndim-1]\) or start is not in the range of \([-ndim, ndim]\).

实际案例

>>> import mindspore.numpy as np
>>> x = np.ones((2,3,4))
>>> output = np.rollaxis(x, 0, 2)
>>> print(output.shape)
(3, 2, 4)
tinyms.rot90(a, k=1, axes=(0, 1))[源代码]

Rotates a tensor by 90 degrees in the plane specified by axes. Rotation direction is from the first towards the second axis.

参数
  • a (Tensor) – Input tensor of two or more dimensions.

  • k (int) – Number of times the tensor is rotated by 90 degrees. Default: 1.

  • axes (Union[tuple(int), list(int)]) – The tensor is rotated in the plane defined by the axes. Default: (0, 1). Axes must be different and with the shape of (2,).

返回

Tensor.

引发
  • TypeError – if input a is not a Tensor or the argument k is not integer or the argument axes is not tuple of integers or list of ints.

  • ValueError – if any axis is out of range or the length of axes is not 2.

Supported Platforms:

GPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(24).reshape((2, 3, 4))
>>> output = np.rot90(a)
>>> print(output)
[[[ 8  9 10 11]
  [20 21 22 23]]
 [[ 4  5  6  7]
  [16 17 18 19]]
 [[ 0  1  2  3]
  [12 13 14 15]]]
>>> output = np.rot90(a, 3, (1, 2))
>>> print(output)
[[[ 8  4  0]
  [ 9  5  1]
  [10  6  2]
  [11  7  3]]
 [[20 16 12]
  [21 17 13]
  [22 18 14]
  [23 19 15]]]
tinyms.searchsorted(a, v, side='left', sorter=None)[源代码]

Finds indices where elements should be inserted to maintain order. Finds the indices into a sorted array a such that, if the corresponding elements in v were inserted before the indices, the order of a would be preserved.

参数
  • a (Union[list, tuple, Tensor]) – 1-D input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it.

  • v (Union[int, float, bool, list, tuple, Tensor]) – Values to insert into a.

  • side ('left', 'right', optional) – If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of a).

  • sorter (Union[int, float, bool, list, tuple, Tensor]) – 1-D optional array of integer indices that sort array a into ascending order. They are typically the result of argsort.

返回

Tensor, array of insertion points with the same shape as v.

引发

ValueError – if argument for side or sorter is invalid.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import numpy as np
>>> print(np.searchsorted([1,2,3,4,5], 3))
2
>>> print(np.searchsorted([1,2,3,4,5], 3, side='right'))
3
>>> print(np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]))
[0 5 1 2]
tinyms.select(condlist, choicelist, default=0)[源代码]

Returns an array drawn from elements in choicelist, depending on conditions.

参数
  • condlist (Union[int, float, bool, list, tuple, Tensor]) – The list of conditions which determine from which array in choicelist the output elements are taken. When multiple conditions are satisfied, the first one encountered in condlist is used.

  • choicelist (Union[int, float, bool, list, tuple, Tensor]) – The list of arrays from which the output elements are taken. It has to be of the same length as condlist.

  • default (scalar, optional) – The element inserted in output when all conditions evaluate to False. Defaults to 0.

返回

Tensor, the output at position m is the m-th element of the array in choicelist where the m-th element of the corresponding array in condlist is True.

引发

ValueError – if len(condlist) != len(choicelist).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> condlist = [[True, True, True, False, False], [False, False, True, False, True]]
>>> choicelist = [[0, 1, 2, 3, 4], [0, 1, 4, 9, 16]]
>>> output = np.select(condlist, choicelist)
>>> print(output)
[ 0  1  2  0 16]
tinyms.sign(x, dtype=None)[源代码]

Returns an element-wise indication of the sign of a number.

The sign function returns -1 if x < 0, 0 if x == 0, 1 if x > 0. nan is returned for nan inputs.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Complex inputs are not supported now. On Ascend, integer inputs are not supported.

参数
  • x (Union[int, float, list, tuple, Tensor]) – Input values.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

The sign of x. This is a tensor or a scalar when x is a scalar.

引发

TypeError – if dtype of the input is not in the given types or the input can not be converted to tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.sign(np.array([-1., 0., 1., 1.2]))
>>> print(output)
[-1.  0.  1.  1.]
tinyms.signbit(x, dtype=None)[源代码]

Returns element-wise True where signbit is set (less than zero).

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Union[int, float, bool, list, tuple, Tensor]) – The input value(s).

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor.

引发

TypeError – If input is not array_like or dtype is not None or bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([1, -2.3, 2.1]).astype('float32')
>>> output = np.signbit(x)
>>> print(output)
[False  True False]
tinyms.sin(x, dtype=None)[源代码]

Trigonometric sine, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([-5, -1, 0, 2, 4, 100]).astype('float32')
>>> output = np.sin(x)
>>> print(output)
[ 0.9589243  -0.84147096  0.   0.9092974  -0.7568025  -0.50636566]
tinyms.sinh(x, dtype=None)[源代码]

Hyperbolic sine, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(5).astype('float32')
>>> print(np.sinh(x))
[ 0.         1.1752012  3.6268604 10.017875  27.289917 ]
tinyms.size(a, axis=None)[源代码]

Returns the number of elements along a given axis.

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input data.

  • axis (int) – Axis along which the elements are counted. Default: None. If None, give the total number of elements.

返回

Number of elements along the specified axis.

Supported Platforms:

Ascend GPU CPU

引发
  • TypeError – If input is not array_like or axis is not int.

  • ValueError – If any axis is out of range or duplicate axes exist.

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(10).reshape(2, 5).astype('float32')
>>> print(np.size(x))
10
>>> print(np.size(x, axis=1))
5
tinyms.sometrue(a, axis=None, keepdims=False)[源代码]

Tests whether any array element along a given axis evaluates to True.

Returns single boolean unless axis is not None

参数
  • a (Union[int, float, bool, list, tuple, Tensor]) – Input tensor or object that can be converted to an array.

  • axis (Union[None, int, tuple(int)]) – Axis or axes along which a logical OR reduction is performed. Default: None. If None, perform a logical OR over all the dimensions of the input array. If negative, it counts from the last to the first axis. If tuple of integers, a reduction is performed on multiple axes, instead of a single axis or all the axes as before.

  • keepdims (bool) – Default: False. If True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then keepdims will not be passed through to the any method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised.

返回

Returns single boolean unless axis is not None

引发
  • TypeError – If input is not array_like or axis is not int or tuple of integers or keepdims is not integer or initial is not scalar.

  • ValueError – If any axis is out of range or duplicate axes exist.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([1, -2.3, 2.1]).astype('float32')
>>> output = np.sometrue(x)
>>> print(output)
True
tinyms.split(x, indices_or_sections, axis=0)[源代码]

Splits a tensor into multiple sub-tensors along the given axis.

参数
  • x (Tensor) – A Tensor to be divided.

  • indices_or_sections (Union[int, tuple(int), list(int)]) – If integer, \(N\), the tensor will be divided into \(N\) equal tensors along axis. If tuple(int), list(int) or of sorted integers, the entries indicate where along axis the array is split. For example, \([2, 3]\) would, for \(axis=0\), result in three sub-tensors \(x[:2]\), \(x[2:3]\). If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.

  • axis (int) – The axis along which to split. Default: 0.

返回

A list of sub-tensors.

引发
  • TypeError – If argument indices_or_sections is not integer, tuple(int) or list(int) or argument axis is not integer.

  • ValueError – If argument axis is out of range of \([-x.ndim, x.ndim)\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.arange(9).astype("float32")
>>> output = np.split(input_x, 3)
>>> print(output)
(Tensor(shape=[3], dtype=Float32,
  value= [ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00]),
 Tensor(shape=[3], dtype=Float32,
  value= [ 3.00000000e+00,  4.00000000e+00,  5.00000000e+00]),
 Tensor(shape=[3], dtype=Float32,
  value= [ 6.00000000e+00,  7.00000000e+00,  8.00000000e+00]))
tinyms.sqrt(x, dtype=None)[源代码]

Returns the non-negative square-root of an array, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16 and np.float32.

参数
  • x (Tensor) – The values whose square-roots are required.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, an array of the same shape as x, containing the positive square-root of each element in x. For negative elements, nan is returned. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(6).reshape(2, 3).astype('float32')
>>> x_squared = np.square(x)
>>> output = np.sqrt(x_squared)
>>> print(output)
[[ 0. 1. 2.]
[ 3. 4. 5.]]
tinyms.square(x, dtype=None)[源代码]

Returns the element-wise square of the input.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16 and np.float32.

参数
  • x (Tensor) – Input data.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, element-wise x*x, of the same shape and dtype as x. This is a scalar if x is a scalar..

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.square(np.arange(6).reshape(2, 3).astype('float32'))
>>> print(x)
[[ 0.  1.  4.]
[ 9. 16. 25.]]
tinyms.squeeze(a, axis=None)[源代码]

Removes single-dimensional entries from the shape of a tensor.

参数
返回

Tensor, with all or a subset of the dimensions of length \(1\) removed.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If specified axis has shape entry \(> 1\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.ones((1,2,2,1))
>>> x = np.squeeze(x)
>>> print(x.shape)
(2, 2)
tinyms.stack(arrays, axis=0)[源代码]

Joins a sequence of arrays along a new axis.

The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

注解

Numpy argument out is not supported.

参数
  • arrays (sequence of Tensor) – Each array must have the same shape.

  • axis (int, optional) – The axis in the result array along which the input arrays are stacked. Default: 0.

返回

Tensor, The stacked array has one more dimension than the input arrays.

引发

ValueError – if input is not Tensor, tuple, or list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> arrays = [np.ones((3, 4)) for _ in range(10)]
>>> output = np.stack(arrays, axis=0)
>>> print(output.shape)
(10, 3, 4)
>>> output = np.stack(arrays, axis=1)
>>> print(output.shape)
(3, 10, 4)
>>> output = np.stack(arrays, axis=2)
>>> print(output.shape)
(3, 4, 10)
tinyms.std(x, axis=None, ddof=0, keepdims=False)[源代码]

Computes the standard deviation along the specified axis. The standard deviation is the square root of the average of the squared deviations from the mean, i.e., \(std = sqrt(mean(abs(x - x.mean())**2))\).

Returns the standard deviation, which is computed for the flattened array by default, otherwise over the specified axis.

注解

Numpy arguments dtype, out and where are not supported.

参数
  • x (Tensor) – A Tensor to be calculated.

  • axis (Union[None, int, tuple(int)]) –

    Axis or axes along which the standard deviation is computed. Default: None.

    If None, compute the standard deviation of the flattened array.

  • ddof (int) – Means Delta Degrees of Freedom. The divisor used in calculations is \(N - ddof\), where \(N\) represents the number of elements. Default: 0.

  • keepdims – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input tensor. If the default value is passed, then keepdims will not be passed through to the std method of sub-classes of tensor, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised. Default: False.

返回

Standard deviation tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.array([1., 2., 3., 4.])
>>> output = np.std(input_x)
>>> print(output)
1.118034
tinyms.subtract(x1, x2, dtype=None)[源代码]

Subtracts arguments, element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – the input to be subtracted from.

  • x2 (Tensor) – the input to be subtracted by.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the difference of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.full((3, 2), [1, 2])
>>> x2 = np.full((3, 2), [3, 4])
>>> output = np.subtract(x1, x2)
>>> print(output)
[[-2 -2]
[-2 -2]
[-2 -2]]
tinyms.sum(a, axis=None, dtype=None, keepdims=False, initial=None)

Returns sum of array elements over a given axis.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Union[int, float, bool, list, tuple, Tensor]) – Elements to sum.

  • axis (Union[None, int, tuple(int)]) – Axis or axes along which a sum is performed. Default: None. If None, sum all of the elements of the input array. If axis is negative it counts from the last to the first axis. If axis is a tuple of integers, a sum is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised. Default: False.

  • initial (scalar) – Starting value for the sum, if None, which refers to the first element of the reduction. Default: None.

返回

Tensor. An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.

引发
  • TypeError – If input is not array_like or axis is not int or tuple of integers or keepdims is not integer or initial is not scalar.

  • ValueError – If any axis is out of range or duplicate axes exist.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.sum([0.5, 1.5]))
2.0
>>> x = np.arange(10).reshape(2, 5).astype('float32')
>>> print(np.sum(x, axis=1))
[10. 35.]
tinyms.swapaxes(x, axis1, axis2)[源代码]

Interchanges two axes of a tensor.

参数
  • x (Tensor) – A tensor to be transposed.

  • axis1 (int) – First axis.

  • axis2 (int) – Second axis.

返回

Transposed tensor, has the same data type as the original tensor x.

引发
  • TypeError – If axis1 or axis2 is not integer, or x is not tensor.

  • ValueError – If axis1 or axis2 is not in the range of \([-ndim, ndim-1]\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.ones((2,3,4))
>>> output = np.swapaxes(x, 0, 2)
>>> print(output.shape)
(4,3,2)
tinyms.take(a, indices, axis=None, mode='clip')[源代码]

Takes elements from an array along an axis.

When axis is not None, this function does the same thing as “fancy” indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis. A call such as np.take(arr, indices, axis=3) is equivalent to arr[:,:,:,indices,...].

注解

Numpy argument out is not supported. mode = 'raise' is not supported, and the default mode is ‘clip’ instead.

参数
  • a (Tensor) – Source array with shape (Ni…, M, Nk…).

  • indices (Tensor) – The indices with shape (Nj…) of the values to extract.

  • axis (int, optional) – The axis over which to select values. By default, the flattened input array is used.

  • mode (‘raise’, ‘wrap’, ‘clip’, optional) –

    Specifies how out-of-bounds indices will behave.

    ‘raise’ – raise an error;

    ‘wrap’ – wrap around;

    ‘clip’ – clip to the range. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

返回

Tensor, the indexed result.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([4, 3, 5, 7, 6, 8])
>>> indices = np.array([0, 1, 4])
>>> output = np.take(a, indices)
>>> print(output)
[4 3 6]
>>> indices = np.array([[0, 1], [2, 3]])
>>> output = np.take(a, indices)
>>> print(output)
[[4 3]
[5 7]]
tinyms.take_along_axis(arr, indices, axis)[源代码]

Takes values from the input array by matching 1d index and data slices.

This iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to look up values in the latter. These slices can be different lengths.

参数
  • arr (Tensor) – Source array with shape (Ni…, M, Nk…).

  • indices (Tensor) – Indices with shape (Ni…, J, Nk…) to take along each 1d slice of arr. This must match the dimension of arr, but dimensions Ni and Nj only need to broadcast against arr.

  • axis (int) – The axis to take 1d slices along. If axis is None, the input array is treated as if it had first been flattened to 1d.

返回

Tensor, the indexed result, with shape (Ni…, J, Nk…).

引发
  • ValueError – if input array and indices have different number of dimensions.

  • TypeError – if the input is not a Tensor.

Supported Platforms:

Ascend GPU CPU

示例

>>> import mindspore.numpy as np
>>> x = np.arange(12).reshape(3, 4)
>>> indices = np.arange(3).reshape(1, 3)
>>> output = np.take_along_axis(x, indices, 1)
>>> print(output)
[[ 0  1  2]
[ 4  5  6]
[ 8  9 10]]
tinyms.tan(x, dtype=None)[源代码]

Computes tangent element-wise.

Equivalent to \(np.sin(x)/np.cos(x)\) element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

引发

TypeError – If the input is not a tensor or is tensor.dtype is mindspore.float64.

Supported Platforms:

Ascend CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.array([-5, -1, 0, 2, 4, 100]).astype('float32')
>>> print(np.tan(x))
[ 3.380515   -1.5574077   0.         -2.1850398   1.1578213  -0.58721393]
tinyms.tanh(x, dtype=None)[源代码]

Computes hyperbolic tangent element-wise.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – Input tensor.

  • dtype (mindspore.dtype, optional) – Default: None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.arange(5).astype('float32')
>>> print(np.tanh(x))
[0.        0.7615942 0.9640276 0.9950548 0.9993293]
tinyms.tensordot(a, b, axes=2)[源代码]

Computes tensor dot product along specified axes.

Given two tensors, a and b, and an array_like object containing two array_like objects, (a_axes, b_axes), sum the products of a’s and b’s elements (components) over the axes specified by a_axes and b_axes. The third argument can be a single non-negative integer_like scalar, N; if it is such, then the last N dimensions of a and the first N dimensions of b are summed over. Three common use cases are:

  • axes = 0 : tensor product

  • axes = 1 : tensor dot product

  • axes = 2 : (default) tensor double contraction

When axes is integer_like, the sequence for evaluation will be: first the -Nth axis in a and 0th axis in b, and the -1th axis in a and Nth axis in b last. When there is more than one axis to sum over - and they are not the last (first) axes of a (b) - the argument axes should consist of two sequences of the same length, with the first axis to sum over given first in both sequences, the second axis second, and so forth. The shape of the result consists of the non-contracted axes of the first tensor, followed by the non-contracted axes of the second.

注解

On CPU, the supported dypes are np.float16 and np.float32. On GPU, the supported dypes are np.float16 and np.float32.

参数
  • a (Tensor) – Tensor to “dot”.

  • b (Tensor) – Tensor to “dot”.

  • axes (int or sequence of ints) –

    integer_like: If an int N, sum over the last N axes of a and the first N axes of b in order. The sizes of the corresponding axes must match.

    sequence of ints: Or, a list of axes to be summed over, first sequence applying to a, second to b. Both elements array_like must be of the same length.

返回

Tensor, or list of tensors, the tensor dot product of the input.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((3, 4, 5))
>>> b = np.ones((4, 3, 2))
>>> output = np.tensordot(a, b, axes=([1,0],[0,1]))
>>> print(output.shape)
(5, 2)
tinyms.tile(a, reps)[源代码]

Constructs an array by repeating a the number of times given by reps.

If reps has length d, the result will have dimension of max(d, a.ndim). If a.ndim < d, a is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired behavior, promote a to d-dimensions manually before calling this function. If a.ndim > d, reps is promoted to a.ndim by pre-pending 1’s to it. Thus for an a of shape (2, 3, 4, 5), a reps of (2, 2) is treated as (1, 1, 2, 2).

参数
  • a (Tensor) – The input array.

  • reps (int or sequence of ints) – The number of repetitions of a along each axis.

返回

Tensor, the tiled output array.

引发

TypeError – if the input is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.array([0, 1, 2])
>>> output = np.tile(a, 2)
>>> print(output)
[0 1 2 0 1 2]
>>> output = np.tile(a, (2, 2))
>>> print(output)
[[0 1 2 0 1 2]
[0 1 2 0 1 2]]
>>> output = np.tile(a, (2, 1, 2))
>>> print(output)
[[[0 1 2 0 1 2]]
[[0 1 2 0 1 2]]]
tinyms.trace(a, offset=0, axis1=0, axis2=1, dtype=None)[源代码]

Returns the sum along diagonals of the array.

If a is 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i. If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. The shape of the resulting array is the same as that of a with axis1 and axis2 removed.

注解

On GPU, the supported dtypes are np.float16, and np.float32. On CPU, the supported dtypes are np.float16, np.float32, and np.float64.

参数
  • a (Tensor) – Array from which the diagonals are taken.

  • offset (int, optional) – Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal.

  • axis1 (int, optional) – Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0).

  • axis2 (int, optional) – Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor, sum_along_diagonals. If a is 2-D, the sum along the diagonal is returned. If a has larger dimensions, then an array of sums along diagonals is returned.

引发

ValueError – if the input tensor has less than two dimensions.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.trace(np.eye(3))
>>> print(output)
3.0
>>> a = np.arange(8).reshape((2,2,2))
>>> output = np.trace(a)
>>> print(output)
[6 8]
>>> a = np.arange(24).reshape((2,2,2,3))
>>> output = np.trace(a).shape
>>> print(output)
(2, 3)
tinyms.transpose(a, axes=None)[源代码]

Reverses or permutes the axes of a tensor; returns the modified tensor.

参数
  • a (Tensor) – a tensor to be transposed

  • axes (Union[None, tuple, list]) – the axes order, if axes is None, transpose the entire tensor. Default is None.

返回

Tensor, the transposed tensor array.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If the number of axes is not euqal to a.ndim.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x = np.ones((1,2,3))
>>> x = np.transpose(x)
>>> print(x.shape)
(3, 2, 1)
tinyms.trapz(y, x=None, dx=1.0, axis=-1)[源代码]

Integrates along the given axis using the composite trapezoidal rule.

Integrates y (x) along given axis.

参数
  • y (Tensor) – Input array to integrate.

  • x (Union[int, float, bool, list, tuple, Tensor], optional) – The sample points corresponding to the y values. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.

  • dx (scalar, optional) – The spacing between sample points when x is None. The default is 1.0.

  • axis (int, optional) – The axis along which to integrate. Defaults to -1.

返回

Tensor of float, definite integral as approximated by trapezoidal rule.

引发

ValueError – If axis is out of range of [-y.ndim, y.ndim).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.arange(6).reshape(2, 3)
>>> output = np.trapz(a,  x=[-2, 1, 2], axis=1)
>>> print(output)
[ 3. 15.]
>>> output = np.trapz(a,  dx=3, axis=0)
>>> print(output)
[ 4.5  7.5 10.5]
tinyms.tri(N, M=None, k=0, dtype=mindspore.float32)[源代码]

Returns a tensor with ones at and below the given diagonal and zeros elsewhere.

参数
  • N (int) – Number of rows in the array.

  • M (int, optional) – Number of columns in the array. By default, M is taken equal to N.

  • k (int, optional) – The sub-diagonal at and below which the array is filled. \(k = 0\) is the main diagonal, while \(k < 0\) is below it, and \(k > 0\) is above. The default is 0.

  • dtype (mindspore.dtype, optional) – Data type of the returned array. The default is mstype.float32.

返回

Tensor with shape (N, M), with its lower triangle filled with ones and zeros elsewhere; in other words \(T[i,j] = 1\) for \(j <= i + k\), 0 otherwise.

引发

TypeError – If input arguments have types not specified above.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.tri(3, 3, 1)
>>> print(output)
[[1. 1. 0.]
[1. 1. 1.]
[1. 1. 1.]]
tinyms.tril(m, k=0)[源代码]

Returns a lower triangle of a tensor.

Returns a copy of a tensor with elements above the k-th diagonal zeroed.

参数
  • m (Union[Tensor, list, tuple]) – The shape and data-type of m define these same attributes of the returned tensor.

  • k (int, optional) – Diagonal above which to zero elements. \(k = 0\) (the default) is the main diagonal, \(k < 0\) is below it and \(k > 0\) is above.

返回

Lower triangle of m, of same shape and data-type as m.

Supported Platforms:

Ascend GPU CPU

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If input m’s rank \(< 1\).

实际案例

>>> import mindspore.numpy as np
>>> output = np.tril(np.ones((3, 3)))
>>> print(output)
[[1. 0. 0.]
[1. 1. 0.]
[1. 1. 1.]]
tinyms.tril_indices(n, k=0, m=None)[源代码]

Returns the indices for the lower-triangle of an (n, m) array.

参数
  • n (int) – The size of the arrays for which the returned indices will be valid.

  • k (int, optional) – Diagonal offset, default is 0.

  • m (int, optional) – The column dimension of the arrays for which the returned arrays will be valid. By default m is taken equal to n.

返回

The indices for the triangle. The returned tuple contains two tensors, each with the indices along one dimension of the tensor.

引发

TypeError – if n, k, m are not numbers.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.tril_indices(3))
(Tensor(shape=[6], dtype=Int32, value= [0, 1, 1, 2, 2, 2]),
Tensor(shape=[6], dtype=Int32, value= [0, 0, 1, 0, 1, 2]))
tinyms.tril_indices_from(arr, k=0)[源代码]

Returns the indices for the lower-triangle of arr.

参数
  • arr (Union[Tensor, list, tuple]) – 2-dimensional array.

  • k (int, optional) – Diagonal offset, default is 0.

返回

triu_indices_from, tuple of 2 tensor, shape(N) Indices for the upper-triangle of arr.

引发
  • TypeError – if arr cannot be converted to tensor, or k is not a number.

  • ValueError – if arr cannot be converted to a 2-dimensional tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> tensor = np.ones((3,3))
>>> print(np.tril_indices_from(tensor))
(Tensor(shape=[6], dtype=Int32, value= [0, 1, 1, 2, 2, 2]),
 Tensor(shape=[6], dtype=Int32, value= [0, 0, 1, 0, 1, 2]))
tinyms.triu(m, k=0)[源代码]

Returns an upper triangle of a tensor.

Returns a copy of a tensor with elements below the k-th diagonal zeroed.

参数
  • m (Union[Tensor, list, tuple]) – The shape and data-type of m define these same attributes of the returned tensor.

  • k (int, optional) – Diagonal below which to zero elements. \(k = 0\) (the default) is the main diagonal, \(k < 0\) is below it and \(k > 0\) is above.

返回

Upper triangle of m, of same shape and data-type as m.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If input m’s rank < 1.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.triu(np.ones((3, 3)))
>>> print(output)
[[1. 1. 1.]
[0. 1. 1.]
[0. 0. 1.]]
tinyms.triu_indices(n, k=0, m=None)[源代码]

Returns the indices for the upper-triangle of an (n, m) array.

参数
  • n (int) – The size of the arrays for which the returned indices will be valid.

  • k (int, optional) – Diagonal offset, default is 0.

  • m (int, optional) – The column dimension of the arrays for which the returned arrays will be valid. By default m is taken equal to n.

返回

The indices for the triangle. The returned tuple contains two tensors, each with the indices along one dimension of the tensor.

引发

TypeError – if n, k, m are not numbers.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.triu_indices(3))
(Tensor(shape=[6], dtype=Int32, value= [0, 0, 0, 1, 1, 2]),
 Tensor(shape=[6], dtype=Int32, value= [0, 1, 2, 1, 2, 2]))
tinyms.triu_indices_from(arr, k=0)[源代码]

Returns the indices for the upper-triangle of arr.

参数
  • arr (Union[Tensor, list, tuple]) – 2-dimensional array.

  • k (int, optional) – Diagonal offset, default is 0.

返回

triu_indices_from, tuple of 2 tensor, shape(N) Indices for the upper-triangle of arr.

引发
  • TypeError – if arr cannot be converted to tensor, or k is not a number.

  • ValueError – if arr cannot be converted to a 2-dimensional tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> tensor = np.ones((3,3))
>>> print(np.triu_indices_from(tensor))
(Tensor(shape=[6], dtype=Int32, value= [0, 0, 0, 1, 1, 2]),
Tensor(shape=[6], dtype=Int32, value= [0, 1, 2, 1, 2, 2]))
tinyms.true_divide(x1, x2, dtype=None)[源代码]

Returns a true division of the inputs, element-wise.

Instead of the Python traditional ‘floor division’, this returns a true division.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x1 (Tensor) – the divident.

  • x2 (Tensor) – the divisor.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, this is a scalar if both x1 and x2 are scalars.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.full((3, 2), [1, 2])
>>> x2 = np.full((3, 2), [3, 4])
>>> output = np.true_divide(x1, x2)
>>> print(output)
[[0.33333334 0.5       ]
[0.33333334 0.5       ]
[0.33333334 0.5       ]]
tinyms.trunc(x, dtype=None)[源代码]

Returns the truncated value of the input, element-wise.

The truncated value of the scalar x is the nearest integer i which is closer to zero than x is. In short, the fractional part of the signed number x is discarded.

注解

Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.

参数
  • x (Tensor) – input data.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the truncated value of each element in x. This is a scalar if x is a scalar.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> output = np.trunc(np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]))
>>> print(output)
[-1. -1. -0.  0.  1.  1.  2.]
tinyms.unique(x, return_inverse=False)[源代码]

Finds the unique elements of a tensor. The input tensor will be flattened first when it has more than one dimension.

注解

Numpy arguments axis, return_index and return_counts are not supported. On CPU, this operator must be executed in graph mode.

参数
  • x (Tensor) – The input tensor to be processed.

  • return_inverse (bool) – If True, also return the indices of the unique tensor. Default: False.

返回

Tensor or tuple of Tensors. - If return_inverse is False, just return the unique tensor. - If return_inverse is True, return tuple of tensors.

Supported Platforms:

Ascend GPU CPU

引发

TypeError – If x is not tensor.

实际案例

>>> import mindspore.numpy as np
>>> from mindspore import context
>>> context.set_context(mode=context.GRAPH_MODE)
>>> input_x = np.asarray([1, 2, 2, 2, 3, 4, 5]).astype('int32')
>>> output_x = np.unique(input_x)
>>> print(output_x)
[1 2 3 4 5]
>>> output_x = np.unique(input_x, return_inverse=True)
>>> print(output_x)
(Tensor(shape=[5], dtype=Int32, value= [ 1, 2, 3, 4, 5]), Tensor(shape=[7], dtype=Int32,
    value= [0, 1, 1, 1, 2, 3, 4]))
tinyms.unravel_index(indices, shape, order='C')[源代码]

Converts a flat index or array of flat indices into a tuple of coordinate arrays.

注解

Out-of-bound indices are clipped by the boundaries of shape instead of raising an error.

参数
  • indices (Union[int, float, bool, list, tuple, Tensor]) – An integer array whose elements are indices into the flattened version of an array of dimensions shape.

  • shape (tuple of integers) – The shape of the array to use for unraveling indices.

  • order (Union['C', 'F'], optional) – Determines whether the indices should be viewed as indexing in row-major (C-style) or column-major (Fortran-style) order.

返回

Tensor, each array in the tuple has the same shape as the indices array.

Supported Platforms:

Ascend GPU CPU

引发

ValueError – if order is not ‘C’ or ‘F’.

实际案例

>>> import mindspore.numpy as np
>>> print(np.unravel_index([22, 41, 37], (7,6)))
(Tensor(shape=[3], dtype=Int32, value= [3, 6, 6]),
Tensor(shape=[3], dtype=Int32, value= [4, 5, 1]))
>>> print(np.unravel_index([31, 41, 13], (7,6), order='F'))
(Tensor(shape=[3], dtype=Int32, value= [3, 6, 6]),
Tensor(shape=[3], dtype=Int32, value= [4, 5, 1]))
tinyms.unwrap(p, discont=3.141592653589793, axis=-1)[源代码]

Unwraps by changing deltas between values to 2*pi complement. Unwraps radian phase p by changing absolute jumps greater than discont to their 2*pi complement along the given axis.

注解

For absolute jumps that are within a very close range to pi, unwrapping may be done differently than numpy due to differences in round-off.

参数
  • p (Union[int, float, bool, list, tuple, Tensor) – Input array.

  • discont (float, optional) – Maximum discontinuity between values, default is pi.

  • axis (int, optional) – Axis along which unwrap will operate, default is -1.

返回

Tensor.

引发

ValueError – if the axis is out of range.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> phase = np.add(np.linspace(0, np.pi, num=5), [0, 0, 0, np.pi, np.pi])
>>> print(phase)
[0.        0.7853982 1.5707964 5.4977875 6.2831855]
>>> print(np.unwrap(phase))
[ 0.0000000e+00  7.8539819e-01  1.5707964e+00 -7.8539848e-01 -4.7683716e-07]
tinyms.vander(x, N=None, increasing=False)[源代码]

Generates a Vandermonde matrix.

The columns of the output matrix are powers of the input vector. The order of the powers is determined by the increasing boolean argument. Specifically, when increasing is False, the i-th output column is the input vector raised element-wise to the power of \(N - i - 1\). Such a matrix with a geometric progression in each row is named for Alexandre-Theophile Vandermonde.

参数
  • x (Union[list, tuple, Tensor]) – 1-D input array.

  • N (int, optional) – Number of columns in the output. If N is not specified, a square array is returned (N = len(x)).

  • increasing (bool, optional) – Order of the powers of the columns. If True, the powers increase from left to right, if False (the default) they are reversed.

返回

Vandermonde matrix. If increasing is False, the first column is \(x^{(N-1)}\), the second \(x^{(N-2)}\) and so forth. If increasing is True, the columns are \(x^0, x^1, ..., x^{(N-1)}\).

引发
  • TypeError – If inputs have types not specified above.

  • ValueError – If x is not 1-D, or N < 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.vander([1., 2., 3., 4., 5.]))
[[  1.   1.   1.   1.   1.]
 [ 16.   8.   4.   2.   1.]
 [ 81.  27.   9.   3.   1.]
 [256.  64.  16.   4.   1.]
 [625. 125.  25.   5.   1.]]
tinyms.var(x, axis=None, ddof=0, keepdims=False)[源代码]

Computes the variance along the specified axis. The variance is the average of the squared deviations from the mean, i.e., \(var = mean(abs(x - x.mean())**2)\).

Returns the variance, which is computed for the flattened array by default, otherwise over the specified axis.

注解

Numpy arguments dtype, out and where are not supported.

参数
  • x (Tensor) – A Tensor to be calculated.

  • axis (Union[None, int, tuple(int)]) – Axis or axes along which the variance is computed. The default is to compute the variance of the flattened array. Default: None.

  • ddof (int) – Means Delta Degrees of Freedom. Default: 0. The divisor used in calculations is \(N - ddof\), where \(N\) represents the number of elements.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input tensor. If the default value is passed, then keepdims will not be passed through to the var method of sub-classes of tensor, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised. Default: False.

Supported Platforms:

Ascend GPU CPU

返回

Standard deviation tensor.

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.array([1., 2., 3., 4.])
>>> output = np.var(input_x)
>>> print(output)
1.25
tinyms.vsplit(x, indices_or_sections)[源代码]

Splits a tensor into multiple sub-tensors vertically (row-wise). It is equivalent to split with \(axis=0\) (default), the array is always split along the first axis regardless of the array dimension.

参数
  • x (Tensor) – A Tensor to be divided.

  • indices_or_sections (Union[int, tuple(int), list(int)]) – If integer, \(N\), the tensor will be divided into \(N\) equal tensors along axis. If tuple(int), list(int) or of sorted integers, the entries indicate where along axis the array is split. For example, \([2, 3]\) would, for \(axis=0\), result in three sub-tensors \(x[:2]\), \(x[2:3]\). If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.

返回

A list of sub-tensors.

引发

TypeError – If argument indices_or_sections is not integer.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> input_x = np.arange(9).reshape((3, 3)).astype('float32')
>>> output = np.vsplit(input_x, 3)
>>> print(output)
(Tensor(shape=[1, 3], dtype=Float32,
  value=[[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00]]),
 Tensor(shape=[1, 3], dtype=Float32,
  value=[[ 3.00000000e+00,  4.00000000e+00,  5.00000000e+00]]),
 Tensor(shape=[1, 3], dtype=Float32,
  value=[[ 6.00000000e+00,  7.00000000e+00,  8.00000000e+00]]))
tinyms.vstack(tup)[源代码]

Stacks tensors in sequence vertically. This is equivalent to concatenation along the first axis. 1-D tensors should firstly be reshaped to (1, N), and then be concatenated along the first axis.

参数

tup (Union[Tensor, tuple, list]) – A sequence of 1-D or 2-D tensors. The tensors must have the same shape along all but the first axis. 1-D tensors must have the same shape.

返回

Stacked Tensor, formed by stacking the given tensors.

Supported Platforms:

Ascend GPU CPU

引发

实际案例

>>> import mindspore.numpy as np
>>> x1 = np.array([1, 2, 3]).astype('int32')
>>> x2 = np.array([4, 5, 6]).astype('int32')
>>> output = np.vstack((x1, x2))
>>> print(output)
[[1 2 3]
 [4 5 6]]
tinyms.where(condition, x=None, y=None)[源代码]

Returns elements chosen from x or y depending on condition.

注解

As nonzero is not supported, both x and y must be provided Tensor

input.

参数
  • condition (Tensor) – where True, yield x, otherwise yield y.

  • x (Tensor) – Values from which to choose. Defaults to None.

  • y (Tensor) – Values from which to choose. x, y and condition need to be broadcastable to some shape. Defaults to None.

返回

Tensor or scalar, with elements from x where condition is True, and elements from y elsewhere.

引发

ValueError – if operands cannot be broadcast.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> condition = np.full((1, 1, 2), [False, True])
>>> x = np.full((1, 3, 2), 5)
>>> y = np.full((2, 1, 1), 7)
>>> output = np.where(condition, x, y)
>>> print(output)
[[[7 5]
[7 5]
[7 5]]
[[7 5]
[7 5]
[7 5]]]
tinyms.zeros(shape, dtype=mindspore.float32)[源代码]

Returns a new tensor of given shape and type, filled with zeros.

参数
  • shape (Union[int, tuple, list]) – the shape of the new tensor.

  • dtype (Union[mindspore.dtype, str], optional) – Designated tensor dtype. Default is mstype.float32.

返回

Tensor, with the designated shape and dtype, filled with zeros.

引发
  • TypeError – If input arguments have types not specified above.

  • ValueError – If shape entries have values \(< 0\).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> print(np.zeros((2,2)))
[[0. 0.]
[0. 0.]]
tinyms.zeros_like(a, dtype=None, shape=None)[源代码]

Returns an array of zeros with the same shape and type as a given array.

注解

Input array must have the same size across a dimension. If a is not a Tensor, dtype is float32 by default if not provided.

参数
  • a (Union[Tensor, list, tuple]) – The shape and data-type of a define these same attributes of the returned array.

  • dtype (mindspore.dtype, optional) – Overrides the data type of the result.

  • shape (int or sequence of ints, optional) – Overrides the shape of the result.

返回

Tensor, array of zeros with the same shape and type as a.

引发

ValueError – if a is not a Tensor, list or tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.numpy as np
>>> a = np.ones((4,1,2))
>>> output = np.zeros_like(a)
>>> print(output)
[[[0. 0.]]
[[0. 0.]]
[[0. 0.]]
[[0. 0.]]]

tinyms.context

The context of TinyMS, used to configure the current execution environment, includeing the execution mode, execution backend and other feature switches.

tinyms.context.set_context(**kwargs)[源代码]

Set context for running environment.

Context should be configured before running your program. If there is no configuration, it will be automatically set according to the device target by default.

注解

Attribute name is required for setting attributes. The mode is not recommended to be changed after net was initialized because the implementations of some operations are different in graph mode and pynative mode. Default: GRAPH_MODE.

Some configurations are device specific, see the below table for details:

Function Classification

Configuration Parameters

Hardware Platform Support

System Configuration

device_id

CPU/GPU/Ascend

device_target

CPU/GPU/Ascend

max_device_memory

GPU

variable_memory_max_size

Ascend

Debug Configuration

save_graphs

CPU/GPU/Ascend

save_graphs_path

CPU/GPU/Ascend

enable_dump

Ascend

save_dump_path

Ascend

enable_profiling

Ascend

profiling_options

Ascend

print_file_path

Ascend

env_config_path

CPU/GPU/Ascend

precompile_only

CPU/GPU/Ascend

reserve_class_name_in_scope

CPU/GPU/Ascend

pynative_synchronize

GPU/Ascend

Executive Control

mode

CPU/GPU/Ascend

enable_graph_kernel

Ascend/GPU

graph_kernel_flags

Ascend/GPU

enable_reduce_precision

Ascend

auto_tune_mode

Ascend

check_bprop

CPU/GPU/Ascend

max_call_depth

CPU/GPU/Ascend

enable_sparse

CPU/GPU/Ascend

grad_for_scalar

CPU/GPU/Ascend

save_compile_cache

CPU/GPU/Ascend

load_compile_cache

CPU/GPU/Ascend

参数
  • device_id (int) – ID of the target device, the value must be in [0, device_num_per_host-1], while device_num_per_host should be no more than 4096. Default: 0.

  • device_target (str) – The target device to run, support “Ascend”, “GPU”, and “CPU”. If device target is not set, the version of MindSpore package is used.

  • max_device_memory (str) – Set the maximum memory available for devices. Currently, it is only supported on GPU. The format is “xxGB”. Default: “1024GB”. The actual used memory size is the minimum of the available memory of the device and max_device_memory.

  • variable_memory_max_size (str) – Set the maximum size of the variable memory max size. Default: “30GB”. After this parameter is set, the maximum memory used by the framework is restricted to the configured value.

  • save_graphs (bool) – Whether to save graphs. Default: False. When the save_graphs attribute is set as True, attribute of save_graphs_path is used to set the intermediate compilation graph storage path. By default, the graphs are saved in the current directory.

  • save_graphs_path (str) – Path to save graphs. Default: “.”. If the specified directory does not exist, the system will automatically create the directory. During distributed training, graphs will be saved to the directory of save_graphs_path/rank_${rank_id}/. rank_id is the ID of the current device in the cluster.

  • enable_dump (bool) – This parameters is deprecated, and will be deleted in the next version.

  • save_dump_path (str) – This parameters is deprecated, and will be deleted in the next version.

  • enable_profiling (bool) – This parameters is deprecated, and will be deleted in the next version. Please use mindspore.profiler.Profiler api instead.

  • profiling_options (str) – This parameters is deprecated, and will be deleted in the next version. Please use mindspore.profiler.Profiler api instead.

  • print_file_path (str) – The path of saving print data. If this parameter is set, print data is saved to a file by default, and print_file_path is not set, the screen will be displayed. If the saved file already exists, the timestamp suffix will be added to the file. Saving data to a file solves the problem of data loss in screen printing when a large amount of data is generated. If it is not set, an error will be reported: prompt to set the upper absolute path.

  • env_config_path (str) –

    Config path for DFX. Through context.set_context(env_config_path=”./mindspore_config.json”)

    configure RDR:

    • enable: controls whether the RDR is enabled to collect the key data during training and save key data in the fault scenario. When set to true, the RDR will be turned on. When set to false, the RDR will be turned off.

    • path: sets the path where RDR saves data. The current path must be absolute.

    Memory reuse:

    • mem_Reuse: controls whether the memory reuse function is turned on. When set to True,

    • the memory reuse function is turned on. When set to False, the memory reuse function is turned off.

  • precompile_only (bool) – Whether to only precompile the network. Default: False. If set to True, the network will only be compiled, not executed.

  • reserve_class_name_in_scope (bool) –

    Whether to save the network class name in the scope. Default: True. Each node has a scope. A scope of a subnode is the name of its parent node. If reserve_class_name_in_scope is set to True, the class name will be saved after keyword ‘net-’ in the scope. For example:

    Default/net-Net1/net-Net2 (reserve_class_name_in_scope=True)

    Default/net/net (reserve_class_name_in_scope=False)

  • pynative_synchronize (bool) – Whether to enable synchronous execution of the device in PyNative mode. Default: False. When the value is set to False, the operator is executed asynchronously on the device. When an error occurs in the execution of the operator, the specific error script code location cannot be located, when the value is set to True, the operator is executed synchronously on the device. It will reduce the execution performance of the program. At this time, when an error occurs in the execution of the operator, the location of the error script code can be located according to the call stack of the error.

  • mode (int) – Running in GRAPH_MODE(0) or PYNATIVE_MODE(1). Default: GRAPH_MODE(0). GRAPH_MODE or PYNATIVE_MODE can be set by mode attribute and both modes support all backends, default mode is GRAPH_MODE.

  • enable_graph_kernel (bool) – Whether to enable graph kernel fusion to optimize network execution performance. Default: False. Indicates whether to enable image-computing convergence to optimize network execution performance. If enable_graph_kernel is set to True, acceleration can be enabled. For details of graph kernel fusion, please check Enabling Graph Kernel Fusion.

  • graph_kernel_flags (str) –

    Optimization options of graph kernel fusion, and the priority is higher when it conflicts with enable_graph_kernel. Only for experienced users. For example, context.set_context(graph_kernel_flags=”–opt_level=2 –dump_as_text”). Some general options:

    • opt_level: Set the optimization level. Default: 2. Graph kernel fusion can be enabled equivalently by setting opt_level greater than 0. Available values are:

      • 0: Disable graph kernel fusion;

      • 1: enable the basic fusion of operators;

      • 2: includes all optimizations of level 1, and turns on more optimizations such as CSE, arithmetic simplification and so on;

      • 3: includes all optimizations of level 2, and turns on more optimizations such as SitchingFusion, ParallelFusion and so on. Optimizations of this level are radical and unstable in some scenarios. Be caution when using this level.

    • dump_as_text: dump detail info as text files. Default: false.

    More options can refer to the implementation code. These options can also be set by environment variable MS_GRAPH_KERNEL_FLAGS, without modifying network source code. For example, export MS_GRAPH_KERNEL_FLAGS=”–opt_level=2 –dump_as_text”.

  • enable_reduce_precision (bool) – Whether to enable precision reduction. Default: True. If set to True: user specified precision is not supported, the precision will change automatically. If set to False: if the specified precision of the use case is not specified, an error will be reported and exit; For example, on the ascend backend, conv2d only supports fp16 input. Under the fp32 input condition, set true will automatically insert the cast operator to convert fp16, and the flash personnel will report an error and exit.

  • auto_tune_mode (str) –

    The mode of auto tune when op building, get the best tiling performance. Default: NO_TUNE. The value must be in [‘RL’, ‘GA’, ‘RL,GA’].

    • RL: Reinforcement Learning tune.

    • GA: Genetic Algorithm tune.

    • RL,GA: When both RL and GA optimization are enabled, the tool automatically selects RL or GA based on different types of operators in the network model. The sequence of RL and GA is not differentiated. (Automatic selection).

    For more information about the enable operator tuning tool settings, please check Enable the operator optimization tool.

  • check_bprop (bool) – Whether to check back propagation nodes. The checking ensures that the shape and dtype of back propagation node outputs is the same as input parameters. Default: False.

  • max_call_depth (int) – Specify the maximum depth of function call. Must be positive integer. Default: 1000. The max_call_depth parameter needs to be set when the nested call is too deep or the number of subgraphs is too large. If max_call_depth is set larger than before, the system max stack depth should be set larger too, otherwise a core dumped exception may be raised because of system stack overflow.

  • enable_sparse (bool) – Whether to enable sparsity feature. Default: False. For details of sparsity and sparse tensor, please check sparse tensor.

  • grad_for_scalar (bool) – Whether to get gradient for scalar. Default: False. When grad_for_scalar is set to True, the function’s scalar input can be derived. The default value is False. Because the back-end does not support scaling operations currently, this interface only supports simple operations that can be deduced by the front-end.

  • save_compile_cache (bool) – Whether to cache the graph compiled by front-end. Default: False. After save_compile_cache is set to True, a hardware-independent compilation cache is generated and exported to a MINDIR file, This is an experimental prototype that is subject to change and/or deletion.

  • load_compile_cache (bool) – Whether to use the cache of the graph compiled by front-end. This parameter must be used together with save_compile_cache. After save_compile_cache is set to True, a hardware-independent compilation cache is generated and exported to a MINDIR file. When the network is executed again, if load_compile_cache is set to True, the compile cache is loaded. By now, we do not support automatic checking for changes. Default: False. This is an experimental prototype that is subject to change and/or deletion.

引发

ValueError – If input key is not an attribute in context.

实际案例

>>> context.set_context(mode=context.PYNATIVE_MODE)
>>> context.set_context(precompile_only=True)
>>> context.set_context(device_target="Ascend")
>>> context.set_context(device_id=0)
>>> context.set_context(save_graphs=True, save_graphs_path="./model.ms")
>>> context.set_context(enable_reduce_precision=True)
>>> context.set_context(enable_dump=True, save_dump_path=".")
>>> context.set_context(enable_graph_kernel=True)
>>> context.set_context(graph_kernel_flags="--opt_level=2 --dump_as_text")
>>> context.set_context(reserve_class_name_in_scope=True)
>>> context.set_context(variable_memory_max_size="6GB")
>>> context.set_context(enable_profiling=True,
...                     profiling_options='{"output":"/home/data/output","training_trace":"on"}')
>>> context.set_context(check_bprop=True)
>>> context.set_context(max_device_memory="3.5GB")
>>> context.set_context(print_file_path="print.pb")
>>> context.set_context(enable_sparse=True)
>>> context.set_context(max_call_depth=80)
>>> context.set_context(env_config_path="./env_config.json")
>>> context.set_context(auto_tune_mode="GA,RL")
>>> context.set_context(grad_for_scalar=True)
>>> context.set_context(save_compile_cache=True)
>>> context.set_context(load_compile_cache=True)
>>> context.set_context(pynative_synchronize=True)
tinyms.context.get_context(attr_key)[源代码]

Get context attribute value according to the input key. If some attributes are not set, they will be automatically obtained.

参数

attr_key (str) – The key of the attribute.

返回

Object, The value of given attribute key.

引发

ValueError – If input key is not an attribute in context.

实际案例

>>> context.get_context("device_target")
>>> context.get_context("device_id")
tinyms.context.set_auto_parallel_context(**kwargs)[源代码]

Set auto parallel context, which is valid only for Ascend and GPU target.

Auto parallel context should be configured before the initialization of your network.

注解

Attribute name is required for setting attributes. If a program has tasks on different parallel modes, before setting a new parallel mode for the next task, interface mindspore.context.reset_auto_parallel_context() should be called to reset the configuration. Setting or changing parallel modes must be called before creating any Initializer, otherwise, it may have RuntimeError when compiling the network.

Some configurations are parallel mode specific, see the below table for details:

Common

AUTO_PARALLEL

device_num

gradient_fp32_sync

global_rank

loss_repeated_mean

gradients_mean

auto_parallel_search_mode

parallel_mode

strategy_ckpt_load_file

all_reduce_fusion_config

strategy_ckpt_save_file

enable_parallel_optimizer

dataset_strategy

pipeline_stages

grad_accumulation_step

参数
  • device_num (int) – Available device number, the value must be in [1, 4096]. Default: 1.

  • global_rank (int) – Global rank id, the value must be in [0, 4095]. Default: 0.

  • gradients_mean (bool) – Whether to perform mean operator after allreduce of gradients. “stand_alone” do not support gradients_mean. Default: False.

  • gradient_fp32_sync (bool) – Run allreduce of gradients in fp32. “stand_alone”, “data_parallel” and “hybrid_parallel” do not support gradient_fp32_sync. Default: True.

  • parallel_mode (str) –

    There are five kinds of parallel modes, “stand_alone”, “data_parallel”, “hybrid_parallel”, “semi_auto_parallel” and “auto_parallel”. Default: “stand_alone”.

    • stand_alone: Only one processor is working.

    • data_parallel: Distributes the data across different processors.

    • hybrid_parallel: Achieves data parallelism and model parallelism manually.

    • semi_auto_parallel: Achieves data and model parallelism by setting parallel strategies.

    • auto_parallel: Achieving parallelism automatically.

  • auto_parallel_search_mode (str) –

    There are two kinds of shard strategy search modes, “recursive_programming” and “dynamic_programming”. Default: “dynamic_programming”.

    • recursive_programming: Recursive programming search mode.

    • dynamic_programming: Dynamic programming search mode.

  • parameter_broadcast (bool) – Whether to broadcast parameters before training. Before training, in order to have the same network initialization parameter values for all devices, broadcast the parameters on device 0 to other devices. Parameter broadcasting in different parallel modes is different, data_parallel mode, all parameters are broadcast except for the parameter whose attribute layerwise_parallel is True. Hybrid_parallel, semi_auto_parallel and auto_parallel mode, the segmented parameters do not participate in broadcasting. Default: False.

  • strategy_ckpt_load_file (str) – The path to load parallel strategy checkpoint. Default: ‘’

  • strategy_ckpt_save_file (str) – The path to save parallel strategy checkpoint. Default: ‘’

  • full_batch (bool) – If you load whole batch datasets in auto_parallel mode, this parameter should be set as True. Default: False. The interface is not be recommended currently, it is better using ‘dataset_strategy’ to replace it.

  • dataset_strategy (Union[str, tuple]) – Dataset sharding strategy. Default: “data_parallel”. dataset_strategy=”data_parallel” is equal to full_batch=False, dataset_strategy=”full_batch” is equal to full_batch=True. For dataset load into net by model parallel strategy likes ds_stra ((1, 8), (1, 8)), it requires using set_auto_parallel_context(dataset_strategy=ds_stra).

  • enable_parallel_optimizer (bool) – This is a developing feature, which shards the weight update computation for data parallel training in the benefit of time and memory saving. Currently, auto and semi auto parallel mode support all optimizers in both Ascend and GPU. Data parallel mode only supports Lamb and AdamWeightDecay in Ascend . Default: False.

  • all_reduce_fusion_config (list) – Set allreduce fusion strategy by parameters indices. Only support ReduceOp.SUM and HCCL_WORLD_GROUP/NCCL_WORLD_GROUP. No Default, if it is not set, the fusion is closed.

  • pipeline_stages (int) – Set the stage information for pipeline parallel. This indicates how the devices are distributed alone the pipeline. The total devices will be divided into ‘pipeline_stags’ stages. Currently this could only be used when parallel mode semi_auto_parallel is enabled. Default: 1.

  • grad_accumulation_step (int) – Set the accumulation steps of gradients in auto and semi auto parallel mode. This should be a positive int. Default: 1.

引发

ValueError – If input key is not attribute in auto parallel context.

实际案例

>>> context.set_auto_parallel_context(device_num=8)
>>> context.set_auto_parallel_context(global_rank=0)
>>> context.set_auto_parallel_context(gradients_mean=True)
>>> context.set_auto_parallel_context(gradient_fp32_sync=False)
>>> context.set_auto_parallel_context(parallel_mode="auto_parallel")
>>> context.set_auto_parallel_context(auto_parallel_search_mode="dynamic_programming")
>>> context.set_auto_parallel_context(parameter_broadcast=False)
>>> context.set_auto_parallel_context(strategy_ckpt_load_file="./strategy_stage1.ckpt")
>>> context.set_auto_parallel_context(strategy_ckpt_save_file="./strategy_stage1.ckpt")
>>> context.set_auto_parallel_context(dataset_strategy=((1, 8), (1, 8)))
>>> context.set_auto_parallel_context(enable_parallel_optimizer=False)
>>> context.set_auto_parallel_context(all_reduce_fusion_config=[8, 160])
>>> context.set_auto_parallel_context(pipeline_stages=2)
tinyms.context.get_auto_parallel_context(attr_key)[源代码]

Get auto parallel context attribute value according to the key.

参数

attr_key (str) – The key of the attribute.

返回

Returns attribute value according to the key.

引发

ValueError – If input key is not attribute in auto parallel context.

tinyms.context.reset_auto_parallel_context()[源代码]

Reset auto parallel context attributes to the default values:

  • device_num: 1.

  • global_rank: 0.

  • gradients_mean: False.

  • gradient_fp32_sync: True.

  • parallel_mode: ‘stand_alone’.

  • auto_parallel_search_mode: ‘dynamic_programming’.

  • parameter_broadcast: False.

  • strategy_ckpt_load_file: ‘’.

  • strategy_ckpt_save_file: ‘’.

  • full_batch: False.

  • enable_parallel_optimizer: False.

  • pipeline_stages: 1.

class tinyms.context.ParallelMode[源代码]

Parallel mode options.

There are five kinds of parallel modes, “STAND_ALONE”, “DATA_PARALLEL”, “HYBRID_PARALLEL”, “SEMI_AUTO_PARALLEL” and “AUTO_PARALLEL”. Default: “STAND_ALONE”.

  • STAND_ALONE: Only one processor is working.

  • DATA_PARALLEL: Distributes the data across different processors.

  • HYBRID_PARALLEL: Achieves data parallelism and model parallelism manually.

  • SEMI_AUTO_PARALLEL: Achieves data parallelism and model parallelism by setting parallel strategies.

  • AUTO_PARALLEL: Achieves parallelism automatically.

MODE_LIST: The list of all supported parallel modes.

tinyms.context.set_ps_context(**kwargs)[源代码]

Set parameter server training mode context.

注解

Some other environment variables should also be set for parameter server training mode. These environment variables are listed below:

MS_SERVER_NUM: Server number

MS_WORKER_NUM: Worker number

MS_SCHED_HOST: Scheduler IP address

MS_SCHED_PORT: Scheduler port

MS_ROLE: The role of this process:

MS_SCHED: represents the scheduler,

MS_WORKER: represents the worker,

MS_PSERVER: represents the Server

参数

enable_ps (bool) – Whether to enable parameter server training mode. Only after enable_ps is set True, the environment variables will be effective. Default: False.

引发

ValueError – If input key is not the attribute in parameter server training mode context.

实际案例

>>> context.set_ps_context(enable_ps=True)
tinyms.context.get_ps_context(attr_key)[源代码]

Get parameter server training mode context attribute value according to the key.

参数

attr_key (str) –

The key of the attribute:

  • enable_ps (bool): Whether to enable parameter server training mode.

返回

Returns attribute value according to the key.

引发

ValueError – If input key is not attribute in auto parallel context.

实际案例

>>> context.get_ps_context(enable_ps)
tinyms.context.reset_ps_context()[源代码]

Reset parameter server training mode context attributes to the default values:

  • enable_ps: False.

tinyms.context.set_fl_context(**kwargs)[源代码]

Set federated learning training mode context.

参数
  • enable_fl (bool) – Whether to enable federated learning training mode. Default: False.

  • server_mode (str) – Describe the server mode, which must one of ‘FEDERATED_LEARNING’ and ‘HYBRID_TRAINING’. Default: ‘FEDERATED_LEARNING’.

  • ms_role (str) – The process’s role in the federated learning mode, which must be one of ‘MS_SERVER’, ‘MS_WORKER’ and ‘MS_SCHED’. Default: ‘MS_SERVER’.

  • worker_num (int) – The number of workers. For current version, this must be set to 1 or 0.

  • server_num (int) – The number of federated learning servers. Default: 0.

  • scheduler_ip (str) – The scheduler IP. Default: ‘0.0.0.0’.

  • scheduler_port (int) – The scheduler port. Default: 6667.

  • fl_server_port (int) – The http port of the federated learning server. Normally for each server this should be set to the same value. Default: 6668.

  • enable_fl_client (bool) – Whether this process is federated learning client. Default: False.

  • start_fl_job_threshold (int) – The threshold count of startFLJob. Default: 1.

  • start_fl_job_time_window (int) – The time window duration for startFLJob in millisecond. Default: 3000.

  • share_secrets_ratio (float) – The ratio for computing the threshold count of share secrets. Default: 1.0.

  • update_model_ratio (float) – The ratio for computing the threshold count of updateModel. Default: 1.0.

  • cipher_time_window (int) – The time window duration for each cipher round in millisecond. Default: 300000.

  • reconstruct_secrets_threshold (int) – The threshold count of reconstruct threshold. Default: 0.

  • update_model_time_window (int) – The time window duration for updateModel in millisecond. Default: 3000.

  • fl_name (string) – The federated learning job name. Default: ‘’.

  • fl_iteration_num (int) – Iteration number of federated learning, which is the number of interactions between client and server. Default: 20.

  • client_epoch_num (int) – Client training epoch number. Default: 25.

  • client_batch_size (int) – Client training data batch size. Default: 32.

  • client_learning_rate (float) – Client training learning rate. Default: 0.001.

  • worker_step_num_per_iteration (int) – The worker’s standalone training step number before communicating with server. Default: 65.

  • dp_eps (float) – Epsilon budget of differential privacy mechanism. The smaller the dp_eps, the better the privacy protection effect. Default: 50.0.

  • dp_delta (float) – Delta budget of differential privacy mechanism, which is usually equals the reciprocal of client number. The smaller the dp_delta, the better the privacy protection effect. Default: 0.01.

  • dp_norm_clip (float) – A factor used for clipping model’s weights for differential mechanism. Its value is suggested to be 0.5~2. Default: 1.0.

  • encrypt_type (string) – Secure schema for federated learning, which can be ‘NOT_ENCRYPT’, ‘DP_ENCRYPT’ or ‘PW_ENCRYPT’. If ‘DP_ENCRYPT’, differential privacy schema would be applied for clients and the privacy protection effect would be determined by dp_eps, dp_delta and dp_norm_clip as described above. If ‘PW_ENCRYPT’, pairwise secure aggregation would be applied to protect clients’ model from stealing. Default: ‘NOT_ENCRYPT’.

  • config_file_path (string) – Configuration file path used by recovery. Default: ‘’.

  • scheduler_manage_port (int) – scheduler manage port used to scale out/in. Default: 11202.

  • enable_ssl (bool) – Set PS SSL mode enabled or disabled. Default: true.

  • client_password (str) – Password to decrypt the secret key stored in the client certificate.

  • server_password (str) – Password to decrypt the secret key stored in the server certificate.

引发

ValueError – If input key is not the attribute in federated learning mode context.

实际案例

>>> context.set_fl_context(enable_fl=True, server_mode='FEDERATED_LEARNING')
tinyms.context.get_fl_context(attr_key)[源代码]

Get federated learning mode context attribute value according to the key.

参数

attr_key (str) – The key of the attribute. Please refer to set_fl_context’s parameters to decide what key should be passed.

返回

Returns attribute value according to the key.

引发

ValueError – If input key is not attribute in federated learning mode context.

实际案例

>>> context.get_fl_context("server_mode")

tinyms.data

class tinyms.data.UnalignedDataset(dataset_path, phase, max_dataset_size=inf, shuffle=True)[源代码]

This dataset class can load unaligned/unpaired datasets.

参数
  • dataset_path (str) – The path of images (should have subfolders trainA, trainB, testA, testB, etc).

  • phase (str) – Train or test. It requires two directories in dataset_path, like trainA and trainB to. host training images from domain A ‘{dataset_path}/trainA’ and from domain B ‘{dataset_path}/trainB’ respectively.

  • max_dataset_size (int) – Maximum number of return image paths.

返回

Two domain image path list.

class tinyms.data.GanImageFolderDataset(dataset_path, max_dataset_size=inf)[源代码]

This dataset class can load images from image folder.

参数
  • dataset_path (str) – ‘{dataset_path}/testA’, ‘{dataset_path}/testB’, etc.

  • max_dataset_size (int) – Maximum number of return image paths.

返回

Image path list.

class tinyms.data.ImdbDataset(imdb_path, glove_path, embed_size=300)[源代码]

parse aclImdb data to features and labels. sentence->tokenized->encoded->padding->features

参数
  • imdb_path (str) – The path where the aclImdb dataset stored.

  • glove_path (str) – The path where the GloVe stored.

  • embed_size (int) – Embed_size. Default: 300.

实际案例

>>> from tinyms.data import ImdbDataset
>>>
>>> imdb_ds = ImdbDataset('./aclImdb', './glove')
convert_to_mindrecord(preprocess_path, shard_num=1)[源代码]

convert imdb dataset to mindrecoed dataset

get_datas(seg)[源代码]

get features, labels, and weight by gensim.

parse()[源代码]

parse imdb data to memory

class tinyms.data.BertDataset(data_dir, schema_dir=None, shuffle=True, num_parallel_workers=None)[源代码]

This dataset class can load bert from data folder.

参数
  • data_dir (str) – ‘{data_dir}/result1.tfrecord’, ‘{data_dir}/result2.tfrecord’, etc.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • shuffle (Union[bool, Shuffle level], optional) –

    Perform reshuffling of the data every epoch (default=Shuffle.GLOBAL). If shuffle is False, no shuffling will be performed; If shuffle is True, the behavior is the same as setting shuffle to be Shuffle.GLOBAL Otherwise, there are two levels of shuffling:

    • Shuffle.GLOBAL: Shuffle both the files and samples.

    • Shuffle.FILES: Shuffle files only.

  • schema (Union[str, Schema], optional) – Path to the JSON schema file or schema object (default=None). If the schema is not provided, the meta data from the TFData file is considered the schema.

实际案例

>>> from tinyms.data import BertDataset
>>>
>>> bert_ds = BertDataset('data')
class tinyms.data.KaggleDisplayAdvertisingDataset(data_dir, num_parallel_workers=None, shuffle=True)[源代码]

parse aclImdb data to features and labels. sentence->tokenized->encoded->padding->features

参数
  • data_dir (str) – The path where the uncompressed dataset stored.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • shuffle (bol) – Whether the dataset needs to be shuffled. Default: True.

实际案例

>>> from tinyms.data import KaggleDisplayAdvertisingDataset
>>>
>>> kaggle_display_advertising_ds = KaggleDisplayAdvertisingDataset('data')
>>> kaggle_display_advertising_ds.stats_data()
>>> kaggle_display_advertising_ds.convert_to_mindrecord()
>>> train_ds = kaggle_display_advertising_ds.load_mindreocrd_dataset(usage='train')
>>> test_ds = kaggle_display_advertising_ds.load_mindreocrd_dataset(usage='test')
load_mindreocrd_dataset(usage='train', batch_size=1000)[源代码]

load mindrecord dataset. :param usage: Dataset mode. Default: ‘train’. :type usage: str :param batch_size: batch size. Default: 1000. :type batch_size: int

返回

MindDataset

stats_data()[源代码]

stats data

class tinyms.data.DistributedSampler(dataset_size, num_replicas=None, rank=None, shuffle=True)[源代码]

Distributed sampler.

参数
  • dataset_size (int) – Dataset list length

  • num_replicas (int) – Replicas num.

  • rank (int) – Device rank.

  • shuffle (bool) – Whether the dataset needs to be shuffled. Default: True.

返回

DistributedSampler instance.

class tinyms.data.CelebADataset(dataset_dir, num_parallel_workers=None, shuffle=None, usage='all', sampler=None, decode=False, extensions=None, num_samples=None, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset for reading and parsing CelebA dataset. Only support to read list_attr_celeba.txt currently, which is the attribute annotations of the dataset.

The generated dataset has two columns: [image, attr]. The tensor of column image is of the uint8 type. The tensor of column attr is of the uint32 type and one hot encoded.

参数
  • dataset_dir (str) – Path to the root directory that contains the dataset.

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, will use value set in the config).

  • shuffle (bool, optional) – Whether to perform shuffle on the dataset (default=None).

  • usage (str, optional) – Specify the train, valid, test part or all parts of dataset (default=`all`, will read all samples).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None).

  • decode (bool, optional) – decode the images after reading (default=False).

  • extensions (list[str], optional) – List of file extensions to be included in the dataset (default=None).

  • num_samples (int, optional) – The number of images to be included in the dataset (default=None, will include all images).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_dir does not contain data files.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> celeba_dataset_dir = "/path/to/celeba_dataset_directory"
>>>
>>> # Read 5 samples from CelebA dataset
>>> dataset = ds.CelebADataset(dataset_dir=celeba_dataset_dir, usage='train', num_samples=5)
>>>
>>> # Note: In celeba dataset, each data dictionary owns keys "image" and "attr"

About CelebA dataset:

CelebFaces Attributes Dataset (CelebA) is a large-scale face attributes dataset with more than 200K celebrity images, each with 40 attribute annotations.

The images in this dataset cover large pose variations and background clutter. CelebA has large diversities, large quantities, and rich annotations, including

  • 10,177 number of identities,

  • 202,599 number of face images, and

  • 5 landmark locations, 40 binary attributes annotations per image.

The dataset can be employed as the training and test sets for the following computer vision tasks: face attribute recognition, face detection, landmark (or facial part) localization, and face editing & synthesis.

Original CelebA dataset structure:

.
└── CelebA
     ├── README.md
     ├── Img
     │    ├── img_celeba.7z
     │    ├── img_align_celeba_png.7z
     │    └── img_align_celeba.zip
     ├── Eval
     │    └── list_eval_partition.txt
     └── Anno
          ├── list_landmarks_celeba.txt
          ├── list_landmarks_align_celeba.txt
          ├── list_bbox_celeba.txt
          ├── list_attr_celeba.txt
          └── identity_CelebA.txt

You can unzip the dataset files into the following structure and read by MindSpore’s API.

.
└── celeba_dataset_directory
    ├── list_attr_celeba.txt
    ├── 000001.jpg
    ├── 000002.jpg
    ├── 000003.jpg
    ├── ...

Citation:

@article{DBLP:journals/corr/LiuLWT14,
author        = {Ziwei Liu and Ping Luo and Xiaogang Wang and Xiaoou Tang},
title         = {Deep Learning Face Attributes in the Wild},
journal       = {CoRR},
volume        = {abs/1411.7766},
year          = {2014},
url           = {http://arxiv.org/abs/1411.7766},
archivePrefix = {arXiv},
eprint        = {1411.7766},
timestamp     = {Tue, 10 Dec 2019 15:37:26 +0100},
biburl        = {https://dblp.org/rec/journals/corr/LiuLWT14.bib},
bibsource     = {dblp computer science bibliography, https://dblp.org},
howpublished  = {http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html}
}
class tinyms.data.Cifar100Dataset(dataset_dir, usage=None, num_samples=None, num_parallel_workers=None, shuffle=None, sampler=None, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset for reading and parsing Cifar100 dataset.

The generated dataset has three columns [image, coarse_label, fine_label]. The tensor of column image is of the uint8 type. The tensor of column coarse_label and fine_labels are each a scalar of uint32 type.

参数
  • dataset_dir (str) – Path to the root directory that contains the dataset.

  • usage (str, optional) – Usage of this dataset, can be train, test or all . train will read from 50,000 train samples, test will read from 10,000 test samples, all will read from all 60,000 samples (default=None, all samples).

  • num_samples (int, optional) – The number of images to be included in the dataset (default=None, all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the config).

  • shuffle (bool, optional) – Whether to perform shuffle on the dataset (default=None, expected order behavior shown in the table).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, expected order behavior shown in the table).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, ‘num_samples’ reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_dir does not contain data files.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> cifar100_dataset_dir = "/path/to/cifar100_dataset_directory"
>>>
>>> # 1) Get all samples from CIFAR100 dataset in sequence
>>> dataset = ds.Cifar100Dataset(dataset_dir=cifar100_dataset_dir, shuffle=False)
>>>
>>> # 2) Randomly select 350 samples from CIFAR100 dataset
>>> dataset = ds.Cifar100Dataset(dataset_dir=cifar100_dataset_dir, num_samples=350, shuffle=True)
>>>
>>> # In CIFAR100 dataset, each dictionary has 3 keys: "image", "fine_label" and "coarse_label"

About CIFAR-100 dataset:

This dataset is just like the CIFAR-10, except it has 100 classes containing 600 images each. There are 500 training images and 100 testing images per class. The 100 classes in the CIFAR-100 are grouped into 20 superclasses. Each image comes with a “fine” label (the class to which it belongs) and a “coarse” label (the superclass to which it belongs).

Here is the original CIFAR-100 dataset structure. You can unzip the dataset files into the following directory structure and read by MindSpore’s API.

.
└── cifar-100-binary
    ├── train.bin
    ├── test.bin
    ├── fine_label_names.txt
    └── coarse_label_names.txt

Citation:

@techreport{Krizhevsky09,
author       = {Alex Krizhevsky},
title        = {Learning multiple layers of features from tiny images},
institution  = {},
year         = {2009},
howpublished = {http://www.cs.toronto.edu/~kriz/cifar.html}
}
class tinyms.data.Cifar10Dataset(dataset_dir, usage=None, num_samples=None, num_parallel_workers=None, shuffle=None, sampler=None, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset for reading and parsing Cifar10 dataset. This api only supports parsing Cifar10 file in binary version now.

The generated dataset has two columns [image, label]. The tensor of column image is of the uint8 type. The tensor of column label is a scalar of the uint32 type.

参数
  • dataset_dir (str) – Path to the root directory that contains the dataset.

  • usage (str, optional) – Usage of this dataset, can be train, test or all . train will read from 50,000 train samples, test will read from 10,000 test samples, all will read from all 60,000 samples (default=None, all samples).

  • num_samples (int, optional) – The number of images to be included in the dataset (default=None, all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the config).

  • shuffle (bool, optional) – Whether to perform shuffle on the dataset (default=None, expected order behavior shown in the table).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, expected order behavior shown in the table).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_dir does not contain data files.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> cifar10_dataset_dir = "/path/to/cifar10_dataset_directory"
>>>
>>> # 1) Get all samples from CIFAR10 dataset in sequence
>>> dataset = ds.Cifar10Dataset(dataset_dir=cifar10_dataset_dir, shuffle=False)
>>>
>>> # 2) Randomly select 350 samples from CIFAR10 dataset
>>> dataset = ds.Cifar10Dataset(dataset_dir=cifar10_dataset_dir, num_samples=350, shuffle=True)
>>>
>>> # 3) Get samples from CIFAR10 dataset for shard 0 in a 2-way distributed training
>>> dataset = ds.Cifar10Dataset(dataset_dir=cifar10_dataset_dir, num_shards=2, shard_id=0)
>>>
>>> # In CIFAR10 dataset, each dictionary has keys "image" and "label"

About CIFAR-10 dataset:

The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images. The 10 different classes represent airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks.

Here is the original CIFAR-10 dataset structure. You can unzip the dataset files into the following directory structure and read by MindSpore’s API.

.
└── cifar-10-batches-bin
     ├── data_batch_1.bin
     ├── data_batch_2.bin
     ├── data_batch_3.bin
     ├── data_batch_4.bin
     ├── data_batch_5.bin
     ├── test_batch.bin
     ├── readme.html
     └── batches.meta.txt

Citation:

@techreport{Krizhevsky09,
author       = {Alex Krizhevsky},
title        = {Learning multiple layers of features from tiny images},
institution  = {},
year         = {2009},
howpublished = {http://www.cs.toronto.edu/~kriz/cifar.html}
}
class tinyms.data.CLUEDataset(dataset_files, task='AFQMC', usage='train', num_samples=None, num_parallel_workers=None, shuffle=<Shuffle.GLOBAL: 'global'>, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset that reads and parses CLUE datasets. Supported CLUE classification tasks: AFQMC, TNEWS, IFLYTEK, CMNLI, WSC and CSL.

The generated dataset with different task setting has different output columns:

  • task = AFQMC
    • usage = train, output columns: [sentence1, dtype=string], [sentence2, dtype=string], [label, dtype=string].

    • usage = test, output columns: [id, dtype=uint8], [sentence1, dtype=string], [sentence2, dtype=string].

    • usage = eval, output columns: [sentence1, dtype=string], [sentence2, dtype=string], [label, dtype=string].

  • task = TNEWS
    • usage = train, output columns: [label, dtype=string], [label_des, dtype=string], [sentence, dtype=string], [keywords, dtype=string].

    • usage = test, output columns: [label, dtype=string], [label_des, dtype=string], [sentence, dtype=string], [keywords, dtype=string].

    • usage = eval, output columns: [label, dtype=string], [label_des, dtype=string], [sentence, dtype=string], [keywords, dtype=string].

  • task = IFLYTEK
    • usage = train, output columns: [label, dtype=string], [label_des, dtype=string], [sentence, dtype=string].

    • usage = test, output columns: [id, dtype=string], [sentence, dtype=string].

    • usage = eval, output columns: [label, dtype=string], [label_des, dtype=string], [sentence, dtype=string].

  • task = CMNLI
    • usage = train, output columns: [sentence1, dtype=string], [sentence2, dtype=string], [label, dtype=string].

    • usage = test, output columns: [id, dtype=uint8], [sentence1, dtype=string], [sentence2, dtype=string].

    • usage = eval, output columns: [sentence1, dtype=string], [sentence2, dtype=string], [label, dtype=string].

  • task = WSC
    • usage = train, output columns: [span1_index, dtype=uint8], [span2_index, dtype=uint8], [span1_text, dtype=string], [span2_text, dtype=string], [idx, dtype=uint8], [text, dtype=string], [label, dtype=string].

    • usage = output columns: [span1_index, dtype=uint8], [span2_index, dtype=uint8], [span1_text, dtype=string], [span2_text, dtype=string], [idx, dtype=uint8], [text, dtype=string].

    • usage = eval, output columns: [span1_index, dtype=uint8], [span2_index, dtype=uint8], [span1_text, dtype=string], [span2_text, dtype=string], [idx, dtype=uint8], [text, dtype=string], [label, dtype=string].

  • task = CSL
    • usage = train, output columns: [id, dtype=uint8], [abst, dtype=string], [keyword, dtype=string], [label, dtype=string].

    • usage = test, output columns: [id, dtype=uint8], [abst, dtype=string], [keyword, dtype=string].

    • usage = eval, output columns: [id, dtype=uint8], [abst, dtype=string], [keyword, dtype=string], [label, dtype=string].

参数
  • dataset_files (Union[str, list[str]]) – String or list of files to be read or glob strings to search for a pattern of files. The list will be sorted in a lexicographical order.

  • task (str, optional) – The kind of task, one of AFQMC, TNEWS, IFLYTEK, CMNLI, WSC and CSL. (default=AFQMC).

  • usage (str, optional) – Specify the train, test or eval part of dataset (default=”train”).

  • num_samples (int, optional) – The number of samples to be included in the dataset (default=None, will include all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the config).

  • shuffle (Union[bool, Shuffle level], optional) –

    Perform reshuffling of the data every epoch (default=Shuffle.GLOBAL). If shuffle is False, no shuffling will be performed; If shuffle is True, the behavior is the same as setting shuffle to be Shuffle.GLOBAL Otherwise, there are two levels of shuffling:

    • Shuffle.GLOBAL: Shuffle both the files and samples.

    • Shuffle.FILES: Shuffle files only.

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_files are not valid or do not exist.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

实际案例

>>> clue_dataset_dir = ["/path/to/clue_dataset_file"] # contains 1 or multiple clue files
>>> dataset = ds.CLUEDataset(dataset_files=clue_dataset_dir, task='AFQMC', usage='train')

About CLUE dataset:

CLUE, a Chinese Language Understanding Evaluation benchmark. It contains multiple tasks, including single-sentence classification, sentence pair classification, and machine reading comprehension.

You can unzip the dataset files into the following structure and read by MindSpore’s API, such as afqmc dataset:

.
└── afqmc_public
     ├── train.json
     ├── test.json
     └── dev.json

Citation:

@article{CLUEbenchmark,
title   = {CLUE: A Chinese Language Understanding Evaluation Benchmark},
author  = {Liang Xu, Xuanwei Zhang, Lu Li, Hai Hu, Chenjie Cao, Weitang Liu, Junyi Li, Yudong Li,
        Kai Sun, Yechen Xu, Yiming Cui, Cong Yu, Qianqian Dong, Yin Tian, Dian Yu, Bo Shi, Jun Zeng,
        Rongzhao Wang, Weijian Xie, Yanting Li, Yina Patterson, Zuoyu Tian, Yiwen Zhang, He Zhou,
        Shaoweihua Liu, Qipeng Zhao, Cong Yue, Xinrui Zhang, Zhengliang Yang, Zhenzhong Lan},
journal = {arXiv preprint arXiv:2004.05986},
year    = {2020},
howpublished = {https://github.com/CLUEbenchmark/CLUE}
}
class tinyms.data.CocoDataset(dataset_dir, annotation_file, task='Detection', num_samples=None, num_parallel_workers=None, shuffle=None, decode=False, sampler=None, num_shards=None, shard_id=None, cache=None, extra_metadata=False)[源代码]

A source dataset for reading and parsing COCO dataset.

CocoDataset supports four kinds of tasks, which are Object Detection, Keypoint Detection, Stuff Segmentation and Panoptic Segmentation of 2017 Train/Val/Test dataset.

The generated dataset with different task setting has different output columns:

  • task = Detection, output columns: [image, dtype=uint8], [bbox, dtype=float32], [category_id, dtype=uint32], [iscrowd, dtype=uint32].

  • task = Stuff, output columns: [image, dtype=uint8], [segmentation,dtype=float32], [iscrowd,dtype=uint32].

  • task = Keypoint, output columns: [image, dtype=uint8], [keypoints, dtype=float32], [num_keypoints, dtype=uint32].

  • task = Panoptic, output columns: [image, dtype=uint8], [bbox, dtype=float32], [category_id, dtype=uint32], [iscrowd, dtype=uint32], [area, dtype=uint32].

参数
  • dataset_dir (str) – Path to the root directory that contains the dataset.

  • annotation_file (str) – Path to the annotation JSON file.

  • task (str, optional) – Set the task type for reading COCO data. Supported task types: Detection, Stuff, Panoptic and Keypoint (default=`Detection`).

  • num_samples (int, optional) – The number of images to be included in the dataset (default=None, all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the configuration file).

  • shuffle (bool, optional) – Whether to perform shuffle on the dataset (default=None, expected order behavior shown in the table).

  • decode (bool, optional) – Decode the images after reading (default=False).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, expected order behavior shown in the table).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

  • extra_metadata (bool, optional) – Flag to add extra meta-data to row. If True, an additional column will be output at the end [_meta-filename, dtype=string] (default=False).

引发
  • RuntimeError – If dataset_dir does not contain data files.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • RuntimeError – If parse JSON file failed.

  • ValueError – If task is not in [Detection, Stuff, Panoptic, Keypoint].

  • ValueError – If annotation_file is not exist.

  • ValueError – If dataset_dir is not exist.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • Column ‘[_meta-filename, dtype=string]’ won’t be output unless an explicit rename dataset op is added to remove the prefix(‘_meta-‘).

  • CocoDataset doesn’t support PKSampler.

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> coco_dataset_dir = "/path/to/coco_dataset_directory/images"
>>> coco_annotation_file = "/path/to/coco_dataset_directory/annotation_file"
>>>
>>> # 1) Read COCO data for Detection task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...                          annotation_file=coco_annotation_file,
...                          task='Detection')
>>>
>>> # 2) Read COCO data for Stuff task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...                          annotation_file=coco_annotation_file,
...                          task='Stuff')
>>>
>>> # 3) Read COCO data for Panoptic task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...                          annotation_file=coco_annotation_file,
...                          task='Panoptic')
>>>
>>> # 4) Read COCO data for Keypoint task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...                          annotation_file=coco_annotation_file,
...                          task='Keypoint')
>>>
>>> # In COCO dataset, each dictionary has keys "image" and "annotation"

About COCO dataset:

COCO(Microsoft Common Objects in Context) is a large-scale object detection, segmentation, and captioning dataset with several features: Object segmentation, Recognition in context, Superpixel stuff segmentation, 330K images (>200K labeled), 1.5 million object instances, 80 object categories, 91 stuff categories, 5 captions per image, 250,000 people with keypoints. In contrast to the popular ImageNet dataset, COCO has fewer categories but more instances in per category.

You can unzip the original COCO-2017 dataset files into this directory structure and read by MindSpore’s API.

.
└── coco_dataset_directory
     ├── train2017
     │    ├── 000000000009.jpg
     │    ├── 000000000025.jpg
     │    ├── ...
     ├── test2017
     │    ├── 000000000001.jpg
     │    ├── 000000058136.jpg
     │    ├── ...
     ├── val2017
     │    ├── 000000000139.jpg
     │    ├── 000000057027.jpg
     │    ├── ...
     └── annotations
          ├── captions_train2017.json
          ├── captions_val2017.json
          ├── instances_train2017.json
          ├── instances_val2017.json
          ├── person_keypoints_train2017.json
          └── person_keypoints_val2017.json

Citation:

@article{DBLP:journals/corr/LinMBHPRDZ14,
author        = {Tsung{-}Yi Lin and Michael Maire and Serge J. Belongie and
                Lubomir D. Bourdev and  Ross B. Girshick and James Hays and
                Pietro Perona and Deva Ramanan and Piotr Doll{'{a}}r and C. Lawrence Zitnick},
title         = {Microsoft {COCO:} Common Objects in Context},
journal       = {CoRR},
volume        = {abs/1405.0312},
year          = {2014},
url           = {http://arxiv.org/abs/1405.0312},
archivePrefix = {arXiv},
eprint        = {1405.0312},
timestamp     = {Mon, 13 Aug 2018 16:48:13 +0200},
biburl        = {https://dblp.org/rec/journals/corr/LinMBHPRDZ14.bib},
bibsource     = {dblp computer science bibliography, https://dblp.org}
}
get_class_indexing()[源代码]

Get the class index.

返回

dict, a str-to-list<int> mapping from label name to index.

实际案例

>>> coco_dataset_dir = "/path/to/coco_dataset_directory/images"
>>> coco_annotation_file = "/path/to/coco_dataset_directory/annotation_file"
>>>
>>> # Read COCO data for Detection task
>>> dataset = ds.CocoDataset(dataset_dir=coco_dataset_dir,
...                          annotation_file=coco_annotation_file,
...                          task='Detection')
>>>
>>> class_indexing = dataset.get_class_indexing()
class tinyms.data.CSVDataset(dataset_files, field_delim=', ', column_defaults=None, column_names=None, num_samples=None, num_parallel_workers=None, shuffle=<Shuffle.GLOBAL: 'global'>, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset that reads and parses comma-separated values (CSV) datasets. The columns of generated dataset depend on the source CSV files.

参数
  • dataset_files (Union[str, list[str]]) – String or list of files to be read or glob strings to search for a pattern of files. The list will be sorted in a lexicographical order.

  • field_delim (str, optional) – A string that indicates the char delimiter to separate fields (default=’,’).

  • column_defaults (list, optional) – List of default values for the CSV field (default=None). Each item in the list is either a valid type (float, int, or string). If this is not provided, treats all columns as string type.

  • column_names (list[str], optional) – List of column names of the dataset (default=None). If this is not provided, infers the column_names from the first row of CSV file.

  • num_samples (int, optional) – The number of samples to be included in the dataset (default=None, will include all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the config).

  • shuffle (Union[bool, Shuffle level], optional) –

    Perform reshuffling of the data every epoch (default=Shuffle.GLOBAL). If shuffle is False, no shuffling will be performed; If shuffle is True, the behavior is the same as setting shuffle to be Shuffle.GLOBAL Otherwise, there are two levels of shuffling:

    • Shuffle.GLOBAL: Shuffle both the files and samples.

    • Shuffle.FILES: Shuffle files only.

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_files are not valid or do not exist.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

实际案例

>>> csv_dataset_dir = ["/path/to/csv_dataset_file"] # contains 1 or multiple csv files
>>> dataset = ds.CSVDataset(dataset_files=csv_dataset_dir, column_names=['col1', 'col2', 'col3', 'col4'])
class tinyms.data.GeneratorDataset(source, column_names=None, column_types=None, schema=None, num_samples=None, num_parallel_workers=1, shuffle=None, sampler=None, num_shards=None, shard_id=None, python_multiprocessing=True, max_rowsize=6)[源代码]

A source dataset that generates data from Python by invoking Python data source each epoch.

The column names and column types of generated dataset depend on Python data defined by users.

参数
  • source (Union[Callable, Iterable, Random Accessible]) – A generator callable object, an iterable Python object or a random accessible Python object. Callable source is required to return a tuple of NumPy arrays as a row of the dataset on source().next(). Iterable source is required to return a tuple of NumPy arrays as a row of the dataset on iter(source).next(). Random accessible source is required to return a tuple of NumPy arrays as a row of the dataset on source[idx].

  • column_names (Union[str, list[str]], optional) – List of column names of the dataset (default=None). Users are required to provide either column_names or schema.

  • column_types (list[mindspore.dtype], optional) – List of column data types of the dataset (default=None). If provided, sanity check will be performed on generator output.

  • schema (Union[Schema, str], optional) – Path to the JSON schema file or schema object (default=None). Users are required to provide either column_names or schema. If both are provided, schema will be used.

  • num_samples (int, optional) – The number of samples to be included in the dataset (default=None, all images).

  • num_parallel_workers (int, optional) – Number of subprocesses used to fetch the dataset in parallel (default=1).

  • shuffle (bool, optional) – Whether or not to perform shuffle on the dataset. Random accessible input is required. (default=None, expected order behavior shown in the table).

  • sampler (Union[Sampler, Iterable], optional) – Object used to choose samples from the dataset. Random accessible input is required (default=None, expected order behavior shown in the table).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). Random accessible input is required. When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument must be specified only when num_shards is also specified. Random accessible input is required.

  • python_multiprocessing (bool, optional) – Parallelize Python operations with multiple worker process. This option could be beneficial if the Python operation is computational heavy (default=True).

  • max_rowsize (int, optional) – Maximum size of row in MB that is used for shared memory allocation to copy data between processes. This is only used if python_multiprocessing is set to True (default 6 MB).

引发
  • RuntimeError – If source raises an exception during execution.

  • RuntimeError – If len of column_names does not match output len of source.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> import numpy as np
>>>
>>> # 1) Multidimensional generator function as callable input.
>>> def generator_multidimensional():
...     for i in range(64):
...         yield (np.array([[i, i + 1], [i + 2, i + 3]]),)
>>>
>>> dataset = ds.GeneratorDataset(source=generator_multidimensional, column_names=["multi_dimensional_data"])
>>>
>>> # 2) Multi-column generator function as callable input.
>>> def generator_multi_column():
...     for i in range(64):
...         yield np.array([i]), np.array([[i, i + 1], [i + 2, i + 3]])
>>>
>>> dataset = ds.GeneratorDataset(source=generator_multi_column, column_names=["col1", "col2"])
>>>
>>> # 3) Iterable dataset as iterable input.
>>> class MyIterable:
...     def __init__(self):
...         self._index = 0
...         self._data = np.random.sample((5, 2))
...         self._label = np.random.sample((5, 1))
...
...     def __next__(self):
...         if self._index >= len(self._data):
...             raise StopIteration
...         else:
...             item = (self._data[self._index], self._label[self._index])
...             self._index += 1
...             return item
...
...     def __iter__(self):
...         self._index = 0
...         return self
...
...     def __len__(self):
...         return len(self._data)
>>>
>>> dataset = ds.GeneratorDataset(source=MyIterable(), column_names=["data", "label"])
>>>
>>> # 4) Random accessible dataset as random accessible input.
>>> class MyAccessible:
...     def __init__(self):
...         self._data = np.random.sample((5, 2))
...         self._label = np.random.sample((5, 1))
...
...     def __getitem__(self, index):
...         return self._data[index], self._label[index]
...
...     def __len__(self):
...         return len(self._data)
>>>
>>> dataset = ds.GeneratorDataset(source=MyAccessible(), column_names=["data", "label"])
>>>
>>> # list, dict, tuple of Python is also random accessible
>>> dataset = ds.GeneratorDataset(source=[(np.array(0),), (np.array(1),), (np.array(2),)], column_names=["col"])
class tinyms.data.GraphData(dataset_file, num_parallel_workers=None, working_mode='local', hostname='127.0.0.1', port=50051, num_client=1, auto_shutdown=True)[源代码]

Reads the graph dataset used for GNN training from the shared file and database.

参数
  • dataset_file (str) – One of file names in the dataset.

  • num_parallel_workers (int, optional) – Number of workers to process the dataset in parallel (default=None).

  • working_mode (str, optional) –

    Set working mode, now supports ‘local’/’client’/’server’ (default=’local’).

    • ’local’, used in non-distributed training scenarios.

    • ’client’, used in distributed training scenarios. The client does not load data, but obtains data from the server.

    • ’server’, used in distributed training scenarios. The server loads the data and is available to the client.

  • hostname (str, optional) – Hostname of the graph data server. This parameter is only valid when working_mode is set to ‘client’ or ‘server’ (default=’127.0.0.1’).

  • port (int, optional) – Port of the graph data server. The range is 1024-65535. This parameter is only valid when working_mode is set to ‘client’ or ‘server’ (default=50051).

  • num_client (int, optional) – Maximum number of clients expected to connect to the server. The server will allocate resources according to this parameter. This parameter is only valid when working_mode is set to ‘server’ (default=1).

  • auto_shutdown (bool, optional) – Valid when working_mode is set to ‘server’, when the number of connected clients reaches num_client and no client is being connected, the server automatically exits (default=True).

实际案例

>>> graph_dataset_dir = "/path/to/graph_dataset_file"
>>> graph_dataset = ds.GraphData(dataset_file=graph_dataset_dir, num_parallel_workers=2)
>>> nodes = graph_dataset.get_all_nodes(node_type=1)
>>> features = graph_dataset.get_node_feature(node_list=nodes, feature_types=[1])
get_all_edges(edge_type)[源代码]

Get all edges in the graph.

参数

edge_type (int) – Specify the type of edge.

返回

numpy.ndarray, array of edges.

实际案例

>>> edges = graph_dataset.get_all_edges(edge_type=0)
引发

TypeError – If edge_type is not integer.

get_all_neighbors(node_list, neighbor_type, output_format=<OutputFormat.NORMAL: 0>)[源代码]

Get neighbor_type neighbors of the nodes in node_list. We try to use the following example to illustrate the definition of these formats. 1 represents connected between two nodes, and 0 represents not connected.

Adjacent Matrix

0

1

2

3

0

0

1

0

0

1

0

0

1

0

2

1

0

0

1

3

1

0

0

0

Normal Format

src

0

1

2

3

dst_0

1

2

0

1

dst_1

-1

-1

3

-1

COO Format

src

0

1

2

2

3

dst

1

2

0

3

1

CSR Format

offsetTable

0

1

2

4

dstTable

1

2

0

3

1

参数
  • node_list (Union[list, numpy.ndarray]) – The given list of nodes.

  • neighbor_type (int) – Specify the type of neighbor.

  • output_format (OutputFormat, optional) – Output storage format (default=OutputFormat.NORMAL) It can be any of [OutputFormat.NORMAL, OutputFormat.COO, OutputFormat.CSR].

返回

For NORMAL format or COO format numpy.ndarray which represents the array of neighbors will return. As if CSR format is specified, two numpy.ndarrays will return. The first one is offset table, the second one is neighbors

实际案例

>>> from mindspore.dataset.engine import OutputFormat
>>> nodes = graph_dataset.get_all_nodes(node_type=1)
>>> neighbors = graph_dataset.get_all_neighbors(node_list=nodes, neighbor_type=2)
>>> neighbors_coo = graph_dataset.get_all_neighbors(node_list=nodes, neighbor_type=2,
...                                                 output_format=OutputFormat.COO)
>>> offset_table, neighbors_csr = graph_dataset.get_all_neighbors(node_list=nodes, neighbor_type=2,
...                                                               output_format=OutputFormat.CSR)
引发
  • TypeError – If node_list is not list or ndarray.

  • TypeError – If neighbor_type is not integer.

get_all_nodes(node_type)[源代码]

Get all nodes in the graph.

参数

node_type (int) – Specify the type of node.

返回

numpy.ndarray, array of nodes.

实际案例

>>> nodes = graph_dataset.get_all_nodes(node_type=1)
引发

TypeError – If node_type is not integer.

get_edge_feature(edge_list, feature_types)[源代码]

Get feature_types feature of the edges in edge_list.

参数
返回

numpy.ndarray, array of features.

实际案例

>>> edges = graph_dataset.get_all_edges(edge_type=0)
>>> features = graph_dataset.get_edge_feature(edge_list=edges, feature_types=[1])
引发
  • TypeError – If edge_list is not list or ndarray.

  • TypeError – If feature_types is not list or ndarray.

get_edges_from_nodes(node_list)[源代码]

Get edges from the nodes.

参数

node_list (Union[list[tuple], numpy.ndarray]) – The given list of pair nodes ID.

返回

numpy.ndarray, array of edges ID.

实际案例

>>> edges = graph_dataset.get_edges_from_nodes(node_list=[(101, 201), (103, 207)])
引发

TypeError – If edge_list is not list or ndarray.

get_neg_sampled_neighbors(node_list, neg_neighbor_num, neg_neighbor_type)[源代码]

Get neg_neighbor_type negative sampled neighbors of the nodes in node_list.

参数
  • node_list (Union[list, numpy.ndarray]) – The given list of nodes.

  • neg_neighbor_num (int) – Number of neighbors sampled.

  • neg_neighbor_type (int) – Specify the type of negative neighbor.

返回

numpy.ndarray, array of neighbors.

实际案例

>>> nodes = graph_dataset.get_all_nodes(node_type=1)
>>> neg_neighbors = graph_dataset.get_neg_sampled_neighbors(node_list=nodes, neg_neighbor_num=5,
...                                                         neg_neighbor_type=2)
引发
  • TypeError – If node_list is not list or ndarray.

  • TypeError – If neg_neighbor_num is not integer.

  • TypeError – If neg_neighbor_type is not integer.

get_node_feature(node_list, feature_types)[源代码]

Get feature_types feature of the nodes in node_list.

参数
返回

numpy.ndarray, array of features.

实际案例

>>> nodes = graph_dataset.get_all_nodes(node_type=1)
>>> features = graph_dataset.get_node_feature(node_list=nodes, feature_types=[2, 3])
引发
  • TypeError – If node_list is not list or ndarray.

  • TypeError – If feature_types is not list or ndarray.

get_nodes_from_edges(edge_list)[源代码]

Get nodes from the edges.

参数

edge_list (Union[list, numpy.ndarray]) – The given list of edges.

返回

numpy.ndarray, array of nodes.

引发

TypeError – If edge_list is not list or ndarray.

get_sampled_neighbors(node_list, neighbor_nums, neighbor_types, strategy=<SamplingStrategy.RANDOM: 0>)[源代码]

Get sampled neighbor information.

The api supports multi-hop neighbor sampling. That is, the previous sampling result is used as the input of next-hop sampling. A maximum of 6-hop are allowed.

The sampling result is tiled into a list in the format of [input node, 1-hop sampling result, 2-hop sampling result …]

参数
  • node_list (Union[list, numpy.ndarray]) – The given list of nodes.

  • neighbor_nums (Union[list, numpy.ndarray]) – Number of neighbors sampled per hop.

  • neighbor_types (Union[list, numpy.ndarray]) – Neighbor type sampled per hop.

  • strategy (SamplingStrategy, optional) –

    Sampling strategy (default=SamplingStrategy.RANDOM). It can be any of [SamplingStrategy.RANDOM, SamplingStrategy.EDGE_WEIGHT].

    • SamplingStrategy.RANDOM, random sampling with replacement.

    • SamplingStrategy.EDGE_WEIGHT, sampling with edge weight as probability.

返回

numpy.ndarray, array of neighbors.

实际案例

>>> nodes = graph_dataset.get_all_nodes(node_type=1)
>>> neighbors = graph_dataset.get_sampled_neighbors(node_list=nodes, neighbor_nums=[2, 2],
...                                                 neighbor_types=[2, 1])
引发
  • TypeError – If node_list is not list or ndarray.

  • TypeError – If neighbor_nums is not list or ndarray.

  • TypeError – If neighbor_types is not list or ndarray.

graph_info()[源代码]

Get the meta information of the graph, including the number of nodes, the type of nodes, the feature information of nodes, the number of edges, the type of edges, and the feature information of edges.

返回

dict, meta information of the graph. The key is node_type, edge_type, node_num, edge_num, node_feature_type and edge_feature_type.

random_walk(target_nodes, meta_path, step_home_param=1.0, step_away_param=1.0, default_node=-1)[源代码]

Random walk in nodes.

参数
  • target_nodes (list[int]) – Start node list in random walk

  • meta_path (list[int]) – node type for each walk step

  • step_home_param (float, optional) – return hyper parameter in node2vec algorithm (Default = 1.0).

  • step_away_param (float, optional) – in out hyper parameter in node2vec algorithm (Default = 1.0).

  • default_node (int, optional) – default node if no more neighbors found (Default = -1). A default value of -1 indicates that no node is given.

返回

numpy.ndarray, array of nodes.

实际案例

>>> nodes = graph_dataset.get_all_nodes(node_type=1)
>>> walks = graph_dataset.random_walk(target_nodes=nodes, meta_path=[2, 1, 2])
引发
  • TypeError – If target_nodes is not list or ndarray.

  • TypeError – If meta_path is not list or ndarray.

class tinyms.data.ImageFolderDataset(dataset_dir, num_samples=None, num_parallel_workers=None, shuffle=None, sampler=None, extensions=None, class_indexing=None, decode=False, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset that reads images from a tree of directories. All images within one folder have the same label.

The generated dataset has two columns: [image, label]. The tensor of column image is of the uint8 type. The tensor of column label is of a scalar of uint32 type.

参数
  • dataset_dir (str) – Path to the root directory that contains the dataset.

  • num_samples (int, optional) – The number of images to be included in the dataset (default=None, all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, set in the config).

  • shuffle (bool, optional) – Whether or not to perform shuffle on the dataset (default=None, expected order behavior shown in the table).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, expected order behavior shown in the table).

  • extensions (list[str], optional) – List of file extensions to be included in the dataset (default=None).

  • class_indexing (dict, optional) – A str-to-int mapping from folder name to index (default=None, the folder names will be sorted alphabetically and each class will be given a unique index starting from 0).

  • decode (bool, optional) – Decode the images after reading (default=False).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_dir does not contain data files.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • RuntimeError – If class_indexing is not a dictionary.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • The shape of the image column is [image_size] if decode flag is False, or [H,W,C] otherwise.

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> image_folder_dataset_dir = "/path/to/image_folder_dataset_directory"
>>>
>>> # 1) Read all samples (image files) in image_folder_dataset_dir with 8 threads
>>> dataset = ds.ImageFolderDataset(dataset_dir=image_folder_dataset_dir,
...                                 num_parallel_workers=8)
>>>
>>> # 2) Read all samples (image files) from folder cat and folder dog with label 0 and 1
>>> dataset = ds.ImageFolderDataset(dataset_dir=image_folder_dataset_dir,
...                                 class_indexing={"cat":0, "dog":1})
>>>
>>> # 3) Read all samples (image files) in image_folder_dataset_dir with extensions .JPEG and .png (case sensitive)
>>> dataset = ds.ImageFolderDataset(dataset_dir=image_folder_dataset_dir,
...                                 extensions=[".JPEG", ".png"])

About ImageFolderDataset:

You can construct the following directory structure from your dataset files and read by MindSpore’s API.

.
└── image_folder_dataset_directory
     ├── class1
     │    ├── 000000000001.jpg
     │    ├── 000000000002.jpg
     │    ├── ...
     ├── class2
     │    ├── 000000000001.jpg
     │    ├── 000000000002.jpg
     │    ├── ...
     ├── class3
     │    ├── 000000000001.jpg
     │    ├── 000000000002.jpg
     │    ├── ...
     ├── classN
     ├── ...
class tinyms.data.ManifestDataset(dataset_file, usage='train', num_samples=None, num_parallel_workers=None, shuffle=None, sampler=None, class_indexing=None, decode=False, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset for reading images from a Manifest file.

The generated dataset has two columns: [image, label]. The tensor of column image is of the uint8 type. The tensor of column label is of a scalar of uint64 type.

参数
  • dataset_file (str) – File to be read.

  • usage (str, optional) – Acceptable usages include train, eval and inference (default=`train`).

  • num_samples (int, optional) – The number of images to be included in the dataset. (default=None, will include all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, will use value set in the config).

  • shuffle (bool, optional) – Whether to perform shuffle on the dataset (default=None, expected order behavior shown in the table).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, expected order behavior shown in the table).

  • class_indexing (dict, optional) – A str-to-int mapping from label name to index (default=None, the folder names will be sorted alphabetically and each class will be given a unique index starting from 0).

  • decode (bool, optional) – decode the images after reading (default=False).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the max number of samples per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_files are not valid or do not exist.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • RuntimeError – If class_indexing is not a dictionary.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • The shape of the image column is [image_size] if decode flag is False, or [H,W,C] otherwise.

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> manifest_dataset_dir = "/path/to/manifest_dataset_file"
>>>
>>> # 1) Read all samples specified in manifest_dataset_dir dataset with 8 threads for training
>>> dataset = ds.ManifestDataset(dataset_file=manifest_dataset_dir, usage="train", num_parallel_workers=8)
>>>
>>> # 2) Read samples (specified in manifest_file.manifest) for shard 0 in a 2-way distributed training setup
>>> dataset = ds.ManifestDataset(dataset_file=manifest_dataset_dir, num_shards=2, shard_id=0)
get_class_indexing()[源代码]

Get the class index.

返回

dict, a str-to-int mapping from label name to index.

实际案例

>>> manifest_dataset_dir = "/path/to/manifest_dataset_file"
>>>
>>> dataset = ds.ManifestDataset(dataset_file=manifest_dataset_dir)
>>> class_indexing = dataset.get_class_indexing()
class tinyms.data.MindDataset(dataset_file, columns_list=None, num_parallel_workers=None, shuffle=None, num_shards=None, shard_id=None, sampler=None, padded_sample=None, num_padded=None, num_samples=None, cache=None)[源代码]

A source dataset for reading and parsing MindRecord dataset.

The columns of generated dataset depend on the source MindRecord files.

参数
  • dataset_file (Union[str, list[str]]) – If dataset_file is a str, it represents for a file name of one component of a mindrecord source, other files with identical source in the same path will be found and loaded automatically. If dataset_file is a list, it represents for a list of dataset files to be read directly.

  • columns_list (list[str], optional) – List of columns to be read (default=None).

  • num_parallel_workers (int, optional) – The number of readers (default=None).

  • shuffle (Union[bool, Shuffle level], optional) –

    Perform reshuffling of the data every epoch (default=None, performs global shuffle). If shuffle is False, no shuffling will be performed; If shuffle is True, the behavior is the same as setting shuffle to be Shuffle.GLOBAL Otherwise, there are three levels of shuffling:

    • Shuffle.GLOBAL: Global shuffle of all rows of data in dataset.

    • Shuffle.FILES: Shuffle the file sequence but keep the order of data within each file.

    • Shuffle.INFILE: Keep the file sequence the same but shuffle the data within each file.

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, ‘num_samples’ reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, sampler is exclusive with shuffle and block_reader). Support list: SubsetRandomSampler, PkSampler, RandomSampler, SequentialSampler, DistributedSampler.

  • padded_sample (dict, optional) – Samples will be appended to dataset, where keys are the same as column_list.

  • num_padded (int, optional) – Number of padding samples. Dataset size plus num_padded should be divisible by num_shards.

  • num_samples (int, optional) – The number of samples to be included in the dataset (default=None, all samples).

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_files are not valid or do not exist.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> mind_dataset_dir = ["/path/to/mind_dataset_file"] # contains 1 or multiple MindRecord files
>>> dataset = ds.MindDataset(dataset_file=mind_dataset_dir)
class tinyms.data.MnistDataset(dataset_dir, usage=None, num_samples=None, num_parallel_workers=None, shuffle=None, sampler=None, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset for reading and parsing the MNIST dataset.

The generated dataset has two columns [image, label]. The tensor of column image is of the uint8 type. The tensor of column label is a scalar of the uint32 type.

参数
  • dataset_dir (str) – Path to the root directory that contains the dataset.

  • usage (str, optional) – Usage of this dataset, can be train, test or all . train will read from 60,000 train samples, test will read from 10,000 test samples, all will read from all 70,000 samples. (default=None, will read all samples)

  • num_samples (int, optional) – The number of images to be included in the dataset (default=None, will read all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, will use value set in the config).

  • shuffle (bool, optional) – Whether or not to perform shuffle on the dataset (default=None, expected order behavior shown in the table).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, expected order behavior shown in the table).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_dir does not contain data files.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> mnist_dataset_dir = "/path/to/mnist_dataset_directory"
>>>
>>> # Read 3 samples from MNIST dataset
>>> dataset = ds.MnistDataset(dataset_dir=mnist_dataset_dir, num_samples=3)
>>>
>>> # Note: In mnist_dataset dataset, each dictionary has keys "image" and "label"

About MNIST dataset:

The MNIST database of handwritten digits has a training set of 60,000 examples, and a test set of 10,000 examples. It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image.

Here is the original MNIST dataset structure. You can unzip the dataset files into this directory structure and read by MindSpore’s API.

.
└── mnist_dataset_dir
     ├── t10k-images-idx3-ubyte
     ├── t10k-labels-idx1-ubyte
     ├── train-images-idx3-ubyte
     └── train-labels-idx1-ubyte

Citation:

@article{lecun2010mnist,
title        = {MNIST handwritten digit database},
author       = {LeCun, Yann and Cortes, Corinna and Burges, CJ},
journal      = {ATT Labs [Online]},
volume       = {2},
year         = {2010},
howpublished = {http://yann.lecun.com/exdb/mnist}
}
class tinyms.data.NumpySlicesDataset(data, column_names=None, num_samples=None, num_parallel_workers=1, shuffle=None, sampler=None, num_shards=None, shard_id=None)[源代码]

Creates a dataset with given data slices, mainly for loading Python data into dataset.

The column names and column types of generated dataset depend on Python data defined by users.

参数
  • data (Union[list, tuple, dict]) – list, tuple, dict and other NumPy formats. Input data will be sliced along the first dimension and generate additional rows, if input is list, there will be one column in each row, otherwise there tends to be multi columns. Large data is not recommended to be loaded in this way as data is loading into memory.

  • column_names (list[str], optional) – List of column names of the dataset (default=None). If column_names is not provided, the output column names will be named as the keys of dict when the input data is a dict, otherwise they will be named like column_0, column_1 …

  • num_samples (int, optional) – The number of samples to be included in the dataset (default=None, all samples).

  • num_parallel_workers (int, optional) – Number of subprocesses used to fetch the dataset in parallel (default=1).

  • shuffle (bool, optional) – Whether or not to perform shuffle on the dataset. Random accessible input is required. (default=None, expected order behavior shown in the table).

  • sampler (Union[Sampler, Iterable], optional) – Object used to choose samples from the dataset. Random accessible input is required (default=None, expected order behavior shown in the table).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). Random accessible input is required. When this argument is specified, num_samples reflects the max sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument must be specified only when num_shards is also specified. Random accessible input is required.

注解

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

引发
  • RuntimeError – If len of column_names does not match output len of data.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

实际案例

>>> # 1) Input data can be a list
>>> data = [1, 2, 3]
>>> dataset = ds.NumpySlicesDataset(data=data, column_names=["column_1"])
>>>
>>> # 2) Input data can be a dictionary, and column_names will be its keys
>>> data = {"a": [1, 2], "b": [3, 4]}
>>> dataset = ds.NumpySlicesDataset(data=data)
>>>
>>> # 3) Input data can be a tuple of lists (or NumPy arrays), each tuple element refers to data in each column
>>> data = ([1, 2], [3, 4], [5, 6])
>>> dataset = ds.NumpySlicesDataset(data=data, column_names=["column_1", "column_2", "column_3"])
>>>
>>> # 4) Load data from CSV file
>>> import pandas as pd
>>> df = pd.read_csv(filepath_or_buffer=csv_dataset_dir[0])
>>> dataset = ds.NumpySlicesDataset(data=dict(df), shuffle=False)
class tinyms.data.PaddedDataset(padded_samples)[源代码]

Creates a dataset with filler data provided by user. Mainly used to add to the original data set and assign it to the corresponding shard.

参数

padded_samples (list(dict)) – Samples provided by user.

引发
  • TypeError – If padded_samples is not an instance of list.

  • TypeError – If the element of padded_samples is not an instance of dict.

  • ValueError – If the padded_samples is empty.

实际案例

>>> import numpy as np
>>> data = [{'image': np.zeros(1, np.uint8)}, {'image': np.zeros(2, np.uint8)}]
>>> dataset = ds.PaddedDataset(padded_samples=data)
class tinyms.data.TextFileDataset(dataset_files, num_samples=None, num_parallel_workers=None, shuffle=<Shuffle.GLOBAL: 'global'>, num_shards=None, shard_id=None, cache=None)[源代码]

A source dataset that reads and parses datasets stored on disk in text format. The generated dataset has one column [text] with type string.

参数
  • dataset_files (Union[str, list[str]]) – String or list of files to be read or glob strings to search for a pattern of files. The list will be sorted in a lexicographical order.

  • num_samples (int, optional) – The number of samples to be included in the dataset (default=None, will include all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the config).

  • shuffle (Union[bool, Shuffle level], optional) –

    Perform reshuffling of the data every epoch (default=Shuffle.GLOBAL). If shuffle is False, no shuffling will be performed; If shuffle is True, the behavior is the same as setting shuffle to be Shuffle.GLOBAL Otherwise, there are two levels of shuffling:

    • Shuffle.GLOBAL: Shuffle both the files and samples.

    • Shuffle.FILES: Shuffle files only.

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_files are not valid or do not exist.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

实际案例

>>> text_file_dataset_dir = ["/path/to/text_file_dataset_file"] # contains 1 or multiple text files
>>> dataset = ds.TextFileDataset(dataset_files=text_file_dataset_dir)
class tinyms.data.TFRecordDataset(dataset_files, schema=None, columns_list=None, num_samples=None, num_parallel_workers=None, shuffle=<Shuffle.GLOBAL: 'global'>, num_shards=None, shard_id=None, shard_equal_rows=False, cache=None)[源代码]

A source dataset for reading and parsing datasets stored on disk in TFData format.

The columns of generated dataset depend on the source TFRecord files.

参数
  • dataset_files (Union[str, list[str]]) – String or list of files to be read or glob strings to search for a pattern of files. The list will be sorted in a lexicographical order.

  • schema (Union[str, Schema], optional) – Path to the JSON schema file or schema object (default=None). If the schema is not provided, the meta data from the TFData file is considered the schema.

  • columns_list (list[str], optional) – List of columns to be read (default=None, read all columns).

  • num_samples (int, optional) – The number of samples (rows) to be included in the dataset (default=None). If num_samples is None and numRows(parsed from schema) does not exist, read the full dataset; If num_samples is None and numRows(parsed from schema) is greater than 0, read numRows rows; If both num_samples and numRows(parsed from schema) are greater than 0, read num_samples rows.

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the config).

  • shuffle (Union[bool, Shuffle level], optional) –

    Perform reshuffling of the data every epoch (default=Shuffle.GLOBAL). If shuffle is False, no shuffling will be performed; If shuffle is True, the behavior is the same as setting shuffle to be Shuffle.GLOBAL Otherwise, there are two levels of shuffling:

    • Shuffle.GLOBAL: Shuffle both the files and samples.

    • Shuffle.FILES: Shuffle files only.

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • shard_equal_rows (bool, optional) – Get equal rows for all shards(default=False). If shard_equal_rows is false, number of rows of each shard may be not equal, and may lead to a failure in distributed training. When the number of samples of per TFRecord file are not equal, it is suggested to set to true. This argument should only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

引发
  • RuntimeError – If dataset_files are not valid or do not exist.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

实际案例

>>> from mindspore import dtype as mstype
>>>
>>> tfrecord_dataset_dir = ["/path/to/tfrecord_dataset_file"] # contains 1 or multiple TFRecord files
>>> tfrecord_schema_file = "/path/to/tfrecord_schema_file"
>>>
>>> # 1) Get all rows from tfrecord_dataset_dir with no explicit schema.
>>> # The meta-data in the first row will be used as a schema.
>>> dataset = ds.TFRecordDataset(dataset_files=tfrecord_dataset_dir)
>>>
>>> # 2) Get all rows from tfrecord_dataset_dir with user-defined schema.
>>> schema = ds.Schema()
>>> schema.add_column(name='col_1d', de_type=mstype.int64, shape=[2])
>>> dataset = ds.TFRecordDataset(dataset_files=tfrecord_dataset_dir, schema=schema)
>>>
>>> # 3) Get all rows from tfrecord_dataset_dir with schema file.
>>> dataset = ds.TFRecordDataset(dataset_files=tfrecord_dataset_dir, schema=tfrecord_schema_file)
class tinyms.data.VOCDataset(dataset_dir, task='Segmentation', usage='train', class_indexing=None, num_samples=None, num_parallel_workers=None, shuffle=None, decode=False, sampler=None, num_shards=None, shard_id=None, cache=None, extra_metadata=False)[源代码]

A source dataset for reading and parsing VOC dataset.

The generated dataset with different task setting has different output columns:

  • task = Detection, output columns: [image, dtype=uint8], [bbox, dtype=float32], [label, dtype=uint32], [difficult, dtype=uint32], [truncate, dtype=uint32].

  • task = Segmentation, output columns: [image, dtype=uint8], [target,dtype=uint8].

参数
  • dataset_dir (str) – Path to the root directory that contains the dataset.

  • task (str, optional) – Set the task type of reading voc data, now only support Segmentation or Detection (default=`Segmentation`).

  • usage (str, optional) – Set the task type of ImageSets(default=`train`). If task is Segmentation, image and annotation list will be loaded in ./ImageSets/Segmentation/usage + “.txt”; If task is Detection, image and annotation list will be loaded in ./ImageSets/Main/usage + “.txt”; if task and usage are not set, image and annotation list will be loaded in ./ImageSets/Segmentation/train.txt as default.

  • class_indexing (dict, optional) – A str-to-int mapping from label name to index, only valid in Detection task (default=None, the folder names will be sorted alphabetically and each class will be given a unique index starting from 0).

  • num_samples (int, optional) – The number of images to be included in the dataset (default=None, all images).

  • num_parallel_workers (int, optional) – Number of workers to read the data (default=None, number set in the config).

  • shuffle (bool, optional) – Whether to perform shuffle on the dataset (default=None, expected order behavior shown in the table).

  • decode (bool, optional) – Decode the images after reading (default=False).

  • sampler (Sampler, optional) – Object used to choose samples from the dataset (default=None, expected order behavior shown in the table).

  • num_shards (int, optional) – Number of shards that the dataset will be divided into (default=None). When this argument is specified, num_samples reflects the maximum sample number of per shard.

  • shard_id (int, optional) – The shard ID within num_shards (default=None). This argument can only be specified when num_shards is also specified.

  • cache (DatasetCache, optional) – Use tensor caching service to speed up dataset processing. (default=None, which means no cache is used).

  • extra_metadata (bool, optional) – Flag to add extra meta-data to row. If True, an additional column named [_meta-filename, dtype=string] will be output at the end (default=False).

引发
  • RuntimeError – If dataset_dir does not contain data files.

  • RuntimeError – If num_parallel_workers exceeds the max thread numbers.

  • RuntimeError – If xml of Annotations is an invalid format.

  • RuntimeError – If xml of Annotations loss attribution of object.

  • RuntimeError – If xml of Annotations loss attribution of bndbox.

  • RuntimeError – If sampler and shuffle are specified at the same time.

  • RuntimeError – If sampler and sharding are specified at the same time.

  • RuntimeError – If num_shards is specified but shard_id is None.

  • RuntimeError – If shard_id is specified but num_shards is None.

  • ValueError – If task is not equal ‘Segmentation’ or ‘Detection’.

  • ValueError – If task equal ‘Segmentation’ but class_indexing is not None.

  • ValueError – If txt related to mode is not exist.

  • ValueError – If shard_id is invalid (< 0 or >= num_shards).

注解

  • Column ‘[_meta-filename, dtype=string]’ won’t be output unless an explicit rename dataset op is added to remove the prefix(‘_meta-‘).

  • This dataset can take in a sampler. sampler and shuffle are mutually exclusive. The table below shows what input arguments are allowed and their expected behavior.

Expected Order Behavior of Using sampler and shuffle

Parameter sampler

Parameter shuffle

Expected Order Behavior

None

None

random order

None

True

random order

None

False

sequential order

Sampler object

None

order defined by sampler

Sampler object

True

not allowed

Sampler object

False

not allowed

实际案例

>>> voc_dataset_dir = "/path/to/voc_dataset_directory"
>>>
>>> # 1) Read VOC data for segmentation training
>>> dataset = ds.VOCDataset(dataset_dir=voc_dataset_dir, task="Segmentation", usage="train")
>>>
>>> # 2) Read VOC data for detection training
>>> dataset = ds.VOCDataset(dataset_dir=voc_dataset_dir, task="Detection", usage="train")
>>>
>>> # 3) Read all VOC dataset samples in voc_dataset_dir with 8 threads in random order
>>> dataset = ds.VOCDataset(dataset_dir=voc_dataset_dir, task="Detection", usage="train",
...                         num_parallel_workers=8)
>>>
>>> # 4) Read then decode all VOC dataset samples in voc_dataset_dir in sequence
>>> dataset = ds.VOCDataset(dataset_dir=voc_dataset_dir, task="Detection", usage="train",
...                         decode=True, shuffle=False)
>>>
>>> # In VOC dataset, if task='Segmentation', each dictionary has keys "image" and "target"
>>> # In VOC dataset, if task='Detection', each dictionary has keys "image" and "annotation"

About VOC dataset.

The PASCAL Visual Object Classes (VOC) challenge is a benchmark in visual object category recognition and detection, providing the vision and machine learning communities with a standard dataset of images and annotation, and standard evaluation procedures.

You can unzip the original VOC-2012 dataset files into this directory structure and read by MindSpore’s API.

.
└── voc2012_dataset_dir
    ├── Annotations
    │    ├── 2007_000027.xml
    │    ├── 2007_000032.xml
    │    ├── ...
    ├── ImageSets
    │    ├── Action
    │    ├── Layout
    │    ├── Main
    │    └── Segmentation
    ├── JPEGImages
    │    ├── 2007_000027.jpg
    │    ├── 2007_000032.jpg
    │    ├── ...
    ├── SegmentationClass
    │    ├── 2007_000032.png
    │    ├── 2007_000033.png
    │    ├── ...
    └── SegmentationObject
         ├── 2007_000032.png
         ├── 2007_000033.png
         ├── ...

Citation:

@article{Everingham10,
author       = {Everingham, M. and Van~Gool, L. and Williams, C. K. I. and Winn, J. and Zisserman, A.},
title        = {The Pascal Visual Object Classes (VOC) Challenge},
journal      = {International Journal of Computer Vision},
volume       = {88},
year         = {2012},
number       = {2},
month        = {jun},
pages        = {303--338},
biburl       = {http://host.robots.ox.ac.uk/pascal/VOC/pubs/everingham10.html#bibtex},
howpublished = {http://host.robots.ox.ac.uk/pascal/VOC/voc2012/index.html}
}
get_class_indexing()[源代码]

Get the class index.

返回

dict, a str-to-int mapping from label name to index.

实际案例

>>> voc_dataset_dir = "/path/to/voc_dataset_directory"
>>>
>>> dataset = ds.VOCDataset(dataset_dir=voc_dataset_dir)
>>> class_indexing = dataset.get_class_indexing()
class tinyms.data.DistributedSampler(dataset_size, num_replicas=None, rank=None, shuffle=True)[源代码]

Distributed sampler.

参数
  • dataset_size (int) – Dataset list length

  • num_replicas (int) – Replicas num.

  • rank (int) – Device rank.

  • shuffle (bool) – Whether the dataset needs to be shuffled. Default: True.

返回

DistributedSampler instance.

class tinyms.data.PKSampler(num_val, num_class=None, shuffle=False, class_column='label', num_samples=None)[源代码]

Samples K elements for each P class in the dataset.

参数
  • num_val (int) – Number of elements to sample for each class.

  • num_class (int, optional) – Number of classes to sample (default=None, sample all classes). The parameter does not supported to specify currently.

  • shuffle (bool, optional) – If True, the class IDs are shuffled, otherwise it will not be shuffled (default=False).

  • class_column (str, optional) – Name of column with class labels for MindDataset (default=’label’).

  • num_samples (int, optional) – The number of samples to draw (default=None, which means sample all elements).

实际案例

>>> # creates a PKSampler that will get 3 samples from every class.
>>> sampler = ds.PKSampler(3)
>>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir,
...                                 num_parallel_workers=8,
...                                 sampler=sampler)
引发
parse()[源代码]

Parse the sampler.

parse_for_minddataset()[源代码]

Parse the sampler for MindRecord.

class tinyms.data.RandomSampler(replacement=False, num_samples=None)[源代码]

Samples the elements randomly.

参数
  • replacement (bool, optional) – If True, put the sample ID back for the next draw (default=False).

  • num_samples (int, optional) – Number of elements to sample (default=None, which means sample all elements).

实际案例

>>> # creates a RandomSampler
>>> sampler = ds.RandomSampler()
>>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir,
...                                 num_parallel_workers=8,
...                                 sampler=sampler)
引发
  • TypeError – If replacement is not a boolean value.

  • TypeError – If num_samples is not an integer value.

  • ValueError – If num_samples is a negative value.

parse()[源代码]

Parse the sampler.

parse_for_minddataset()[源代码]

Parse the sampler for MindRecord.

class tinyms.data.SequentialSampler(start_index=None, num_samples=None)[源代码]

Samples the dataset elements sequentially that is equivalent to not using a sampler.

参数
  • start_index (int, optional) – Index to start sampling at. (default=None, start at first ID)

  • num_samples (int, optional) – Number of elements to sample (default=None, which means sample all elements).

实际案例

>>> # creates a SequentialSampler
>>> sampler = ds.SequentialSampler()
>>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir,
...                                 num_parallel_workers=8,
...                                 sampler=sampler)
引发
  • TypeError – If start_index is not an integer value.

  • TypeError – If num_samples is not an integer value.

  • RuntimeError – If start_index is a negative value.

  • ValueError – If num_samples is a negative value.

parse()[源代码]

Parse the sampler.

parse_for_minddataset()[源代码]

Parse the sampler for MindRecord.

class tinyms.data.SubsetRandomSampler(indices, num_samples=None)[源代码]

Samples the elements randomly from a sequence of indices.

参数
  • indices (Any iterable Python object but string) – A sequence of indices.

  • num_samples (int, optional) – Number of elements to sample (default=None, which means sample all elements).

实际案例

>>> indices = [0, 1, 2, 3, 7, 88, 119]
>>>
>>> # create a SubsetRandomSampler, will sample from the provided indices
>>> sampler = ds.SubsetRandomSampler(indices)
>>> data = ds.ImageFolderDataset(image_folder_dataset_dir, num_parallel_workers=8, sampler=sampler)
引发
  • TypeError – If type of indices element is not a number.

  • TypeError – If num_samples is not an integer value.

  • ValueError – If num_samples is a negative value.

parse()[源代码]

Parse the sampler.

parse_for_minddataset()[源代码]

Parse the sampler for MindRecord.

class tinyms.data.WeightedRandomSampler(weights, num_samples=None, replacement=True)[源代码]

Samples the elements from [0, len(weights) - 1] randomly with the given weights (probabilities).

参数
  • weights (list[float, int]) – A sequence of weights, not necessarily summing up to 1.

  • num_samples (int, optional) – Number of elements to sample (default=None, which means sample all elements).

  • replacement (bool) – If True, put the sample ID back for the next draw (default=True).

实际案例

>>> weights = [0.9, 0.01, 0.4, 0.8, 0.1, 0.1, 0.3]
>>>
>>> # creates a WeightedRandomSampler that will sample 4 elements without replacement
>>> sampler = ds.WeightedRandomSampler(weights, 4)
>>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir,
...                                 num_parallel_workers=8,
...                                 sampler=sampler)
引发
  • TypeError – If type of weights element is not a number.

  • TypeError – If num_samples is not an integer value.

  • TypeError – If replacement is not a boolean value.

  • RuntimeError – If weights is empty or all zero.

  • ValueError – If num_samples is a negative value.

parse()[源代码]

Parse the sampler.

class tinyms.data.SubsetSampler(indices, num_samples=None)[源代码]

Samples the elements from a sequence of indices.

参数
  • indices (Any iterable Python object but string) – A sequence of indices.

  • num_samples (int, optional) – Number of elements to sample (default=None, which means sample all elements).

实际案例

>>> indices = [0, 1, 2, 3, 4, 5]
>>>
>>> # creates a SubsetSampler, will sample from the provided indices
>>> sampler = ds.SubsetSampler(indices)
>>> dataset = ds.ImageFolderDataset(image_folder_dataset_dir,
...                                 num_parallel_workers=8,
...                                 sampler=sampler)
引发
  • TypeError – If type of indices element is not a number.

  • TypeError – If num_samples is not an integer value.

  • ValueError – If num_samples is a negative value.

parse()[源代码]

Parse the sampler.

parse_for_minddataset()[源代码]

Parse the sampler for MindRecord.

class tinyms.data.DatasetCache(session_id, size=0, spilling=False, hostname=None, port=None, num_connections=None, prefetch_size=None)[源代码]

A client to interface with tensor caching service.

For details, please check Tutorial, Programming guide.

参数
  • session_id (int) – A user assigned session id for the current pipeline.

  • size (int, optional) – Size of the memory set aside for the row caching (default=0, which means unlimited, note that it might bring in the risk of running out of memory on the machine).

  • spilling (bool, optional) – Whether or not spilling to disk if out of memory (default=False).

  • hostname (str, optional) – Host name (default=None, use default hostname ‘127.0.0.1’).

  • port (int, optional) – Port to connect to server (default=None, use default port 50052).

  • num_connections (int, optional) – Number of tcp/ip connections (default=None, use default value 12).

  • prefetch_size (int, optional) – The size of the cache queue between operations (default=None, use default value 20).

实际案例

>>> import mindspore.dataset as ds
>>>
>>> # create a cache instance, in which session_id is generated from command line `cache_admin -g`
>>> some_cache = ds.DatasetCache(session_id=session_id, size=0)
>>>
>>> dataset_dir = "path/to/imagefolder_directory"
>>> ds1 = ds.ImageFolderDataset(dataset_dir, cache=some_cache)
get_stat()[源代码]

Get the statistics from a cache.

class tinyms.data.DSCallback(step_size=1)[源代码]

Abstract base class used to build a dataset callback class.

参数

step_size (int, optional) – The number of steps between the step_begin and step_end are called (Default=1).

实际案例

>>> from mindspore.dataset import DSCallback
>>>
>>> class PrintInfo(DSCallback):
...     def ds_epoch_end(self, ds_run_context):
...         print(cb_params.cur_epoch_num)
...         print(cb_params.cur_step_num)
>>>
>>> # dataset is an instance of Dataset object
>>> dataset = dataset.map(operations=op, callbacks=PrintInfo())
create_runtime_obj()[源代码]

Creates a runtime (C++) object from the callback methods defined by the user.

返回

_c_dataengine.PyDSCallback.

ds_begin(ds_run_context)[源代码]

Called before the data pipeline is started.

参数

ds_run_context (RunContext) – Include some information of the pipeline.

ds_epoch_begin(ds_run_context)[源代码]

Called before a new epoch is started.

参数

ds_run_context (RunContext) – Include some information of the pipeline.

ds_epoch_end(ds_run_context)[源代码]

Called after an epoch is finished.

参数

ds_run_context (RunContext) – Include some information of the pipeline.

ds_step_begin(ds_run_context)[源代码]

Called before each step start.

参数

ds_run_context (RunContext) – Include some information of the pipeline.

ds_step_end(ds_run_context)[源代码]

Called after each step finished.

参数

ds_run_context (RunContext) – Include some information of the pipeline.

class tinyms.data.Schema(schema_file=None)[源代码]

Class to represent a schema of a dataset.

参数

schema_file (str) – Path of the schema file (default=None).

返回

Schema object, schema info about dataset.

引发

RuntimeError – If schema file failed to load.

实际案例

>>> from mindspore import dtype as mstype
>>>
>>> # Create schema; specify column name, mindspore.dtype and shape of the column
>>> schema = ds.Schema()
>>> schema.add_column(name='col1', de_type=mstype.int64, shape=[2])
add_column(name, de_type, shape=None)[源代码]

Add new column to the schema.

参数
  • name (str) – The new name of the column.

  • de_type (str) – Data type of the column.

  • shape (list[int], optional) – Shape of the column (default=None, [-1] which is an unknown shape of rank 1).

引发

ValueError – If column type is unknown.

from_json(json_obj)[源代码]

Get schema file from JSON object.

参数

json_obj (dictionary) – Object of JSON parsed.

引发
parse_columns(columns)[源代码]

Parse the columns and add it to self.

参数

columns (Union[dict, list[dict], tuple[dict]]) –

Dataset attribute information, decoded from schema file.

  • list[dict], ‘name’ and ‘type’ must be in keys, ‘shape’ optional.

  • dict, columns.keys() as name, columns.values() is dict, and ‘type’ inside, ‘shape’ optional.

引发

实际案例

>>> schema = Schema()
>>> columns1 = [{'name': 'image', 'type': 'int8', 'shape': [3, 3]},
>>>             {'name': 'label', 'type': 'int8', 'shape': [1]}]
>>> schema.parse_columns(columns1)
>>> columns2 = {'image': {'shape': [3, 3], 'type': 'int8'}, 'label': {'shape': [1], 'type': 'int8'}}
>>> schema.parse_columns(columns2)
to_json()[源代码]

Get a JSON string of the schema.

返回

str, JSON string of the schema.

class tinyms.data.WaitedDSCallback(step_size=1)[源代码]

Abstract base class used to build a dataset callback class that is synchronized with the training callback.

This class can be used to execute a user defined logic right after the previous step or epoch. For example, one augmentation needs the loss from the previous trained epoch to update some of its parameters.

参数

step_size (int, optional) – The number of rows in each step. Usually the step size will be equal to the batch size (Default=1).

实际案例

>>> from mindspore.dataset import WaitedDSCallback
>>>
>>> my_cb = WaitedDSCallback(32)
>>> # dataset is an instance of Dataset object
>>> dataset = dataset.map(operations=AugOp(), callbacks=my_cb)
>>> dataset = dataset.batch(32)
>>> # define the model
>>> model.train(epochs, data, callbacks=[my_cb])
create_runtime_obj()[源代码]

Creates a runtime (C++) object from the callback methods defined by the user. This method is internal.

返回

_c_dataengine.PyDSCallback.

ds_epoch_begin(ds_run_context)[源代码]

Internal method, do not call/override. Defines ds_epoch_begin of DSCallback to wait for MS epoch_end callback.

参数

ds_run_context – Include some information of the pipeline.

ds_step_begin(ds_run_context)[源代码]

Internal method, do not call/override. Defines ds_step_begin of DSCallback to wait for MS step_end callback.

参数

ds_run_context – Include some information of the pipeline.

end(run_context)[源代码]

Internal method, release the wait if training is ended.

参数

run_context – Include some information of the model.

epoch_end(run_context)[源代码]

Internal method, do not call/override. Defines epoch_end of Callback to release the wait in ds_epoch_begin.

参数

run_context – Include some information of the model.

step_end(run_context)[源代码]

Internal method, do not call/override. Defines step_end of Callback to release the wait in ds_step_begin.

参数

run_context – Include some information of the model.

sync_epoch_begin(train_run_context, ds_run_context)[源代码]

Called before a new dataset epoch is started and after the previous training epoch is ended.

参数
  • train_run_context – Include some information of the model with feedback from the previous epoch.

  • ds_run_context – Include some information of the dataset pipeline.

sync_step_begin(train_run_context, ds_run_context)[源代码]

Called before a new dataset step is started and after the previous training step is ended.

参数
  • train_run_context – Include some information of the model with feedback from the previous step.

  • ds_run_context – Include some information of the dataset pipeline.

tinyms.data.compare(pipeline1, pipeline2)[源代码]

Compare if two dataset pipelines are the same.

参数
  • pipeline1 (Dataset) – a dataset pipeline.

  • pipeline2 (Dataset) – a dataset pipeline.

返回

Whether pipeline1 is equal to pipeline2.

实际案例

>>> pipeline1 = ds.MnistDataset(mnist_dataset_dir, 100)
>>> pipeline2 = ds.Cifar10Dataset(cifar_dataset_dir, 100)
>>> ds.compare(pipeline1, pipeline2)
tinyms.data.deserialize(input_dict=None, json_filepath=None)[源代码]

Construct dataset pipeline from a JSON file produced by de.serialize().

注解

Currently Python function deserialization of map operator are not supported.

参数
  • input_dict (dict) – A Python dictionary containing a serialized dataset graph.

  • json_filepath (str) – A path to the JSON file.

返回

de.Dataset or None if error occurs.

引发

OSError – Can not open the JSON file.

实际案例

>>> dataset = ds.MnistDataset(mnist_dataset_dir, 100)
>>> one_hot_encode = c_transforms.OneHot(10)  # num_classes is input argument
>>> dataset = dataset.map(operation=one_hot_encode, input_column_names="label")
>>> dataset = dataset.batch(batch_size=10, drop_remainder=True)
>>> # Use case 1: to/from JSON file
>>> ds.engine.serialize(dataset, json_filepath="/path/to/mnist_dataset_pipeline.json")
>>> dataset = ds.engine.deserialize(json_filepath="/path/to/mnist_dataset_pipeline.json")
>>> # Use case 2: to/from Python dictionary
>>> serialized_data = ds.engine.serialize(dataset)
>>> dataset = ds.engine.deserialize(input_dict=serialized_data)
tinyms.data.serialize(dataset, json_filepath='')[源代码]

Serialize dataset pipeline into a JSON file.

注解

Currently some Python objects are not supported to be serialized. For Python function serialization of map operator, de.serialize will only return its function name.

参数
  • dataset (Dataset) – The starting node.

  • json_filepath (str) – The filepath where a serialized JSON file will be generated.

返回

Dict, The dictionary contains the serialized dataset graph.

引发

OSError – Can not open a file

实际案例

>>> dataset = ds.MnistDataset(mnist_dataset_dir, 100)
>>> one_hot_encode = c_transforms.OneHot(10)  # num_classes is input argument
>>> dataset = dataset.map(operation=one_hot_encode, input_column_names="label")
>>> dataset = dataset.batch(batch_size=10, drop_remainder=True)
>>> # serialize it to JSON file
>>> ds.engine.serialize(dataset, json_filepath="/path/to/mnist_dataset_pipeline.json")
>>> serialized_data = ds.engine.serialize(dataset)  # serialize it to Python dict
tinyms.data.show(dataset, indentation=2)[源代码]

Write the dataset pipeline graph to logger.info file.

参数
  • dataset (Dataset) – The starting node.

  • indentation (int, optional) – The indentation used by the JSON print. Do not indent if indentation is None.

实际案例

>>> dataset = ds.MnistDataset(mnist_dataset_dir, 100)
>>> one_hot_encode = c_transforms.OneHot(10)
>>> dataset = dataset.map(operation=one_hot_encode, input_column_names="label")
>>> dataset = dataset.batch(batch_size=10, drop_remainder=True)
>>> ds.show(dataset)
tinyms.data.zip(datasets)[源代码]

Zip the datasets in the input tuple of datasets.

参数

datasets (tuple of class Dataset) – A tuple of datasets to be zipped together. The number of datasets must be more than 1.

返回

ZipDataset, dataset zipped.

引发

实际案例

>>> # Create a dataset which is the combination of dataset_1 and dataset_2
>>> dataset = ds.zip((dataset_1, dataset_2))
class tinyms.data.FileWriter(file_name, shard_num=1)[源代码]

Class to write user defined raw data into MindRecord files.

注解

After the MindRecord file is generated, if the file name is changed, the file may fail to be read.

参数
  • file_name (str) – File name of MindRecord file.

  • shard_num (int, optional) – The Number of MindRecord file. Default: 1. It should be between [1, 1000].

引发

ParamValueError – If file_name or shard_num is invalid.

实际案例

>>> from mindspore.mindrecord import FileWriter
>>> schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
>>> indexes = ["file_name", "label"]
>>> data = [{"file_name": "1.jpg", "label": 0,
...          "data": b"\x10c\xb3w\xa8\xee$o&<q\x8c\x8e(\xa2\x90\x90\x96\xbc\xb1\x1e\xd4QER\x13?\xff"},
...         {"file_name": "2.jpg", "label": 56,
...          "data": b"\xe6\xda\xd1\xae\x07\xb8>\xd4\x00\xf8\x129\x15\xd9\xf2q\xc0\xa2\x91YFUO\x1dsE1"},
...         {"file_name": "3.jpg", "label": 99,
...          "data": b"\xaf\xafU<\xb8|6\xbd}\xc1\x99[\xeaj+\x8f\x84\xd3\xcc\xa0,i\xbb\xb9-\xcdz\xecp{T\xb1"}]
>>> writer = FileWriter(file_name="test.mindrecord", shard_num=1)
>>> writer.add_schema(schema_json, "test_schema")
0
>>> writer.add_index(indexes)
MSRStatus.SUCCESS
>>> writer.write_raw_data(data)
MSRStatus.SUCCESS
>>> writer.commit()
MSRStatus.SUCCESS
add_index(index_fields)[源代码]

Select index fields from schema to accelerate reading.

注解

The index fields should be primitive type. e.g. int/float/str. If the function is not called, the fields of the primitive type in schema are set as indexes by default.

Please refer to the Examples of class: mindspore.mindrecord.FileWriter.

参数

index_fields (list[str]) – fields from schema.

返回

MSRStatus, SUCCESS or FAILED.

引发
  • ParamTypeError – If index field is invalid.

  • MRMDefineIndexError – If index field is not primitive type.

  • MRMAddIndexError – If failed to add index field.

  • MRMGetMetaError – If the schema is not set or failed to get meta.

add_schema(content, desc=None)[源代码]

The schema is added to describe the raw data to be written.

注解

Please refer to the Examples of class: mindspore.mindrecord.FileWriter.

参数
  • content (dict) – Dictionary of schema content.

  • desc (str, optional) – String of schema description, Default: None.

返回

int, schema id.

引发
  • MRMInvalidSchemaError – If schema is invalid.

  • MRMBuildSchemaError – If failed to build schema.

  • MRMAddSchemaError – If failed to add schema.

commit()[源代码]

Flush data in memory to disk and generate the corresponding database files.

注解

Please refer to the Examples of class: mindspore.mindrecord.FileWriter.

返回

MSRStatus, SUCCESS or FAILED.

引发
  • MRMOpenError – If failed to open MindRecord file.

  • MRMSetHeaderError – If failed to set header.

  • MRMIndexGeneratorError – If failed to create index generator.

  • MRMGenerateIndexError – If failed to write to database.

  • MRMCommitError – If failed to flush data to disk.

open_and_set_header()[源代码]

Open writer and set header. The function is only used for parallel writing and is called before the write_raw_data.

返回

MSRStatus, SUCCESS or FAILED.

引发
  • MRMOpenError – If failed to open MindRecord file.

  • MRMSetHeaderError – If failed to set header.

classmethod open_for_append(file_name)[源代码]

Open MindRecord file and get ready to append data.

参数

file_name (str) – String of MindRecord file name.

返回

FileWriter, file writer object for the opened MindRecord file.

引发
  • ParamValueError – If file_name is invalid.

  • FileNameError – If path contains invalid characters.

  • MRMOpenError – If failed to open MindRecord file.

  • MRMOpenForAppendError – If failed to open file for appending data.

实际案例

>>> from mindspore.mindrecord import FileWriter
>>> schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
>>> data = [{"file_name": "1.jpg", "label": 0,
...          "data": b"\x10c\xb3w\xa8\xee$o&<q\x8c\x8e(\xa2\x90\x90\x96\xbc\xb1\x1e\xd4QER\x13?\xff"}]
>>> writer = FileWriter(file_name="test.mindrecord", shard_num=1)
>>> writer.add_schema(schema_json, "test_schema")
0
>>> writer.write_raw_data(data)
MSRStatus.SUCCESS
>>> writer.commit()
MSRStatus.SUCCESS
>>> write_append = FileWriter.open_for_append("test.mindrecord")
>>> write_append.write_raw_data(data)
MSRStatus.SUCCESS
>>> write_append.commit()
MSRStatus.SUCCESS
set_header_size(header_size)[源代码]

Set the size of header which contains shard information, schema information, page meta information, etc. The larger a header, the more data the MindRecord file can store. If the size of header is larger than the default size (16MB), users need to call the API to set a proper size.

参数

header_size (int) – Size of header, between 16*1024(16KB) and 128*1024*1024(128MB).(default=16MB)

返回

MSRStatus, SUCCESS or FAILED.

引发

MRMInvalidHeaderSizeError – If failed to set header size.

实际案例

>>> from mindspore.mindrecord import FileWriter
>>> writer = FileWriter(file_name="test.mindrecord", shard_num=1)
>>> writer.set_header_size(1 << 25) # 32MB
MSRStatus.SUCCESS
set_page_size(page_size)[源代码]

Set the size of page that represents the area where data is stored, and the areas are divided into two types: raw page and blob page. The larger a page, the more data the page can store. If the size of a sample is larger than the default size (32MB), users need to call the API to set a proper size.

参数

page_size (int) – Size of page, between 32*1024(32KB) and 256*1024*1024(256MB).(default=32MB)

返回

MSRStatus, SUCCESS or FAILED.

引发

MRMInvalidPageSizeError – If failed to set page size.

实际案例

>>> from mindspore.mindrecord import FileWriter
>>> writer = FileWriter(file_name="test.mindrecord", shard_num=1)
>>> writer.set_page_size(1 << 26) # 128MB
MSRStatus.SUCCESS
write_raw_data(raw_data, parallel_writer=False)[源代码]

Convert raw data into a series of consecutive MindRecord files after the raw data is verified against the schema.

注解

Please refer to the Examples of class: mindspore.mindrecord.FileWriter.

参数
  • raw_data (list[dict]) – List of raw data.

  • parallel_writer (bool, optional) – Write raw data in parallel if it equals to True. Default: False.

返回

MSRStatus, SUCCESS or FAILED.

引发
  • ParamTypeError – If index field is invalid.

  • MRMOpenError – If failed to open MindRecord file.

  • MRMValidateDataError – If data does not match blob fields.

  • MRMSetHeaderError – If failed to set header.

  • MRMWriteDatasetError – If failed to write dataset.

class tinyms.data.FileReader(file_name, num_consumer=4, columns=None, operator=None)[源代码]

Class to read MindRecord files.

注解

If file_name is a filename string, it tries to load all MindRecord files generated in a conversion, and throws an exceptions if a MindRecord file is missing. If file_name is a filename list, only the MindRecord files in the list are loaded.

参数
  • file_name (str, list[str]) – One of MindRecord file or a file list.

  • num_consumer (int, optional) – Number of reader workers which load data. Default: 4. It should not be smaller than 1 or larger than the number of processor cores.

  • columns (list[str], optional) – A list of fields where corresponding data would be read. Default: None.

  • operator (int, optional) – Reserved parameter for operators. Default: None.

引发

ParamValueError – If file_name, num_consumer or columns is invalid.

close()[源代码]

Stop reader worker and close File.

get_next()[源代码]

Yield a batch of data according to columns at a time.

生成器

Dict – a batch whose keys are the same as columns.

引发

MRMUnsupportedSchemaError – If schema is invalid.

class tinyms.data.MindPage(file_name, num_consumer=4)[源代码]

Class to read MindRecord files in pagination.

参数
  • file_name (str) – One of MindRecord files or a file list.

  • num_consumer (int, optional) – The number of reader workers which load data. Default: 4. It should not be smaller than 1 or larger than the number of processor cores.

引发
  • ParamValueError – If file_name, num_consumer or columns is invalid.

  • MRMInitSegmentError – If failed to initialize ShardSegment.

property candidate_fields

Return candidate category fields.

返回

list[str], by which data could be grouped.

property category_field

Getter function for category fields.

返回

list[str], by which data could be grouped.

get_category_fields()[源代码]

Return candidate category fields.

返回

list[str], by which data could be grouped.

read_at_page_by_id(category_id, page, num_row)[源代码]

Query by category id in pagination.

参数
  • category_id (int) – Category id, referred to the return of read_category_info.

  • page (int) – Index of page.

  • num_row (int) – Number of rows in a page.

返回

list[dict], data queried by category id.

引发
  • ParamValueError – If any parameter is invalid.

  • MRMFetchDataError – If failed to fetch data by category.

  • MRMUnsupportedSchemaError – If schema is invalid.

read_at_page_by_name(category_name, page, num_row)[源代码]

Query by category name in pagination.

参数
  • category_name (str) – String of category field’s value, referred to the return of read_category_info.

  • page (int) – Index of page.

  • num_row (int) – Number of row in a page.

返回

list[dict], data queried by category name.

read_category_info()[源代码]

Return category information when data is grouped by indicated category field.

返回

str, description of group information.

引发

MRMReadCategoryInfoError – If failed to read category information.

set_category_field(category_field)[源代码]

Set category field for reading.

注解

Should be a candidate category field.

参数

category_field (str) – String of category field name.

返回

MSRStatus, SUCCESS or FAILED.

class tinyms.data.Cifar10ToMR(source, destination)[源代码]

A class to transform from cifar10 to MindRecord.

注解

For details about Examples, please refer to Converting the CIFAR-10 Dataset.

参数
  • source (str) – the cifar10 directory to be transformed.

  • destination (str) – the MindRecord file path to transform into.

引发

ValueError – If source or destination is invalid.

run(fields=None)[源代码]

Execute transformation from cifar10 to MindRecord.

参数

fields (list[str], optional) – A list of index fields. Default: None.

返回

MSRStatus, whether cifar10 is successfully transformed to MindRecord.

transform(fields=None)[源代码]

Encapsulate the run function to exit normally

参数

fields (list[str], optional) – A list of index fields. Default: None.

返回

MSRStatus, whether cifar10 is successfully transformed to MindRecord.

class tinyms.data.Cifar100ToMR(source, destination)[源代码]

A class to transform from cifar100 to MindRecord.

注解

For details about Examples, please refer to Converting the CIFAR-10 Dataset.

参数
  • source (str) – the cifar100 directory to be transformed.

  • destination (str) – the MindRecord file path to transform into.

引发

ValueError – If source or destination is invalid.

run(fields=None)[源代码]

Execute transformation from cifar100 to MindRecord.

参数

fields (list[str]) – A list of index field, e.g.[“fine_label”, “coarse_label”].

返回

MSRStatus, whether cifar100 is successfully transformed to MindRecord.

transform(fields=None)[源代码]

Encapsulate the run function to exit normally

参数

fields (list[str]) – A list of index field, e.g.[“fine_label”, “coarse_label”].

返回

MSRStatus, whether cifar100 is successfully transformed to MindRecord.

class tinyms.data.CsvToMR(source, destination, columns_list=None, partition_number=1)[源代码]

A class to transform from csv to MindRecord.

注解

For details about Examples, please refer to Converting CSV Dataset.

参数
  • source (str) – the file path of csv.

  • destination (str) – the MindRecord file path to transform into.

  • columns_list (list[str], optional) – A list of columns to be read. Default: None.

  • partition_number (int, optional) – partition size, Default: 1.

引发
  • ValueError – If source, destination, partition_number is invalid.

  • RuntimeError – If columns_list is invalid.

run()[源代码]

Execute transformation from csv to MindRecord.

返回

MSRStatus, whether csv is successfully transformed to MindRecord.

transform()[源代码]

Encapsulate the run function to exit normally

class tinyms.data.ImageNetToMR(map_file, image_dir, destination, partition_number=1)[源代码]

A class to transform from imagenet to MindRecord.

注解

For details about Examples, please refer to Converting the ImageNet Dataset.

参数
  • map_file (str) –

    the map file that indicates label. The map file content should be like this:

    n02119789 0
    n02100735 1
    n02110185 2
    n02096294 3
    

  • image_dir (str) – image directory contains n02119789, n02100735, n02110185 and n02096294 directory.

  • destination (str) – the MindRecord file path to transform into.

  • partition_number (int, optional) – partition size. Default: 1.

引发

ValueError – If map_file, image_dir or destination is invalid.

run()[源代码]

Execute transformation from imagenet to MindRecord.

返回

MSRStatus, whether imagenet is successfully transformed to MindRecord.

transform()[源代码]

Encapsulate the run function to exit normally

class tinyms.data.MnistToMR(source, destination, partition_number=1)[源代码]

A class to transform from Mnist to MindRecord.

参数
  • source (str) – directory that contains t10k-images-idx3-ubyte.gz, train-images-idx3-ubyte.gz, t10k-labels-idx1-ubyte.gz and train-labels-idx1-ubyte.gz.

  • destination (str) – the MindRecord file directory to transform into.

  • partition_number (int, optional) – partition size. Default: 1.

引发

ValueError – If source, destination, partition_number is invalid.

run()[源代码]

Execute transformation from Mnist to MindRecord.

返回

MSRStatus, whether successfully written into MindRecord.

transform()[源代码]

Encapsulate the run function to exit normally

class tinyms.data.TFRecordToMR(source, destination, feature_dict, bytes_fields=None)[源代码]

A class to transform from TFRecord to MindRecord.

注解

For details about Examples, please refer to Converting TFRecord Dataset.

参数
  • source (str) – the TFRecord file to be transformed.

  • destination (str) – the MindRecord file path to transform into.

  • feature_dict (dict) – a dictionary that states the feature type, and VarLenFeature is not supported.

  • bytes_fields (list, optional) – the bytes fields which are in feature_dict and can be images bytes.

引发
  • ValueError – If parameter is invalid.

  • Exception – when tensorflow module is not found or version is not correct.

run()[源代码]

Execute transformation from TFRecord to MindRecord.

返回

MSRStatus, whether TFRecord is successfully transformed to MindRecord.

tfrecord_iterator()[源代码]

Yield a dictionary whose keys are fields in schema.

生成器

dict, data dictionary whose keys are the same as columns.

tfrecord_iterator_oldversion()[源代码]

Yield a dict with key to be fields in schema, and value to be data. This function is for old version tensorflow whose version number < 2.1.0

生成器

dict, data dictionary whose keys are the same as columns.

transform()[源代码]

Encapsulate the run function to exit normally

tinyms.data.download_dataset(dataset_name, local_path='.')[源代码]

This function is defined to easily download any public dataset without specifing much details.

参数
  • dataset_name (str) – The official name of dataset, currently supports mnist, cifar10 and cifar100.

  • local_path (str) – Specifies the local location of dataset to be downloaded. Default: ..

返回

str, the source location of dataset downloaded.

实际案例

>>> from tinyms.data import download_dataset
>>>
>>> ds_path = download_dataset('mnist')
tinyms.data.generate_image_list(dir_path, max_dataset_size=inf)[源代码]

Traverse the directory to generate a list of images path.

参数
  • dir_path (str) – image directory.

  • max_dataset_size (int) – Maximum number of return image paths.

返回

Image path list.

tinyms.data.load_resized_img(path, width=256, height=256)[源代码]

Load image with RGB and resize to (256, 256).

参数
  • path (str) – image path.

  • width (int) – image width, default: 256.

  • height (int) – image height, default: 256.

返回

PIL image class.

tinyms.data.load_img(path)[源代码]

Load image with RGB.

参数

path (str) – image path.

返回

PIL image class.

tinyms.vision

This module is to support vision augmentations. transforms is a high performance image augmentation module which is developed with C++ OpenCV.

class tinyms.vision.ImageViewer(image, label=None)[源代码]

ImageViewer is a class defined for visualizing the input image.

参数
  • image (Union[PIL.Image, numpy.ndarray]) – image input.

  • label (str, optional) – specifies the label of this image. Default: None.

引发

TypeError – When image input is not numpy.ndarray or PIL.Image.

draw(pred_res, labels)[源代码]

Draw the predicting boxes on the picture and show the visualized picture.

参数
  • pred_res (dict) – The predcition result from tinyms.serving.predict method.

  • labels (list) – The labels should be input manually with a list of string. This argument is required for distinguishing the color from different classes.

注解

This function is only valid when being called in interactive environment, such like Jupyter notebook.

实际案例

>>> form PIL import Image
>>>
>>> img = Image.open('example.jpg')
>>> img_viewer = ImageViewer(img)
>>> labels = ['1', '2', '3']
>>> img_viewer.draw(pred_res, labels)
show()[源代码]

Directly show the visualized picture.

注解

This function is only valid when being called in interactive environment, such like Jupyter notebook.

实际案例

>>> form PIL import Image
>>>
>>> img = Image.open('example.jpg')
>>> img_viewer = ImageViewer(img, label='cat')
>>> img_viewer.show()
class tinyms.vision.Inter[源代码]

Interpolation Modes.

Possible enumeration values are: Inter.NEAREST, Inter.ANTIALIAS, Inter.LINEAR, Inter.BILINEAR, Inter.CUBIC, Inter.BICUBIC, Inter.AREA, Inter.PILCUBIC.

  • Inter.NEAREST: means interpolation method is nearest-neighbor interpolation.

  • Inter.ANTIALIAS: means the interpolation method is antialias interpolation.

  • Inter.LINEAR: means interpolation method is bilinear interpolation, here is the same as Inter.BILINEAR.

  • Inter.BILINEAR: means interpolation method is bilinear interpolation.

  • Inter.CUBIC: means the interpolation method is bicubic interpolation, here is the same as Inter.BICUBIC.

  • Inter.BICUBIC: means the interpolation method is bicubic interpolation.

  • Inter.AREA: means interpolation method is pixel area interpolation.

  • Inter.PILCUBIC: means interpolation method is bicubic interpolation like implemented in pillow, input should be in 3 channels format.

class tinyms.vision.Border[源代码]

Padding Mode, Border Type.

Possible enumeration values are: Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC.

  • Border.CONSTANT: means it fills the border with constant values.

  • Border.EDGE: means it pads with the last value on the edge.

  • Border.REFLECT: means it reflects the values on the edge omitting the last value of edge.

  • Border.SYMMETRIC: means it reflects the values on the edge repeating the last value of edge.

Note: This class derived from class str to support json serializable.

class tinyms.vision.ImageBatchFormat[源代码]

Data Format of images after batch operation.

Possible enumeration values are: ImageBatchFormat.NHWC, ImageBatchFormat.NCHW.

  • ImageBatchFormat.NHWC: in orders like, batch N, height H, width W, channels C to store the data.

  • ImageBatchFormat.NCHW: in orders like, batch N, channels C, height H, width W to store the data.

tinyms.vision.ssd_bboxes_encode(boxes)[源代码]

Labels anchors with ground truth inputs.

参数

boxes (numpy.ndarray) – Ground truth with shape [N, 5], for each row, it stores [ymin, xmin, ymax, xmax, cls].

返回

numpy.ndarray, location ground truth with shape [num_anchors, 4]. numpy.ndarray, class ground truth with shape [num_anchors, 1]. numpy.ndarray, number of positives in an image.

tinyms.vision.ssd_bboxes_filter(boxes, box_scores, image_shape)[源代码]

Filter predict boxes with minimum score and nms threshold.

参数
  • boxes (numpy.ndarray) – Ground truth with shape [N, 4], for each row, it stores [ymin, xmin, ymax, xmax].

  • box_scores (numpy.ndarray) – Class scores with shape [N, 21].

  • image_shape (tuple) – Shape of original image with the format [h, w].

返回

list[list[float]], ground truth with shape [N, 4], for each row, it stores [ymin, xmin, ymax, xmax]. list[list[float]], class scores with shape [N, 21]. list[list[int]], class label with shape [N, 21].

tinyms.vision.coco_eval(pred_data, anno_file)[源代码]

Calculate mAP of predicted bboxes.

class tinyms.vision.MnistTransform(configs=None)[源代码]

Mnist dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in Mnist-style.

Outputs:

numpy.ndarray, transformed image.

实际案例

>>> from PIL import Image
>>> from tinyms.vision import MnistTransform
>>>
>>> mnist_transform = MnistTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = mnist_transform(img)
apply_ds(mnist_ds, repeat_size=1, batch_size=32, num_parallel_workers=None)[源代码]

Apply preprocess operation on MnistDataset instance.

参数
  • mnist_ds (data.MnistDataset) – MnistDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

返回

data.MnistDataset, the preprocessed MnistDataset instance.

实际案例

>>> from tinyms.vision import MnistTransform
>>>
>>> mnist_transform = MnistTransform()
>>> mnist_ds = mnist_transform.apply_ds(mnist_ds)
postprocess(input, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

参数
  • input (numpy.ndarray) – Prediction result.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

返回

str, the postprocess result.

class tinyms.vision.Cifar10Transform(configs=None)[源代码]

Cifar10 dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in Cifar10-style.

Outputs:

numpy.ndarray, Transformed image.

实际案例

>>> from PIL import Image
>>> from tinyms.vision import Cifar10Transform
>>>
>>> cifar10_transform = Cifar10Transform()
>>> img = Image.open('object_detection.jpg')
>>> img = cifar10_transform(img)

“””

apply_ds(cifar10_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)[源代码]

Apply preprocess operation on Cifar10Dataset instance.

参数
  • cifar10_ds (data.Cifar10Dataset) – Cifar10Dataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

返回

data.Cifar10Dataset, the preprocessed Cifar10Dataset instance.

实际案例

>>> from tinyms.vision import Cifar10Transform
>>>
>>> cifar10_transform = Cifar10Transform()
>>> cifar10_ds = cifar10_transform.apply_ds(cifar10_ds)
postprocess(input, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

参数
  • input (numpy.ndarray) – Prediction result.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

返回

str, the postprocess result.

class tinyms.vision.ImageFolderTransform(configs=None)[源代码]

ImageFolder dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in ImageFolder-style.

Outputs:

numpy.ndarray, transformed image.

实际案例

>>> from PIL import Image
>>> from tinyms.vision import ImageFolderTransform
>>>
>>> imagefolder_transform = ImageFolderTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = imagefolder_transform(img)
apply_ds(imagefolder_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)[源代码]

Apply preprocess operation on ImageFolderDataset instance.

参数
  • cifar10_ds (data.ImageFolderDataset) – ImageFolderDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

返回

data.ImageFolderDataset, the preprocessed ImageFolderDataset instance.

实际案例

>>> from tinyms.vision import ImageFolderTransform
>>>
>>> imagefolder_transform = ImageFolderTransform()
>>> imagefolder_ds = imagefolder_transform.apply_ds(imagefolder_ds)
postprocess(input, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

参数
  • input (numpy.ndarray) – Prediction result.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

返回

str, the postprocess result.

class tinyms.vision.VOCTransform(configs=None)[源代码]

VOC dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in VOC-style.

Outputs:

numpy.ndarray, transformed image.

实际案例

>>> from PIL import Image
>>> from tinyms.vision import VOCTransform
>>>
>>> voc_transform = VOCTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = voc_transform(img)
apply_ds(voc_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)[源代码]

Apply preprocess operation on VOCDataset instance.

参数
  • voc_ds (data.VOCDataset) – VOCDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

返回

data.VOCDataset, the preprocessed VOCDataset instance.

实际案例

>>> from tinyms.vision import VOCTransform
>>>
>>> VOC_transform = VOCTransform()
>>> voc_ds = voc_transform.apply_ds(voc_ds)
postprocess(input, image_shape, strategy='TOP1_CLASS')[源代码]

Apply postprocess operation for prediction result.

参数
  • input (numpy.ndarray) – Prediction result.

  • image_shape (tuple) – Image shape.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

返回

dict, the postprocess result.

class tinyms.vision.ShanshuiTransform(configs=None)[源代码]

Shanshui dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in VOC-style.

Outputs:

numpy.ndarray, transformed image.

实际案例

>>> from PIL import Image
>>> from tinyms.vision import ShanshuiTransform
>>>
>>> shanshui_transform = ShanshuiTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = shanshui_transform(img)
apply_ds(voc_ds, repeat_size=1, batch_size=32, num_parallel_workers=None, is_training=True)

Apply preprocess operation on VOCDataset instance.

参数
  • voc_ds (data.VOCDataset) – VOCDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: None.

  • is_training (bool) – Specifies if is in training step. Default: True.

返回

data.VOCDataset, the preprocessed VOCDataset instance.

实际案例

>>> from tinyms.vision import VOCTransform
>>>
>>> VOC_transform = VOCTransform()
>>> voc_ds = voc_transform.apply_ds(voc_ds)
postprocess(input, image_shape, strategy='TOP1_CLASS')

Apply postprocess operation for prediction result.

参数
  • input (numpy.ndarray) – Prediction result.

  • image_shape (tuple) – Image shape.

  • strategy (str) – Specifies the postprocess strategy. Default: TOP1_CLASS.

返回

dict, the postprocess result.

class tinyms.vision.CycleGanDatasetTransform(configs=None)[源代码]

CycleGan dataset transform class.

Inputs:

img (Union[numpy.ndarray, PIL.Image]): Image to be transformed in city_scape.

Outputs:

numpy.ndarray, transformed image.

实际案例

>>> from PIL import Image
>>> from tinyms.vision import CycleGanDatasetTransform
>>>
>>> cyclegan_transform = CycleGanDatasetTransform()
>>> img = Image.open('object_detection.jpg')
>>> img = cyclegan_transform(img)
apply_ds(gan_generator_ds, repeat_size=1, batch_size=1, num_parallel_workers=1, shuffle=True, phase='train')[源代码]

Apply preprocess operation on GeneratorDataset instance.

参数
  • gan_generator_ds (data.GeneratorDataset) – GeneratorDataset instance.

  • repeat_size (int) – The repeat size of dataset. Default: 1.

  • batch_size (int) – Batch size. Default: 32.

  • num_parallel_workers (int) – The number of concurrent workers. Default: 1.

  • shuffle (bool) – Specifies if applying shuffle operation. Default: True.

  • phase (str) – Specifies the current phase. Default: train.

返回

data.GeneratorDataset, the preprocessed GeneratorDataset instance.

实际案例

>>> from tinyms.vision import CycleGanDatasetTransform
>>>
>>> cyclegan_transform = CycleGanDatasetTransform()
>>> gan_generator_ds = cyclegan_transform.apply_ds(gan_generator_ds)
引发

TypeError – If gan_generator_ds is not instance of GeneratorDataset.

class tinyms.vision.AutoContrast(cutoff=0.0, ignore=None)[源代码]

Apply automatic contrast on input image. This operator calculates histogram of image, reassign cutoff percent of lightest pixels from histogram to 255, and reassign cutoff percent of darkest pixels from histogram to 0.

参数
  • cutoff (float, optional) – Percent of lightest and darkest pixels to cut off from the histogram of input image. the value must be in the range [0.0, 50.0) (default=0.0).

  • ignore (Union[int, sequence], optional) – The background pixel values to ignore (default=None).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.AutoContrast(cutoff=10.0, ignore=[10, 20])]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.BoundingBoxAugment(transform, ratio=0.3)[源代码]

Apply a given image transform on a random selection of bounding box regions of a given image.

参数
  • transform – C++ transformation operator to be applied on random selection of bounding box regions of a given image.

  • ratio (float, optional) – Ratio of bounding boxes to apply augmentation on. Range: [0, 1] (default=0.3).

实际案例

>>> # set bounding box operation with ratio of 1 to apply rotation on all bounding boxes
>>> bbox_aug_op = c_vision.BoundingBoxAugment(c_vision.RandomRotation(90), 1)
>>> # map to apply ops
>>> image_folder_dataset = image_folder_dataset.map(operations=[bbox_aug_op],
...                                                 input_columns=["image", "bbox"],
...                                                 output_columns=["image", "bbox"],
...                                                 column_order=["image", "bbox"])
class tinyms.vision.CenterCrop(size)[源代码]

Crop the input image at the center to the given size. If input image size is smaller than output size, input image will be padded with 0 before cropping.

参数

size (Union[int, sequence]) – The output size of the cropped image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

实际案例

>>> # crop image to a square
>>> transforms_list1 = [c_vision.Decode(), c_vision.CenterCrop(50)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # crop image to portrait style
>>> transforms_list2 = [c_vision.Decode(), c_vision.CenterCrop((60, 40))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.CutMixBatch(image_batch_format, alpha=1.0, prob=1.0)[源代码]

Apply CutMix transformation on input batch of images and labels. Note that you need to make labels into one-hot format and batched before calling this operator.

参数
  • image_batch_format (Image Batch Format) – The method of padding. Can be any of [ImageBatchFormat.NHWC, ImageBatchFormat.NCHW].

  • alpha (float, optional) – hyperparameter of beta distribution (default = 1.0).

  • prob (float, optional) – The probability by which CutMix is applied to each image (default = 1.0).

实际案例

>>> from mindspore.dataset.vision import ImageBatchFormat
>>> onehot_op = c_transforms.OneHot(num_classes=10)
>>> image_folder_dataset= image_folder_dataset.map(operations=onehot_op,
...                                                input_columns=["label"])
>>> cutmix_batch_op = c_vision.CutMixBatch(ImageBatchFormat.NHWC, 1.0, 0.5)
>>> image_folder_dataset = image_folder_dataset.batch(5)
>>> image_folder_dataset = image_folder_dataset.map(operations=cutmix_batch_op,
...                                                 input_columns=["image", "label"])
class tinyms.vision.CutOut(length, num_patches=1)[源代码]

Randomly cut (mask) out a given number of square patches from the input image array.

参数
  • length (int) – The side length of each square patch.

  • num_patches (int, optional) – Number of patches to be cut out of an image (default=1).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.CutOut(80, num_patches=10)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Decode(rgb=True)[源代码]

Decode the input image in RGB mode(default) or BGR mode(deprecated).

参数

rgb (bool, optional) – Mode of decoding input image (default=True). If True means format of decoded image is RGB else BGR(deprecated).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlip()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Equalize[源代码]

Apply histogram equalization on input image.

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.Equalize()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Grayscale(num_output_channels=1)[源代码]

Convert the input PIL Image to grayscale.

参数

num_output_channels (int) – Number of channels of the output grayscale image, which can be 1 or 3 (default=1). If num_output_channels is 3, the returned image will have 3 identical RGB channels.

实际案例

>>> from mindspore.dataset.transforms.py_transforms import Compose
>>> transforms_list = Compose([py_vision.Decode(),
...                            py_vision.Grayscale(3),
...                            py_vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns="image")
classmethod from_json(json_string)

Base from_json for Python tensor operations class

to_json()

Base to_json for Python tensor operations class

class tinyms.vision.HWC2CHW[源代码]

Transpose the input image from shape (H, W, C) to shape (C, H, W). The input image should be 3 channels image.

实际案例

>>> transforms_list = [c_vision.Decode(),
...                    c_vision.RandomHorizontalFlip(0.75),
...                    c_vision.RandomCrop(512),
...                    c_vision.HWC2CHW()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Invert[源代码]

Apply invert on input image in RGB mode. This operator will reassign every pixel to (255 - pixel).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.Invert()]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.MixUpBatch(alpha=1.0)[源代码]

Apply MixUp transformation on input batch of images and labels. Each image is multiplied by a random weight (lambda) and then added to a randomly selected image from the batch multiplied by (1 - lambda). The same formula is also applied to the one-hot labels. The lambda is generated based on the specified alpha value. Two coefficients x1, x2 are randomly generated in the range [alpha, 1], and lambda = (x1 / (x1 + x2)). Note that you need to make labels into one-hot format and batched before calling this operator.

参数

alpha (float, optional) – Hyperparameter of beta distribution (default = 1.0).

实际案例

>>> onehot_op = c_transforms.OneHot(num_classes=10)
>>> image_folder_dataset= image_folder_dataset.map(operations=onehot_op,
...                                                input_columns=["label"])
>>> mixup_batch_op = c_vision.MixUpBatch(alpha=0.9)
>>> image_folder_dataset = image_folder_dataset.batch(5)
>>> image_folder_dataset = image_folder_dataset.map(operations=mixup_batch_op,
...                                                 input_columns=["image", "label"])
class tinyms.vision.Normalize(mean, std)[源代码]

Normalize the input image with respect to mean and standard deviation. This operator will normalize the input image with: output[channel] = (input[channel] - mean[channel]) / std[channel], where channel >= 1.

参数
  • mean (sequence) – List or tuple of mean values for each channel, with respect to channel order. The mean values must be in range [0.0, 255.0].

  • std (sequence) – List or tuple of standard deviations for each channel, with respect to channel order. The standard deviation values must be in range (0.0, 255.0].

实际案例

>>> decode_op = c_vision.Decode()
>>> normalize_op = c_vision.Normalize(mean=[121.0, 115.0, 100.0], std=[70.0, 68.0, 71.0])
>>> transforms_list = [decode_op, normalize_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Pad(padding, fill_value=0, padding_mode=<Border.CONSTANT: 'constant'>)[源代码]

Pad the image according to padding parameters.

参数
  • padding (Union[int, sequence]) – The number of pixels to pad the image. If a single number is provided, it pads all borders with this value. If a tuple or lists of 2 values are provided, it pads the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, it pads the left, top, right and bottom respectively.

  • fill_value (Union[int, tuple], optional) – The pixel intensity of the borders, only valid for padding_mode Border.CONSTANT. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255] (default=0).

  • padding_mode (Border mode, optional) –

    The method of padding (default=Border.CONSTANT). Can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.Pad([100, 100, 100, 100])]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
tinyms.vision.PILRandomHorizontalFlip

mindspore.dataset.vision.py_transforms.RandomHorizontalFlip 的别名

class tinyms.vision.RandomAffine(degrees, translate=None, scale=None, shear=None, resample=<Inter.NEAREST: 0>, fill_value=0)[源代码]

Apply Random affine transformation to the input image.

参数
  • degrees (int or float or sequence) – Range of the rotation degrees. If degrees is a number, the range will be (-degrees, degrees). If degrees is a sequence, it should be (min, max).

  • translate (sequence, optional) – Sequence (tx_min, tx_max, ty_min, ty_max) of minimum/maximum translation in x(horizontal) and y(vertical) directions (default=None). The horizontal and vertical shift is selected randomly from the range: (tx_min*width, tx_max*width) and (ty_min*height, ty_max*height), respectively. If a tuple or list of size 2, then a translate parallel to the X axis in the range of (translate[0], translate[1]) is applied. If a tuple of list of size 4, then a translate parallel to the X axis in the range of (translate[0], translate[1]) and a translate parallel to the Y axis in the range of (translate[2], translate[3]) are applied. If None, no translation is applied.

  • scale (sequence, optional) – Scaling factor interval (default=None, original scale is used).

  • shear (int or float or sequence, optional) – Range of shear factor (default=None). If a number, then a shear parallel to the X axis in the range of (-shear, +shear) is applied. If a tuple or list of size 2, then a shear parallel to the X axis in the range of (shear[0], shear[1]) is applied. If a tuple of list of size 4, then a shear parallel to X axis in the range of (shear[0], shear[1]) and a shear parallel to Y axis in the range of (shear[2], shear[3]) is applied. If None, no shear is applied.

  • resample (Inter mode, optional) –

    An optional resampling filter (default=Inter.NEAREST). It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.BILINEAR, means resample method is bilinear interpolation.

    • Inter.NEAREST, means resample method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means resample method is bicubic interpolation.

  • fill_value (tuple or int, optional) – Optional fill_value to fill the area outside the transform in the output image. There must be three elements in tuple and the value of single element is [0, 255]. (default=0, filling is performed).

引发
  • ValueError – If degrees is negative.

  • ValueError – If translation value is not between -1 and 1.

  • ValueError – If scale is not positive.

  • ValueError – If shear is a number but is not positive.

  • TypeError – If degrees is not a number or a list or a tuple. If degrees is a list or tuple, its length is not 2.

  • TypeError – If translate is specified but is not list or a tuple of length 2 or 4.

  • TypeError – If scale is not a list or tuple of length 2.

  • TypeError – If shear is not a list or tuple of length 2 or 4.

  • TypeError – If fill_value is not a single integer or a 3-tuple.

实际案例

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> random_affine_op = c_vision.RandomAffine(degrees=15,
...                                          translate=(-0.1, 0.1, 0, 0),
...                                          scale=(0.9, 1.1),
...                                          resample=Inter.NEAREST)
>>> transforms_list = [decode_op, random_affine_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomColor(degrees=(0.1, 1.9))[源代码]

Adjust the color of the input image by a fixed or random degree. This operation works only with 3-channel color images.

参数

degrees (sequence, optional) – Range of random color adjustment degrees. It should be in (min, max) format. If min=max, then it is a single fixed magnitude operation (default=(0.1, 1.9)).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomColor((0.5, 2.0))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomColorAdjust(brightness=(1, 1), contrast=(1, 1), saturation=(1, 1), hue=(0, 0))[源代码]

Randomly adjust the brightness, contrast, saturation, and hue of the input image.

参数
  • brightness (Union[float, list, tuple], optional) – Brightness adjustment factor (default=(1, 1)). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness), 1+brightness]. If it is a sequence, it should be [min, max] for the range.

  • contrast (Union[float, list, tuple], optional) – Contrast adjustment factor (default=(1, 1)). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast), 1+contrast]. If it is a sequence, it should be [min, max] for the range.

  • saturation (Union[float, list, tuple], optional) – Saturation adjustment factor (default=(1, 1)). Cannot be negative. If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation), 1+saturation]. If it is a sequence, it should be [min, max] for the range.

  • hue (Union[float, list, tuple], optional) – Hue adjustment factor (default=(0, 0)). If it is a float, the range will be [-hue, hue]. Value should be 0 <= hue <= 0.5. If it is a sequence, it should be [min, max] where -0.5 <= min <= max <= 0.5.

实际案例

>>> decode_op = c_vision.Decode()
>>> transform_op = c_vision.RandomColorAdjust(brightness=(0.5, 1),
...                                           contrast=(0.4, 1),
...                                           saturation=(0.3, 1))
>>> transforms_list = [decode_op, transform_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomCrop(size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=<Border.CONSTANT: 'constant'>)[源代码]

Crop the input image at a random location. If input image size is smaller than output size, input image will be padded before cropping.

注解

If the input image is more than one, then make sure that the image size is the same.

参数
  • size (Union[int, sequence]) – The output size of the cropped image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • padding (Union[int, sequence], optional) – The number of pixels to pad the image (default=None). If padding is not None, pad image first with padding values. If a single number is provided, pad all borders with this value. If a tuple or lists of 2 values are provided, pad the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, pad the left, top, right and bottom respectively.

  • pad_if_needed (bool, optional) – Pad the image if either side is smaller than the given output size (default=False).

  • fill_value (Union[int, tuple], optional) – The pixel intensity of the borders, only valid for padding_mode Border.CONSTANT. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255] (default=0).

  • padding_mode (Border mode, optional) –

    The method of padding (default=Border.CONSTANT). It can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

实际案例

>>> from mindspore.dataset.vision import Border
>>> decode_op = c_vision.Decode()
>>> random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=Border.EDGE)
>>> transforms_list = [decode_op, random_crop_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomCropDecodeResize(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=<Inter.BILINEAR: 2>, max_attempts=10)[源代码]

A combination of Crop, Decode and Resize. It will get better performance for JPEG images. This operator will crop the input image at a random location, decode the cropped image in RGB mode, and resize the decoded image.

参数
  • size (Union[int, sequence]) – The output size of the resized image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • scale (list, tuple, optional) – Range [min, max) of respective size of the original size to be cropped (default=(0.08, 1.0)).

  • ratio (list, tuple, optional) – Range [min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)).

  • interpolation (Inter mode, optional) –

    Image interpolation mode for resize operator(default=Inter.BILINEAR). It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area (default=10). If exceeded, fall back to use center_crop instead.

实际案例

>>> from mindspore.dataset.vision import Inter
>>> resize_crop_decode_op = c_vision.RandomCropDecodeResize(size=(50, 75),
...                                                         scale=(0.25, 0.5),
...                                                         interpolation=Inter.NEAREST,
...                                                         max_attempts=5)
>>> transforms_list = [resize_crop_decode_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomCropWithBBox(size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=<Border.CONSTANT: 'constant'>)[源代码]

Crop the input image at a random location and adjust bounding boxes accordingly.

参数
  • size (Union[int, sequence]) – The output size of the cropped image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • padding (Union[int, sequence], optional) – The number of pixels to pad the image (default=None). If padding is not None, first pad image with padding values. If a single number is provided, pad all borders with this value. If a tuple or lists of 2 values are provided, pad the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, pad the left, top, right and bottom respectively.

  • pad_if_needed (bool, optional) – Pad the image if either side is smaller than the given output size (default=False).

  • fill_value (Union[int, tuple], optional) – The pixel intensity of the borders, only valid for padding_mode Border.CONSTANT. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255] (default=0).

  • padding_mode (Border mode, optional) –

    The method of padding (default=Border.CONSTANT). It can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

实际案例

>>> decode_op = c_vision.Decode()
>>> random_crop_with_bbox_op = c_vision.RandomCropWithBBox([512, 512], [200, 200, 200, 200])
>>> transforms_list = [decode_op, random_crop_with_bbox_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomHorizontalFlip(prob=0.5)[源代码]

Randomly flip the input image horizontally with a given probability.

参数

prob (float, optional) – Probability of the image being flipped (default=0.5).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlip(0.75)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomHorizontalFlipWithBBox(prob=0.5)[源代码]

Flip the input image horizontally randomly with a given probability and adjust bounding boxes accordingly.

参数

prob (float, optional) – Probability of the image being flipped (default=0.5).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomHorizontalFlipWithBBox(0.70)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomPosterize(bits=(8, 8))[源代码]

Reduce the number of bits for each color channel to posterize the input image randomly with a given probability.

参数

bits (sequence or int, optional) – Range of random posterize to compress image. Bits values must be in range of [1,8], and include at least one integer value in the given range. It must be in (min, max) or integer format. If min=max, then it is a single fixed magnitude operation (default=(8, 8)).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomPosterize((6, 8))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomResize(size)[源代码]

Resize the input image using a randomly selected interpolation mode.

参数

size (Union[int, sequence]) – The output size of the resized image. If size is an integer, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

实际案例

>>> # randomly resize image, keeping aspect ratio
>>> transforms_list1 = [c_vision.Decode(), c_vision.RandomResize(50)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # randomly resize image to landscape style
>>> transforms_list2 = [c_vision.Decode(), c_vision.RandomResize((40, 60))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=<Inter.BILINEAR: 2>, max_attempts=10)[源代码]

Crop the input image to a random size and aspect ratio. This operator will crop the input image randomly, and resize the cropped image using a selected interpolation mode.

注解

If the input image is more than one, then make sure that the image size is the same.

参数
  • size (Union[int, sequence]) – The output size of the resized image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • scale (list, tuple, optional) – Range [min, max) of respective size of the original size to be cropped (default=(0.08, 1.0)).

  • ratio (list, tuple, optional) – Range [min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)).

  • interpolation (Inter mode, optional) –

    Image interpolation mode for resize operator (default=Inter.BILINEAR). It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC, Inter.PILCUBIC].

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

    • Inter.AREA, means interpolation method is pixel area interpolation.

    • Inter.PILCUBIC, means interpolation method is bicubic interpolation like implemented in pillow, input should be in 3 channels format.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area (default=10). If exceeded, fall back to use center_crop instead.

实际案例

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> resize_crop_op = c_vision.RandomResizedCrop(size=(50, 75), scale=(0.25, 0.5),
...                                             interpolation=Inter.BILINEAR)
>>> transforms_list = [decode_op, resize_crop_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomResizedCropWithBBox(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=<Inter.BILINEAR: 2>, max_attempts=10)[源代码]

Crop the input image to a random size and aspect ratio and adjust bounding boxes accordingly.

参数
  • size (Union[int, sequence]) – The size of the output image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • scale (list, tuple, optional) – Range (min, max) of respective size of the original size to be cropped (default=(0.08, 1.0)).

  • ratio (list, tuple, optional) – Range (min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)).

  • interpolation (Inter mode, optional) –

    Image interpolation mode (default=Inter.BILINEAR). It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.BILINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop area (default=10). If exceeded, fall back to use center crop instead.

实际案例

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> bbox_op = c_vision.RandomResizedCropWithBBox(size=50, interpolation=Inter.NEAREST)
>>> transforms_list = [decode_op, bbox_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomResizeWithBBox(size)[源代码]

Tensor operation to resize the input image using a randomly selected interpolation mode and adjust bounding boxes accordingly.

参数

size (Union[int, sequence]) – The output size of the resized image. If size is an integer, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

实际案例

>>> # randomly resize image with bounding boxes, keeping aspect ratio
>>> transforms_list1 = [c_vision.Decode(), c_vision.RandomResizeWithBBox(60)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # randomly resize image with bounding boxes to portrait style
>>> transforms_list2 = [c_vision.Decode(), c_vision.RandomResizeWithBBox((80, 60))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.RandomRotation(degrees, resample=<Inter.NEAREST: 0>, expand=False, center=None, fill_value=0)[源代码]

Rotate the input image randomly within a specified range of degrees.

参数
  • degrees (Union[int, float, sequence]) – Range of random rotation degrees. If degrees is a number, the range will be converted to (-degrees, degrees). If degrees is a sequence, it should be (min, max).

  • resample (Inter mode, optional) –

    An optional resampling filter (default=Inter.NEAREST). It can be any of [Inter.BILINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.BILINEAR, means resample method is bilinear interpolation.

    • Inter.NEAREST, means resample method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means resample method is bicubic interpolation.

  • expand (bool, optional) – Optional expansion flag (default=False). If set to True, expand the output image to make it large enough to hold the entire rotated image. If set to False or omitted, make the output image the same size as the input. Note that the expand flag assumes rotation around the center and no translation.

  • center (tuple, optional) – Optional center of rotation (a 2-tuple) (default=None). Origin is the top left corner. None sets to the center of the image.

  • fill_value (Union[int, tuple], optional) – Optional fill color for the area outside the rotated image. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255] (default=0).

实际案例

>>> from mindspore.dataset.vision import Inter
>>> transforms_list = [c_vision.Decode(),
...                    c_vision.RandomRotation(degrees=5.0,
...                    resample=Inter.NEAREST,
...                    expand=True)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomSelectSubpolicy(policy)[源代码]

Choose a random sub-policy from a policy list to be applied on the input image.

参数

policy (list(list(tuple(TensorOp, prob (float)))) – List of sub-policies to choose from. A sub-policy is a list of tuples (op, prob), where op is a TensorOp operation and prob is the probability that this op will be applied, and the prob values must be in range [0, 1]. Once a sub-policy is selected, each op within the sub-policy with be applied in sequence according to its probability.

实际案例

>>> policy = [[(c_vision.RandomRotation((45, 45)), 0.5),
...            (c_vision.RandomVerticalFlip(), 1),
...            (c_vision.RandomColorAdjust(), 0.8)],
...           [(c_vision.RandomRotation((90, 90)), 1),
...            (c_vision.RandomColorAdjust(), 0.2)]]
>>> image_folder_dataset = image_folder_dataset.map(operations=c_vision.RandomSelectSubpolicy(policy),
...                                                 input_columns=["image"])
class tinyms.vision.RandomSharpness(degrees=(0.1, 1.9))[源代码]

Adjust the sharpness of the input image by a fixed or random degree. Degree of 0.0 gives a blurred image, degree of 1.0 gives the original image, and degree of 2.0 gives a sharpened image.

参数

degrees (Union[list, tuple], optional) – Range of random sharpness adjustment degrees. It should be in (min, max) format. If min=max, then it is a single fixed magnitude operation (default = (0.1, 1.9)).

引发
  • TypeError – If degrees is not a list or tuple.

  • ValueError – If degrees is negative.

  • ValueError – If degrees is in (max, min) format instead of (min, max).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomSharpness(degrees=(0.2, 1.9))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomSolarize(threshold=(0, 255))[源代码]

Randomly selects a subrange within the specified threshold range and sets the pixel value within the subrange to (255 - pixel).

参数

threshold (tuple, optional) – Range of random solarize threshold (default=(0, 255)). Threshold values should always be in (min, max) format, where min and max are integers in the range (0, 255), and min <= max. If min=max, then invert all pixel values above min(max).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomSolarize(threshold=(10,100))]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomVerticalFlip(prob=0.5)[源代码]

Randomly flip the input image vertically with a given probability.

参数

prob (float, optional) – Probability of the image being flipped (default=0.5).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomVerticalFlip(0.25)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.RandomVerticalFlipWithBBox(prob=0.5)[源代码]

Flip the input image vertically, randomly with a given probability and adjust bounding boxes accordingly.

参数

prob (float, optional) – Probability of the image being flipped (default=0.5).

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.RandomVerticalFlipWithBBox(0.20)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Rescale(rescale, shift)[源代码]

Rescale the input image with the given rescale and shift. This operator will rescale the input image with: output = image * rescale + shift.

参数
  • rescale (float) – Rescale factor.

  • shift (float) – Shift factor.

实际案例

>>> transforms_list = [c_vision.Decode(), c_vision.Rescale(1.0 / 255.0, -1.0)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.Resize(size, interpolation=<Inter.BILINEAR: 2>)[源代码]

Resize the input image to the given size with a given interpolation mode.

参数
  • size (Union[int, sequence]) – The output size of the resized image. If size is an integer, the smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

  • interpolation (Inter mode, optional) –

    Image interpolation mode (default=Inter.LINEAR). It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC, Inter.PILCUBIC].

    • Inter.LINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

    • Inter.AREA, means interpolation method is pixel area interpolation.

    • Inter.PILCUBIC, means interpolation method is bicubic interpolation like implemented in pillow, input should be in 3 channels format.

实际案例

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> resize_op = c_vision.Resize([100, 75], Inter.BICUBIC)
>>> transforms_list = [decode_op, resize_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.ResizeWithBBox(size, interpolation=<Inter.BILINEAR: 2>)[源代码]

Resize the input image to the given size and adjust bounding boxes accordingly.

参数
  • size (Union[int, sequence]) – The output size of the resized image. If size is an integer, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

  • interpolation (Inter mode, optional) –

    Image interpolation mode (default=Inter.LINEAR). It can be any of [Inter.LINEAR, Inter.NEAREST, Inter.BICUBIC].

    • Inter.LINEAR, means interpolation method is bilinear interpolation.

    • Inter.NEAREST, means interpolation method is nearest-neighbor interpolation.

    • Inter.BICUBIC, means interpolation method is bicubic interpolation.

实际案例

>>> from mindspore.dataset.vision import Inter
>>> decode_op = c_vision.Decode()
>>> bbox_op = c_vision.ResizeWithBBox(50, Inter.NEAREST)
>>> transforms_list = [decode_op, bbox_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])
class tinyms.vision.SoftDvppDecodeRandomCropResizeJpeg(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), max_attempts=10)[源代码]

A combination of Crop, Decode and Resize using the simulation algorithm of Ascend series chip DVPP module.

The usage scenario is consistent with SoftDvppDecodeResizeJpeg. The input image size should be in range [32*32, 8192*8192]. The zoom-out and zoom-in multiples of the image length and width should in the range [1/32, 16]. Only images with an even resolution can be output. The output of odd resolution is not supported.

参数
  • size (Union[int, sequence]) – The size of the output image. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • scale (list, tuple, optional) – Range [min, max) of respective size of the original size to be cropped (default=(0.08, 1.0)).

  • ratio (list, tuple, optional) – Range [min, max) of aspect ratio to be cropped (default=(3. / 4., 4. / 3.)).

  • max_attempts (int, optional) – The maximum number of attempts to propose a valid crop_area (default=10). If exceeded, fall back to use center_crop instead.

实际案例

>>> # decode, randomly crop and resize image, keeping aspect ratio
>>> transforms_list1 = [c_vision.SoftDvppDecodeRandomCropResizeJpeg(90)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # decode, randomly crop and resize to landscape style
>>> transforms_list2 = [c_vision.SoftDvppDecodeRandomCropResizeJpeg((80, 100))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.SoftDvppDecodeResizeJpeg(size)[源代码]

Decode and resize JPEG image using the simulation algorithm of Ascend series chip DVPP module.

It is recommended to use this algorithm in the following scenarios: When training, the DVPP of the Ascend chip is not used, and the DVPP of the Ascend chip is used during inference, and the accuracy of inference is lower than the accuracy of training; and the input image size should be in range [32*32, 8192*8192]. The zoom-out and zoom-in multiples of the image length and width should in the range [1/32, 16]. Only images with an even resolution can be output. The output of odd resolution is not supported.

参数

size (Union[int, sequence]) – The output size of the resized image. If size is an integer, smaller edge of the image will be resized to this value with the same image aspect ratio. If size is a sequence of length 2, it should be (height, width).

实际案例

>>> # decode and resize image, keeping aspect ratio
>>> transforms_list1 = [c_vision.SoftDvppDecodeResizeJpeg(70)]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list1,
...                                                 input_columns=["image"])
>>> # decode and resize to portrait style
>>> transforms_list2 = [c_vision.SoftDvppDecodeResizeJpeg((80, 60))]
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list2,
...                                                     input_columns=["image"])
class tinyms.vision.UniformAugment(transforms, num_ops=2)[源代码]

Perform randomly selected augmentation on input image.

参数
  • transforms – List of C++ operations (Python operations are not accepted).

  • num_ops (int, optional) – Number of operations to be selected and applied (default=2).

实际案例

>>> import mindspore.dataset.vision.py_transforms as py_vision
>>> transforms_list = [c_vision.RandomHorizontalFlip(),
...                    c_vision.RandomVerticalFlip(),
...                    c_vision.RandomColorAdjust(),
...                    c_vision.RandomRotation(degrees=45)]
>>> uni_aug_op = c_vision.UniformAugment(transforms=transforms_list, num_ops=2)
>>> transforms_all = [c_vision.Decode(), c_vision.Resize(size=[224, 224]),
...                   uni_aug_op]
>>> image_folder_dataset_1 = image_folder_dataset.map(operations=transforms_all,
...                                                   input_columns="image",
...                                                   num_parallel_workers=1)
class tinyms.vision.Compose(transforms)[源代码]

Compose a list of transforms into a single transform.

参数

transforms (list) – List of transformations to be applied.

实际案例

>>> compose = c_transforms.Compose([c_vision.Decode(), c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=compose)
class tinyms.vision.Concatenate(axis=0, prepend=None, append=None)[源代码]

Tensor operation that concatenates all columns into a single tensor.

参数
  • axis (int, optional) – Concatenate the tensors along given axis (Default=0).

  • prepend (numpy.array, optional) – NumPy array to be prepended to the already concatenated tensors (Default=None).

  • append (numpy.array, optional) – NumPy array to be appended to the already concatenated tensors (Default=None).

实际案例

>>> import numpy as np
>>> # concatenate string
>>> prepend_tensor = np.array(["dw", "df"], dtype='S')
>>> append_tensor = np.array(["dwsdf", "df"], dtype='S')
>>> concatenate_op = c_transforms.Concatenate(0, prepend_tensor, append_tensor)
>>> data = [["This","is","a","string"]]
>>> dataset = ds.NumpySlicesDataset(data)
>>> dataset = dataset.map(operations=concatenate_op)
class tinyms.vision.Duplicate[源代码]

Duplicate the input tensor to output, only support transform one column each time.

实际案例

>>> # Data before
>>> # |  x      |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------+
>>> data = [[1,2,3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["x"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Duplicate(),
...                                                 input_columns=["x"],
...                                                 output_columns=["x", "y"],
...                                                 column_order=["x", "y"])
>>> # Data after
>>> # |  x      |  y      |
>>> # +---------+---------+
>>> # | [1,2,3] | [1,2,3] |
>>> # +---------+---------+
class tinyms.vision.Fill(fill_value)[源代码]

Tensor operation to fill all elements in the tensor with the specified value. The output tensor will have the same shape and type as the input tensor.

参数

fill_value (Union[str, bytes, int, float, bool]) – scalar value to fill the tensor with.

实际案例

>>> import numpy as np
>>> # generate a 1D integer numpy array from 0 to 4
>>> def generator_1d():
...     for i in range(5):
...         yield (np.array([i]),)
>>> generator_dataset = ds.GeneratorDataset(generator_1d, column_names="col1")
>>> # [[0], [1], [2], [3], [4]]
>>> fill_op = c_transforms.Fill(3)
>>> generator_dataset = generator_dataset.map(operations=fill_op)
>>> # [[3], [3], [3], [3], [3]]
class tinyms.vision.Mask(operator, constant, dtype=mindspore.bool_)[源代码]

Mask content of the input tensor with the given predicate. Any element of the tensor that matches the predicate will be evaluated to True, otherwise False.

参数
  • operator (Relational) – relational operators, it can be any of [Relational.EQ, Relational.NE, Relational.LT, Relational.GT, Relational.LE, Relational.GE], take Relational.EQ as example, EQ refers to equal.

  • constant (Union[str, int, float, bool]) – Constant to be compared to. Constant will be cast to the type of the input tensor.

  • dtype (mindspore.dtype, optional) – Type of the generated mask (Default mstype.bool_).

实际案例

>>> from mindspore.dataset.transforms.c_transforms import Relational
>>> # Data before
>>> # |  col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------+
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Mask(Relational.EQ, 2))
>>> # Data after
>>> # |       col         |
>>> # +--------------------+
>>> # | [False,True,False] |
>>> # +--------------------+
class tinyms.vision.OneHot(num_classes)[源代码]

Tensor operation to apply one hot encoding.

参数

num_classes (int) – Number of classes of objects in dataset. It should be larger than the largest label number in the dataset.

引发

RuntimeError – feature size is bigger than num_classes.

实际案例

>>> # Assume that dataset has 10 classes, thus the label ranges from 0 to 9
>>> onehot_op = c_transforms.OneHot(num_classes=10)
>>> mnist_dataset = mnist_dataset.map(operations=onehot_op, input_columns=["label"])
class tinyms.vision.PadEnd(pad_shape, pad_value=None)[源代码]

Pad input tensor according to pad_shape, input tensor needs to have same rank.

参数
  • pad_shape (list(int)) – List of integers representing the shape needed. Dimensions that set to None will not be padded (i.e., original dim will be used). Shorter dimensions will truncate the values.

  • pad_value (Union[str, bytes, int, float, bool]), optional) – Value used to pad. Default to 0 or empty string in case of tensors of strings.

实际案例

>>> # Data before
>>> # |   col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------|
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.PadEnd(pad_shape=[4],
...                                                                                pad_value=10))
>>> # Data after
>>> # |    col     |
>>> # +------------+
>>> # | [1,2,3,10] |
>>> # +------------|
class tinyms.vision.RandomApply(transforms, prob=0.5)[源代码]

Randomly perform a series of transforms with a given probability.

参数
  • transforms (list) – List of transformations to be applied.

  • prob (float, optional) – The probability to apply the transformation list (default=0.5).

实际案例

>>> rand_apply = c_transforms.RandomApply([c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=rand_apply)
class tinyms.vision.RandomChoice(transforms)[源代码]

Randomly select one transform from a list of transforms to perform operation.

参数

transforms (list) – List of transformations to be chosen from to apply.

实际案例

>>> rand_choice = c_transforms.RandomChoice([c_vision.CenterCrop(50), c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=rand_choice)
class tinyms.vision.Slice(*slices)[源代码]

Slice operation to extract a tensor out using the given n slices.

The functionality of Slice is similar to NumPy’s indexing feature (Currently only rank-1 tensors are supported).

参数

slices (Union[int, list[int], slice, None, Ellipsis]) –

Maximum n number of arguments to slice a tensor of rank n . One object in slices can be one of:

  1. int: Slice this index only along the first dimension. Negative index is supported.

  2. list(int): Slice these indices along the first dimension. Negative indices are supported.

  3. slice: Slice the generated indices from the slice object along the first dimension. Similar to start:stop:step.

  4. None: Slice the whole dimension. Similar to [:] in Python indexing.

  5. Ellipsis: Slice the whole dimension, same result with None.

实际案例

>>> # Data before
>>> # |   col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------|
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> # slice indices 1 and 2 only
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Slice(slice(1,3)))
>>> # Data after
>>> # |   col   |
>>> # +---------+
>>> # |  [2,3]  |
>>> # +---------|
class tinyms.vision.TypeCast(data_type)[源代码]

Tensor operation to cast to a given MindSpore data type.

参数

data_type (mindspore.dtype) – mindspore.dtype to be cast to.

实际案例

>>> import numpy as np
>>> from mindspore import dtype as mstype
>>>
>>> # Generate 1d int numpy array from 0 - 63
>>> def generator_1d():
...     for i in range(64):
...         yield (np.array([i]),)
>>>
>>> dataset = ds.GeneratorDataset(generator_1d, column_names='col')
>>> type_cast_op = c_transforms.TypeCast(mstype.int32)
>>> dataset = dataset.map(operations=type_cast_op)
class tinyms.vision.Unique[源代码]

Perform the unique operation on the input tensor, only support transform one column each time.

Return 3 tensor: unique output tensor, index tensor, count tensor.

Unique output tensor contains all the unique elements of the input tensor in the same order that they occur in the input tensor.

Index tensor that contains the index of each element of the input tensor in the unique output tensor.

Count tensor that contains the count of each element of the output tensor in the input tensor.

注解

Call batch op before calling this function.

实际案例

>>> # Data before
>>> # |  x                 |
>>> # +--------------------+
>>> # | [[0,1,2], [1,2,3]] |
>>> # +--------------------+
>>> data = [[[0,1,2], [1,2,3]]]
>>> dataset = ds.NumpySlicesDataset(data, ["x"])
>>> dataset = dataset.map(operations=c_transforms.Unique(),
...                       input_columns=["x"],
...                       output_columns=["x", "y", "z"],
...                       column_order=["x", "y", "z"])
>>> # Data after
>>> # |  x      |  y              |z        |
>>> # +---------+-----------------+---------+
>>> # | [0,1,2,3] | [0,1,2,1,2,3] | [1,2,2,1]
>>> # +---------+-----------------+---------+

tinyms.text

This module is to support text processing for NLP tasks. It is a high performance NLP text processing module which is developed with ICU4C and cppjieba.

class tinyms.text.BertDatasetTransform[源代码]

Apply preprocess operation on GeneratorDataset instance.

class tinyms.text.Lookup(vocab, unknown_token=None, data_type=mindspore.int32)[源代码]

Look up a word into an id according to the input vocabulary table.

参数
  • vocab (Vocab) – A vocabulary object.

  • unknown_token (str, optional) – Word is used for lookup. In case of the word is out of vocabulary (OOV), the result of lookup will be replaced with unknown_token. If the unknown_token is not specified or it is OOV, runtime error will be thrown (default={}, means no unknown_token is specified).

  • data_type (mindspore.dtype, optional) – The data type that lookup operation maps string to(default=mindspore.int32).

实际案例

>>> # Load vocabulary from list
>>> vocab = text.Vocab.from_list(['深', '圳', '欢', '迎', '您'])
>>> # Use Lookup operator to map tokens to ids
>>> lookup = text.Lookup(vocab)
>>> text_file_dataset = text_file_dataset.map(operations=[lookup])
class tinyms.text.JiebaTokenizer(hmm_path, mp_path, mode=<JiebaMode.MIX: 0>, with_offsets=False)[源代码]

Tokenize Chinese string into words based on dictionary.

注解

The integrity of the HMMSEgment algorithm and MPSegment algorithm files must be confirmed.

参数
  • hmm_path (str) – Dictionary file is used by HMMSegment algorithm. The dictionary can be obtained on the official website of cppjieba.

  • mp_path (str) – Dictionary file is used by MPSegment algorithm. The dictionary can be obtained on the official website of cppjieba.

  • mode (JiebaMode, optional) –

    Valid values can be any of [JiebaMode.MP, JiebaMode.HMM, JiebaMode.MIX](default=JiebaMode.MIX).

    • JiebaMode.MP, tokenize with MPSegment algorithm.

    • JiebaMode.HMM, tokenize with Hidden Markov Model Segment algorithm.

    • JiebaMode.MIX, tokenize with a mix of MPSegment and HMMSegment algorithm.

  • with_offsets (bool, optional) – Whether or not output offsets of tokens (default=False).

实际案例

>>> from mindspore.dataset.text import JiebaMode
>>> # If with_offsets=False, default output one column {["text", dtype=str]}
>>> jieba_hmm_file = "/path/to/jieba/hmm/file"
>>> jieba_mp_file = "/path/to/jieba/mp/file"
>>> tokenizer_op = text.JiebaTokenizer(jieba_hmm_file, jieba_mp_file, mode=JiebaMode.MP, with_offsets=False)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer_op)
>>> # If with_offsets=False, then output three columns {["token", dtype=str], ["offsets_start", dtype=uint32],
>>> #                                                   ["offsets_limit", dtype=uint32]}
>>> tokenizer_op = text.JiebaTokenizer(jieba_hmm_file, jieba_mp_file, mode=JiebaMode.MP, with_offsets=True)
>>> text_file_dataset_1 = text_file_dataset_1.map(operations=tokenizer_op, input_columns=["text"],
...                                               output_columns=["token", "offsets_start", "offsets_limit"],
...                                               column_order=["token", "offsets_start", "offsets_limit"])
add_dict(user_dict)[源代码]

Add a user defined word to JiebaTokenizer’s dictionary.

参数

user_dict (Union[str, dict]) –

One of the two loading methods is file path(str) loading (according to the Jieba dictionary format) and the other is Python dictionary(dict) loading, Python Dict format: {word1:freq1, word2:freq2,…}. Jieba dictionary format : word(required), freq(optional), such as:

word1 freq1
word2 None
word3 freq3

Only valid word-freq pairs in user provided file will be added into the dictionary. Rows containing invalid input will be ignored. No error nor warning Status is returned.

实际案例

>>> from mindspore.dataset.text import JiebaMode
>>> jieba_hmm_file = "/path/to/jieba/hmm/file"
>>> jieba_mp_file = "/path/to/jieba/mp/file"
>>> user_dict = {"男默女泪": 10}
>>> jieba_op = text.JiebaTokenizer(jieba_hmm_file, jieba_mp_file, mode=JiebaMode.MP)
>>> jieba_op.add_dict(user_dict)
>>> text_file_dataset = text_file_dataset.map(operations=jieba_op, input_columns=["text"])
add_word(word, freq=None)[源代码]

Add a user defined word to JiebaTokenizer’s dictionary.

参数
  • word (str) – The word to be added to the JiebaTokenizer instance. The added word will not be written into the built-in dictionary on disk.

  • freq (int, optional) – The frequency of the word to be added. The higher the frequency, the better chance the word will be tokenized (default=None, use default frequency).

实际案例

>>> from mindspore.dataset.text import JiebaMode
>>> jieba_hmm_file = "/path/to/jieba/hmm/file"
>>> jieba_mp_file = "/path/to/jieba/mp/file"
>>> jieba_op = text.JiebaTokenizer(jieba_hmm_file, jieba_mp_file, mode=JiebaMode.MP)
>>> sentence_piece_vocab_file = "/path/to/sentence/piece/vocab/file"
>>> with open(sentence_piece_vocab_file, 'r') as f:
...     for line in f:
...         word = line.split(',')[0]
...         jieba_op.add_word(word)
>>> text_file_dataset = text_file_dataset.map(operations=jieba_op, input_columns=["text"])
class tinyms.text.UnicodeCharTokenizer(with_offsets=False)[源代码]

Tokenize a scalar tensor of UTF-8 string to Unicode characters.

参数

with_offsets (bool, optional) – Whether or not output offsets of tokens (default=False).

实际案例

>>> # If with_offsets=False, default output one column {["text", dtype=str]}
>>> tokenizer_op = text.UnicodeCharTokenizer(with_offsets=False)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer_op)
>>> # If with_offsets=True, then output three columns {["token", dtype=str], ["offsets_start", dtype=uint32],
>>> #                                                   ["offsets_limit", dtype=uint32]}
>>> tokenizer_op = text.UnicodeCharTokenizer(with_offsets=True)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer_op, input_columns=["text"],
...                                           output_columns=["token", "offsets_start", "offsets_limit"],
...                                           column_order=["token", "offsets_start", "offsets_limit"])
class tinyms.text.Ngram(n, left_pad=('', 0), right_pad=('', 0), separator=' ')[源代码]

TensorOp to generate n-gram from a 1-D string Tensor.

Refer to https://en.wikipedia.org/wiki/N-gram#Examples for an overview of what n-gram is and how it works.

参数
  • n (list[int]) – n in n-gram, which is a list of positive integers. For example, if n=[4, 3], then the result would be a 4-gram followed by a 3-gram in the same tensor. If the number of words is not enough to make up for a n-gram, an empty string will be returned. For example, 3 grams on [“mindspore”, “best”] will result in an empty string produced.

  • left_pad (tuple, optional) – Padding performed on left side of the sequence shaped like (“pad_token”, pad_width). pad_width will be capped at n-1. For example, specifying left_pad=(“_”, 2) would pad left side of the sequence with “__” (default=None).

  • right_pad (tuple, optional) – Padding performed on right side of the sequence shaped like (“pad_token”, pad_width). pad_width will be capped at n-1. For example, specifying right_pad=(“_”, 2) would pad right side of the sequence with “__” (default=None).

  • separator (str, optional) – Symbol used to join strings together. For example. if 2-gram is [“mindspore”, “amazing”] with separator=”-”, the result would be [“mindspore-amazing”] (default=None, which will use whitespace as separator).

实际案例

>>> ngram_op = text.Ngram(3, separator="-")
>>> output = ngram_op(["WildRose Country", "Canada's Ocean Playground", "Land of Living Skies"])
>>> # output
>>> # ["WildRose Country-Canada's Ocean Playground-Land of Living Skies"]
>>> # same ngram_op called through map
>>> text_file_dataset = text_file_dataset.map(operations=ngram_op)
class tinyms.text.WordpieceTokenizer(vocab, suffix_indicator='##', max_bytes_per_token=100, unknown_token='[UNK]', with_offsets=False)[源代码]

Tokenize scalar token or 1-D tokens to 1-D subword tokens.

参数
  • vocab (Vocab) – A vocabulary object.

  • suffix_indicator (str, optional) – Used to show that the subword is the last part of a word (default=’##’).

  • max_bytes_per_token (int, optional) – Tokens exceeding this length will not be further split (default=100).

  • unknown_token (str, optional) – When a token cannot be found: if ‘unknown_token’ is empty string, return the token directly, else return ‘unknown_token’ (default=’[UNK]’).

  • with_offsets (bool, optional) – Whether or not output offsets of tokens (default=False).

实际案例

>>> vocab_list = ["book", "cholera", "era", "favor", "##ite", "my", "is", "love", "dur", "##ing", "the"]
>>> vocab = text.Vocab.from_list(vocab_list)
>>> # If with_offsets=False, default output one column {["text", dtype=str]}
>>> tokenizer_op = text.WordpieceTokenizer(vocab=vocab, unknown_token='[UNK]',
...                                        max_bytes_per_token=100, with_offsets=False)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer_op)
>>> # If with_offsets=True, then output three columns {["token", dtype=str], ["offsets_start", dtype=uint32],
>>> #                                                   ["offsets_limit", dtype=uint32]}
>>> tokenizer_op = text.WordpieceTokenizer(vocab=vocab, unknown_token='[UNK]',
...                                       max_bytes_per_token=100, with_offsets=True)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer_op, input_columns=["text"],
...                                           output_columns=["token", "offsets_start", "offsets_limit"],
...                                           column_order=["token", "offsets_start", "offsets_limit"])
class tinyms.text.TruncateSequencePair(max_length)[源代码]

Truncate a pair of rank-1 tensors such that the total length is less than max_length.

This operation takes two input tensors and returns two output Tensors.

参数

max_length (int) – Maximum length required.

实际案例

>>> dataset = ds.NumpySlicesDataset(data={"col1": [[1, 2, 3]], "col2": [[4, 5]]})
>>> # Data before
>>> # |   col1    |   col2    |
>>> # +-----------+-----------|
>>> # | [1, 2, 3] |  [4, 5]   |
>>> # +-----------+-----------+
>>> truncate_sequence_pair_op = text.TruncateSequencePair(max_length=4)
>>> dataset = dataset.map(operations=truncate_sequence_pair_op)
>>> # Data after
>>> # |   col1    |   col2    |
>>> # +-----------+-----------+
>>> # |  [1, 2]   |  [4, 5]   |
>>> # +-----------+-----------+
class tinyms.text.ToNumber(data_type)[源代码]

Tensor operation to convert every element of a string tensor to a number.

Strings are cast according to the rules specified in the following links, except that any strings which represent negative numbers cannot be cast to an unsigned integer type, rules links are as follows: https://en.cppreference.com/w/cpp/string/basic_string/stof, https://en.cppreference.com/w/cpp/string/basic_string/stoul,

参数

data_type (mindspore.dtype) – Type to be cast to. Must be a numeric type in mindspore.dtype.

引发

RuntimeError – If strings are invalid to cast, or are out of range after being cast.

实际案例

>>> from mindspore import dtype as mstype
>>> data = [["1", "2", "3"]]
>>> dataset = ds.NumpySlicesDataset(data)
>>> to_number_op = text.ToNumber(mstype.int8)
>>> dataset = dataset.map(operations=to_number_op)
class tinyms.text.SlidingWindow(width, axis=0)[源代码]

Construct a tensor from given data (only support 1-D for now), where each element in the dimension axis is a slice of data starting at the corresponding position, with a specified width.

参数
  • width (int) – The width of the window. It must be an integer and greater than zero.

  • axis (int, optional) – The axis along which the sliding window is computed (default=0).

实际案例

>>> dataset = ds.NumpySlicesDataset(data=[[1, 2, 3, 4, 5]], column_names="col1")
>>> # Data before
>>> # |     col1     |
>>> # +--------------+
>>> # | [[1, 2, 3, 4, 5]] |
>>> # +--------------+
>>> dataset = dataset.map(operations=text.SlidingWindow(3, 0))
>>> # Data after
>>> # |     col1     |
>>> # +--------------+
>>> # |  [[1, 2, 3], |
>>> # |   [2, 3, 4], |
>>> # |   [3, 4, 5]] |
>>> # +--------------+
class tinyms.text.SentencePieceTokenizer(mode, out_type)[源代码]

Tokenize scalar token or 1-D tokens to tokens by sentencepiece.

参数
  • mode (Union[str, SentencePieceVocab]) – If the input parameter is a file, then its type should be string. If the input parameter is a SentencePieceVocab object, then its type should be SentencePieceVocab.

  • out_type (SPieceTokenizerOutType) –

    The type of output, it can be any of [SPieceTokenizerOutType.STRING, SPieceTokenizerOutType.INT].

    • SPieceTokenizerOutType.STRING, means output type of SentencePice Tokenizer is string.

    • SPieceTokenizerOutType.INT, means output type of SentencePice Tokenizer is int.

实际案例

>>> from mindspore.dataset.text import SentencePieceModel, SPieceTokenizerOutType
>>> sentence_piece_vocab_file = "/path/to/sentence/piece/vocab/file"
>>> vocab = text.SentencePieceVocab.from_file([sentence_piece_vocab_file], 5000, 0.9995,
...                                           SentencePieceModel.UNIGRAM, {})
>>> tokenizer = text.SentencePieceTokenizer(vocab, out_type=SPieceTokenizerOutType.STRING)
>>> text_file_dataset = text_file_dataset.map(operations=tokenizer)
class tinyms.text.PythonTokenizer(tokenizer)[源代码]

Class that applies user-defined string tokenizer into input string.

参数

tokenizer (Callable) – Python function that takes a str and returns a list of str as tokens.

实际案例

>>> def my_tokenizer(line):
...     return line.split()
>>> text_file_dataset = text_file_dataset.map(operations=text.PythonTokenizer(my_tokenizer))
tinyms.text.to_str(array, encoding='utf8')[源代码]

Convert NumPy array of bytes to array of str by decoding each element based on charset encoding.

参数
  • array (numpy.ndarray) – Array of bytes type representing strings.

  • encoding (str) – Indicating the charset for decoding.

返回

numpy.ndarray, NumPy array of str.

实际案例

>>> text_file_dataset_dir = ["/path/to/text_file_dataset_file"]
>>> dataset = ds.TextFileDataset(dataset_files=text_file_dataset_dir, shuffle=False)
>>> for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(text.to_str(item["text"]))
tinyms.text.to_bytes(array, encoding='utf8')[源代码]

Convert NumPy array of str to array of bytes by encoding each element based on charset encoding.

参数
  • array (numpy.ndarray) – Array of str type representing strings.

  • encoding (str) – Indicating the charset for encoding.

返回

numpy.ndarray, NumPy array of bytes.

class tinyms.text.Vocab[源代码]

Vocab object that is used to lookup a word.

It contains a map that maps each word(str) to an id (int).

classmethod from_dataset(dataset, columns=None, freq_range=None, top_k=None, special_tokens=None, special_first=True)[源代码]

Build a vocab from a dataset.

This would collect all unique words in a dataset and return a vocab within the frequency range specified by user in freq_range. User would be warned if no words fall into the frequency. Words in vocab are ordered from highest frequency to lowest frequency. Words with the same frequency would be ordered lexicographically.

参数
  • dataset (Dataset) – dataset to build vocab from.

  • columns (list[str], optional) – column names to get words from. It can be a list of column names. (default=None, where all columns will be used. If any column isn’t string type, will return error).

  • freq_range (tuple, optional) – A tuple of integers (min_frequency, max_frequency). Words within the frequency range would be kept. 0 <= min_frequency <= max_frequency <= total_words. min_frequency=0 is the same as min_frequency=1. max_frequency > total_words is the same as max_frequency = total_words. min_frequency/max_frequency can be None, which corresponds to 0/total_words separately (default=None, all words are included).

  • top_k (int, optional) – top_k is greater than 0. Number of words to be built into vocab. top_k means most frequent words are taken. top_k is taken after freq_range. If not enough top_k, all words will be taken (default=None, all words are included).

  • special_tokens (list, optional) – A list of strings, each one is a special token. For example special_tokens=[“<pad>”,”<unk>”] (default=None, no special tokens will be added).

  • special_first (bool, optional) – Whether special_tokens will be prepended/appended to vocab. If special_tokens is specified and special_first is set to True, special_tokens will be prepended (default=True).

返回

Vocab, vocab built from the dataset.

实际案例

>>> dataset = ds.TextFileDataset("/path/to/sentence/piece/vocab/file", shuffle=False)
>>> vocab = text.Vocab.from_dataset(dataset, "text", freq_range=None, top_k=None,
...                                 special_tokens=["<pad>", "<unk>"],
...                                 special_first=True)
>>> dataset = dataset.map(operations=text.Lookup(vocab, "<unk>"), input_columns=["text"])
classmethod from_dict(word_dict)[源代码]

Build a vocab object from a dict.

参数

word_dict (dict) – Dict contains word and id pairs, where word should be str and id be int. id is recommended to start from 0 and be continuous. ValueError will be raised if id is negative.

返回

Vocab, vocab built from the dict.

实际案例

>>> vocab = text.Vocab.from_dict({"home": 3, "behind": 2, "the": 4, "world": 5, "<unk>": 6})
classmethod from_file(file_path, delimiter='', vocab_size=None, special_tokens=None, special_first=True)[源代码]

Build a vocab object from a list of word.

参数
  • file_path (str) – Path to the file which contains the vocab list.

  • delimiter (str, optional) – A delimiter to break up each line in file, the first element is taken to be the word (default=””).

  • vocab_size (int, optional) – Number of words to read from file_path (default=None, all words are taken).

  • special_tokens (list, optional) – A list of strings, each one is a special token. for example special_tokens=[“<pad>”,”<unk>”] (default=None, no special tokens will be added).

  • special_first (bool, optional) – Whether special_tokens will be prepended/appended to vocab, If special_tokens is specified and special_first is set to True, special_tokens will be prepended (default=True).

返回

Vocab, vocab built from the file.

实际案例

>>> vocab = text.Vocab.from_file("/path/to/simple/vocab/file", ",", None, ["<pad>", "<unk>"], True)
classmethod from_list(word_list, special_tokens=None, special_first=True)[源代码]

Build a vocab object from a list of word.

参数
  • word_list (list) – A list of string where each element is a word of type string.

  • special_tokens (list, optional) – A list of strings, each one is a special token. for example special_tokens=[“<pad>”,”<unk>”] (default=None, no special tokens will be added).

  • special_first (bool, optional) – Whether special_tokens is prepended or appended to vocab. If special_tokens is specified and special_first is set to True, special_tokens will be prepended (default=True).

返回

Vocab, vocab built from the list.

实际案例

>>> vocab = text.Vocab.from_list(["w1", "w2", "w3"], special_tokens=["<unk>"], special_first=True)
class tinyms.text.SentencePieceVocab[源代码]

SentencePiece object that is used to do words segmentation.

classmethod from_dataset(dataset, col_names, vocab_size, character_coverage, model_type, params)[源代码]

Build a SentencePiece from a dataset.

参数
  • dataset (Dataset) – Dataset to build SentencePiece.

  • col_names (list) – The list of the col name.

  • vocab_size (int) – Vocabulary size.

  • character_coverage (float) – Amount of characters covered by the model, good defaults are: 0.9995 for languages with rich character set like Japanese or Chinese and 1.0 for other languages with small character set.

  • model_type (SentencePieceModel) –

    It can be any of [SentencePieceModel.UNIGRAM, SentencePieceModel.BPE, SentencePieceModel.CHAR, SentencePieceModel.WORD], default is SentencePieceModel.UNIGRAM. The input sentence must be pre-tokenized when using SentencePieceModel.WORD type.

    • SentencePieceModel.UNIGRAM, Unigram Language Model means the next word in the sentence is assumed to be independent of the previous words generated by the model.

    • SentencePieceModel.BPE, refers to byte pair encoding algorithm, which replaces the most frequent pair of bytes in a sentence with a single, unused byte.

    • SentencePieceModel.CHAR, refers to char based sentencePiece Model type.

    • SentencePieceModel.WORD, refers to word based sentencePiece Model type.

  • params (dict) – A dictionary with no incoming parameters.

返回

SentencePieceVocab, vocab built from the dataset.

实际案例

>>> from mindspore.dataset.text import SentencePieceModel
>>> dataset = ds.TextFileDataset("/path/to/sentence/piece/vocab/file", shuffle=False)
>>> vocab = text.SentencePieceVocab.from_dataset(dataset, ["text"], 5000, 0.9995,
...                                              SentencePieceModel.UNIGRAM, {})
classmethod from_file(file_path, vocab_size, character_coverage, model_type, params)[源代码]

Build a SentencePiece object from a list of word.

参数
  • file_path (list) – Path to the file which contains the SentencePiece list.

  • vocab_size (int) – Vocabulary size.

  • character_coverage (float) – Amount of characters covered by the model, good defaults are: 0.9995 for languages with rich character set like Japanese or Chinese and 1.0 for other languages with small character set.

  • model_type (SentencePieceModel) –

    It can be any of [SentencePieceModel.UNIGRAM, SentencePieceModel.BPE, SentencePieceModel.CHAR, SentencePieceModel.WORD], default is SentencePieceModel.UNIGRAM. The input sentence must be pre-tokenized when using SentencePieceModel.WORD type.

    • SentencePieceModel.UNIGRAM, Unigram Language Model means the next word in the sentence is assumed to be independent of the previous words generated by the model.

    • SentencePieceModel.BPE, refers to byte pair encoding algorithm, which replaces the most frequent pair of bytes in a sentence with a single, unused byte.

    • SentencePieceModel.CHAR, refers to char based sentencePiece Model type.

    • SentencePieceModel.WORD, refers to word based sentencePiece Model type.

  • params (dict) –

    A dictionary with no incoming parameters(The parameters are derived from SentencePiece library).

    input_sentence_size 0
    max_sentencepiece_length 16
    

返回

SentencePieceVocab, vocab built from the file.

实际案例

>>> from mindspore.dataset.text import SentencePieceModel
>>> vocab = text.SentencePieceVocab.from_file(["/path/to/sentence/piece/vocab/file"], 5000, 0.9995,
...                                           SentencePieceModel.UNIGRAM, {})
classmethod save_model(vocab, path, filename)[源代码]

Save model into given filepath.

参数
  • vocab (SentencePieceVocab) – A SentencePiece object.

  • path (str) – Path to store model.

  • filename (str) – The name of the file.

实际案例

>>> from mindspore.dataset.text import SentencePieceModel
>>> vocab = text.SentencePieceVocab.from_file(["/path/to/sentence/piece/vocab/file"], 5000, 0.9995,
...                                           SentencePieceModel.UNIGRAM, {})
>>> text.SentencePieceVocab.save_model(vocab, "./", "m.model")
class tinyms.text.SentencePieceModel[源代码]

An enumeration for SentencePieceModel.

Possible enumeration values are: SentencePieceModel.UNIGRAM, SentencePieceModel.BPE, SentencePieceModel.CHAR, SentencePieceModel.WORD.

  • SentencePieceModel,UNIGRAM: Unigram Language Model means the next word in the sentence is assumed to be independent of the previous words generated by the model.

  • SentencePieceModel.BPE: refers to byte pair encoding algorithm, which replaces the most frequent pair of bytes in a sentence with a single, unused byte.

  • SentencePieceModel.CHAR: refers to char based sentencePiece Model type.

  • SentencePieceModel.WORD: refers to word based sentencePiece Model type.

class tinyms.text.SPieceTokenizerOutType[源代码]

An enumeration for SPieceTokenizerOutType.

Possible enumeration values are: SPieceTokenizerOutType.STRING, SPieceTokenizerOutType.INT.

  • SPieceTokenizerOutType.STRING: means output type of SentencePice Tokenizer is string.

  • SPieceTokenizerOutType.INT: means output type of SentencePice Tokenizer is int.

class tinyms.text.SPieceTokenizerLoadType[源代码]

An enumeration for SPieceTokenizerLoadType.

Possible enumeration values are: SPieceTokenizerLoadType.FILE, SPieceTokenizerLoadTypeMODEL.

  • SPieceTokenizerLoadType.FILE: Load sentencepiece tokenizer from local sentencepiece vocab file.

  • SPieceTokenizerLoadType.MODEL: Load sentencepiece tokenizer from sentencepiece vocab instance.

class tinyms.text.Compose(transforms)[源代码]

Compose a list of transforms into a single transform.

参数

transforms (list) – List of transformations to be applied.

实际案例

>>> compose = c_transforms.Compose([c_vision.Decode(), c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=compose)
class tinyms.text.Concatenate(axis=0, prepend=None, append=None)[源代码]

Tensor operation that concatenates all columns into a single tensor.

参数
  • axis (int, optional) – Concatenate the tensors along given axis (Default=0).

  • prepend (numpy.array, optional) – NumPy array to be prepended to the already concatenated tensors (Default=None).

  • append (numpy.array, optional) – NumPy array to be appended to the already concatenated tensors (Default=None).

实际案例

>>> import numpy as np
>>> # concatenate string
>>> prepend_tensor = np.array(["dw", "df"], dtype='S')
>>> append_tensor = np.array(["dwsdf", "df"], dtype='S')
>>> concatenate_op = c_transforms.Concatenate(0, prepend_tensor, append_tensor)
>>> data = [["This","is","a","string"]]
>>> dataset = ds.NumpySlicesDataset(data)
>>> dataset = dataset.map(operations=concatenate_op)
class tinyms.text.Duplicate[源代码]

Duplicate the input tensor to output, only support transform one column each time.

实际案例

>>> # Data before
>>> # |  x      |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------+
>>> data = [[1,2,3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["x"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Duplicate(),
...                                                 input_columns=["x"],
...                                                 output_columns=["x", "y"],
...                                                 column_order=["x", "y"])
>>> # Data after
>>> # |  x      |  y      |
>>> # +---------+---------+
>>> # | [1,2,3] | [1,2,3] |
>>> # +---------+---------+
class tinyms.text.Fill(fill_value)[源代码]

Tensor operation to fill all elements in the tensor with the specified value. The output tensor will have the same shape and type as the input tensor.

参数

fill_value (Union[str, bytes, int, float, bool]) – scalar value to fill the tensor with.

实际案例

>>> import numpy as np
>>> # generate a 1D integer numpy array from 0 to 4
>>> def generator_1d():
...     for i in range(5):
...         yield (np.array([i]),)
>>> generator_dataset = ds.GeneratorDataset(generator_1d, column_names="col1")
>>> # [[0], [1], [2], [3], [4]]
>>> fill_op = c_transforms.Fill(3)
>>> generator_dataset = generator_dataset.map(operations=fill_op)
>>> # [[3], [3], [3], [3], [3]]
class tinyms.text.Mask(operator, constant, dtype=mindspore.bool_)[源代码]

Mask content of the input tensor with the given predicate. Any element of the tensor that matches the predicate will be evaluated to True, otherwise False.

参数
  • operator (Relational) – relational operators, it can be any of [Relational.EQ, Relational.NE, Relational.LT, Relational.GT, Relational.LE, Relational.GE], take Relational.EQ as example, EQ refers to equal.

  • constant (Union[str, int, float, bool]) – Constant to be compared to. Constant will be cast to the type of the input tensor.

  • dtype (mindspore.dtype, optional) – Type of the generated mask (Default mstype.bool_).

实际案例

>>> from mindspore.dataset.transforms.c_transforms import Relational
>>> # Data before
>>> # |  col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------+
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Mask(Relational.EQ, 2))
>>> # Data after
>>> # |       col         |
>>> # +--------------------+
>>> # | [False,True,False] |
>>> # +--------------------+
class tinyms.text.OneHot(num_classes)[源代码]

Tensor operation to apply one hot encoding.

参数

num_classes (int) – Number of classes of objects in dataset. It should be larger than the largest label number in the dataset.

引发

RuntimeError – feature size is bigger than num_classes.

实际案例

>>> # Assume that dataset has 10 classes, thus the label ranges from 0 to 9
>>> onehot_op = c_transforms.OneHot(num_classes=10)
>>> mnist_dataset = mnist_dataset.map(operations=onehot_op, input_columns=["label"])
class tinyms.text.PadEnd(pad_shape, pad_value=None)[源代码]

Pad input tensor according to pad_shape, input tensor needs to have same rank.

参数
  • pad_shape (list(int)) – List of integers representing the shape needed. Dimensions that set to None will not be padded (i.e., original dim will be used). Shorter dimensions will truncate the values.

  • pad_value (Union[str, bytes, int, float, bool]), optional) – Value used to pad. Default to 0 or empty string in case of tensors of strings.

实际案例

>>> # Data before
>>> # |   col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------|
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.PadEnd(pad_shape=[4],
...                                                                                pad_value=10))
>>> # Data after
>>> # |    col     |
>>> # +------------+
>>> # | [1,2,3,10] |
>>> # +------------|
class tinyms.text.RandomApply(transforms, prob=0.5)[源代码]

Randomly perform a series of transforms with a given probability.

参数
  • transforms (list) – List of transformations to be applied.

  • prob (float, optional) – The probability to apply the transformation list (default=0.5).

实际案例

>>> rand_apply = c_transforms.RandomApply([c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=rand_apply)
class tinyms.text.RandomChoice(transforms)[源代码]

Randomly select one transform from a list of transforms to perform operation.

参数

transforms (list) – List of transformations to be chosen from to apply.

实际案例

>>> rand_choice = c_transforms.RandomChoice([c_vision.CenterCrop(50), c_vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=rand_choice)
class tinyms.text.Slice(*slices)[源代码]

Slice operation to extract a tensor out using the given n slices.

The functionality of Slice is similar to NumPy’s indexing feature (Currently only rank-1 tensors are supported).

参数

slices (Union[int, list[int], slice, None, Ellipsis]) –

Maximum n number of arguments to slice a tensor of rank n . One object in slices can be one of:

  1. int: Slice this index only along the first dimension. Negative index is supported.

  2. list(int): Slice these indices along the first dimension. Negative indices are supported.

  3. slice: Slice the generated indices from the slice object along the first dimension. Similar to start:stop:step.

  4. None: Slice the whole dimension. Similar to [:] in Python indexing.

  5. Ellipsis: Slice the whole dimension, same result with None.

实际案例

>>> # Data before
>>> # |   col   |
>>> # +---------+
>>> # | [1,2,3] |
>>> # +---------|
>>> data = [[1, 2, 3]]
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["col"])
>>> # slice indices 1 and 2 only
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=c_transforms.Slice(slice(1,3)))
>>> # Data after
>>> # |   col   |
>>> # +---------+
>>> # |  [2,3]  |
>>> # +---------|
class tinyms.text.TypeCast(data_type)[源代码]

Tensor operation to cast to a given MindSpore data type.

参数

data_type (mindspore.dtype) – mindspore.dtype to be cast to.

实际案例

>>> import numpy as np
>>> from mindspore import dtype as mstype
>>>
>>> # Generate 1d int numpy array from 0 - 63
>>> def generator_1d():
...     for i in range(64):
...         yield (np.array([i]),)
>>>
>>> dataset = ds.GeneratorDataset(generator_1d, column_names='col')
>>> type_cast_op = c_transforms.TypeCast(mstype.int32)
>>> dataset = dataset.map(operations=type_cast_op)
class tinyms.text.Unique[源代码]

Perform the unique operation on the input tensor, only support transform one column each time.

Return 3 tensor: unique output tensor, index tensor, count tensor.

Unique output tensor contains all the unique elements of the input tensor in the same order that they occur in the input tensor.

Index tensor that contains the index of each element of the input tensor in the unique output tensor.

Count tensor that contains the count of each element of the output tensor in the input tensor.

注解

Call batch op before calling this function.

实际案例

>>> # Data before
>>> # |  x                 |
>>> # +--------------------+
>>> # | [[0,1,2], [1,2,3]] |
>>> # +--------------------+
>>> data = [[[0,1,2], [1,2,3]]]
>>> dataset = ds.NumpySlicesDataset(data, ["x"])
>>> dataset = dataset.map(operations=c_transforms.Unique(),
...                       input_columns=["x"],
...                       output_columns=["x", "y", "z"],
...                       column_order=["x", "y", "z"])
>>> # Data after
>>> # |  x      |  y              |z        |
>>> # +---------+-----------------+---------+
>>> # | [0,1,2,3] | [0,1,2,1,2,3] | [1,2,2,1]
>>> # +---------+-----------------+---------+

tinyms.primitives

Primitives module. Operators can be used in the construct function of Layer.

实际案例

>>> import tinyms as ts
>>> from tinyms.primitives import tensor_add
>>>
>>> x = ts.ones([2, 3])
>>> y = ts.ones([2, 3])
>>> print(tensor_add(x, y))
[[2. 2. 2.]
[2. 2. 2.]]
tinyms.primitives.core(fn=None, **flags)[源代码]

A decorator that adds a flag to the function.

By default, the function is marked as True, enabling to use this decorator to set flag to a graph.

参数
  • fn (Function) – Function to add flag. Default: None.

  • flags (dict) – The following flags can be set core, which indicates that this is a core function or other flag. Default: None.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> net = core(net, predit=True)
>>> print(hasattr(net, '_mindspore_flags'))
True
tinyms.primitives.add_flags(fn=None, **flags)[源代码]

A decorator that adds a flag to the function.

注解

Only supports bool value.

参数
  • fn (Function) – Function or cell to add flag. Default: None.

  • flags (dict) – Flags use kwargs. Default: None.

返回

Function, the function with added flags.

实际案例

>>> net = Net();
>>> net = add_flags(net, predit=True)
>>> print(hasattr(net, '_mindspore_flags'))
True
class tinyms.primitives.MultitypeFuncGraph(name, read_value=False)[源代码]

Generates overloaded functions.

MultitypeFuncGraph is a class used to generate overloaded functions, considering different types as inputs. Initialize an MultitypeFuncGraph object with name, and use register with input types as the decorator for the function to be registered. And the object can be called with different types of inputs, and work with HyperMap and Map.

参数
  • name (str) – Operator name.

  • read_value (bool) – If the registered function not need to set value on Parameter, and all inputs will pass by value, set read_value to True. Default: False.

引发

ValueError – If failed to find find a matching function for the given arguments.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # `add` is a metagraph object which will add two objects according to
>>> # input type using ".register" decorator.
>>> from mindspore import Tensor
>>> from mindspore import ops
>>> from mindspore import dtype as mstype
>>>
>>> tensor_add = ops.Add()
>>> add = MultitypeFuncGraph('add')
>>> @add.register("Number", "Number")
... def add_scala(x, y):
...     return x + y
>>> @add.register("Tensor", "Tensor")
... def add_tensor(x, y):
...     return tensor_add(x, y)
>>> output = add(1, 2)
>>> print(output)
3
>>> output = add(Tensor([0.1, 0.6, 1.2], dtype=mstype.float32), Tensor([0.1, 0.6, 1.2], dtype=mstype.float32))
>>> print(output)
[0.2 1.2 2.4]
register(*type_names)[源代码]

Register a function for the given type string.

参数

type_names (Union[str, mindspore.dtype]) – Inputs type names or types list.

返回

decorator, a decorator to register the function to run, when called under the types described in type_names.

class tinyms.primitives.GradOperation(get_all=False, get_by_list=False, sens_param=False)[源代码]

A higher-order function which is used to generate the gradient function for the input function.

The gradient function generated by GradOperation higher-order function can be customized by construction arguments.

Given an input function net = Net() that takes x and y as inputs, and has a parameter z, see Net in Examples.

To generate a gradient function that returns gradients with respect to the first input (see GradNetWrtX in Examples).

  1. Construct a GradOperation higher-order function with default arguments: grad_op = GradOperation().

  2. Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).

  3. Call the gradient function with input function’s inputs to get the gradients with respect to the first input: grad_op(net)(x, y).

To generate a gradient function that returns gradients with respect to all inputs (see GradNetWrtXY in Examples).

  1. Construct a GradOperation higher-order function with get_all=True which indicates getting gradients with respect to all inputs, they are x and y in example function Net(): grad_op = GradOperation(get_all=True).

  2. Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).

  3. Call the gradient function with input function’s inputs to get the gradients with respect to all inputs: gradient_function(x, y).

To generate a gradient function that returns gradients with respect to given parameters (see GradNetWithWrtParams in Examples).

  1. Construct a GradOperation higher-order function with get_by_list=True: grad_op = GradOperation(get_by_list=True).

  2. Construct a ParameterTuple that will be passed to the input function when constructing GradOperation higher-order function, it will be used as a parameter filter that determine which gradient to return: params = ParameterTuple(net.trainable_params()).

  3. Call it with input function and params as arguments to get the gradient function: gradient_function = grad_op(net, params).

  4. Call the gradient function with input function’s inputs to get the gradients with respect to given parameters: gradient_function(x, y).

To generate a gradient function that returns gradients with respect to all inputs and given parameters in the format of ((dx, dy), (dz))(see GradNetWrtInputsAndParams in Examples).

  1. Construct a GradOperation higher-order function with get_all=True and get_by_list=True: grad_op = GradOperation(get_all=True, get_by_list=True).

  2. Construct a ParameterTuple that will be passed along input function when constructing GradOperation higher-order function: params = ParameterTuple(net.trainable_params()).

  3. Call it with input function and params as arguments to get the gradient function: gradient_function = grad_op(net, params).

  4. Call the gradient function with input function’s inputs to get the gradients with respect to all inputs and given parameters: gradient_function(x, y).

We can configure the sensitivity(gradient with respect to output) by setting sens_param as True and passing an extra sensitivity input to the gradient function, the sensitivity input should has the same shape and type with input function’s output(see GradNetWrtXYWithSensParam in Examples).

  1. Construct a GradOperation higher-order function with get_all=True and sens_param=True: grad_op = GradOperation(get_all=True, sens_param=True).

  2. Define grad_wrt_output as sens_param which works as the gradient with respect to output: grad_wrt_output = Tensor(np.ones([2, 2]).astype(np.float32)).

  3. Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).

  4. Call the gradient function with input function’s inputs and sens_param to get the gradients with respect to all inputs: gradient_function(x, y, grad_wrt_output).

参数
  • get_all (bool) – If True, get all the gradients with respect to inputs. Default: False.

  • get_by_list (bool) – If True, get all the gradients with respect to Parameter variables. If get_all and get_by_list are both False, get the gradient with respect to first input. If get_all and get_by_list are both True, get the gradients with respect to inputs and Parameter variables at the same time in the form of ((gradients with respect to inputs), (gradients with respect to parameters)). Default: False.

  • sens_param (bool) – Whether to append sensitivity (gradient with respect to output) as input. If sens_param is False, a ‘ones_like(outputs)’ sensitivity will be attached automatically. Default: False. If the sensor_param is True, a sensitivity (gradient with respect to output) needs to be transferred through the location parameter or key-value pair parameter. If the value is transferred through the key-value pair parameter, the key must be sens.

返回

The higher-order function which takes a function as argument and returns gradient function for it.

引发

TypeError – If get_all, get_by_list or sens_param is not a bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import ParameterTuple
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.matmul = P.MatMul()
...         self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')
...     def construct(self, x, y):
...         x = x * self.z
...         out = self.matmul(x, y)
...         return out
...
>>> class GradNetWrtX(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtX, self).__init__()
...         self.net = net
...         self.grad_op = GradOperation()
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net)
...         return gradient_function(x, y)
...
>>> x = Tensor([[0.5, 0.6, 0.4], [1.2, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.01, 0.3, 1.1], [0.1, 0.2, 1.3], [2.1, 1.2, 3.3]], dtype=mstype.float32)
>>> output = GradNetWrtX(Net())(x, y)
>>> print(output)
[[1.4100001 1.5999999 6.6      ]
 [1.4100001 1.5999999 6.6      ]]
>>>
>>> class GradNetWrtXY(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtXY, self).__init__()
...         self.net = net
...         self.grad_op = GradOperation(get_all=True)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net)
...         return gradient_function(x, y)
>>>
>>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWrtXY(Net())(x, y)
>>> print(output)
(Tensor(shape=[2, 3], dtype=Float32, value=
[[ 4.50999975e+00,  2.70000005e+00,  3.60000014e+00],
 [ 4.50999975e+00,  2.70000005e+00,  3.60000014e+00]]), Tensor(shape=[3, 3], dtype=Float32, value=
[[ 2.59999990e+00,  2.59999990e+00,  2.59999990e+00],
 [ 1.89999998e+00,  1.89999998e+00,  1.89999998e+00],
 [ 1.30000007e+00,  1.30000007e+00,  1.30000007e+00]]))
>>>
>>> class GradNetWrtXYWithSensParam(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtXYWithSensParam, self).__init__()
...         self.net = net
...         self.grad_op = GradOperation(get_all=True, sens_param=True)
...         self.grad_wrt_output = Tensor([[0.1, 0.6, 0.2], [0.8, 1.3, 1.1]], dtype=mstype.float32)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net)
...         return gradient_function(x, y, self.grad_wrt_output)
>>>
>>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWrtXYWithSensParam(Net())(x, y)
>>> print(output)
(Tensor(shape=[2, 3], dtype=Float32, value=
[[ 2.21099997e+00,  5.09999990e-01,  1.49000001e+00],
 [ 5.58800030e+00,  2.68000007e+00,  4.07000017e+00]]), Tensor(shape=[3, 3], dtype=Float32, value=
[[ 1.51999998e+00,  2.81999993e+00,  2.14000010e+00],
 [ 1.09999990e+00,  2.04999995e+00,  1.54999995e+00],
 [ 9.00000036e-01,  1.54999995e+00,  1.25000000e+00]]))
>>>
>>> class GradNetWithWrtParams(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWithWrtParams, self).__init__()
...         self.net = net
...         self.params = ParameterTuple(net.trainable_params())
...         self.grad_op = GradOperation(get_by_list=True)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net, self.params)
...         return gradient_function(x, y)
>>>
>>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWithWrtParams(Net())(x, y)
>>> print(output)
(Tensor(shape=[1], dtype=Float32, value= [ 2.15359993e+01]),)
>>>
>>> class GradNetWrtInputsAndParams(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtInputsAndParams, self).__init__()
...         self.net = net
...         self.params = ParameterTuple(net.trainable_params())
...         self.grad_op = GradOperation(get_all=True, get_by_list=True)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net, self.params)
...         return gradient_function(x, y)
>>>
>>> x = Tensor([[0.1, 0.6, 1.2], [0.5, 1.3, 0.1]], dtype=mstype.float32)
>>> y = Tensor([[0.12, 2.3, 1.1], [1.3, 0.2, 2.4], [0.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWrtInputsAndParams(Net())(x, y)
>>> print(output)
((Tensor(shape=[2, 3], dtype=Float32, value=
[[ 3.51999998e+00,  3.90000010e+00,  2.59999990e+00],
 [ 3.51999998e+00,  3.90000010e+00,  2.59999990e+00]]), Tensor(shape=[3, 3], dtype=Float32, value=
[[ 6.00000024e-01,  6.00000024e-01,  6.00000024e-01],
 [ 1.89999998e+00,  1.89999998e+00,  1.89999998e+00],
 [ 1.30000007e+00,  1.30000007e+00,  1.30000007e+00]])), (Tensor(shape=[1], dtype=Float32, value=
 [ 1.29020004e+01]),))
class tinyms.primitives.HyperMap(ops=None, reverse=False)[源代码]

Hypermap will apply the set operation to input sequences.

Apply the operations to every elements of the sequence or nested sequence. Different from Map, the HyperMap supports to apply on nested structure.

参数
  • ops (Union[MultitypeFuncGraph, None]) – ops is the operation to apply. If ops is None, the operations should be put in the first input of the instance. Default is None.

  • reverse (bool) – The optimizer needs to be inverted in some scenarios to improve parallel performance, general users please ignore. reverse is the flag to decide if apply the operation reversely. Only supported in graph mode. Default is False.

Inputs:
  • args (Tuple[sequence]) - If ops is not None, all the inputs should be sequences with the same length. And each row of the sequences will be the inputs of the operation.

    If ops is None, the first input is the operation, and the others are inputs.

Outputs:

Sequence or nested sequence, the sequence of output after applying the function. e.g. operation(args[0][i], args[1][i]).

引发
  • TypeError – If ops is neither MultitypeFuncGraph nor None.

  • TypeError – If args is not a Tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import dtype as mstype
>>> nest_tensor_list = ((Tensor(1, mstype.float32), Tensor(2, mstype.float32)),
...                     (Tensor(3, mstype.float32), Tensor(4, mstype.float32)))
>>> # square all the tensor in the nested list
>>>
>>> square = MultitypeFuncGraph('square')
>>> @square.register("Tensor")
... def square_tensor(x):
...     return ops.square(x)
>>>
>>> common_map = HyperMap()
>>> output = common_map(square, nest_tensor_list)
>>> print(output)
((Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4)),
(Tensor(shape=[], dtype=Float32, value= 9), Tensor(shape=[], dtype=Float32, value= 16)))
>>> square_map = HyperMap(square, False)
>>> output = square_map(nest_tensor_list)
>>> print(output)
((Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4)),
(Tensor(shape=[], dtype=Float32, value= 9), Tensor(shape=[], dtype=Float32, value= 16)))
tinyms.primitives.normal(shape, mean, stddev, seed=None)[源代码]

Generates random numbers according to the Normal (or Gaussian) random number distribution.

参数
  • shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions.

  • mean (Tensor) – The mean μ distribution parameter, which specifies the location of the peak, with data type in [int8, int16, int32, int64, float16, float32].

  • stddev (Tensor) – The deviation σ distribution parameter. It should be greater than 0, with data type in [int8, int16, int32, int64, float16, float32].

  • seed (int) – Seed is used as entropy source for the Random number engines to generate pseudo-random numbers. must be non-negative. Default: None, which will be treated as 0.

返回

Tensor. The shape should be equal to the broadcasted shape between the input shape and shapes of mean and stddev. The dtype is float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> shape = (3, 1, 2)
>>> mean = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> stddev = Tensor(1.0, mindspore.float32)
>>> output = ops.normal(shape, mean, stddev, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 2)
>>> shape = (3, 1, 3)
>>> mean = Tensor(np.array([[3, 4, 3], [3, 5, 6]]), mindspore.float32)
>>> stddev = Tensor(1.0, mindspore.float32)
>>> output = ops.normal(shape, mean, stddev, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 3)
>>> shape = (3, 1, 3)
>>> mean = Tensor(np.array([[1, 2, 3], [3, 4, 3], [3, 5, 6]]), mindspore.float32)
>>> stddev = Tensor(1.0, mindspore.float32)
>>> output = ops.normal(shape, mean, stddev, seed=5)
>>> result = output.shape
>>> print(result)
(3, 3, 3)
tinyms.primitives.laplace(shape, mean, lambda_param, seed=None)[源代码]

Generates random numbers according to the Laplace random number distribution. It is defined as:

\[\text{f}(x;μ,λ) = \frac{1}{2λ}\exp(-\frac{|x-μ|}{λ}),\]
参数
  • shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions.

  • mean (Tensor) – The mean μ distribution parameter, which specifies the location of the peak. With float32 data type.

  • lambda_param (Tensor) – The parameter used for controlling the variance of this random distribution. The variance of Laplace distribution is equal to twice the square of lambda_param. With float32 data type.

  • seed (int) – Seed is used as entropy source for Random number engines generating pseudo-random numbers. Default: None, which will be treated as 0.

返回

Tensor. The shape should be the broadcasted shape of input shape and shapes of mean and lambda_param. The dtype is float32.

Supported Platforms:

Ascend

实际案例

>>> import mindspore
>>> from mindspore import Tensor
>>> from mindspore import ops as ops
>>> shape = (2, 3)
>>> mean = Tensor(1.0, mindspore.float32)
>>> lambda_param = Tensor(1.0, mindspore.float32)
>>> output = ops.laplace(shape, mean, lambda_param, seed=5)
>>> print(output.shape)
(2, 3)
tinyms.primitives.uniform(shape, minval, maxval, seed=None, dtype=mindspore.float32)[源代码]

Generates random numbers according to the Uniform random number distribution.

注解

The number in tensor minval should be strictly less than maxval at any position after broadcasting.

参数
  • shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions and the length of \((N,*)\) should be less than 8 in broadcast operation.

  • minval (Tensor) – The distribution parameter a. It defines the minimum possible generated value, with int32 or float32 data type. If dtype is int32, only one number is allowed.

  • maxval (Tensor) – The distribution parameter b. It defines the maximum possible generated value, with int32 or float32 data type. If dtype is int32, only one number is allowed.

  • seed (int) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative. Default: None, which will be treated as 0.

  • dtype (mindspore.dtype) – Type of the Uniform distribution. If it is int32, it generates numbers from discrete uniform distribution; if it is float32, it generates numbers from continuous uniform distribution. It only supports these two data types. Default: mindspore.float32.

返回

Tensor. The shape should be equal to the broadcasted shape between the input shape and shapes of minval and maxval. The dtype is designated as the input dtype.

引发
  • TypeError – If shape is not tuple.

  • TypeError – If ‘minval’ or ‘maxval’ is neither int32 nor float32 and dtype of ‘minval’ is not the same as ‘maxval’.

  • TypeError – If seed is not an int.

  • TypeError – If ‘dtype’ is neither int32 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> # For discrete uniform distribution, only one number is allowed for both minval and maxval:
>>> shape = (4, 2)
>>> minval = Tensor(1, mindspore.int32)
>>> maxval = Tensor(2, mindspore.int32)
>>> output = ops.uniform(shape, minval, maxval, seed=5, dtype=mindspore.int32)
>>>
>>> # For continuous uniform distribution, minval and maxval can be multi-dimentional:
>>> shape = (3, 1, 2)
>>> minval = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> maxval = Tensor([8.0, 10.0], mindspore.float32)
>>> output = ops.uniform(shape, minval, maxval, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 2)
tinyms.primitives.gamma(shape, alpha, beta, seed=None)[源代码]

Generates random numbers according to the Gamma random number distribution.

参数
  • shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions.

  • alpha (Tensor) – The alpha α distribution parameter. It should be greater than 0 with float32 data type.

  • beta (Tensor) – The beta β distribution parameter. It should be greater than 0 with float32 data type.

  • seed (int) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative. Default: None, which will be treated as 0.

返回

Tensor. The shape should be equal to the broadcasted shape between the input shape and shapes of alpha and beta. The dtype is float32.

引发
  • TypeError – If shape is not a tuple.

  • TypeError – If neither alpha nor beta is a Tensor.

  • TypeError – If seed is not an int.

  • TypeError – If dtype of alpha and beta is not float32.

Supported Platforms:

Ascend

实际案例

>>> # case 1: alpha_shape is (2, 2)
>>> shape = (3, 1, 2)
>>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([1.0]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 2)
>>> # case 2: alpha_shape is (2, 3), so shape is (3, 1, 3)
>>> shape = (3, 1, 3)
>>> alpha = Tensor(np.array([[1, 3, 4], [2, 5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([1.0]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 3)
>>> # case 3: beta_shape is (1, 2), the output is different.
>>> shape = (3, 1, 2)
>>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([1.0, 2]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(output)
[[[ 2.2132034  5.8855834]]
 [ 3.3981476  7.5805717]
[[ 3.3981476  7.5805717]]
 [ 3.7190282 19.941492]
[[ 2.9512358  2.5969937]]
 [ 3.786061   5.160872 ]]]
>>> # case 4: beta_shape is (2, 1), the output is different.
>>> shape = (3, 1, 2)
>>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32)
>>> beta = Tensor(np.array([[1.0], [2.0]]), mindspore.float32)
>>> output = ops.gamma(shape, alpha, beta, seed=5)
>>> result = output.shape
>>> print(output)
[[[ 5.6085486  7.8280783]]
 [ 15.97684  16.116285]
[[ 1.8347423  1.713663]]
 [ 3.2434065 15.667398]
[[ 4.2922077  7.3365674]]
 [ 5.3876944  13.159832 ]]]
tinyms.primitives.poisson(shape, mean, seed=None)[源代码]

Generates random numbers according to the Poisson random number distribution.

\[\text{P}(i|μ) = \frac{\exp(-μ)μ^{i}}{i!}\]
参数
  • shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions.

  • mean (Tensor) – The mean μ distribution parameter. It should be greater than 0 with float32 data type.

  • seed (int) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers and must be non-negative. Default: None, which will be treated as 0.

返回

Tensor. The shape should be equal to the broadcasted shape between the input “shape” and shapes of mean. The dtype is float32.

引发
  • TypeError – If shape is not a tuple.

  • TypeError – If mean is not a Tensor whose dtype is not float32.

  • TypeError – If seed is not an int.

Supported Platforms:

Ascend

实际案例

>>> # case 1: It can be broadcast.
>>> shape = (4, 1)
>>> mean = Tensor(np.array([5.0, 10.0]), mindspore.float32)
>>> output = ops.poisson(shape, mean, seed=5)
>>> result = output.shape
>>> print(result)
(4, 2)
>>> # case 2: It can not be broadcast. It is recommended to use the same shape.
>>> shape = (2, 2)
>>> mean = Tensor(np.array([[5.0, 10.0], [5.0, 1.0]]), mindspore.float32)
>>> output = ops.poisson(shape, mean, seed=5)
>>> result = output.shape
>>> print(result)
(2, 2)
tinyms.primitives.multinomial(inputs, num_sample, replacement=True, seed=None)[源代码]

Returns a tensor sampled from the multinomial probability distribution located in the corresponding row of the input tensor.

注解

The rows of input do not need to sum to one (in which case we use the values as weights), but must be non-negative, finite and have a non-zero sum.

参数
  • x (Tensor) – The input tensor containing probabilities, must be 1 or 2 dimensions, with float32 data type.

  • num_sample (int) – Number of samples to draw.

  • replacement (bool, optional) – Whether to draw with replacement or not, default True.

  • seed (int, optional) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative. Default: 0.

Outputs:

Tensor, has the same rows with input. The number of sampled indices of each row is num_samples. The dtype is float32.

引发
  • TypeError – If x is not a Tensor whose dtype is not float32.

  • TypeError – If num_sample is not an int.

  • TypeError – If seed is neither an int nor a optional.

Supported Platforms:

GPU

实际案例

>>> # case 1: The output is random, and the length of the output is the same as num_sample.
>>> x = Tensor([0, 9, 4, 0], mindspore.float32)
>>> output = ops.multinomial(x, 2)
>>> # print(output)
>>> # [1 2] or [2 1]
>>> # the case where the result is [2 1] in multiple times.
>>> # This is because the value corresponding to the index 1 is larger than the value of the index 2.
>>> print(len(output))
2
>>> # case 2: The output is random, and the length of the output is the same as num_sample.
>>> # replacement is False(Default).
>>> # If the extracted value is 0, the index value of 1 will be returned.
>>> x = Tensor([0, 9, 4, 0], mstype.float32)
>>> output = ops.multinomial(x, 4)
>>> print(output)
[1 1 2 1]
>>> # case 3: num_sample == x_length = 4, and replacement is True, Can extract the same elements。
>>> x = Tensor([0, 9, 4, 0], mstype.float32)
>>> output = ops.multinomial(x, 4, True)
>>> print(output)
[1 1 2 2]
tinyms.primitives.clip_by_value(x, clip_value_min, clip_value_max)[源代码]

Clips tensor values to a specified min and max.

Limits the value of \(x\) to a range, whose lower limit is ‘clip_value_min’ and upper limit is ‘clip_value_max’.

\[\begin{split}out_i= \left\{ \begin{array}{align} clip\_value_{max} & \text{ if } x_i\ge clip\_value_{max} \\ x_i & \text{ if } clip\_value_{min} \lt x_i \lt clip\_value_{max} \\ clip\_value_{min} & \text{ if } x_i \le clip\_value_{min} \\ \end{array}\right.\end{split}\]

注解

‘clip_value_min’ needs to be less than or equal to ‘clip_value_max’.

参数
  • x (Tensor) – Input data. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

  • clip_value_min (Tensor) – The minimum value.

  • clip_value_max (Tensor) – The maximum value.

返回

Tensor, a clipped Tensor. It has the same shape and data type as x.

Supported Platforms:

Ascend GPU

实际案例

>>> min_value = Tensor(5, mindspore.float32)
>>> max_value = Tensor(20, mindspore.float32)
>>> x = Tensor(np.array([[1., 25., 5., 7.], [4., 11., 6., 21.]]), mindspore.float32)
>>> output = ops.clip_by_value(x, min_value, max_value)
>>> print(output)
[[ 5. 20.  5.  7.]
 [ 5. 11.  6. 20.]]
tinyms.primitives.clip_by_global_norm(x, clip_norm=1.0, use_norm=None)[源代码]

Clips tensor values by the ratio of the sum of their norms.

注解

Input ‘x’ should be a tuple or list of tensors. Otherwise, it will raise an error.

参数
  • x (Union(tuple[Tensor], list[Tensor])) – Input data to clip. The shape of each Tensor in tuple is \((N,*)\) where \(*\) means, any number of additional dimensions.

  • clip_norm (Union(float, int)) – The clipping ratio, it should be greater than 0. Default: 1.0

  • use_norm (None) – The global norm. Default: None. Currently only none is supported.

返回

tuple[Tensor], a clipped Tensor. It has the same data type as x and each Tensor in the output tuple is the same as the original input shape.

Supported Platforms:

Ascend GPU

实际案例

>>> x1 = np.array([[2., 3.], [1., 2.]]).astype(np.float32)
>>> x2 = np.array([[1., 4.], [3., 1.]]).astype(np.float32)
>>> input_x = (Tensor(x1), Tensor(x2))
>>> out = ops.clip_by_global_norm(input_x, 1.0)
>>> print(out)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 2.98142403e-01,  4.47213590e-01],
 [ 1.49071202e-01,  2.98142403e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 1.49071202e-01,  5.96284807e-01],
 [ 4.47213590e-01,  1.49071202e-01]]))
tinyms.primitives.count_nonzero(x, axis=(), keep_dims=False, dtype=mindspore.int32)[源代码]

Count number of nonzero elements across axis of input tensor

参数
  • x (Tensor) – Input data is used to count non-zero numbers. \((N,*)\) where \(*\) means, any number of additional dimensions.

  • axis (Union[int, tuple(int), list(int)]) – The dimensions to reduce. Only constant value is allowed. Default: (), reduce all dimensions.

  • keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default: False.

  • dtype (Union[Number, mindspore.bool_]) – The data type of the output tensor. Only constant value is allowed. Default: mindspore.int32

返回

Tensor, number of nonzero element. The data type is dtype.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1: each value specified.
>>> x = Tensor(np.array([[0, 1, 0], [1, 1, 0]]).astype(np.float32))
>>> nonzero_num = ops.count_nonzero(x=x, axis=[0, 1], keep_dims=True, dtype=mindspore.int32)
>>> print(nonzero_num)
[[3]]
>>> # case 2: all value is default.
>>> nonzero_num = ops.count_nonzero(x=x)
>>> print(nonzero_num)
3
>>> # case 3: axis value was specified 0.
>>> nonzero_num = ops.count_nonzero(x=x, axis=[0,])
>>> print(nonzero_num)
[1 2 0]
>>> # case 4: axis value was specified 1.
>>> nonzero_num = ops.count_nonzero(x=x, axis=[1,])
>>> print(nonzero_num)
[1 2]
>>> # case 5: keep_dims value was specified.
>>> nonzero_num = ops.count_nonzero(x=x,  keep_dims=True)
>>> print(nonzero_num)
[[3]]
>>> # case 6: keep_dims and axis value was specified.
>>> nonzero_num = ops.count_nonzero(x=x, axis=[0,], keep_dims=True)
>>> print(nonzero_num)
[[1 2 0]]
tinyms.primitives.tensor_dot(x1, x2, axes, prim_name='tensor_dot')[源代码]

Computation of Tensor contraction on arbitrary axes between tensors a and b.

Contraction allows for the summation of products of elements of a and b on specified axes. The same number of axes must be specified for both x1 and x2, and values must be within range of number of dims of both a and b.

Selected dims in both inputs must also match.

axes = 0 leads to outer product axes = 1 leads to normal matrix multiplication when inputs both 2D. axes = 1 is the same as axes = ((1,),(0,) where both a and b are 2D. axes = 2 is the same as axes = ((1,2),(0,1)) where both a and b are 3D.

Inputs:
  • x1 (Tensor) - First tensor in tensor_dot with datatype float16 or float32

  • x2 (Tensor) - Second tensor in tensor_dot with datatype float16 or float32

  • axes (Union[int, tuple(int), tuple(tuple(int)), list(list(int))]) - Single value or tuple/list of length 2 with dimensions specified for a and b each. If single value N passed, automatically picks up last N dims from a input shape and first N dims from b input shape in order as axes for each respectively.

Outputs:

Tensor, the shape of the output tensor is \((N + M)\). Where \(N\) and \(M\) are the free axes not contracted in both inputs

引发
  • TypeError – If x1 or x2 is not a Tensor.

  • TypeError – If axes is not one of the following: int, tuple, list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x1 = Tensor(np.ones(shape=[1, 2, 3]), mindspore.float32)
>>> input_x2 = Tensor(np.ones(shape=[3, 1, 2]), mindspore.float32)
>>> output = ops.tensor_dot(input_x1, input_x2, ((0,1),(1,2)))
>>> print(output)
[[2. 2. 2]
 [2. 2. 2]
 [2. 2. 2]]
tinyms.primitives.dot(x1, x2, prim_name=None)[源代码]

Computation a dot product between samples in two tensors.

Inputs:
  • x1 (Tensor) - First tensor in Dot op with datatype float16 or float32 The rank must be greater than or equal to 2.

  • x2 (Tensor) - Second tensor in Dot op with datatype float16 or float32 The rank must be greater than or equal to 2.

Outputs:

Tensor, dot product of x1 and x2.

引发
  • TypeError – If type of x1 and x2 are not the same.

  • TypeError – If dtype of x1 or x2 is not float16 or float32.

  • ValueError – If rank of x1 or x2 less than 2.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x1 = Tensor(np.ones(shape=[2, 3]), mindspore.float32)
>>> input_x2 = Tensor(np.ones(shape=[1, 3, 2]), mindspore.float32)
>>> output = ops.dot(input_x1, input_x2)
>>> print(output)
[[[3. 3.]]
 [[3. 3.]]]
>>> print(output.shape)
(2, 1, 2)
>>> input_x1 = Tensor(np.ones(shape=[1, 2, 3]), mindspore.float32)
>>> input_x2 = Tensor(np.ones(shape=[1, 3, 2]), mindspore.float32)
>>> output = ops.dot(input_x1, input_x2)
>>> print(output)
[[[[3. 3.]]
  [[3. 3.]]]]
>>> print(output.shape)
(1, 2, 1, 2)
>>> input_x1 = Tensor(np.ones(shape=[1, 2, 3]), mindspore.float32)
>>> input_x2 = Tensor(np.ones(shape=[2, 3, 2]), mindspore.float32)
>>> output = ops.dot(input_x1, input_x2)
>>> print(output)
[[[[3. 3.]
   [3. 3.]]
  [[3. 3.]
   [3. 3.]]]]
>>> print(output.shape)
(1, 2, 2, 2)
>>> input_x1 = Tensor(np.ones(shape=[3, 2, 3]), mindspore.float32)
>>> input_x2 = Tensor(np.ones(shape=[2, 1, 3, 2]), mindspore.float32)
>>> output = ops.dot(input_x1, input_x2)
>>> print(output)
[[[[[3. 3.]]
   [[3. 3.]]]
  [[[3. 3.]]
   [[3. 3.]]]]
 [[[[3. 3.]]
   [[3. 3.]]]
  [[[3. 3.]]
   [[3. 3.]]]]
 [[[[3. 3.]]
   [[3. 3.]]]
  [[[3. 3.]]
   [[3. 3.]]]]]
>>> print(output.shape)
(3, 2, 2, 1, 2)
tinyms.primitives.batch_dot(x1, x2, axes=None, prim_name=None)[源代码]

Computation of batch dot product between samples in two tensors containing batch dims.

\[output = x1[batch, :] * x2[batch, :]\]
Inputs:
  • x1 (Tensor) - First tensor in Batch Dot op with datatype float32 and the rank of x1 must be greater than or equal to 2.

  • x2 (Tensor) - Second tensor in Batch Dot op with datatype float32. The datatype of x2 should be same as x1 and the rank of x2 must be greater than or equal to 2.

  • axes (Union[int, tuple(int), list(int)]) - Single value or tuple/list of length 2 with dimensions specified for a and b each. If single value N passed, automatically picks up last N dims from a input shape and last N dimensions from b input shape in order as axes for each respectively. Default: None.

Outputs:

Tensor, batch dot product of x1 and x2.For example: The Shape of output for input x1 shapes (batch, d1, axes, d2) and x2 shapes (batch, d3, axes, d4) is (batch, d1, d2, d3, d4), where d1 and d2 means any number.

引发
  • TypeError – If type of x1 and x2 are not the same.

  • TypeError – If dtype of x1 or x2 is not float32.

  • ValueError – If rank of x1 or x2 less than 2.

  • ValueError – If batch dim used in axes.

  • ValueError – If len(axes) less than 2.

  • ValueError – If axes is not one of those: None, int, (int, int).

  • ValueError – If axes reversed from negative int is too low for dimensions of input arrays.

  • ValueError – If axes value is too high for dimensions of input arrays.

  • ValueError – If batch size of x1 and x2 are not the same.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x1 = Tensor(np.ones(shape=[2, 2, 3]), mindspore.float32)
>>> x2 = Tensor(np.ones(shape=[2, 3, 2]), mindspore.float32)
>>> axes = (-1, -2)
>>> output = ops.batch_dot(x1, x2, axes)
>>> print(output)
[[[3. 3.]
  [3. 3.]]
 [[3. 3.]
  [3. 3.]]]
>>> x1 = Tensor(np.ones(shape=[2, 2]), mindspore.float32)
>>> x2 = Tensor(np.ones(shape=[2, 3, 2]), mindspore.float32)
>>> axes = (1, 2)
>>> output = ops.batch_dot(x1, x2, axes)
>>> print(output)
[[2. 2. 2.]
 [2. 2. 2.]]
>>> print(output.shape)
(2, 3)
>>> x1 = Tensor(np.ones(shape=[6, 2, 3, 4]), mindspore.float32)
>>> x2 = Tensor(np.ones(shape=[6, 5, 4, 8]), mindspore.float32)
>>> output = ops.batch_dot(x1, x2)
>>> print(output.shape)
(6, 2, 3, 5, 8)
>>> x1 = Tensor(np.ones(shape=[2, 2, 4]), mindspore.float32)
>>> x2 = Tensor(np.ones(shape=[2, 5, 4, 5]), mindspore.float32)
>>> output = ops.batch_dot(x1, x2)
>>> print(output.shape)
(2, 2, 5, 5)
tinyms.primitives.repeat_elements(x, rep, axis=0)[源代码]

Repeat elements of a tensor along an axis, like np.repeat.

参数
  • x (Tensor) – The tensor to repeat values for. Must be of type: float16, float32, int8, uint8, int16, int32, or int64.

  • rep (int) – The number of times to repeat, must be positive, required.

  • axis (int) – The axis along which to repeat, default 0.

Outputs:

One tensor with values repeated along the specified axis. If x has shape (s1, s2, …, sn) and axis is i, the output will have shape (s1, s2, …, si * rep, …, sn). The output type will be the same as the type of x.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1 : repeat on axis 0
>>> x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32)
>>> output = ops.repeat_elements(x, rep = 2, axis = 0)
>>> print(output)
[[0 1 2]
 [0 1 2]
 [3 4 5]
 [3 4 5]]
>>> # case 2 : repeat on axis 1
>>> x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32)
>>> output = ops.repeat_elements(x, rep = 2, axis = 1)
>>> print(output)
[[0 0 1 1 2 2]
 [3 3 4 4 5 5]]
tinyms.primitives.sequence_mask(lengths, maxlen=None, prim_name='sequence_mask')[源代码]

Returns a mask tensor representing the first N positions of each cell.

If lengths has shape [d_1, d_2, …, d_n], then the resulting tensor mask has type dtype and shape [d_1, d_2, …, d_n, maxlen], with mask[i_1, i_2, …, i_n, j] = (j < lengths[i_1, i_2, …, i_n])

Inputs:
  • lengths (Tensor) - Tensor to calculate the mask for. All values in this tensor should be less than or equal to maxlen. Values greater than maxlen will be treated as maxlen. Must be type int32 or int64.

  • maxlen (int) - size of the last dimension of returned tensor. Must be positive and same type as elements in lengths. Default is None.

Outputs:

One mask tensor of shape lengths.shape + (maxlen,).

引发
  • TypeError – If lengths is not a Tensor.

  • TypeError – If maxlen is not an int.

  • TypeError – If dtype of lengths is neither int32 nor int64.

Supported Platforms:

GPU

实际案例

>>> # case 1: When maxlen is assigned
>>> x = Tensor(np.array([1, 2, 3, 4]))
>>> output = ops.sequence_mask(x, 5)
>>> print(output)
[[ True False False False False]
 [ True  True False False False]
 [ True  True  True False False]
 [ True  True  True  True False]]
>>> # case 2: When there is 0 in x
>>> x = Tensor(np.array([[1, 3], [2, 0]]))
>>> output = ops.sequence_mask(x, 5)
>>> print(output)
[[[ True False False False False]
  [ True  True  True False False]]
 [[ True  True False False False]
  [False False False False False]]]
>>> # case 3: when the maxlen is not assigned
>>> x = Tensor(np.array([[1, 3], [2, 4]]))
>>> output = ops.sequence_mask(x)
>>> print(output)
[[[ True False False False ]
  [ True  True  True False ]]
 [[ True  True False False ]
  [ True  True  True  True ]]]
tinyms.primitives.matmul(x1, x2, dtype=None, prim_name=None)[源代码]

Returns the matrix product of two arrays.

注解

Numpy arguments out, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16 and np.float32. On CPU, the supported dtypes are np.float16 and np.float32.

参数
  • x1 (Tensor) – Input tensor, scalar not allowed. The last dimension of x1 must be the same size as the second last dimension of x2. And the shape of x1 and x2 could be broadcast.

  • x2 (Tensor) – Input tensor, scalar not allowed. The last dimension of x1 must be the same size as the second last dimension of x2. And the shape of x1 and x2 could be broadcast.

  • dtype (mindspore.dtype, optional) – defaults to None. Overrides the dtype of the output Tensor.

返回

Tensor or scalar, the matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors.

引发
  • ValueError – If the last dimension of x1 is not the same size as the second-to-last dimension of x2, or if a scalar value is passed in.

  • ValueError – If the shape of x1 and x2 could not broadcast together。

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1 : Reasonable application of broadcast mechanism
>>> x1 = Tensor(np.arange(2*3*4).reshape(2, 3, 4), mindspore.float32)
>>> x2 = Tensor(np.arange(4*5).reshape(4, 5), mindspore.float32)
>>> output = ops.matmul(x1, x2)
>>> print(output)
[[[  70.   76.   82.   88.   94.]
[ 190.  212.  234.  256.  278.]
[ 310.  348.  386.  424.  462.]]
[[ 430.  484.  538.  592.  646.]
[ 550.  620.  690.  760.  830.]
[ 670.  756.  842.  928. 1014.]]]
>>> print(output.shape)
(2, 3, 5)
>>> # case 2 : the rank of `x1` is 1
>>> x1 = Tensor(np.ones([1, 2]), mindspore.float32)
>>> x2 = Tensor(np.ones([2,]), mindspore.float32)
>>> output = ops.matmul(x1, x2)
>>> print(output)
[2.]
>>> print(output.shape)
(1,)
class tinyms.primitives.ACos(*args, **kwargs)[源代码]

Computes arccosine of input tensors element-wise.

\[out_i = cos^{-1}(x_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is

    \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> acos = ops.ACos()
>>> x = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32)
>>> output = acos(x)
>>> print(output)
[0.7377037 1.5307858 1.2661037 0.97641146]
class tinyms.primitives.Abs(*args, **kwargs)[源代码]

Returns absolute value of a tensor element-wise.

\[out_i = |x_i|\]
Inputs:
  • x (Tensor) - The input tensor. The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-1.0, 1.0, 0.0]), mindspore.float32)
>>> abs = ops.Abs()
>>> output = abs(x)
>>> print(output)
[1. 1. 0.]
class tinyms.primitives.AccumulateNV2(*args, **kwargs)[源代码]

Computes accumulation of all input tensors element-wise.

AccumulateNV2 is similar to AddN, but there is a significant difference among them: AccumulateNV2 will not wait for all of its inputs to be ready before summing. That is to say, AccumulateNV2 is able to save memory when inputs are ready at different time since the minimum temporary storage is proportional to the output size rather than the input size.

Inputs:
  • x (Union(tuple[Tensor], list[Tensor])) - The input tuple or list is made up of multiple tensors whose dtype is number to be added together.

Outputs:

Tensor, has the same shape and dtype as each entry of the x.

引发

TypeError – If x is neither tuple nor list.

Supported Platforms:

Ascend

实际案例

>>> class NetAccumulateNV2(nn.Cell):
...     def __init__(self):
...         super(NetAccumulateNV2, self).__init__()
...         self.accumulateNV2 = ops.AccumulateNV2()
...
...     def construct(self, *z):
...         return self.accumulateNV2(z)
...
>>> net = NetAccumulateNV2()
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> y = Tensor(np.array([4, 5, 6]), mindspore.float32)
>>> output = net(x, y, x, y)
>>> print(output)
[10. 14. 18.]
class tinyms.primitives.Acosh(*args, **kwargs)[源代码]

Computes inverse hyperbolic cosine of the inputs element-wise.

\[out_i = \cosh^{-1}(input_i)\]

警告

Given an input tensor x, the function computes inverse hyperbolic cosine of every element. Input range is [1, inf].

Inputs:
  • x (Tensor) - The data type should be one of the following types: float16, float32. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape and type as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> import mindspore.ops as ops
>>> from mindspore import Tensor, dtype
>>> acosh = ops.Acosh()
>>> x = Tensor(np.array([1.0, 1.5, 3.0, 100.0]), dtype.float32)
>>> output = acosh(x)
>>> print(output)
[0. 0.9624236 1.7627472 5.298292]
class tinyms.primitives.Adam(*args, **kwargs)[源代码]

Updates gradients by the Adaptive Moment Estimation (Adam) algorithm.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

For more details, please refer to nn.Adam.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w = w - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]

\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t(\beta_1^{t})\) and \(beta_2^t(\beta_2^{t})\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents var, \(\epsilon\) represents epsilon.

参数
  • use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

  • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.

Inputs:
  • var (Tensor) - Weights to be updated. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions. The data type can be float16 or float32.

  • m (Tensor) - The 1st moment vector in the updating formula, the shape and data type value should be the same as var.

  • v (Tensor) - the 2nd moment vector in the updating formula, the shape and data type value should be the same as var. Mean square gradients with the same type as var.

  • beta1_power (float) - \(beta_1^t(\beta_1^{t})\) in the updating formula, the data type value should be the same as var.

  • beta2_power (float) - \(beta_2^t(\beta_2^{t})\) in the updating formula, the data type value should be the same as var.

  • lr (float) - \(l\) in the updating formula. The paper suggested value is \(10^{-8}\), the data type value should be the same as var.

  • beta1 (float) - The exponential decay rate for the 1st moment estimations, the data type value should be the same as var. The paper suggested value is \(0.9\)

  • beta2 (float) - The exponential decay rate for the 2nd moment estimations, the data type value should be the same as var. The paper suggested value is \(0.999\)

  • epsilon (float) - Term added to the denominator to improve numerical stability.

  • gradient (Tensor) - Gradient, has the same shape and data type as var.

Outputs:

Tuple of 3 Tensor, the updated parameters.

  • var (Tensor) - The same shape and data type as Inputs var.

  • m (Tensor) - The same shape and data type as Inputs m.

  • v (Tensor) - The same shape and data type as Inputs v.

引发
  • TypeError – If neither use_locking nor use_nesterov is a bool.

  • TypeError – If var, m or v is not a Tensor.

  • TypeError – If beta1_power, beta2_power1, lr, beta1, beta2, epsilon or gradient is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_adam = ops.Adam()
...         self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var")
...         self.m = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="m")
...         self.v = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="v")
...     def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad):
...         out = self.apply_adam(self.var, self.m, self.v, beta1_power, beta2_power, lr, beta1, beta2,
...                               epsilon, grad)
...         return out
...
>>> net = Net()
>>> gradient = Tensor(np.ones([2, 2]).astype(np.float32))
>>> output = net(0.9, 0.999, 0.001, 0.9, 0.999, 1e-8, gradient)
>>> print(net.var.asnumpy())
[[0.9996838 0.9996838]
 [0.9996838 0.9996838]]
class tinyms.primitives.AdamNoUpdateParam(*args, **kwargs)[源代码]

Updates gradients by Adaptive Moment Estimation (Adam) algorithm. This operator do not update the parameter, but calculate the value that should be added to the parameter instead.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ \Delta{w} = - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]

\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t(\beta_1^{t})\) and \(beta_2^t(\beta_2^{t})\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents the parameter to be updated, \(\epsilon\) represents epsilon.

参数
  • use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

  • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.

Inputs:
  • m (Tensor) - The 1st moment vector in the updating formula. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions. The data type must be float32.

  • v (Tensor) - the 2nd moment vector in the updating formula. The shape must be the same as m. The data type must be float32.

  • beta1_power (Tensor) - \(beta_1^t(\beta_1^{t})\) in the updating formula. The shape is \((1, )\) and the data type must be float32.

  • beta2_power (Tensor) - \(beta_2^t(\beta_1^{t})\) in the updating formula. The shape is \((1, )\) and the data type must be float32.

  • lr (Tensor) - \(l\) in the updating formula. The shape is \((1, )\) and the data type must be float32.

  • beta1 (Tensor) - The exponential decay rate for the 1st moment estimations. The shape is \((1, )\) and the data type must be float32.

  • beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations. The shape is \((1, )\) and the data type must be float32.

  • epsilon (Tensor) - Term added to the denominator to improve numerical stability. The shape is \((1, )\) and the data type must be float32.

  • gradient (Tensor) - Gradient, the shape must be the same as m, the data type must be float32.

Outputs:

Tensor, whose shape and data type are the same with Inputs gradient, is a value that should be added to the parameter to be updated.

引发
  • TypeError – If neither use_locking nor use_nesterov is a bool.

  • TypeError – If m, v, beta1_power, beta2_power1, lr, beta1, beta2, epsilon or gradient is not a Tensor.

Supported Platforms:

CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.adam = ops.AdamNoUpdateParam()
...         self.m = Parameter(Tensor(np.array([[0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]).astype(np.float32)),
...                            name="m")
...         self.v = Parameter(Tensor(np.array([[0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]).astype(np.float32)),
...                            name="v")
...     def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad):
...         out = self.adam(self.m, self.v, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad)
...         return out
>>> net = Net()
>>> beta1_power = Tensor(0.9, ms.float32)
>>> beta2_power = Tensor(0.999, ms.float32)
>>> lr = Tensor(0.001, ms.float32)
>>> beta1 = Tensor(0.9, ms.float32)
>>> beta2 = Tensor(0.999, ms.float32)
>>> epsilon = Tensor(1e-8, ms.float32)
>>> gradient = Tensor(np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1]]).astype(np.float32))
>>> result = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient)
>>> print(result)
[[-0.00010004 -0.00010004 -0.00010004]
[-0.00013441 -0.00013441 -0.00013441]]
class tinyms.primitives.AdamWeightDecay(*args, **kwargs)[源代码]

Updates gradients by the Adaptive Moment Estimation (AdamWeightDecay) algorithm with weight decay.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization. The AdamWeightDecay variant was proposed in Decoupled Weight Decay Regularization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ update = \frac{m}{\sqrt{v} + eps} \\ update = \begin{cases} update + weight\_decay * w & \text{ if } weight\_decay > 0 \\ update & \text{ otherwise } \end{cases} \\ w = w - lr * update \end{array}\end{split}\]

\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(\beta_1, \beta_2\) represent beta1 and beta2, \(lr\) represents learning_rate, \(w\) represents var, \(decay\) represents weight_decay, \(\epsilon\) represents epsilon.

参数

use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

Inputs:
  • var (Tensor) - Weights to be updated. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions. The data type can be float16 or float32.

  • m (Tensor) - The 1st moment vector in the updating formula, the shape and data type value should be the same as var.

  • v (Tensor) - the 2nd moment vector in the updating formula, the shape and data type value should be the same as var. Mean square gradients with the same type as var.

  • lr (float) - \(l\) in the updating formula. The paper suggested value is \(10^{-8}\), the data type value should be the same as var.

  • beta1 (float) - The exponential decay rate for the 1st moment estimations, the data type value should be the same as var. The paper suggested value is \(0.9\)

  • beta2 (float) - The exponential decay rate for the 2nd moment estimations, the data type value should be the same as var. The paper suggested value is \(0.999\)

  • epsilon (float) - Term added to the denominator to improve numerical stability.

  • decay (float) - The weight decay value, must be a scalar tensor with float data type. Default: 0.0.

  • gradient (Tensor) - Gradient, has the same shape and data type as var.

Outputs:

Tuple of 3 Tensor, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • m (Tensor) - The same shape and data type as m.

  • v (Tensor) - The same shape and data type as v.

Supported Platforms:

GPU CPU

实际案例

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor, Parameter, ops
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.adam_weight_decay = ops.AdamWeightDecay()
...         self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var")
...         self.m = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="m")
...         self.v = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="v")
...     def construct(self, lr, beta1, beta2, epsilon, decay, grad):
...         out = self.adam_weight_decay(self.var, self.m, self.v, lr, beta1, beta2,
...                               epsilon, decay, grad)
...         return out
>>> net = Net()
>>> gradient = Tensor(np.ones([2, 2]).astype(np.float32))
>>> output = net(0.001, 0.9, 0.999, 1e-8, 0.0, gradient)
>>> print(net.var.asnumpy())
class tinyms.primitives.AdaptiveAvgPool2D(*args, **kwargs)[源代码]

AdaptiveAvgPool2D operation.

This operator applies a 2D adaptive average pooling to an input signal composed of multiple input planes. That is, for any input size, the size of the specified output is H x W. The number of output features is equal to the number of input planes.

参数

output_size (Union[int, tuple]) – The target output size is H x W. ouput_size can be a tuple, or a single H for H x H, and H and W can be int or None which means the output size is the same as the input.

Inputs:
  • input_x (Tensor) - The input of AdaptiveAvgPool2D, which is a 3D or 4D tensor, with float16, float32, float64 data type.

Outputs:

Tensor, with the same type as the input_x.

Shape of the output is input_x_shape[:len(input_x_shape) - len(out_shape)] + out_shape.

If output_size contains None:

  • out_shape = input_x_shape[-2] + output_size[1]: If output_size is (None, w)

  • out_shape = output_size[0] + input_x_shape[-1]: If output_size is (h, None)

  • out_shape = input_x_shape[-2:]: If output_size is (None, None)

If output_size does not contain None:

  • out_shape = (h, h): If output_size is h

  • out_shape = (h, w): If output_size is (h, w)

引发
  • ValueError – If output_size is a tuple and if output_size length is not 2.

  • TypeError – If input_x is not a tensor.

  • TypeError – If dtype of input_x is not float16, float32, float64.

  • ValueError – If input_x dimension is less than or equal to output_size dimension.

Supported Platforms:

GPU

实际案例

>>> # case 1: output_size=(None, 2)
>>> input_x = Tensor(np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
...                            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
...                            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]), mindspore.float32)
>>> adaptive_avg_pool_2d = ops.AdaptiveAvgPool2D((None, 2))
>>> output = adaptive_avg_pool_2d(input_x)
>>> print(output)
[[[1.5 2.5]
  [4.5 5.5]
  [7.5 8.5]]
 [[1.5 2.5]
  [4.5 5.5]
  [7.5 8.5]]
 [[1.5 2.5]
  [4.5 5.5]
  [7.5 8.5]]]
>>> # case 2: output_size=2
>>> adaptive_avg_pool_2d = ops.AdaptiveAvgPool2D(2)
>>> output = adaptive_avg_pool_2d(input_x)
>>> print(output)
[[[3. 4.]
  [6. 7.]]
 [[3. 4.]
  [6. 7.]]
 [[3. 4.]
  [6. 7.]]]
>>> # case 3: output_size=(1, 2)
>>> adaptive_avg_pool_2d = ops.AdaptiveAvgPool2D((1, 2))
>>> output = adaptive_avg_pool_2d(input_x)
>>> print(output)
[[[4.5 5.5]]
 [[4.5 5.5]]
 [[4.5 5.5]]]
class tinyms.primitives.Add(*args, **kwargs)[源代码]

Adds two input tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = x_{i} + y_{i}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number, or a bool, or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If x and y is not one of the following: Tensor, Number, bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1: x and y are both Tensor.
>>> add = ops.Add()
>>> x = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> y = Tensor(np.array([4, 5, 6]).astype(np.float32))
>>> output = add(x, y)
>>> print(output)
[5. 7. 9.]
>>> # case 2: x is a scalar and y is a Tensor
>>> add = ops.Add()
>>> x = Tensor(1, mindspore.int32)
>>> y = Tensor(np.array([4, 5, 6]).astype(np.float32))
>>> output = add(x, y)
>>> print(output)
[5. 6. 7.]
>>> # the data type of x is int32, the data type of y is float32,
>>> # and the output is the data format of higher precision flost32.
>>> print(output.dtype)
Float32
class tinyms.primitives.AddN(*args, **kwargs)[源代码]

Computes addition of all input tensors element-wise.

All input tensors must have the same shape.

Inputs:
  • x (Union(tuple[Tensor], list[Tensor])) - The input tuple or list is made up of multiple tensors whose dtype is number or bool to be added together.

Outputs:

Tensor, has the same shape and dtype as each entry of the x.

引发

TypeError – If x is neither tuple nor list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class NetAddN(nn.Cell):
...     def __init__(self):
...         super(NetAddN, self).__init__()
...         self.addN = ops.AddN()
...
...     def construct(self, *z):
...         return self.addN(z)
...
>>> net = NetAddN()
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> y = Tensor(np.array([4, 5, 6]), mindspore.float32)
>>> output = net(x, y, x, y)
>>> print(output)
[10. 14. 18.]
class tinyms.primitives.AllGather(*args, **kwargs)[源代码]

Gathers tensors from the specified communication group.

注解

The tensors must have the same shape and format in all processes of the collection.

参数

group (str) – The communication group to work on. Default: “hccl_world_group”.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

Tensor. If the number of devices in the group is N, then the shape of output is \((N, x_1, x_2, ..., x_R)\).

引发
  • TypeError – If group is not a str.

  • ValueError – If the local rank id of the calling process in the group is larger than the group’s rank size.

Supported Platforms:

Ascend GPU

实际案例

>>> # This example should be run with two devices. Refer to the tutorial > Distributed Training on mindspore.cn
>>> import numpy as np
>>> import mindspore.ops as ops
>>> import mindspore.nn as nn
>>> from mindspore.communication import init
>>> from mindspore import Tensor, context
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
... class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.allgather = ops.AllGather()
...
...     def construct(self, x):
...         return self.allgather(x)
...
>>> input_x = Tensor(np.ones([2, 8]).astype(np.float32))
>>> net = Net()
>>> output = net(input_x)
>>> print(output)
[[1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1.]]
class tinyms.primitives.AllReduce(*args, **kwargs)[源代码]

Reduces the tensor data across all devices in such a way that all devices will get the same final result.

注解

The operation of AllReduce does not support “prod” currently. The tensors must have the same shape and format in all processes of the collection.

参数
  • op (str) – Specifies an operation used for element-wise reductions, like sum, max, and min. Default: ReduceOp.SUM.

  • group (str) – The communication group to work on. Default: “hccl_world_group”.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

Tensor, has the same shape of the input, i.e., \((x_1, x_2, ..., x_R)\). The contents depend on the specified operation.

引发
  • TypeError – If any of op and group is not a str, or fusion is not an integer, or the input’s dtype is bool.

  • ValueError – If the op is “prod”.

Supported Platforms:

Ascend GPU

实际案例

>>> # This example should be run with two devices. Refer to the tutorial > Distributed Training on mindspore.cn
>>> import numpy as np
>>> from mindspore.communication import init
>>> from mindspore import Tensor
>>> from mindspore.ops import ReduceOp
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>> init()
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.allreduce_sum = ops.AllReduce(ReduceOp.SUM)
...
...     def construct(self, x):
...         return self.allreduce_sum(x)
...
>>> input_ = Tensor(np.ones([2, 8]).astype(np.float32))
>>> net = Net()
>>> output = net(input_)
>>> print(output)
[[2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]]
class tinyms.primitives.AllSwap(*args, **kwargs)[源代码]

AllSwap is a collective operation.

AllSwap sends data from the all processes to the all processes in the specified group. It has two phases:

  • The scatter phase: On each process, the operand is split into the send size of blocks along the 0-th axis, and the blocks are scattered to all processes, e.g., the ith block is send to the ith process.

  • The gather phase: Each process concatenates the received blocks along the 0-th axis.

注解

The tensors must have the same format in all processes of the collection.

参数

group (str) – The communication group name.

Inputs:

tensor_in (tensor): A 2-D tensor. On each process, divide blocks into number of the send size. send_size (tensor): A 1-D int64 tensor. The element is the send data size for each process. recv_size (tensor): A 1-D int64 tensor. The element is the receive data size for each process.

返回

The result tensor.

返回类型

tensor_out (tensor)

引发

TypeError – If group is not a string.

class tinyms.primitives.AngleAtomEnergy(*args, **kwargs)[源代码]

Add the potential energy caused by angle terms to the total potential energy of each atom. Assume the number of angles is m and the number of atoms is n.

The calculation formula is the same as operator AngleEnergy().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数

angle_numbers (int32) – the number of angles m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd and the central atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tensor) - The 3rd atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • angle_k (Tensor) - The force constant for each angle. The data type is float32 and the shape is \((m,)\).

  • angle_theta0 (Tensor) - The equilibrium position value for each angle. The data type is float32 and the shape is \((m,)\).

Outputs:
  • ene (Tensor) - The accumulated potential energy for each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.AngleEnergy(*args, **kwargs)[源代码]

Calculate the energy caused by 3-atoms angle term. Assume the number of angles is m and the number of atoms is n.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr_{ab} = (x_b-x_a, y_b-y_a, z_b-z_a)\]
\[dr_{cb} = (x_b-x_c, y_b-y_c, z_b-z_c)\]
\[theta = arccos(inner_product(dr_{ab}, dr_{cb})/|dr_{ab}|/|dr_{cb}|)\]
\[E = k*(theta - theta_0)^2\]
参数

angle_numbers (int32) – the number of angles m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd and the central atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tensor) - The 3rd atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • angle_k (Tensor) - The force constant for each angle. The data type is float32 and the shape is \((m,)\).

  • angle_theta0 (Tensor) - The equilibrium position value for each angle. The data type is float32 and the shape is \((m,)\).

Outputs:
  • ene (Tensor) - The potential energy for each angle term. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.AngleForce(*args, **kwargs)[源代码]

Calculate the force exerted by angles made of 3 atoms on the corresponding atoms. Assume the number of angles is m and the number of atoms is n.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr_{ab} = (x_b-x_a, y_b-y_a, z_b-z_a)\]
\[dr_{cb} = (x_b-x_c, y_b-y_c, z_b-z_c)\]
\[theta = arccos(inner_product(dr_{ab}, dr_{cb})/|dr_{ab}|/|dr_{cb}|)\]
\[F_a = -2*k*(theta-theta_0)/sin(theta)*[cos(theta)/|dr_{ab}|^2*dr_{ab} - 1/|dr_{ab}|/|dr_{cb}|*dr_{cb}]\]
\[F_c = -2*k*(theta-theta_0)/sin(theta)*[cos(theta)/|dr_{cb}|^2*dr_{cb} - 1/|dr_{cb}|/|dr_{ab}|*dr_{ab}]\]
\[F_b = -F_a - F_c\]
参数

angle_numbers (int32) – the number of angles m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd and the central atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tensor) - The 3rd atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • angle_k (Tensor) - The force constant for each angle. The data type is float32 and the shape is \((m,)\).

  • angle_theta0 (Tensor) - The equilibrium position value for each angle. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.AngleForceWithAtomEnergy(*args, **kwargs)[源代码]

Calculate angle force and potential energy together. Assume the number of angles is m and the number of atoms is n.

The calculation formula is the same as operator AngleForce() and AngleEnergy().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数

angle_numbers (int32) – the number of angles m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd and the central atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tensor) - The 3rd atom index of each angle. The data type is int32 and the shape is \((m,)\).

  • angle_k (Tensor) - The force constant for each angle. The data type is float32 and the shape is \((m,)\).

  • angle_theta0 (Tensor) - The equilibrium position value for each angle. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - same as operator AngleForce(). The data type is float32 and the shape is \((n, 3)\).

  • ene (Tensor) - same as operator AngleAtomEnergy(). The data type is float and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.ApplyAdaMax(*args, **kwargs)[源代码]

Updates relevant entries according to the adamax scheme.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta_1 * m_{t} + (1 - \beta_1) * g \\ v_{t+1} = \max(\beta_2 * v_{t}, \left| g \right|) \\ var = var - \frac{l}{1 - \beta_1^{t+1}} * \frac{m_{t+1}}{v_{t+1} + \epsilon} \end{array}\end{split}\]

\(t\) represents updating step while \(m\) represents the 1st moment vector, \(m_{t}\) is the last momentent of \(m_{t+1}\), \(v\) represents the 2nd moment vector, \(v_{t}\) is the last momentent of \(v_{t+1}\), \(l\) represents scaling factor lr, \(g\) represents grad, \(\beta_1, \beta_2\) represent beta1 and beta2, \(beta_1^{t+1}\) represents beta1_power, \(var\) represents the variable to be updated, \(\epsilon\) represents epsilon.

Inputs of var, m, v and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • var (Parameter) - Variable to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • m (Parameter) - The 1st moment vector in the updating formula, has the same shape and type as var. With float32 or float16 data type.

  • v (Parameter) - The 2nd moment vector in the updating formula. Mean square gradients with the same shape and type as var. With float32 or float16 data type.

  • beta1_power (Union[Number, Tensor]) - \(beta_1^t\) in the updating formula, must be scalar. With float32 or float16 data type.

  • lr (Union[Number, Tensor]) - Learning rate, \(l\) in the updating formula, must be scalar. With float32 or float16 data type.

  • beta1 (Union[Number, Tensor]) - The exponential decay rate for the 1st moment estimations, must be scalar. With float32 or float16 data type.

  • beta2 (Union[Number, Tensor]) - The exponential decay rate for the 2nd moment estimations, must be scalar. With float32 or float16 data type.

  • epsilon (Union[Number, Tensor]) - A small value added for numerical stability, must be scalar. With float32 or float16 data type.

  • grad (Tensor) - A tensor for gradient, has the same shape and type as var. With float32 or float16 data type.

Outputs:

Tuple of 3 Tensor, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • m (Tensor) - The same shape and data type as m.

  • v (Tensor) - The same shape and data type as v.

引发
  • TypeError – If dtype of var, m, v, beta_power, lr, beta1, beta2, epsilon or grad is neither float16 nor float32.

  • TypeError – If beta_power, lr, beta1, beta2 or epsilon is neither a Number nor a Tensor.

  • TypeError – If grad is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_ada_max = ops.ApplyAdaMax()
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.m = Parameter(Tensor(np.array([[0.6, 0.5],
...                                             [0.2, 0.6]]).astype(np.float32)), name="m")
...         self.v = Parameter(Tensor(np.array([[0.9, 0.1],
...                                             [0.7, 0.8]]).astype(np.float32)), name="v")
...     def construct(self, beta1_power, lr, beta1, beta2, epsilon, grad):
...         out = self.apply_ada_max(self.var, self.m, self.v, beta1_power, lr, beta1, beta2, epsilon, grad)
...         return out
...
>>> net = Net()
>>> beta1_power =Tensor(0.9, mindspore.float32)
>>> lr = Tensor(0.001, mindspore.float32)
>>> beta1 = Tensor(0.9, mindspore.float32)
>>> beta2 = Tensor(0.99, mindspore.float32)
>>> epsilon = Tensor(1e-10, mindspore.float32)
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(beta1_power, lr, beta1, beta2, epsilon, grad)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.93602717e-01,  3.92571449e-01],
 [ 9.72582996e-02,  4.92249995e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.69999993e-01,  5.19999981e-01],
 [ 1.89999998e-01,  6.20000005e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 8.90999973e-01,  6.99999988e-01],
 [ 6.93000019e-01,  8.00000012e-01]]))
class tinyms.primitives.ApplyAdadelta(*args, **kwargs)[源代码]

Updates relevant entries according to the adadelta scheme.

\[\begin{split}\begin{array}{ll} \\ accum = \rho * accum + (1 - \rho) * grad^2 \\ \text{update} = \sqrt{\text{accum_update} + \epsilon} * \frac{grad}{\sqrt{accum + \epsilon}} \\ \text{accum_update} = \rho * \text{accum_update} + (1 - \rho) * update^2 \\ var -= lr * update \end{array}\end{split}\]

where \(\rho\) represents rho, \(\epsilon\) represents epsilon.

Inputs of var, accum, accum_update and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • var (Parameter) - Weights to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Accumulation to be updated, has the same shape and data type as var.

  • accum_update (Parameter) - Accum_update to be updated, has the same shape and data type as var.

  • lr (Union[Number, Tensor]) - Learning rate, must be scalar. With float32 or float16 data type.

  • rho (Union[Number, Tensor]) - Decay rate, must be scalar. With float32 or float16 data type.

  • epsilon (Union[Number, Tensor]) - A small value added for numerical stability, must be scalar. With float32 or float16 data type.

  • grad (Tensor) - Gradients, has the same shape and data type as var.

Outputs:

Tuple of 3 Tensor, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • accum (Tensor) - The same shape and data type as accum.

  • accum_update (Tensor) - The same shape and data type as accum_update.

引发
  • TypeError – If dtype of var, accum, accum_update, lr, rho, epsilon or grad is neither float16 nor float32.

  • TypeError – If accum_update, lr, rho or epsilon is neither a Number nor a Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_adadelta = ops.ApplyAdadelta()
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.6, 0.5],
...                                                 [0.2, 0.6]]).astype(np.float32)), name="accum")
...         self.accum_update = Parameter(Tensor(np.array([[0.9, 0.1],
...                                                        [0.7, 0.8]]).astype(np.float32)),
...                                                             name="accum_update")
...     def construct(self, lr, rho, epsilon, grad):
...         out = self.apply_adadelta(self.var, self.accum, self.accum_update, lr, rho, epsilon, grad)
...         return out
...
>>> net = Net()
>>> lr = Tensor(0.001, mindspore.float32)
>>> rho = Tensor(0.0, mindspore.float32)
>>> epsilon = Tensor(1e-6, mindspore.float32)
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(lr, rho, epsilon, grad)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.99051356e-01,  3.99683774e-01],
 [ 9.91633832e-02,  4.99105573e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 9.00000036e-02,  4.89999980e-01],
 [ 1.00000007e-02,  6.40000045e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 8.99990976e-01,  1.00000791e-01],
 [ 6.99930906e-01,  7.99999654e-01]]))
class tinyms.primitives.ApplyAdagrad(*args, **kwargs)[源代码]

Updates relevant entries according to the adagrad scheme.

\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ var -= lr * grad * \frac{1}{\sqrt{accum}} \end{array}\end{split}\]

Inputs of var, accum and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

update_slots (bool) – If True, accum will be updated. Default: True.

Inputs:
  • var (Parameter) - Variable to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Accumulation to be updated. The shape and data type must be the same as var.

  • lr (Union[Number, Tensor]) - The learning rate value, must be scalar. With float32 or float16 data type.

  • grad (Tensor) - A tensor for gradient. The shape and data type must be the same as var.

Outputs:

Tuple of 2 Tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • accum (Tensor) - The same shape and data type as accum.

引发
  • TypeError – If dtype of var, accum, lr or grad is neither float16 nor float32.

  • TypeError – If lr is neither a Number nor a Tensor.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_adagrad = ops.ApplyAdagrad()
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.6, 0.5],
...                                                 [0.2, 0.6]]).astype(np.float32)), name="accum")
...     def construct(self, lr, grad):
...         out = self.apply_adagrad(self.var, self.accum, lr, grad)
...         return out
...
>>> net = Net()
>>> lr = Tensor(0.001, mindspore.float32)
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(lr, grad)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.99638879e-01,  3.99296492e-01],
 [ 9.97817814e-02,  4.99281585e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 6.90000057e-01,  9.90000010e-01],
 [ 2.10000008e-01,  1.24000001e+00]]))
class tinyms.primitives.ApplyAdagradDA(*args, **kwargs)[源代码]

Update var according to the proximal adagrad scheme.

\[\begin{split}\begin{array}{ll} \\ grad_accum += grad \\ grad_squared_accum += grad * grad \\ tmp_val=sign(grad_accum) * max\left \{|grad_accum|-l1*global_step, 0\right \} if l1>0 else grad_accum \\ x_value = -1 * lr * tmp_val \\ y_value = l2 * global_step * lr + \sqrt{grad_squared_accum} \\ var = x_value / y_value \end{array}\end{split}\]

Inputs of var, gradient_accumulator, gradient_squared_accumulator and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – If True, updating of the var and accum tensors will be protected by a lock. Otherwise the behavior is undefined, but may exhibit less contention. Default: False.

Inputs:
  • var (Parameter) - Variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • gradient_accumulator (Parameter) - The dict of mutable tensor gradient_accumulator. Must have the same shape and dtype as var.

  • gradient_squared_accumulator (Parameter) - The dict of mutable tensor gradient_squared_accumulator. Must have the same shape and dtype as var.

  • grad (Tensor) - A tensor for gradient. Must have the same shape and dtype as var.

  • lr ([Number, Tensor]) - Scaling factor. Must be a scalar. With float32 or float16 data type.

  • l1 ([Number, Tensor]) - L1 regularization. Must be a scalar. With float32 or float16 data type.

  • l2 ([Number, Tensor]) - L2 regularization. Must be a scalar. With float32 or float16 data type.

  • global_step ([Number, Tensor]) - Training step number. Must be a scalar. With int32 or int64 data type.

Outputs:

Tuple of 3 Tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • gradient_accumulator (Tensor) - The same shape and data type as gradient_accumulator.

  • gradient_squared_accumulator (Tensor) - The same shape and data type as gradient_squared_accumulator.

引发
  • TypeError – If var, gradient_accumulator, gradient_squared_accumulator is not a Parameter.

  • TypeError – If grad is not a Tensor.

  • TypeError – If lr, l1, l2 or global_step is neither a Number nor a Tensor.

  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of var, gradient_accumulator, gradient_squared_accumulator, gradient_accumulator, lr, l1, l2 is neither float16 nor float32.

  • TypeError – If dtype of gradient_accumulator, gradient_squared_accumulator, gradient_accumulator is not same as var.

  • TypeError – If dtype of global_step is not int32 or int64.

  • ValueError – If the shape size of lr, l1, l2 and global_step is not 0.

Supported Platforms:

Ascend

实际案例

>>> class ApplyAdagradDANet(nn.Cell):
...     def __init__(self, use_locking=False):
...         super(ApplyAdagradDANet, self).__init__()
...         self.apply_adagrad_d_a = P.ApplyAdagradDA(use_locking)
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4], [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.gradient_accumulator = Parameter(Tensor(np.array([[0.1, 0.3],
...                                                                [0.1, 0.5]]).astype(np.float32)),
...                                               name="gradient_accumulator")
...         self.gradient_squared_accumulator = Parameter(Tensor(np.array([[0.2, 0.1],
...                                                                        [0.1, 0.2]]).astype(np.float32)),
...                                                       name="gradient_squared_accumulator")
...         self.gradient_accumulator = Parameter(Tensor(np.array([[0.1, 0.3],
...                                                                [0.1, 0.5]]).astype(np.float32)),
...                                               name="gradient_accumulator")
...     def construct(self, grad, lr, l1, l2, global_step):
...         out = self.apply_adagrad_d_a(self.var, self.gradient_accumulator,
...                                      self.gradient_squared_accumulator, grad, lr, l1, l2, global_step)
...         return out
...
>>> net = ApplyAdagradDANet()
>>> grad = Tensor(np.array([[0.3, 0.4], [0.1, 0.2]]).astype(np.float32))
>>> lr = Tensor(0.001, mstype.float32)
>>> l1 = Tensor(0.001, mstype.float32)
>>> l2 = Tensor(0.001, mstype.float32)
>>> global_step = Tensor(2, mstype.int32)
>>> output = net(grad, lr, l1, l2, global_step)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[-7.39064650e-04, -1.36888528e-03],
 [-5.96988888e-04, -1.42478070e-03]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 4.00000006e-01,  7.00000048e-01],
 [ 2.00000003e-01,  6.99999988e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 2.90000021e-01,  2.60000020e-01],
 [ 1.09999999e-01,  2.40000010e-01]]))
class tinyms.primitives.ApplyAdagradV2(*args, **kwargs)[源代码]

Updates relevant entries according to the adagradv2 scheme.

\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ var -= lr * grad * \frac{1}{\sqrt{accum} + \epsilon} \end{array}\end{split}\]

where \(\epsilon\) represents epsilon.

Inputs of var, accum and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

注解

The difference is that ApplyAdagradV2 has one more small constant value than ApplyAdagrad.

参数
  • epsilon (float) – A small value added for numerical stability.

  • update_slots (bool) – If True, accum will be updated. Default: True.

Inputs:
  • var (Parameter) - Variable to be updated. With float16 or float32 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Accumulation to be updated. The shape and data type must be the same as var.

  • lr (Union[Number, Tensor]) - The learning rate value, must be a float number or a scalar tensor with float16 or float32 data type.

  • grad (Tensor) - A tensor for gradient. The shape and data type must be the same as var.

Outputs:

Tuple of 2 Tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • accum (Tensor) - The same shape and data type as m.

引发
  • TypeError – If dtype of var, accum, lr or grad is neither float16 nor float32.

  • TypeError – If lr is neither a Number nor a Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_adagrad_v2 = ops.ApplyAdagradV2(epsilon=1e-6)
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.6, 0.5],
...                                                 [0.2, 0.6]]).astype(np.float32)), name="accum")
...     def construct(self, lr, grad):
...         out = self.apply_adagrad_v2(self.var, self.accum, lr, grad)
...         return out
...
>>> net = Net()
>>> lr = Tensor(0.001, mindspore.float32)
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(lr, grad)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.99638879e-01,  3.99296492e-01],
 [ 9.97817814e-02,  4.99281585e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 6.90000057e-01,  9.90000010e-01],
 [ 2.10000008e-01,  1.24000001e+00]]))
class tinyms.primitives.ApplyAddSign(*args, **kwargs)[源代码]

Updates relevant entries according to the AddSign algorithm.

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta * m_{t} + (1 - \beta) * g \\ \text{update} = (\alpha + \text{sign_decay} * sign(g) * sign(m)) * g \\ var = var - lr_{t+1} * \text{update} \end{array}\end{split}\]

\(t\) represents updating step while \(m\) represents the 1st moment vector, \(m_{t}\) is the last momentent of \(m_{t+1}\), \(lr\) represents scaling factor lr, \(g\) represents grad, \(\alpha\) represents alpha, \(\beta\) represents beta.

Inputs of var, accum and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • var (Parameter) - Variable tensor to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • m (Parameter) - Variable tensor to be updated, has the same shape and data type as var.

  • lr (Union[Number, Tensor]) - The learning rate value, must be a scalar. With float32 or float16 data type.

  • alpha (Union[Number, Tensor]) - Must be a scalar. With float32 or float16 data type.

  • sign_decay (Union[Number, Tensor]) - Must be a scalar. With float32 or float16 data type.

  • beta (Union[Number, Tensor]) - The exponential decay rate, must be a scalar. With float32 or float16 data type.

  • grad (Tensor) - A tensor of the same shape and data type as var, for the gradient.

Outputs:

Tuple of 2 Tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • m (Tensor) - The same shape and data type as m.

引发
  • TypeError – If dtype of var, lr, alpha, sign_decay or beta is neither float16 nor float32.

  • TypeError – If lr, alpha or sign_decay is neither a Number nor a Tensor.

  • TypeError – If grad is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_add_sign = ops.ApplyAddSign()
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.m = Parameter(Tensor(np.array([[0.6, 0.5],
...                                             [0.2, 0.6]]).astype(np.float32)), name="m")
...         self.lr = 0.001
...         self.alpha = 1.0
...         self.sign_decay = 0.99
...         self.beta = 0.9
...     def construct(self, grad):
...         out = self.apply_add_sign(self.var, self.m, self.lr, self.alpha, self.sign_decay, self.beta, grad)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(grad)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.99403024e-01,  3.98607016e-01],
 [ 9.98010039e-02,  4.98407990e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.70000052e-01,  5.19999981e-01],
 [ 1.89999998e-01,  6.20000064e-01]]))
class tinyms.primitives.ApplyCenteredRMSProp(*args, **kwargs)[源代码]

Optimizer that implements the centered RMSProp algorithm. Please refer to the usage in source code of nn.RMSProp.

The updating formulas of ApplyCenteredRMSProp algorithm are as follows,

\[\begin{split}\begin{array}{ll} \\ g_{t+1} = \rho g_{t} + (1 - \rho)\nabla Q_{i}(w) \\ s_{t+1} = \rho s_{t} + (1 - \rho)(\nabla Q_{i}(w))^2 \\ m_{t+1} = \beta m_{t} + \frac{\eta} {\sqrt{s_{t+1} - g_{t+1}^2 + \epsilon}} \nabla Q_{i}(w) \\ w = w - m_{t+1} \end{array}\end{split}\]

where \(w\) represents var, which will be updated. \(g_{t+1}\) represents mean_gradient, \(g_{t}\) is the last momentent of \(g_{t+1}\). \(s_{t+1}\) represents mean_square, \(s_{t}\) is the last momentent of \(s_{t+1}\), \(m_{t+1}\) represents moment, \(m_{t}\) is the last momentent of \(m_{t+1}\). \(\rho\) represents decay. \(\beta\) is the momentum term, represents momentum. \(\epsilon\) is a smoothing term to avoid division by zero, represents epsilon. \(\eta\) represents learning_rate. \(\nabla Q_{i}(w)\) represents grad.

注解

The difference between ApplyCenteredRMSProp and ApplyRMSProp is that the fromer uses the centered RMSProp algorithm, and the centered RRMSProp algorithm uses an estimate of the centered second moment(i.e., the variance) for normalization, as opposed to regular RMSProp, which uses the (uncentered) second moment. This often helps with training, but is slightly more exapnsive interms of computation and memory.

警告

In dense implementation of this algorithm, mean_gradient, mean_square, and moment will update even if the grad is zero. But in this sparse implementation, mean_gradient, mean_square, and moment will not update in iterations during which the grad is zero.

参数

use_locking (bool) – Whether to enable a lock to protect the variable and accumlation tensors from being updated. Default: False.

Inputs:
  • var (Tensor) - Weights to be update.

  • mean_gradient (Tensor) - Mean gradients, must have the same type as var.

  • mean_square (Tensor) - Mean square gradients, must have the same type as var.

  • moment (Tensor) - Delta of var, must have the same type as var.

  • grad (Tensor) - Gradient, must have the same type as var.

  • learning_rate (Union[Number, Tensor]) - Learning rate. Must be a float number or a scalar tensor with float16 or float32 data type.

  • decay (float) - Decay rate.

  • momentum (float) - Momentum.

  • epsilon (float) - Ridge term.

Outputs:

Tensor, parameters to be update.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If var, mean_gradient, mean_square, moment or grad is not a Tensor.

  • TypeError – If learing_rate is neither a Number nor a Tensor.

  • TypeError – If dtype of learing_rate is neither float16 nor float32.

  • TypeError – If decay, momentum or epsilon is not a float.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_centerd_rms_prop = ops.ApplyCenteredRMSProp()
...         self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var")
...
...     def construct(self, mean_grad, mean_square, moment, grad, decay, momentum, epsilon, lr):
...         out = self.apply_centerd_rms_prop(self.var, mean_grad, mean_square, moment, grad,
...                                           lr, decay, momentum, epsilon)
...         return out
...
>>> net = Net()
>>> mean_grad = Tensor(np.ones([2, 2]).astype(np.float32))
>>> mean_square = Tensor(np.ones([2, 2]).astype(np.float32))
>>> moment = Tensor(np.ones([2, 2]).astype(np.float32))
>>> grad = Tensor(np.ones([2, 2]).astype(np.float32))
>>> output = net(mean_grad, mean_square, moment, grad, 0.0, 1e-10, 0.001, 0.01)
>>> print(net.var.asnumpy())
[[0.68377227  0.68377227]
 [0.68377227  0.68377227]]
class tinyms.primitives.ApplyFtrl(*args, **kwargs)[源代码]

Updates relevant entries according to the FTRL scheme.

For more details, please refer to nn.FTRL.

参数

use_locking (bool) – Use locks for updating operation if true . Default: False.

Inputs:
  • var (Parameter) - The variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - The accumulation to be updated, must be same shape and data type as var.

  • linear (Parameter) - The linear coefficient to be updated, must be same shape and data type as var.

  • grad (Tensor) - Gradient. The data type must be float16 or float32.

  • lr (Union[Number, Tensor]) - The learning rate value, must be positive. Default: 0.001. It must be a float number or a scalar tensor with float16 or float32 data type.

  • l1 (Union[Number, Tensor]) - l1 regularization strength, must be greater than or equal to zero. Default: 0.0. It must be a float number or a scalar tensor with float16 or float32 data type.

  • l2 (Union[Number, Tensor]) - l2 regularization strength, must be greater than or equal to zero. Default: 0.0. It must be a float number or a scalar tensor with float16 or float32 data type.

  • lr_power (Union[Number, Tensor]) - Learning rate power controls how the learning rate decreases during training, must be less than or equal to zero. Use fixed learning rate if lr_power is zero. Default: -0.5. It must be a float number or a scalar tensor with float16 or float32 data type.

Outputs:
  • var (Tensor) - Represents the updated var. As the input parameters has been updated in-place, this value is always zero when the platforms is GPU.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of var, grad, lr, l1, l2 or lr_power is neither float16 nor float32.

  • TypeError – If lr, l1, l2 or lr_power is neither a Number nor a Tensor.

  • TypeError – If grad is not a Tensor.

Supported Platforms:

Ascend GPU

实际案例

>>> class ApplyFtrlNet(nn.Cell):
...     def __init__(self):
...         super(ApplyFtrlNet, self).__init__()
...         self.apply_ftrl = ops.ApplyFtrl()
...         self.lr = 0.001
...         self.l1 = 0.0
...         self.l2 = 0.0
...         self.lr_power = -0.5
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.6, 0.5],
...                                                 [0.2, 0.6]]).astype(np.float32)), name="accum")
...         self.linear = Parameter(Tensor(np.array([[0.9, 0.1],
...                                                  [0.7, 0.8]]).astype(np.float32)), name="linear")
...
...     def construct(self, grad):
...         out = self.apply_ftrl(self.var, self.accum, self.linear, grad, self.lr, self.l1, self.l2,
...                               self.lr_power)
...         return out
...
>>> net = ApplyFtrlNet()
>>> input_x = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(input_x)
>>> print(net.var.asnumpy())
[[ 0.0390525  0.11492836]
 [ 0.00066425 0.15075898]]
class tinyms.primitives.ApplyGradientDescent(*args, **kwargs)[源代码]

Updates relevant entries according to the following.

\[var = var - \alpha * \delta\]

where \(\alpha\) represents alpha, \(\delta\) represents delta.

Inputs of var and delta comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • var (Parameter) - Variable tensor to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • alpha (Union[Number, Tensor]) - Scaling factor, must be a scalar. With float32 or float16 data type.

  • delta (Tensor) - A tensor for the change, has the same shape and data type as var.

Outputs:

Tensor, represents the updated var.

引发
  • TypeError – If dtype of var or alpha is neither float16 nor float32.

  • TypeError – If delta is not a Tensor.

  • TypeError – If alpha is neither a Number nor a Tensor.

Supported Platforms:

Ascend GPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_gradient_descent = ops.ApplyGradientDescent()
...         self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var")
...         self.alpha = 0.001
...     def construct(self, delta):
...         out = self.apply_gradient_descent(self.var, self.alpha, delta)
...         return out
...
>>> net = Net()
>>> delta = Tensor(np.array([[0.1, 0.1], [0.1, 0.1]]).astype(np.float32))
>>> output = net(delta)
>>> print(output)
[[0.9999 0.9999]
 [0.9999 0.9999]]
class tinyms.primitives.ApplyMomentum(*args, **kwargs)[源代码]

Optimizer that implements the Momentum algorithm.

Refer to the paper On the importance of initialization and momentum in deep learning for more details.

Refer to mindspore.nn.Momentum for more details about the formula and usage.

Inputs of variable, accumulation and gradient comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. Data type conversion of Parameter is not supported. RuntimeError exception will be thrown.

参数
  • use_locking (bool) – Whether to enable a lock to protect the variable and accumulation tensors from being updated. Default: False.

  • use_nesterov (bool) – Enable Nesterov momentum. Default: False.

  • gradient_scale (float) – The scale of the gradient. Default: 1.0.

Inputs:
  • variable (Parameter) - Weights to be updated. data type must be float.

  • accumulation (Parameter) - Accumulated gradient value by moment weight. Has the same data type with variable.

  • learning_rate (Union[Number, Tensor]) - The learning rate value, must be a float number or a scalar tensor with float data type.

  • gradient (Tensor) - Gradient, has the same data type as variable.

  • momentum (Union[Number, Tensor]) - Momentum, must be a float number or a scalar tensor with float data type.

Outputs:

Tensor, parameters to be updated.

引发

TypeError – If the use_locking or use_nesterov is not a bool or gradient_scale is not a float.

Supported Platforms:

Ascend GPU CPU

实际案例

Please refer to the usage in mindspore.nn.Momentum.

class tinyms.primitives.ApplyPowerSign(*args, **kwargs)[源代码]

Updates relevant entries according to the AddSign algorithm.

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta * m_{t} + (1 - \beta) * g \\ \text{update} = \exp(\text{logbase} * \text{sign_decay} * sign(g) * sign(m)) * g \\ var = var - lr_{t+1} * \text{update} \end{array}\end{split}\]

\(t\) represents updating step while \(m\) represents the 1st moment vector, \(m_{t}\) is the last momentent of \(m_{t+1}\), \(lr\) represents scaling factor lr, \(g\) represents grad, \(\beta\) represents beta.

All of inputs comply with the implicit type conversion rules to make the data types consistent. If lr, logbase, sign_decay or beta is a number, the number is automatically converted to Tensor, and the data type is consistent with the Tensor data type involved in the operation. If inputs are tensors and have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • var (Parameter) - Variable tensor to be updated. With float32 or float16 data type. If data type of var is float16, all inputs must have the same data type as var. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • m (Parameter) - Variable tensor to be updated, has the same shape and data type as var.

  • lr (Union[Number, Tensor]) - The learning rate value, must be a scalar. With float32 or float16 data type.

  • logbase (Union[Number, Tensor]) - Must be a scalar. With float32 or float16 data type.

  • sign_decay (Union[Number, Tensor]) - Must be a scalar. With float32 or float16 data type.

  • beta (Union[Number, Tensor]) - The exponential decay rate, must be a scalar. With float32 or float16 data type.

  • grad (Tensor) - A tensor of the same shape and data type as var, for the gradient.

Outputs:

Tuple of 2 Tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • m (Tensor) - The same shape and data type as m.

引发
  • TypeError – If dtype of var, lr, logbase, sign_decay, beta or grad is neither float16 nor float32.

  • TypeError – If lr, logbase, sign_decay or beta is neither a Number nor a Tensor.

  • TypeError – If grad is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_power_sign = ops.ApplyPowerSign()
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.m = Parameter(Tensor(np.array([[0.6, 0.5],
...                                             [0.2, 0.6]]).astype(np.float32)), name="m")
...         self.lr = 0.001
...         self.logbase = np.e
...         self.sign_decay = 0.99
...         self.beta = 0.9
...     def construct(self, grad):
...         out = self.apply_power_sign(self.var, self.m, self.lr, self.logbase,
...                                        self.sign_decay, self.beta, grad)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(grad)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.95575690e-01,  3.89676481e-01],
 [ 9.85252112e-02,  4.88201708e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.70000052e-01,  5.19999981e-01],
 [ 1.89999998e-01,  6.20000064e-01]]))
class tinyms.primitives.ApplyProximalAdagrad(*args, **kwargs)[源代码]

Updates relevant entries according to the proximal adagrad algorithm.

\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ \text{prox_v} = var - lr * grad * \frac{1}{\sqrt{accum}} \\ var = \frac{sign(\text{prox_v})}{1 + lr * l2} * \max(\left| \text{prox_v} \right| - lr * l1, 0) \end{array}\end{split}\]

Inputs of var, accum and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – If true, the var and accumulation tensors will be protected from being updated. Default: False.

Inputs:
  • var (Parameter) - Variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Accumulation to be updated. Must has the same shape and dtype as var.

  • lr (Union[Number, Tensor]) - The learning rate value, must be scalar. The data type must be float16 or float32.

  • l1 (Union[Number, Tensor]) - l1 regularization strength, must be scalar. The data type must be float16 or float32.

  • l2 (Union[Number, Tensor]) - l2 regularization strength, must be scalar. The data type must be float16 or float32.

  • grad (Tensor) - Gradient with the same shape and dtype as var.

Outputs:

Tuple of 2 Tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • accum (Tensor) - The same shape and data type as accum.

引发
  • TypeError – If use_blocking is not a bool.

  • TypeError – If dtype of var, lr, l1 or l2 is neither float16 nor float32.

  • TypeError – If lr, l1 or l2 is neither a Number nor a Tensor.

  • TypeError – If grad is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_proximal_adagrad = ops.ApplyProximalAdagrad()
...         self.var = Parameter(Tensor(np.array([[0.6, 0.4],
...                                               [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.6, 0.5],
...                                                 [0.2, 0.6]]).astype(np.float32)), name="accum")
...         self.lr = 0.01
...         self.l1 = 0.0
...         self.l2 = 0.0
...     def construct(self, grad):
...         out = self.apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1, self.l2, grad)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> output = net(grad)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.96388459e-01,  3.92964751e-01],
 [ 9.78178233e-02,  4.92815793e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 6.90000057e-01,  9.90000010e-01],
 [ 2.10000008e-01,  1.24000001e+00]]))
class tinyms.primitives.ApplyProximalGradientDescent(*args, **kwargs)[源代码]

Updates relevant entries according to the FOBOS(Forward Backward Splitting) algorithm.

\[\begin{split}\begin{array}{ll} \\ \text{prox_v} = var - \alpha * \delta \\ var = \frac{sign(\text{prox_v})}{1 + \alpha * l2} * \max(\left| \text{prox_v} \right| - \alpha * l1, 0) \end{array}\end{split}\]

where \(\alpha\) represents alpha, \(\delta\) represents delta.

Inputs of var and delta comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • var (Parameter) - Variable tensor to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • alpha (Union[Number, Tensor]) - Scaling factor, must be a scalar. With float32 or float16 data type.

  • l1 (Union[Number, Tensor]) - l1 regularization strength, must be scalar. With float32 or float16 data type.

  • l2 (Union[Number, Tensor]) - l2 regularization strength, must be scalar. With float32 or float16 data type.

  • delta (Tensor) - A tensor for the change, has the same shape and data type as var.

Outputs:

Tensor, represents the updated var.

引发
  • TypeError – If dtype of var, alpha, l1 or l2 is neither float16 nor float32.

  • TypeError – If alpha, l1 or l2 is neither a Number nor a Tensor.

  • TypeError – If delta is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_proximal_gradient_descent = ops.ApplyProximalGradientDescent()
...         self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var")
...         self.alpha = 0.001
...         self.l1 = 0.1
...         self.l2 = 0.1
...     def construct(self, delta):
...         out = self.apply_proximal_gradient_descent(self.var, self.alpha, self.l1, self.l2, delta)
...         return out
...
>>> net = Net()
>>> delta = Tensor(np.array([[0.1, 0.1], [0.1, 0.1]]).astype(np.float32))
>>> output = net(delta)
>>> print(output)
[[0.99969995 0.99969995]
 [0.99969995 0.99969995]]
class tinyms.primitives.ApplyRMSProp(*args, **kwargs)[源代码]

Optimizer that implements the Root Mean Square prop(RMSProp) algorithm. Please refer to the usage in source code of nn.RMSProp.

The updating formulas of ApplyRMSProp algorithm are as follows,

\[\begin{split}\begin{array}{ll} \\ s_{t+1} = \rho s_{t} + (1 - \rho)(\nabla Q_{i}(w))^2 \\ m_{t+1} = \beta m_{t} + \frac{\eta} {\sqrt{s_{t+1} + \epsilon}} \nabla Q_{i}(w) \\ w = w - m_{t+1} \end{array}\end{split}\]

where \(w\) represents var, which will be updated. \(s_{t+1}\) represents mean_square, \(s_{t}\) is the last momentent of \(s_{t+1}\), \(m_{t+1}\) represents moment, \(m_{t}\) is the last momentent of \(m_{t+1}\). \(\rho\) represents decay. \(\beta\) is the momentum term, represents momentum. \(\epsilon\) is a smoothing term to avoid division by zero, represents epsilon. \(\eta\) represents learning_rate. \(\nabla Q_{i}(w)\) represents grad.

警告

Note that in dense implementation of this algorithm, “mean_square” and “moment” will update even if “grad” is 0, but in this sparse implementation, “mean_square” and “moment” will not update in iterations during which “grad” is 0.

参数

use_locking (bool) – Whether to enable a lock to protect the variable and accumlation tensors from being updated. Default: False.

Inputs:
  • var (Tensor) - Weights to be update.

  • mean_square (Tensor) - Mean square gradients, must have the same type as var.

  • moment (Tensor) - Delta of var, must have the same type as var.

  • learning_rate (Union[Number, Tensor]) - Learning rate. Must be a float number or a scalar tensor with float16 or float32 data type.

  • grad (Tensor) - Gradient, must have the same type as var.

  • decay (float) - Decay rate. Only constant value is allowed.

  • momentum (float) - Momentum. Only constant value is allowed.

  • epsilon (float) - Ridge term. Only constant value is allowed.

Outputs:

Tensor, parameters to be update.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If var, mean_square, moment or decay is not a Tensor.

  • TypeError – If learning_rate is neither a Number nor a Tensor.

  • TypeError – If dtype of decay, momentum or epsilon is not float.

  • TypeError – If dtype of learning_rate is neither float16 nor float32.

  • ValueError – If decay, momentum or epsilon is not a constant value.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.apply_rms_prop = ops.ApplyRMSProp()
...         self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var")
...
...     def construct(self, mean_square, moment, grad, decay, momentum, epsilon, lr):
...         out = self.apply_rms_prop(self.var, mean_square, moment, lr, grad, decay, momentum, epsilon)
...         return out
...
>>> net = Net()
>>> mean_square = Tensor(np.ones([2, 2]).astype(np.float32))
>>> moment = Tensor(np.ones([2, 2]).astype(np.float32))
>>> grad = Tensor(np.ones([2, 2]).astype(np.float32))
>>> output = net(mean_square, moment, grad, 0.0, 1e-10, 0.001, 0.01)
>>> print(net.var.asnumpy())
[[0.990005  0.990005]
 [0.990005  0.990005]]
class tinyms.primitives.ApproximateEqual(*args, **kwargs)[源代码]

Returns True if abs(x-y) is smaller than tolerance element-wise, otherwise False.

\[\begin{split}out_i = \begin{cases} & \text{ if } \left | x_{i} - y_{i} \right | < \text{tolerance},\ \ True \\ & \text{ if } \left | x_{i} - y_{i} \right | \ge \text{tolerance},\ \ False \end{cases}\end{split}\]

where \(\text{tolerance}\) indicates Acceptable maximum tolerance.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

tolerance (float) – The maximum deviation that two elements can be considered equal. Default: 1e-05.

Inputs:
  • x (Tensor) - A tensor. Must be one of the following types: float32, float16. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • y (Tensor) - A tensor of the same type and shape as ‘x’.

Outputs:

Tensor, the shape is the same as the shape of ‘x’, and the data type is bool.

引发

TypeError – If tolerance is not a float.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> y = Tensor(np.array([2, 4, 6]), mindspore.float32)
>>> approximate_equal = ops.ApproximateEqual(2.)
>>> output = approximate_equal(x, y)
>>> print(output)
[ True  True  False]
class tinyms.primitives.ArgMaxWithValue(*args, **kwargs)[源代码]

Calculates the maximum value with the corresponding index.

Calculates the maximum value along with the given axis for the input tensor. It returns the maximum values and indices.

注解

In auto_parallel and semi_auto_parallel mode, the first output index can not be used.

警告

  • If there are multiple maximum values, the index of the first maximum value is used.

  • The value range of “axis” is [-dims, dims - 1]. “dims” is the dimension length of “input_x”.

参数
  • axis (int) – The dimension to reduce. Default: 0.

  • keep_dims (bool) – Whether to reduce dimension, if true, the output will keep same dimension with the input, the output will reduce dimension if false. Default: False.

Inputs:
  • input_x (Tensor) - The input tensor, can be any dimension. Set the shape of input tensor as \((x_1, x_2, ..., x_N)\). And the data type only support mindspore.float16 or float32.

Outputs:

tuple (Tensor), tuple of 2 tensors, containing the corresponding index and the maximum value of the input tensor. - index (Tensor) - The index for the maximum value of the input tensor. If keep_dims is true, the shape of output tensors is \((x_1, x_2, ..., x_{axis-1}, 1, x_{axis+1}, ..., x_N)\). Otherwise, the shape is \((x_1, x_2, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\). - output_x (Tensor) - The maximum value of input tensor, with the same shape as index.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32)
>>> index, output = ops.ArgMaxWithValue()(input_x)
>>> print(index, output)
3 0.7
>>> index, output = ops.ArgMaxWithValue(keep_dims=True)(input_x)
>>> print(index, output)
[3] [0.7]
class tinyms.primitives.ArgMinWithValue(*args, **kwargs)[源代码]

Calculates the minimum value with corresponding index, and returns indices and values.

Calculates the minimum value along with the given axis for the input tensor. It returns the minimum values and indices.

注解

In auto_parallel and semi_auto_parallel mode, the first output index can not be used.

警告

  • If there are multiple minimum values, the index of the first minimum value is used.

  • The value range of “axis” is [-dims, dims - 1]. “dims” is the dimension length of “input_x”.

参数
  • axis (int) – The dimension to reduce. Default: 0.

  • keep_dims (bool) – Whether to reduce dimension, if true the output will keep the same dimension as the input, the output will reduce dimension if false. Default: False.

Inputs:
  • input_x (Tensor) - The input tensor, can be any dimension. Set the shape of input tensor as \((x_1, x_2, ..., x_N)\).

Outputs:

tuple (Tensor), tuple of 2 tensors, containing the corresponding index and the minimum value of the input tensor. - index (Tensor) - The index for the minimum value of the input tensor. If keep_dims is true, the shape of output tensors is \((x_1, x_2, ..., x_{axis-1}, 1, x_{axis+1}, ..., x_N)\). Otherwise, the shape is \((x_1, x_2, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\). - output_x (Tensor) - The minimum value of input tensor, with the same shape as index.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32)
>>> output = ops.ArgMinWithValue()(input_x)
>>> print(output)
(Tensor(shape=[], dtype=Int32, value= 0), Tensor(shape=[], dtype=Float32, value= 0))
>>> output = ops.ArgMinWithValue(keep_dims=True)(input_x)
>>> print(output)
(Tensor(shape=[1], dtype=Int32, value= [0]), Tensor(shape=[1], dtype=Float32, value= [ 0.00000000e+00]))
class tinyms.primitives.Argmax(*args, **kwargs)[源代码]

Returns the indices of the maximum value of a tensor across the axis.

If the shape of input tensor is \((x_1, ..., x_N)\), the shape of the output tensor will be \((x_1, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\).

参数
  • axis (int) – Axis where the Argmax operation applies to. Default: -1.

  • output_type (mindspore.dtype) – An optional data type of mindspore.dtype.int32. Default: mindspore.dtype.int32.

Inputs:
  • input_x (Tensor) - Input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions. Support data type list as follows:

    • Ascend: Float16, Float32.

    • GPU: Float16, Float32.

    • CPU: Float16, Float32, Float64.

Outputs:

Tensor, indices of the max value of input tensor across the axis.

引发
  • TypeError – If axis is not an int.

  • TypeError – If output_type is neither int32 nor int64.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[1, 20, 5], [67, 8, 9], [130, 24, 15]]).astype(np.float32))
>>> output = ops.Argmax(output_type=mindspore.int32)(input_x)
>>> print(output)
[1 0 0]
class tinyms.primitives.Argmin(*args, **kwargs)[源代码]

Returns the indices of the minimum value of a tensor across the axis.

If the shape of input tensor is \((x_1, ..., x_N)\), the shape of the output tensor is \((x_1, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\).

参数
  • axis (int) – Axis where the Argmin operation applies to. Default: -1.

  • output_type (mindspore.dtype) – An optional data type of mindspore.dtype.int32. Default: mindspore.dtype.int32.

Inputs:
  • input_x (Tensor) - Input tensor. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, indices of the min value of input tensor across the axis.

引发
  • TypeError – If axis is not an int.

  • TypeError – If output_type is neither int32 nor int64.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([2.0, 3.1, 1.2]), mindspore.float32)
>>> index = ops.Argmin()(input_x)
>>> print(index)
2
class tinyms.primitives.Asin(*args, **kwargs)[源代码]

Computes arcsine of input tensors element-wise.

\[out_i = sin^{-1}(x_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is

    \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> asin = ops.Asin()
>>> x = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32)
>>> output = asin(x)
>>> print(output)
[0.8330927  0.04001068  0.30469266  0.59438497]
class tinyms.primitives.Asinh(*args, **kwargs)[源代码]

Computes inverse hyperbolic sine of the input element-wise.

\[out_i = \sinh^{-1}(input_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8. The data type should be one of the following types: float16, float32.

Outputs:

Tensor, has the same shape and type as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> asinh = ops.Asinh()
>>> x = Tensor(np.array([-5.0, 1.5, 3.0, 100.0]), mindspore.float32)
>>> output = asinh(x)
>>> print(output)
[-2.3124385  1.1947632  1.8184465  5.298342 ]
class tinyms.primitives.Assert(*args, **kwargs)[源代码]

Asserts that the given condition is True. If input condition evaluates to false, print the list of tensor in data.

参数

summarize (int) – Print this many entries of each tensor.

Inputs:
  • condition [Union[Tensor[bool], bool]] - The condition to evaluate.

  • input_data (Union(tuple[Tensor], list[Tensor])) - The tensors to print out when condition is false.

引发
  • TypeError – If summarize is not an int.

  • TypeError – If condition is neither a Tensor nor a bool.

  • TypeError – If input_data is neither a tuple nor a list.

实际案例

>>> class AssertDemo(nn.Cell):
...     def __init__(self):
...         super(AssertDemo, self).__init__()
...         self.assert1 = ops.Assert(summarize=10)
...         self.add = ops.Add()
...
...     def construct(self, x, y):
...         data = self.add(x, y)
...         self.assert1(True, [data])
...         return data
...
class tinyms.primitives.Assign(*args, **kwargs)[源代码]

Assigns Parameter with a value.

Inputs of variable and value comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • variable (Parameter) - The Parameter. \((N,*)\) where \(*\) means ,any number of additional dimensions, its rank should less than 8.

  • value (Tensor) - The value to be assigned, has the same shape with variable.

Outputs:

Tensor, has the same data type and shape as original variable.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> value = Tensor([2.0], mindspore.float32)
>>> variable = mindspore.Parameter(Tensor([1.0], mindspore.float32), name="variable")
>>> assign = ops.Assign()
>>> output = assign(variable, value)
>>> print(output)
[2.]
class tinyms.primitives.AssignAdd(*args, **kwargs)[源代码]

Updates a Parameter by adding a value to it.

Inputs of variable and value comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. If value is a number, the number is automatically converted to Tensor, and the data type is consistent with the Tensor data type involved in the operation. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

注解

Since variable is a data type Parameter, the data type cannot be changed, so only the type of value is allowed to be promoted to the type of variable. And the conversion type supported by different devices will be different, it is recommended to use the same data type when using this operator.

Inputs:
  • variable (Parameter) - The Parameter. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • value (Union[numbers.Number, Tensor]) - The value to be added to the variable. It must have the same shape as variable if it is a Tensor. it is recommended to use the same data type when using this operator.

Outputs:

Tensor, has the same data type and shape as original variable.

引发

TypeError – If value is neither Number nor Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.AssignAdd = ops.AssignAdd()
...         self.variable = mindspore.Parameter(initializer(1, [1], mindspore.int64), name="global_step")
...
...     def construct(self, x):
...         self.AssignAdd(self.variable, x)
...         return self.variable
...
>>> net = Net()
>>> value = Tensor(np.ones([1]).astype(np.int64)*100)
>>> output = net(value)
>>> print(output)
[101]
class tinyms.primitives.AssignSub(*args, **kwargs)[源代码]

Updates a Parameter by subtracting a value from it.

Inputs of variable and value comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. If value is a number, the number is automatically converted to Tensor, and the data type is consistent with the Tensor data type involved in the operation. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

注解

Since variable is a data type Parameter, the data type cannot be changed, so only the type of value is allowed to be promoted to the type of variable. And the conversion type supported by different devices will be different, it is recommended to use the same data type when using this operator.

Inputs:
  • variable (Parameter) - The Parameter. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • value (Union[numbers.Number, Tensor]) - The value to be subtracted from the variable. It must have the same shape as variable if it is a Tensor. it is recommended to use the same data type when using this operator.

Outputs:

Tensor, has the same data type and shape as original variable.

引发

TypeError – If value is neither Number nor Tensor.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.AssignSub = ops.AssignSub()
...         self.variable = mindspore.Parameter(initializer(1, [1], mindspore.int32), name="global_step")
...
...     def construct(self, x):
...         self.AssignSub(self.variable, x)
...         return self.variable
...
>>> net = Net()
>>> value = Tensor(np.ones([1]).astype(np.int32)*100)
>>> output = net(value)
>>> print(output)
[-99]
class tinyms.primitives.Atan(*args, **kwargs)[源代码]

Computes the trigonometric inverse tangent of the input element-wise.

\[out_i = tan^{-1}(x_i)\]
Inputs:
  • x (Tensor): The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions. The data type should be one of the following types: float16, float32.

Outputs:

A Tensor, has the same type as the input.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 0.0]), mindspore.float32)
>>> atan = ops.Atan()
>>> output = atan(x)
>>> print(output)
[0.7853982 0.       ]
class tinyms.primitives.Atan2(*args, **kwargs)[源代码]

Returns arctangent of x/y element-wise.

It returns \(\theta\ \in\ [-\pi, \pi]\) such that \(x = r*\sin(\theta), y = r*\cos(\theta)\), where \(r = \sqrt{x^2 + y^2}\).

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • x (Tensor) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions. The data type will give priority to the high-precision data type

  • y (Tensor) - The input tensor. It has the same shape with x. The data type will give priority to the high-precision data type.

Outputs:

Tensor, the shape is the same as the one after broadcasting,and the data type is same as x.

引发

TypeError – If x or y is not a Tensor.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> x = Tensor(np.array([0, 1]), mindspore.float32)
>>> y = Tensor(np.array([1, 1]), mindspore.float32)
>>> atan2 = ops.Atan2()
>>> output = atan2(x, y)
>>> print(output)
[0.        0.7853982]
class tinyms.primitives.Atanh(*args, **kwargs)[源代码]

Computes inverse hyperbolic tangent of the input element-wise.

\[out_i = \tanh^{-1}(x_{i})\]
Inputs:
  • x (Tensor): The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

A Tensor, has the same type as the input.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend CPU

实际案例

>>> x = Tensor(np.array([0, -0.5]), mindspore.float32)
>>> atanh = ops.Atanh()
>>> output = atanh(x)
>>> print(output)
[0. -0.54930614]
class tinyms.primitives.AvgPool(*args, **kwargs)[源代码]

Average pooling operation.

Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes. Typically the input is of shape \((N_{in}, C_{in}, H_{in}, W_{in})\), AvgPool outputs regional average in the \((H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1)\), the operation is as follows.

\[\text{output}(N_i, C_j, h, w) = \frac{1}{h_{ker} * w_{ker}} \sum_{m=0}^{h_{ker}-1} \sum_{n=0}^{w_{ker}-1} \text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)\]

警告

  • Only single input and single output are supported.

  • Global pooling is supported.

  • The height of “kernel_size” and the weight of “kernel_size” are positive integers within the range [1, 255]. ksize_h * ksize_w < 256.

  • Due to instruction restrictions, the values of “strides_h” and “strides_w” are positive integers within the range [1, 63].

参数
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the average value, is an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively. Default: 1.

  • strides (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

  • data_format (str) – The format of input and output data. It should be ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, with shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If kernel_size or strides is neither int nor tuple.

  • ValueError – If pad_mode is neither ‘valid’ nor ‘same’ with not case sensitive.

  • ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.

  • ValueError – If kernel_size or strides is less than 1.

  • ValueError – If length of shape of x is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.avgpool_op = ops.AvgPool(pad_mode="VALID", kernel_size=2, strides=1)
...
...     def construct(self, x):
...         result = self.avgpool_op(x)
...         return result
...
>>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4), mindspore.float32)
>>> net = Net()
>>> output = net(x)
>>> print(output)
[[[[ 2.5   3.5   4.5]
   [ 6.5   7.5   8.5]]
  [[14.5  15.5  16.5]
   [18.5  19.5  20.5]]
  [[26.5  27.5  28.5]
   [30.5  31.5  32.5]]]]
class tinyms.primitives.AvgPool3D(*args, **kwargs)[源代码]

3D Average pooling operation.

Applies a 3D average pooling over an input Tensor which can be regarded as a composition of 3D input planes. Typically the input is of shape \((N, C, D_{in}, H_{in}, W_{in})\), AvgPool3D outputs regional average in the \((D_{in}, H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (d_{ker}, h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1, s_2)\), the operation is as follows.

警告

“kernel_size” is in the range [1, 255]. “strides” is in the range [1, 63].

\[\text{output}(N_i, C_j, d, h, w) = \frac{1}{d_{ker} * h_{ker} * w_{ker}} \sum_{l=0}^{d_{ker}-1} \sum_{m=0}^{h_{ker}-1} \sum_{n=0}^{w_{ker}-1} \text{input}(N_i, C_j, s_0 \times d + l, s_1 \times h + m, s_2 \times w + n)\]
参数
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the average value, is an int number that represents depth, height and width are both kernel_size, or a tuple of three int numbers that represent depth, height and width respectively. Default: 1.

  • strides (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “SAME”, “VALID”, “PAD”, not case sensitive. Default: “VALID”.

    • same: Adopts the way of completion. The depth, height and width of the output will be the same as the input. The total number of padding will be calculated in depth, horizontal and vertical directions and evenly distributed to head and tail, top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the tail, bottom and the right side. If this mode is set, pad must be 0.

    • valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.

    • pad: Implicit paddings on both sides of the input in depth, height, width. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.

  • pad (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to pad. If pad is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to pad[0], pad[1], pad[2], pad[3], pad[4] and pad[5] correspondingly.

  • ceil_mode (bool) – If True, ceil instead of floor to compute the output shape. Default: False.

  • count_include_pad (bool) – If True, averaging calculation will include the zero-padding. Default: True.

  • divisor_override (int) – If specified, it will be used as divisor in the averaging calculation, otherwise kernel_size will be used. Default: 0.

  • data_format (str) – The optional value for data format. Currently only support ‘NCDHW’. Default: ‘NCDHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C, D_{in}, H_{in}, W_{in})\). Currently support float16 and float32 data type.

Outputs:

Tensor, with shape \((N, C, D_{out}, H_{out}, W_{out})\). Has the same data type with x.

引发
  • TypeError – If kernel_size, strides or pad is neither an int not a tuple.

  • TypeError – If ceil_mode or count_include_pad is not a bool.

  • TypeError – If pad_mode or data_format is not a string.

  • TypeError – If divisor_override is not an int.

  • ValueError – If numbers in kernel_size or strides are not positive.

  • ValueError – If kernel_size or strides is a tuple whose length is not equal to 3.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’ or ‘pad’.

  • ValueError – If pad is a tuple whose length is not equal to 6.

  • ValueError – If element of pad is less than 0.

  • ValueError – If pad_mode is not equal to ‘pad’ and pad is not equal to 0 or (0, 0, 0, 0, 0, 0).

  • ValueError – If data_format is not ‘NCDHW’.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.arange(1 * 2 * 2 * 2 * 3).reshape((1, 2, 2, 2, 3)), mindspore.float16)
>>> avg_pool3d = ops.AvgPool3D(kernel_size=2, strides=1, pad_mode="valid")
>>> output = avg_pool3d(x)
>>> print(output)
[[[[[ 5.  6.]]]
  [[[17. 18.]]]]]
class tinyms.primitives.BCEWithLogitsLoss(*args, **kwargs)[源代码]

Adds sigmoid activation function to input logits, and uses the given logits to compute binary cross entropy between the logits and the label.

Sets input logits as \(X\), input label as \(Y\), input weight as \(W\), output as \(L\). Then,

\[\begin{split}\begin{array}{ll} \\ p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}} \\ L_{ij} = -[Y_{ij} * log(p_{ij}) + (1 - Y_{ij})log(1 - p_{ij})] \end{array}\end{split}\]

\(i\) indicates the \(i^{th}\) sample, \(j\) indicates the category. Then,

\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]

\(\ell\) indicates the method of calculating the loss. There are three methods: the first method is to provide the loss value directly, the second method is to calculate the average value of all losses, and the third method is to calculate the sum of all losses.

This operator will multiply the output by the corresponding weight. The tensor weight assigns different weights to each piece of data in the batch, and the tensor pos_weight adds corresponding weights to the positive examples of each category.

In addition, it can trade off recall and precision by adding weights to positive examples. In the case of multi-label classification the loss can be described as:

\[\begin{split}\begin{array}{ll} \\ p_{ij,c} = sigmoid(X_{ij,c}) = \frac{1}{1 + e^{-X_{ij,c}}} \\ L_{ij,c} = -[P_{c}Y_{ij,c} * log(p_{ij,c}) + (1 - Y_{ij,c})log(1 - p_{ij,c})] \end{array}\end{split}\]

where c is the class number (c>1 for multi-label binary classification, c=1 for single-label binary classification), n is the number of the sample in the batch and \(p_c\) is the weight of the positive answer for the class c. \(p_c>1\) increases the recall, \(p_c<1\) increases the precision.

参数

reduction (str) – Type of reduction to be applied to loss. The optional values are ‘mean’, ‘sum’, and ‘none’, not case sensitive. If ‘none’, do not perform reduction. Default:’mean’.

Inputs:
  • logits (Tensor) - Input logits. Data type must be float16 or float32. Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.

  • label (Tensor) - Ground truth label, has the same shape as logits. Data type must be float16 or float32.

  • weight (Tensor) - A rescaling weight applied to the loss of each batch element. It can be broadcast to a tensor with shape of logits. Data type must be float16 or float32.

  • pos_weight (Tensor) - A weight of positive examples. Must be a vector with length equal to the number of classes. It can be broadcast to a tensor with shape of logits. Data type must be float16 or float32.

Outputs:

Tensor or Scalar, if reduction is ‘none’, it’s a tensor with the same shape and type as input logits. Otherwise, the output is a scalar.

引发
  • TypeError – If data type of any input is neither float16 nor float32.

  • ValueError – If weight or pos_weight can not be broadcast to a tensor with shape of logits.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend GPU

实际案例

>>> logits = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]), mindspore.float32)
>>> label = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]), mindspore.float32)
>>> weight = Tensor(np.array([1.0, 1.0, 1.0]), mindspore.float32)
>>> pos_weight = Tensor(np.array([1.0, 1.0, 1.0]), mindspore.float32)
>>> loss = ops.BCEWithLogitsLoss()
>>> output = loss(logits, label, weight, pos_weight)
>>> print(output)
0.3463612
class tinyms.primitives.BNTrainingReduce(*args, **kwargs)[源代码]

For the BatchNorm operation this operator updates the moving averages for training and is used in conjunction with BNTrainingUpdate.

Inputs:
  • x (Tensor) - A 4-D Tensor with float16 or float32 data type. Tensor of shape \((N, C, A, B)\).

Outputs:
  • sum (Tensor) - A 1-D Tensor with float32 data type. Tensor of shape \((C,)\).

  • square_sum (Tensor) - A 1-D Tensor with float16 or float32 data type. Tensor of shape \((C,)\).

引发
  • TypeError – If x is not a Tensor.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.ones([128, 3, 32, 3]), mindspore.float32)
>>> bn_training_reduce = ops.BNTrainingReduce()
>>> output = bn_training_reduce(x)
>>> print(output)
(Tensor(shape=[3], dtype=Float32, value=
[ 1.22880000e+04, 1.22880000e+04, 1.22880000e+04]), Tensor(shape=[3], dtype=Float32, value=
[ 1.22880000e+04, 1.22880000e+04, 1.22880000e+04]))
class tinyms.primitives.BNTrainingUpdate(*args, **kwargs)[源代码]

For the BatchNorm operation, this operator updates the moving averages for training and is used in conjunction with BNTrainingReduce. Where the moving averages is a method of analyzing data points by creating a series of averages of different subsets of the entire data set.

警告

For Ascend 310, the result accuracy fails to reach 1‰ due to the square root instruction.

参数
  • isRef (bool) – If a ref. Default: True. Ref indicates whether to enable the output multiplexing input address.

  • epsilon (float) – A small value added to variance avoid dividing by zero. Default: 1e-5.

  • factor (float) – A weight for updating the mean and variance. Default: 0.1.

Inputs:
  • input_x (Tensor) - A 4-D Tensor with float16 or float32 data type. Tensor of shape \((N, C, A, B)\).

  • sum (Tensor) - A 1-D Tensor with float16 or float32 data type for the output of operator BNTrainingReduce. Tensor of shape \((C,)\).

  • square_sum (Tensor) - A 1-D Tensor with float16 or float32 data type for the output of operator BNTrainingReduce. Tensor of shape \((C,)\).

  • scale (Tensor) - A 1-D Tensor with float16 or float32, for the scaling factor. Tensor of shape \((C,)\).

  • offset (Tensor) - A 1-D Tensor with float16 or float32, for the scaling offset. Tensor of shape \((C,)\).

  • mean (Tensor) - A 1-D Tensor with float16 or float32, for the scaling mean. Tensor of shape \((C,)\).

  • variance (Tensor) - A 1-D Tensor with float16 or float32, for the update variance. Tensor of shape \((C,)\).

Outputs:
  • y (Tensor) - Tensor, has the same shape and data type as input_x.

  • mean (Tensor) - Tensor for the updated mean, with float32 data type. Has the same shape as variance.

  • variance (Tensor) - Tensor for the updated variance, with float32 data type. Has the same shape as variance.

  • batch_mean (Tensor) - Tensor for the mean of input_x, with float32 data type. Has the same shape as variance.

  • batch_variance (Tensor) - Tensor for the mean of variance, with float32 data type. Has the same shape as variance.

引发
  • TypeError – If isRef is not a bool.

  • TypeError – If dtype of epsilon or factor is not float.

  • TypeError – If input_x, sum, square_sum, scale, offset, mean or variance is not a Tensor.

  • TypeError – If dtype of input_x, sum, square_sum, scale, offset, mean or variance is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.ones([1, 2, 2, 2]), mindspore.float32)
>>> sum_val = Tensor(np.ones([2]), mindspore.float32)
>>> square_sum = Tensor(np.ones([2]), mindspore.float32)
>>> scale = Tensor(np.ones([2]), mindspore.float32)
>>> offset = Tensor(np.ones([2]), mindspore.float32)
>>> mean = Tensor(np.ones([2]), mindspore.float32)
>>> variance = Tensor(np.ones([2]), mindspore.float32)
>>> bn_training_update = ops.BNTrainingUpdate()
>>> output = bn_training_update(input_x, sum_val, square_sum, scale, offset, mean, variance)
>>> print(output)
(Tensor(shape=[1, 2, 2, 2], dtype=Float32, value=
[[[[ 2.73200464e+00,  2.73200464e+00],
   [ 2.73200464e+00,  2.73200464e+00]],
  [[ 2.73200464e+00,  2.73200464e+00],
   [ 2.73200464e+00,  2.73200464e+00]]]]), Tensor(shape=[2], dtype=Float32, value= [9.24999952e-01,
9.24999952e-01]), Tensor(shape=[2], dtype=Float32, value= [ 9.24999952e-01, 9.24999952e-01]),
Tensor(shape=[2], dtype=Float32, value= [ 2.50000000e-01, 2.50000000e-01]), Tensor(shape=[2], dtype=Float32,
value= [ 1.87500000e-01, 1.87500000e-01]))
class tinyms.primitives.BasicLSTMCell(*args, **kwargs)[源代码]

It’s similar to operator DynamicRNN. BasicLSTMCell will be deprecated in the future. Please use DynamicRNN instead.

Supported Platforms:

Deprecated

class tinyms.primitives.BatchMatMul(*args, **kwargs)[源代码]

Computes matrix multiplication between two tensors by batch.

\[\begin{split}\\text{output}[..., :, :] = \\text{matrix}(x[..., :, :]) * \\text{matrix}(y[..., :, :])\end{split}\]

The two input tensors must have the same rank and the rank must be not less than 3.

参数
  • transpose_x (bool) – If true, the last two dimensions of x is transposed before multiplication. Default: False.

  • transpose_y (bool) – If true, the last two dimensions of y is transposed before multiplication. Default: False.

Inputs:
  • x (Tensor) - The first tensor to be multiplied. The shape of the tensor is \((*B, N, C)\), where \(*B\) represents the batch size which can be multidimensional, \(N\) and \(C\) are the size of the last two dimensions. If transpose_a is True, its shape must be \((*B, C, N)\).

  • y (Tensor) - The second tensor to be multiplied. The shape of the tensor is \((*B, C, M)\). If transpose_b is True, its shape must be \((*B, M, C)\).

Outputs:

Tensor, the shape of the output tensor is \((*B, N, M)\).

引发
  • TypeError – If transpose_x or transpose_y is not a bool.

  • ValueError – If length of shape of x is not equal to length of shape of y or length of shape of x is less than 3.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.ones(shape=[2, 4, 1, 3]), mindspore.float32)
>>> y = Tensor(np.ones(shape=[2, 4, 3, 4]), mindspore.float32)
>>> batmatmul = ops.BatchMatMul()
>>> output = batmatmul(x, y)
>>> print(output)
[[[[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]]
 [[[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]]]
>>> x = Tensor(np.ones(shape=[2, 4, 3, 1]), mindspore.float32)
>>> y = Tensor(np.ones(shape=[2, 4, 3, 4]), mindspore.float32)
>>> batmatmul = ops.BatchMatMul(transpose_a=True)
>>> output = batmatmul(x, y)
>>> print(output)
[[[[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]]
 [[[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]
  [[3. 3. 3. 3.]]]]
class tinyms.primitives.BatchNorm(*args, **kwargs)[源代码]

Batch Normalization for input data and updated parameters.

Batch Normalization is widely used in convolutional neural networks. This operation applies Batch Normalization over inputs to avoid internal covariate shift as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. It rescales and recenters the features using a mini-batch of data and the learned parameters can be described in the following formula,

\[y = \frac{x - mean}{\sqrt{variance + \epsilon}} * \gamma + \beta\]

where \(\gamma\) is scale, \(\beta\) is bias, \(\epsilon\) is epsilon, \(mean\) is the mean of x, \(variance\) is the variance of x.

警告

  • If the operation is used for inference, and outputs “reserve_space_1” and “reserve_space_2” are available, then “reserve_space_1” has the same value as “mean” and “reserve_space_2” has the same value as “variance”.

  • For Ascend 310, the result accuracy fails to reach 1‰ due to the square root instruction.

参数
  • is_training (bool) – If is_training is True, mean and variance are computed during training. If is_training is False, they’re loaded from checkpoint during inference. Default: False.

  • epsilon (float) – A small value added for numerical stability. Default: 1e-5.

  • momentum (float) – The hyper parameter to compute moving average for running_mean and running_var (e.g. \(new\_running\_mean = (1 - momentum) * running\_mean + momentum * current\_mean\)). Momentum value must be [0, 1]. Default: 0.1.

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: “NCHW”.

Inputs:

If is_training is False, inputs are Tensors.

  • input_x (Tensor) - Tensor of shape \((N, C)\), with float16 or float32 data type.

  • scale (Tensor) - Tensor of shape \((C,)\), with float16 or float32 data type.

  • bias (Tensor) - Tensor of shape \((C,)\), has the same data type with scale.

  • mean (Tensor) - Tensor of shape \((C,)\), has the same data type with scale.

  • variance (Tensor) - Tensor of shape \((C,)\), has the same data type with scale.

If is_training is True, scale, bias, mean and variance are Parameters.

  • input_x (Tensor) - Tensor of shape \((N, C)\), with float16 or float32 data type.

  • scale (Parameter) - Parameter of shape \((C,)\), with float16 or float32 data type.

  • bias (Parameter) - Parameter of shape \((C,)\), has the same data type with scale.

  • mean (Parameter) - Parameter of shape \((C,)\), has the same data type with scale.

  • variance (Parameter) - Parameter of shape \((C,)\), has the same data type with scale.

Outputs:

Tuple of 5 Tensors, the normalized inputs and the updated parameters.

  • output_x (Tensor) - The same type and shape as the input_x. The shape is \((N, C)\).

  • updated_scale (Tensor) - Tensor of shape \((C,)\).

  • updated_bias (Tensor) - Tensor of shape \((C,)\).

  • reserve_space_1 (Tensor) - Tensor of shape \((C,)\).

  • reserve_space_2 (Tensor) - Tensor of shape \((C,)\).

引发
  • TypeError – If is_training is not a bool.

  • TypeError – If dtype of epsilon or momentum is not float.

  • TypeError – If data_format is not a str.

  • TypeError – If input_x, scale, bias, mean or variance is not a Tensor.

  • TypeError – If dtype of input_x, scale is neither float16 nor float32.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> input_x = Tensor(np.ones([2, 2]), mindspore.float32)
>>> scale = Tensor(np.ones([2]), mindspore.float32)
>>> bias = Tensor(np.ones([2]), mindspore.float32)
>>> mean = Tensor(np.ones([2]), mindspore.float32)
>>> variance = Tensor(np.ones([2]), mindspore.float32)
>>> batch_norm = ops.BatchNorm()
>>> output = batch_norm(input_x, scale, bias, mean, variance)
>>> print(output[0])
[[1. 1.]
 [1. 1.]]
class tinyms.primitives.BatchToSpace(*args, **kwargs)[源代码]

Divides batch dimension with blocks and interleaves these blocks back into spatial dimensions.

This operation will divide batch dimension N into blocks with block_size, the output tensor’s N dimension is the corresponding number of blocks after division. The output tensor’s H, W dimension is product of original H, W dimension and block_size with given amount to crop from dimension, respectively.

参数
  • block_size (int) – The block size of division, has the value not less than 2.

  • crops (Union[list(int), tuple(int)]) – The crop value for H and W dimension, containing 2 subtraction lists. Each list contains 2 integers. All values must be not less than 0. crops[i] specifies the crop values for the spatial dimension i, which corresponds to the input dimension i+2. It is required that input_shape[i+2]*block_size >= crops[i][0]+crops[i][1].

Inputs:
  • input_x (Tensor) - The input tensor. It must be a 4-D tensor, dimension 0 must be divisible by product of block_shape. The data type is float16 or float32.

Outputs:

Tensor, the output tensor with the same type as input. Assume input shape is (n, c, h, w) with block_size and crops. The output shape will be (n’, c’, h’, w’), where

\(n' = n//(block\_size*block\_size)\)

\(c' = c\)

\(h' = h*block\_size-crops[0][0]-crops[0][1]\)

\(w' = w*block\_size-crops[1][0]-crops[1][1]\)

引发
  • TypeError – If block_size or element of crops is not an int.

  • TypeError – If crops is neither list nor tuple.

  • ValueError – If block_size is less than 2.

Supported Platforms:

Ascend GPU

实际案例

>>> block_size = 2
>>> crops = [[0, 0], [0, 0]]
>>> batch_to_space = ops.BatchToSpace(block_size, crops)
>>> input_x = Tensor(np.array([[[[1]]], [[[2]]], [[[3]]], [[[4]]]]), mindspore.float32)
>>> output = batch_to_space(input_x)
>>> print(output)
[[[[1.  2.]
   [3.  4.]]]]
class tinyms.primitives.BatchToSpaceND(*args, **kwargs)[源代码]

Divides batch dimension with blocks and interleaves these blocks back into spatial dimensions.

This operation will divide batch dimension N into blocks with block_shape, the output tensor’s N dimension is the corresponding number of blocks after division. The output tensor’s H, W dimension is product of original H, W dimension and block_shape with given amount to crop from dimension, respectively.

参数
  • block_shape (Union[list(int), tuple(int), int]) – The block shape of dividing block with all value greater than 1. If block_shape is a tuple or list, the length of block_shape is M corresponding to the number of spatial dimensions. If block_shape is a int, the block size of M dimendions are the same, equal to block_shape. M must be 2.

  • crops (Union[list(int), tuple(int)]) – The crop value for H and W dimension, containing 2 subtraction list, each containing 2 int value. All values must be >= 0. crops[i] specifies the crop values for spatial dimension i, which corresponds to input dimension i+2. It is required that input_shape[i+2]*block_shape[i] > crops[i][0]+crops[i][1].

Inputs:
  • input_x (Tensor) - The input tensor. It must be a 4-D tensor, dimension 0 must be divisible by product of block_shape. The data type is float16 or float32.

Outputs:

Tensor, the output tensor with the same type as input. Assume input shape is (n, c, h, w) with block_shape and crops. The output shape will be (n’, c’, h’, w’), where

\(n' = n//(block\_shape[0]*block\_shape[1])\)

\(c' = c\)

\(h' = h*block\_shape[0]-crops[0][0]-crops[0][1]\)

\(w' = w*block\_shape[1]-crops[1][0]-crops[1][1]\)

引发
  • TypeError – If block_shape is not one of list, tuple, int.

  • TypeError – If crops is neither list nor tuple.

  • ValueError – If length of block_shape or crops is not equal to 2.

Supported Platforms:

Ascend

实际案例

>>> block_shape = [2, 2]
>>> crops = [[0, 0], [0, 0]]
>>> batch_to_space_nd = ops.BatchToSpaceND(block_shape, crops)
>>> input_x = Tensor(np.array([[[[1]]], [[[2]]], [[[3]]], [[[4]]]]), mindspore.float32)
>>> output = batch_to_space_nd(input_x)
>>> print(output)
[[[[1.  2.]
   [3.  4.]]]]
class tinyms.primitives.BesselI0e(*args, **kwargs)[源代码]

Computes BesselI0e of input element-wise.

Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions. Data type must be float16 or float32.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> bessel_i0e = ops.BesselI0e()
>>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32)
>>> output = bessel_i0e(x)
>>> print(output)
[0.7979961  0.5144438  0.75117415  0.9157829 ]
class tinyms.primitives.BesselI1e(*args, **kwargs)[源代码]

Computes BesselI1e of input element-wise.

Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions. Data type must be float16 or float32.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> bessel_i1e = ops.BesselI1e()
>>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32)
>>> output = bessel_i1e(x)
>>> print(output)
[0.09507662 0.19699717 0.11505538 0.04116856]
class tinyms.primitives.BiasAdd(*args, **kwargs)[源代码]

Returns sum of input and bias tensor.

Adds the 1-D bias tensor to the input tensor, and broadcasts the shape on all axis except for the channel axis.

参数

data_format (str) – The format of input and output data. It should be ‘NHWC’, ‘NCHW’ or ‘NCDHW’. Default is ‘NCHW’.

Inputs:
  • input_x (Tensor) - The input tensor. The shape can be 2-5 dimensions. The data type should be float16 or float32.

  • bias (Tensor) - The bias tensor, with shape \((C)\). The shape of bias must be the same as input_x’s channel dimension. The data type should be float16 or float32.

Outputs:

Tensor, with the same shape and data type as input_x.

引发
  • TypeError – If data_format is not a str.

  • TypeError – If input_x or bias is not a Tensor.

  • TypeError – If dtype of input_x or bias is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.arange(6).reshape((2, 3)), mindspore.float32)
>>> bias = Tensor(np.random.random(3).reshape((3,)), mindspore.float32)
>>> bias_add = ops.BiasAdd()
>>> output = bias_add(input_x, bias)
>>> print(output.shape)
(2, 3)
class tinyms.primitives.BinaryCrossEntropy(*args, **kwargs)[源代码]

Computes the binary cross entropy between the logits and the labels.

Sets logits as \(x\), labels as \(y\), output as \(\ell(x, y)\). Let,

\[L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]\]

In which, \(L\) indicates the loss of all batch_sizes, \(l\) indicates the loss of one batch_size, and n indicates one batch_size in the 1-N range. Then,

\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]

警告

  • The value of “x” must range from 0 to 1.

  • The value of “y” must be “0” or “1”.

参数

reduction (str) – Specifies the reduction to be applied to the output. Its value must be one of ‘none’, ‘mean’, ‘sum’. Default: ‘mean’.

Inputs:
  • logits (Tensor) - The input Tensor. The data type must be float16 or float32, The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • labels (Tensor) - The label Tensor which has same shape and data type as logits.

  • weight (Tensor, optional) - A rescaling weight applied to the loss of each batch element. And it must have same shape and data type as logits. Default: None.

Outputs:

Tensor or Scalar, if reduction is ‘none’, then output is a tensor and has the same shape as logits. Otherwise, the output is a scalar.

引发
  • TypeError – If dtype of logits, labels or weight (if given) is neither float16 not float32.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

  • ValueError – If shape of labels is not the same as logits or weight (if given).

  • TypeError – If logits, labels or weight is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.binary_cross_entropy = ops.BinaryCrossEntropy()
...     def construct(self, logits, labels, weight):
...         result = self.binary_cross_entropy(logits, labels, weight)
...         return result
...
>>> net = Net()
>>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
>>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32)
>>> weight = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = net(logits, labels, weight)
>>> print(output)
0.38240486
class tinyms.primitives.BitwiseAnd(*args, **kwargs)[源代码]

Returns bitwise and of two tensors element-wise.

\[out_i = x_{i} \wedge y_{i}\]

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • x (Tensor) - The input tensor with int16, int32 or uint16 data type. \((N,*)\) where \(*\) means, any number of additional dimensions.

  • y (Tensor) - The input tensor with same type as the x.

Outputs:

Tensor, has the same type as the x.

引发

TypeError – If x or y is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mindspore.int16)
>>> y = Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mindspore.int16)
>>> bitwise_and = ops.BitwiseAnd()
>>> output = bitwise_and(x, y)
>>> print(output)
[ 0  0  1 -1  1  0  1]
class tinyms.primitives.BitwiseOr(*args, **kwargs)[源代码]

Returns bitwise or of two tensors element-wise.

\[out_i = x_{i} \mid y_{i}\]

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • x (Tensor) - The input tensor with int16, int32 or uint16 data type.

  • y (Tensor) - The input tensor with same type as the x.

Outputs:

Tensor, has the same type as the x.

引发

TypeError – If x or y is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mindspore.int16)
>>> y = Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mindspore.int16)
>>> bitwise_or = ops.BitwiseOr()
>>> output = bitwise_or(x, y)
>>> print(output)
[ 0  1  1 -1 -1  3  3]
class tinyms.primitives.BitwiseXor(*args, **kwargs)[源代码]

Returns bitwise xor of two tensors element-wise.

\[out_i = x_{i} \oplus y_{i}\]

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • x (Tensor) - The input tensor with int16, int32 or uint16 data type.

  • y (Tensor) - The input tensor with same type as the x.

Outputs:

Tensor, has the same type as the x.

引发

TypeError – If x or y is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mindspore.int16)
>>> y = Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mindspore.int16)
>>> bitwise_xor = ops.BitwiseXor()
>>> output = bitwise_xor(x, y)
>>> print(output)
[ 0  1  0  0 -2  3  2]
class tinyms.primitives.BondAtomEnergy(*args, **kwargs)[源代码]

Add the potential energy caused by simple harmonic bonds to the total potential energy of each atom.

The calculation formula is the same as operator BondEnergy().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • atom_numbers (int32) – the number of atoms n.

  • bond_numbers (int32) – the number of harmonic bonds m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor (x, y, z), between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The first atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The second atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • bond_k (Tensor) - The force constant of each bond. The data type is float32 and the shape is \((m,)\).

  • bond_r0 (Tensor) - The equlibrium length of each bond. The data type is float32 and the shape is \((m,)\).

Outputs:
  • atom_ene (Tensor) - The accumulated potential energy for each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.BondEnergy(*args, **kwargs)[源代码]

Calculate the harmonic potential energy between each bonded atom pair. Assume our system has n atoms and m harmonic bonds.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr = (x_1-x_2, y_1-y_2, z_1-z_2)\]
\[E = k*(|dr| - r_0)^2\]
参数
  • atom_numbers (int32) – the number of atoms n.

  • bond_numbers (int32) – the number of harmonic bonds m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor (x, y, z), between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The first atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The second atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • bond_k (Tensor) - The force constant of each bond. The data type is float32 and the shape is \((m,)\).

  • bond_r0 (Tensor) - The equlibrium length of each bond. The data type is float32 and the shape is \((m,)\).

Outputs:
  • bond_ene (Tensor) - The harmonic potential energy for each bond. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.BondForce(*args, **kwargs)[源代码]

Calculate the force exerted by the simple harmonic bond on the corresponding atoms. Assume the number of harmonic bonds is m and the number of atoms is n.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr = (x_1-x_2, y_1-y_2, z_1-z_2)\]
\[F = (F_x, F_y, F_z) = 2*k*(1 - r_0/|dr|)*dr\]
参数
  • atom_numbers (int32) – the number of atoms n.

  • bond_numbers (int32) – the number of harmonic bonds m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor (x, y, z), between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The first atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The second atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • bond_k (Tensor) - The force constant of each bond. The data type is float32 and the shape is \((m,)\).

  • bond_r0 (Tensor) - The equlibrium length of each bond. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.BondForceWithAtomEnergy(*args, **kwargs)[源代码]

Calculate bond force and harmonic potential energy together.

The calculation formula is the same as operator BondForce() and BondEnergy().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • atom_numbers (int32) – the number of atoms n.

  • bond_numbers (int32) – the number of harmonic bonds m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor (x, y, z), between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The first atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The second atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • bond_k (Tensor) - The force constant of each bond. The data type is float32 and the shape is \((m,)\).

  • bond_r0 (Tensor) - The equlibrium length of each bond. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • atom_e (Tensor) - The accumulated potential energy for each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.BondForceWithAtomEnergyAndVirial(*args, **kwargs)[源代码]

Calculate bond force, harmonic potential energy and atom virial together.

The calculation formula is the same as operator BondForce() and BondEnergy().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • bond_numbers (int32) – the number of harmonic bonds m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor (x, y, z), The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The first atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The second atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • bond_k (Tensor) - The force constant of each bond. The data type is float32 and the shape is \((m,)\).

  • bond_r0 (Tensor) - The equlibrium length of each bond. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - The force of each atom. The data type is float32 and the shape is \((n, 3)\).

  • atom_e (Tensor) - The energy of each atom. The data type is float32 and the shape is \((n,)\).

  • atom_virial (Tensor) - The virial of each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.BondForceWithAtomVirial(*args, **kwargs)[源代码]

Calculate bond force and the virial coefficient caused by simple harmonic bond for each atom together.

The calculation formula of the force part is the same as operator BondForce().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

The Virial part is as follows:

\[dr = (x_1-x_2, y_1-y_2, z_1-z_2)\]
\[virial = |dr|*(|dr| - r_0)*k\]
参数
  • atom_numbers (int32) – the number of atoms n.

  • bond_numbers (int32) – the number of harmonic bonds m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor (x, y, z), between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The first atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The second atom index of each bond. The data type is int32 and the shape is \((m,)\).

  • bond_k (Tensor) - The force constant of each bond. The data type is float32 and the shape is \((m,)\).

  • bond_r0 (Tensor) - The equlibrium length of each bond. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - Same as operator BondForce(). The data type is float32 and the shape is \((n, 3)\).

  • atom_v (Tensor) - The accumulated virial coefficient for each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.BoundingBoxDecode(*args, **kwargs)[源代码]

Decodes bounding boxes locations.

参数
  • means (tuple) – The means of deltas calculation. Default: (0.0, 0.0, 0.0, 0.0).

  • stds (tuple) – The standard deviations of deltas calculation. Default: (1.0, 1.0, 1.0, 1.0).

  • max_shape (tuple) – The max size limit for decoding box calculation.

  • wh_ratio_clip (float) – The limit of width and height ratio for decoding box calculation. Default: 0.016.

Inputs:
  • anchor_box (Tensor) - Anchor boxes. The shape of anchor_box must be (n, 4).

  • deltas (Tensor) - Delta of boxes. Which has the same shape with anchor_box.

Outputs:

Tensor, decoded boxes. It has the same data type and shape as anchor_box.

引发
  • TypeError – If means, stds or max_shape is not a tuple.

  • TypeError – If wh_ratio_clip is not a float.

  • TypeError – If anchor_box or deltas is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> anchor_box = Tensor([[4, 1, 2, 1], [2, 2, 2, 3]], mindspore.float32)
>>> deltas = Tensor([[3, 1, 2, 2], [1, 2, 1, 4]], mindspore.float32)
>>> boundingbox_decode = ops.BoundingBoxDecode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0),
...                                          max_shape=(768, 1280), wh_ratio_clip=0.016)
>>> output = boundingbox_decode(anchor_box, deltas)
>>> print(output)
[[ 4.1953125  0.         0.         5.1953125]
 [ 2.140625   0.         3.859375  60.59375  ]]
class tinyms.primitives.BoundingBoxEncode(*args, **kwargs)[源代码]

Encodes bounding boxes locations.

参数
  • means (tuple) – Means for encoding bounding boxes calculation. Default: (0.0, 0.0, 0.0, 0.0).

  • stds (tuple) – The standard deviations of deltas calculation. Default: (1.0, 1.0, 1.0, 1.0).

Inputs:
  • anchor_box (Tensor) - Anchor boxes. The shape of anchor_box must be (n, 4).

  • groundtruth_box (Tensor) - Ground truth boxes. Which has the same shape with anchor_box.

Outputs:

Tensor, encoded bounding boxes. It has the same data type and shape as input anchor_box.

引发
  • TypeError – If means or stds is not a tuple.

  • TypeError – If anchor_box or groundtruth_box is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> anchor_box = Tensor([[2, 2, 2, 3], [2, 2, 2, 3]], mindspore.float32)
>>> groundtruth_box = Tensor([[1, 2, 1, 4], [1, 2, 1, 4]], mindspore.float32)
>>> boundingbox_encode = ops.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0))
>>> output = boundingbox_encode(anchor_box, groundtruth_box)
>>> print(output)
[[ -1.  0.25  0.  0.40551758]
 [ -1.  0.25  0.  0.40551758]]
class tinyms.primitives.Broadcast(*args, **kwargs)[源代码]

Broadcasts the tensor to the whole group.

注解

The tensors must have the same shape and format in all processes of the collection.

参数
  • root_rank (int) – Source rank. Required in all processes except the one that is sending the data.

  • group (str) – The communication group to work on. Default: “hccl_world_group”.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

Tensor, has the same shape of the input, i.e., \((x_1, x_2, ..., x_R)\). The contents depend on the data of the root_rank device.

引发

TypeError – If root_rank is not a integer or group is not a string.

Supported Platforms:

Ascend GPU

实际案例

>>> # This example should be run with multiple processes.
>>> # Please refer to the tutorial > Distributed Training on mindspore.cn.
>>> from mindspore import Tensor
>>> from mindspore import context
>>> from mindspore.communication import init
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>> import numpy as np
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.broadcast = ops.Broadcast(1)
...
...     def construct(self, x):
...         return self.broadcast((x,))
...
>>> input_x = Tensor(np.ones([2, 4]).astype(np.int32))
>>> net = Net()
>>> output = net(input_x)
>>> print(output)
(Tensor(shape[2,4], dtype=Int32, value=
[[1, 1, 1, 1],
 [1, 1, 1, 1]]),)
class tinyms.primitives.BroadcastTo(*args, **kwargs)[源代码]

Broadcasts input tensor to a given shape.

Input shape can be broadcast to target shape if for each dimension pair they are either equal or input is one or the target dimension is -1. In case of -1 in target shape, it will be replaced by the input shape’s value in that dimension.

When input shape is broadcast to target shape, it starts with the trailing dimensions. If there is a -1 in the target shape, the -1 cannot be in a leading, non-existing dimension.

参数

shape (tuple) – The target shape to broadcast. Can be fully specified, or have -1 in one position where it will be substituted by the input tensor’s shape in that position, see example.

Inputs:
  • input_x (Tensor) - The input tensor. The data type should be one of the following types: float16, float32, int32, int8, uint8. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

Outputs:

Tensor, with the given shape and the same data type as input_x.

引发
  • TypeError – If shape is not a tuple.

  • ValueError – if the target and input shapes are incompatible, or if a - 1 in the target shape is in an invalid location.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> shape = (2, 3)
>>> input_x = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> broadcast_to = ops.BroadcastTo(shape)
>>> output = broadcast_to(input_x)
>>> print(output)
[[1. 2. 3.]
 [1. 2. 3.]]
>>> shape = (-1, 2)
>>> input_x = Tensor(np.array([[1], [2]]).astype(np.float32))
>>> broadcast_to = ops.BroadcastTo(shape)
>>> output = broadcast_to(input_x)
>>> print(output)
[[1. 1.]
 [2. 2.]]
class tinyms.primitives.BufferAppend(*args, **kwargs)[源代码]

In reinforcement learning, the experience data is collected in each step. We use BufferAppend to push data to the bottom of buffer under the First-In-First-Out rule.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • capacity (int64) – Capacity of the buffer, must be non-negative.

  • buffer_shape (tuple(shape)) – The shape of an buffer.

  • buffer_dtype (tuple(type)) – The type of an buffer.

Inputs:
  • data (tuple(Parameter(Tensor))) - The tuple(Tensor) represents replaybuffer,

each tensor is described by the buffer_shape and buffer_type.

  • exp (tuple(Parameter(Tensor))) - The tuple(Tensor) represents one list of experince data,

each tensor is described by the buffer_shape and buffer_type.

  • count (Parameter) - The count means the real available size of the buffer,

data type: int32.

  • head (Parameter) - The position of the first data in buffer, data type: int32.

Outputs:

None.

引发
  • ValueError – If count and head is not a integer.

  • ValueError – If capacity is not a positive integer.

  • ValueError – If length of data not equal to length of exp.

  • ValueError – If dim of data euqals to dim of exp, but data[1:] not equal to the shape in exp.

  • ValueError – If the shape of data[1:] not equal to the shape in exp.

  • TypeError – If the type in exp is not the same with data.

Supported Platforms:

GPU CPU

实际案例

>>> capacity = 100
>>> count = Parameter(Tensor(5, ms.int32), name="count")
>>> head = Parameter(Tensor(0, ms.int32), name="head")
>>> buffer = [Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="states"),
              Parameter(Tensor(np.arange(100 * 2).reshape(100, 2).astype(np.int32)), name="action"),
              Parameter(Tensor(np.ones((100, 1)).astype(np.int32)), name="reward"),
              Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="state_")]
>>> exp = [Tensor(np.array([2, 2, 2, 2]), ms.float32), Tensor(np.array([0, 0]), ms.int32),
           Tensor(np.array([0]), ms.int32), Tensor(np.array([3, 3, 3, 3]), ms.float32)]
>>> batch_exp = [Tensor(np.array([[2, 2, 2, 2], [2, 2, 2, 2]]), ms.float32),
                 Tensor(np.array([[0, 0], [0, 0]), ms.int32),
                 Tensor(np.array([[0], [0]]), ms.int32),
                 Tensor(np.array([[3, 3, 3, 3], [3, 3, 3, 3]]), ms.float32)]
>>> buffer_append = ops.BufferAppend(capacity, shapes, types)
>>> buffer_append(buffer, exp, count, head)
>>> buffer_append(buffer, batch_exp, count, head)
class tinyms.primitives.BufferGetItem(*args, **kwargs)[源代码]

Get the data from buffer in the position of input index.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • capacity (int64) – Capacity of the buffer, must be non-negative.

  • buffer_shape (tuple(shape)) – The shape of an buffer.

  • buffer_dtype (tuple(type)) – The type of an buffer.

Inputs:
  • data (tuple(Parameter(Tensor))) - The tuple(Tensor) represents replaybuffer,

each tensor is described by the buffer_shape and buffer_type.

  • count (Parameter) - The count means the real available size of the buffer,

data type: int32.

  • head (Parameter) - The position of the first data in buffer, data type: int32.

  • index (int64) - The position of the data in buffer.

Outputs:

tuple(Tensor). The shape is buffer_shape. The dtype is buffer_dtype.

引发
  • ValueError – If count and head is not a integer.

  • ValueError – If capacity is not a positive integer.

  • TypeError – If buffer_shape is not a tuple.

Supported Platforms:

GPU CPU

实际案例

>>> capacity = 100
>>> index = 3
>>> count = Parameter(Tensor(5, ms.int32), name="count")
>>> head = Parameter(Tensor(0, ms.int32), name="head")
>>> buffer = [Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="states"),
              Parameter(Tensor(np.arange(100 * 2).reshape(100, 2).astype(np.int32)), name="action"),
              Parameter(Tensor(np.ones((100, 1)).astype(np.int32)), name="reward"),
              Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="state_")]
>>> buffer_get = ops.BufferGetItem(capacity, shapes, types)
>>> output = buffer_get(buffer, count, head, index)
>>> print(output)
    (Tensor(shape=[4], dtype=Float32, value=
        [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01]),
     Tensor(shape=[2], dtype=Int32, value= [6, 7]),
     Tensor(shape=[1], dtype=Int32, value= [1]),
     Tensor(shape=[4], dtype=Float32, value=
        [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01]))
class tinyms.primitives.BufferSample(*args, **kwargs)[源代码]

In reinforcement learning, the data is sampled from the replaybuffer randomly.

Returns the tuple tensor with the given shape, decided by the given batchsize.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • capacity (int64) – Capacity of the buffer, must be non-negative.

  • batch_size (int64) – The size of the sampled data, lessequal to capacity.

  • buffer_shape (tuple(shape)) – The shape of an buffer.

  • buffer_dtype (tuple(type)) – The type of an buffer.

  • seed (int64) – Random seed for sample. Default: 0. If use the default seed, it will generate a ramdom

  • in kernel. Set a number other than 0 to keep a specific seed. Default (one) –

  • unique (bool) – Whether the sampled data is strictly unique. Setting it to False has a better performance. Default: False

Inputs:
  • data (tuple(Parameter(Tensor))) - The tuple(Tensor) represents replaybuffer,

each tensor is described by the buffer_shape and buffer_type.

  • count (Parameter) - The count means the real available size of the buffer,

data type: int32.

  • head (Parameter) - The position of the first data in buffer, data type: int32.

Outputs:

tuple(Tensor). The shape is batch_size * buffer_shape. The dtype is buffer_dtype.

引发
  • TypeError – If buffer_shape is not a tuple.

  • ValueError – If batch_size is larger than capacity.

  • ValueError – If capacity is not a positive integer.

Supported Platforms:

GPU CPU

实际案例

>>> capacity = 100
>>> batch_size = 5
>>> count = Parameter(Tensor(5, ms.int32), name="count")
>>> head = Parameter(Tensor(0, ms.int32), name="head")
>>> shapes = [(4,), (2,), (1,), (4,)]
>>> types = [ms.float32, ms.int32, ms.int32, ms.float32]
>>> buffer = [Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="states"),
              Parameter(Tensor(np.arange(100 * 2).reshape(100, 2).astype(np.int32)), name="action"),
              Parameter(Tensor(np.ones((100, 1)).astype(np.int32)), name="reward"),
              Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="state_")]
>>> buffer_sample = ops.BufferSample(capacity, batch_size, shapes, types)
>>> output = buffer_sample(buffer, count, head)
>>> print(output)
    (Tensor(shape=[5, 4], dtype=Float32, value=
        [[ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00, 3.00000000e+00],
        [ 8.00000000e+00, 9.00000000e+00, 1.00000000e+01, 1.10000000e+01],
        [ 1.60000000e+01, 1.70000000e+01, 1.80000000e+01, 1.90000000e+01],
        [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01],
        [ 3.20000000e+01, 3.30000000e+01, 3.40000000e+01, 3.50000000e+01]]),
     Tensor(shape=[5, 2], dtype=Int32, value=
        [[ 0, 1],
        [ 4, 5],
        [ 8, 9],
        [ 6, 7],
        [16, 17]]),
     Tensor(shape=[5, 1], dtype=Int32, value=
        [[1],
        [1],
        [1],
        [1],
        [1]]),
     Tensor(shape=[5, 4], dtype=Float32, value=
        [[ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00, 3.00000000e+00],
        [ 8.00000000e+00, 9.00000000e+00, 1.00000000e+01, 1.10000000e+01],
        [ 1.60000000e+01, 1.70000000e+01, 1.80000000e+01, 1.90000000e+01],
        [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01],
        [ 3.20000000e+01, 3.30000000e+01, 3.40000000e+01, 3.50000000e+01]]))
class tinyms.primitives.CTCGreedyDecoder(*args, **kwargs)[源代码]

Performs greedy decoding on the logits given in inputs.

参数

merge_repeated (bool) – If true, merge repeated classes in output. Default: True.

Inputs:
  • inputs (Tensor) - The input Tensor must be a 3-D tensor whose shape is \((max\_time, batch\_size, num\_classes)\). num_classes must be num_labels + 1 classes, num_labels indicates the number of actual labels. Blank labels are reserved. Default blank label is num_classes - 1. Data type must be float32 or float64.

  • sequence_length (Tensor) - A tensor containing sequence lengths with the shape of \((batch\_size, )\). The type must be int32. Each value in the tensor must be equal to or less than max_time.

Outputs:
  • decoded_indices (Tensor) - A tensor with shape of \((total\_decoded\_outputs, 2)\). Data type is int64.

  • decoded_values (Tensor) - A tensor with shape of \((total\_decoded\_outputs, )\), it stores the decoded classes. Data type is int64.

  • decoded_shape (Tensor) - A tensor with shape of \((batch\_size, max\_decoded\_legth)\). Data type is int64.

  • log_probability (Tensor) - A tensor with shape of \((batch\_size, 1)\), containing sequence log-probability, has the same type as inputs.

引发
  • TypeError – If merge_repeated is not a bool.

  • ValueError – If length of shape of inputs is not equal to 3.

  • ValueError – If length of shape of sequence_length is not equal to 1.

Supported Platforms:

Ascend

实际案例

>>> inputs = Tensor(np.array([[[0.6, 0.4, 0.2], [0.8, 0.6, 0.3]],
...                           [[0.0, 0.6, 0.0], [0.5, 0.4, 0.5]]]), mindspore.float32)
>>> sequence_length = Tensor(np.array([2, 2]), mindspore.int32)
>>> ctc_greedyDecoder = ops.CTCGreedyDecoder()
>>> decoded_indices, decoded_values, decoded_shape, log_probability = ctc_greedyDecoder(inputs, sequence_length)
>>> print(decoded_indices)
[[0 0]
 [0 1]
 [1 0]]
>>> print(decoded_values)
[0 1 0]
>>> print(decoded_shape)
[2 2]
>>> print(log_probability)
[[-1.2]
 [-1.3]]
class tinyms.primitives.CTCLoss(*args, **kwargs)[源代码]

Calculates the CTC (Connectionist Temporal Classification) loss and the gradient.

The CTC algorithm is proposed in Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with Recurrent Neural Networks.

参数
  • preprocess_collapse_repeated (bool) – If true, repeated labels will be collapsed prior to the CTC calculation. Default: False.

  • ctc_merge_repeated (bool) – If false, during CTC calculation, repeated non-blank labels will not be merged and these labels will be interpreted as individual ones. This is a simplfied version of CTC. Default: True.

  • ignore_longer_outputs_than_inputs (bool) – If true, sequences with longer outputs than inputs will be ignored. Default: False.

Inputs:
  • x (Tensor) - The input Tensor must be a 3-D tensor whose shape is \((max\_time, batch\_size, num\_classes)\). num_classes must be num_labels + 1 classes, num_labels indicates the number of actual labels. Blank labels are reserved. Default blank label is num_classes - 1. Data type must be float16, float32 or float64.

  • labels_indices (Tensor) - The indices of labels. labels_indices[i, :] = [b, t] means labels_values[i] stores the id for (batch b, time t). The type must be int64 and rank must be 2.

  • labels_values (Tensor) - A 1-D input tensor. The values are associated with the given batch and time. The type must be int32. labels_values[i] must in the range of [0, num_classes).

  • sequence_length (Tensor) - A tensor containing sequence lengths with the shape of \((batch\_size, )\). The type must be int32. Each value in the tensor must not be greater than max_time.

Outputs:
  • loss (Tensor) - A tensor containing log-probabilities, the shape is \((batch\_size, )\). The tensor has the same data type as x.

  • gradient (Tensor) - The gradient of loss, has the same shape and data type as x.

引发
  • TypeError – If preprocess_collapse_repeated, ctc_merge_repeated or ignore_longer_outputs_than_inputs is not a bool.

  • TypeError – If x, labels_indices, labels_values or sequence_length is not a Tensor.

  • ValueError – If rank of labels_indices is not equal 2.

  • TypeError – If dtype of x is not one of the following: float16, float32 or float64.

  • TypeError – If dtype of labels_indices is not int64.

  • TypeError – If dtype of labels_values or sequence_length is not int32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[[0.3, 0.6, 0.6],
...                       [0.4, 0.3, 0.9]],
...
...                      [[0.9, 0.4, 0.2],
...                       [0.9, 0.9, 0.1]]]).astype(np.float32))
>>> labels_indices = Tensor(np.array([[0, 0], [1, 0]]), mindspore.int64)
>>> labels_values = Tensor(np.array([2, 2]), mindspore.int32)
>>> sequence_length = Tensor(np.array([2, 2]), mindspore.int32)
>>> ctc_loss = ops.CTCLoss()
>>> loss, gradient = ctc_loss(x, labels_indices, labels_values, sequence_length)
>>> print(loss)
[ 0.79628  0.5995158 ]
>>> print(gradient)
[[[ 0.27029088  0.36485454  -0.6351454  ]
  [ 0.28140804  0.25462854  -0.5360366 ]]
 [[ 0.47548494  0.2883962    0.04510255 ]
  [ 0.4082751   0.4082751    0.02843709 ]]]
class tinyms.primitives.CalculateNowrapCrd(*args, **kwargs)[源代码]

Calculate the inside-box periodic image of each atom.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数

atom_numbers (int32) – the number of atoms n.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • box (Tensor) - The 3-D size of system. The data type is float32 and the shape is \((3, )\).

  • box_map_times (Tensor) - The number of times each atom has crossed the box. The data type is int32 and the shape is \((n, 3)\).

Outputs:
  • nowrap_crd (Tensor) - The inside-box periodic image of each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.Cast(*args, **kwargs)[源代码]

Returns a tensor with the new specified data type.

Inputs:
  • input_x (Union[Tensor, Number]) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The tensor to be cast.

  • type (dtype.Number) - The valid data type of the output tensor. Only constant value is allowed.

Outputs:

Tensor, the shape of tensor is the same as input_x, \((x_1, x_2, ..., x_R)\).

引发
  • TypeError – If input_x is neither Tensor nor Number.

  • TypeError – If type is not a Number.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
>>> input_x = Tensor(input_np)
>>> type_dst = mindspore.int32
>>> cast = ops.Cast()
>>> output = cast(input_x, type_dst)
>>> print(output.dtype)
Int32
>>> print(output.shape)
(2, 3, 4, 5)
class tinyms.primitives.Cdist(*args, **kwargs)[源代码]

Computes batched the p norm distance between each pair of the two collections of row vectors.

参数

p (float) – P value for the p norm distance to calculate between each vector pair ∈[0,∞].

Inputs:
  • input_x (Tensor) - Input tensor of shape \((B, P, M)\). Letter \(B\) represents 0 or positive int number. When \(B\) is equal to 0, it means this dimension can be ignored, i.e. shape of the tensor is \((P, M)\).

  • input_y (Tensor) - Input tensor of shape \((B, R, M)\).

Outputs:

Tensor, has the same dtype as input_x, which shape is \((B, P, R)\).

引发
  • TypeError – If input_x or input_y is not a Tensor.

  • TypeError – If dtype of input_x or input_y is neither float16 nor float32.

  • TypeError – If p is not a float.

  • ValueError – If p is a negative float.

  • ValueError – If dimension of input_x is not the same as input_y.

  • ValueError – If dimension of input_x or input_y is neither 2 nor 3.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([[[1.0, 1.0], [2.0, 2.0]]]).astype(np.float32))
>>> input_y = Tensor(np.array([[[3.0, 3.0], [3.0, 3.0]]]).astype(np.float32))
>>> op = ops.Cdist(p=2.0)
>>> output = op(input_x, input_y)
>>> print(output)
[[[2.8284273 2.8284273]
  [1.4142137 1.4142137]]]
class tinyms.primitives.Ceil(*args, **kwargs)[源代码]

Rounds a tensor up to the closest integer element-wise.

\[out_i = \lceil x_i \rceil = \lfloor x_i \rfloor + 1\]
Inputs:
  • x (Tensor) - The input tensor. It’s element data type must be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([1.1, 2.5, -1.5]), mindspore.float32)
>>> ceil_op = ops.Ceil()
>>> output = ceil_op(x)
>>> print(output)
[ 2.  3. -1.]
class tinyms.primitives.CheckBprop(*args, **kwargs)[源代码]

Checks whether the data type and the shape of corresponding elements from tuples x and y are the same.

Inputs:
  • input_x (tuple[Tensor]) - The input_x contains the outputs of bprop to be checked.

  • input_y (tuple[Tensor]) - The input_y contains the inputs of bprop to check against.

Outputs:

(tuple[Tensor]), the input_x, if data type and shape of corresponding elements from input_x and input_y are the same.

引发

TypeError – If input_x or input_y is not a Tensor.

实际案例

>>> input_x = (Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32),)
>>> input_y = (Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32),)
>>> out = ops.CheckBprop()(input_x, input_y)
class tinyms.primitives.CheckValid(*args, **kwargs)[源代码]

Checks bounding box.

Checks whether the bounding box cross data and data border are valid.

警告

specifying the valid boundary (heights x ratio, weights x ratio).

Inputs:
  • bboxes (Tensor) - Bounding boxes tensor with shape (N, 4). Data type must be float16 or float32.

  • img_metas (Tensor) - Raw image size information with the format of (height, width, ratio). Data type must be float16 or float32.

Outputs:

Tensor, with shape of (N,) and dtype of bool.

引发
  • TypeError – If bboxes or img_metas is not a Tensor.

  • TypeError – If dtype of bboxes or img_metas is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore
>>> import mindspore.nn as nn
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.check_valid = ops.CheckValid()
...     def construct(self, x, y):
...         valid_result = self.check_valid(x, y)
...         return valid_result
...
>>> bboxes = Tensor(np.linspace(0, 6, 12).reshape(3, 4), mindspore.float32)
>>> img_metas = Tensor(np.array([2, 1, 3]), mindspore.float32)
>>> net = Net()
>>> output = net(bboxes, img_metas)
>>> print(output)
[ True False False]
class tinyms.primitives.ComputeAccidentalHits(*args, **kwargs)[源代码]

Compute accidental hits of sampled classes which match target classes.

When a target class matches the sample class, we call it “accidental hit”. The result of calculating accidental hits contain three parts (index, id, weight), where index represents the row number in true_classes, and id represents the position in sampled_candidates, the weight is -FLOAT_MAX. FLOAT_MAX indicates the max value in the type of Float

参数

num_true (int) – The number of target classes per training example. Default: 1.

Inputs:
  • true_classes (Tensor) - The target classes. With data type of int32 or int64 and shape \((batch\_size, num\_true)\).

  • sampled_candidates (Tensor) - The Candidate sampling results of operators, types of training samples, with data type of int32 or int64 and shape \((num\_sampled, )\).

Outputs:

Tuple of 3 Tensors.

  • indices (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the same type as true_classes.

  • ids (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the same type as true_classes.

  • weights (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the type float32.

引发
  • TypeError – If dtype of num_true is not int.

  • TypeError – If true_classes or sampled_candidates is not a Tensor.

  • TypeError – If dtype of true_classes or sampled_candidates is neither int32 nor int64.

Supported Platforms:

Ascend

实际案例

>>> true_classes = np.array([[1, 2], [0, 4], [3, 3]])
>>> sampled_candidates = np.array([0, 1, 2, 3, 4])
>>> sampler = ops.ComputeAccidentalHits(2)
>>> indices, ids, weights = sampler(Tensor(true_classes), Tensor(sampled_candidates))
>>> print(indices, ids, weights)
[0 0 1 1 2 2]
[1 2 0 4 3 3]
[-3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38]
class tinyms.primitives.Concat(*args, **kwargs)[源代码]

Connect tensor in the specified axis.

Connect input tensors along with the given axis.

The input data is a tuple of tensors. These tensors have the same rank R. Set the given axis as m, and \(0 \le m < R\). Set the number of input tensors as N. For the \(i\)-th tensor \(t_i\), it has the shape of \((x_1, x_2, ..., x_{mi}, ..., x_R)\). \(x_{mi}\) is the \(m\)-th dimension of the \(i\)-th tensor. Then, the shape of the output tensor is

\[(x_1, x_2, ..., \sum_{i=1}^Nx_{mi}, ..., x_R)\]

警告

The value range of “axis” is [-dims, dims - 1]. “dims” is the dimension length of “input_x”.

参数

axis (int) – The specified axis. Default: 0.

Inputs:
  • input_x (tuple, list) - A tuple or a list of input tensors. Suppose there are two tensors in this tuple or list, namely x1 and x2. To perform Concat in the axis 0 direction, except for the 0th axis, all other axes should be equal, that is, \(x1.shape[1] == x2.shape[1], x1.shape[2] == x2.shape[2], ..., x1.shape[R] == x2.shape[R]\), where the \(R\) indicates the last axis.

Outputs:
  • Tensor, the shape is \((x_1, x_2, ..., \sum_{i=1}^Nx_{mi}, ..., x_R)\). The data type is the same with input_x.

引发

TypeError – If axis is not an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x1 = Tensor(np.array([[0, 1], [2, 1]]).astype(np.float32))
>>> input_x2 = Tensor(np.array([[0, 1], [2, 1]]).astype(np.float32))
>>> op = ops.Concat()
>>> output = op((input_x1, input_x2))
>>> print(output)
[[0. 1.]
 [2. 1.]
 [0. 1.]
 [2. 1.]]
>>> op = ops.Concat(1)
>>> output = op((input_x1, input_x2))
>>> print(output)
[[0. 1. 0. 1.]
 [2. 1. 2. 1.]]
class tinyms.primitives.ConfusionMatrix(*args, **kwargs)[源代码]

Calculates the confusion matrix from labels and predictions.

参数
  • num_classes (int) – The num of classes.

  • dtype (str) – Data type of confusion matrix. Default: ‘int32’.

Inputs:
  • labels (Tensor) - real labels, tensor of 1-D. the dtype must be non-negative Integer.

  • predictions (Tensor) - the labels from prediction, tensor of 1-D. the shape same as labels and the dtype must be non-negative Integer.

  • weights (Tensor) - tensor of 1-D. the shape same as predictions.

Outputs:

Tensor, the confusion matrix, with shape (num_classes, num_classes).

引发
  • TypeError – If num_classes is not an int.

  • TypeError – If dtype is not a str.

  • TypeError – If labels, predictions or weight` is not a Tensor.

实际案例

>>> confusion_matrix = ops.ConfusionMatrix(4)
>>> labels = Tensor([0, 1, 1, 3], mindspore.int32)
>>> predictions = Tensor([1, 2, 1, 3], mindspore.int32)
>>> output = confusion_matrix(labels, predictions)
>>> print(output)
[[0 1 0 0]
 [0 1 1 0]
 [0 0 0 0]
 [0 0 0 1]]
class tinyms.primitives.Conj(*args, **kwargs)[源代码]

Returns a Tensor that is the real part of the input.

Inputs:
  • input (Tensor, complex) - The input tensor. types: complex64, complex128.

Outputs:

Tensor, has the float type.

引发

TypeError – If the dtype of input is not one of: complex64, complex128.

Supported Platforms:

GPU

实际案例

>>> x = Tensor(np.asarray(np.complex(1.3+0.4j)), mindspore.complex64)
>>> conj = ops.Conj()
>>> output = conj(x)
>>> print(output)
1.3-0.4j
class tinyms.primitives.Constrain(*args, **kwargs)[源代码]

Calculate the constraint force and virial depends on pressure calculation.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • constrain_pair_numbers (int32) – the number of constrain pairs m.

  • iteration_numbers (int32) – the number of iteration numbers p.

  • half_exp_gamma_plus_half (float32) – half exp_gamma plus half q.

  • update_interval (int32) – the number of update interval, default 10.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • quarter_cof (Tensor) - The 3-D scale factor. The data type is float32 and the shape is \((3,)\).

  • mass_inverse (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

  • scaler (Tensor) - The 3-D scale factor (x, y, z), The data type is float32 and the shape is \((3,)\).

  • pair_dr (Tensor) - The displacement vector of each constrained atom pair. The data type is float32 and the shape is \((m, 3)\).

  • atom_i_serials (Tensor) - The first atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • atom_j_serials (Tensor) - The second atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • constant_rs (Tensor) - The constrained distance of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

  • constrain_ks (Tensor) - The coefficient of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

  • need_pressure (Tensor) - If need pressure, 1 else 0. The data type is int32 and the shape is \((1,)\) or \(()\).

Outputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • frc (Tensor) - The constraint force on each atom. The data type is float32 and the shape is \((n, 3)\).

  • virial (Tensor) - The constraint virial on each atom. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.ConstrainForce(*args, **kwargs)[源代码]

Calculate the constraint force in a step with iteration numbers.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • constrain_pair_numbers (int32) – the number of constrain pairs m.

  • iteration_numbers (int32) – the number of iteration numbers p.

  • half_exp_gamma_plus_half (float32) – half exp_gamma plus half q.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • quarter_cof (Tensor) - The 3-D scale factor. The data type is float32 and the shape is \((3,)\).

  • mass_inverse (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

  • scaler (Tensor) - The 3-D scale factor (x, y, z), The data type is float32 and the shape is \((3,)\).

  • pair_dr (Tensor) - The displacement vector of each constrained atom pair. The data type is float32 and the shape is \((m, 3)\).

  • atom_i_serials (Tensor) - The first atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • atom_j_serials (Tensor) - The second atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • constant_rs (Tensor) - The constrained distance of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

  • constrain_ks (Tensor) - The coefficient of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

Outputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • frc (Tensor) - The constraint force on each atom. The data type is float32 and the shape is \((n, 3)\).

  • virial (Tensor) - The constraint virial on each atom and it is zero. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.ConstrainForceCycle(*args, **kwargs)[源代码]

Calculate the constraint force in each iteration.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • constrain_pair_numbers (int32) – the number of constrain pairs m.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler (Tensor) - The 3-D scale factor (x, y, z), The data type is float32 and the shape is \((3,)\).

  • pair_dr (Tensor) - The displacement vector of each constrained atom pair. The data type is float32 and the shape is \((m, 3)\).

  • atom_i_serials (Tensor) - The first atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • atom_j_serials (Tensor) - The second atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • constant_rs (Tensor) - The constrained distance of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

  • constrain_ks (Tensor) - The coefficient of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

Outputs:
  • test_frc (Tensor) - The constraint force. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.ConstrainForceCycleWithVirial(*args, **kwargs)[源代码]

Calculate the constraint force and virial in each iteration.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • constrain_pair_numbers (int32) – the number of constrain pairs m.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler (Tensor) - The 3-D scale factor (x, y, z), The data type is float32 and the shape is \((3,)\).

  • pair_dr (Tensor) - The displacement vector of each constrained atom pair. The data type is float32 and the shape is \((m, 3)\).

  • atom_i_serials (Tensor) - The first atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • atom_j_serials (Tensor) - The second atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • constant_rs (Tensor) - The constrained distance of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

  • constrain_ks (Tensor) - The coefficient of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

Outputs:
  • test_frc (Tensor) - The constraint force. The data type is float32 and the shape is \((n, 3)\).

  • atom_virial (Tensor) - The virial caused by constraint force of each atom. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.ConstrainForceVirial(*args, **kwargs)[源代码]

Calculate the constraint force and virial in a step with iteration numbers.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • constrain_pair_numbers (int32) – the number of constrain pairs m.

  • iteration_numbers (int32) – the number of iteration numbers p.

  • half_exp_gamma_plus_half (float32) – half exp_gamma plus half q.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • quarter_cof (Tensor) - The 3-D scale factor. The data type is float32 and the shape is \((3,)\).

  • mass_inverse (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

  • scaler (Tensor) - The 3-D scale factor (x, y, z), The data type is float32 and the shape is \((3,)\).

  • pair_dr (Tensor) - The displacement vector of each constrained atom pair. The data type is float32 and the shape is \((m, 3)\).

  • atom_i_serials (Tensor) - The first atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • atom_j_serials (Tensor) - The second atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • constant_rs (Tensor) - The constrained distance of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

  • constrain_ks (Tensor) - The coefficient of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

Outputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • virial (Tensor) - The constraint virial on each atom. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.Conv2D(*args, **kwargs)[源代码]

2D convolution layer.

Applies a 2D convolution over an input tensor which is typically of shape \((N, C_{in}, H_{in}, W_{in})\), where \(N\) is batch size, \(C\) is channel number, \(H\) is height, \(W\) is width, \(X_i\) is the \(i^{th}\) input value and \(b_i\) indicates the deviation value of the \(i^{th}\) input value. For each batch of shape \((C_{in}, H_{in}, W_{in})\), the formula is defined as:

\[out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,\]

where \(ccor\) is the cross correlation operator, \(C_{in}\) is the input channel number, \(j\) ranges from \(0\) to \(C_{out} - 1\), \(W_{ij}\) corresponds to the \(i\)-th channel of the \(j\)-th filter and \(out_{j}\) corresponds to the \(j\)-th channel of the output. \(W_{ij}\) is a slice of kernel and it has shape \((\text{kernel_size[0]}, \text{kernel_size[1]})\), where \(\text{kernel_size[0]}\) and \(\text{kernel_size[1]}\) are the height and width of the convolution kernel. The full kernel has shape \((C_{out}, C_{in} // \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]})\), where group is the group number to split the input in the channel dimension.

If the ‘pad_mode’ is set to be “valid”, the output height and width will be \(\left \lfloor{1 + \frac{H_{in} + \text{padding[0]} + \text{padding[1]} - \text{kernel_size[0]} - (\text{kernel_size[0]} - 1) \times (\text{dilation[0]} - 1) }{\text{stride[0]}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{W_{in} + \text{padding[2]} + \text{padding[3]} - \text{kernel_size[1]} - (\text{kernel_size[1]} - 1) \times (\text{dilation[1]} - 1) }{\text{stride[1]}}} \right \rfloor\) respectively. Where \(dialtion\) is Spacing between kernel elements, \(stride\) is The step length of each step, \(padding\) is zero-padding added to both sides of the input.

The first introduction can be found in paper Gradient Based Learning Applied to Document Recognition. More detailed introduction can be found here: http://cs231n.github.io/convolutional-networks/.

参数
  • out_channel (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means the value is for both the height and the width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.

  • mode (int) – Modes for different convolutions. 0 Math convolutiuon, 1 cross-correlation convolution , 2 deconvolution, 3 depthwise convolution. Default: 1.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input x. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side. If this mode is set, pad must be 0.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.

    • pad: Implicit paddings on both sides of the input x. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.

  • pad (Union(int, tuple[int])) – Implicit paddings on both sides of the input x. If pad is one integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple with four integers, the paddings of top, bottom, left and right will be equal to pad[0], pad[1], pad[2], and pad[3] accordingly. Default: 0.

  • stride (Union(int, tuple[int])) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • dilation (Union(int, tuple[int])) – The data type is int or a tuple of 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits input into groups. Default: 1.

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: “NCHW”.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

  • weight (Tensor) - Set size of kernel is \((\text{kernel_size[0]}, \text{kernel_size[1]})\), then the shape is \((C_{out}, C_{in}, \text{kernel_size[0]}, \text{kernel_size[1]})\).

Outputs:

Tensor, the value that applied 2D convolution. The shape is \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.

  • TypeError – If out_channel or group is not an int.

  • ValueError – If kernel_size, stride or dilation is less than 1.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If pad is a tuple whose length is not equal to 4.

  • ValueError – If pad_mode it not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0).

  • ValueError – If data_format is neither ‘NCHW’ not ‘NHWC’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32)
>>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32)
>>> conv2d = ops.Conv2D(out_channel=32, kernel_size=3)
>>> output = conv2d(x, weight)
>>> print(output.shape)
(10, 32, 30, 30)
class tinyms.primitives.Conv2DBackpropInput(*args, **kwargs)[源代码]

Computes the gradients of convolution with respect to the input.

参数
  • out_channel (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means the value is for both the height and the width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.

  • pad_mode (str) – Modes to fill padding. It could be “valid”, “same”, or “pad”. Default: “valid”.

  • pad (Union[int, tuple[int]]) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple of four integers, the padding of top, bottom, left and right equal to pad[0], pad[1], pad[2], and pad[3] correspondingly.

  • mode (int) – Modes for different convolutions. 0 Math convolutiuon, 1 cross-correlation convolution , 2 deconvolution, 3 depthwise convolution. Default: 1.

  • stride (Union[int. tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • dilation (Union[int. tuple[int]]) – Specifies the dilation rate to be used for the dilated convolution. Default: 1.

  • group (int) – Splits input into groups. Default: 1.

  • data_format (str) – The format of input and output data. It should be ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • dout (Tensor) - The gradients write respect to the output of the convolution. The shape conforms to the default data_format \((N, C_{out}, H_{out}, W_{out})\).

  • weight (Tensor) - Set size of kernel is \((\text{ks_w}, \text{ks_h})\), where \(\text{ks_w}\) and \(\text{ks_h}\) are the height and width of the convolution kernel, then the shape is \((C_{out}, C_{in}, \text{ks_w}, \text{ks_h})\).

  • input_size (Tensor) - A tuple describes the shape of the input which conforms to the format \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, the gradients with respect to the input of convolution. It has the same shape as the input.

引发
  • TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.

  • TypeError – If out_channel or group is not an int.

  • ValueError – If kernel_size, stride or dilation is less than 1.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If padding is a tuple whose length is not equal to 4.

  • ValueError – If pad_mode it not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0).

  • ValueError – If data_format is neither ‘NCHW’ not ‘NHWC’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> import mindspore.ops as ops
>>> dout = Tensor(np.ones([10, 32, 30, 30]), mindspore.float32)
>>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32)
>>> input_x = Tensor(np.ones([10, 32, 32, 32]))
>>> conv2d_backprop_input = ops.Conv2DBackpropInput(out_channel=32, kernel_size=3)
>>> output = conv2d_backprop_input(dout, weight, ops.shape(input_x))
>>> print(output.shape)
(10, 32, 32, 32)
class tinyms.primitives.Conv2DTranspose(*args, **kwargs)[源代码]

Compute a 2D transposed convolution, which is also known as a deconvolution (although it is not an actual deconvolution).

参数
  • out_channel (int) – The dimensionality of the output space.

  • kernel_size (Union[int, tuple[int]]) – The size of the convolution window.

  • pad_mode (str) – Modes to fill padding. It could be “valid”, “same”, or “pad”. Default: “valid”.

  • pad (Union[int, tuple[int]]) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple of four integers, the padding of top, bottom, left and right equal to pad[0], pad[1], pad[2], and pad[3] correspondingly.

  • mode (int) – Modes for different convolutions. 0 Math convolutiuon, 1 cross-correlation convolution , 2 deconvolution, 3 depthwise convolution. Default: 1.

  • stride (Union[int. tuple[int]]) – The stride to be applied to the convolution filter. Default: 1.

  • dilation (Union[int. tuple[int]]) – Specifies the dilation rate to be used for the dilated convolution. Default: 1.

  • group (int) – Splits input into groups. Default: 1.

  • data_format (str) –

Inputs:
  • dout (Tensor) - the gradients w.r.t the output of the convolution. The shape conforms to the default data_format \((N, C_{out}, H_{out}, W_{out})\).

  • weight (Tensor) - Set size of kernel is \((K_1, K_2)\), then the shape is \((C_{out}, C_{in}, K_1, K_2)\).

  • input_size (Tensor) - A tuple describes the shape of the input which conforms to the format \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, the gradients w.r.t the input of convolution. It has the same shape as the input.

引发
  • TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.

  • TypeError – If out_channel or group is not an int.

  • ValueError – If kernel_size, stride or dilation is less than 1.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If padding is a tuple whose length is not equal to 4.

  • ValueError – If pad_mode it not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0).

  • ValueError – If data_format is neither ‘NCHW’ not ‘NHWC’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> dout = Tensor(np.ones([10, 32, 30, 30]), mindspore.float32)
>>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32)
>>> x = Tensor(np.ones([10, 32, 32, 32]))
>>> conv2d_transpose_input = ops.Conv2DTranspose(out_channel=32, kernel_size=3)
>>> output = conv2d_transpose_input(dout, weight, ops.shape(x))
>>> print(output.shape)
(10, 32, 32, 32)
class tinyms.primitives.Conv3D(*args, **kwargs)[源代码]

3D convolution layer.

Applies a 3D convolution over an input tensor which is typically of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\) and output shape \((N, C_{out}, D_{out}, H_{out}, W_{out})\). Where \(N\) is batch size, \(C\) is channel number, \(D\) is depth, \(H\) is height, \(W\) is width. the formula is defined as:

\[\operatorname{out}\left(N_{i}, C_{\text {out}_j}\right)=\operatorname{bias}\left(C_{\text {out}_j}\right)+ \sum_{k=0}^{C_{in}-1} ccor(\text {weight}\left(C_{\text {out}_j}, k\right), \operatorname{input}\left(N_{i}, k\right))\]

where \(k\) is kernel, \(ccor\) is the cross-correlation operator.

If the ‘pad_mode’ is set to be “valid”, the output depth, height and width will be \(\left \lfloor{1 + \frac{D_{in} + 2 \times \text{padding} - \text{ks_d} - (\text{ks_d} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{H_{in} + 2 \times \text{padding} - \text{ks_h} - (\text{ks_h} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{W_{in} + 2 \times \text{padding} - \text{ks_w} - (\text{ks_w} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) respectively. Where \(dialtion\) is Spacing between kernel elements, \(stride\) is The step length of each step, \(padding\) is zero-padding added to both sides of the input.

参数
  • out_channel (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 3 integers. Specifies the depth, height and width of the 3D convolution window. Single int means the value is for the depth, height and the width of the kernel. A tuple of 3 ints means the first value is for the depth, height and the other is for the width of the kernel.

  • mode (int) – Modes for different convolutions. It is currently not used. Default: 1.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “valid”.

    • same: Adopts the way of completion. The depth, height and width of the output will be the same as the input. The total number of padding will be calculated in depth, horizontal and vertical directions and evenly distributed to head and tail, top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the tail, bottom and the right side. If this mode is set, pad must be 0.

    • valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.

    • pad: Implicit paddings on both sides of the input in depth, height, width. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.

  • pad (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to pad. If pad is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to pad[0], pad[1], pad[2], pad[3], pad[4] and pad[5] correspondingly.

  • dilation (Union[int, tuple[int]]) – The data type is int or a tuple of 3 integers : math:(dilation_d, dilation_h, dilation_w). Currently, dilation on depth only supports the case of 1. Specifies the dilation rate to use for dilated convolution. If set \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1. Only 1 is currently supported.

  • data_format (str) – The optional value for data format. Currently only support “NCDHW”.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\). Currently input data type only support float16 and float32.

  • weight (Tensor) - Set size of kernel is \((k_d, K_h, K_w)\), then the shape is \((C_{out}, C_{in}//groups, k_d, K_h, K_w)\). Currently weight data type only support float16 and float32.

  • bias (Tensor) - Tensor of shape \(C_{in}\). Currently, only support none.

Outputs:

Tensor, the value that applied 3D convolution. The shape is \((N, C_{out}, D_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If out_channel or group is not an int.

  • TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.

  • ValueError – If out_channel, kernel_size, stride or dilation is less than 1.

  • ValueError – If pad is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If pad is a tuple whose length is not equal to 6.

  • ValueError – If pad_mode is not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0, 0, 0).

  • ValueError – If data_format is not ‘NCDHW’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.ones([16, 3, 10, 32, 32]), mindspore.float16)
>>> weight = Tensor(np.ones([32, 3, 4, 3, 3]), mindspore.float16)
>>> conv3d = ops.Conv3D(out_channel=32, kernel_size=(4, 3, 3))
>>> output = conv3d(x, weight)
>>> print(output.shape)
(16, 32, 7, 30, 30)
class tinyms.primitives.Conv3DTranspose(*args, **kwargs)[源代码]

Computes a 3D transposed convolution, which is also known as a deconvolution (although it is not an actual deconvolution).

Input is typically of shape \((N, C, D, H, W)\), where \(N\) is batch size, \(C\) is channel number, \(D\) is depth, \(H\) is height, \(W\) is width.

If the ‘pad_mode’ is set to be “pad”, the depth, height and width of output are defined as:

\[ \begin{align}\begin{aligned}D_{out} = (D_{in} - 1) \times \text{stride}[0] - 2 \times \text{pad}[0] + \text{dilation}[0] \times (\text{kernel\_size}[0] - 1) + \text{output\_padding}[0] + 1\\H_{out} = (H_{in} - 1) \times \text{stride}[1] - 2 \times \text{pad}[1] + \text{dilation}[1] \times (\text{kernel\_size}[1] - 1) + \text{output\_padding}[1] + 1\\W_{out} = (W_{in} - 1) \times \text{stride}[2] - 2 \times \text{pad}[2] + \text{dilation}[2] \times (\text{kernel\_size}[2] - 1) + \text{output\_padding}[2] + 1\end{aligned}\end{align} \]
参数
  • in_channel (int) – The channel of the input x.

  • out_channel (int) – The channel of the weight x.

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 3 integers. Specifies the depth, height and width of the 3D convolution window. Single int means the value is for the depth, height and the width of the kernel. A tuple of 3 ints means the first value is for the depth, second value is for height and the other is for the width of the kernel.

  • mode (int) – Modes for different convolutions. Default is 1. It is currently not used.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The depth, height and width of the output will be the same as the input. The total number of padding will be calculated in depth, horizontal and vertical directions and evenly distributed to head and tail, top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the tail, bottom and the right side. If this mode is set, pad and output_padding must be 0.

    • valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad and output_padding must be 0.

    • pad: Implicit paddings on both sides of the input in depth, height, width. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.

  • pad (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to pad. If pad is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to pad[0], pad[1], pad[2], pad[3], pad[4] and pad[5] correspondingly.

  • stride (Union(int, tuple[int])) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Default: 1.

  • dilation (Union(int, tuple[int])) – Specifies the space to use between kernel elements. Default: 1.

  • group (int) – Splits input into groups. Default: 1. Only 1 is currently supported.

  • output_padding (Union(int, tuple[int])) – Add extra size to each dimension of the output. Default: 0.

  • data_format (str) – The optional value for data format. Currently only ‘NCDHW’ is supported.

Inputs:
  • dout (Tensor) - The gradients with respect to the output of the convolution. The shape conforms to the default. data_format \((N, C_{in}, D_{out}, H_{out}, W_{out})\). Currently dout data type only supports float16 and float32.

  • weight (Tensor) - Set size of kernel is \((K_d, K_h, K_w)\), then the shape is \((C_{in}, C_{out}//group, K_d, K_h, K_w)\). Where \(group\) is the Args parameter. Currently weight data type only supports float16 and float32.

  • bias (Tensor) - Tensor of shape \(C_{out}\). Currently, only support none.

Outputs:

Tensor, the gradients with respect to the input of convolution 3D. Tensor of shape \((N, C_{out}//group, D_{out}, H_{out}, W_{out})\), where \(group\) is the Args parameter.

Supported Platforms:

Ascend GPU

引发
  • TypeError – If in_channel, out_channel or group is not an int.

  • TypeError – If kernel_size, stride, pad , dilation or output_padding is neither an int not a tuple.

  • ValueError – If in_channel, out_channel, kernel_size, stride or dilation is less than 1.

  • ValueError – If pad is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If pad is a tuple whose length is not equal to 6.

  • ValueError – If pad_mode is not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0, 0, 0).

  • ValueError – If data_format is not ‘NCDHW’.

  • TypeError – If dout and weight data type is not float16.

  • ValueError – If bias is not none. The rank of dout and weight is not 5.

实际案例

>>> dout = Tensor(np.ones([32, 16, 10, 32, 32]), mindspore.float16)
>>> weight = Tensor(np.ones([16, 3, 4, 6, 2]), mindspore.float16)
>>> conv3d_transpose = ops.Conv3DTranspose(in_channel=16, out_channel=3, kernel_size=(4, 6, 2))
>>> output = conv3d_transpose(dout, weight)
>>> print(output.shape)
(32, 3, 13, 37, 33)
class tinyms.primitives.Cos(*args, **kwargs)[源代码]

Computes cosine of input element-wise.

\[out_i = cos(x_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> cos = ops.Cos()
>>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32)
>>> output = cos(x)
>>> print(output)
[0.971338 0.67487574 0.95233357 0.9959527 ]
class tinyms.primitives.Cosh(*args, **kwargs)[源代码]

Computes hyperbolic cosine of input element-wise.

\[out_i = \cosh(input_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend CPU

实际案例

>>> cosh = ops.Cosh()
>>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32)
>>> output = cosh(x)
>>> print(output)
[1.0289385 1.364684 1.048436 1.0040528]
class tinyms.primitives.CrdToUintCrd(*args, **kwargs)[源代码]

Convert FP32 coordinate to Uint32 coordinate.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数

atom_numbers (int32) – the number of atoms n.

Inputs:
  • crd_to_uint_crd_cof (Tensor) - The scale factor between the unsigned int value and the real space coordinates. The data type is float32 and the shape is \((3,)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

Outputs:
  • output (Scalar) - The data type is uint32.

Supported Platforms:

GPU

class tinyms.primitives.CrdToUintCrdQuarter(*args, **kwargs)[源代码]

Convert FP32 coordinate to Uint32 coordinate.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数

atom_numbers (int32) – the number of atoms n.

Inputs:
  • crd_to_uint_crd_cof (Tensor) - The crd_to_uint_crd coefficient. The data type is float32 and the shape is \((3,)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

Outputs:
  • output (Tensor) - The unsigned int coordinates. The data type is unsigned int32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.CropAndResize(*args, **kwargs)[源代码]

Extracts crops from the input image tensor and resizes them.

注解

In case that the output shape depends on crop_size, the crop_size must be constant.

参数
  • method (str) – An optional string that specifies the sampling method for resizing. It can be “bilinear”, “nearest” or “bilinear_v2”. The option “bilinear” stands for standard bilinear interpolation algorithm, while “bilinear_v2” may result in better result in some cases. Default: “bilinear”

  • extrapolation_value (float) – An optional float value used extrapolation, if applicable. Default: 0.

Inputs:
  • x (Tensor) - The input image must be a 4-D tensor of shape [batch, image_height, image_width, depth]. Types allowed: int8, int16, int32, int64, float16, float32, float64, uint8, uint16.

  • boxes (Tensor) - A 2-D tensor of shape [num_boxes, 4]. The i-th row of the tensor specifies the coordinates of a box in the box_ind[i] image and is specified in normalized coordinates [y1, x1, y2, x2]. A normalized coordinate value of y is mapped to the image coordinate at y * (image_height - 1), so as the [0, 1] interval of normalized image height is mapped to [0, image_height - 1] in image height coordinates. We do allow y1 > y2, in which case the sampled crop is an up-down flipped version of the original image. The width dimension is treated similarly. Normalized coordinates outside the [0, 1] range are allowed, in which case we use extrapolation_value to extrapolate the input image values. Types allowed: float32.

  • box_index (Tensor) - A 1-D tensor of shape [num_boxes] with int32 values in [0, batch). The value of box_ind[i] specifies the image that the i-th box refers to. Types allowed: int32.

  • crop_size (Tuple[int]) - A tuple of two int32 elements: (crop_height, crop_width). Only constant value is allowed. All cropped image patches are resized to this size. The aspect ratio of the image content is not preserved. Both crop_height and crop_width need to be positive.

Outputs:

A 4-D tensor of shape [num_boxes, crop_height, crop_width, depth] with type: float32.

引发
  • TypeError – If method is not a str.

  • TypeError – If extrapolation_value is not a float.

  • ValueError – If method is not one of ‘bilinear’, ‘nearest’, ‘bilinear_v2’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class CropAndResizeNet(nn.Cell):
...     def __init__(self, crop_size):
...         super(CropAndResizeNet, self).__init__()
...         self.crop_and_resize = ops.CropAndResize()
...         self.crop_size = crop_size
...
...     def construct(self, x, boxes, box_index):
...         return self.crop_and_resize(x, boxes, box_index, self.crop_size)
...
>>> BATCH_SIZE = 1
>>> NUM_BOXES = 5
>>> IMAGE_HEIGHT = 256
>>> IMAGE_WIDTH = 256
>>> CHANNELS = 3
>>> image = np.random.normal(size=[BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS]).astype(np.float32)
>>> boxes = np.random.uniform(size=[NUM_BOXES, 4]).astype(np.float32)
>>> box_index = np.random.uniform(size=[NUM_BOXES], low=0, high=BATCH_SIZE).astype(np.int32)
>>> crop_size = (24, 24)
>>> crop_and_resize = CropAndResizeNet(crop_size=crop_size)
>>> output = crop_and_resize(Tensor(image), Tensor(boxes), Tensor(box_index))
>>> print(output.shape)
(5, 24, 24, 3)
class tinyms.primitives.CumProd(*args, **kwargs)[源代码]

Computes the cumulative product of the tensor x along axis.

参数
  • exclusive (bool) – If true, perform exclusive cumulative product. Default: False.

  • reverse (bool) – If true, reverse the result along axis. Default: False

Inputs:
  • x (Tensor[Number]) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (int) - The dimensions to compute the cumulative product. Only constant value is allowed.

Outputs:

Tensor, has the same shape and dtype as the x.

引发
Supported Platforms:

Ascend GPU

实际案例

>>> a, b, c, = 1, 2, 3
>>> x = Tensor(np.array([a, b, c]).astype(np.float32))
>>> op0 = ops.CumProd()
>>> output0 = op0(x, 0) # output=[a, a * b, a * b * c]
>>> op1 = ops.CumProd(exclusive=True)
>>> output1 = op1(x, 0) # output=[1, a, a * b]
>>> op2 = ops.CumProd(reverse=True)
>>> output2 = op2(x, 0) # output=[a * b * c, b * c, c]
>>> op3 = ops.CumProd(exclusive=True, reverse=True)
>>> output3 = op3(x, 0) # output=[b * c, c, 1]
>>> print(output0)
[1. 2. 6.]
>>> print(output1)
[1. 1. 2.]
>>> print(output2)
[6. 6. 3.]
>>> print(output3)
[6. 3. 1.]
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [5, 3, 5]]).astype(np.float32))
>>> output4 = op0(x, 0)
>>> output5 = op0(x, 1)
>>> print(output4)
[[ 1.  2.  3.]
 [ 4. 10. 18.]
 [20. 30. 90.]]
>>> print(output5)
[[1.  2.   6.]
 [4. 20. 120.]
 [5. 15.  75.]]
class tinyms.primitives.CumSum(*args, **kwargs)[源代码]

Computes the cumulative sum of input tensor along axis.

\[y_i = x_1 + x_2 + x_3 + ... + x_i\]
参数
  • exclusive (bool) – If true, perform exclusive mode. Default: False.

  • reverse (bool) – If true, perform inverse cumulative sum. Default: False.

Inputs:
  • input (Tensor) - The input tensor to accumulate.

  • axis (int) - The axis to accumulate the tensor’s value. Only constant value is allowed. Must be in the range [-rank(input), rank(input)).

Outputs:

Tensor, the shape of the output tensor is consistent with the input tensor’s.

引发
  • TypeError – If exclusive or reverse is not a bool.

  • TypeError – If axis is not an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))
>>> cumsum = ops.CumSum()
>>> # case 1: along the axis 0
>>> y = cumsum(x, 0)
>>> print(y)
[[ 3.  4.  6. 10.]
 [ 4. 10. 13. 19.]
 [ 8. 13. 21. 26.]
 [ 9. 16. 28. 35.]]
>>> # case 2: along the axis 1
>>> y = cumsum(x, 1)
>>> print(y)
[[ 3.  7. 13. 23.]
 [ 1.  7. 14. 23.]
 [ 4.  7. 15. 22.]
 [ 1.  4. 11. 20.]]
>>> # Next demonstrate exclusive and reverse, along axis 1
>>> # case 3: exclusive = True
>>> cumsum = ops.CumSum(exclusive=True)
>>> y = cumsum(x, 1)
>>> print(y)
[[ 0.  3.  7. 13.]
 [ 0.  1.  7. 14.]
 [ 0.  4.  7. 15.]
 [ 0.  1.  4. 11.]]
>>> # case 4: reverse = True
>>> cumsum = ops.CumSum(reverse=True)
>>> y = cumsum(x, 1)
>>> print(y)
[[23. 20. 16. 10.]
 [23. 22. 16.  9.]
 [22. 18. 15.  7.]
 [20. 19. 16.  9.]]
class tinyms.primitives.DType(*args, **kwargs)[源代码]

Returns the data type of the input tensor as mindspore.dtype.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

mindspore.dtype, the data type of a tensor.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
>>> output = ops.DType()(input_tensor)
>>> print(output)
Float32
class tinyms.primitives.DataFormatDimMap(*args, **kwargs)[源代码]

Returns the dimension index in the destination data format given in the source data format.

参数
  • src_format (str) – An optional value for source data format. The format can be ‘NHWC’ and ‘NCHW’. Default: ‘NHWC’.

  • dst_format (str) – An optional value for destination data format. The format can be ‘NHWC’ and ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • input_x (Tensor) - A Tensor with each element as a dimension index in source data format. The suggested values is in the range [-4, 4). Only supports int32.

Outputs:

Tensor, Return the dimension index in the given target data format, has the same data type and shape as the input_x.

引发
  • TypeError – If src_format or dst_format is not a str.

  • TypeError – If input_x is not a Tensor whose dtype is not int32.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor([0, 1, 2, 3], mindspore.int32)
>>> dfdm = ops.DataFormatDimMap()
>>> output = dfdm(input_x)
>>> print(output)
[0 3 1 2]
class tinyms.primitives.Depend(*args, **kwargs)[源代码]

Depend is used for processing dependency operations.

In most scenarios, if operators have IO side effects or memory side effects, they will be executed according to the user’s semantics. In some scenarios, if the two operators A and B have no order dependency, and A must be executed before B, we recommend using Depend to specify their execution order. The usage method is as follows:

a = A(x)                --->        a = A(x)
b = B(y)                --->        y = Depend(y, a)
                        --->        b = B(y)
Inputs:
  • value (Tensor) - the real value to return for depend operator.

  • expr (Expression) - the expression to execute with no outputs.

Outputs:

Tensor, the value passed by last operator.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>> from mindspore import Tensor
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.softmax = ops.Softmax()
...         self.depend = ops.Depend()
...
...     def construct(self, x, y):
...         mul = x * y
...         y = self.depend(y, mul)
...         ret = self.softmax(y)
...         return ret
...
>>> x = Tensor(np.ones([4, 5]), dtype=mindspore.float32)
>>> y = Tensor(np.ones([4, 5]), dtype=mindspore.float32)
>>> net = Net()
>>> output = net(x, y)
>>> print(output)
[[0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]]
class tinyms.primitives.DepthToSpace(*args, **kwargs)[源代码]

Rearranges blocks of depth data into spatial dimensions.

This is the reverse operation of SpaceToDepth.

The depth of output tensor is \(input\_depth / (block\_size * block\_size)\).

The output tensor’s height dimension is \(height * block\_size\).

The output tensor’s weight dimension is \(weight * block\_size\).

The input tensor’s depth must be divisible by block_size * block_size. The data format is “NCHW”.

参数

block_size (int) – The block size used to divide depth data. It must be >= 2.

Inputs:
  • x (Tensor) - The target tensor. It must be a 4-D tensor with shape \((N, C_{in}, H_{in}, W_{in})\). The data type is Number.

Outputs:

Tensor of shape \((N, C_{in} / \text{block_size} ^ 2, H_{in} * \text{block_size}, W_{in} * \text{block_size})\).

引发
  • TypeError – If block_size is not an int.

  • ValueError – If block_size is less than 2.

  • ValueError – If length of shape of x is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.random.rand(1, 12, 1, 1), mindspore.float32)
>>> block_size = 2
>>> depth_to_space = ops.DepthToSpace(block_size)
>>> output = depth_to_space(x)
>>> print(output.shape)
(1, 3, 2, 2)
class tinyms.primitives.DepthwiseConv2dNative(*args, **kwargs)[源代码]

Returns the depth-wise convolution value for the input.

Applies depthwise conv2d for the input, which will generate more channels with channel_multiplier. Given an input tensor of shape \((N, C_{in}, H_{in}, W_{in})\) where \(N\) is the batch size, \(C\) is the channels, \(H\) is height, \(W\) is width and a filter tensor with kernel size \((\text{kernel_size[0]}, \text{kernel_size[1]})\), where \(\text{kernel_size[0]}\) indicates the kernel_size of height, \(\text{kernel_size[1]}\) indicates the kernel_size of width, containing \(C_{in} * \text{channel_multiplier}\) convolutional filters of depth 1; it applies different filters to each input channel (channel_multiplier channels for each input channel has the default value 1), then concatenates the results together. The output has \(C_{in} * \text{channel_multiplier}\) channels.

参数
  • channel_multiplier (int) – The multiplier for the original output convolution. Its value must be greater than 0.

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means the value is for both the height and the width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.

  • mode (int) – Modes for different convolutions. 0 Math convolution, 1 cross-correlation convolution , 2 deconvolution, 3 depthwise convolution. Default: 3.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input x. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side. If this mode is set, pad must be 0.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.

    • pad: Implicit paddings on both sides of the input x. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.

  • pad (Union[int, tuple[int]]) – Implicit paddings on both sides of the input x. If pad is one integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple with four integers, the paddings of top, bottom, left and right will be equal to pad[0], pad[1], pad[2], and pad[3] accordingly. Default: 0.

  • stride (Union(int, tuple[int])) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • dilation (Union(int, tuple[int])) – The data type is int or a tuple of 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits input into groups. Default: 1.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

  • weight (Tensor) - Set the size of kernel as \((\text{kernel_size[0]}, \text{kernel_size[1]})\), then the shape is \((K, C_{in}, \text{kernel_size[0]}, \text{kernel_size[1]})\), K must be 1.

Outputs:

Tensor of shape \((N, C_{in} * \text{channel_multiplier}, H_{out}, W_{out})\).

引发
  • TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.

  • TypeError – If channel_multiplier or group is not an int.

  • ValueError – If stride or dilation is less than 1.

  • ValueError – If pad_mode is not one of the following:’same’, ‘valid’ or ‘pad’.

  • ValueError – If pad_mode it not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0).

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32)
>>> weight = Tensor(np.ones([1, 32, 3, 3]), mindspore.float32)
>>> depthwise_conv2d = ops.DepthwiseConv2dNative(channel_multiplier=3, kernel_size=(3, 3))
>>> output = depthwise_conv2d(x, weight)
>>> print(output.shape)
(10, 96, 30, 30)
class tinyms.primitives.Diag(*args, **kwargs)[源代码]

Constructs a diagonal tensor with a given diagonal values.

Assume input_x has dimensions \([D_1,... D_k]\), the output is a tensor of rank 2k with dimensions \([D_1,..., D_k, D_1,..., D_k]\) where: \(output[i_1,..., i_k, i_1,..., i_k] = input_x[i_1,..., i_k]\) and 0 everywhere else.

Inputs:
  • input_x (Tensor) - The input tensor. The input shape must be less than 5d.

Outputs:

Tensor, has the same dtype as the input_x.

引发
Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor([1, 2, 3, 4])
>>> diag = ops.Diag()
>>> output = diag(input_x)
>>> print(output)
[[1, 0, 0, 0],
 [0, 2, 0, 0],
 [0, 0, 3, 0],
 [0, 0, 0, 4]]
class tinyms.primitives.DiagPart(*args, **kwargs)[源代码]

Extracts the diagonal part from given tensor.

Assume input has dimensions \([D_1,..., D_k, D_1,..., D_k]\), the output is a tensor of rank k with dimensions \([D_1,..., D_k]\) where: \(output[i_1,..., i_k] = input[i_1,..., i_k, i_1,..., i_k]\).

Inputs:
  • input_x (Tensor) - The input tensor of rank 2k, k is not zero.

Outputs:

Tensor, the extracted diagonal has the same dtype as the input_x.

引发
  • TypeError – If input_x is not a Tensor.

  • ValueError – If rank of input_x is not even or zero.

  • ValueError – If input_shape[i] is not equal to input_shape[i + len(input_shape)/2].

Supported Platforms:

Ascend

Examples
>>> input_x = Tensor([[1, 0, 0, 0],
...                   [0, 2, 0, 0],
...                   [0, 0, 3, 0],
...                   [0, 0, 0, 4]])
>>> diag_part = ops.DiagPart()
>>> output = diag_part(input_x)
>>> print(output)
[1 2 3 4]
class tinyms.primitives.Dihedral14CFAtomEnergy(*args, **kwargs)[源代码]

Add the potential energy caused by Coulumb energy correction for each necessary dihedral 1,4 terms to the total potential energy of each atom.

The calculation formula is the same as operator Dihedral14CFEnergy.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJ_type (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge of each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength_f (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • cf_scale_factor (Tensor) - The scale factor for the Coulomb part of force correction for each dihedral 1,4 terms. The data type is float32 and the shape is \((m,)\).

Outputs:
  • ene (Tensor) - The accumulated potential energy of each atom. The data type is float32 and the shape is \((n,)\)

Supported Platforms:

GPU

class tinyms.primitives.Dihedral14CFEnergy(*args, **kwargs)[源代码]

Calculate the Coulumb part of 1,4 dihedral energy correction for each necessary dihedral terms on the corresponding atoms.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr = (x_a-x_b, y_a-y_b, z_a-z_b)\]
\[E = k*q_a*q_b/|dr|\]
参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJ_type (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge of each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength_f (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • cf_scale_factor (Tensor) - The scale factor for the Coulomb part of force correction for each dihedral 1,4 terms. The data type is float32 and the shape is \((m,)\).

Outputs:
  • ene (Tensor) - The accumulated potential energy of each atom. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.Dihedral14ForceWithAtomEnergyVirial(*args, **kwargs)[源代码]

Calculate the Lennard-Jones and Coulumb energy correction and force correction for each necessary dihedral 1,4 terms together and add them to the total force and potential energy for each atom.

The calculation formula of force correction is the same as operator Dihedral14LJForceWithDirectCF, and the energy correction part is the same as operator Dihedral14LJEnergy and Dihedral14CFEnergy.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJtype (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • lj_scale_factor (Tensor) - The scale factor for the Lennard-Jones part of force correction of each dihedral 1,4 term.

  • cf_scale_factor (Tensor) - The scale factor for the Coulomb force. The data type is float32 and the shape is \((m,)\).

  • LJ_type_A (Tensor) - The A parameter in Lennard-Jones scheme of each atom pair type. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

  • LJ_type_B (Tensor) - The B parameter in Lennard-Jones shceme of each atom pair type. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

Outputs:
  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • atom_energy (Tensor) - The accumulated potential energy for each atom. The data type is float32 and the shape is \((n, )\).

  • atom_virial (Tensor) - The accumulated potential virial for each atom. The data type is float32 and the shape is \((n, )\).

Supported Platforms:

GPU

class tinyms.primitives.Dihedral14LJAtomEnergy(*args, **kwargs)[源代码]

Add the potential energy caused by Lennard-Jones energy correction for each necessary dihedral 1,4 terms to the total potential energy of each atom.

The calculation formula is the same as operator Dihedral14LJEnergy().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJ_type (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge of each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength_f (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • lj_scale_factor (Tensor) - The scale factor for the Lennard-Jones part of force correction of each dihedral 1,4 term. The data type is float32 and the shape is \((m,)\).

  • cf_scale_factor (Tensor) - The scale factor for the Coulomb part of force correction for each dihedral 1,4 terms. The data type is float32 and the shape is \((m,)\).

  • LJ_type_A (Tensor) - The A parameter in Lennard-Jones scheme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • LJ_type_B (Tensor) - The B parameter in Lennard-Jones shceme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

Outputs:
  • ene (Tensor) - The accumulated potential energy of each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.Dihedral14LJCFForceWithAtomEnergy(*args, **kwargs)[源代码]

Calculate the Lennard-Jones and Coulumb energy correction and force correction for each necessary dihedral 1,4 terms together and add them to the total force and potential energy for each atom.

The calculation formula of force correction is the same as operator Dihedral14LJForceWithDirectCF, and the energy correction part is the same as operator Dihedral14LJEnergy and Dihedral14CFEnergy.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJ_type (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge of each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength_f (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • lj_scale_factor (Tensor) - The scale factor for the Lennard-Jones part of force correction of each dihedral 1,4 term. The data type is float32 and the shape is \((m,)\).

  • cf_scale_factor (Tensor) - The scale factor for the Coulomb part of force correction for each dihedral 1,4 terms. The data type is float32 and the shape is \((m,)\).

  • LJ_type_A (Tensor) - The A parameter in Lennard-Jones scheme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • LJ_type_B (Tensor) - The B parameter in Lennard-Jones shceme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

Outputs:
  • frc_f (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • atom_energy (Tensor) - The accumulated potential energy for each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.Dihedral14LJEnergy(*args, **kwargs)[源代码]

Calculate the Lennard-Jones part of 1,4 dihedral energy correction for each necessary dihedral terms on the corresponding atoms.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr = (x_a-x_b, y_a-y_b, z_a-z-b)\]
\[E = k*(A/|dr|^{12} - B/|dr|^{6})\]
参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJ_type (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge of each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength_f (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • lj_scale_factor (Tensor) - The scale factor for the Lennard-Jones part of force correction of each dihedral 1,4 term. The data type is float32 and the shape is \((m,)\).

  • LJ_type_A (Tensor) - The A parameter in Lennard-Jones scheme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • LJ_type_B (Tensor) - The B parameter in Lennard-Jones shceme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

Outputs:
  • ene (Tensor) - The Lennard-Jones potential energy correction. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.Dihedral14LJForce(*args, **kwargs)[源代码]

Calculate the Lennard-Jones part of 1,4 dihedral force correction for each necessary dihedral terms on the corresponding atoms.

Assume the number of necessary dihedral 1,4 terms is m, the number of atoms is n, and the number of Lennard-Jones types for all atoms is P, which means there will be q = P*(P+1)/2 types of possible Lennard-Jones interactions for all kinds of atom pairs.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr = (x_a-x_b, y_a-y_b, z_a-z_b)\]
\[F = k*(-12*A/|dr|^{14} + 6*B/|dr|^{8})*dr\]
参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJ_type (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge of each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength_f (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • lj_scale_factor (Tensor) - The scale factor for the Lennard-Jones part of force correction of each dihedral 1,4 term. The data type is float32 and the shape is \((m,)\).

  • LJ_type_A (Tensor) - The A parameter in Lennard-Jones scheme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • LJ_type_B (Tensor) - The B parameter in Lennard-Jones shceme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

Outputs:
  • frc_f (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.Dihedral14LJForceWithDirectCF(*args, **kwargs)[源代码]

Calculate the Lennard-Jones part and the Coulomb part of force correction for each necessary dihedral 1,4 terms.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

The calculation formula of the Lennard-Jones part is the same as operator Dihedral14LJForce(), and the Coulomb part is as follows:

\[dr = (x_a-x_b, y_a-y_b, z_a-z_b)\]
\[F = -k*q_a*q_b/|r|^3*dr\]
参数
  • nb14_numbers (int32) – the number of necessary dihedral 1,4 terms m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJ_type (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge of each atom. The data type is float32 and the shape is \((n,)\).

  • boxlength_f (Tensor) - The length of molecular simulation box in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • a_14 (Tensor) - The first atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • b_14 (Tensor) - The second atom index of each dihedral 1,4 term. The data type is int32 and the shape is \((m,)\).

  • lj_scale_factor (Tensor) - The scale factor for the Lennard-Jones part of force correction of each dihedral 1,4 term. The data type is float32 and the shape is \((m,)\).

  • cf_scale_factor (Tensor) - The scale factor for the Coulomb part of force correction for each dihedral 1,4 terms. The data type is float32 and the shape is \((m,)\).

  • LJ_type_A (Tensor) - The A parameter in Lennard-Jones scheme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • LJ_type_B (Tensor) - The B parameter in Lennard-Jones shceme of each atom pair type. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

Outputs:
  • frc_f (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\)

Supported Platforms:

GPU

class tinyms.primitives.DihedralAtomEnergy(*args, **kwargs)[源代码]

Add the potential energy caused by dihedral terms to the total potential energy of each atom.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

The calculation formula is the same as operator DihedralEnergy().

参数

dihedral_numbers (int32) – the number of dihedral terms m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tenso) - The 3rd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_d (Tensor) - The 4th atom index of each dihedral. 4 atoms are connected in the form a-b-c-d. The data type is int32 and the shape is \((m,)\).

  • ipn (Tensor) - The period of dihedral angle of each dihedral. The data type is int32 and the shape is \((m,)\).

  • pk (Tensor) - The force constant of each dihedral. The data type is float32 and the shape is \((m,)\).

  • gamc (Tensor) - k*cos(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • gams (Tensor) - k*sin(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • pn (Tensor) - The floating point form of ipn. The data type is float32 and the shape is \((m,)\).

Outputs:
  • ene (Tensor) - The accumulated potential energy for each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.DihedralEnergy(*args, **kwargs)[源代码]

Calculate the potential energy caused by dihedral terms for each 4-atom pair. Assume our system has n atoms and m dihedral terms.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数

dihedral_numbers (int32) – the number of dihedral terms m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tensor) - The 3rd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_d (Tensor) - The 4th atom index of each dihedral. 4 atoms are connected in the form a-b-c-d. The data type is int32 and the shape is \((m,)\).

  • ipn (Tensor) - The period of dihedral angle of each dihedral. The data type is int32 and the shape is \((m,)\).

  • pk (Tensor) - The force constant of each dihedral. The data type is int32 and the shape is \((m,)\).

  • gamc (Tensor) - k*cos(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • gams (Tensor) - k*sin(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • pn (Tensor) - The floating point form of ipn. The data type is float32 and the shape is \((m,)\).

Outputs:
  • ene (Tensor) - The potential energy for each dihedral term. The data type is float32 and the shape is \((m,)\).

Supported Platforms:

GPU

class tinyms.primitives.DihedralForce(*args, **kwargs)[源代码]

Calculate the force exerted by the dihedral term which made of 4-atoms on the corresponding atoms. Assume the number of dihedral terms is m and the number of atoms is n.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数

dihedral_numbers (int32) – the number of dihedral terms m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tensor) - The 3rd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_d (Tensor) - The 4th atom index of each dihedral. 4 atoms are connected in the form a-b-c-d. The data type is int32 and the shape is \((m,)\).

  • ipn (Tensor) - The period of dihedral angle of each dihedral. The data type is int32 and the shape is \((m,)\).

  • pk (Tensor) - The force constant of each dihedral. The data type is float32 and the shape is \((m,)\).

  • gamc (Tensor) - k*cos(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • gams (Tensor) - k*sin(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • pn (Tensor) - The floating point form of ipn. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.DihedralForceWithAtomEnergy(*args, **kwargs)[源代码]

Calculate dihedral force and potential energy together.

The calculation formula is the same as operator DihedralForce() and DihedralEnergy().

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数

dihedral_numbers (int32) – the number of dihedral terms m.

Inputs:
  • uint_crd_f (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • scaler_f (Tensor) - The 3-D scale factor between the real space float coordinates and the unsigned int coordinates. The data type is float32 and the shape is \((3,)\).

  • atom_a (Tensor) - The 1st atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_b (Tensor) - The 2nd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_c (Tenso) - The 3rd atom index of each dihedral. The data type is int32 and the shape is \((m,)\).

  • atom_d (Tensor) - The 4th atom index of each dihedral. 4 atoms are connected in the form a-b-c-d. The data type is int32 and the shape is \((m,)\).

  • ipn (Tensor) - The period of dihedral angle of each dihedral. The data type is int32 and the shape is \((m,)\).

  • pk (Tensor) - The force constant of each dihedral. The data type is float32 and the shape is \((m,)\).

  • gamc (Tensor) - k*cos(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • gams (Tensor) - k*sin(phi_0) of each dihedral. The data type is float32 and the shape is \((m,)\).

  • pn (Tensor) - The floating point form of ipn. The data type is float32 and the shape is \((m,)\).

Outputs:
  • frc_f (Tensor) - Same as operator DihedralForce(). The data type is float32 and the shape is \((n, 3)\).

  • ene (Tensor) - Same as operator DihedralAtomEnergy(). The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.Div(*args, **kwargs)[源代码]

Computes the quotient of dividing the first input tensor by the second input tensor element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = \frac{x_i}{y_i}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - When the first input is a tensor, The second input could be a number, a bool, or a tensor whose data type is number or bool. When the first input is a number or a bool, the second input must be a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1 :has same data type and shape of the two inputs
>>> x = Tensor(np.array([-4.0, 5.0, 6.0]), mindspore.float32)
>>> y = Tensor(np.array([3.0, 2.0, 3.0]), mindspore.float32)
>>> div = ops.Div()
>>> output = div(x, y)
>>> print(output)
[-1.3333334  2.5        2.        ]
>>> # case 2 : different data type and shape of the two inputs
>>> x = Tensor(np.array([-4.0, 5.0, 6.0]), mindspore.int32)
>>> y = Tensor(2, mindspore.float32)
>>> output = div(x, y)
>>> print(output)
[-2.  2.5  3.]
>>> print(output.dtype)
Float32
class tinyms.primitives.DivNoNan(*args, **kwargs)[源代码]

Computes a safe divide and returns 0 if the y is zero.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([-1.0, 0., 1.0, 5.0, 6.0]), mindspore.float32)
>>> y = Tensor(np.array([0., 0., 0., 2.0, 3.0]), mindspore.float32)
>>> div_no_nan = ops.DivNoNan()
>>> output = div_no_nan(x, y)
>>> print(output)
[0.  0.  0.  2.5 2. ]
class tinyms.primitives.Dropout(*args, **kwargs)[源代码]

During training, randomly zeroes some of the elements of the input tensor with probability 1-keep_prob from a Bernoulli distribution.

参数
  • keep_prob (float) – The keep rate, between 0 and 1, e.g. keep_prob = 0.9, means dropping out 10% of input units. Default: 0.5.

  • Seed0 (int) – Seed0 value for random generating. Default: 0.

  • Seed1 (int) – Seed1 value for random generating. Default: 0.

Inputs:
  • x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:
  • output (Tensor) - With the same shape and data type as x.

  • mask (Tensor) - With the same shape as x.

引发
  • TypeError – If keep_prob is not a float.

  • TypeError – If Seed0 or Seed1 is not an int.

  • TypeError – If dtype of x is neither float16 nor float32.

  • TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> dropout = ops.Dropout(keep_prob=0.5)
>>> x = Tensor(((20, 16), (50, 50)), mindspore.float32)
>>> output, mask = dropout(x)
>>> print(output.shape)
(2, 2)
class tinyms.primitives.Dropout2D(*args, **kwargs)[源代码]

During training, randomly zeroes some of the channels of the input tensor with probability 1-keep_prob from a Bernoulli distribution(For a 4-dimensional tensor with a shape of NCHW, the channel feature map refers to a 2-dimensional feature map with the shape of HW).

For example, the \(j_th\) channel of the \(i_th\) sample in the batched input is a 2D tensor input[i,j]. Each channel will be zeroed out independently on every forward call with probability 1-keep_prob using samples from a Bernoulli distribution.

Dropout2D can improve the independence between channel feature maps.

参数

keep_prob (float) – The keep probability of a channel, between 0 and 1, e.g. keep_prob = 0.8, means dropping out 20% of channels. Default: 0.5.

Inputs:
  • x (Tensor) - A 4-D tensor with shape \((N, C, H, W)\). The data type should be int8, int16, int32, int64, float16 or float32.

Outputs:
  • output (Tensor) - With the same shape and data type as x.

  • mask (Tensor) - With the same shape as x and the data type is bool.

引发
  • TypeError – If the data type of keep_prob is not float.

  • ValueError – If keep_prob is out of the range [0.0, 1.0]; or if the dim of input is not 4-D.

Supported Platforms:

Ascend

实际案例

>>> dropout = ops.Dropout2D(keep_prob=0.5)
>>> x = Tensor(np.ones([2, 1, 2, 3]), mindspore.float32)
>>> output, mask = dropout(x)
>>> print(output.shape)
(2, 1, 2, 3)
class tinyms.primitives.Dropout3D(*args, **kwargs)[源代码]

During training, randomly zeroes some of the channels of the input tensor with probability 1-keep_prob from a Bernoulli distribution(For a 5-dimensional tensor with a shape of NCDHW, the channel feature map refers to a 3-dimensional feature map with a shape of DHW).

For example, the \(j_th\) channel of the \(i_th\) sample in the batched input is a 3D tensor input[i,j,k]. Each channel will be zeroed out independently on every forward call with probability 1-keep_prob using samples from a Bernoulli distribution.

Dropout3D can improve the independence between channel feature maps.

参数

keep_prob (float) – The keep probability of a channel, between 0 and 1, e.g. keep_prob = 0.8, means dropping out 20% of channels. Default: 0.5.

Inputs:
  • x (Tensor) - A 5-D tensor with shape \((N, C, D, H, W)\). The data type should be int8, int16, int32, int64, float16 or float32.

Outputs:
  • output (Tensor) - With the same shape and data type as x.

  • mask (Tensor) - With the same shape as x and the data type is bool.

引发
  • TypeError – If the data type of keep_prob is not float.

  • ValueError – If keep_prob is out of the range [0.0, 1.0]; or if the dim of input is not 5-D.

Supported Platforms:

Ascend GPU

实际案例

>>> dropout = ops.Dropout3D(keep_prob=0.5)
>>> x = Tensor(np.ones([2, 1, 2, 1, 2]), mindspore.float32)
>>> output, mask = dropout(x)
>>> print(output.shape)
(2, 1, 2, 1, 2)
class tinyms.primitives.DropoutDoMask(*args, **kwargs)[源代码]

Applies dropout mask on the input tensor.

Take the mask output of DropoutGenMask as input, and apply dropout on the input.

Dropout means that neural network units are temporarily dropped from the network according to a certain probability during the deep learning network training. Generally, The effect of Dropout is the same as that of DropoutGenMask and DropoutDoMask. The DropoutGenMask generates a mask shape that is specified based on the input. Next, The DropoutDoMask is a mask generated using DropoutGenMask. The input tensor is randomly set to zero based on the probability p.

Inputs:
  • input_x (Tensor) - The input tensor. Tensor of shape \((N, \ldots)\). The data type should be float32, float16 or int32

  • mask (Tensor) - The mask to be applied on input_x, which is the output of DropoutGenMask. And the shape of input_x must be the same as the value of DropoutGenMask’s input shape. If input wrong mask, the output of DropoutDoMask are unpredictable.

  • keep_prob (Union[Tensor, float]) - The keep rate, greater than 0 and less equal than 1, e.g. keep_prob = 0.9, means dropping out 10% of input units. The value of keep_prob is the same as the input keep_prob of the operator DropoutGenMask.

Outputs:

Tensor, the value that applied dropout on, as the same data type and shape as input_x.

引发
  • TypeError – If input_x, mask or keep_prob is not a Tensor.

  • TypeError – If keep_prob is not a float.

  • ValueError – If value of keep_prob is not same as DropoutGenMaks.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.ones([2, 2, 3]), mindspore.float32)
>>> shape = (2, 2, 3)
>>> keep_prob = Tensor(0.5, mindspore.float32)
>>> dropout_gen_mask = ops.DropoutGenMask()
>>> dropout_do_mask = ops.DropoutDoMask()
>>> mask = dropout_gen_mask(shape, keep_prob)
>>> output = dropout_do_mask(input_x, mask, keep_prob)
>>> print(output.shape)
(2, 2, 3)
class tinyms.primitives.DropoutGenMask(*args, **kwargs)[源代码]

Generates the mask value for the input shape.

Dropout means that neural network units are temporarily dropped from the network according to a certain probability during the deep learning network training. Generally, The effect of Dropout is the same as that of DropoutGenMask and DropoutDoMask. The DropoutGenMask generates a mask shape that is specified based on the input. Next, The DropoutDoMask is a mask generated using DropoutGenMask. The input tensor is randomly set to zero based on the probability p.

参数
  • Seed0 (int) – Seed0 value for random generating. Default: 0.

  • Seed1 (int) – Seed1 value for random generating. Default: 0.

Inputs:
  • shape (tuple[int]) - The shape of target mask.

  • keep_prob (Tensor) - The keep rate, greater than 0 and less equal than 1, e.g. keep_prob = 0.9, means dropping out 10% of input units.

Outputs:

Tensor, the value of generated mask for Inputs shape.

引发
  • TypeError – If neither seed0 nor seed1 is an int.

  • TypeError – If shape is not a tuple.

  • TypeError – If keep_prob is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> dropout_gen_mask = ops.DropoutGenMask()
>>> shape = (2, 4, 5)
>>> keep_prob = Tensor(0.5, mindspore.float32)
>>> output = dropout_gen_mask(shape, keep_prob)
>>> print(output.shape)
(16,)
class tinyms.primitives.DynamicGRUV2(*args, **kwargs)[源代码]

Applies a single-layer gated recurrent unit (GRU) to an input sequence.

\[\begin{split}\begin{array}{ll} r_{t+1} = \sigma(W_{ir} x_{t+1} + b_{ir} + W_{hr} h_{(t)} + b_{hr}) \\ z_{t+1} = \sigma(W_{iz} x_{t+1} + b_{iz} + W_{hz} h_{(t)} + b_{hz}) \\ n_{t+1} = \tanh(W_{in} x_{t+1} + b_{in} + r_{t+1} * (W_{hn} h_{(t)}+ b_{hn})) \\ h_{t+1} = (1 - z_{t+1}) * n_{t+1} + z_{t+1} * h_{(t)} \end{array}\end{split}\]

where \(h_{t+1}\) is the hidden state at time t+1, \(x_{t+1}\) is the input at time t+1, \(h_{t}\) is the hidden state of the layer at time t or the initial hidden state at time 0, and \(r_{t+1}\), \(z_{t+1}\), \(n_{t+1}\) are the reset, update, and new gates, respectively. \(W\), \(b\) are the weight parameter and the deviation parameter respectively. \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product.

参数
  • direction (str) – A string identifying the direction in the op. Default: ‘UNIDIRECTIONAL’. Only ‘UNIDIRECTIONAL’ is currently supported.

  • cell_depth (int) – An integer identifying the cell depth in the op. Default: 1.

  • keep_prob (float) – A float identifying the keep prob in the op. Default: 1.0.

  • cell_clip (float) – A float identifying the cell clip in the op. Default: -1.0.

  • num_proj (int) – An integer identifying the num proj in the op. Default: 0.

  • time_major (bool) – A bool identifying the time major in the op. Default: True.

  • activation (str) – A string identifying the type of activation function in the op. Default: ‘tanh’. Only ‘tanh’ is currently supported.

  • gate_order (str) – A string identifying the gate order in weight and bias. Default: ‘rzh. ‘zrh’ is another option.

  • reset_after (bool) – A bool identifying whether to apply reset gate after matrix multiplication. Default: True.

  • is_training (bool) – A bool identifying is training in the op. Default: True.

Inputs:
  • x (Tensor) - Current words. Tensor of shape \((\text{num_step}, \text{batch_size}, \text{input_size})\). The data type must be float16.

  • weight_input (Tensor) - Input-hidden weight. Tensor of shape \((\text{input_size}, 3 \times \text{hidden_size})\). The data type must be float16.

  • weight_hidden (Tensor) - Hidden-hidden weight. Tensor of shape \((\text{hidden_size}, 3 \times \text{hidden_size})\). The data type must be float16.

  • init_h (Tensor) - Hidden state of initial time. Tensor of shape \((\text{batch_size}, \text{hidden_size})\). The data type must be float16 or float32.

  • bias_input (Tensor) - Input-hidden bias. Tensor of shape \((3 \times \text{hidden_size})\), or None. Has the same data type with input init_h.

  • bias_hidden (Tensor) - Hidden-hidden bias. Tensor of shape \((3 \times \text{hidden_size})\), or None. Has the same data type with input init_h.

  • seq_length (Tensor) - The length of each batch. Tensor of shape \((\text{batch_size})\). Only None is currently supported.

Outputs:
  • y (Tensor) - A Tensor of shape:

    • y_shape = \((num\_step, batch\_size, min(hidden\_size, num\_proj))\): If num_proj > 0,

    • y_shape = \((num\_step, batch\_size, hidden\_size)\): If num_proj = 0.

    Has the same data type with input bias_type.

  • output_h (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.

  • update (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.

  • reset (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.

  • new (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.

  • hidden_new (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.

A note about the bias_type:

  • If bias_input and bias_hidden both are None, bias_type is date type of init_h.

  • If bias_input is not None, bias_type is the date type of bias_input.

  • If bias_input is None and bias_hidden is not None, bias_type is the date type of bias_hidden.

引发
  • TypeError – If direction, activation or gate_order is not a str.

  • TypeError – If cell_depth or num_proj is not an int.

  • TypeError – If keep_prob or cell_clip is not a float.

  • TypeError – If time_major, reset_after or is_training is not a bool.

  • TypeError – If x, weight_input, weight_hidden, bias_input, bias_hidden, seq_length or ini_h is not a Tensor.

  • TypeError – If dtype of x, weight_input or weight_hidden is not float16.

  • TypeError – If dtype of init_h is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.random.rand(2, 8, 64).astype(np.float16))
>>> weight_i = Tensor(np.random.rand(64, 48).astype(np.float16))
>>> weight_h = Tensor(np.random.rand(16, 48).astype(np.float16))
>>> bias_i = Tensor(np.random.rand(48).astype(np.float16))
>>> bias_h = Tensor(np.random.rand(48).astype(np.float16))
>>> init_h = Tensor(np.random.rand(8, 16).astype(np.float16))
>>> dynamic_gru_v2 = ops.DynamicGRUV2()
>>> output = dynamic_gru_v2(x, weight_i, weight_h, bias_i, bias_h, None, init_h)
>>> print(output[0].shape)
(2, 8, 16)
class tinyms.primitives.DynamicRNN(*args, **kwargs)[源代码]

Applies a recurrent neural network to the input. Only long short-term memory (LSTM) currently supported.

\[\begin{split}\begin{array}{ll} \\ i_{t+1} = \sigma(W_{ix} x_{t+1} + b_{ix} + W_{ih} h_{(t)} + b_{ih}) \\ f_{t+1} = \sigma(W_{fx} x_{t+1} + b_{fx} + W_{fh} h_{(t)} + b_{fh}) \\ \tilde{c}_{t+1} = \tanh(W_{cx} x_{t+1} + b_{cx} + W_{ch} h_{(t)} + b_{ch}) \\ o_{t+1} = \sigma(W_{ox} x_{t+1} + b_{ox} + W_{oh} h_{(t)} + b_{oh}) \\ c_{t+1} = f_{t+1} * c_{(t)} + i_t * \tilde{c}_{t+1} \\ h_{t+1} = o_{t+1} * \tanh(c_{t+1}) \\ \end{array}\end{split}\]

where \(h_{t+1}\) is the hidden state at time t+1, \(x_{t+1}\) is the input at time t+1, \(h_{t}\) is the hidden state of the layer at time t or the initial hidden state at time 0, \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product. \(W, b\) are learnable weights between the output and the input in the formula. For instance, \(W_{ix}, b_{ix}\) are the weight and bias used to transform from input \(x\) to \(i\).

参数
  • cell_type (str) – A string identifying the cell type in the op. Default: ‘LSTM’. Only ‘LSTM’ is currently supported.

  • direction (str) – A string identifying the direction in the op. Default: ‘UNIDIRECTIONAL’. Only ‘UNIDIRECTIONAL’ is currently supported.

  • cell_depth (int) – An integer identifying the cell depth in the op. Default: 1.

  • use_peephole (bool) – A bool identifying if use peephole in the op. Default: False.

  • keep_prob (float) – A float identifying the keep prob in the op. Default: 1.0.

  • cell_clip (float) – A float identifying the cell clip in the op. Default: -1.0.

  • num_proj (int) – An integer identifying the num proj in the op. Default: 0.

  • time_major (bool) – A bool identifying the time major in the op. Default: True. Only True is currently supported.

  • activation (str) – A string identifying the type of activation function in the op. Default: ‘tanh’. Only ‘tanh’ is currently supported.

  • forget_bias (float) – A float identifying the forget bias in the op. Default: 0.0.

  • is_training (bool) – A bool identifying is training in the op. Default: True.

Inputs:
  • x (Tensor) - Current words. Tensor of shape \((num\_step, batch\_size, input\_size)\). The data type must be float16.

  • w (Tensor) - Weight. Tensor of shape \((input\_size + hidden\_size, 4 x hidden\_size)\). The data type must be float16.

  • b (Tensor) - Bias. Tensor of shape :math`(4 x hidden_size)`. The data type must be float16 or float32.

  • seq_length (Tensor) - The length of each batch. Tensor of shape \((batch\_size, )\). Only None is currently supported.

  • init_h (Tensor) - Hidden state of initial time. Tensor of shape \((1, batch\_size, hidden\_size)\). The data type must be float16.

  • init_c (Tensor) - Cell state of initial time. Tensor of shape \((1, batch\_size, hidden\_size)\). The data type must be float16.

Outputs:
  • y (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.

  • output_h (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). With data type of float16.

  • output_c (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.

  • i (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.

  • j (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.

  • f (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.

  • o (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.

  • tanhct (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.

引发
  • TypeError – If cell_type, direction or activation is not a str.

  • TypeError – If cell_depth or num_proj is not an int.

  • TypeError – If keep_prob, cell_clip or forget_bias is not a float.

  • TypeError – If use_peehpole, time_major or is_training is not a bool.

  • TypeError – If x, w, b, seq_length, init_h or init_c is not a Tensor.

  • TypeError – If dtype of x, w, init_h or nit_c is not float16.

  • TypeError – If dtype of b is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.random.rand(2, 16, 64).astype(np.float16))
>>> w = Tensor(np.random.rand(96, 128).astype(np.float16))
>>> b = Tensor(np.random.rand(128).astype(np.float16))
>>> init_h = Tensor(np.random.rand(1, 16, 32).astype(np.float16))
>>> init_c = Tensor(np.random.rand(1, 16, 32).astype(np.float16))
>>> dynamic_rnn = ops.DynamicRNN()
>>> output = dynamic_rnn(x, w, b, None, init_h, init_c)
>>> print(output[0].shape)
(2, 16, 32)
class tinyms.primitives.DynamicShape(*args, **kwargs)[源代码]

Returns the shape of the input tensor. And it used to be dynamic shape.

注解

Dynamic shape: After the graph is running, as the tensor flows in the graph, the specific shape of the tensor on each node on the graph can be inferred according to the structure of the graph. This shape is called a dynamic shape. As the input shape of the graph is different, the dynamic shape of the tensor in the graph will change.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

Tensor[int], 1-dim Tensor of type int32

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
>>> shape = ops.DynamicShape()
>>> output = shape(input_x)
>>> print(output)
[3 2 1]
class tinyms.primitives.EditDistance(*args, **kwargs)[源代码]

Computes the Levenshtein Edit Distance. It is used to measure the similarity of two sequences. The inputs are variable-length sequences provided by SparseTensors (hypothesis_indices, hypothesis_values, hypothesis_shape) and (truth_indices, truth_values, truth_shape).

参数

normalize (bool) – If true, edit distances are normalized by length of truth. Default: True.

Inputs:
  • hypothesis_indices (Tensor) - The indices of the hypothesis list SparseTensor. With int64 data type. The shape of tensor is \((N, R)\).

  • hypothesis_values (Tensor) - The values of the hypothesis list SparseTensor. With float32 data type. Must be 1-D vector with length of N.

  • hypothesis_shape (Tensor) - The shape of the hypothesis list SparseTensor. Must be R-length vector with int64 data type. Only constant value is allowed.

  • truth_indices (Tensor) - The indices of the truth list SparseTensor. With int64 data type. The shape of tensor is \((M, R)\).

  • truth_values (Tensor) - The values of the truth list SparseTensor. Must be 1-D vector with length of M. With float32 data type.

  • truth_shape (Tensor) - The shape of the truth list SparseTensor. Must be R-length vector with int64 data type. Only constant value is allowed.

Outputs:

Tensor, a dense tensor with rank R-1 and float32 data type.

引发

TypeError – If normalize is not a bool.

Supported Platforms:

Ascend

实际案例

>>> import numpy as np
>>> from mindspore import context
>>> from mindspore import Tensor
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>> class EditDistance(nn.Cell):
...     def __init__(self, hypothesis_shape, truth_shape, normalize=True):
...         super(EditDistance, self).__init__()
...         self.edit_distance = ops.EditDistance(normalize)
...         self.hypothesis_shape = hypothesis_shape
...         self.truth_shape = truth_shape
...
...     def construct(self, hypothesis_indices, hypothesis_values, truth_indices, truth_values):
...         return self.edit_distance(hypothesis_indices, hypothesis_values, self.hypothesis_shape,
...                                   truth_indices, truth_values, self.truth_shape)
...
>>> hypothesis_indices = Tensor(np.array([[0, 0, 0], [1, 0, 1], [1, 1, 1]]).astype(np.int64))
>>> hypothesis_values = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> hypothesis_shape = Tensor(np.array([1, 1, 2]).astype(np.int64))
>>> truth_indices = Tensor(np.array([[0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1]]).astype(np.int64))
>>> truth_values = Tensor(np.array([1, 3, 2, 1]).astype(np.float32))
>>> truth_shape = Tensor(np.array([2, 2, 2]).astype(np.int64))
>>> edit_distance = EditDistance(hypothesis_shape, truth_shape)
>>> output = edit_distance(hypothesis_indices, hypothesis_values, truth_indices, truth_values)
>>> print(output)
[[1. 1.]
 [1. 1.]]
class tinyms.primitives.Elu(*args, **kwargs)[源代码]

Computes exponential linear:

\[\begin{split}\text{ELU}(x)= \left\{ \begin{array}{align} \alpha(e^{x} - 1) & \text{if } x \le 0\\ x & \text{if } x \gt 0\\ \end{array}\right.\end{split}\]

The data type of input tensor must be float.

参数

alpha (float) – The coefficient of negative factor whose type is float, only support ‘1.0’ currently. Default: 1.0.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, has the same shape and data type as input_x.

引发
  • TypeError – If alpha is not a float.

  • TypeError – If dtype of input_x is neither float16 nor float32.

  • ValueError – If alpha is not equal to 1.0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> elu = ops.Elu()
>>> output = elu(input_x)
>>> print(output)
[[-0.63212055  4.         -0.99966455]
 [ 2.         -0.99326205  9.        ]]
class tinyms.primitives.EmbeddingLookup(*args, **kwargs)[源代码]

Returns a slice of input tensor based on the specified indices.

This Primitive has the similar functionality as GatherV2 operating on axis = 0, but has one more inputs: offset.

Inputs:
  • input_params (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). This represents a Tensor slice, instead of the entire Tensor. Currently, the dimension is restricted to be 2.

  • input_indices (Tensor) - The shape of tensor is \((y_1, y_2, ..., y_S)\). Specifies the indices of elements of the original Tensor. Values can be out of range of input_params, and the exceeding part will be filled with 0 in the output. Values does not support negative and the result is undefined if values are negative. The data type should be int32 or int64.

  • offset (int) - Specifies the offset value of this input_params slice. Thus the real indices are equal to input_indices minus offset.

Outputs:

Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\). The data type is the same with input_params.

引发
  • TypeError – If dtype of input_indices is not int.

  • ValueError – If length of shape of input_params is greater than 2.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> input_params = Tensor(np.array([[8, 9], [10, 11], [12, 13], [14, 15]]), mindspore.float32)
>>> input_indices = Tensor(np.array([[5, 2], [8, 5]]), mindspore.int32)
>>> offset = 4
>>> output = ops.EmbeddingLookup()(input_params, input_indices, offset)
>>> print(output)
[[[10. 11.]
  [ 0.  0.]]
 [[ 0.  0.]
  [10. 11.]]]
class tinyms.primitives.Eps(*args, **kwargs)[源代码]

Creates a tensor filled with x dtype minimum value.

Inputs:
  • x (Tensor) - Input tensor. The data type must be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same type and shape as x, but filled with x dtype minimum val.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor([4, 1, 2, 3], mindspore.float32)
>>> output = ops.Eps()(x)
>>> print(output)
[1.5258789e-05 1.5258789e-05 1.5258789e-05 1.5258789e-05]
class tinyms.primitives.Equal(*args, **kwargs)[源代码]

Computes the equivalence between two tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[\begin{split}out_{i} =\begin{cases} & \text{True, if } x_{i} = y_{i} \\ & \text{False, if } x_{i} \ne y_{i} \end{cases}\end{split}\]
Inputs:
  • x (Union[Tensor, Number]) - The first input is a number or a tensor whose data type is number.

  • y (Union[Tensor, Number]) - The second input is a number when the first input is a tensor or a tensor whose data type is number. The data type is the same as the first input.

Outputs:

Tensor, the shape is the same as the one after broadcasting,and the data type is bool.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1: The shape of two inputs are different
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> equal = ops.Equal()
>>> output = equal(x, 2.0)
>>> print(output)
[False True False]
>>> # case 2: The shape of two inputs are the same
>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 2, 4]), mindspore.int32)
>>> equal = ops.Equal()
>>> output = equal(x, y)
>>> print(output)
[ True  True False]
class tinyms.primitives.EqualCount(*args, **kwargs)[源代码]

Computes the number of the same elements of two tensors.

The two input tensors must have the same data type and shape.

Inputs:
  • x (Tensor) - The first input tensor. If the data type and shape of y are determined, then x must be the same as y, and vice versa. \((N,*)\) where \(*\) means, any number of additional dimensions.

  • y (Tensor) - The second input tensor. If the data type and shape of x are determined, then y must be the same as x, and vice versa.

Outputs:

Tensor, with the type same as input tensor and size as (1,).

引发
  • TypeError – If x or y is not a Tensor.

  • ValueError – If shape of x is not equal to shape of y.

Supported Platforms:

GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 2, 4]), mindspore.int32)
>>> equal_count = ops.EqualCount()
>>> output = equal_count(x, y)
>>> print(output)
[2]
class tinyms.primitives.Erf(*args, **kwargs)[源代码]

Computes the Gauss error function of x element-wise.

\[erf(x)=\frac{2} {\sqrt{\pi}} \int\limits_0^{x} e^{-t^{2}} dt\]
Inputs:
  • x (Tensor) - The input tensor. The data type must be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape and dtype as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([-1, 0, 1, 2, 3]), mindspore.float32)
>>> erf = ops.Erf()
>>> output = erf(x)
>>> print(output)
[-0.8427168   0.          0.8427168   0.99530876  0.99997765]
class tinyms.primitives.Erfc(*args, **kwargs)[源代码]

Computes the complementary error function of x element-wise.

\[erfc(x) = 1 - \frac{2} {\sqrt{\pi}} \int\limits_0^{x} e^{-t^{2}} dt\]
Inputs:
  • x (Tensor) - The input tensor. The data type must be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shap dtype as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([-1, 0, 1, 2, 3]), mindspore.float32)
>>> erfc = ops.Erfc()
>>> output = erfc(x)
>>> print(output)
[1.8427168e+00 1.0000000e+00 1.5728319e-01 4.6912432e-03 2.2351742e-05]
class tinyms.primitives.Erfinv(*args, **kwargs)[源代码]

Computes the inverse error function of input. The inverse error function is defined in the range (-1, 1) as:

\[erfinv(erf(x)) = x\]
Inputs:
  • input_x (Tensor) - The input tensor to compute to, with data type float32, float16.

Outputs:

Tensor, has the same shape and dtype as input_x.

引发

TypeError – If dtype of input_x is not one of: float32, float16.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([0, 0.5, -0.9]), mindspore.float32)
>>> erfinv = P.Erfinv()
>>> output = erfinv(x)
>>> print(output)
[ 0.          0.47695306 -1.1630805 ]
class tinyms.primitives.Erfinv(*args, **kwargs)[源代码]

Computes the inverse error function of input. The inverse error function is defined in the range (-1, 1) as:

\[erfinv(erf(x)) = x\]
Inputs:
  • input_x (Tensor) - The input tensor to compute to, with data type float32, float16.

Outputs:

Tensor, has the same shape and dtype as input_x.

引发

TypeError – If dtype of input_x is not one of: float32, float16.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([0, 0.5, -0.9]), mindspore.float32)
>>> erfinv = P.Erfinv()
>>> output = erfinv(x)
>>> print(output)
[ 0.          0.47695306 -1.1630805 ]
class tinyms.primitives.Exp(*args, **kwargs)[源代码]

Returns exponential of a tensor element-wise.

\[out_i = e^{x_i}\]
Inputs:
  • x (Tensor) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape and dtype as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
>>> exp = ops.Exp()
>>> output = exp(x)
>>> print(output)
[ 2.718282  7.389056 54.598152]
class tinyms.primitives.ExpandDims(*args, **kwargs)[源代码]

Adds an additional dimension to ‘input_x` at the given axis.

注解

If the specified axis is a negative number, the index is counted backward from the end and starts at 1.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • axis (int) - Specifies the dimension index at which to expand the shape of input_x. The value of axis must be in the range [-input_x.ndim-1, input_x.ndim]. Only constant value is allowed.

Outputs:

Tensor, the shape of tensor is \((1, x_1, x_2, ..., x_R)\) if the value of axis is 0. It has the same data type as input_x.

引发

ValueError – If axis is not an int or not in the valid range.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
>>> expand_dims = ops.ExpandDims()
>>> output = expand_dims(input_tensor, 0)
>>> print(output)
[[[2. 2.]
  [2. 2.]]]
class tinyms.primitives.Expm1(*args, **kwargs)[源代码]

Returns exponential then minus 1 of a tensor element-wise.

\[out_i = e^{x_i} - 1\]
Inputs:
  • x (Tensor) - The input tensor. With float16 or float32 data type. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([0.0, 1.0, 2.0, 4.0]), mindspore.float32)
>>> expm1 = ops.Expm1()
>>> output = expm1(x)
>>> print(output)
[ 0.        1.718282  6.389056 53.598152]
class tinyms.primitives.Eye(*args, **kwargs)[源代码]

Creates a tensor with ones on the diagonal and zeros the rest.

Inputs:
  • n (int) - The number of rows of returned tensor. only constant value.

  • m (int) - The number of columns of returned tensor. only constant value.

  • t (mindspore.dtype) - MindSpore’s dtype, The data type of the returned tensor. The data type can be Number.

Outputs:

Tensor, a tensor with ones on the diagonal and the rest of elements are zero. The shape of output depends on the user’s Inputs n and m. And the data type depends on Inputs t.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> eye = ops.Eye()
>>> output = eye(2, 2, mindspore.int32)
>>> print(output)
[[1 0]
 [0 1]]
>>> print(output.dtype)
Int32
>>> output = eye(1, 2, mindspore.float64)
>>> print(output)
[[1. 0.]]
>>> print(output.dtype)
Float64
>>> # if wants a anti-diagonal
>>> anti_diagonal_input = eye(2, 2, mindspore.int32)
>>> # Note that ReverseV2 only supports "Ascend" at this time
>>> reverse = ops.ReverseV2([1])
>>> anti_diagonal_output = reverse(anti_diagonal_input)
>>> print(anti_diagonal_output)
[[0 1]
 [1 0]]
class tinyms.primitives.FFT3D(*args, **kwargs)[源代码]

Forward FFT with Three-Dimensional Input.

警告

This is an experimental prototype that is subject to change and/or deletion.

Inputs:
  • input_tensor (Tensor) - Three dimensional tensor, supported data type is float32.

Outputs:
  • output_tensor (Tensor) - The tensor after undergoing fast Fourier transform, the data type is complex64.

Supported Platforms:

GPU

class tinyms.primitives.FastGeLU(*args, **kwargs)[源代码]

Fast Gaussian Error Linear Units activation function.

FastGeLU is defined as follows:

\[\text{output} = \frac {x} {1 + \exp(-1.702 * \left| x \right|)} * \exp(0.851 * (x - \left| x \right|)),\]

where \(x\) is the element of the input.

Inputs:
  • x (Tensor) - Input to compute the FastGeLU with data type of float16 or float32.

Outputs:

Tensor, with the same type and shape as x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> fast_gelu = ops.FastGeLU()
>>> output = fast_gelu(x)
>>> print(output)
[[-1.5418735e-01  3.9921875e+00 -9.7473649e-06]
 [ 1.9375000e+00 -1.0052517e-03  8.9824219e+00]]
class tinyms.primitives.FastGelu(**kwargs)[源代码]

Same as operator FastGeLU. FastGelu will be deprecated in the future. Please use FastGeLU instead.

class tinyms.primitives.Fill(*args, **kwargs)[源代码]

Creates a tensor filled with a scalar value.

Creates a tensor with shape described by the first argument and fills it with values in the second argument.

Inputs:
  • type (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed.

  • shape (tuple) - The specified shape of output tensor. Only constant value is allowed.

  • value (scalar) - Value to fill the returned tensor. Only constant value is allowed.

Outputs:

Tensor, has the same type and shape as input value.

引发

TypeError – If shape is not a tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> fill = ops.Fill()
>>> output = fill(mindspore.float32, (2, 2), 1)
>>> print(output)
[[1. 1.]
 [1. 1.]]
>>> output = fill(mindspore.float32, (3, 3), 0)
>>> print(output)
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
class tinyms.primitives.Flatten(*args, **kwargs)[源代码]

Flattens a tensor without changing its batch size on the 0-th axis.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, \ldots)\) to be flattened, where \(N\) is batch size.

Outputs:

Tensor, the shape of the output tensor is \((N, X)\), where \(X\) is the product of the remaining dimension.

引发
  • TypeError – If input_x is not a Tensor.

  • ValueError – If length of shape of input_x is less than 1.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.ones(shape=[1, 2, 3, 4]), mindspore.float32)
>>> flatten = ops.Flatten()
>>> output = flatten(input_x)
>>> print(output.shape)
(1, 24)
class tinyms.primitives.FloatStatus(*args, **kwargs)[源代码]

Determines if the elements contain Not a Number(NaN), infinite or negative infinite. 0 for normal, 1 for overflow.

Inputs:
  • x (Tensor) - The input tensor. The data type must be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the shape of (1,), and the dtype is mindspore.dtype.float32.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

GPU

实际案例

>>> float_status = ops.FloatStatus()
>>> x = Tensor(np.array([np.log(-1), 1, np.log(0)]), mindspore.float32)
>>> result = float_status(x)
>>> print(result)
[1.]
class tinyms.primitives.Floor(*args, **kwargs)[源代码]

Rounds a tensor down to the closest integer element-wise.

\[out_i = \lfloor x_i \rfloor\]
Inputs:
  • x (Tensor) - The input tensor. Its element data type must be float. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If dtype of x is not float.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.1, 2.5, -1.5]), mindspore.float32)
>>> floor = ops.Floor()
>>> output = floor(x)
>>> print(output)
[ 1.  2. -2.]
class tinyms.primitives.FloorDiv(*args, **kwargs)[源代码]

Divides the first input tensor by the second input tensor element-wise and round down to the closest integer.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = \text{floor}( \frac{x_i}{y_i})\]

where the \(floor\) indicates the Floor operator, for more details, please refer to the Floor operator.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([2, 4, -1]), mindspore.int32)
>>> y = Tensor(np.array([3, 3, 3]), mindspore.int32)
>>> floor_div = ops.FloorDiv()
>>> output = floor_div(x, y)
>>> print(output)
[ 0  1 -1]
class tinyms.primitives.FloorMod(*args, **kwargs)[源代码]

Computes the remainder of division element-wise. It’s a flooring divide. E.g. \(floor(x / y) * y + mod(x, y) = x\).

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool , and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} =\text{floor}(x_{i} // y_{i})\]

where the \(floor\) indicates the Floor operator, for more details, please refer to the Floor operator.

警告

  • The input data does not support 0.

  • When the elements of input exceeds 2048 , the accuracy of operator cannot guarantee the requirement of double thousandths in the mini form.

  • Due to different architectures, the calculation results of this operator on NPU and CPU may be inconsistent.

  • If shape is expressed as (D1,D2… ,Dn), then D1*D2… *DN<=1000000,n<=8.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([2, 4, -1]), mindspore.int32)
>>> y = Tensor(np.array([3, 3, 3]), mindspore.int32)
>>> floor_mod = ops.FloorMod()
>>> output = floor_mod(x, y)
>>> print(output)
[2 1 2]
class tinyms.primitives.FusedCastAdamWeightDecay(*args, **kwargs)[源代码]

Updates gradients by the Adaptive Moment Estimation (AdamWeightDecay) algorithm with weight decay. This operator incorporates type conversion when parameters are initialized with dtype of float16.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization. The AdamWeightDecay variant was proposed in Decoupled Weight Decay Regularization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ update = \frac{m}{\sqrt{v} + eps} \\ update = \begin{cases} update + weight\_decay * w & \text{ if } weight\_decay > 0 \\ update & \text{ otherwise } \end{cases} \\ w = w - lr * update \end{array}\end{split}\]

\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(\beta_1, \beta_2\) represent beta1 and beta2, \(lr\) represents learning_rate, \(w\) represents var, \(decay\) represents weight_decay, \(\epsilon\) represents epsilon.

参数

use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

Inputs:
  • var (Tensor) - Weights to be updated with the type float16 or float32.

  • m (Tensor) - The 1st moment vector in the updating formula with the type float32.

  • v (Tensor) - the 2nd moment vector in the updating formula with the type float32.

  • lr (float) - \(lr\) in the updating formula.

  • beta1 (float) - The exponential decay rate for the 1st moment estimations.

  • beta2 (float) - The exponential decay rate for the 2nd moment estimations.

  • epsilon (float) - Term added to the denominator to improve numerical stability.

  • decay (float) - The weight decay value, must be a scalar tensor with float data type.

  • gradient (Tensor) - Gradient, has the type float16.

Outputs:

Tuple of 3 Tensor, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • m (Tensor) - The same shape and data type as m.

  • v (Tensor) - The same shape and data type as v.

Supported Platforms:

CPU

实际案例

>>> import numpy as np
>>> import mindspore.context as context
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>> from mindspore import Tensor, Parameter
>>> from mindspore import dtype as mstype
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.opt = ops.FusedCastAdamWeightDecay()
...         self.var = Parameter(Tensor(np.ones([2, 2]), mstype.float16), name="var")
...         self.m = Parameter(Tensor(np.ones([2, 2]), mstype.float32), name="m")
...         self.v = Parameter(Tensor(np.ones([2, 2]), mstype.float32), name="v")
...     def construct(self, lr, beta1, beta2, epsilon, decay, grad):
...         out = self.opt(self.var, self.m, self.v, lr, beta1, beta2, epsilon, decay, grad)
...         return out
>>> context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
>>> net = Net()
>>> gradient = Tensor(np.ones([2, 2]), mstype.float16)
>>> output = net(0.001, 0.9, 0.999, 1e-8, 0.0, gradient)
>>> print(net.var.asnumpy())
class tinyms.primitives.FusedSparseAdam(*args, **kwargs)[源代码]

Merges the duplicate value of the gradient and then updates parameters by the Adaptive Moment Estimation (Adam) algorithm. This operator is used when the gradient is sparse.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w = w - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]

\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t\) and \(beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents var, \(\epsilon\) represents epsilon.

All of inputs except indices comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

  • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.

Inputs:
  • var (Parameter) - Parameters to be updated with float32 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • m (Parameter) - The 1st moment vector in the updating formula, has the same shape and data type as var.

  • v (Parameter) - The 2nd moment vector in the updating formula, has the same shape and data type as var. Mean square gradients, has the same type as var with float32 data type.

  • beta1_power (Tensor) - \(beta_1^t\) in the updating formula with float32 data type. The shape is \((1, )\).

  • beta2_power (Tensor) - \(beta_2^t\) in the updating formula with float32 data type. The shape is \((1, )\).

  • lr (Tensor) - \(l\) in the updating formula. With float32 data type. The shape is \((1, )\).

  • beta1 (Tensor) - The exponential decay rate for the 1st moment estimations with float32 data type. The shape is \((1, )\).

  • beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations with float32 data type. The shape is \((1, )\).

  • epsilon (Tensor) - Term added to the denominator to improve numerical stability with float32 data type. The shape is \((1, )\).

  • gradient (Tensor) - Gradient, has the same data type as var and gradient.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - Gradient indices with int32 data type and indices.shape[0] = gradient.shape[0].

Outputs:

Tuple of 3 Tensors, this operator will update the input parameters directly, the outputs are useless.

  • var (Tensor) - A Tensor with shape \((1, )\).

  • m (Tensor) - A Tensor with shape \((1, )\).

  • v (Tensor) - A Tensor with shape \((1, )\).

引发
  • TypeError – If neither use_locking nor use_neserov is a bool.

  • TypeError – If dtype of var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient or indices is not float32.

Supported Platforms:

Ascend CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.sparse_apply_adam = ops.FusedSparseAdam()
...         self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var")
...         self.m = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="m")
...         self.v = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="v")
...     def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad, indices):
...         out = self.sparse_apply_adam(self.var, self.m, self.v, beta1_power, beta2_power, lr, beta1, beta2,
...                                      epsilon, grad, indices)
...         return out
...
>>> net = Net()
>>> beta1_power = Tensor(0.9, mindspore.float32)
>>> beta2_power = Tensor(0.999, mindspore.float32)
>>> lr = Tensor(0.001, mindspore.float32)
>>> beta1 = Tensor(0.9, mindspore.float32)
>>> beta2 = Tensor(0.999, mindspore.float32)
>>> epsilon = Tensor(1e-8, mindspore.float32)
>>> gradient = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]), mindspore.float32)
>>> indices = Tensor([0, 1], mindspore.int32)
>>> output = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient, indices)
>>> print(net.var.asnumpy())
[[[0.9997121  0.9997121 ]]
 [[0.9997121  0.9997121 ]]
 [[0.99971527 0.99971527]]]
class tinyms.primitives.FusedSparseFtrl(*args, **kwargs)[源代码]

Merges the duplicate value of the gradient and then updates relevant entries according to the FTRL-proximal scheme.

All of inputs except indices comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • lr (float) – The learning rate value, must be positive.

  • l1 (float) – l1 regularization strength, must be greater than or equal to zero.

  • l2 (float) – l2 regularization strength, must be greater than or equal to zero.

  • lr_power (float) – Learning rate power controls how the learning rate decreases during training, must be less than or equal to zero. Use fixed learning rate if lr_power is zero.

  • use_locking (bool) – Use locks for updating operation if true . Default: False.

Inputs:
  • var (Parameter) - The variable to be updated. The data type must be float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - The accumulation to be updated, must be same type and shape as var.

  • linear (Parameter) - the linear coefficient to be updated, must be same type and shape as var.

  • grad (Tensor) - A tensor of the same type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A vector of indices into the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].

Outputs:

Tuple of 3 Tensor, this operator will update the input parameters directly, the outputs are useless.

  • var (Tensor) - A Tensor with shape \((1, )\).

  • accum (Tensor) - A Tensor with shape \((1, )\).

  • linear (Tensor) - A Tensor with shape \((1, )\).

引发
  • TypeError – If lr, l1, l2 or lr_power is not a float.

  • ValueError – If shape of lr_power less than or equal to zero.

  • TypeError – If dtype of var is not float32.

  • TypeError – If dtype of indices is not int32.

  • TypeError – If shape of accum, linear or grad is not same as var.

  • TypeError – If shape of indices is not same as shape of first dimension of grad.

Supported Platforms:

Ascend CPU

实际案例

>>> class SparseApplyFtrlNet(nn.Cell):
...     def __init__(self):
...         super(SparseApplyFtrlNet, self).__init__()
...         self.sparse_apply_ftrl = ops.FusedSparseFtrl(lr=0.01, l1=0.0, l2=0.0, lr_power=-0.5)
...         self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="accum")
...         self.linear = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="linear")
...
...     def construct(self, grad, indices):
...         out = self.sparse_apply_ftrl(self.var, self.accum, self.linear, grad, indices)
...         return out
...
>>> net = SparseApplyFtrlNet()
>>> grad = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]).astype(np.float32))
>>> indices = Tensor(np.array([0, 1]).astype(np.int32))
>>> output = net(grad, indices)
>>> print(net.var.asnumpy())
[[[-0.00598256 -0.00598256]]
 [[-0.00598256 -0.00598256]]
 [[ 1.          1.        ]]]
class tinyms.primitives.FusedSparseLazyAdam(*args, **kwargs)[源代码]

Merges the duplicate value of the gradient and then updates parameters by the Adaptive Moment Estimation (LazyAdam) algorithm. This operator is used when the gradient is sparse. The behavior is not equivalent to the original Adam algorithm, as only the current indices parameters will be updated.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w = w - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]

\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t\) and \(beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents var, \(\epsilon\) represents epsilon.

All of inputs except indices comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

  • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.

Inputs:
  • var (Parameter) - Parameters to be updated with float32 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • m (Parameter) - The 1st moment vector in the updating formula, has the same shape and data type as var.

  • v (Parameter) - The 2nd moment vector in the updating formula, has the same shape and data type as var. Mean square gradients, has the same type as var with float32 data type.

  • beta1_power (Tensor) - \(beta_1^t\) in the updating formula with float32 data type. The shape is \((1, )\).

  • beta2_power (Tensor) - \(beta_2^t\) in the updating formula with float32 data type. The shape is \((1, )\).

  • lr (Tensor) - \(l\) in the updating formula with float32 data type. The shape is \((1, )\).

  • beta1 (Tensor) - The exponential decay rate for the 1st moment estimations with float32 data type. The shape is \((1, )\).

  • beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations with float32 data type. The shape is \((1, )\).

  • epsilon (Tensor) - Term added to the denominator to improve numerical stability with float32 data type. The shape is \((1, )\).

  • gradient (Tensor) - Gradient value with float32 data type and gradient.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - Gradient indices with int32 data type and indices.shape[0] = gradient.shape[0].

Outputs:

Tuple of 3 Tensors, this operator will update the input parameters directly, the outputs are useless.

  • var (Tensor) - A Tensor with shape \((1, )\).

  • m (Tensor) - A Tensor with shape \((1, )\).

  • v (Tensor) - A Tensor with shape \((1, )\).

引发
  • TypeError – If neither use_locking nor use_nestrov is a bool.

  • TypeError – If dtype of var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon or gradient is not float32.

  • TypeError – If dtype of indices is not int32.

Supported Platforms:

Ascend CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.sparse_apply_lazyadam = ops.FusedSparseLazyAdam()
...         self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var")
...         self.m = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="m")
...         self.v = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="v")
...     def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad, indices):
...         out = self.sparse_apply_lazyadam(self.var, self.m, self.v, beta1_power, beta2_power, lr, beta1,
...                                          beta2, epsilon, grad, indices)
...         return out
...
>>> net = Net()
>>> beta1_power = Tensor(0.9, mindspore.float32)
>>> beta2_power = Tensor(0.999, mindspore.float32)
>>> lr = Tensor(0.001, mindspore.float32)
>>> beta1 = Tensor(0.9, mindspore.float32)
>>> beta2 = Tensor(0.999, mindspore.float32)
>>> epsilon = Tensor(1e-8, mindspore.float32)
>>> gradient = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]), mindspore.float32)
>>> indices = Tensor([0, 1], mindspore.int32)
>>> output = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient, indices)
>>> print(net.var.asnumpy())
[[[0.9997121  0.9997121 ]]
 [[0.9997121  0.9997121 ]]
 [[1.         1.        ]]]
class tinyms.primitives.FusedSparseProximalAdagrad(*args, **kwargs)[源代码]

Merges the duplicate value of the gradient and then updates relevant entries according to the proximal adagrad algorithm.

\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ \text{prox_v} = var - lr * grad * \frac{1}{\sqrt{accum}} \\ var = \frac{sign(\text{prox_v})}{1 + lr * l2} * \max(\left| \text{prox_v} \right| - lr * l1, 0) \end{array}\end{split}\]

All of inputs except indices comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – If true, the variable and accumulation tensors will be protected from being updated. Default: False.

Inputs:
  • var (Parameter) - Variable tensor to be updated. The data type must be float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Variable tensor to be updated, has the same shape and data type as var.

  • lr (Tensor) - The learning rate value. The data type must be float32. The shape is \((1, )\).

  • l1 (Tensor) - l1 regularization strength. The data type must be float32. The shape is \((1, )\).

  • l2 (Tensor) - l2 regularization strength. The data type must be float32. The shape is \((1, )\).

  • grad (Tensor) - A tensor of the same data type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A vector of indices into the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].

Outputs:

Tuple of 2 Tensors, this operator will update the input parameters directly, the outputs are useless.

  • var (Tensor) - A Tensor with shape \((1, )\).

  • accum (Tensor) - A Tensor with shape \((1, )\).

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of var, accum, lr, l1, l2 or grad is not float32.

  • TypeError – If dtype of indices is not int32.

Supported Platforms:

Ascend CPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.sparse_apply_proximal_adagrad = ops.FusedSparseProximalAdagrad()
...         self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="accum")
...         self.lr = Tensor(0.01, mindspore.float32)
...         self.l1 = Tensor(0.0, mindspore.float32)
...         self.l2 = Tensor(0.0, mindspore.float32)
...     def construct(self, grad, indices):
...         out = self.sparse_apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1,
...                                                  self.l2, grad, indices)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]).astype(np.float32))
>>> indices = Tensor(np.array([0, 1]).astype(np.int32))
>>> output = net(grad, indices)
>>> print(net.var.asnumpy())
[[[0.99900496 0.99900496]]
 [[0.99900496 0.99900496]]
 [[1.         1.        ]]]
class tinyms.primitives.FusedWeightScaleApplyMomentum(*args, **kwargs)[源代码]

Optimizer that implements the Momentum algorithm with weight decay and loss scale.

Refer to the paper On the importance of initialization and momentum in deep learning for more details.

Refer to mindspore.nn.Momentum for more details about the formula and usage.

Inputs of variable, accumulation and gradient comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. Data type conversion of Parameter is not supported. RuntimeError exception will be thrown.

Inputs:
  • weight_decay (Tensor) - The weight decay value, must be a scalar tensor with float data type. Default: 0.0.

  • loss_scale (Tensor) - The loss scale value, must be a scalar tensor with float data type. Default: 1.0.

  • variable (Parameter) - Weights to be updated. data type must be float.

  • accumulation (Parameter) - Accumulated gradient value by moment weight. Has the same data type with variable.

  • learning_rate (Union[Number, Tensor]) - The learning rate value, must be a float number or a scalar tensor with float data type.

  • gradient (Tensor) - Gradient, has the same data type as variable.

  • momentum (Union[Number, Tensor]) - Momentum, must be a float number or a scalar tensor with float data type.

Outputs:

Tensor, parameters to be updated.

Supported Platforms:

GPU

实际案例

Please refer to the usage in mindspore.nn.Momentum, and add weight_decay and loss_scale as inputs.

class tinyms.primitives.Gamma(*args, **kwargs)[源代码]

Produces random positive floating-point values x, distributed according to probability density function:

\[\text{P}(x|α,β) = \frac{\exp(-x/β)}{{β^α}\cdot{\Gamma(α)}}\cdot{x^{α-1}}\]
参数
  • seed (int) – Random seed, must be non-negative. Default: 0.

  • seed2 (int) – Random seed2, must be non-negative. Default: 0.

Inputs:
  • shape (tuple) - The shape of random tensor to be generated. Only constant value is allowed.

  • alpha (Tensor) - The α distribution parameter. It must be greater than 0. It is also known as the shape parameter with float32 data type.

  • beta (Tensor) - The β distribution parameter. It must be greater than 0. It is also known as the scale parameter with float32 data type.

Outputs:

Tensor. The shape must be the broadcasted shape of Input “shape” and shapes of alpha and beta. The dtype is float32.

引发
  • TypeError – If neither seed nor seed2 is an int.

  • TypeError – If neither alpha nor beta is a Tensor.

  • ValueError – If shape is not a constant value.

Supported Platforms:

Ascend

实际案例

>>> shape = (3, 1, 2)
>>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mstype.float32)
>>> beta = Tensor(np.array([1.0]), mstype.float32)
>>> gamma = ops.Gamma(seed=3)
>>> output = gamma(shape, alpha, beta)
>>> result = output.shape
>>> print(result)
(3, 2, 2)
class tinyms.primitives.Gather(*args, **kwargs)[源代码]

Returns a slice of the input tensor based on the specified indices and axis.

Slices the input tensor base on the indices at specified axis. See the following example for more clear.

Inputs:
  • input_params (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The original Tensor.

  • input_indices (Tensor) - The shape of tensor is \((y_1, y_2, ..., y_S)\). Specifies the indices of elements of the original Tensor. Must be in the range [0, input_param.shape[axis]) which are only validated on CPU. The data type can be int32 or int64.

  • axis (int) - Specifies the dimension index to gather indices.

Outputs:

Tensor, the shape of tensor is \(input\_params.shape[:axis] + input\_indices.shape + input\_params.shape[axis + 1:]\).

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_params = Tensor(np.array([[1, 2, 7, 42], [3, 4, 54, 22], [2, 2, 55, 3]]), mindspore.float32)
>>> input_indices = Tensor(np.array([1, 2]), mindspore.int32)
>>> axis = 1
>>> output = ops.Gather()(input_params, input_indices, axis)
>>> print(output)
[[ 2.  7.]
 [ 4. 54.]
 [ 2. 55.]]
>>> axis = 0
>>> output = ops.Gather()(input_params, input_indices, axis)
>>> print(output)
[[3. 4. 54. 22.]
 [2. 2. 55.  3.]]
class tinyms.primitives.GatherD(*args, **kwargs)[源代码]

Gathers values along an axis specified by dim.

For a 3-D tensor, the output is:

output[i][j][k] = x[index[i][j][k]][j][k]  # if dim == 0

output[i][j][k] = x[i][index[i][j][k]][k]  # if dim == 1

output[i][j][k] = x[i][j][index[i][j][k]]  # if dim == 2

If x is an n-D tensor with shape \((z_0, z_1, ..., z_i, ..., z_{n-1})\) and dim = i, the index must be an n-D tensor with shape \((z_0, z_1, ..., y, ..., z_{n-1})\) where y>=1 and the output will have the same shape as index.

Inputs:
  • x (Tensor) - The source tensor. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • dim (int) - The axis along which to index. It must be int32 or int64. Only constant value is allowed.

  • index (Tensor) - The indices of elements to gather. It can be one of the following data types: int32, int64. The value range of each index element is [-x_rank[dim], x_rank[dim]).

Outputs:

Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\), has the same data type with x.

引发
  • TypeError – If dtype of dim or index is neither int32 nor int64.

  • ValueError – If length of shape of x is not equal to length of shape of index.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[1, 2], [3, 4]]), mindspore.int32)
>>> index = Tensor(np.array([[0, 0], [1, 0]]), mindspore.int32)
>>> dim = 1
>>> output = ops.GatherD()(x, dim, index)
>>> print(output)
[[1 1]
 [4 3]]
class tinyms.primitives.GatherNd(*args, **kwargs)[源代码]

Gathers slices from a tensor by indices.

Using given indices to gather slices from a tensor with a specified shape.

indices is an K-dimensional integer tensor. Supposes it as a (K-1)-dimensional tensor and each element of it defines a slice of input_x:

\[output[(i_0, ..., i_{K-2})] = input\_x[indices[(i_0, ..., i_{K-2})]]\]

The last dimension of indices can not more than the rank of input_x: \(indices.shape[-1] <= input\_x.rank\).

Inputs:
  • input_x (Tensor) - The target tensor to gather values. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index tensor, with int32 or int64 data type. The dimension of indices should be <= the dimension of input_x.

Outputs:

Tensor, has the same type as input_x and the shape is indices_shape[:-1] + x_shape[indices_shape[-1]:].

引发

ValueError – If length of shape of input_x is less than the last dimension of indices.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> op = ops.GatherNd()
>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
>>> output = op(input_x, indices)
>>> print(output)
[-0.1  0.5]
class tinyms.primitives.GatherV2(**kwargs)[源代码]

Same as operator Gather. GatherV2 will be deprecated in the future. Please use Gather instead.

class tinyms.primitives.GeLU(*args, **kwargs)[源代码]

Gaussian Error Linear Units activation function.

GeLU is described in the paper Gaussian Error Linear Units (GELUs). And also please refer to BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.

GeLU is defined as follows:

\[\text{output} = 0.5 * x * (1 + tanh(x / \sqrt{2})),\]

where \(tanh\) is the hyperbolic tangent.

Inputs:
  • x (Tensor) - Input to compute the GeLU with data type of float16 or float32.

Outputs:

Tensor, with the same type and shape as x.

引发
  • TypeError – If x is not a Tensor.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
>>> gelu = ops.GeLU()
>>> result = gelu(x)
>>> print(result)
[0.841192  1.9545976  2.9963627]
class tinyms.primitives.GeSwitch(*args, **kwargs)[源代码]

Adds control switch to data.

Switch data flows into false or true branch depending on the condition. If the condition is true, the true branch will be activated, or vise verse.

Inputs:
  • data (Union[Tensor, Number]) - The data to be used for switch control.

  • pred (Tensor) - It must be a scalar whose type is bool and shape is (), It is used as condition for switch control.

Outputs:

tuple. Output is tuple(false_output, true_output). The Elements in the tuple has the same shape of input data. The false_output connects with the false_branch and the true_output connects with the true_branch.

引发
  • TypeError – If data is neither a Tensor nor a Number.

  • TypeError – If pred is not a Tensor.

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.square = ops.Square()
...         self.add = ops.Add()
...         self.value = Tensor(np.full((1), 3), mindspore.float32)
...         self.switch = ops.GeSwitch()
...         self.merge = ops.Merge()
...         self.less = ops.Less()
...
...     def construct(self, x, y):
...         cond = self.less(x, y)
...         st1, sf1 = self.switch(x, cond)
...         st2, sf2 = self.switch(y, cond)
...         add_ret = self.add(st1, st2)
...         st3, sf3 = self.switch(self.value, cond)
...         sq_ret = self.square(sf3)
...         ret = self.merge((add_ret, sq_ret))
...         return ret[0]
...
>>> x = Tensor(10.0, dtype=mindspore.float32)
>>> y = Tensor(5.0, dtype=mindspore.float32)
>>> net = Net()
>>> output = net(x, y)
>>> print(output)
class tinyms.primitives.Gelu(**kwargs)[源代码]

Same as operator GeLU. Gelu will be deprecated in the future. Please use GeLU instead.

class tinyms.primitives.GetCenterOfMass(*args, **kwargs)[源代码]

Get coordinate of centroid of each residue. Assume system has n atoms.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数

residue_numbers (int32) – the number of residues m.

Inputs:
  • start (Tensor) - The start atom index of each residue. The data type is int32 and the shape is \((m,)\).

  • end (Tensor) - The end atom index of each residue. The data type is int32 and the shape is \((m,)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • atom_mass (Tensor) - The mass of each atom and the atom number is n. The data type is float32 and the shape is \((n,)\).

  • residue_mass_inverse (Tensor) - The inverse of mass of each residue. The data type is float32 and the shape is \((m,)\).

Outputs:
  • center_of_mass (Tensor) - The coordinate of centroid of each residue. The data type is float32 and the shape is \((m, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.GetNext(*args, **kwargs)[源代码]

Returns the next element in the dataset queue.

注解

The GetNext operation needs to be associated with network and it also depends on the init_dataset interface, it can’t be used directly as a single operation. For details, please refer to connect_network_with_dataset source code.

参数
  • types (list[mindspore.dtype]) – The type of the outputs.

  • shapes (list[tuple[int]]) – The dimensionality of the outputs.

  • output_num (int) – The output number, length of types and shapes.

  • shared_name (str) – The queue name of init_dataset interface.

Inputs:

No inputs.

Outputs:

tuple[Tensor], the output of Dataset. The shape is described in shapes and the type is described in types.

Supported Platforms:

Ascend GPU

实际案例

>>> train_dataset = create_custom_dataset()
>>> dataset_helper = mindspore.DatasetHelper(train_dataset, dataset_sink_mode=True)
>>> dataset = dataset_helper.iter.dataset
>>> dataset_types, dataset_shapes = dataset_helper.types_shapes()
>>> queue_name = dataset.__transfer_dataset__.queue_name
>>> get_next = ops.GetNext(dataset_types, dataset_shapes, len(dataset_types), queue_name)
>>> data, label = get_next()
>>> relu = ops.ReLU()
>>> result = relu(data).asnumpy()
>>> print(result.shape)
(32, 1, 32, 32)
class tinyms.primitives.Greater(*args, **kwargs)[源代码]

Computes the boolean value of \(x > y\) element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[\begin{split}out_{i} =\begin{cases} & \text{True, if } x_{i}>y_{i} \\ & \text{False, if } x_{i}<=y_{i} \end{cases}\end{split}\]

注解

Broadcasting is supported.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is bool.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 1, 4]), mindspore.int32)
>>> greater = ops.Greater()
>>> output = greater(x, y)
>>> print(output)
[False  True False]
class tinyms.primitives.GreaterEqual(*args, **kwargs)[源代码]

Computes the boolean value of \(x >= y\) element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[\begin{split}out_{i} =\begin{cases} & \text{True, if } x_{i}>=y_{i} \\ & \text{False, if } x_{i}<y_{i} \end{cases}\end{split}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is bool.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 1, 4]), mindspore.int32)
>>> greater_equal = ops.GreaterEqual()
>>> output = greater_equal(x, y)
>>> print(output)
[True True False]
class tinyms.primitives.HShrink(*args, **kwargs)[源代码]

Applies the hard shrinkage function element-wise, each element complies the follow function:

\[\begin{split}\text{HardShrink}(x) = \begin{cases} x, & \text{ if } x > \lambda \\ x, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases}\end{split}\]
参数

lambd (float) – The value for the HardShrink formulation. Default: 0.5

Inputs:
  • input_x (Tensor) - The input of HardShrink with data type of float16 or float32.

Outputs:

Tensor, the same shape and data type as the input.

Supported Platforms:

Ascend

引发
  • TypeError – If lambd is not a float.

  • TypeError – If dtype of input_x is neither float16 nor float32.

实际案例

>>> input_x = Tensor(np.array([[ 0.5,  1,  2.0],[0.0533,0.0776,-2.1233]]),mstype.float32)
>>> hshrink = P.HShrink()
>>> output = hshrink(input_x)
>>> print(output)
[[ 0.      1.      2.    ]
[ 0.      0.     -2.1233]]
class tinyms.primitives.HSigmoid(*args, **kwargs)[源代码]

Hard sigmoid activation function.

Applies hard sigmoid activation element-wise. The input is a Tensor with any valid shape.

Hard sigmoid is defined as:

\[\text{hsigmoid}(x_{i}) = max(0, min(1, \frac{x_{i} + 3}{6})),\]

where \(x_i\) is an element of the input Tensor.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the input_x.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> hsigmoid = ops.HSigmoid()
>>> input_x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> result = hsigmoid(input_x)
>>> print(result)
[0.3333 0.1666 0.5    0.8335 0.6665]
class tinyms.primitives.HSwish(*args, **kwargs)[源代码]

Hard swish activation function.

Applies hswish-type activation element-wise. The input is a Tensor with any valid shape.

Hard swish is defined as:

\[\text{hswish}(x_{i}) = x_{i} * \frac{ReLU6(x_{i} + 3)}{6},\]

where \(x_i\) is an element of the input Tensor.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the input_x.

引发
  • TypeError – If input_x is not a Tensor.

  • TypeError – If dtype of input_x is neither float16 nor float32.

Supported Platforms:

GPU CPU

实际案例

>>> hswish = ops.HSwish()
>>> input_x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> result = hswish(input_x)
>>> print(result)
[-0.3333  -0.3333  0  1.666  0.6665]
class tinyms.primitives.HistogramFixedWidth(*args, **kwargs)[源代码]

Returns a rank 1 histogram counting the number of entries in values that fall into every bin. The bins are equal width and determined by the arguments range and nbins.

参数
  • dtype (str) – An optional attribute. The dtype must be “int32”. Default: “int32”.

  • nbins (int) – The number of histogram bins, the type is a positive integer.

Inputs:
  • x (Tensor) - Numeric Tensor. Must be one of the following types: int32, float32, float16.

  • range (Tensor) - Must has the same data type as x, and the shape is [2]. x <= range[0] will be mapped to hist[0], x >= range[1] will be mapped to hist[-1].

Outputs:

Tensor, the type is int32.

引发
  • TypeError – If dtype is not a str or nbins is not an int.

  • ValueError – If nbins is less than 1.

  • ValueError – If dtype is neither ‘int32’ nor ‘int64’.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor([-1.0, 0.0, 1.5, 2.0, 5.0, 15], mindspore.float16)
>>> range_op = Tensor([0.0, 5.0], mindspore.float16)
>>> hist = ops.HistogramFixedWidth(5)
>>> output = hist(x, range_op)
>>> print(output)
[2 1 1 0 2]
class tinyms.primitives.HistogramSummary(*args, **kwargs)[源代码]

Outputs the tensor to protocol buffer through histogram summary operator.

Inputs:
  • name (str) - The name of the input variable.

  • value (Tensor) - The value of tensor, and the rank of tensor must be greater than 0.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class SummaryDemo(nn.Cell):
...     def __init__(self,):
...         super(SummaryDemo, self).__init__()
...         self.summary = ops.HistogramSummary()
...         self.add = ops.Add()
...
...     def construct(self, x, y):
...         x = self.add(x, y)
...         name = "x"
...         self.summary(name, x)
...         return x
...
class tinyms.primitives.HookBackward(hook_fn, cell_id='')[源代码]

This operation is used as a tag to hook gradient in intermediate variables. Note that this function is only supported in Pynative Mode.

注解

The hook function must be defined like hook_fn(grad) -> Tensor or None, where grad is the gradient passed to the primitive and gradient may be modified and passed to next primitive. The difference between a hook function and callback of InsertGradientOf is that a hook function is executed in the python environment while callback will be parsed and added to the graph.

参数

hook_fn (Function) – Python function. hook function.

Inputs:
  • inputs (Tensor) - The variable to hook.

引发
  • TypeError – If inputs are not a Tensor.

  • TypeError – If hook_fn is not a function of python.

实际案例

>>> def hook_fn(grad_out):
...     print(grad_out)
...
>>> grad_all = GradOperation(get_all=True)
>>> hook = ops.HookBackward(hook_fn)
>>> def hook_test(x, y):
...     z = x * y
...     z = hook(z)
...     z = z * y
...     return z
...
>>> def backward(x, y):
...     return grad_all(hook_test)(x, y)
...
>>> output = backward(1, 2)
>>> print(output)
class tinyms.primitives.IFFT3D(*args, **kwargs)[源代码]

Inverse FFT with Three-Dimensional Input.

警告

This is an experimental prototype that is subject to change and/or deletion.

Inputs:
  • input_tensor (Tensor) - Three dimensional input tensor, supported data type is complex64.

Outputs:
  • output_tensor (Tensor) - Returns the tensor after undergoing inverse Fourier transform, the data type is float32.

Supported Platforms:

GPU

class tinyms.primitives.IOU(*args, **kwargs)[源代码]

Calculates intersection over union for boxes.

Computes the intersection over union (IOU) or the intersection over foreground (IOF) based on the ground-truth and predicted regions.

\[ \begin{align}\begin{aligned}\text{IOU} = \frac{\text{Area of Overlap}}{\text{Area of Union}}\\\text{IOF} = \frac{\text{Area of Overlap}}{\text{Area of Ground Truth}}\end{aligned}\end{align} \]

警告

In Ascend, only computation of float16 data is supported. To avoid overflow, the input length and width are scaled by 0.2 internally.

参数

mode (string) – The mode is used to specify the calculation method, now supporting ‘iou’ (intersection over union) or ‘iof’ (intersection over foreground) mode. Default: ‘iou’.

Inputs:
  • anchor_boxes (Tensor) - Anchor boxes, tensor of shape (N, 4). “N” indicates the number of anchor boxes, and the value “4” refers to “x0”, “y0”, “x1”, and “y1”. Data type must be float16 or float32.

  • gt_boxes (Tensor) - Ground truth boxes, tensor of shape (M, 4). “M” indicates the number of ground truth boxes, and the value “4” refers to “x0”, “y0”, “x1”, and “y1”. Data type must be float16 or float32.

Outputs:

Tensor, the ‘iou’ values, tensor of shape (M, N), with the same data type as anchor_boxes.

引发

KeyError – When mode is not ‘iou’ or ‘iof’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> iou = ops.IOU()
>>> anchor_boxes = Tensor(np.random.randint(1.0, 5.0, [3, 4]), mindspore.float16)
>>> gt_boxes = Tensor(np.random.randint(1.0, 5.0, [3, 4]), mindspore.float16)
>>> output = iou(anchor_boxes, gt_boxes)
>>> print(output.shape)
(3, 3)
class tinyms.primitives.Identity(*args, **kwargs)[源代码]

Returns a Tensor with the same shape and contents as input.

Inputs:
  • x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The data type is Number.

Outputs:

Tensor, the shape of tensor and the data type are the same as input_x, \((x_1, x_2, ..., x_R)\).

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> x = Tensor(np.array([1, 2, 3, 4]), mindspore.int64)
>>> output = ops.Identity()(x)
>>> print(output)
[1 2 3 4]
class tinyms.primitives.Imag(*args, **kwargs)[源代码]

Returns a new tensor containing imaginary value of the input.

Inputs:
  • input (Tensor, complex) - The input tensor. types: complex64, complex128.

Outputs:

Tensor, has the float type.

引发

TypeError – If the dtype of input is not one of: complex64, complex128.

Supported Platforms:

GPU

实际案例

>>> x = Tensor(np.asarray(np.complex(1.3+0.4j)), mindspore.complex64)
>>> conj = ops.Imag()
>>> output = conj(x)
>>> print(output)
0.4
class tinyms.primitives.ImageSummary(*args, **kwargs)[源代码]

Outputs the image tensor to protocol buffer through image summary operator.

Inputs:
  • name (str) - The name of the input variable, it must not be an empty string.

  • value (Tensor) - The value of image, the rank of tensor must be 4.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.summary = ops.ImageSummary()
...
...     def construct(self, x):
...         name = "image"
...         out = self.summary(name, x)
...         return out
...
class tinyms.primitives.InTopK(*args, **kwargs)[源代码]

Determines whether the targets are in the top k predictions.

参数

k (int) – Specifies the number of top elements to be used for computing precision.

Inputs:
  • x1 (Tensor) - A 2D Tensor defines the predictions of a batch of samples with float16 or float32 data type.

  • x2 (Tensor) - A 1D Tensor defines the labels of a batch of samples with int32 data type. The size of x2 must be equal to x1’s first dimension. The values of x2 can not be negative and must be equal to or less than index of x1’s second dimension.

Outputs:

Tensor has 1 dimension of type bool and the same shape with x2. For labeling sample i in x2, if the label in the first k predictions for sample i is in x1, then the value is True, otherwise False.

引发
  • TypeError – If k is not an int.

  • TypeError – If x1 or x2 is not a Tensor.

  • TypeError – If dtype of x1 is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> x1 = Tensor(np.array([[1, 8, 5, 2, 7], [4, 9, 1, 3, 5]]), mindspore.float32)
>>> x2 = Tensor(np.array([1, 3]), mindspore.int32)
>>> in_top_k = ops.InTopK(3)
>>> output = in_top_k(x1, x2)
>>> print(output)
[ True  False]
class tinyms.primitives.IndexAdd(*args, **kwargs)[源代码]

Adds tensor y to specified axis and indices of tensor x. The axis should be in the range from 0 to len(x.dim) - 1, and indices should be in the range from 0 to the size of x at the axis dimension.

参数

axis (int) – The dimension along which to index.

Inputs:
  • x (Parameter) - The input tensor to add to.

  • indices (Tensor) - The index of x on the axis th dimension to add to, with data type int32. The indices must be 1D with the same size as the size of the axis th dimension of y. The values of indices should be in the range of 0 to the size of the axis th dimension of x.

  • y (Tensor) - The input tensor with the value to add. Must have same data type as x. The shape must be the same as x except the axis th dimension.

Outputs:

Tensor, has the same shape and dtype as x.

引发
  • TypeError – If x is not a Tensor.

  • TypeError – If neither indices nor y is a Tensor.

  • ValueError – If axis is out of x rank’s range.

  • ValueError – If x rank is not the same as y rank.

  • ValueError – If size of indices is not equal to dimension of y[axis].

  • ValueError – If y’s shape is not the same as x except the axis th dimension.

Supported Platforms:

Ascend GPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.index_add = ops.IndexAdd(axis=1)
...         self.x = Parameter(Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32))
...         self.indices = Tensor(np.array([0, 2]), mindspore.int32)
...
...     def construct(self, y):
...         return self.index_add(self.x, self.indices, y)
...
>>> y = Tensor(np.array([[0.5, 1.0], [1.0, 1.5], [2.0, 2.5]]), mindspore.float32)
>>> net = Net()
>>> output = net(y)
>>> print(output)
[[ 1.5  2.   4. ]
 [ 5.   5.   7.5]
 [ 9.   8.  11.5]]
class tinyms.primitives.InplaceAdd(*args, **kwargs)[源代码]

Adds v into specified rows of x. Computes y = x; y[i,] += v.

参数

indices (Union[int, tuple]) – Indices into the left-most dimension of x, and determines which rows of x to add with v. It is an integer or a tuple, whose value is in [0, the first dimension size of x).

Inputs:
  • x (Tensor) - The first input is a tensor whose data type is float16, float32 or int32. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • input_v (Tensor) - The second input is a tensor that has the same dimension sizes as x except the first dimension, which must be the same as indices’s size. It has the same data type with x.

Outputs:

Tensor, has the same shape and dtype as x.

引发
  • TypeError – If indices is neither int nor tuple.

  • TypeError – If indices is a tuple whose elements are not all int.

  • ValueError – If length of shape of x is not equal to length of shape of input_v.

Supported Platforms:

Ascend

实际案例

>>> indices = (0, 1)
>>> x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32)
>>> input_v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32)
>>> inplaceAdd = ops.InplaceAdd(indices)
>>> output = inplaceAdd(x, input_v)
>>> print(output)
[[1.5 3. ]
 [4.  5.5]
 [5.  6. ]]
class tinyms.primitives.InplaceSub(*args, **kwargs)[源代码]

Subtracts v into specified rows of x. Computes y = x; y[i, :] -= v.

参数

indices (Union[int, tuple]) – Indices into the left-most dimension of x, and determines which rows of x to subtract with v. It is a int or tuple, whose value is in [0, the first dimension size of x).

Inputs:
  • x (Tensor) - The first input is a tensor whose data type is float16, float32 or int32. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • input_v (Tensor) - The second input is a tensor who has the same dimension sizes as x except the first dimension, which must be the same as indices’s size. It has the same data type with x.

Outputs:

Tensor, has the same shape and dtype as x.

引发
  • TypeError – If indices is neither int nor tuple.

  • TypeError – If indices is a tuple whose elements are not all int.

  • ValueError – If length of shape of x is not equal to length of shape of input_v.

Supported Platforms:

Ascend

实际案例

>>> indices = (0, 1)
>>> x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32)
>>> input_v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32)
>>> inplaceSub = ops.InplaceSub(indices)
>>> output = inplaceSub(x, input_v)
>>> print(output)
[[0.5 1. ]
 [2.  2.5]
 [5.  6. ]]
class tinyms.primitives.InplaceUpdate(*args, **kwargs)[源代码]

Updates specified rows with values in v.

参数

indices (Union[int, tuple]) – Indices into the left-most dimension of x, and determines which rows of x to update with v. It is a int or tuple, whose value is in [0, the first dimension size of x).

Inputs:
  • x (Tensor) - A tensor which to be inplace updated. It can be one of the following data types: float32, float16 and int32.

  • v (Tensor) - A tensor with the same type as x and the same dimension size as x except the first dimension, which must be the same as the size of indices.

Outputs:

Tensor, with the same type and shape as the input x.

引发
  • TypeError – If indices is neither int nor tuple.

  • TypeError – If indices is a tuple and its element is not an int.

Supported Platforms:

Ascend

实际案例

>>> indices = (0, 1)
>>> x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32)
>>> v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32)
>>> inplace_update = ops.InplaceUpdate(indices)
>>> output = inplace_update(x, v)
>>> print(output)
[[0.5 1. ]
 [1.  1.5]
 [5.  6. ]]
class tinyms.primitives.InsertGradientOf(*args, **kwargs)[源代码]

Attaches callback to the graph node that will be invoked on the node’s gradient.

参数

f (Function) – MindSpore’s Function. Callback function.

Inputs:
  • input_x (Any) - The graph node to attach to.

Outputs:

Tensor, returns input_x directly. InsertGradientOf does not affect the forward result.

引发

TypeError – If f is not a function of mindspore.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> def clip_gradient(dx):
...     ret = dx
...     if ret > 1.0:
...         ret = 1.0
...
...     if ret < 0.2:
...         ret = 0.2
...
...     return ret
...
>>> clip = ops.InsertGradientOf(clip_gradient)
>>> grad_all = ops.GradOperation(get_all=True)
>>> def InsertGradientOfClipDemo():
...     def clip_test(x, y):
...         x = clip(x)
...         y = clip(y)
...         c = x * y
...         return c
...
...     @ms_function
...     def f(x, y):
...         return clip_test(x, y)
...
...     def fd(x, y):
...         return grad_all(clip_test)(x, y)
...
...     print("forward: ", f(1.1, 0.1))
...     print("clip_gradient:", fd(1.1, 0.1))
...
class tinyms.primitives.Inv(*args, **kwargs)[源代码]

Computes Inv(Reciprocal) of input tensor element-wise.

\[out_i = out_i = \frac{1}{x_{i} }\]
Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions. Must be one of the following types: float16, float32, int32.

Outputs:

Tensor, has the same shape and data type as x.

引发

TypeError – If dtype of x is not one of float16, float32, int32.

Supported Platforms:

Ascend

实际案例

>>> inv = ops.Inv()
>>> x = Tensor(np.array([0.25, 0.4, 0.31, 0.52]), mindspore.float32)
>>> output = inv(x)
>>> print(output)
[4.        2.5       3.2258065 1.923077 ]
class tinyms.primitives.Invert(*args, **kwargs)[源代码]

Flips all bits of input tensor element-wise.

\[out_i = -x_{i}\]
Inputs:
  • x (Tensor[int16], Tensor[uint16]) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If dtype of x is neither int16 nor uint16.

Supported Platforms:

Ascend

实际案例

>>> invert = ops.Invert()
>>> x = Tensor(np.array([25, 4, 13, 9]), mindspore.int16)
>>> output = invert(x)
>>> print(output)
[-26 -5 -14 -10]
class tinyms.primitives.InvertPermutation(*args, **kwargs)[源代码]

Computes the inverse of an index permutation.

This operator is mainly used to calculate the inverse of index permutation. It requires a 1-dimensional integer tensor x, which represents the index of a zero-based array, and exchanges each value with its index position. In other words, For output tensor y and input tensor x, this operation calculates the following values:

\(y[x[i]] = i, \quad i \in [0, 1, \ldots, \text{len}(x)-1]\).

注解

These values must include 0. There must be no duplicate values and the values can not be negative.

Inputs:
  • input_x (Union(tuple[int], list[int]) - The input is constructed by multiple integers, i.e., \((y_1, y_2, ..., y_S)\) representing the indices. The values must include 0. There can be no duplicate values or negative values. Only constant value is allowed. The maximum value must be equal to length of input_x.

Outputs:

tuple[int]. It has the same length as the input.

引发
  • TypeError – If input_x is neither tuple nor list.

  • TypeError – If element of input_x is not an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> invert = ops.InvertPermutation()
>>> input_data = (3, 4, 0, 2, 1)
>>> output = invert(input_data)
>>> print(output)
(2, 4, 3, 0, 1)
class tinyms.primitives.IsFinite(*args, **kwargs)[源代码]

Determines which elements are finite for each position.

\[\begin{split}out_i = \begin{cases} & \text{ if } x_{i} = \text{Finite},\ \ True\ \\ & \text{ if } x_{i} \ne \text{Finite},\ \ False \end{cases}\end{split}\]
Inputs:
  • x (Tensor) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape of input, and the dtype is bool.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> is_finite = ops.IsFinite()
>>> x = Tensor(np.array([np.log(-1), 1, np.log(0)]), mindspore.float32)
>>> output = is_finite(x)
>>> print(output)
[False  True False]
class tinyms.primitives.IsInf(*args, **kwargs)[源代码]

Determines which elements are inf or -inf for each position

\[\begin{split}out_i = \begin{cases} & \text{ if } x_{i} = \text{Inf},\ \ True \\ & \text{ if } x_{i} \ne \text{Inf},\ \ False \end{cases}\end{split}\]

where \(Inf\) means not a number.

Inputs:
  • x (Tensor) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape of input, and the dtype is bool.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

GPU

实际案例

>>> is_inf = ops.IsInf()
>>> x = Tensor(np.array([np.log(-1), 1, np.log(0)]), mindspore.float32)
>>> output = is_inf(x)
>>> print(output)
[False False True]
class tinyms.primitives.IsInstance(*args, **kwargs)[源代码]

Checks whether an object is an instance of a target type.

Inputs:
  • inst (Any Object) - The instance to be checked. Only constant value is allowed.

  • type_ (mindspore.dtype) - The target type. Only constant value is allowed.

Outputs:

bool, the check result.

引发

TypeError – If type_ is not a Type.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> inst = 1
>>> output = ops.IsInstance()(inst, mindspore.int32)
>>> print(output)
False
class tinyms.primitives.IsNan(*args, **kwargs)[源代码]

Determines which elements are NaN for each position.

\[\begin{split}out_i = \begin{cases} & \text{ if } x_{i} = \text{Nan},\ \ True \\ & \text{ if } x_{i} \ne \text{Nan},\ \ False \end{cases}\end{split}\]

where \(Nan\) means not a number.

Inputs:
  • x (Tensor) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape of input, and the dtype is bool.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

GPU CPU

实际案例

>>> is_nan = ops.IsNan()
>>> x = Tensor(np.array([np.log(-1), 1, np.log(0)]), mindspore.float32)
>>> output = is_nan(x)
>>> print(output)
[True False False]
class tinyms.primitives.IsSubClass(*args, **kwargs)[源代码]

Checks whether this type is a sub-class of another type.

Inputs:
  • sub_type (mindspore.dtype) - The type to be checked. Only constant value is allowed.

  • type_ (mindspore.dtype) - The target type. Only constant value is allowed.

Outputs:

bool, the check result.

引发

TypeError – If sub_type or type_ is not a Type.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> output = ops.IsSubClass()(mindspore.int32,  mindspore.intc)
>>> print(output)
True
class tinyms.primitives.KLDivLoss(*args, **kwargs)[源代码]

Computes the Kullback-Leibler divergence between the logits and the labels.

The updating formulas of KLDivLoss algorithm are as follows,

\[L = \{l_1,\dots,l_N\}^\top, \quad l_n = y_n \cdot (\log y_n - x_n)\]

Then,

\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]

where \(x\) represents logits. \(y\) represents labels. \(\ell(x, y)\) represents output.

参数

reduction (str) – Specifies the reduction to be applied to the output. Its value must be one of ‘none’, ‘mean’, ‘sum’. Default: ‘mean’.

Inputs:
  • logits (Tensor) - The input Tensor. The data type must be float32.

  • labels (Tensor) - The label Tensor which has the same shape and data type as logits.

Outputs:

Tensor or Scalar, if reduction is ‘none’, then output is a tensor and has the same shape as logits. Otherwise it is a scalar.

引发
  • TypeError – If reduction is not a str.

  • TypeError – If neither logits nor labels is a Tensor.

  • TypeError – If dtype of logits or labels is not float32.

Supported Platforms:

GPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.kldiv_loss = ops.KLDivLoss()
...     def construct(self, logits, labels):
...         result = self.kldiv_loss(logits, labels)
...         return result
...
>>> net = Net()
>>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
>>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32)
>>> output = net(logits, labels)
>>> print(output)
-0.23333333
class tinyms.primitives.L2Loss(*args, **kwargs)[源代码]

Calculates half of the L2 norm of a tensor without using the sqrt.

Set input_x as x and output as loss.

\[loss = sum(x ** 2) / 2\]
Inputs:
  • input_x (Tensor) - A input Tensor. Data type must be float16 or float32.

Outputs:

Tensor, has the same dtype as input_x. The output tensor is the value of loss which is a scalar tensor.

引发
  • TypeError – If input_x not a Tensor.

  • TypeError – If dtype of input_x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

Examples
>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.float16)
>>> l2_loss = ops.L2Loss()
>>> output = l2_loss(input_x)
>>> print(output)
7.0
class tinyms.primitives.L2Normalize(*args, **kwargs)[源代码]

L2 Normalization Operator.

This operator will normalize the input using the given axis. The function is shown as follows:

\[\text{output} = \frac{x}{\sqrt{\text{max}(\text{sum} (\text{x}^2), \epsilon)}},\]

where \(\epsilon\) is epsilon.

参数
  • axis (Union[list(int), tuple(int), int]) – The starting axis for the input to apply the L2 Normalization. Default: 0.

  • epsilon (float) – A small value added for numerical stability. Default: 1e-4.

Inputs:
  • x (Tensor) - Input to compute the normalization. Tensor of shape \((N, \ldots)\). Data type must be float16 or float32.

Outputs:

Tensor, with the same type and shape as the x.

引发
  • TypeError – If axis is not one of the following: list, tuple or int.

  • TypeError – If epsilon is not a float.

  • TypeError – If x is not a Tensor.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> l2_normalize = ops.L2Normalize()
>>> x = Tensor(np.random.randint(-256, 256, (2, 3, 4)), mindspore.float32)
>>> output = l2_normalize(x)
>>> print(output.shape)
(2, 3, 4)
class tinyms.primitives.LARSUpdate(*args, **kwargs)[源代码]

Conducts LARS (layer-wise adaptive rate scaling) update on the sum of squares of gradient.

For more details, please refer to nn.LARS.

参数
  • epsilon (float) – Term added to the denominator to improve numerical stability. Default: 1e-05.

  • hyperpara (float) – Trust coefficient for calculating the local learning rate. Default: 0.001.

  • use_clip (bool) – Whether to use clip operation for calculating the local learning rate. Default: False.

Inputs:
  • weight (Tensor) - A tensor, representing the weight. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • gradient (Tensor) - The gradient of weight, which has the same shape and dtype with weight.

  • norm_weight (Tensor) - A scalar tensor, representing the sum of squares of weight.

  • norm_gradient (Tensor) - A scalar tensor, representing the sum of squares of gradient.

  • weight_decay (Union[Number, Tensor]) - Weight decay. It must be a scalar tensor or number.

  • learning_rate (Union[Number, Tensor]) - Learning rate. It must be a scalar tensor or number.

Outputs:

Tensor, represents the new gradient.

引发
  • TypeError – If neither epsilon nor hyperpara is a float.

  • TypeError – If use_clip is a bool.

  • TypeError – If weight, gradient, norm_weight or norm_gradient is not a Tensor.

  • TypeError – If weight_decay or learning_rate is neither a Number nor a Tensor.

  • TypeError – If shape of gradient is not same as weight.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.lars = ops.LARSUpdate()
...         self.reduce = ops.ReduceSum()
...         self.square = ops.Square()
...     def construct(self, weight, gradient):
...         w_square_sum = self.reduce(self.square(weight))
...         grad_square_sum = self.reduce(self.square(gradient))
...         grad_t = self.lars(weight, gradient, w_square_sum, grad_square_sum, 0.0, 1.0)
...         return grad_t
...
>>> weight = Tensor(np.array([[0.5, 0.8, 0.2], [0.6, 0.4, 0.2]]).astype(np.float32))
>>> gradient = Tensor(np.array([[0.4, 0.4, 0.5], [0.2, 0.4, 0.3]]).astype(np.float32))
>>> net = Net()
>>> output = net(Tensor(weight), Tensor(gradient))
>>> print(output)
[[0.0005265  0.0005265 0.00065813]
 [0.00026325 0.0005265 0.00039488]]
class tinyms.primitives.LJEnergy(*args, **kwargs)[源代码]

Calculate the Van der Waals interaction energy described by Lennard-Jones potential for each atom. Assume the number of atoms is n, and the number of Lennard-Jones types for all atoms is P, which means there will be q = P*(P+1)/2 types of possible Lennard-Jones interactions for all kinds of atom pairs.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr = (x_a-x_b, y_a-y_b, z_a-z_b)\]
\[E = A/|dr|^{12} - B/|dr|^{6}\]
参数
  • atom_numbers (int32) – the number of atoms, n.

  • cutoff_square (float32) – the square value of cutoff.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom.

    The data type is uint32 and the shape is \((n, 3)\)

  • LJtype (Tensor) - The Lennard-Jones type of each atom.

    The data type is int32 and the shape is \((n,)\)

  • charge (Tensor) - The charge carried by each atom.

    The data type is float32 and the shape is \((n,)\)

  • scaler (Tensor) - The scale factor between real space coordinate and its unsigned int value. The data type is float32 and the shape is \((3,)\)

  • nl_numbers - (Tensor) - The each atom. The data type is int32 and the shape is \((n,)\)

  • nl_serial - (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, 800)\).

  • d_LJ_A (Tensor) - The Lennard-Jones A coefficient of each kind of atom pair. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • d_LJ_B (Tensor) - The Lennard-Jones B coefficient of each kind of atom pair. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

Outputs:
  • d_LJ_energy_atom (Tensor) - The Lennard-Jones potential energy of each atom.

    The data type is float32 and the shape is \((n,)\).

  • d_LJ_energy_sum (Scalar), the sum of Lennard-Jones potential energy of each atom. The data type is float32.

Supported Platforms:

GPU

class tinyms.primitives.LJForce(*args, **kwargs)[源代码]

Calculate the Van der Waals interaction force described by Lennard-Jones potential energy for each atom.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[dr = (x_a-x_b, y_a-y_b, z_a-z_b)\]
\[F = (-12*A/|dr|^{14} + 6*B/|dr|^{8}) * dr\]
参数
  • atom_numbers (int32) – the number of atoms, n.

  • cutoff_square (float32) – the square value of cutoff.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\)

  • LJtype (Tensor) - The Lennard-Jones type of each atom.

    The data type is int32 and the shape is \((n,)\)

  • charge (Tensor) - The charge carried by each atom.

    The data type is float32 and the shape is \((n,)\)

  • scaler (Tensor) - The scale factor between real space coordinates and its unsigned int value. The data type is float32 and the shape is \((3,)\)

  • nl_numbers - (Tensor) - The each atom. The data type is int32 and the shape is \((n,)\)

  • nl_serial - (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, 800)\).

  • d_LJ_A (Tensor) - The Lennard-Jones A coefficient of each kind of atom pair. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • d_LJ_B (Tensor) - The Lennard-Jones B coefficient of each kind of atom pair. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

outputs:
  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.LJForceWithPMEDirectForce(*args, **kwargs)[源代码]

Calculate the Lennard-Jones force and PME direct force together.

The calculation formula of Lennard-Jones part is the same as operator LJForce(), and the PME direct part is within PME method.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • cutoff_square (float32) – the square value of cutoff.

  • pme_beta (float32) – PME beta parameter, same as operator PMEReciprocalForce().

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJtype (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\).

  • scaler (Tensor) - The scale factor between real space coordinate and its unsigned int value. The data type is float32 and the shape is \((3,)\).

  • nl_numbers - (Tensor) - The each atom. The data type is int32 and the shape is \((n,)\).

  • nl_serial - (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, 800)\).

  • d_LJ_A (Tensor) - The Lennard-Jones A coefficient of each kind of atom pair. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

  • d_LJ_B (Tensor) - The Lennard-Jones B coefficient of each kind of atom pair. q is the number of atom pair. The data type is float32 and the shape is \((q,)\).

Outputs:
  • frc (Tensor), The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.LJForceWithPMEDirectForceUpdate(*args, **kwargs)[源代码]

Calculate the Lennard-Jones force and PME direct force together for pressure.

The calculation formula of Lennard-Jones part is the same as operator LJForce(), and the PME direct part is within PME method.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • cutoff (float32) – the square value of cutoff.

  • pme_beta (float32) – PME beta parameter, same as operator PMEReciprocalForce().

  • need_update (int32) – if need_update = 1, calculate the pressure, default 0.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJtype (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\).

  • scaler (Tensor) - The scale factor between real space coordinate and its unsigned int value. The data type is float32 and the shape is \((3,)\).

  • nl_numbers (Tensor) - The each atom. The data type is int32 and the shape is \((n,)\).

  • nl_serial (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, 800)\).

  • d_LJ_A (Tensor) - The Lennard-Jones A coefficient of each kind of atom pair. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

  • d_LJ_B (Tensor) - The Lennard-Jones B coefficient of each kind of atom pair. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

  • beta (Tensor) - PME beta parameter. The data type is float32 and the shape is \((1,)\).

Outputs:
  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.LJForceWithVirialEnergy(*args, **kwargs)[源代码]

Calculate the Lennard-Jones force, virial and atom energy together.

The calculation formula of Lennard-Jones part is the same as operator LJForce(), and the PME direct part is within PME method.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • cutoff (float32) – the square value of cutoff.

  • pme_beta (float32) – PME beta parameter, same as operator PMEReciprocalForce().

  • max_neighbor_numbers (int32) – the max neighbor numbers, default 800.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJtype (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\).

  • scaler (Tensor) - The scale factor between real space coordinate and its unsigned int value. The data type is float32 and the shape is \((3,)\).

  • nl_numbers (Tensor) - The each atom. The data type is int32 and the shape is \((n,)\).

  • nl_serial (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, 800)\).

  • d_LJ_A (Tensor) - The Lennard-Jones A coefficient of each kind of atom pair. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

  • d_LJ_B (Tensor) - The Lennard-Jones B coefficient of each kind of atom pair. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

Outputs:
  • frc (Tensor), The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • virial (Tensor), The virial felt by each atom. The data type is float32 and the shape is \((n,)\).

  • atom_energy (Tensor), The atom energy felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.LJForceWithVirialEnergyUpdate(*args, **kwargs)[源代码]

Calculate the Lennard-Jones force and PME direct force together for pressure.

The calculation formula of Lennard-Jones part is the same as operator LJForce(), and the PME direct part is within PME method.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • cutoff (float32) – the square value of cutoff.

  • pme_beta (float32) – PME beta parameter, same as operator PMEReciprocalForce().

  • max_neighbor_numbers (int32) – the max neighbor numbers, default 800.

  • need_update (int32) – if need_update = 1, calculate the pressure, default 0.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • LJtype (Tensor) - The Lennard-Jones type of each atom. The data type is int32 and the shape is \((n,)\).

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\).

  • scaler (Tensor) - The scale factor. The data type is float32 and the shape is \((3,)\).

  • nl_numbers (Tensor) - The each atom. The data type is int32 and the shape is \((n,)\).

  • nl_serial (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, 800)\).

  • d_LJ_A (Tensor) - The Lennard-Jones A coefficient of each kind of atom pair. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

  • d_LJ_B (Tensor) - The Lennard-Jones B coefficient of each kind of atom pair. The number of atom pair is q. The data type is float32 and the shape is \((q,)\).

  • beta (Tensor) - The PME beta parameter to be updated in pressure calculation. The data type is float32 and the shape is \((1,)\)

Outputs:
  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • virial (Tensor) - The accumulated potential virial for each atom. The data type is float32 and the shape is \((n, )\).

  • atom_energy (Tensor) - The accumulated potential energy for each atom. The data type is float32 and the shape is \((n, )\).

Supported Platforms:

GPU

class tinyms.primitives.LRN(*args, **kwargs)[源代码]

Local Response Normalization.

\[b_{c} = a_{c}\left(k + \frac{\alpha}{n} \sum_{c'=\max(0, c-n/2)}^{\min(N-1,c+n/2)}a_{c'}^2\right)^{-\beta}\]

where the \(a_{c}\) indicates the represents the specific value of the pixel corresponding to c in feature map; where the \(n/2\) indicate the depth_radius; where the \(k\) indicate the bias; where the \(\alpha\) indicate the`alpha`; where the \(\beta\) indicate the beta.

参数
  • depth_radius (int) – Half-width of the 1-D normalization window with the shape of 0-D. Default: 5.

  • bias (float) – An offset (usually positive to avoid dividing by 0). Default: 1.0.

  • alpha (float) – A scale factor, usually positive. Default: 1.0.

  • beta (float) – An exponent. Default: 0.5.

  • norm_region (str) – Specifies normalization region. Options: “ACROSS_CHANNELS”. Default: “ACROSS_CHANNELS”.

Inputs:
  • x (Tensor) - A 4D Tensor with float16 or float32 data type.

Outputs:

Tensor, with the same shape and data type as x.

引发
Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([[[[0.1], [0.2]],
...                       [[0.3], [0.4]]]]), mindspore.float32)
>>> lrn = ops.LRN()
>>> output = lrn(x)
>>> print(output)
[[[[0.09534626]
   [0.1825742 ]]
  [[0.2860388 ]
   [0.3651484 ]]]]
class tinyms.primitives.LSTM(*args, **kwargs)[源代码]

Performs the Long Short-Term Memory (LSTM) on the input.

For detailed information, please refer to nn.LSTM.

参数
  • input_size (int) – Number of features of input.

  • hidden_size (int) – Number of features of hidden layer.

  • num_layers (int) – Number of layers of stacked LSTM.

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh.

  • bidirectional (bool) – Specifies whether it is a bidirectional LSTM.

  • dropout (float) – If not 0, append Dropout layer on the outputs of each LSTM layer except the last layer. The range of dropout is [0.0, 1.0].

Inputs:
  • input (Tensor) - Tensor of shape (seq_len, batch_size, input_size) or (batch_size, seq_len, input_size).

  • h (tuple) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

  • c (tuple) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

Outputs:

Tuple, a tuple contains (output, h_n, c_n, reserve, state).

  • output (Tensor) - Tensor of shape (seq_len, batch_size, num_directions * hidden_size).

  • h_n (Tensor) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

  • c_n (Tensor) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

  • reserve (Tensor) - Tensor of shape (r, 1).

  • state (Tensor) - Random number generator state and its shape is (s, 1).

引发
  • TypeError – If input_size, hidden_size or num_layers is not an int.

  • TypeError – If has_bias or bidirectional is not a bool.

  • TypeError – If dropout is not a float.

  • ValueError – If dropout is not in range [0.0, 1.0].

Supported Platforms:

GPU CPU

实际案例

>>> input_size = 10
>>> hidden_size = 2
>>> num_layers = 1
>>> seq_len = 5
>>> batch_size = 2
>>>
>>> net = ops.LSTM(input_size, hidden_size, num_layers, True, False, 0.0)
>>> input_tensor = Tensor(np.ones([seq_len, batch_size, input_size]).astype(np.float32))
>>> h0 = Tensor(np.ones([num_layers, batch_size, hidden_size]).astype(np.float32))
>>> c0 = Tensor(np.ones([num_layers, batch_size, hidden_size]).astype(np.float32))
>>> w = Tensor(np.ones([112, 1, 1]).astype(np.float32))
>>> output, hn, cn, _, _ = net(input_tensor, h0, c0, w)
>>> print(output)
[[[0.9640267  0.9640267 ]
  [0.9640267  0.9640267 ]]
 [[0.9950539  0.9950539 ]
  [0.9950539  0.9950539 ]]
 [[0.99932843 0.99932843]
  [0.99932843 0.99932843]]
 [[0.9999084  0.9999084 ]
  [0.9999084  0.9999084 ]]
 [[0.9999869  0.9999869 ]
  [0.9999869  0.9999869 ]]]
class tinyms.primitives.LastCrdToDr(*args, **kwargs)[源代码]

Calculate the displacement vector of each constrained atom pair.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • constrain_pair_numbers (int32) – the number of constrain pairs m.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • quarter_cof (Tensor) - The 3-D scale factor. The data type is float32 and the shape is \((3,)\).

  • uint_dr_to_dr (Tensor) - The 3-D scale factor (x, y, z) The data type is int32 and the shape is \((3,)\)..

  • atom_i_serials (Tensor) - The first atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • atom_j_serials (Tensor) - The second atom index of each constrained atom pair. The data type is int32 and the shape is \((m,)\).

  • constant_rs (Tensor) - The constrained distance of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

  • constrain_ks (Tensor) - The coefficient of each constrained atom pair. The data type is float32 and the shape is \((m,)\).

Outputs:
  • pair_dr (Tensor) - The displacement vector of each constrained atom pair. The data type is float32 and the shape is \((m, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.LayerNorm(*args, **kwargs)[源代码]

Applies the Layer Normalization to the input tensor.

This operator will normalize the input tensor on given axis. LayerNorm is described in the paper Layer Normalization.

\[y = \frac{x - mean}{\sqrt{variance + \epsilon}} * \gamma + \beta\]

where \(\gamma\) is scale, \(\beta\) is bias, \(\epsilon\) is epsilon.

参数
  • begin_norm_axis (int) – The begin axis of the input_x to apply LayerNorm, the value must be in [-1, rank(input)). Default: 1.

  • begin_params_axis (int) – The begin axis of the parameter input (gamma, beta) to apply LayerNorm, the value must be in [-1, rank(input)). Default: 1.

  • epsilon (float) – A value added to the denominator for numerical stability. Default: 1e-7.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, \ldots)\). The input of LayerNorm.

  • gamma (Tensor) - Tensor of shape \((P_0, \ldots, P_\text{begin_params_axis})\). The learnable parameter gamma as the scale on norm.

  • beta (Tensor) - Tensor of shape \((P_0, \ldots, P_\text{begin_params_axis})\). The learnable parameter beta as the scale on norm.

Outputs:

tuple[Tensor], tuple of 3 tensors, the normalized input and the updated parameters.

  • output_x (Tensor) - The normalized input, has the same type and shape as the input_x. The shape is \((N, C)\).

  • mean (Tensor) - Tensor of shape \((C,)\).

  • variance (Tensor) - Tensor of shape \((C,)\).

引发
  • TypeError – If begin_norm_axis or begin_params_axis is not an int.

  • TypeError – If epsilon is not a float.

  • TypeError – If input_x, gamma or beta is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[1, 2, 3], [1, 2, 3]]), mindspore.float32)
>>> gamma = Tensor(np.ones([3]), mindspore.float32)
>>> beta = Tensor(np.ones([3]), mindspore.float32)
>>> layer_norm = ops.LayerNorm()
>>> output, mean, variance = layer_norm(input_x, gamma, beta)
>>> print(output)
[[-0.2247448  1.         2.2247448]
 [-0.2247448  1.         2.2247448]]
>>> print(mean)
[[2.]
 [2.]]
>>> print(variance)
[[0.6666667]
 [0.6666667]]
class tinyms.primitives.Lerp(*args, **kwargs)[源代码]

Does a linear interpolation of two tensors start and end based on a float or tensor weight.

If weight is a tensor, the shapes of three inputs need to be broadcast; If weight is a float, the shapes of start and end need to be broadcast.

\[output_{i} = start_{i} + weight_{i} * (end_{i} - start_{i})\]
Inputs:
  • start (Tensor) - The tensor with the starting points. Data type must be float16 or float32.

  • end (Tensor) - The tensor with the ending points. Data type must be float16 or float32.

  • weight (Union[float, Tensor]) – The weight for the interpolation formula. Must be a float or a scalar tensor with float16 or float32 data type.

Outputs:

Tensor, has the same type and shape as input start.

引发
  • TypeError – If start or end is not a tensor.

  • TypeError – If weight is neither float nor tensor.

  • TypeError – If dtype of start or end is neither float16 nor float32.

  • TypeError – If dtype of weight is neither float16 nor float32 when it is a tensor.

  • TypeError – If start and end have different data types.

  • TypeError – If start, end and weight have different data types when weight is a tensor.

  • ValueError – If end could not be broadcast to a tensor with shape of start.

  • ValueError – If weight could not be broadcast to tensors with shapes of start and end when it is a tensor.

Supported Platforms:

Ascend

实际案例

>>> start = Tensor(np.array([1., 2., 3., 4.]), mindspore.float32)
>>> end = Tensor(np.array([10., 10., 10., 10.]), mindspore.float32)
>>> lerp = ops.Lerp()
>>> output = lerp(start, end, 0.5)
>>> print(output)
[5.5 6. 6.5 7. ]
class tinyms.primitives.Less(*args, **kwargs)[源代码]

Computes the boolean value of \(x < y\) element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[\begin{split}out_{i} =\begin{cases} & \text{True, if } x_{i}<y_{i} \\ & \text{False, if } x_{i}>=y_{i} \end{cases}\end{split}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting,and the data type is bool.

引发

TypeError – If x and y is not one of the following: Tensor, Number, bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 1, 4]), mindspore.int32)
>>> less = ops.Less()
>>> output = less(x, y)
>>> print(output)
[False False True]
class tinyms.primitives.LessEqual(*args, **kwargs)[源代码]

Computes the boolean value of \(x <= y\) element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool , and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[\begin{split}out_{i} =\begin{cases} & \text{True, if } x_{i}<=y_{i} \\ & \text{False, if } x_{i}>y_{i} \end{cases}\end{split}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting,and the data type is bool.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 1, 4]), mindspore.int32)
>>> less_equal = ops.LessEqual()
>>> output = less_equal(x, y)
>>> print(output)
[ True False  True]
class tinyms.primitives.LinSpace(*args, **kwargs)[源代码]

The OP returns a Tensor whose value is num evenly spaced in the interval start and stop (including start and stop), and the length of the output Tensor is num.

\[\begin{split}\begin{aligned} &step = (stop - start)/(num - 1)\\ &output = [start, start+step, start+2*step, ... , stop] \end{aligned}\end{split}\]
Inputs:
  • start (Tensor[float32]) - Start value of interval, With shape of 0-D.

  • stop (Tensor[float32]) - Last value of interval, With shape of 0-D.

  • num (int) - Number of ticks in the interval, inclusive of start and stop.

Outputs:

Tensor, has the same shape as start.

Supported Platforms:

Ascend GPU

实际案例

>>> linspace = ops.LinSpace()
>>> start = Tensor(1, mindspore.float32)
>>> stop = Tensor(10, mindspore.float32)
>>> num = 5
>>> output = linspace(start, stop, num)
>>> print(output)
[ 1.    3.25  5.5   7.75 10.  ]
class tinyms.primitives.Log(*args, **kwargs)[源代码]

Returns the natural logarithm of a tensor element-wise.

\[y_i = log_e(x_i)\]

警告

If the input value of operator Log is within the range (0, 0.01] or [0.95, 1.05], the output accuracy is subject to change.

Inputs:
  • x (Tensor) - The input tensor. The value must be greater than 0. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
>>> log = ops.Log()
>>> output = log(x)
>>> print(output)
[0.        0.6931472 1.3862944]
class tinyms.primitives.Log1p(*args, **kwargs)[源代码]

Returns the natural logarithm of one plus the input tensor element-wise.

Inputs:
  • x (Tensor) - The input tensor. With float16 or float32 data type. The value must be greater than -1. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
>>> log1p = ops.Log1p()
>>> output = log1p(x)
>>> print(output)
[0.6931472 1.0986123 1.609438 ]
class tinyms.primitives.LogSoftmax(*args, **kwargs)[源代码]

Log Softmax activation function.

Applies the Log Softmax function to the input tensor on the specified axis. Supposes a slice in the given aixs, \(x\) for each element \(x_i\), the Log Softmax function is shown as follows:

\[\text{output}(x_i) = \log \left(\frac{\exp(x_i)} {\sum_{j = 0}^{N-1}\exp(x_j)}\right),\]

where \(N\) is the length of the Tensor.

参数

axis (int) – The axis to perform the Log softmax operation. Default: -1.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the logits.

引发
  • TypeError – If axis is not an int.

  • TypeError – If dtype of logits is neither float16 nor float32.

  • ValueError – If axis is not in range [-len(logits.shape), len(logits.shape)).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> logits = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32)
>>> log_softmax = ops.LogSoftmax()
>>> output = log_softmax(logits)
>>> print(output)
[-4.4519143 -3.4519143 -2.4519143 -1.4519144 -0.4519144]
class tinyms.primitives.LogUniformCandidateSampler(*args, **kwargs)[源代码]

Generates random labels with a log-uniform distribution for sampled_candidates.

Random sampling a tensor of sampled classes from the range of integers [0, range_max).

参数
  • num_true (int) – The number of target classes per training example. Default: 1.

  • num_sampled (int) – The number of classes to randomly sample. Default: 5.

  • unique (bool) – Determines whether sample with rejection. If unique is True, all sampled classes in a batch are unique. Default: True.

  • range_max (int) – The number of possible classes. When unique is True, range_max must be greater than or equal to num_sampled. Default: 5.

  • seed (int) – Random seed, must be non-negative. Default: 0.

Inputs:
  • true_classes (Tensor) - The target classes. With data type of int64 and shape [batch_size, num_true].

Outputs:

Tuple of 3 Tensors.

  • sampled_candidates (Tensor) - A Tensor with shape (num_sampled,) and the same type as true_classes.

  • true_expected_count (Tensor) - A Tensor with the same shape as true_classes and type float32.

  • sampled_expected_count (Tensor) - A Tensor with the same shape as sampled_candidates and type float32.

引发
  • TypeError – If neither num_true nor num_sampled is an int.

  • TypeError – If unique is not a bool.

  • TypeError – If neither range_max nor seed is an int.

  • TypeError – If true_classes is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> sampler = ops.LogUniformCandidateSampler(2, 5, True, 5)
>>> output1, output2, output3 = sampler(Tensor(np.array([[1, 7], [0, 4], [3, 3]])))
>>> print(output1, output2, output3)
[3 2 0 4 1]
[[0.92312991 0.49336370]
 [0.99248987 0.65806371]
 [0.73553443 0.73553443]]
[0.73553443 0.82625800 0.99248987 0.65806371 0.92312991]
class tinyms.primitives.LogicalAnd(*args, **kwargs)[源代码]

Computes the “logical AND” of two tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one bool. When the inputs are two tensors, the shapes of them could be broadcast, and the data types of them must be bool. When the inputs are one tensor and one bool, the bool object could only be a constant, and the data type of the tensor must be bool.

\[out_{i} = x_{i} \wedge y_{i}\]

注解

LogicalAnd supports broadcasting.

Inputs:
  • x (Union[Tensor, bool]) - The first input is a bool or a tensor whose data type is bool.

  • y (Union[Tensor, bool]) - The second input is a bool when the first input is a tensor or a tensor whose data type is bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is bool.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([True, False, True]), mindspore.bool_)
>>> y = Tensor(np.array([True, True, False]), mindspore.bool_)
>>> logical_and = ops.LogicalAnd()
>>> output = logical_and(x, y)
>>> print(output)
[ True False False]
class tinyms.primitives.LogicalNot(*args, **kwargs)[源代码]

Computes the “logical NOT” of a tensor element-wise.

\[out_{i} = \neg x_{i}\]
Inputs:
  • x (Tensor) - The input tensor whose dtype is bool. \((N,*)\) where \(*\) means,any number of additional dimensions.

Outputs:

Tensor, the shape is the same as the x, and the dtype is bool.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([True, False, True]), mindspore.bool_)
>>> logical_not = ops.LogicalNot()
>>> output = logical_not(x)
>>> print(output)
[False  True False]
class tinyms.primitives.LogicalOr(*args, **kwargs)[源代码]

Computes the “logical OR” of two tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one bool. When the inputs are two tensors, the shapes of them could be broadcast, and the data types of them must be bool. When the inputs are one tensor and one bool, the bool object could only be a constant, and the data type of the tensor must be bool.

\[out_{i} = x_{i} \vee y_{i}\]

注解

LogicalOr supports broadcasting.

Inputs:
  • x (Union[Tensor, bool]) - The first input is a bool or a tensor whose data type is bool.

  • y (Union[Tensor, bool]) - The second input is a bool when the first input is a tensor or a tensor whose data type is bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is bool.

引发

TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([True, False, True]), mindspore.bool_)
>>> y = Tensor(np.array([True, True, False]), mindspore.bool_)
>>> logical_or = ops.LogicalOr()
>>> output = logical_or(x, y)
>>> print(output)
[ True  True  True]
class tinyms.primitives.MDIterationGradientDescent(*args, **kwargs)[源代码]

Update the coordinate of each atom in the direction of potential for energy minimization.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • learning_rate (float32) – the update step length.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • frc (Tensor), The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

Output:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.MDIterationLeapFrog(*args, **kwargs)[源代码]

One step of classical leap frog algorithm to solve the finite difference Hamiltonian equations of motion for certain system.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • dt (float32) – the simulation time step.

Inputs:
  • sqrt_mass_inverse (Tensor) - The square root of the inverse value of the mass of each atom. The data type is float32 and the shape is \((n,)\).

  • vel (Tensor) - The velocity of each atom. The data type is float32 and the shape is \((n, 3)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • acc (Tensor) - The acceleration of each atom. The data type is float32 and the shape is \((n, 3)\).

  • inverse_mass (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.MDIterationLeapFrog(*args, **kwargs)[源代码]

One step of classical leap frog algorithm to solve the finite difference Hamiltonian equations of motion for certain system.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • dt (float32) – the simulation time step.

Inputs:
  • sqrt_mass_inverse (Tensor) - The square root of the inverse value of the mass of each atom. The data type is float32 and the shape is \((n,)\).

  • vel (Tensor) - The velocity of each atom. The data type is float32 and the shape is \((n, 3)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • acc (Tensor) - The acceleration of each atom. The data type is float32 and the shape is \((n, 3)\).

  • inverse_mass (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.MDIterationLeapFrogLiujian(*args, **kwargs)[源代码]

One step of classical leap frog algorithm to solve the finite difference Hamiltonian equations of motion for certain system, using Langevin dynamics with Liu’s thermostat scheme. Assume the number of atoms is n and the target control temperature is T.

Detailed iteration formula can be found in this paper: A unified thermostat scheme for efficient configurational sampling for classical/quantum canonical ensembles via molecular dynamics. DOI: 10.1063/1.4991621.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • atom_numbers (int32) – the number of atoms n.

  • dt (float32) – time step for finite difference.

  • half_dt (float32) – half of time step for finite difference.

  • exp_gamma (float32) – parameter in Liu’s dynamic.

Inputs:
  • inverse_mass (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n)\).

  • sqrt_mass_inverse (Tensor) - The inverse square root value of effect mass in Liu’s dynamics of each atom. The data type is float32 and the shape is \((n,)\).

  • vel (Tensor) - The velocity of each atom. The data type is float32 and the shape is \((n, 3)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • acc (Tensor) - The acceleration of each atom. The data type is float32 and the shape is \((n, 3)\).

  • rand_state (Tensor) - Random state to generate random force. The data type is float32 and the shape is \((math.ceil(n * 3.0 / 4.0) * 16, )\).

  • rand_frc (Tensor) - The random forces. The data type is float32 and the shape is \((n, 3)\).

Outputs:
  • output (Tensor) - The output coordinates. The data type is float32, and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.MDIterationLeapFrogLiujianWithMaxVel(*args, **kwargs)[源代码]

One step of classical leap frog algorithm to solve the finite difference Hamiltonian equations of motion for certain system, using Langevin dynamics with Liu’s thermostat scheme, but with an maximum velocity limit. Assume the number of atoms is n and the target control temperature is T.

Detailed iteration formula can be found in this paper: A unified thermostat scheme for efficient configurational sampling for classical/quantum canonical ensembles via molecular dynamics. DOI: 10.1063/1.4991621.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • dt (float32) – time step for finite difference.

  • half_dt (float32) – half of time step for finite difference.

  • exp_gamma (float32) – parameter in Liu’s dynamic, exp(-gamma_ln * dt).

  • max_vel (float32) – the maximum velocity limit.

Inputs:
  • inverse_mass (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

  • sqrt_mass_inverse (Tensor) - The inverse sqrt of the mass in Liu’s dynamics of each atom. The data type is float32 and the shape is \((n,)\).

  • vel (Tensor) - The velocity of each atom. The data type is float32 and the shape is \((n, 3)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • acc (Tensor) - The acceleration of each atom. The data type is float32 and the shape is \((n, 3)\).

  • rand_state (Tensor) - Random state to generate random force. The data type is float32 and the shape is \((math.ceil(n * 3.0 / 4.0) * 16, )\).

  • rand_frc (Tensor) - The random forces. The data type is float32 and the shape is \((n, 3)\).

Outputs:
  • output (float32) - The output coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.MDIterationLeapFrogWithMaxVel(*args, **kwargs)[源代码]

Leap frog algorithm to solve the Hamiltonian equations of motion with a maximum velocity limit.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • dt (float32) – the simulation time step.

  • max_velocity (float32) – the maximum velocity limit.

Inputs:
  • vel (Tensor) - The velocity of each atom. The data type is float32 and the shape is \((n, 3)\).

  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • frc (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\).

  • acc (Tensor) - The acceleration of each atom. The data type is float32 and the shape is \((n, 3)\).

  • inverse_mass (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.MDIterationSetupRandState(*args, **kwargs)[源代码]

Compute the random state of the iteration.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • atom_numbers (int32) – the number of atoms n.

  • seed (int32) – random seed.

Outputs:
  • output (Tensor) random state. The data type is float32 and the shape is \((ceil(n * 3 / 4),)\).

Supported Platforms:

GPU

class tinyms.primitives.MDTemperature(*args, **kwargs)[源代码]

Compute the MD temperature.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • residue_numbers (int32) – the number of residues m.

  • atom_numbers (int32) – the number of atoms n.

Inputs:
  • start (Tensor) - The start atom index of each residue. The data type is int32 and the shape is \((m,)\).

  • end (Tensor) - The end atom index of each residue. The data type is int32 and the shape is \((m,)\).

  • atom_vel_f (Tensor) - The velocity of each atom. The data type is float32 and the shape is \((n, 3)\).

  • atom_mass (Tensor) - The mass of each atom. The data type is float32 and the shape is \((n,)\).

Outputs:
  • ek (Tensor) - The temperature of each atom. The data type is float32 and the shape is \((n,)\).

Supported Platforms:

GPU

class tinyms.primitives.MakeRefKey(*args, **kwargs)[源代码]

Makes a RefKey instance by string. RefKey stores the name of Parameter, can be passed through the functions, and used for Assign target.

参数

tag (str) – Parameter name to make the RefKey.

Inputs:

No inputs.

Outputs:

RefKeyType, made from the Parameter name.

引发

TypeError – If tag is not a str.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import Parameter, Tensor
>>> from mindspore import dtype as mstype
>>> import mindspore.ops as ops
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.y = Parameter(Tensor(np.ones([2, 3]), mstype.int32), name="y")
...         self.make_ref_key = ops.MakeRefKey("y")
...
...     def construct(self, x):
...         key = self.make_ref_key()
...         ref = ops.make_ref(key, x, self.y)
...         return ref * x
...
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]]), mindspore.int32)
>>> net = Net()
>>> output = net(x)
>>> print(output)
[[ 1  4  9]
 [16 25 36]]
class tinyms.primitives.MapCenterOfMass(*args, **kwargs)[源代码]

Map all atoms in the same residue to the same periodic box, scale if necessary (usually in pressurestat). Assume system has n atoms.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数

residue_numbers (int32) – the number of residues m.

Inputs:
  • start (Tensor) - The start atom index of each residue. The data type is int32 and the shape is \((m,)\).

  • end (Tensor) - The end atom index of each residue. The data type is int32 and the shape is \((m,)\).

  • center_of_mass (Tensor) - The coordinate of centroid of each residue. The data type is float32 and the shape is \((m, 3)\).

  • box_length (Tensor) - The box length of the simulation box. The data type is float32 and the shape is \((3,)\).

  • no_wrap_crd (Tensor) - The coordinate of each atom before wrap. The data type is float32 and the shape is \((n, 3)\).

  • crd (Tensor) - The coordinate of each atom after wrap. The data type is float32 and the shape is \((n, 3)\).

  • scaler (Tensor) - The scaler of system. The data type is float32 and the shape is \((1,)\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.MaskedFill(*args, **kwargs)[源代码]

Fills elements of self tensor with value where mask is True.

The shapes of input and mask need to be the same or broadcast.

Inputs:
  • input (Tensor) - The source tensor whose data type is one of float16, float32, int8, int32.

  • mask (Tensor[bool]) - The boolean mask.

  • value (Union[float, Tensor]) – The value to fill in with, which only supports a 0-dimensional tensor or a float number.

Outputs:

Tensor, has the same type and shape as input.

引发
  • TypeError – If input or mask is not a tensor.

  • TypeError – If value is neither float number nor tensor.

  • TypeError – If dtype of input or value is not one of float16, float32, int8, int32.

  • TypeError – If dtype of value is different from that of input.

  • TypeError – If dtype of mask is not bool.

  • ValueError – If the shapes of input and mask could not be broadcast.

Supported Platforms:

Ascend

实际案例

>>> input = Tensor(np.array([1., 2., 3., 4.]), mindspore.float32)
>>> mask = Tensor(np.array([True, True, False, True]), mindspore.bool_)
>>> output = ops.MaskedFill()(input, mask, 0.5)
>>> print(output)
[0.5 0.5 3.  0.5]
class tinyms.primitives.MaskedSelect(*args, **kwargs)[源代码]

Returns a new 1-D Tensor which indexes the input tensor according to the boolean mask. The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable.

Inputs:
  • x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • mask (Tensor[bool]) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

A 1-D Tensor, with the same type as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3, 4]), mindspore.int64)
>>> mask = Tensor(np.array([1, 0, 1, 0]), mindspore.bool_)
>>> output = ops.MaskedSelect()(x, mask)
>>> print(output)
[1 3]
class tinyms.primitives.MatMul(*args, **kwargs)[源代码]

Multiplies matrix x and matrix y.

\[(Output)_{i j}=\sum_{k=1}^{p} a_{i k} b_{k j}=a_{i 1} b_{1 j}+a_{i 2} b_{2 j}+\cdots+a_{i p} b_{p j}, p\in N\]

where the \(i,j\) indicates the output of the i-th row and j-th column element.

参数
  • transpose_x (bool) – If true, x is transposed before multiplication. Default: False.

  • transpose_y (bool) – If true, y is transposed before multiplication. Default: False.

Inputs:
  • x (Tensor) - The first tensor to be multiplied. The shape of the tensor is \((N, C)\). If transpose_x is True, its shape must be \((N, C)\) after transpose.

  • y (Tensor) - The second tensor to be multiplied. The shape of the tensor is \((C, M)\). If transpose_y is True, its shape must be \((C, M)\) after transpose.

Outputs:

Tensor, the shape of the output tensor is \((N, M)\).

引发
  • TypeError – If transpose_a or transpose_b is not a bool.

  • ValueError – If the column of matrix dimensions of x is not equal to the row of matrix dimensions of y.

  • ValueError – If length of shape of x or y is not equal to 2.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.ones(shape=[1, 3]), mindspore.float32)
>>> y = Tensor(np.ones(shape=[3, 4]), mindspore.float32)
>>> matmul = ops.MatMul()
>>> output = matmul(x, y)
>>> print(output)
[[3. 3. 3. 3.]]
check_shape_size(x1, x2)[源代码]

Check the shape size of inputs for MatMul.

class tinyms.primitives.MatrixInverse(*args, **kwargs)[源代码]

Returns the inverse of the input matrix. If the matrix is irreversible, an error may be reported or an unknown result may be returned.

注解

The parameter ‘adjoint’ is only supporting False right now. Because complex number is not supported at present.

参数

adjoint (bool) – An optional bool. Default: False.

Inputs:
  • x (Tensor) - A matrix to be calculated. The matrix must be at least two dimensions, and the last two dimensions must be the same size. types: float32, float64.

Outputs:

Tensor, has the same type and shape as input x.

引发
  • TypeError – If adjoint is not a bool.

  • TypeError – If dtype of x is neither float32 nor float64.

  • ValueError – If the last two dimensions of x is not same size.

  • ValueError – If the dimension of x is less than 2.

Supported Platforms:

GPU

实际案例

>>> x = Tensor(np.array([[[-0.710504  , -1.1207525],
...                       [-1.7651395 , -1.7576632]],
...                      [[ 0.52412605,  1.9070215],
...                       [ 1.3384849 ,  1.4274558]]]), mindspore.float32)
>>> matrix_inverse = ops.MatrixInverse(adjoint=False)
>>> output = matrix_inverse(x)
>>> print(output)
[[[ 2.4095483  -1.536419  ]
  [-2.4197974   0.97401696]]
 [[-0.79111797  1.0569006 ]
  [ 0.74180895 -0.2904787 ]]]
class tinyms.primitives.MaxPool(*args, **kwargs)[源代码]

Max pooling operation.

Applies a 2D max pooling over an input Tensor which can be regarded as a composition of 2D planes.

Typically the input is of shape \((N_{in}, C_{in}, H_{in}, W_{in})\), MaxPool outputs regional maximum in the \((H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1)\), the operation is as follows.

\[\text{output}(N_i, C_j, h, w) = \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1} \text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)\]
参数
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the maximum value, is an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively. Default: 1.

  • strides (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, with shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If kernel_size or strides is neither int nor tuple.

  • ValueError – If pad_mode is neither ‘valid’ nor ‘same’ with not case sensitive.

  • ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.

  • ValueError – If kernel_size or strides is less than 1.

  • ValueError – If length of shape of input is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape((1, 3, 3, 4)), mindspore.float32)
>>> maxpool_op = ops.MaxPool(pad_mode="VALID", kernel_size=2, strides=1)
>>> output = maxpool_op(x)
>>> print(output)
[[[[ 5.  6.  7.]
   [ 9. 10. 11.]]
  [[17. 18. 19.]
   [21. 22. 23.]]
  [[29. 30. 31.]
   [33. 34. 35.]]]]
class tinyms.primitives.MaxPool3D(*args, **kwargs)[源代码]

3D max pooling operation.

Applies a 3D max pooling over an input Tensor which can be regarded as a composition of 3D planes.

Typically the input is of shape \((N_{in}, C_{in}, D_{in}, H_{in}, W_{in})\), MaxPool outputs regional maximum in the \((D_{in}, H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (d_{ker}, h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1, s_2)\), the operation is as follows.

\[\text{output}(N_i, C_j, d, h, w) = \max_{l=0, \ldots, d_{ker}-1} \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1} \text{input}(N_i, C_j, s_0 \times d + l, s_1 \times h + m, s_2 \times w + n)\]
参数
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the maximum value, is an int number that represents depth, height and width of the kernel, or a tuple of three int numbers that represent depth, height and width respectively. Default: 1.

  • strides (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

    • pad: Implicit paddings on both sides of the input in depth, height, width. The number of “pad” will be padded to the input Tensor borders. “pad” must be greater than or equal to 0.

  • pad_list (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to pad. If pad is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to pad[0], pad[1], pad[2], pad[3], pad[4] and pad[5] correspondingly.

  • ceil_mode (bool) – Whether to use ceil instead of floor to calculate output shape. Only effective in “pad” mode. When “pad_mode” is “pad” and “ceil_mode” is “None”, “ceil_mode” will be set as “False”. Default: None.

  • data_format (str) – The optional value for data format. Currently only support ‘NCDHW’. Default: ‘NCDHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C, D_{in}, H_{in}, W_{in})\). Data type must be float16 or float32.

Outputs:

Tensor, with shape \((N, C, D_{out}, H_{out}, W_{out})\). Has the data type with x.

引发
  • TypeError – If kernel_size or strides is neither an int not a tuple.

  • TypeError – If pad_mode or data_format is not a string.

  • ValueError – If numbers in kernel_size or strides are not positive.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If pad_mode is ‘same’ or ‘valid’, ‘ceil_mode’ is not None.

  • ValueError – If kernel_size or strides is a tuple whose length is not equal to 3.

  • ValueError – If data_format is not ‘NCDHW’.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.arange(1 * 2 * 2 * 2 * 3).reshape((1, 2, 2, 2, 3)), mindspore.float32)
>>> max_pool3d = ops.MaxPool3D(kernel_size=2, strides=1, pad_mode="valid")
>>> output = max_pool3d(x)
>>> print(output)
[[[[[10. 11.]]]
  [[[22. 23.]]]]]
class tinyms.primitives.MaxPoolWithArgmax(*args, **kwargs)[源代码]

Performs max pooling on the input Tensor and returns both max values and indices.

Typically the input is of shape \((N_{in}, C_{in}, H_{in}, W_{in})\), MaxPool outputs regional maximum in the \((H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1)\), the operation is as follows.

\[\text{output}(N_i, C_j, h, w) = \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1} \text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)\]
参数
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the maximum value and arg value, is an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively. Default: 1.

  • strides (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\). Data type must be float16 or float32.

Outputs:

Tuple of 2 Tensors, representing the maxpool result and where the max values are generated.

  • output (Tensor) - Maxpooling result, with shape \((N, C_{out}, H_{out}, W_{out})\). It has the same data type as x.

  • mask (Tensor) - Max values’ index represented by the mask. Data type is int32.

引发
  • TypeError – If the data type of x is neither float16 nor float32.

  • TypeError – If kernel_size or strides is neither an int nor a tuple.

  • TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape((1, 3, 3, 4)), mindspore.float32)
>>> maxpool_arg_op = ops.MaxPoolWithArgmax(pad_mode="VALID", kernel_size=2, strides=1)
>>> output_tensor, argmax = maxpool_arg_op(x)
>>> print(output_tensor)
[[[[ 5.  6.  7.]
   [ 9. 10. 11.]]
  [[17. 18. 19.]
   [21. 22. 23.]]
  [[29. 30. 31.]
   [33. 34. 35.]]]]
class tinyms.primitives.Maximum(*args, **kwargs)[源代码]

Computes the maximum of input tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发
  • TypeError – If x and y is not one of the following: Tensor, Number, bool.

  • ValueError – If x and y are not the same shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1 : same data type
>>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.float32)
>>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32)
>>> maximum = ops.Maximum()
>>> output = maximum(x, y)
>>> print(output)
[4. 5. 6.]
>>> # case 2 : different data type
>>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.int32)
>>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32)
>>> output = maximum(x, y)
>>> print(output.dtype)
Float32
class tinyms.primitives.Merge(*args, **kwargs)[源代码]

Merges all input data to one.

One and only one of the inputs must be selected as the output

Inputs:
  • inputs (Union(Tuple, List)) - The data to be merged. All tuple elements must have the same data type.

Outputs:

tuple. Output is tuple(data, output_index). The data has the same shape of inputs element.

引发

TypeError – If inputs is neither Tuple nor list.

实际案例

>>> merge = ops.Merge()
>>> input_x = Tensor(np.linspace(0, 8, 8).reshape(2, 4), mindspore.float32)
>>> input_y = Tensor(np.random.randint(-4, 4, (2, 4)), mindspore.float32)
>>> result = merge((input_x, input_y))
class tinyms.primitives.Meshgrid(*args, **kwargs)[源代码]

Generates coordinate matrices from given coordinate tensors.

Given N one-dimensional coordinate tensors, returns a tuple outputs of N N-D coordinate tensors for evaluating expressions on an N-D grid.

参数

indexing (str) – Either ‘xy’ or ‘ij’. Default: ‘xy’. When the indexing argument is set to ‘xy’ (the default), the broadcasting instructions for the first two dimensions are swapped.

Inputs:
  • input (Union[tuple]) - A Tuple of N 1-D Tensor objects. The length of input should be greater than 1. The data type is Number.

Outputs:

Tensors, A Tuple of N N-D Tensor objects. The data type is the same with the Inputs.

引发
  • TypeError – If indexing is not a str or input is not a tuple.

  • ValueError – If indexing is neither ‘xy’ nor ‘ij’.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([1, 2, 3, 4]).astype(np.int32))
>>> y = Tensor(np.array([5, 6, 7]).astype(np.int32))
>>> z = Tensor(np.array([8, 9, 0, 1, 2]).astype(np.int32))
>>> inputs = (x, y, z)
>>> meshgrid = ops.Meshgrid(indexing="xy")
>>> output = meshgrid(inputs)
>>> print(output)
(Tensor(shape=[3, 4, 5], dtype=Int32, value=
 [[[1, 1, 1, 1, 1],
   [2, 2, 2, 2, 2],
   [3, 3, 3, 3, 3],
   [4, 4, 4, 4, 4]],
  [[1, 1, 1, 1, 1],
   [2, 2, 2, 2, 2],
   [3, 3, 3, 3, 3],
   [4, 4, 4, 4, 4]],
  [[1, 1, 1, 1, 1],
   [2, 2, 2, 2, 2],
   [3, 3, 3, 3, 3],
   [4, 4, 4, 4, 4]]]),
 Tensor(shape=[3, 4, 5], dtype=Int32, value=
 [[[5, 5, 5, 5, 5],
   [5, 5, 5, 5, 5],
   [5, 5, 5, 5, 5],
   [5, 5, 5, 5, 5]],
  [[6, 6, 6, 6, 6],
   [6, 6, 6, 6, 6],
   [6, 6, 6, 6, 6],
   [6, 6, 6, 6, 6]],
  [[7, 7, 7, 7, 7],
   [7, 7, 7, 7, 7],
   [7, 7, 7, 7, 7],
   [7, 7, 7, 7, 7]]]),
 Tensor(shape=[3, 4, 5], dtype=Int32, value=
 [[[8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2]],
  [[8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2]],
  [[8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2]]]))
class tinyms.primitives.Minimum(*args, **kwargs)[源代码]

Computes the minimum of input tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发
  • TypeError – If x and y is not one of the following: Tensor, Number, bool.

  • ValueError – If x and y are not the same shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1 : same data type
>>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.float32)
>>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32)
>>> minimum = ops.Minimum()
>>> output = minimum(x, y)
>>> print(output)
[1. 2. 3.]
>>> # case 2 : different data type
>>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.int32)
>>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32)
>>> output = minimum(x, y)
>>> print(output.dtype)
Float32
class tinyms.primitives.MirrorPad(*args, **kwargs)[源代码]

Pads the input tensor according to the paddings and mode.

参数

mode (str) – Specifies the padding mode. The optional values are “REFLECT” and “SYMMETRIC”. Default: “REFLECT”.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions.

  • paddings (Tensor) - The paddings tensor. The value of paddings is a matrix(list), and its shape is (N, 2). N is the rank of input data. All elements of paddings are int type. For the input in the D th dimension, paddings[D, 0] indicates how many sizes to be extended ahead of the input tensor in the D th dimension, and paddings[D, 1] indicates how many sizes to be extended behind the input tensor in the D th dimension.

Outputs:

Tensor, the tensor after padding.

  • If mode is “REFLECT”, it uses a way of symmetrical copying through the axis of symmetry to fill in. If the input_x is [[1,2,3], [4,5,6], [7,8,9]] and paddings is [[1,1], [2,2]], then the Outputs is [[6,5,4,5,6,5,4], [3,2,1,2,3,2,1], [6,5,4,5,6,5,4], [9,8,7,8,9,8,7], [6,5,4,5,6,5,4]].

  • If mode is “SYMMETRIC”, the filling method is similar to the “REFLECT”. It is also copied according to the symmetry axis, except that it includes the symmetry axis. If the input_x is [[1,2,3], [4,5,6], [7,8,9]] and paddings is [[1,1], [2,2]], then the Outputs is [[2,1,1,2,3,3,2], [2,1,1,2,3,3,2], [5,4,4,5,6,6,5], [8,7,7,8,9,9,8], [8,7,7,8,9,9,8]].

引发
  • TypeError – If input_x or paddings is not a Tensor.

  • TypeError – If mode is not a str.

  • ValueError – If paddings.size is not equal to 2 * len(input_x).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case1: mode="REFLECT"
>>> class Net(nn.Cell):
...    def __init__(self, mode):
...        super(Net, self).__init__()
...        self.pad = ops.MirrorPad(mode=mode)
...        self.paddings = Tensor([[1, 1], [2, 2]])
...    def construct(self, input_x):
...        return self.pad(input_x, self.paddings)
...
>>> input_x = Tensor([[1,2,3], [4,5,6], [7,8,9]])
>>> pad = Net("REFLECT")
>>> output = pad(input_x)
>>> print(output)
[[6 5 4 5 6 5 4]
 [3 2 1 2 3 2 1]
 [6 5 4 5 6 5 4]
 [9 8 7 8 9 8 7]
 [6 5 4 5 6 5 4]]
>>> # case2: mode="SYMMETRIC"
>>> pad = Net("SYMMETRIC")
>>> output = pad(input_x)
>>> print(output)
[[2 1 1 2 3 3 2]
 [2 1 1 2 3 3 2]
 [5 4 4 5 6 6 5]
 [8 7 7 8 9 9 8]
 [8 7 7 8 9 9 8]]
class tinyms.primitives.Mish(*args, **kwargs)[源代码]

Computes MISH(A Self Regularized Non-Monotonic Neural Activation Function) of input tensors element-wise.

The function is shown as follows:

\[\text{output} = x * \tan(\log(1 + \exp(\text{x})))\]

See more details in A Self Regularized Non-Monotonic Neural Activation Function.

Inputs:
  • x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the x.

Supported Platforms:

Ascend

引发

TypeError – If dtype of x is neither float16 nor float32.

实际案例

>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> mish = ops.Mish()
>>> output = mish(x)
>>> print(output)
[[-0.30273438  3.9974136 -0.015625]
 [ 1.9439697  -0.02929688 8.999999]]
class tinyms.primitives.Mod(*args, **kwargs)[源代码]

Computes the remainder of dividing the first input tensor by the second input tensor element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, both dtypes cannot be bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = x_{i} // y_{i}\]

警告

  • The input data does not support 0.

  • When the elements of input exceeds 2048 , the accuracy of operator cannot guarantee the requirement of double thousandths in the mini form.

  • Due to different architectures, the calculation results of this operator on NPU and CPU may be inconsistent.

  • If shape is expressed as (D1,D2… ,Dn), then D1*D2… *DN<=1000000,n<=8.

Inputs:
  • x (Union[Tensor, Number]) - The first input is a number or a tensor whose data type is number.

  • y (Union[Tensor, Number]) - When the first input is a tensor, The second input could be a number or a tensor whose data type is number. When the first input is a number, the second input must be a tensor whose data type is number.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

ValueError – When x and y are not the same dtype.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-4.0, 5.0, 6.0]), mindspore.float32)
>>> y = Tensor(np.array([3.0, 2.0, 3.0]), mindspore.float32)
>>> mod = ops.Mod()
>>> output = mod(x, y)
>>> print(output)
[-1.  1.  0.]
class tinyms.primitives.Mul(*args, **kwargs)[源代码]

Multiplies two tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = x_{i} * y_{i}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发
  • TypeError – If x and y is not one of the following: Tensor, Number, bool.

  • ValueError – If x and y are not the same shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
>>> y = Tensor(np.array([4.0, 5.0, 6.0]), mindspore.float32)
>>> mul = ops.Mul()
>>> output = mul(x, y)
>>> print(output)
[ 4. 10. 18.]
class tinyms.primitives.MulNoNan(*args, **kwargs)[源代码]

Computes x * y element-wise. If y is zero, no matter what x is, it will return 0, and also If x is zero, no matter what y is, it will return 0.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, the shapes of them could be broadcasted. When the inputs are one tensor and one scalar, the scalar could only be a constant.

注解

The shapes of x and y should be the same or can be broadcasted.

Inputs:
  • x (Union[Tensor]) - The first input is a tensor whose data type is one of flota16, float32, int32, int64 currently or scalar.

  • y (Union[Tensor]) - The second input is a tensor whose data type is one of flota16, float32, int32, int64 currently or scalar.

Outputs:

Tensor, the shape is the same as the shape after broadcasting, and the data type is the one with higher precision among the two inputs.

Supported Platforms:

Ascend

引发

TypeError – If neither x nor y is a bool Tensor.

实际案例

>>> # case 1 : same data type and shape of two inputs, there are some 0 in y.
>>> x = Tensor(np.array([[-1.0, 6.0, np.inf], [np.nan, -7.0, 4.0]]), mindspore.float32)
>>> y = Tensor(np.array([[-1.0, 4.0, 0], [0, -3.0, 1.0]]), mindspore.float32)
>>> mul_no_nan = ops.MulNoNan()
>>> output = mul_no_nan(x, y)
>>> print(output)
[[ 1. 24. 0.]
[ 0. 21. 4.]]
>>> # case 2 : the shape of two inputs is same, there are some 0 in x, y.
>>> x = Tensor(np.array([[-1.0, 6.0, 0], [0, np.nan, 4.0]]), mindspore.int32)
>>> y = Tensor(np.array([[-1.0, 4.0, np.inf], [np.nan, 0, 1.0]]), mindspore.float32)
>>> output = mul_no_nan(x, y)
>>> print(output)
[[ 1. 24. 0.]
 [ 0.  0. 4.]]
>>> print(output.dtype)
Float32
>>> # case 3 : the y is a scalar.
>>> x = Tensor(np.array([[-1.0, 6.0, 0], [0, np.nan, 4.0]]), mindspore.float32)
>>> y = Tensor(0, mindspore.float32)
>>> output = mul_no_nan(x, y)
>>> print(output)
[[ 0. 0. 0.]
 [ 0. 0. 0.]]
class tinyms.primitives.Multinomial(*args, **kwargs)[源代码]

Returns a tensor sampled from the multinomial probability distribution located in the corresponding row of tensor input.

注解

The rows of input do not need to sum to one (in which case we use the values as weights), but must be non-negative, finite and have a non-zero sum.

参数
  • seed (int) – Random seed, must be non-negative. Default: 0.

  • seed2 (int) – Random seed2, must be non-negative. Default: 0.

Inputs:
  • x (Tensor[float32]) - the input tensor containing the cumsum of probabilities, must be 1 or 2 dimensions.

  • num_samples (int32) - number of samples to draw.

Outputs:

Tensor with the same rows as x, each row has num_samples sampled indices.

引发
  • TypeError – If neither seed nor seed2 is an int.

  • TypeError – If input is not a Tensor whose dtype is float32.

  • TypeError – If dtype of num_samples is not int32.

Supported Platforms:

GPU

实际案例

>>> x = Tensor([0., 9., 4., 0.], mstype.float32)
>>> multinomial = ops.Multinomial(seed=10)
>>> output = multinomial(x, 2)
>>> print(output)
[2 1]
class tinyms.primitives.NLLLoss(*args, **kwargs)[源代码]

Gets the negative log likelihood loss between logits and labels.

The nll loss with reduction=none can be described as:

\[\ell(x, t)=L=\left\{l_{1}, \ldots, l_{N}\right\}^{\top}, \quad l_{n}=-w_{t_{n}} x_{n, t_{n}}, \quad w_{c}=\text { weight }[c] \cdot 1\]

where \(x\) is the logits, \(t\) is the labels, \(w\) is the weight, N is the batch size, \(c\) belonging [0, C-1] is class index, where \(C\) is the number of classes.

If reduction is not ‘none’ (default ‘mean’), then

\[\begin{split}\ell(x, t)=\left\{\begin{array}{ll} \sum_{n=1}^{N} \frac{1}{\sum_{n=1}^{N} w_{t n}} l_{n}, & \text { if reduction }=\text { 'mean'; } \\ \sum_{n=1}^{N} l_{n}, & \text { if reduction }=\text { 'sum' } \end{array}\right.\end{split}\]
参数

reduction (str) – Apply specific reduction method to the output: ‘none’, ‘mean’, ‘sum’, Default: “mean”.

Inputs:
  • logits (Tensor) - Input logits, with shape \((N, C)\). Data type only support float32 or float16.

  • labels (Tensor) - Ground truth labels, with shape \((N,)\). Data type only support int32.

  • weight (Tensor) - The rescaling weight to each class, with shape \((C,)\) and data type only support float32 or float16.

Outputs:

Tuple of 2 tensors composed with loss and total_weight.

  • loss (Tensor) - When reduction is ‘none’ and logits is 2D tensor, the loss shape is \((N,)\). Otherwise, the loss is a scalar. The data type is same with input’s.

  • total_weight (Tensor) - The total_weight is a scalar. The data type is same with weight’s.

引发
  • TypeError – If dtype of logits or weight is neither float16 nor float32, labels is not int32.

  • ValueError – If logits is not a one or two dimension tensor, labels and weight not a one dimension tensor. When logits is a two dimension tensor, the first dimension of logits is not equal to labels, and second dimension of logits is not equal to weight. When logits is a one dimension tensor, the dimensions of logits, labels and weight should be equal to each other.

Supported Platforms:

Ascend GPU

实际案例

>>> logits = Tensor(np.array([[0.5488135, 0.71518934],
...                           [0.60276335, 0.5448832],
...                           [0.4236548, 0.6458941]]).astype(np.float32))
>>> labels = Tensor(np.array([0, 0, 0]).astype(np.int32))
>>> weight = Tensor(np.array([0.3834415, 0.79172504]).astype(np.float32))
>>> nll_loss = ops.NLLLoss(reduction="mean")
>>> loss, weight = nll_loss(logits, labels, weight)
>>> print(loss)
-0.52507716
>>> print(weight)
1.1503246
class tinyms.primitives.NMSWithMask(*args, **kwargs)[源代码]

When object detection problem is performed in the computer vision field, object detection algorithm generates a plurality of bounding boxes. Selects some bounding boxes in descending order of score(Descending order is not supported in Ascend platform currently). Use the box with the highest score calculate the overlap between other boxes and the current box, and delete the box based on a certain threshold(IOU). The IOU is as follows,

\[\text{IOU} = \frac{\text{Area of Overlap}}{\text{Area of Union}}\]

警告

Only supports up to 2864 input boxes at one time.

参数

iou_threshold (float) – Specifies the threshold of overlap boxes with respect to IOU. Default: 0.5.

Inputs:
  • bboxes (Tensor) - The shape of tensor is \((N, 5)\). Input bounding boxes. N is the number of input bounding boxes. Every bounding box contains 5 values, the first 4 values are the coordinates(x0, y0, x1, y1) of bounding box which represents the point of top-left and bottom-right, and the last value is the score of this bounding box. The data type must be float16 or float32.

Outputs:

tuple[Tensor], tuple of three tensors, they are selected_boxes, selected_idx and selected_mask.

  • selected_boxes (Tensor) - The shape of tensor is \((N, 5)\). The list of bounding boxes after non-max suppression calculation.

  • selected_idx (Tensor) - The shape of tensor is \((N,)\). The indexes list of valid input bounding boxes.

  • selected_mask (Tensor) - The shape of tensor is \((N,)\). A mask list of valid output bounding boxes.

引发

ValueError – If the iou_threshold is not a float number, or if the first dimension of input Tensor is less than or equal to 0, or if the data type of the input Tensor is not float16 or float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> bbox = np.array([[100.0, 100.0, 50.0, 68.0, 0.63], [150.0, 75.0, 165.0, 115.0, 0.55],
...                  [12.0, 190.0, 288.0, 200.0, 0.9], [28.0, 130.0, 106.0, 172.0, 0.3]])
>>> bbox[:, 2] += bbox[:, 0]
>>> bbox[:, 3] += bbox[:, 1]
>>> inputs = Tensor(bbox, mindspore.float32)
>>> nms = ops.NMSWithMask(0.1)
>>> output_boxes, indices, mask = nms(inputs)
>>> indices_np = indices.asnumpy()
>>> print(indices_np[mask.asnumpy()])
[0 1 2]
class tinyms.primitives.NPUAllocFloatStatus(*args, **kwargs)[源代码]

Allocates a flag to store the overflow status.

The flag is a tensor whose shape is (8,) and data type is mindspore.dtype.float32.

注解

Examples: see NPUGetFloatStatus.

Outputs:

Tensor, has the shape of (8,).

Supported Platforms:

Ascend

实际案例

>>> alloc_status = ops.NPUAllocFloatStatus()
>>> output = alloc_status()
>>> print(output)
[0. 0. 0. 0. 0. 0. 0. 0.]
class tinyms.primitives.NPUClearFloatStatus(*args, **kwargs)[源代码]

Clears the flag which stores the overflow status.

注解

The flag is in the register on the Ascend device. It will be reset and can not be reused again after the NPUClearFloatStatus is called. In addition, there are strict sequencing requirements for use, i.e., before using the NPUGetFloatStatus operator, need to ensure that the NPUClearFlotStatus and your compute has been executed. We use Depend to ensure the execution order.

Examples: see NPUGetFloatStatus.

Inputs:
  • x (Tensor) - The output tensor of NPUAllocFloatStatus. The data type must be float16 or float32.

Outputs:

Tensor, has the same shape as x. All the elements in the tensor will be zero.

Supported Platforms:

Ascend

实际案例

>>> self.alloc_status = ops.NPUAllocFloatStatus()
>>> self.get_status = ops.NPUGetFloatStatus()
>>> self.clear_status = ops.NPUClearFloatStatus()
>>> init = self.alloc_status()
>>> init = F.Depend(init, input)  # Ensure clear_status after input
>>> clear_status = self.clear_status(init)
>>> input = F.Depend(input, clear_status)  # Ensure your compute after clear_status
>>> output = Compute(input)
>>> init = F.Depend(init, output)
>>> flag = self.get_status(init)  # Ensure get_status after your compute
>>> self.clear_status(init)
>>> print(init)
[0. 0. 0. 0. 0. 0. 0. 0.]
class tinyms.primitives.NPUGetFloatStatus(*args, **kwargs)[源代码]

Updates the flag which is the output tensor of NPUAllocFloatStatus with the latest overflow status.

The flag is a tensor whose shape is (8,) and data type is mindspore.dtype.float32. If the sum of the flag equals to 0, there is no overflow happened. If the sum of the flag is bigger than 0, there is overflow happened. In addition, there are strict sequencing requirements for use, i.e., before using the NPUGetFloatStatus operator, need to ensure that the NPUClearFlotStatus and your compute has been executed. We use Depend to ensure the execution order.

Inputs:
  • x (Tensor) - The output tensor of NPUAllocFloatStatus. The data type must be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as x. All the elements in the tensor will be zero.

引发
  • TypeError – If x is not a Tensor.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> self.alloc_status = ops.NPUAllocFloatStatus()
>>> self.get_status = ops.NPUGetFloatStatus()
>>> self.clear_status = ops.NPUClearFloatStatus()
>>> init = self.alloc_status()
>>> init = F.Depend(init, input)  # Ensure clear_status after input
>>> clear_status = self.clear_status(init)
>>> input = F.Depend(input, clear_status)  # Ensure your compute after clear_status
>>> output = Compute(input)
>>> init = F.Depend(init, output)
>>> flag = self.get_status(init)  # Ensure get_status after your compute
>>> self.clear_status(init)
>>> print(init)
[0. 0. 0. 0. 0. 0. 0. 0.]
class tinyms.primitives.Neg(*args, **kwargs)[源代码]

Returns a tensor with negative values of the input tensor element-wise.

\[out_{i} = - x_{i}\]
Inputs:
  • x (Tensor) - The input tensor whose dtype is number. \((N,*)\) where \(*\) means ,any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape and dtype as input.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> neg = ops.Neg()
>>> x = Tensor(np.array([1, 2, -1, 2, 0, -3.5]), mindspore.float32)
>>> output = neg(x)
>>> print(output)
[-1.  -2.   1.  -2.   0.   3.5]
class tinyms.primitives.NeighborListRefresh(*args, **kwargs)[源代码]

Update (or construct if first time) the Verlet neighbor list for the calculation of short-ranged force.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • grid_numbers (int32) – the total number of grids divided G.

  • atom_numbers (int32) – the number of atoms n.

  • not_first_time (int32) – whether to construct the neighbor list first time or not.

  • nxy (int32) – the total number of grids divided in xy plane.

  • excluded_atom_numbers (int32) – the total atom numbers in the excluded list E.

  • cutoff_square (float32) – the cutoff square distance for short-range force calculation.

  • half_skin_square (float32) – the maximum square value of the distance atom allowed to move between two updates.

  • cutoff_with_skin (float32) – cutoff + skin, indicates the radius of the neighbor list for each atom.

  • half_cutoff_with_skin (float32) – cutoff_with_skin/2.

  • cutoff_with_skin_square (float32) – the square value of cutoff_with_skin.

  • refresh_interval (int32) – the number of iteration steps between two updates of neighbor list. Default: 20.

  • cutoff (float32) – the cutoff distance for short-range force calculation. Default: 10.0.

  • skin (float32) – the maximum value of the distance atom allowed to move. Default: 2.0.

  • max_atom_in_grid_numbers (int32) – the maximum number of atoms in one grid k. Default: 64.

  • max_neighbor_numbers (int32) – The maximum number of neighbors m. Default: 800.

  • forced_update (int32) – the flag that decides whether to force an update. Default: 0.

  • forced_check (int32) – the flag that decides whether to force an check. Default: 0.

Inputs:
  • atom_numbers_in_grid_bucket (Tensor) - The number of atoms in each grid bucket. The data type is int32 and the shape is \((G,)\).

  • bucket (Tensor) - (Tensor) - The atom indices in each grid bucket. The data type is int32 and the shape is \((G, k)\).

  • crd (Tensor) - The coordinates of each atom. The data type is float32 and the shape is \((n, 3)\).

  • box_length (Tensor) - The box length of the simulation box. The data type is float32 and the shape is \((3,)\).

  • grid_n (Tensor) - The number of grids divided of 3 dimensions of the simulation box. The data type is int32 and the shape is \((3,)\).

  • grid_length_inverse (Tensor) - The inverse value of grid length. The data type is float32 and the shape is \((3,)\).

  • atom_in_grid_serial (Tensor) - The grid index for each atom. The data type is int32 and the shape is \((n,)\).

  • old_crd (Tensor) - The coordinates before update of each atom. The data type is float32 and the shape is \((n, 3)\).

  • crd_to_uint_crd_cof (Tensor) - The scale factor between the unsigned int coordinate and the real one. The data type is float32 and the shape is \((3,)\).

  • uint_crd (Tensor) - The unsigned int coordinates value fo each atom. The data type is unsigned int32 and the shape is \((n, 3)\).

  • gpointer (Tensor) - The nearest neighbor grids (including self) of each grid. The data type is int32 and the shape is \((G, 125)\).

  • nl_atom_numbers (Tensor) - The number of atoms in neighbor list of each atom. The data type is int32 and the shape is \((n,)\).

  • nl_atom_serial (Tensor) - The indices of atoms in neighbor list of each atom. The data type is int32 and the shape is \((n, m)\).

  • uint_dr_to_dr_cof (Tensor) - The scale factor. The data type is float32 and the shape is \((3,)\).

  • excluded_list_start (Tensor) - The start excluded index in excluded list for each atom. The data type is int32 and the shape is \((n,)\).

  • excluded_list (Tensor) - The contiguous join of excluded list of each atom. The data type is int32 and the shape is \((E,)\).

  • excluded_numbers (Tensor) - The number of atom excluded in excluded list for each atom. The data type is int32 and the shape is \((n,)\).

  • need_refresh_flag (Tensor) - Whether the neighbor list of each atom need update or not. The data type is int32 and the shape is \((1,)\).

  • refresh_count (Union[Tensor, Scalar]) - Count how many iteration steps have passed since last update. The data type is int32 and the shape is \((1,)\) or \(()\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.NeighborListUpdate(*args, **kwargs)[源代码]

Update (or construct if first time) the Verlet neighbor list for the calculation of short-ranged force. Assume the number of atoms is N, the number of grids divided is G, the maximum number of atoms in one grid is m, the maximum number of atoms in single atom’s neighbor list is L, and the number of total atom in excluded list is E.

参数
  • grid_numbers (int32) – the total number of grids divided.

  • not_first_time (int32) – whether to construct the neighbor list first time or not.

  • nxy (int32) – the total number of grids divided in xy plane.

  • excluded_atom_numbers (int32) – the total atom numbers in the excluded list.

  • cutoff (float32) – the cutoff distance for short-range force calculation.

  • skin (float32) – the overflow value of cutoff to maintain a neighbor list.

  • cutoff_square (float32) – the suqare value of cutoff.

  • half_skin_square (float32) – skin*skin/4, indicates the maximum square value of the distance atom allowed to move between two updates.

  • cutoff_with_skin (float32) – cutoff + skin, indicates the radius of the neighbor list for each atom.

  • half_cutoff_with_skin (float32) – cutoff_with_skin/2.

  • cutoff_with_skin_square (float32) – the square value of cutoff_with_skin.

  • refresh_interval (int32) – the number of iteration steps between two updates of neighbor list.

  • max_atom_in_grid_numbers (int32) – the maximum number of atoms in one grid k.

Inputs:
  • atom_numbers_in_grid_bucket (Tensor) - The number of atoms in each grid bucket. The data type is int32 and the shape is \((G,)\).

  • bucket (Tensor) - (Tensor) - The atom indices in each grid bucket. The data type is int32 and the shape is \((G, k)\).

  • crd (Tensor) - The coordinates of each atom. The data type is float32 and the shape is \((n, 3)\).

  • box_length (Tensor) - The box length of the simulation box. The data type is float32 and the shape is \((3,)\).

  • grid_N (Tensor) - The number of grids divided of 3 dimensions of the simulation box. The data type is int32 and the shape is \((3,)\).

  • grid_length_inverse (Tensor) - The inverse value of grid length. The data type is float32 and the shape is \((3,)\).

  • atom_in_grid_serial (Tensor) - The grid index for each atom. The data type is int32 and the shape is \((n,)\).

  • old_crd (Tensor) - The coordinates before update of each atom. The data type is float32 and the shape is \((n, 3)\).

  • crd_to_uint_crd_cof (Tensor) - The scale factor between the unsigned int coordinate and the real one. The data type is float32 and the shape is \((3,)\).

  • uint_crd (Tensor) - The unsigned int coordinates value fo each atom. The data type is unsigned int32 and the shape is \((n, 3)\).

  • gpointer (Tensor) - The nearest neighbor grids (including self) of each grid. The data type is int32 and the shape is \((G, 125)\).

  • nl_atom_numbers (Tensor) - The number of atoms in neighbor list of each atom. The data type is int32 and the shape is \((n,)\).

  • nl_atom_serial (Tensor) - The indices of atoms in neighbor list of each atom. The data type is int32 and the shape is \((n, m)\).

  • uint_dr_to_dr_cof (Tensor) - The scale factor. The data type is float32 and the shape is \((3,)\).

  • excluded_list_start (Tensor) - The start excluded index in excluded list for each atom. The data type is int32 and the shape is \((n,)\).

  • excluded_list (Tensor) - The contiguous join of excluded list of each atom. The data type is int32 and the shape is \((E,)\).

  • excluded_numbers (Tensor) - The number of atom excluded in excluded list for each atom. The data type is int32 and the shape is \((n,)\).

  • need_refresh_flag (Tensor) - Whether the neighbor list of each atom need update or not. The data type is int32 and the shape is \((1,)\).

  • refresh_count (Tensor) - Count how many iteration steps have passed since last update. The data type is int32 and the shape is \((1,)\) or \(()\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.NoRepeatNGram(*args, **kwargs)[源代码]

Updates log_probs with repeat n-grams.

During beam search, if consecutive ngram_size words exist in the generated word sequence, the consecutive ngram_size words will be avoided during subsequent prediction. For example, when ngram_size is 3, the generated word sequence is [1, 2, 3, 2, 3], the next predicted word will not be 2 and the value of log_probs will be replaced with -FLOAT_MAX. Because 3 consecutive words [2, 3, 2] do not appear twice in the word sequence.

参数

ngram_size (int) – Size of n-grams, must be greater than 0. Default: 1.

Inputs:
  • state_seq (Tensor) - A 3-D tensor with shape: (batch_size, beam_width, m).

  • log_probs (Tensor) - A 3-D tensor with shape: (batch_size, beam_width, vocab_size). The value of log_probs will be replaced with -FLOAT_MAX when n-grams repeated.

Outputs:
  • log_probs (Tensor) - The output Tensor with same shape and type as original log_probs.

引发
  • TypeError – If ngram_size is not an int.

  • TypeError – If neither state_seq nor log_probs is a Tensor.

Supported Platforms:

Ascend

实际案例

>>> no_repeat_ngram = ops.NoRepeatNGram(ngram_size=3)
>>> state_seq = Tensor([[[1, 2, 1, 2, 5, 1, 2],
...                      [9, 3, 9, 5, 4, 1, 5]],
...                     [[4, 8, 6, 4, 5, 6, 4],
...                      [4, 8, 8, 4, 3, 4, 8]]], dtype=mindspore.int32)
>>> log_probs = Tensor([[[0.7, 0.8, 0.6, 0.9, 0.2, 0.8, 0.4, 0.6, 0.2, 0.7],
...                      [0.4, 0.5, 0.6, 0.7, 0.8, 0.1, 0.9, 0.8, 0.7, 0.1]],
...                     [[0.9, 0.7, 0.6, 0.3, 0.5, 0.3, 0.5, 0.4, 0.8, 0.6],
...                      [0.5, 0.8, 0.8, 0.7, 0.7, 0.8, 0.2, 0.7, 0.9, 0.7]]], dtype=mindspore.float32)
>>> output = no_repeat_ngram(state_seq, log_probs)
>>> print(output)
[[[ 6.9999999e-01 -3.4028235e+38  6.0000002e-01  8.9999998e-01
    2.0000000e-01 -3.4028235e+38  4.0000001e-01  6.0000002e-01
    2.0000000e-01  6.9999999e-01]
  [ 4.0000001e-01  5.0000000e-01  6.0000002e-01  6.9999999e-01
    8.0000001e-01  1.0000000e-01  8.9999998e-01  8.0000001e-01
    6.9999999e-01  1.0000000e-01]]
 [[ 8.9999998e-01  6.9999999e-01  6.0000002e-01  3.0000001e-01
    5.0000000e-01 -3.4028235e+38  5.0000000e-01  4.0000001e-01
    8.0000001e-01  6.0000002e-01]
  [ 5.0000000e-01  8.0000001e-01  8.0000001e-01  6.9999999e-01
    6.9999999e-01  8.0000001e-01  2.0000000e-01  6.9999999e-01
   -3.4028235e+38  6.9999999e-01]]]
class tinyms.primitives.NotEqual(*args, **kwargs)[源代码]

Computes the non-equivalence of two tensors element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[\begin{split}out_{i} =\begin{cases} & \text{True, if } x_{i} \ne y_{i} \\ & \text{False, if } x_{i} = y_{i} \end{cases}\end{split}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting,and the data type is bool.

引发
  • TypeError – If x and y is not one of the following: Tensor, Number, bool.

  • TypeError – If neither x nor y is a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> not_equal = ops.NotEqual()
>>> output = not_equal(x, 2.0)
>>> print(output)
[ True False  True]
>>>
>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([1, 2, 4]), mindspore.int32)
>>> not_equal = ops.NotEqual()
>>> output = not_equal(x, y)
>>> print(output)
[False False  True]
class tinyms.primitives.OneHot(*args, **kwargs)[源代码]

Computes a one-hot tensor.

Makes a new tensor, whose locations represented by indices in indices take value on_value, while all other locations take value off_value.

注解

If the input indices is rank N, the output will have rank N+1. The new axis is created at dimension axis.

参数

axis (int) – Position to insert the value. e.g. If shape of indices is \((N, C)\), and axis is -1, the output shape will be \((N, C, D)\), If axis is 0, the output shape will be \((D, N, C)\). Default: -1.

Inputs:
  • indices (Tensor) - A tensor of indices. Tensor of shape \((X_0, \ldots, X_n)\). Data type must be int32 or int64.

  • depth (int) - A scalar defining the depth of the one hot dimension.

  • on_value (Tensor) - A value to fill in output when indices[j] = i. With data type of float16 or float32.

  • off_value (Tensor) - A value to fill in output when indices[j] != i. Has the same data type as on_value.

Outputs:

Tensor, one-hot tensor. Tensor of shape \((X_0, \ldots, X_{axis}, \text{depth} ,X_{axis+1}, \ldots, X_n)\).

引发
  • TypeError – If axis or depth is not an int.

  • TypeError – If dtype of indices is neither int32 nor int64.

  • TypeError – If indices, on_value or off_value is not a Tensor.

  • ValueError – If axis is not in range [-1, len(indices_shape)].

  • ValueError – If depth is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> indices = Tensor(np.array([0, 1, 2]), mindspore.int32)
>>> depth, on_value, off_value = 3, Tensor(1.0, mindspore.float32), Tensor(0.0, mindspore.float32)
>>> onehot = ops.OneHot()
>>> output = onehot(indices, depth, on_value, off_value)
>>> print(output)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
class tinyms.primitives.Ones(*args, **kwargs)[源代码]

Creates a tensor filled with value ones.

Creates a tensor with shape described by the first argument and fills it with value ones in type of the second argument.

Inputs:
  • shape (Union[tuple[int], int]) - The specified shape of output tensor. Only constant positive int is allowed.

  • type (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed.

Outputs:

Tensor, has the same type and shape as input shape value.

引发

TypeError – If shape is neither tuple nor int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> ones = ops.Ones()
>>> output = ones((2, 2), mindspore.float32)
>>> print(output)
[[1. 1.]
 [1. 1.]]
>>> output = ones((3, 3), mindspore.float32)
>>> print(output)
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
class tinyms.primitives.OnesLike(*args, **kwargs)[源代码]

Creates a new tensor. The values of all elements are 1.

Returns a tensor of ones with the same shape and type as the input.

Inputs:
  • input_x (Tensor) - Input tensor. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape and type as input_x but filled with ones.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> oneslike = ops.OnesLike()
>>> input_x = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))
>>> output = oneslike(input_x)
>>> print(output)
[[1 1]
 [1 1]]
class tinyms.primitives.PMEEnergy(*args, **kwargs)[源代码]

Calculate the Coulumb energy of the system using PME method.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

\[E = sum_{ij} q_iq_j/r_{ij}\]
参数
  • atom_numbers (int32) – the number of atoms, n.

  • excluded_numbers (int32) – the length of excluded list, E.

  • beta (float32) – the PME beta parameter, determined by the non-bond cutoff value and simulation precision tolerance.

  • fftx (int32) – the number of points for Fourier transform in dimension X.

  • ffty (int32) – the number of points for Fourier transform in dimension Y.

  • fftz (int32) – the number of points for Fourier transform in dimension Z.

  • box_length_0 (float32) – the value of boxlength idx 0

  • box_length_1 (float32) – the value of boxlength idx 1

  • box_length_2 (float32) – the value of boxlength idx 2

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\)

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\)

  • nl_numbers - (Tensor) - The each atom. The data type is int32 and the shape is \((n, 3)\)

  • nl_serial - (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, 800)\)

  • scaler (Tensor) - The scale factor between real space coordinates and its unsigned int value. The data type is float32 and the shape is \((3,)\)

  • excluded_list_start (Tensor) - The start excluded index in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

  • excluded_list (Tensor) - The contiguous join of excluded list of each atom. E is the number of excluded atoms. The data type is int32 and the shape is \((E,)\)

  • excluded_atom_numbers (Tensor) - The number of atom excluded in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

Outputs:
  • reciprocal_ene (Tensor) - The reciprocal term of PME energy. The data type is float32 and the the shape is \((1,)\).

  • self_ene (Tensor) - The self term of PME energy. The data type is float32 and the the shape is \((1,)\).

  • direct_ene (Tensor) - The direct term of PME energy. The data type is float32 and the the shape is \((1,)\).

  • correction_ene (Tensor) - The correction term of PME energy. The data type is float32 and the the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.PMEEnergyUpdate(*args, **kwargs)[源代码]

Calculate the Coulumb energy of the system using PME method for pressure.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • excluded_numbers (int32) – the length of excluded list, E.

  • beta (float32) – the PME beta parameter, determined by the non-bond cutoff value and simulation precision tolerance.

  • fftx (int32) – the number of points for Fourier transform in dimension X.

  • ffty (int32) – the number of points for Fourier transform in dimension Y.

  • fftz (int32) – the number of points for Fourier transform in dimension Z.

  • box_length_0 (float32) – the value of boxlength idx 0.

  • box_length_1 (float32) – the value of boxlength idx 1.

  • box_length_2 (float32) – the value of boxlength idx 2.

  • max_neighbor_numbers (int32) – the max neighbor numbers, m, default 800.

  • need_update (int32) – if need_update = 1, calculate the pressure, default 0.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\)

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\)

  • nl_numbers - (Tensor) - The each atom. The data type is int32 and the shape is \((n, 3)\)

  • nl_serial - (Tensor) - The neighbor list of each atom, the max number is 800. The data type is int32 and the shape is \((n, m)\)

  • scaler (Tensor) - The scale factor between real space coordinates and its unsigned int value. The data type is float32 and the shape is \((3,)\)

  • excluded_list_start (Tensor) - The start excluded index in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

  • excluded_list (Tensor) - The contiguous join of excluded list of each atom. E is the number of excluded atoms. The data type is int32 and the shape is \((E,)\)

  • excluded_atom_numbers (Tensor) - The number of atom excluded in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

  • factor (Tensor) - The factor parameter to be updated in pressure calculation. The data type is float32 and the shape is \((1,)\)

  • beta (Tensor) - The PME beta parameter to be updated in pressure calculation. The data type is float32 and the shape is \((1,)\)

Outputs:
  • reciprocal_ene (Tensor) - The reciprocal term of PME energy. The data type is float32 and the the shape is \((1,)\).

  • self_ene (Tensor) - The self term of PME energy. The data type is float32 and the the shape is \((1,)\).

  • direct_ene (Tensor) - The direct term of PME energy. The data type is float32 and the the shape is \((1,)\).

  • correction_ene (Tensor) - The correction term of PME energy. The data type is float32 and the the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.PMEExcludedForce(*args, **kwargs)[源代码]

Calculate the excluded part of long-range Coulumb force using PME(Particle Meshed Ewald) method. Assume the number of atoms is n, and the length of excluded list is E.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • excluded_numbers (int32) – the length of excluded list, E.

  • beta (float32) – the PME beta parameter, determined by the non-bond cutoff value and simulation precision tolerance.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\)

  • scaler (Tensor) - The scale factor between real space coordinates and its unsigned int value. The data type is float32 and the shape is \((3,)\)

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\)

  • excluded_list_start (Tensor) - The start excluded index in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

  • excluded_list (Tensor) - The contiguous join of excluded list of each atom. E is the number of excluded atoms. The data type is int32 and the shape is \((E,)\)

  • excluded_atom_numbers (Tensor) - The number of atom excluded in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

Outputs:
  • force (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\)

Supported Platforms:

GPU

class tinyms.primitives.PMEExcludedForceUpdate(*args, **kwargs)[源代码]

Calculate the excluded part of long-range Coulumb force using PME(Particle Meshed Ewald) method for pressure. Assume the number of atoms is n, and the length of excluded list is E.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • excluded_numbers (int32) – the length of excluded list, E.

  • beta (float32) – the PME beta parameter, determined by the non-bond cutoff value and simulation precision tolerance.

  • need_update (int32) – if need_update = 1, calculate the pressure, default 0.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\)

  • scaler (Tensor) - The scale factor between real space coordinates and its unsigned int value. The data type is float32 and the shape is \((3,)\)

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\)

  • excluded_list_start (Tensor) - The start excluded index in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

  • excluded_list (Tensor) - The contiguous join of excluded list of each atom. E is the number of excluded atoms. The data type is int32 and the shape is \((E,)\)

  • excluded_atom_numbers (Tensor) - The number of atom excluded in excluded list for each atom. The data type is int32 and the shape is \((n,)\)

  • beta (Tensor) - The PME beta parameter to be updated in pressure calculation. The data type is float32 and the shape is \((1,)\)

Outputs:
  • force (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\)

Supported Platforms:

GPU

class tinyms.primitives.PMEReciprocalForce(*args, **kwargs)[源代码]

Calculate the reciprocal part of long-range Coulumb force using PME(Particle Meshed Ewald) method. Assume the number of atoms is n.

The detailed calculation formula of PME(Particle Meshed Ewald) method can be found in this paper: A Smooth Particle Mesh Ewald Method. DOI: 10.1063/1.470117.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • beta (float32) – the PME beta parameter, determined by the non-bond cutoff value and simulation precision tolerance.

  • fftx (int32) – the number of points for Fourier transform in dimension X.

  • ffty (int32) – the number of points for Fourier transform in dimension Y.

  • fftz (int32) – the number of points for Fourier transform in dimension Z.

  • box_length_0 (float32) – the value of boxlength idx 0

  • box_length_1 (float32) – the value of boxlength idx 1

  • box_length_2 (float32) – the value of boxlength idx 2

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinates value of each atom. The data type is uint32 and the shape is \((n, 3)\)

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\)

Outputs:
  • force (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\)

Supported Platforms:

GPU

class tinyms.primitives.PMEReciprocalForceUpdate(*args, **kwargs)[源代码]

Calculate the reciprocal part of long-range Coulumb force using PME(Particle Meshed Ewald) method for pressure. Assume the number of atoms is n.

The detailed calculation formula of PME(Particle Meshed Ewald) method can be found in this paper: A Smooth Particle Mesh Ewald Method. DOI: 10.1063/1.470117.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms, n.

  • beta (float32) – the PME beta parameter, determined by the non-bond cutoff value and simulation precision tolerance.

  • fftx (int32) – the number of points for Fourier transform in dimension X.

  • ffty (int32) – the number of points for Fourier transform in dimension Y.

  • fftz (int32) – the number of points for Fourier transform in dimension Z.

  • box_length_0 (float32) – the value of boxlength idx 0

  • box_length_1 (float32) – the value of boxlength idx 1

  • box_length_2 (float32) – the value of boxlength idx 2

  • need_update (int32) – if need_update = 1, calculate the pressure, default 0.

Inputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

  • charge (Tensor) - The charge carried by each atom. The data type is float32 and the shape is \((n,)\)

  • beta (Tensor) - The PME beta parameter to be updated in pressure calculation. The data type is float32 and the shape is \((1,)\)

Outputs:
  • force (Tensor) - The force felt by each atom. The data type is float32 and the shape is \((n, 3)\)

Supported Platforms:

GPU

class tinyms.primitives.PReLU(*args, **kwargs)[源代码]

Parametric Rectified Linear Unit activation function.

PReLU is described in the paper Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification. Defined as follows:

\[prelu(x_i)= \max(0, x_i) + \min(0, w * x_i),\]

where \(x_i\) is an element of an channel of the input, w is the weight of the channel.

注解

0-D or 1-D input_x is not supported on Ascend.

Inputs:
  • x (Tensor) - The first input tensor, representing the output of the preview layer. With data type of float16 or float32. The shape is \((N, C, *)\) where \(*\) means, any number of additional dimensions.

  • weight (Tensor) - The second input tensor. The data type is float16 or float32. There are only two shapes are legitimate, 1 or the number of channels of the input_x. Channel dim is the 2nd dim of input. When input is 0-D or 1-D tensor, the number of channels is 1.

Outputs:

Tensor, with the same type as x.

For detailed information, please refer to nn.PReLU.

引发
  • TypeError – If dtype of x or weight is neither float16 nor float32.

  • TypeError – If the x or the weight is not a Tensor.

  • ValueError – If the x is a 0-D or 1-D Tensor on Ascned.

  • ValueError – If the weight is not a 1-D Tensor.

Supported Platforms:

Ascend GPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.prelu = ops.PReLU()
...     def construct(self, x, weight):
...         result = self.prelu(x, weight)
...         return result
...
>>> x = Tensor(np.arange(-6, 6).reshape((2, 3, 2)), mindspore.float32)
>>> weight = Tensor(np.array([0.1, 0.6, -0.3]), mindspore.float32)
>>> net = Net()
>>> output = net(x, weight)
>>> print(output)
[[[-0.60 -0.50]
  [-2.40 -1.80]
  [ 0.60  0.30]]
 [[ 0.00  1.00]
  [ 2.00  3.00]
  [ 4.0   5.00]]]
class tinyms.primitives.Pack(**kwargs)[源代码]

Same as operator Stack. Pack will be deprecated in the future. Please use Stack instead.

class tinyms.primitives.Pad(*args, **kwargs)[源代码]

Pads the input tensor according to the paddings. For example, to pad only the last dimension of the input tensor, then pad has the form (padding_left,padding_right); to pad the last 2 dimensions of the input tensor, then use (padding_left,padding_right, padding_top,padding_bottom); to pad the last 3 dimensions, use (padding_left,padding_right, padding_top,padding_bottom padding_front,padding_back).

\[\begin{split}\begin{aligned} &\text{ input_x_shape} = (N_{1},N_{2},...,N_{n}) \\ &\begin{aligned} \text{output_shape = }(&N_{1}+paddings[0,0]+paddings[0,1], \\ & N_{2}+paddings[1,0]+paddings[1,1], \\ &... , \\ & N_{n}+paddings[n-1,0]+paddings[n-1,1]) \end{aligned} \end{aligned}\end{split}\]
参数

paddings (tuple) – The shape of parameter paddings is (N, 2). N is the rank of input data. All elements of paddings are int type. For the input in D th dimension, paddings[D, 0] indicates how many sizes to be extended ahead of the input tensor in the D th dimension, and paddings[D, 1] indicates how many sizes to be extended behind the input tensor in the D th dimension.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, the tensor after padding.

引发
  • TypeError – If paddings is not a tuple.

  • TypeError – If input_x is not a Tensor.

  • ValueError – If shape of paddings is not \((N, 2)\).

  • ValueError – If paddings.size is not equal to 2 * len(input_x).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> pad_op = ops.Pad(((1, 2), (2, 1)))
>>> output = pad_op(input_x)
>>> print(output)
[[ 0.   0.   0.   0.   0.   0. ]
 [ 0.   0.  -0.1  0.3  3.6  0. ]
 [ 0.   0.   0.4  0.5 -3.2  0. ]
 [ 0.   0.   0.   0.   0.   0. ]
 [ 0.   0.   0.   0.   0.   0. ]]
class tinyms.primitives.Padding(*args, **kwargs)[源代码]

Extends the last dimension of the input tensor from 1 to pad_dim_size, by filling with 0.

参数

pad_dim_size (int) – The value of the last dimension of x to be extended, which must be positive. Default: 8.

Inputs:
  • x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The rank of x must be at least 2. The last dimension of x must be 1. The data type is Number.

Outputs:

Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\).

引发
Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([[8], [10]]), mindspore.float32)
>>> pad_dim_size = 4
>>> output = ops.Padding(pad_dim_size)(x)
>>> print(output)
[[ 8.  0.  0.  0.]
 [10.  0.  0.  0.]]
class tinyms.primitives.ParallelConcat(*args, **kwargs)[源代码]

Concats tensor in the first dimension.

Concats input tensors along with the first dimension.

The difference between Concat and ParallelConcat is that Concat requires all of the inputs be computed before the operation will begin but doesn’t require that the input shapes be known during graph construction. Parallel concat will copy pieces of the input into the output as they become available, in some situations this can provide a performance benefit.

注解

The input tensors are all required to have size 1 in the first dimension.

Inputs:
  • values (tuple, list) - A tuple or a list of input tensors. The data type and shape of these tensors must be the same. The data type is Number except float64.

Outputs:

Tensor, data type is the same as values.

引发
  • ValueError – If length of shape of values is less than 1.

  • ValueError – The data type and shape of these tensors are not the same.

Supported Platforms:

Ascend

实际案例

>>> data1 = Tensor(np.array([[0, 1]]).astype(np.int32))
>>> data2 = Tensor(np.array([[2, 1]]).astype(np.int32))
>>> op = ops.ParallelConcat()
>>> output = op((data1, data2))
>>> print(output)
[[0 1]
 [2 1]]
class tinyms.primitives.Partial(*args, **kwargs)[源代码]

Makes a partial function instance, used for pynative mode.

Inputs:
  • args (Union[FunctionType, Tensor]) - The function and bind arguments.

Outputs:

FunctionType, partial function binded with arguments.

class tinyms.primitives.Poisson(*args, **kwargs)[源代码]

Produces random non-negative integer values i, distributed according to discrete probability function:

\[\text{P}(i|μ) = \frac{\exp(-μ)μ^{i}}{i!},\]
参数
  • seed (int) – Random seed, must be non-negative. Default: 0.

  • seed2 (int) – Random seed2, must be non-negative. Default: 0.

Inputs:
  • shape (tuple) - The shape of random tensor to be generated. Only constant value is allowed.

  • mean (Tensor) - μ parameter the distribution was constructed with. The parameter defines mean number of occurrences of the event. It must be greater than 0. With float32 data type.

Outputs:

Tensor. Its shape must be the broadcasted shape of shape and the shape of mean. The dtype is int32.

引发
  • TypeError – If neither seed nor seed2 is an int.

  • TypeError – If shape is not a tuple.

  • TypeError – If mean is not a Tensor whose dtype is not float32.

Supported Platforms:

Ascend

实际案例

>>> shape = (4, 1)
>>> mean = Tensor(np.array([5.0, 10.0]), mstype.float32)
>>> poisson = ops.Poisson(seed=5)
>>> output = poisson(shape, mean)
>>> result = output.shape
>>> print(result)
(4, 2)
class tinyms.primitives.PopulationCount(*args, **kwargs)[源代码]

Calculates population count.

Inputs:
  • input (Tensor) - The data type must be int16 or uint16.

Outputs:

Tensor, with the same shape as the input.

引发

TypeError – If input is not a Tensor.

Supported Platforms:

Ascend

实际案例

>>> population_count = ops.PopulationCount()
>>> x_input = Tensor([0, 1, 3], mindspore.int16)
>>> output = population_count(x_input)
>>> print(output)
[0 1 2]
class tinyms.primitives.Pow(*args, **kwargs)[源代码]

Computes a tensor to the power of the second input.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = x_{i} ^{ y_{i}}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发
  • TypeError – If x and y is not one of the following: Tensor, Number, bool.

  • ValueError – If x and y are not the same shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
>>> y = 3.0
>>> pow = ops.Pow()
>>> output = pow(x, y)
>>> print(output)
[ 1.  8. 64.]
>>>
>>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
>>> y = Tensor(np.array([2.0, 4.0, 3.0]), mindspore.float32)
>>> pow = ops.Pow()
>>> output = pow(x, y)
>>> print(output)
[ 1. 16. 64.]
class tinyms.primitives.Print(*args, **kwargs)[源代码]

Outputs the tensor or string to stdout. The outputs are printed to screen by default. It can also be saved in a file by setting the parameter print_file_path in context. Once set, the output will be saved in the file specified by print_file_path. parse_print can be employed to reload the data. For more information, please refer to mindspore.context.set_context() and mindspore.parse_print().

注解

In pynative mode, please use python print function. In graph mode, the bool, int and float would be converted into Tensor to print, str remains unchanged.

Inputs:
  • input_x (Union[Tensor, bool, int, float, str]) - The graph node to attach to. Supports multiple inputs which are separated by ‘,’.

Outputs:

Tensor, has the same data type and shape as original input_x.

引发

TypeError – If input_x is not one of the following: Tensor, bool, int, float, str.

Supported Platforms:

Ascend GPU

实际案例

>>> class PrintDemo(nn.Cell):
...     def __init__(self):
...         super(PrintDemo, self).__init__()
...         self.print = ops.Print()
...
...     def construct(self, x, y):
...         self.print('Print Tensor x and Tensor y:', x, y)
...         return x
...
>>> x = Tensor(np.ones([2, 1]).astype(np.int32))
>>> y = Tensor(np.ones([2, 2]).astype(np.int32))
>>> net = PrintDemo()
>>> result = net(x, y)
Print Tensor x and Tensor y:
Tensor(shape=[2, 1], dtype=Int32, value=
[[1]
 [1]])
Tensor(shape=[2, 2], dtype=Int32, value=
[[1 1]
 [1 1]])
class tinyms.primitives.Pull(*args, **kwargs)[源代码]

Pulls weight from parameter server.

Inputs:
  • key (Tensor) - The key of the weight.

  • weight (Tensor) - The weight to be updated.

Outputs:

None.

class tinyms.primitives.PullWeight(*args, **kwargs)[源代码]

Pull weight by its names from server.

Inputs:
  • weight (Tensor) - The weight to be pulled.

  • name (String) - The full name of the weight.

  • index (Int) - The index of the weight.

Outputs:

None.

class tinyms.primitives.Push(*args, **kwargs)[源代码]

Pushes the inputs of the corresponding optimizer to parameter server.

参数
  • optim_type (string) – The optimizer type. Default: ‘ApplyMomentum’.

  • only_shape_indices (list) – The indices of input of which only shape will be pushed to parameter server. Default: None.

Inputs:
  • optim_inputs (tuple) - The inputs for this kind of optimizer.

  • optim_input_shapes (tuple) - The shapes of the inputs.

Outputs:

Tensor, the key of the weight which needs to be updated.

class tinyms.primitives.PushWeight(*args, **kwargs)[源代码]

Upload weight by its names to server.

Inputs:
  • weight (Tensor) - The weight to be uploaded.

  • name (String) - The full name of the weight.

  • index (Int) - The index of the weight.

Outputs:

None.

class tinyms.primitives.PyFunc(fn, in_types, in_shapes, out_types, out_shapes, stateful=True)[源代码]

Execute Python function.

PyFunc encapsulates Python functions as an operator which could be compiled into computation graph. Unlike normal operators, it cannot be exported to MindIR as it is executed in current Python context. As only the weights of the network is stored in the checkpoint, network include PyFunc could save checkpoint and load to the network again, but will lose any Python function state.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • fn (function) – Python function which inputs and outputs should be Python built-in scalar or numpy ndarray.

  • in_types (list[mindspore.dtype]) – The type of the inputs.

  • in_shapes (list[tuple[int]]) – The dimensionality of the inputs. An empty list represents a scalar, otherwise it represent a numpy array.

  • out_types (list[mindspore.dtype]) – The type of the outputs.

  • out_shapes (list[tuple[int]]) – The dimensionality of the outputs. An empty list represents a scalar, otherwise it represent a numpy array.

  • stateful (bool) – Whether the function is stateful or not. If True, the execution order is same with model definition.

Inputs:
  • input_x (Union(tuple[Tensor], list[Tensor])) - The input tuple or list is made up of multiple tensors.

Outputs:

tuple[Tensor], execution results Python functions.

引发
  • TypeError – The Python function execution failed.

  • TypeError – The attributes(in_types/in_shapes/out_types/out_shapes) are inconsistent with Python function specifications.

Supported Platforms:

CPU

实际案例

>>> def func(x1, x2):
>>>     return x1 + x2
>>> x1 = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> x2 = Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> op = P.PyFunc(func, [x1.dtype, x2.dtype], [x1.shape, x2.shape], [x1.dtype], [x1.dtype])
>>> output = op((x1, x2))
>>> print(output[0].asnumpy())
[2. 4. 6.]
class tinyms.primitives.RNNTLoss(*args, **kwargs)[源代码]

Computes the RNNTLoss and its gradient with respect to the softmax outputs.

参数

blank_label (int) – blank label. Default: 0.

Inputs:
  • acts (Tensor) - Tensor of shape \((B, T, U, V)\). Data type must be float16 or float32.

  • labels (Tensor) - Tensor of shape \((B, U-1)\). Data type is int32.

  • input_lengths (Tensor) - Tensor of shape \((B,)\). Data type is int32.

  • label_lengths (Tensor) - Tensor of shape \((B,)\). Data type is int32.

Outputs:
  • costs (Tensor) - Tensor of shape \((B,)\). Data type is int32.

  • grads (Tensor) - Has the same shape and dtype as acts.

引发
  • TypeError – If acts, labels, input_lengths or label_lengths is not a Tensor.

  • TypeError – If dtype of acts is neither float16 nor float32.

  • TypeError – If dtype of labels, input_lengths or label_lengths is not int32.

Supported Platforms:

Ascend

实际案例

>>> B, T, U, V = 1, 2, 3, 5
>>> blank = 0
>>> acts = np.random.random((B, T, U, V)).astype(np.float32)
>>> labels = np.array([[1, 2]]).astype(np.int32)
>>> input_length = np.array([T] * B).astype(np.int32)
>>> label_length = np.array([len(l) for l in labels]).astype(np.int32)
>>> rnnt_loss = ops.RNNTLoss(blank_label=0)
>>> costs, grads = rnnt_loss(Tensor(acts), Tensor(labels), Tensor(input_length), Tensor(label_length))
>>> print(costs.shape)
(1,)
>>> print(grads.shape)
(1, 2, 3, 5)
class tinyms.primitives.ROIAlign(*args, **kwargs)[源代码]

Computes the Region of Interest (RoI) Align operator.

The operator computes the value of each sampling point by bilinear interpolation from the nearby grid points on the feature map. No quantization is performed on any coordinates involved in the RoI, its bins, or the sampling points. The details of (RoI) Align operator are described in Mask R-CNN.

参数
  • pooled_height (int) – The output features height.

  • pooled_width (int) – The output features width.

  • spatial_scale (float) – A scaling factor that maps the raw image coordinates to the input feature map coordinates. Suppose the height of a RoI is ori_h in the raw image and fea_h in the input feature map, the spatial_scale must be fea_h / ori_h.

  • sample_num (int) – Number of sampling points. Default: 2.

  • roi_end_mode (int) – Number must be 0 or 1. Default: 1.

Inputs:
  • features (Tensor) - The input features, whose shape must be \((N, C, H, W)\).

  • rois (Tensor) - The shape is \((rois\_n, 5)\). With data type of float16 or float32. rois_n represents the number of RoI. The size of the second dimension must be 5 and the 5 colunms are \((image\_index, top\_left\_x, top\_left\_y, bottom\_right\_x, bottom\_right\_y)\). image_index represents the index of image. top_left_x and top_left_y represent the x, y coordinates of the top left corner of corresponding RoI, respectively. bottom_right_x and bottom_right_y represent the x, y coordinates of the bottom right corner of corresponding RoI, respectively.

Outputs:

Tensor, the shape is \((rois\_n, C, pooled\_height, pooled\_width)\).

引发
  • TypeError – If pooled_height, pooled_width, sample_num or roi_end_mode is not an int.

  • TypeError – If spatial_scale is not a float.

  • TypeError – If features or rois is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> features = Tensor(np.array([[[[1., 2.], [3., 4.]]]]), mindspore.float32)
>>> rois = Tensor(np.array([[0, 0.2, 0.3, 0.2, 0.3]]), mindspore.float32)
>>> roi_align = ops.ROIAlign(2, 2, 0.5, 2)
>>> output = roi_align(features, rois)
>>> print(output)
[[[[1.775 2.025]
   [2.275 2.525]]]]
class tinyms.primitives.RandomCategorical(*args, **kwargs)[源代码]

Generates random samples from a given categorical distribution tensor.

参数

dtype (mindspore.dtype) – The type of output. Its value must be one of mindspore.int16, mindspore.int32 and mindspore.int64. Default: mindspore.int64.

Inputs:
  • logits (Tensor) - The input tensor. 2-D Tensor with shape [batch_size, num_classes].

  • num_sample (int) - Number of sample to be drawn. Only constant values is allowed.

  • seed (int) - Random seed. Default: 0. Only constant values is allowed.

Outputs:
  • output (Tensor) - The output Tensor with shape [batch_size, num_samples].

引发
  • TypeError – If dtype is not one of the following: mindspore.int16, mindspore.int32, mindspore.int64.

  • TypeError – If logits is not a Tensor.

  • TypeError – If neither num_sample nor seed is an int.

Supported Platforms:

Ascend GPU

实际案例

>>> class Net(nn.Cell):
...   def __init__(self, num_sample):
...     super(Net, self).__init__()
...     self.random_categorical = ops.RandomCategorical(mindspore.int64)
...     self.num_sample = num_sample
...   def construct(self, logits, seed=0):
...     return self.random_categorical(logits, self.num_sample, seed)
...
>>> x = np.random.random((10, 5)).astype(np.float32)
>>> net = Net(8)
>>> output = net(Tensor(x))
>>> result = output.shape
>>> print(result)
(10, 8)
class tinyms.primitives.RandomChoiceWithMask(*args, **kwargs)[源代码]

Generates a random sample as index tensor with a mask tensor from a given tensor.

The input must be a tensor of rank not less than 1. If its rank is greater than or equal to 2, the first dimension specifies the number of samples. The index tensor and the mask tensor have the fixed shapes. The index tensor denotes the index of the nonzero sample, while the mask tensor denotes which elements in the index tensor are valid.

参数
  • count (int) – Number of items expected to get and the number must be greater than 0. Default: 256.

  • seed (int) – Random seed. Default: 0.

  • seed2 (int) – Random seed2. Default: 0.

Inputs:
  • input_x (Tensor[bool]) - The input tensor. The input tensor rank must be greater than or equal to 1 and less than or equal to 5.

Outputs:

Two tensors, the first one is the index tensor and the other one is the mask tensor.

  • index (Tensor) - The output shape is 2-D.

  • mask (Tensor) - The output shape is 1-D.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> rnd_choice_mask = ops.RandomChoiceWithMask()
>>> input_x = Tensor(np.ones(shape=[240000, 4]).astype(np.bool))
>>> output_y, output_mask = rnd_choice_mask(input_x)
>>> result = output_y.shape
>>> print(result)
(256, 2)
>>> result = output_mask.shape
>>> print(result)
(256,)
class tinyms.primitives.Randperm(*args, **kwargs)[源代码]

Generates n random samples from 0 to n-1 without repeating. If max_length > n, the last max_length-n elements will be filled with pad.

参数
  • max_length (int) – Number of items expected to get and the number must be greater than 0. Default: 1.

  • pad (int) – The pad value to be filled. Default: -1.

  • dtype (mindspore.dtype) – The type of output. Default: mindspore.int32.

Inputs:
  • n (Tensor[int32]) - The input tensor with shape: (1,) and the number must be in [0, max_length].

Outputs:
  • output (Tensor) - The output Tensor with shape: (max_length,) and type: dtype.

引发
Supported Platforms:

Ascend GPU

实际案例

>>> # The result of every execution is different because this operator will generate n random samples.
>>> randperm = ops.Randperm(max_length=30, pad=-1)
>>> n = Tensor([20], dtype=mindspore.int32)
>>> output = randperm(n)
>>> print(output)
[15 6 11 19 14 16 9 5 13 18 4 10 8 0 17 2 1 12 3 7
 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]
class tinyms.primitives.Range(*args, **kwargs)[源代码]

Creates a sequence of numbers that begins at start and extends by increments of delta up to but not including limit.

The types of all 3 inputs must be the same. The type of the resulting tensor is the same as the type of the inputs.

参数

maxlen (int) – Memory that can fit maxlen many elements will be allocated for the output. Optional, must be positive, defaults to 1000000. If the output has more than maxlen elements, a runtime error will occur.

Inputs:
  • start (Tensor) - A scalar Tensor. The first number in the sequence. Must have type: int32 or float32

  • limit (Tensor) - A scalar Tensor. Upper limit of the sequence, exclusive. Must have type: int32 or float32

  • delta (Tensor) - A scalar Tensor. Number that increments start. Must have type: int32 or float32

Outputs:

A 1-D Tensor, with the same type as the inputs.

Supported Platforms:

GPU

实际案例

>>> start = Tensor(0, mstype.int32)
>>> limit = Tensor(10, mstype.int32)
>>> delta = Tensor(4, mstype.int32)
>>> output = ops.Range()(start, limit, delta)
>>> print(output)
[0, 4, 8]
infer_value(start_value, limit_value, delat_value)[源代码]

Infer the value of input for Range.

class tinyms.primitives.Rank(*args, **kwargs)[源代码]

Returns the rank of a tensor.

Returns a 0-D int32 Tensor representing the rank of input; the rank of a tensor is the number of indices required to uniquely select each element of the tensor.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The data type is Number.

Outputs:

Tensor. 0-D int32 Tensor representing the rank of input, i.e., \(R\). The data type is an int.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
>>> rank = ops.Rank()
>>> output = rank(input_tensor)
>>> print(output)
2
>>> print(type(output))
<class 'int'>
class tinyms.primitives.ReLU(*args, **kwargs)[源代码]

Computes ReLU (Rectified Linear Unit) of input tensors element-wise.

It returns \(\max(x,\ 0)\) element-wise.

注解

In general, this operator is more commonly used. The difference from ReLuV2 is that the operator will output one more Mask.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with number data type.

Outputs:

Tensor, with the same type and shape as the input_x.

引发
  • TypeError – If dtype of input_x is not number.

  • TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> relu = ops.ReLU()
>>> output = relu(input_x)
>>> print(output)
[[0. 4. 0.]
 [2. 0. 9.]]
class tinyms.primitives.ReLU6(*args, **kwargs)[源代码]

Computes ReLU (Rectified Linear Unit) upper bounded by 6 of input tensors element-wise.

\[\text{ReLU6}(x) = \min(\max(0,x), 6)\]

It returns \(\min(\max(0,x), 6)\) element-wise.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the input_x.

引发
  • TypeError – If dtype of input_x is neither float16 nor float32.

  • TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> relu6 = ops.ReLU6()
>>> result = relu6(input_x)
>>> print(result)
[[0. 4. 0.]
 [2. 0. 6.]]
class tinyms.primitives.ReLUV2(*args, **kwargs)[源代码]

Computes ReLU (Rectified Linear Unit) of input tensors element-wise.

It returns \(\max(x,\ 0)\) element-wise.

注解

The difference from ReLu is that the operator will output one more Mask, and the kernel of the operator is different from ReLu.

Inputs:
  • input_x (Tensor) - The input tensor must be a 4-D tensor.

Outputs:
  • output (Tensor) - Has the same type and shape as the input_x.

  • mask (Tensor) - A tensor whose data type must be uint8.

引发
Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([[[[1, -2], [-3, 4]], [[-5, 6], [7, -8]]]]), mindspore.float32)
>>> relu_v2 = ops.ReLUV2()
>>> output, mask= relu_v2(input_x)
>>> print(output)
[[[[1. 0.]
   [0. 4.]]
  [[0. 6.]
   [7. 0.]]]]
>>> print(mask)
[[[[[1 0]
    [2 0]]
   [[2 0]
    [1 0]]]]]
class tinyms.primitives.Real(*args, **kwargs)[源代码]

Returns a Tensor that is the real part of the input.

Inputs:
  • input (Tensor, complex) - The input tensor. types: complex64, complex128.

Outputs:

Tensor, has the float type.

引发

TypeError – If the dtype of input is not one of: complex64, complex128.

Supported Platforms:

GPU

实际案例

>>> x = Tensor(np.asarray(np.complex(1.3+0.4j)), mindspore.complex64)
>>> conj = ops.Real()
>>> output = conj(x)
>>> print(output)
1.3
class tinyms.primitives.RealDiv(*args, **kwargs)[源代码]

Divides the first input tensor by the second input tensor in floating-point type element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = x_{i} / y_{i}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发
  • TypeError – If x and y is not one of the following: Tensor, Number, bool.

  • ValueError – If x and y are not the same shape.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
>>> y = Tensor(np.array([4.0, 5.0, 6.0]), mindspore.float32)
>>> realdiv = ops.RealDiv()
>>> output = realdiv(x, y)
>>> print(output)
[0.25 0.4  0.5 ]
class tinyms.primitives.Reciprocal(*args, **kwargs)[源代码]

Returns reciprocal of a tensor element-wise.

\[out_{i} = \frac{1}{x_{i}}\]
Inputs:
  • x (Tensor) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
>>> reciprocal = ops.Reciprocal()
>>> output = reciprocal(x)
>>> print(output)
[1.   0.5  0.25]
class tinyms.primitives.ReduceAll(*args, **kwargs)[源代码]

Reduces a dimension of a tensor by the “logicalAND” of all elements in the dimension, by Default. And also can reduces a dimension of x along the axis. Determine whether the dimensions of the output and input are the same by controlling keep_dims.

参数

keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.

Inputs:
  • x (Tensor[bool]) - The input tensor. The dtype of the tensor to be reduced is bool. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)).

Outputs:

Tensor, the dtype is bool.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the “logical and” of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
  • TypeError – If keep_dims is not a bool.

  • TypeError – If x is not a Tensor.

  • ValueError – If axis is not one of the following: int, tuple or list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[True, False], [True, True]]))
>>> op = ops.ReduceAll(keep_dims=True)
>>> # case 1: Reduces a dimension by averaging all elements in the dimension.
>>> output = op(x)
>>> print(output)
[[False]]
>>> print(output.shape)
(1, 1)
>>> # case 2: Reduces a dimension along axis 0.
>>> output = op(x, 0)
>>> print(output)
[[ True False]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = op(x, 1)
>>> print(output)
[[False]
[ True]]
class tinyms.primitives.ReduceAny(*args, **kwargs)[源代码]

Reduces a dimension of a tensor by the “logical OR” of all elements in the dimension, by Default. And also can reduces a dimension of x along the axis. Determine whether the dimensions of the output and input are the same by controlling keep_dims.

参数

keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.

Inputs:
  • x (Tensor[bool]) - The input tensor. The dtype of the tensor to be reduced is bool. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)).

Outputs:

Tensor, the dtype is bool.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the “logical or” of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
  • TypeError – If keep_dims is not a bool.

  • TypeError – If x is not a Tensor.

  • ValueError – If axis is not one of the following: int, tuple or list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[True, False], [True, True]]))
>>> op = ops.ReduceAny(keep_dims=True)
>>> # case 1: Reduces a dimension by averaging all elements in the dimension.
>>> output = op(x)
>>> print(output)
[[ True]]
>>> print(output.shape)
(1, 1)
>>> # case 2: Reduces a dimension along axis 0.
>>> output = op(x, 0)
>>> print(output)
[[ True True]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = op(x, 1)
>>> print(output)
[[True]
[ True]]
class tinyms.primitives.ReduceMax(*args, **kwargs)[源代码]

Reduces a dimension of a tensor by the maximum value in this dimension, by Default. And also can reduces a dimension of x along the axis. Determine whether the dimensions of the output and input are the same by controlling keep_dims.

参数

keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.

Inputs:
  • x (Tensor[Number]) - The input tensor. The dtype of the tensor to be reduced is number. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)).

Outputs:

Tensor, has the same dtype as the x.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the maximum of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
  • TypeError – If keep_dims is not a bool.

  • TypeError – If x is not a Tensor.

  • ValueError – If axis is not one of the following: int, tuple or list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> op = ops.ReduceMax(keep_dims=True)
>>> output = op(x, 1)
>>> result = output.shape
>>> print(result)
(3, 1, 5, 6)
>>> # case 1: Reduces a dimension by averaging all elements in the dimension.
>>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]],
...                      [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]],
...                      [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32)
>>> output = op(x)
>>> print(output)
[[[9.]]]
>>> print(output.shape)
(1, 1, 1)
>>> # case 2: Reduces a dimension along axis 0.
>>> output = op(x, 0)
>>> print(output)
[[[7. 7. 7. 7. 7. 7.]
  [8. 8. 8. 8. 8. 8.]
  [9. 9. 9. 9. 9. 9.]]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = op(x, 1)
>>> print(output)
[[[3. 3. 3. 3. 3. 3.]]
 [[6. 6. 6. 6. 6. 6.]]
 [[9. 9. 9. 9. 9. 9.]]]
>>> # case 4: Reduces a dimension along axis 2.
>>> output = op(x, 2)
>>> print(output)
[[[1.]
  [2.]
  [3.]]
 [[4.]
  [5.]
  [6.]]
 [[7.]
  [8.]
  [9.]]]
class tinyms.primitives.ReduceMean(*args, **kwargs)[源代码]

Reduces a dimension of a tensor by averaging all elements in the dimension, by Default. And also can reduces a dimension of x along the axis. Determine whether the dimensions of the output and input are the same by controlling keep_dims.

参数

keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default: False.

Inputs:
  • x (Tensor[Number]) - The input tensor. The dtype of the tensor to be reduced is number. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)).

Outputs:

Tensor, has the same dtype as the x.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the mean of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
  • TypeError – If keep_dims is not a bool.

  • TypeError – If x is not a Tensor.

  • ValueError – If axis is not one of the following: int, tuple or list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> op = ops.ReduceMean(keep_dims=True)
>>> output = op(x, 1)
>>> result = output.shape
>>> print(result)
(3, 1, 5, 6)
>>> # case 1: Reduces a dimension by averaging all elements in the dimension.
>>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]],
...                      [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]],
...                      [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32)
>>> output = op(x)
>>> print(output)
[[[5.]]]
>>> print(output.shape)
(1, 1, 1)
>>> # case 2: Reduces a dimension along the axis 0
>>> output = op(x, 0)
>>> print(output)
[[[4. 4. 4. 4. 4. 4.]
  [5. 5. 5. 5. 5. 5.]
  [6. 6. 6. 6. 6. 6.]]]
>>> # case 3: Reduces a dimension along the axis 1
>>> output = op(x, 1)
>>> print(output)
[[[2. 2. 2. 2. 2. 2.]]
 [[5. 5. 5. 5. 5. 5.]]
 [[8. 8. 8. 8. 8. 8.]]]
>>> # case 4: Reduces a dimension along the axis 2
>>> output = op(x, 2)
>>> print(output)
[[[1.       ]
  [2.       ]
  [3.       ]]
 [[4.       ]
  [5.       ]
  [6.       ]]
 [[7.0000005]
  [8.       ]
  [9.       ]]]
class tinyms.primitives.ReduceMin(*args, **kwargs)[源代码]

Reduces a dimension of a tensor by the minimum value in the dimension, by Default. And also can reduces a dimension of x along the axis. Determine whether the dimensions of the output and input are the same by controlling keep_dims.

参数

keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.

Inputs:
  • x (Tensor[Number]) - The input tensor. The dtype of the tensor to be reduced is number. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)).

Outputs:

Tensor, has the same dtype as the x.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the minimum of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
  • TypeError – If keep_dims is not a bool.

  • TypeError – If x is not a Tensor.

  • ValueError – If axis is not one of the following: int, tuple or list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> op = ops.ReduceMin(keep_dims=True)
>>> output = op(x, 1)
>>> result = output.shape
>>> print(result)
(3, 1, 5, 6)
>>> # case 1: Reduces a dimension by averaging all elements in the dimension.
>>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]],
...                      [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]],
...                      [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32)
>>> output = op(x)
>>> print(output)
[[[1.]]]
>>> print(output.shape)
(1, 1, 1)
>>> # case 2: Reduces a dimension along axis 0.
>>> output = op(x, 0)
>>> print(output)
[[[1. 1. 1. 1. 1. 1.]
  [2. 2. 2. 2. 2. 2.]
  [3. 3. 3. 3. 3. 3.]]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = op(x, 1)
>>> print(output)
[[[1. 1. 1. 1. 1. 1.]]
 [[4. 4. 4. 4. 4. 4.]]
 [[7. 7. 7. 7. 7. 7.]]]
>>> # case 4: Reduces a dimension along axis 2.
>>> output = op(x, 2)
>>> print(output)
[[[1.]
  [2.]
  [3.]]
 [[4.]
  [5.]
  [6.]]
 [[7.]
  [8.]
  [9.]]]
class tinyms.primitives.ReduceOp[源代码]

Operation options for reducing tensors. This is an enumerated type, not an operator. Mainly used in data parallel mode.

The main calling methods are as follows:

  • SUM: ReduceOp.SUM.

  • MAX: ReduceOp.MAX.

  • MIN: ReduceOp.MIN.

  • PROD: ReduceOp.PROD.

There are four kinds of operation options, “SUM”, “MAX”, “MIN”, and “PROD”.

  • SUM: Take the sum.

  • MAX: Take the maximum.

  • MIN: Take the minimum.

  • PROD: Take the product.

注解

For more, refer to example. This needs to run in an environment with multiple graphics cards.

Supported Platforms:

Ascend GPU

实际案例

>>> from mindspore.communication import init
>>> from mindspore import Tensor, ops
>>> from mindspore.ops import ReduceOp
>>> import mindspore.nn as nn
>>>
>>> init()
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.allreduce_sum = ops.AllReduce(ReduceOp.SUM, group="nccl_world_group")
...
...     def construct(self, x):
...         return self.allreduce_sum(x)
...
>>> input_ = Tensor(np.ones([2, 8]).astype(np.float32))
>>> net = Net()
>>> output = net(input_)
>>> print(output)
[[4. 5. 6. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0.]]
class tinyms.primitives.ReduceProd(*args, **kwargs)[源代码]

Reduces a dimension of a tensor by multiplying all elements in the dimension, by Default. And also can reduces a dimension of x along the axis. Determine whether the dimensions of the output and input are the same by controlling keep_dims.

参数

keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default : False, don’t keep these reduced dimensions.

Inputs:
  • x (Tensor[Number]) - The input tensor. The dtype of the tensor to be reduced is number. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)).

Outputs:

Tensor, has the same dtype as the x.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the product of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
  • TypeError – If keep_dims is not a bool.

  • TypeError – If x is not a Tensor.

  • ValueError – If axis is not one of the following: int, tuple or list.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> op = ops.ReduceProd(keep_dims=True)
>>> output = op(x, 1)
>>> result = output.shape
>>> print(result)
(3, 1, 5, 6)
>>> # case 1: Reduces a dimension by averaging all elements in the dimension.
>>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]],
...                      [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]],
...                      [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32)
>>> output = op(x)
>>> print(output)
[[[2.2833798e+33]]]
>>> print(output.shape)
(1, 1, 1)
>>> # case 2: Reduces a dimension along axis 0.
>>> output = op(x, 0)
>>> print(output)
[[[ 28.  28.  28.  28.  28.  28.]
  [ 80.  80.  80.  80.  80.  80.]
  [162. 162. 162. 162. 162. 162.]]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = op(x, 1)
>>> print(output)
[[[  6.   6.   6.   6.   6.   6.]]
 [[120. 120. 120. 120. 120. 120.]]
 [[504. 504. 504. 504. 504. 504.]]]
>>> # case 4: Reduces a dimension along axis 2.
>>> output = op(x, 2)
>>> print(output)
[[[1.00000e+00]
  [6.40000e+01]
  [7.29000e+02]]
 [[4.09600e+03]
  [1.56250e+04]
  [4.66560e+04]]
 [[1.17649e+05]
  [2.62144e+05]
  [5.31441e+05]]]
class tinyms.primitives.ReduceScatter(*args, **kwargs)[源代码]

Reduces and scatters tensors from the specified communication group.

注解

The back propagation of the op is not supported yet. Stay tuned for more. The tensors must have the same shape and format in all processes of the collection.

参数
  • op (str) – Specifies an operation used for element-wise reductions, like SUM, MAX, AVG. Default: ReduceOp.SUM.

  • group (str) – The communication group to work on. Default: “hccl_world_group”.

引发
  • TypeError – If any of operation and group is not a string.

  • ValueError – If the first dimension of the input cannot be divided by the rank size.

Supported Platforms:

Ascend GPU

实际案例

>>> # This example should be run with two devices. Refer to the tutorial > Distributed Training on mindspore.cn
>>> from mindspore import Tensor, context
>>> from mindspore.communication import init
>>> from mindspore.ops import ReduceOp
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>> import numpy as np
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.reducescatter = ops.ReduceScatter(ReduceOp.SUM)
...
...     def construct(self, x):
...         return self.reducescatter(x)
...
>>> input_ = Tensor(np.ones([8, 8]).astype(np.float32))
>>> net = Net()
>>> output = net(input_)
>>> print(output)
[[2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]
 [2. 2. 2. 2. 2. 2. 2. 2.]]
class tinyms.primitives.ReduceSum(*args, **kwargs)[源代码]

Reduces a dimension of a tensor by summing all elements in the dimension, by Default. And also can reduces a dimension of x along the axis. Determine whether the dimensions of the output and input are the same by controlling keep_dims.

参数

keep_dims (bool) – If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default: False.

Inputs:
  • x (Tensor[Number]) - The input tensor. The dtype of the tensor to be reduced is number. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

  • axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)).

Outputs:

Tensor, has the same dtype as the x.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the sum of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> op = ops.ReduceSum(keep_dims=True)
>>> output = op(x, 1)
>>> output.shape
(3, 1, 5, 6)
>>> # case 1: Reduces a dimension by averaging all elements in the dimension.
>>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]],
...                      [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]],
...                      [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32)
>>> output = op(x)
>>> print(output)
[[[270.]]]
>>> print(output.shape)
(1, 1, 1)
>>> # case 2: Reduces a dimension along axis 0.
>>> output = op(x, 0)
>>> print(output)
[[[12. 12. 12. 12. 12. 12.]
  [15. 15. 15. 15. 15. 15.]
  [18. 18. 18. 18. 18. 18.]]]
>>> # case 3: Reduces a dimension along axis 1.
>>> output = op(x, 1)
>>> print(output)
[[[ 6.  6.  6.  6.  6.  6.]]
 [[15. 15. 15. 15. 15. 15.]]
 [[24. 24. 24. 24. 24. 24.]]]
>>> # case 4: Reduces a dimension along axis 2.
>>> output = op(x, 2)
>>> print(output)
[[[ 6.]
  [12.]
  [18.]]
 [[24.]
  [30.]
  [36.]]
 [[42.]
  [48.]
  [54.]]]
class tinyms.primitives.RefreshBoxmapTimes(*args, **kwargs)[源代码]

Refresh the box-crossing times of each atom.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数

atom_numbers (int32) – the number of atoms n.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • old_crd (Tensor) - The coordinate of each atom at last update. The data type is float32 and the shape is \((n, 3)\).

  • box_length_inverse (Tensor) - The inverse value of box length in 3 dimensions. The data type is float32 and the shape is \((3,)\).

  • box_map_times (Tensor) - The number of times each atom has crossed the box. The data type is int32 and the shape is \((n, 3)\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.RefreshCrdVel(*args, **kwargs)[源代码]

Refresh the coordinate and velocity of each constrained atom after all iterations have ended.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • dt_inverse (float32) – the inverse value of simulation time step.

  • dt (float32) – the simulation time step.

  • exp_gamma (float32) – constant value exp(gamma * dt).

  • half_exp_gamma_plus_half (float32) – constant value (1 + exp_gamma)/2.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • vel (Tensor) - The velocity of each atom. The data type is float32 and the shape is \((n, 3)\).

  • test_frc (Tensor) - The constraint force calculated in the last iteration. The data type is float32 and the shape is \((n, 3)\).

  • mass_inverse (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

Outputs:
  • res (Tensor) - The return value after updating successfully. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.RefreshUintCrd(*args, **kwargs)[源代码]

Refresh the unsigned coordinate of each constrained atom in each constrain iteration.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数
  • atom_numbers (int32) – the number of atoms n.

  • half_exp_gamma_plus_half (float32) – constant value (1.0 + exp(gamma * dt)) if Langvin-Liu thermostat is used, where gamma is friction coefficient and dt is the simulation time step, 1.0 otherwise.

Inputs:
  • crd (Tensor) - The coordinate of each atom. The data type is float32 and the shape is \((n, 3)\).

  • quarter_cof (Tensor) - The 3-D scale factor. The data type is float32 and the shape is \((3,)\).

  • test_frc (Tensor) - The constraint force. The data type is float32 and the shape is \((n, 3)\).

  • mass_inverse (Tensor) - The inverse value of mass of each atom. The data type is float32 and the shape is \((n,)\).

Outputs:
  • uint_crd (Tensor) - The unsigned int coordinate value of each atom. The data type is uint32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.Reshape(*args, **kwargs)[源代码]

Reshapes the input tensor with the same values based on a given shape tuple.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • input_shape (tuple[int]) - The input tuple is constructed by multiple integers, i.e., \((y_1, y_2, ..., y_S)\). Only constant value is allowed.

Outputs:

Tensor, the shape of tensor is \((y_1, y_2, ..., y_S)\).

引发

ValueError – Given a shape tuple, if it has several -1; or if the product of its elements is less than or equal to 0 or cannot be divided by the product of the input tensor shape; or if it does not match the input’s array size.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> reshape = ops.Reshape()
>>> output = reshape(input_x, (3, 2))
>>> print(output)
[[-0.1  0.3]
 [ 3.6  0.4]
 [ 0.5 -3.2]]
class tinyms.primitives.ResizeBilinear(*args, **kwargs)[源代码]

Resizes an image to a certain size using the bilinear interpolation.

The resizing only affects the lower two dimensions which represent the height and width. The input images can be represented by different data types, but the data types of output images are always float32.

参数
  • size (Union[tuple[int], list[int]]) – A tuple or list of 2 int elements \((new\_height, new\_width)\), the new size of the images.

  • align_corners (bool) – If true, rescale input by \((new\_height - 1) / (height - 1)\), which exactly aligns the 4 corners of images and resized images. If false, rescale by \(new\_height / height\). Default: False.

Inputs:
  • x (Tensor) - Image to be resized. Input images must be a 4-D tensor with shape \((batch, channels, height, width)\), with data type of float32 or float16.

Outputs:

Tensor, resized image. 4-D with shape \((batch, channels, new\_height, new\_width)\), with the same data type as input x.

引发
  • TypeError – If size is neither a tuple nor list.

  • TypeError – If align_corners is not a bool.

  • TypeError – If dtype of x is neither float16 nor float32.

  • TypeError – If x is not a Tensor.

  • ValueError – If length of shape of x is not equal to 4.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> x = Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mindspore.float32)
>>> resize_bilinear = ops.ResizeBilinear((5, 5))
>>> output = resize_bilinear(x)
>>> print(output)
[[[[1. 2. 3. 4. 5.]
   [1. 2. 3. 4. 5.]
   [1. 2. 3. 4. 5.]
   [1. 2. 3. 4. 5.]
   [1. 2. 3. 4. 5.]]]]
class tinyms.primitives.ResizeNearestNeighbor(*args, **kwargs)[源代码]

Resizes the input tensor by using the nearest neighbor algorithm.

Resizes the input tensor to a given size by using the nearest neighbor algorithm. The nearest neighbor algorithm selects the value of the nearest point and does not consider the values of neighboring points at all, yielding a piecewise-constant interpolant.

参数
  • size (Union[tuple, list]) – The target size. The dimension of size must be 2.

  • align_corners (bool) – Whether the centers of the 4 corner pixels of the input and output tensors are aligned. Default: False.

Inputs:
  • input_x (Tensor) - The input tensor. The shape of the tensor is \((N, C, H, W)\).

Outputs:
Tensor, the shape of the output tensor is \((N, C, NEW\_H, NEW\_W)\).

The data type is same as the input_x.

引发
  • TypeError – If size is neither tuple nor list.

  • TypeError – If align_corners is not a bool.

  • ValueError – If length of size is not equal to 2.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_tensor = Tensor(np.array([[[[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]]]), mindspore.float32)
>>> resize = ops.ResizeNearestNeighbor((2, 2))
>>> output = resize(input_tensor)
>>> print(output)
[[[[-0.1  0.3]
   [ 0.4  0.5]]]]
class tinyms.primitives.ReverseSequence(*args, **kwargs)[源代码]

Reverses variable length slices.

参数
  • seq_dim (int) – The dimension where reversal is performed. Required.

  • batch_dim (int) – The input is sliced in this dimension. Default: 0.

Inputs:
  • x (Tensor) - The input to reverse, supporting all number types including bool.

  • seq_lengths (Tensor) - Must be a 1-D vector with int32 or int64 types.

Outputs:

Reversed tensor with the same shape and data type as input.

引发

TypeError – If seq_dim or batch_dim is not an int.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([1, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[1. 2. 3.]
 [5. 4. 6.]
 [9. 8. 7.]]
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([1, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=0, batch_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[1. 5. 9.]
 [4. 2. 6.]
 [7. 8. 3.]]
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([2, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[2. 1. 3.]
 [5. 4. 6.]
 [9. 8. 7.]]
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([3, 2, 3]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[3. 2. 1.]
 [5. 4. 6.]
 [9. 8. 7.]]
>>> x = Tensor(np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), mindspore.float32)
>>> seq_lengths = Tensor(np.array([4, 4]))
>>> reverse_sequence = ops.ReverseSequence(seq_dim=1)
>>> output = reverse_sequence(x, seq_lengths)
>>> print(output)
[[4. 3. 2. 1.]
 [8. 7. 6. 5.]]
class tinyms.primitives.ReverseV2(*args, **kwargs)[源代码]

Reverses specific dimensions of a tensor.

警告

The value range of “axis” is [-dims, dims - 1]. “dims” is the dimension length of “input_x”.

参数

axis (Union[tuple(int), list(int)) – The indices of the dimensions to reverse.

Inputs:
  • input_x (Tensor) - The target tensor. The data type is Number except float64. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If axis is neither list nor tuple.

  • TypeError – If element of axis is not an int.

Supported Platforms:

Ascend GPU

实际案例

>>> input_x = Tensor(np.array([[1, 2, 3, 4], [5, 6, 7, 8]]), mindspore.int32)
>>> op = ops.ReverseV2(axis=[1])
>>> output = op(input_x)
>>> print(output)
[[4 3 2 1]
 [8 7 6 5]]
>>> op = ops.ReverseV2(axis=[1, 0])
>>> output = op(input_x)
>>> print(output)
[[8 7 6 5]
 [4 3 2 1]]
class tinyms.primitives.Rint(*args, **kwargs)[源代码]

Returns an integer that is closest to x element-wise.

Inputs:
  • input_x (Tensor) - The target tensor, which must be one of the following types: float16, float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape and type as input_x.

引发

TypeError – If dtype of input_x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([-1.6, -0.1, 1.5, 2.0]), mindspore.float32)
>>> op = ops.Rint()
>>> output = op(input_x)
>>> print(output)
[-2.  0.  2.  2.]
>>> input_x = Tensor(np.array([[-2.0, -1.9, -1.8, -1.7, -1.6],
...                            [-2.0, -1.9, -1.8, -1.7, -1.6]]), mindspore.float32)
>>> output = op(input_x)
>>> print(output)
[[-2. -2. -2. -2. -2.]
 [-2. -2. -2. -2. -2.]]
class tinyms.primitives.Round(*args, **kwargs)[源代码]

Returns half to even of a tensor element-wise.

\[out_i \approx x_i\]
Inputs:
  • x (Tensor) - The input tensor.

Outputs:

Tensor, has the same shape and type as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([0.8, 1.5, 2.3, 2.5, -4.5]), mindspore.float32)
>>> round = ops.Round()
>>> output = round(x)
>>> print(output)
[ 1.  2.  2.  2. -4.]
class tinyms.primitives.Rsqrt(*args, **kwargs)[源代码]

Computes reciprocal of square root of input tensor element-wise.

\[out_{i} = \frac{1}{\sqrt{x_{i}}}\]
Inputs:
  • x (Tensor) - The input of Rsqrt. Each element must be a non-negative number. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same type and shape as x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> input_tensor = Tensor([[4, 4], [9, 9]], mindspore.float32)
>>> rsqrt = ops.Rsqrt()
>>> output = rsqrt(input_tensor)
>>> print(output)
[[0.5        0.5       ]
 [0.33333334 0.33333334]]
class tinyms.primitives.SGD(*args, **kwargs)[源代码]

Computes the stochastic gradient descent. Momentum is optional.

Nesterov momentum is based on the formula from paper On the importance of initialization and momentum in deep learning.

注解

For more details, please refer to nn.SGD.

参数
  • dampening (float) – The dampening for momentum. Default: 0.0.

  • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0.

  • nesterov (bool) – Enable Nesterov momentum. Default: False.

Inputs:
  • parameters (Tensor) - Parameters to be updated. With float16 or float32 data type.

  • gradient (Tensor) - Gradient, with float16 or float32 data type.

  • learning_rate (Tensor) - Learning rate, a scalar tensor with float16 or float32 data type. e.g. Tensor(0.1, mindspore.float32)

  • accum (Tensor) - Accum(velocity) to be updated. With float16 or float32 data type.

  • momentum (Tensor) - Momentum, a scalar tensor with float16 or float32 data type. e.g. Tensor(0.1, mindspore.float32).

  • stat (Tensor) - States to be updated with the same shape as gradient, with float16 or float32 data type.

Outputs:

Tensor, parameters to be updated.

引发
  • TypeError – If dampening or weight_decay is not a float.

  • TypeError – If nesterov is not a bool.

  • TypeError – If parameters, gradient, learning_rate, accum, momentum or stat is not a Tensor.

  • TypeError – If dtype of parameters, gradient, learning_rate, accum, momentum or stat is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> sgd = ops.SGD()
>>> parameters = Tensor(np.array([2, -0.5, 1.7, 4]), mindspore.float32)
>>> gradient = Tensor(np.array([1, -1, 0.5, 2]), mindspore.float32)
>>> learning_rate = Tensor(0.01, mindspore.float32)
>>> accum = Tensor(np.array([0.1, 0.3, -0.2, -0.1]), mindspore.float32)
>>> momentum = Tensor(0.1, mindspore.float32)
>>> stat = Tensor(np.array([1.5, -0.3, 0.2, -0.7]), mindspore.float32)
>>> output = sgd(parameters, gradient, learning_rate, accum, momentum, stat)
>>> print(output)
(Tensor(shape=[4], dtype=Float32,
 value= [ 1.98989999e+00, -4.90300000e-01,  1.69520009e+00,  3.98009992e+00]),)
class tinyms.primitives.SameTypeShape(*args, **kwargs)[源代码]

Checks whether the data type and shape of two tensors are the same.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • input_y (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_S)\).

Outputs:

Tensor, the shape of tensor is \((x_1, x_2, ..., x_R)\), if data type and shape of input_x and input_y are the same.

引发
  • TypeError – If the data types of input_x and input_y are not the same.

  • ValueError – If the shapes of input_x and input_y are not the same.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
>>> input_y = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
>>> output = ops.SameTypeShape()(input_x, input_y)
>>> print(output)
[[2. 2.]
 [2. 2.]]
class tinyms.primitives.ScalarCast(*args, **kwargs)[源代码]

Casts the input scalar to another type.

Inputs:
  • input_x (scalar) - The input scalar. Only constant value is allowed.

  • input_y (mindspore.dtype) - The type to be cast. Only constant value is allowed.

Outputs:

Scalar. The type is the same as the python type corresponding to input_y.

引发

TypeError – If neither input_x nor input_y is a constant value.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> scalar_cast = ops.ScalarCast()
>>> output = scalar_cast(255.0, mindspore.int32)
>>> print(output)
255
class tinyms.primitives.ScalarSummary(*args, **kwargs)[源代码]

Outputs a scalar to a protocol buffer through a scalar summary operator.

Inputs:
  • name (str) - The name of the input variable, it must not be an empty string.

  • value (Tensor) - The value of scalar, and the shape of value must be [] or [1].

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class SummaryDemo(nn.Cell):
...     def __init__(self,):
...         super(SummaryDemo, self).__init__()
...         self.summary = ops.ScalarSummary()
...         self.add = ops.Add()
...
...     def construct(self, x, y):
...         name = "x"
...         self.summary(name, x)
...         x = self.add(x, y)
...         return x
...
class tinyms.primitives.ScalarToArray(*args, **kwargs)[源代码]

Converts a scalar to a Tensor.

Inputs:
  • input_x (Union[int, float]) - The input is a scalar. Only constant value is allowed.

Outputs:

Tensor. 0-D Tensor and the content is the input.

引发

TypeError – If input_x is neither int nor float.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> op = ops.ScalarToArray()
>>> input_x = 1.0
>>> print(type(input_x))
<class 'float'>
>>> output = op(input_x)
>>> print(type(output))
<class 'mindspore.common.tensor.Tensor'>
>>> print(output)
1.0
class tinyms.primitives.ScalarToTensor(*args, **kwargs)[源代码]

Converts a scalar to a Tensor, and converts the data type to the specified type.

Inputs:
  • input_x (Union[int, float]) - The input is a scalar. Only constant value is allowed.

  • dtype (mindspore.dtype) - The target data type. Default: mindspore.float32. Only constant value is allowed.

Outputs:

Tensor. 0-D Tensor and the content is the input.

引发

TypeError – If input_x is neither int nor float.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> op = ops.ScalarToTensor()
>>> data = 1
>>> output = op(data, mindspore.float32)
>>> print(output)
1.0
class tinyms.primitives.ScatterAdd(*args, **kwargs)[源代码]

Updates the value of the input tensor through the addition operation.

Using given values to update tensor value through the add operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

for each i, …, j in indices.shape:

\[\text{input_x}[\text{indices}[i, ..., j], :] \mathrel{+}= \text{updates}[i, ..., j, :]\]

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

注解

This is an in-place update operator. Therefore, the input_x will be updated after the operation is completed.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: False.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index to do min operation whose data type must be mindspore.int32.

  • updates (Tensor) - The tensor doing the min operation with input_x, the data type is same as input_x, the shape is indices_shape + x_shape[1:].

Outputs:

Tensor, the updated input_x, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape + x_shape[1:].

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.ones([2, 2, 3]), mindspore.float32)
>>> scatter_add = ops.ScatterAdd()
>>> output = scatter_add(input_x, indices, updates)
>>> print(output)
[[1. 1. 1.]
 [3. 3. 3.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [1, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [0.0, 0.0, 0.0] + [1.0, 1.0, 1.0] = [1.0, 1.0, 1.0]
>>> # input_x[1] = [0.0, 0.0, 0.0] + [3.0, 3.0, 3.0] = [3.0, 3.0, 3.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [3.0, 3.0, 3.0] + [7.0, 7.0, 7.0] = [10.0, 10.0, 10.0]
>>> # input_x[1] = [10.0, 10.0, 10.0] + [9.0, 9.0, 9.0] = [19.0, 19.0, 19.0]
>>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_add = ops.ScatterAdd()
>>> output = scatter_add(input_x, indices, updates)
>>> print(output)
[[ 1.  1.  1.]
 [19. 19. 19.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="x")
>>> # for indices = [[1, 0], [1, 1]]
>>> # step 1: [1, 0]
>>> # input_x[0] = [0.0, 0.0, 0.0] + [3.0, 3.0, 3.0] = [3.0, 3.0, 3.0]
>>> # input_x[1] = [0.0, 0.0, 0.0] + [1.0, 1.0, 1.0] = [1.0, 1.0, 1.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [1.0, 1.0, 1.0] + [7.0, 7.0, 7.0] = [8.0, 8.0, 8.0]
>>> # input_x[1] = [8.0, 8.0, 8.0] + [9.0, 9.0, 9.0] = [17.0, 17.0, 17.0]
>>> indices = Tensor(np.array([[1, 0], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_add = ops.ScatterAdd()
>>> output = scatter_add(input_x, indices, updates)
>>> print(output)
[[ 3.  3.  3.]
 [17. 17. 17.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [0, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [0.0, 0.0, 0.0] + [1.0, 1.0, 1.0] = [1.0, 1.0, 1.0]
>>> # input_x[1] = [0.0, 0.0, 0.0] + [3.0, 3.0, 3.0] = [3.0, 3.0, 3.0]
>>> # step 2: [0, 1]
>>> # input_x[0] = [1.0, 1.0, 1.0] + [7.0, 7.0, 7.0] = [8.0, 8.0, 8.0]
>>> # input_x[1] = [3.0, 3.0, 3.0] + [9.0, 9.0, 9.0] = [12.0, 12.0, 12.0]
>>> indices = Tensor(np.array([[0, 1], [0, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_add = ops.ScatterAdd()
>>> output = scatter_add(input_x, indices, updates)
>>> print(output)
[[ 8.  8.  8.]
 [12. 12. 12.]]
class tinyms.primitives.ScatterDiv(*args, **kwargs)[源代码]

Updates the value of the input tensor through the divide operation.

Using given values to update tensor value through the div operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

for each i, …, j in indices.shape:

\[\text{input_x}[\text{indices}[i, ..., j], :] \mathrel{/}= \text{updates}[i, ..., j, :]\]

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: False.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index to do min operation whose data type must be mindspore.int32.

  • updates (Tensor) - The tensor doing the min operation with input_x, the data type is same as input_x, the shape is indices_shape + x_shape[1:].

Outputs:

Tensor, the updated input_x, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape + x_shape[1:].

Supported Platforms:

Ascend CPU

实际案例

>>> input_x = Parameter(Tensor(np.array([[6.0, 6.0, 6.0], [2.0, 2.0, 2.0]]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([0, 1]), mindspore.int32)
>>> updates = Tensor(np.array([[2.0, 2.0, 2.0], [2.0, 2.0, 2.0]]), mindspore.float32)
>>> scatter_div = ops.ScatterDiv()
>>> output = scatter_div(input_x, indices, updates)
>>> print(output)
[[3. 3. 3.]
 [1. 1. 1.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[105.0, 105.0, 105.0],
...                                      [315.0, 315.0, 315.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [1, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [105.0, 105.0, 105.0] / [1.0, 1.0, 1.0] = [105.0, 105.0, 105.0]
>>> # input_x[1] = [315.0, 315.0, 315.0] / [3.0, 3.0, 3.0] = [105.0, 105.0, 105.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [105.0, 105.0, 105.0] / [5.0, 5.0, 5.0] = [21.0, 21.0, 21.0]
>>> # input_x[1] = [21.0, 21.0, 21.0] / [7.0, 7.0, 7.0] = [3.0, 3.0, 3.0]
>>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[5.0, 5.0, 5.0], [7.0, 7.0, 7.0]]]), mindspore.float32)
>>> scatter_div = ops.ScatterDiv()
>>> output = scatter_div(input_x, indices, updates)
>>> print(output)
[[105. 105. 105.]
 [  3.   3.   3.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[105.0, 105.0, 105.0],
...                                      [315.0, 315.0, 315.0]]), mindspore.float32), name="x")
>>> # for indices = [[1, 0], [1, 1]]
>>> # step 1: [1, 0]
>>> # input_x[0] = [105.0, 105.0, 105.0] / [3.0, 3.0, 3.0] = [35.0, 35.0, 35.0]
>>> # input_x[1] = [315.0, 315.0, 315.0] / [1.0, 1.0, 1.0] = [315.0, 315.0, 315.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [315.0, 315.0, 315.0] / [5.0, 5.0, 5.0] = [63.0 63.0 63.0]
>>> # input_x[1] = [63.0 63.0 63.0] / [7.0, 7.0, 7.0] = [9.0, 9.0, 9.0]
>>> indices = Tensor(np.array([[1, 0], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[5.0, 5.0, 5.0], [7.0, 7.0, 7.0]]]), mindspore.float32)
>>> scatter_div = ops.ScatterDiv()
>>> output = scatter_div(input_x, indices, updates)
>>> print(output)
[[35. 35. 35.]
 [ 9.  9.  9.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[105.0, 105.0, 105.0],
...                                      [315.0, 315.0, 315.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [0, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [105.0, 105.0, 105.0] / [1.0, 1.0, 1.0] = [105.0, 105.0, 105.0]
>>> # input_x[1] = [315.0, 315.0, 315.0] / [3.0, 3.0, 3.0] = [105.0, 105.0, 105.0]
>>> # step 2: [0, 1]
>>> # input_x[0] = [105.0, 105.0, 105.0] / [5.0, 5.0, 5.0] = [21.0, 21.0, 21.0]
>>> # input_x[1] = [105.0, 105.0, 105.0] / [7.0, 7.0, 7.0] = [15.0, 15.0, 15.0]
>>> indices = Tensor(np.array([[0, 1], [0, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[5.0, 5.0, 5.0], [7.0, 7.0, 7.0]]]), mindspore.float32)
>>> scatter_div = ops.ScatterDiv()
>>> output = scatter_div(input_x, indices, updates)
>>> print(output)
[[21. 21. 21.]
 [15. 15. 15.]]
class tinyms.primitives.ScatterMax(*args, **kwargs)[源代码]

Updates the value of the input tensor through the maximum operation.

Using given values to update tensor value through the max operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

for each i, …, j in indices.shape:

\[\text{input_x}[\text{indices}[i, ..., j], :] = max(\text{input_x}[\text{indices}[i, ..., j], :], \text{updates}[i, ..., j, :])\]

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: True.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index to do max operation whose data type must be mindspore.int32.

  • updates (Tensor) - The tensor that performs the maximum operation with input_x, the data type is the same as input_x, the shape is indices_shape + x_shape[1:].

Outputs:

Tensor, the updated input_x, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape + x_shape[1:].

Supported Platforms:

Ascend CPU

实际案例

>>> input_x = Parameter(Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32),
...                     name="input_x")
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.ones([2, 2, 3]) * 88, mindspore.float32)
>>> scatter_max = ops.ScatterMax()
>>> output = scatter_max(input_x, indices, updates)
>>> print(output)
[[88. 88. 88.]
 [88. 88. 88.]]
class tinyms.primitives.ScatterMin(*args, **kwargs)[源代码]

Updates the value of the input tensor through the minimum operation.

Using given values to update tensor value through the min operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

for each i, …, j in indices.shape:

\[\text{input_x}[\text{indices}[i, ..., j], :] = min(\text{input_x}[\text{indices}[i, ..., j], :], \text{updates}[i, ..., j, :])\]

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: False.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index to do min operation whose data type must be mindspore.int32.

  • updates (Tensor) - The tensor doing the min operation with input_x, the data type is same as input_x, the shape is indices_shape + x_shape[1:].

Outputs:

Tensor, the updated input_x, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape + x_shape[1:].

Supported Platforms:

Ascend CPU

实际案例

>>> input_x = Parameter(Tensor(np.array([[0.0, 1.0, 2.0], [0.0, 0.0, 0.0]]), mindspore.float32),
...                     name="input_x")
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
>>> update = Tensor(np.ones([2, 2, 3]), mindspore.float32)
>>> scatter_min = ops.ScatterMin()
>>> output = scatter_min(input_x, indices, update)
>>> print(output)
[[0. 1. 1.]
 [0. 0. 0.]]
class tinyms.primitives.ScatterMul(*args, **kwargs)[源代码]

Updates the value of the input tensor through the multiply operation.

Using given values to update tensor value through the mul operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

for each i, …, j in indices.shape:

\[\text{input_x}[\text{indices}[i, ..., j], :] \mathrel{*}= \text{updates}[i, ..., j, :]\]

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: False.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index to do min operation whose data type must be mindspore.int32.

  • updates (Tensor) - The tensor doing the min operation with input_x, the data type is same as input_x, the shape is indices_shape + x_shape[1:].

Outputs:

Tensor, the updated input_x, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape + x_shape[1:].

Supported Platforms:

Ascend CPU

实际案例

>>> input_x = Parameter(Tensor(np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([0, 1]), mindspore.int32)
>>> updates = Tensor(np.array([[2.0, 2.0, 2.0], [2.0, 2.0, 2.0]]), mindspore.float32)
>>> scatter_mul = ops.ScatterMul()
>>> output = scatter_mul(input_x, indices, updates)
>>> print(output)
[[2. 2. 2.]
 [4. 4. 4.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [1, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [1.0, 1.0, 1.0] * [1.0, 1.0, 1.0] = [1.0, 1.0, 1.0]
>>> # input_x[1] = [2.0, 2.0, 2.0] * [3.0, 3.0, 3.0] = [6.0, 6.0, 6.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [6.0, 6.0, 6.0] * [7.0, 7.0, 7.0] = [42.0, 42.0, 42.0]
>>> # input_x[1] = [42.0, 42.0, 42.0] * [9.0, 9.0, 9.0] = [378.0, 378.0, 378.0]
>>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_mul = ops.ScatterMul()
>>> output = scatter_mul(input_x, indices, updates)
>>> print(output)
[[  1.   1.   1.]
 [378. 378. 378.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]), mindspore.float32), name="x")
>>> # for indices = [[1, 0], [1, 1]]
>>> # step 1: [1, 0]
>>> # input_x[0] = [1.0, 1.0, 1.0] * [3.0, 3.0, 3.0] = [3.0, 3.0, 3.0]
>>> # input_x[1] = [2.0, 2.0, 2.0] * [1.0, 1.0, 1.0] = [2.0, 2.0, 2.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [2.0, 2.0, 2.0] * [7.0, 7.0, 7.0] = [14.0, 14.0, 14.0]
>>> # input_x[1] = [14.0, 14.0, 14.0] * [9.0, 9.0, 9.0] = [126.0, 126.0, 126.0]
>>> indices = Tensor(np.array([[1, 0], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_mul = ops.ScatterMul()
>>> output = scatter_mul(input_x, indices, updates)
>>> print(output)
[[  3.   3.   3.]
 [126. 126. 126.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [0, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [1.0, 1.0, 1.0] * [1.0, 1.0, 1.0] = [1.0, 1.0, 1.0]
>>> # input_x[1] = [2.0, 2.0, 2.0] * [3.0, 3.0, 3.0] = [6.0, 6.0, 6.0]
>>> # step 2: [0, 1]
>>> # input_x[0] = [1.0, 1.0, 1.0] * [7.0, 7.0, 7.0] = [7.0, 7.0, 7.0]
>>> # input_x[1] = [6.0, 6.0, 6.0] * [9.0, 9.0, 9.0] = [54.0, 54.0, 54.0]
>>> indices = Tensor(np.array([[0, 1], [0, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_mul = ops.ScatterMul()
>>> output = scatter_mul(input_x, indices, updates)
>>> print(output)
[[ 7.  7.  7.]
 [54. 54. 54.]]
class tinyms.primitives.ScatterNd(*args, **kwargs)[源代码]

Scatters a tensor into a new tensor depending on the specified indices.

Creates an empty tensor with the given shape, and set values by scattering the update tensor depending on indices.

The empty tensor has rank P and indices has rank Q where Q >= 2.

indices has shape \((i_0, i_1, ..., i_{Q-2}, N)\) where N <= P.

The last dimension of indices (with length N ) indicates slices along the N th dimension of the empty tensor.

updates is a tensor of rank Q-1+P-N. Its shape is: \((i_0, i_1, ..., i_{Q-2}, shape_N, ..., shape_{P-1})\).

The following figure shows the calculation process of inserting two slices in the first dimension of a rank-3 with two matrices of new values:

tinyms/api_img/ScatterNd.png
Inputs:
  • indices (Tensor) - The index of scattering in the new tensor with int32 or int64 data type. The rank of indices must be at least 2 and indices_shape[-1] <= len(shape).

  • updates (Tensor) - The source Tensor to be scattered. It has shape indices_shape[:-1] + shape[indices_shape[-1]:].

  • shape (tuple[int]) - Define the shape of the output tensor, has the same data type as indices. The shape of shape is \((x_1, x_2, ..., x_R)\), and length of ‘shape’ is greater than or equal 2. In other words, the shape of shape is at least \((x_1, x_2)\). And the value of any element in shape must be greater than or equal 1. In other words, \(x_1\) >= 1, \(x_2\) >= 1.

Outputs:

Tensor, the new tensor, has the same type as update and the same shape as shape.

引发
  • TypeError – If shape is not a tuple.

  • ValueError – If any element of shape is less than 1.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> op = ops.ScatterNd()
>>> indices = Tensor(np.array([[0], [2]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2],
...                             [3, 3, 3, 3], [4, 4, 4, 4]],
...                            [[1, 1, 1, 1], [2, 2, 2, 2],
...                             [3, 3, 3, 3], [4, 4, 4, 4]]]), mindspore.float32)
>>> shape = (4, 4, 4)
>>> output = op(indices, updates, shape)
>>> print(output)
[[[1. 1. 1. 1.]
  [2. 2. 2. 2.]
  [3. 3. 3. 3.]
  [4. 4. 4. 4.]]
 [[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]
 [[1. 1. 1. 1.]
  [2. 2. 2. 2.]
  [3. 3. 3. 3.]
  [4. 4. 4. 4.]]
 [[0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]
  [0. 0. 0. 0.]]]
>>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([3.2, 1.1]), mindspore.float32)
>>> shape = (3, 3)
>>> output = op(indices, updates, shape)
>>> # In order to facilitate understanding, explain the operator pseudo-operation process step by step:
>>> # Step 1: Generate an empty Tensor of the specified shape according to the shape
>>> # [
>>> #     [0. 0. 0.]
>>> #     [0. 0. 0.]
>>> #     [0. 0. 0.]
>>> # ]
>>> # Step 2: Modify the data at the specified location according to the indicators
>>> # 0th row of indices is [0, 1], 0th row of updates is 3.2.
>>> # means that the empty tensor in the 0th row and 1st col set to 3.2
>>> # [
>>> #     [0. 3.2. 0.]
>>> #     [0. 0.   0.]
>>> #     [0. 0.   0.]
>>> # ]
>>> # 1th row of indices is [1, 1], 1th row of updates is 1.1.
>>> # means that the empty tensor in the 1th row and 1st col set to 1.1
>>> # [
>>> #     [0. 3.2. 0.]
>>> #     [0. 1.1  0.]
>>> #     [0. 0.   0.]
>>> # ]
>>> # The final result is as follows:
>>> print(output)
[[0. 3.2 0.]
 [0. 1.1 0.]
 [0. 0.  0.]]
class tinyms.primitives.ScatterNdAdd(*args, **kwargs)[源代码]

Applies sparse addition to individual values or slices in a tensor.

Using given values to update tensor value through the add operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

input_x has rank P and indices has rank Q where Q >= 2.

indices has shape \((i_0, i_1, ..., i_{Q-2}, N)\) where N <= P.

The last dimension of indices (with length N ) indicates slices along the N th dimension of input_x.

updates is a tensor of rank Q-1+P-N. Its shape is: \((i_0, i_1, ..., i_{Q-2}, x\_shape_N, ..., x\_shape_{P-1})\).

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: False.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index to do min operation whose data type must be mindspore.int32. The rank of indices must be at least 2 and indices_shape[-1] <= len(shape).

  • updates (Tensor) - The tensor doing the min operation with input_x, the data type is same as input_x, the shape is indices_shape[:-1] + x_shape[indices_shape[-1]:].

Outputs:

Tensor, the updated input_x, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape[:-1] + x_shape[indices_shape[-1]:].

Supported Platforms:

Ascend GPU

实际案例

>>> input_x = Parameter(Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([[2], [4], [1], [7]]), mindspore.int32)
>>> updates = Tensor(np.array([6, 7, 8, 9]), mindspore.float32)
>>> scatter_nd_add = ops.ScatterNdAdd()
>>> output = scatter_nd_add(input_x, indices, updates)
>>> print(output)
[ 1. 10.  9.  4. 12.  6.  7. 17.]
>>> input_x = Parameter(Tensor(np.zeros((4, 4, 4)), mindspore.int32))
>>> indices = Tensor(np.array([[0], [2]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],
...                            [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]]]), mindspore.int32)
>>> scatter_nd_add = ops.ScatterNdAdd()
>>> output = scatter_nd_add(input_x, indices, updates)
>>> print(output)
[[[1 1 1 1]
  [2 2 2 2]
  [3 3 3 3]
  [4 4 4 4]]
 [[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]
 [[5 5 5 5]
  [6 6 6 6]
  [7 7 7 7]
  [8 8 8 8]]
 [[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]]
class tinyms.primitives.ScatterNdSub(*args, **kwargs)[源代码]

Applies sparse subtraction to individual values or slices in a tensor.

Using given values to update tensor value through the subtraction operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

input_x has rank P and indices has rank Q where Q >= 2.

indices has shape \((i_0, i_1, ..., i_{Q-2}, N)\) where N <= P.

The last dimension of indices (with length N ) indicates slices along the N th dimension of input_x.

updates is a tensor of rank Q-1+P-N. Its shape is: \((i_0, i_1, ..., i_{Q-2}, x\_shape_N, ..., x\_shape_{P-1})\).

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: False.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index of input tensor, with int32 data type. The rank of indices must be at least 2 and indices_shape[-1] <= len(shape).

  • updates (Tensor) - The tensor to be updated to the input tensor, has the same type as input. The shape is indices_shape[:-1] + x_shape[indices_shape[-1]:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape[:-1] + x_shape[indices_shape[-1]:].

Supported Platforms:

Ascend GPU

实际案例

>>> input_x = Parameter(Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([[2], [4], [1], [7]]), mindspore.int32)
>>> updates = Tensor(np.array([6, 7, 8, 9]), mindspore.float32)
>>> scatter_nd_sub = ops.ScatterNdSub()
>>> output = scatter_nd_sub(input_x, indices, updates)
>>> print(output)
[ 1. -6. -3.  4. -2.  6.  7. -1.]
>>> input_x = Parameter(Tensor(np.zeros((4, 4, 4)), mindspore.int32))
>>> indices = Tensor(np.array([[0], [2]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],
...                            [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]]]), mindspore.int32)
>>> scatter_nd_sub = ops.ScatterNdSub()
>>> output = scatter_nd_sub(input_x, indices, updates)
>>> print(output)
[[[-1 -1 -1 -1]
  [-2 -2 -2 -2]
  [-3 -3 -3 -3]
  [-4 -4 -4 -4]]
 [[ 0  0  0  0]
  [ 0  0  0  0]
  [ 0  0  0  0]
  [ 0  0  0  0]]
 [[-5 -5 -5 -5]
  [-6 -6 -6 -6]
  [-7 -7 -7 -7]
  [-8 -8 -8 -8]]
 [[ 0  0  0  0]
  [ 0  0  0  0]
  [ 0  0  0  0]
  [ 0  0  0  0]]]
class tinyms.primitives.ScatterNdUpdate(*args, **kwargs)[源代码]

Updates tensor values by using input indices and value.

Using given values to update tensor value, along with the input indices.

input_x has rank P and indices has rank Q where Q >= 2.

indices has shape \((i_0, i_1, ..., i_{Q-2}, N)\) where N <= P.

The last dimension of indices (with length N ) indicates slices along the N th dimension of input_x.

updates is a tensor of rank Q-1+P-N. Its shape is: \((i_0, i_1, ..., i_{Q-2}, x\_shape_N, ..., x\_shape_{P-1})\).

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: True.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index of input tensor, with int32 data type.

  • updates (Tensor) - The tensor to be updated to the input tensor, has the same type as input. The shape is indices_shape[:-1] + x_shape[indices_shape[-1]:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> np_x = np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]])
>>> input_x = mindspore.Parameter(Tensor(np_x, mindspore.float32), name="x")
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> op = ops.ScatterNdUpdate()
>>> output = op(input_x, indices, updates)
>>> print(output)
[[1.   0.3   3.6]
 [0.4  2.2  -3.2]]
class tinyms.primitives.ScatterNonAliasingAdd(*args, **kwargs)[源代码]

Applies sparse addition to the input using individual values or slices.

Using given values to update tensor value through the add operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

Inputs:
  • input_x (Parameter) - The target parameter. The data type must be float16, float32 or int32.

  • indices (Tensor) - The index to perform the addition operation whose data type must be mindspore.int32.

  • updates (Tensor) - The tensor that performs the addition operation with input_x, the data type is the same as input_x, the shape is indices_shape[:-1] + x_shape[indices_shape[-1]:].

Outputs:

Parameter, the updated input_x.

引发
  • TypeError – If dtype of indices is not int32.

  • TypeError – If dtype of input_x is not one of float16, float32, int32.

  • ValueError – If the shape of updates is not equal to indices_shape[:-1] + x_shape[indices_shape[-1]:].

Supported Platforms:

Ascend

实际案例

>>> input_x = Parameter(Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([[2], [4], [1], [7]]), mindspore.int32)
>>> updates = Tensor(np.array([6, 7, 8, 9]), mindspore.float32)
>>> scatter_non_aliasing_add = ops.ScatterNonAliasingAdd()
>>> output = scatter_non_aliasing_add(input_x, indices, updates)
>>> print(output)
[ 1. 10.  9.  4. 12.  6.  7. 17.]
class tinyms.primitives.ScatterSub(*args, **kwargs)[源代码]

Updates the value of the input tensor through the subtraction operation.

Using given values to update tensor value through the subtraction operation, along with the input indices. This operation outputs the input_x after the update is done, which makes it convenient to use the updated value.

for each i, …, j in indices.shape:

\[\text{input_x}[\text{indices}[i, ..., j], :] \mathrel{-}= \text{updates}[i, ..., j, :]\]

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: False.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index to do min operation whose data type must be mindspore.int32.

  • updates (Tensor) - The tensor doing the min operation with input_x, the data type is same as input_x, the shape is indices_shape + x_shape[1:].

Outputs:

Tensor, the updated input_x, has the same shape and type as input_x.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If indices is not an int32.

  • ValueError – If the shape of updates is not equal to indices_shape + x_shape[1:].

Supported Platforms:

Ascend CPU GPU

实际案例

>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([[0, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]]]), mindspore.float32)
>>> scatter_sub = ops.ScatterSub()
>>> output = scatter_sub(input_x, indices, updates)
>>> print(output)
[[-1. -1. -1.]
 [-1. -1. -1.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [1, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [0.0, 0.0, 0.0] - [1.0, 1.0, 1.0] = [-1.0, -1.0, -1.0]
>>> # input_x[1] = [0.0, 0.0, 0.0] - [3.0, 3.0, 3.0] = [-3.0, -3.0, -3.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [-3.0, -3.0, -3.0] - [7.0, 7.0, 7.0] = [-10.0, -10.0, -10.0]
>>> # input_x[1] = [-10.0, -10.0, -10.0] - [9.0, 9.0, 9.0] = [-19.0, -19.0, -19.0]
>>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_sub = ops.ScatterSub()
>>> output = scatter_sub(input_x, indices, updates)
>>> print(output)
[[ -1.  -1.  -1.]
 [-19. -19. -19.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="x")
>>> # for indices = [[1, 0], [1, 1]]
>>> # step 1: [1, 0]
>>> # input_x[0] = [0.0, 0.0, 0.0] - [3.0, 3.0, 3.0] = [-3.0, -3.0, -3.0]
>>> # input_x[1] = [0.0, 0.0, 0.0] - [1.0, 1.0, 1.0] = [-1.0, -1.0, -1.0]
>>> # step 2: [1, 1]
>>> # input_x[1] = [-1.0, -1.0, -1.0] - [7.0, 7.0, 7.0] = [-8.0, -8.0, -8.0]
>>> # input_x[1] = [-8.0, -8.0, -8.0] - [9.0, 9.0, 9.0] = [-17.0, -17.0, -17.0]
>>> indices = Tensor(np.array([[1, 0], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_sub = ops.ScatterSub()
>>> output = scatter_sub(input_x, indices, updates)
>>> print(output)
[[ -3.  -3.  -3.]
 [-17. -17. -17.]]
>>> # for input_x will be updated after the operation is completed. input_x need to be re-initialized.
>>> input_x = Parameter(Tensor(np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]), mindspore.float32), name="x")
>>> # for indices = [[0, 1], [0, 1]]
>>> # step 1: [0, 1]
>>> # input_x[0] = [0.0, 0.0, 0.0] - [1.0, 1.0, 1.0] = [-1.0, -1.0, -1.0]
>>> # input_x[1] = [0.0, 0.0, 0.0] - [3.0, 3.0, 3.0] = [-3.0, -3.0, -3.0]
>>> # step 2: [0, 1]
>>> # input_x[0] = [-1.0, -1.0, -1.0] - [7.0, 7.0, 7.0] = [-8.0, -8.0, -8.0]
>>> # input_x[1] = [-3.0, -3.0, -3.0] - [9.0, 9.0, 9.0] = [-12.0, -12.0, -12.0]
>>> indices = Tensor(np.array([[0, 1], [0, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1.0, 1.0, 1.0], [3.0, 3.0, 3.0]],
...                            [[7.0, 7.0, 7.0], [9.0, 9.0, 9.0]]]), mindspore.float32)
>>> scatter_sub = ops.ScatterSub()
>>> output = scatter_sub(input_x, indices, updates)
>>> print(output)
[[ -8.  -8.  -8.]
 [-12. -12. -12.]]
class tinyms.primitives.ScatterUpdate(*args, **kwargs)[源代码]

Updates tensor values by using input indices and value.

Using given values to update tensor value, along with the input indices.

for each i, …, j in indices.shape:

\[\text{input_x}[\text{indices}[i, ..., j], :] = \text{updates}[i, ..., j, :]\]

Inputs of input_x and updates comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – Whether protect the assignment by a lock. Default: True.

Inputs:
  • input_x (Parameter) - The target tensor, with data type of Parameter. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index of input tensor. With int32 data type. If there are duplicates in indices, the order for updating is undefined.

  • updates (Tensor) - The tensor to update the input tensor, has the same type as input, and updates.shape = indices.shape + input_x.shape[1:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> np_x = np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]])
>>> input_x = mindspore.Parameter(Tensor(np_x, mindspore.float32), name="x")
>>> indices = Tensor(np.array([0, 1]), mindspore.int32)
>>> np_updates = np.array([[2.0, 1.2, 1.0], [3.0, 1.2, 1.0]])
>>> updates = Tensor(np_updates, mindspore.float32)
>>> op = ops.ScatterUpdate()
>>> output = op(input_x, indices, updates)
>>> print(output)
[[2. 1.2  1.]
 [3. 1.2  1.]]
class tinyms.primitives.SeLU(*args, **kwargs)[源代码]

Computes SeLU (scaled exponential Linear Unit) of input tensors element-wise.

The activation function is defined as:

\[E_{i} = scale * \begin{cases} x_{i}, &\text{if } x_{i} \geq 0; \cr \text{alpha} * (\exp(x_i) - 1), &\text{otherwise.} \end{cases}\]

where \(alpha\) and \(scale\) are pre-defined constants(\(alpha=1.67326324\) and \(scale=1.05070098\)).

See more details in Self-Normalizing Neural Networks.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the input_x.

Supported Platforms:

Ascend

引发

TypeError – If dtype of input_x is neither float16 nor float32.

实际案例

>>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> selu = ops.SeLU()
>>> output = selu(input_x)
>>> print(output)
[[-1.1113307 4.202804 -1.7575096]
[ 2.101402 -1.7462534 9.456309 ]]
class tinyms.primitives.SearchSorted(*args, **kwargs)[源代码]

Find the indices from the innermost dimension of sequence such that the order of the innermost dimension within sequence would be preserved when the corresponding values in values were inserted before the indices.

参数
  • out_int32 (bool) – Output datatype. Optional. If True, the output datatype will be int32; if False, the output datatype will be int64. Default is False.

  • right (bool) – Search Strategy. Optional. If True, return the last suitable index found. If False, return the first such index. Default is False.

Inputs:
  • sequence (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R-1, x_R)\) or (x_1).

    It must contain monitonically increasing sequence on the innermost dimension.

  • values (Tensor) - The shape of tensor is : math:(x_1, x_2, …, x_R-1, x_S).

Outputs:

Tensor containing the indices from the innermost dimension of the input sequence such that, if insert the corresponding value in the values tensor, the order of the tensor sequence would be preserved. The shape of tensor is \((x_1, x_2, ..., x_R-1, x_S)\), whose datatype is int32 if out_int32 is True, otherwise int64, and shape is the same as the shape of values.

引发

ValueError – If sequence and values do not have proper shapes.

Supported Platforms:

CPU

实际案例

>>> sequence = Tensor(np.array([[0, 1, 3, 5, 7], [2, 4, 6, 8, 10]]), mindspore.float32)
>>> values = Tensor(np.array([[3, 6, 9], [3, 6, 9]]), mindspore.float32)
>>> output = ops.SearchSorted()(sequence, values)
>>> print(output)
[[2, 4, 5]
 [1, 2, 4]]
class tinyms.primitives.Select(*args, **kwargs)[源代码]

Returns the selected elements, either from input \(x\) or input \(y\), depending on the condition.

Given a tensor as input, this operation inserts a dimension of 1 at the dimension, it was invalid when both math: ‘x’ and math: ‘y’ are none. Keep in mind that the shape of the output tensor can vary depending on how many true values are in the input. Indexes are output in row-first order.

The conditional tensor acts as an optional compensation (mask), which determines whether the corresponding element / row in the output must be selected from \(x\) (if true) or \(y\) (if false) based on the value of each element.

It can be defined as:

\[\begin{split}out_i = \begin{cases} x_i, & \text{if } condition_i \\ y_i, & \text{otherwise} \end{cases}\end{split}\]

If condition is a vector, then \(x\) and \(y\) are higher-dimensional matrices, then it chooses to copy that row (external dimensions) from \(x\) and \(y\). If condition has the same shape as \(x\) and \(y\), you can choose to copy these elements from \(x\) and \(y\).

Inputs:
  • input_cond (Tensor[bool]) - The shape is \((x_1, x_2, ..., x_N, ..., x_R)\). The condition tensor, decides which element is chosen.

  • input_x (Tensor) - The shape is \((x_1, x_2, ..., x_N, ..., x_R)\). The first input tensor.

  • input_y (Tensor) - The shape is \((x_1, x_2, ..., x_N, ..., x_R)\). The second input tensor.

Outputs:

Tensor, has the same shape as input_x. The shape is \((x_1, x_2, ..., x_N, ..., x_R)\).

引发
  • TypeError – If input_x or input_y is not a Tensor.

  • ValueError – If shape of input_x is not equal to shape of input_y or shape of input_cond.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> select = ops.Select()
>>> input_cond = Tensor([True, False])
>>> input_x = Tensor([2,3], mindspore.float32)
>>> input_y = Tensor([1,2], mindspore.float32)
>>> output = select(input_cond, input_x, input_y)
>>> print(output)
[2. 2.]
class tinyms.primitives.Shape(*args, **kwargs)[源代码]

Returns the shape of the input tensor. And it used to be static shape.

static shape: A shape that can be obtained without running the graph. It is an inherent property of tensor and may be unknown. The static shape information can be completed by artificial setting. No matter what the input of the graph is, the static shape is not affected.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

tuple[int], the output tuple is constructed by multiple integers, \((x_1, x_2, ..., x_R)\).

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
>>> shape = ops.Shape()
>>> output = shape(input_x)
>>> print(output)
(3, 2, 1)
class tinyms.primitives.Sigmoid(*args, **kwargs)[源代码]

Sigmoid activation function.

Computes Sigmoid of input element-wise. The Sigmoid function is defined as:

\[\text{sigmoid}(x_i) = \frac{1}{1 + \exp(-x_i)},\]

where \(x_i\) is an element of the input Tensor.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the input_x.

引发
  • TypeError – If dtype of input_x is neither float16 nor float32.

  • TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32)
>>> sigmoid = ops.Sigmoid()
>>> output = sigmoid(input_x)
>>> print(output)
[0.7310586  0.880797   0.95257413 0.98201376 0.9933072 ]
class tinyms.primitives.SigmoidCrossEntropyWithLogits(*args, **kwargs)[源代码]

Uses the given logits to compute sigmoid cross entropy between the logits and the label.

Measures the distribution error in discrete classification tasks where each class is independent and not mutually exclusive using cross entropy loss.

Sets input logits as \(X\), input label as \(Y\), output as \(loss\). Then,

\[\begin{split}\begin{array}{ll} \\ p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}} \\ loss_{ij} = -[Y_{ij} * ln(p_{ij}) + (1 - Y_{ij})ln(1 - p_{ij})] \end{array}\end{split}\]
Inputs:
  • logits (Tensor) - Input logits. Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.

  • label (Tensor) - Ground truth label. With the same shape and type as logits.

Outputs:

Tensor, with the same shape and type as input logits.

引发

TypeError – If logits or label is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> logits = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
>>> labels = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32))
>>> sigmoid = ops.SigmoidCrossEntropyWithLogits()
>>> output = sigmoid(logits, labels)
>>> print(output)
[[ 0.6111007   0.5032824   0.26318604]
 [ 0.58439666  0.5530153  -0.4368139 ]]
class tinyms.primitives.Sign(*args, **kwargs)[源代码]

Performs sign on the tensor element-wise.

\[sign(x) = \begin{cases} -1, &if\ x < 0 \cr 0, &if\ x = 0 \cr 1, &if\ x > 0\end{cases}\]
Inputs:
  • x (Tensor) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape and type as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> x = Tensor(np.array([[2.0, 0.0, -1.0]]), mindspore.float32)
>>> sign = ops.Sign()
>>> output = sign(x)
>>> print(output)
[[ 1.  0. -1.]]
class tinyms.primitives.Sin(*args, **kwargs)[源代码]

Computes sine of the input element-wise.

\[out_i = sin(x_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is

    \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> sin = ops.Sin()
>>> x = Tensor(np.array([0.62, 0.28, 0.43, 0.62]), mindspore.float32)
>>> output = sin(x)
>>> print(output)
[0.5810352  0.27635565 0.41687083 0.5810352 ]
class tinyms.primitives.Sinh(*args, **kwargs)[源代码]

Computes hyperbolic sine of the input element-wise.

\[out_i = \sinh(input_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape as x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend CPU

实际案例

>>> sinh = ops.Sinh()
>>> x = Tensor(np.array([0.62, 0.28, 0.43, 0.62]), mindspore.float32)
>>> output = sinh(x)
>>> print(output)
[0.6604918  0.28367308 0.44337422 0.6604918 ]
class tinyms.primitives.Size(*args, **kwargs)[源代码]

Returns the size of a tensor.

Returns an int scalar representing the elements size of input, the total number of elements in the tensor.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The data type is Number.

Outputs:

int. A scalar representing the elements size of input_x, tensor is the number of elements in a tensor, \(size=x_1*x_2*...x_R\). The data type is an int.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
>>> size = ops.Size()
>>> output = size(input_x)
>>> print(output)
4
class tinyms.primitives.Slice(*args, **kwargs)[源代码]

Slices a tensor in the specified shape.

Slice the tensor input_x in shape of size and starting at the location specified by begin, The slice begin represents the offset in each dimension of input_x, The slice size represents the size of the output tensor.

Note that begin is zero-based and size is one-based.

If size[i] is -1, all remaining elements in dimension i are included in the slice. This is equivalent to setting \(size[i] = input_x.shape(i) - begin[i]\).

Inputs:
  • input_x (Tensor): The target tensor. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

  • begin (Union[tuple, list]): The beginning of the slice. Only constant value(>=0) is allowed.

  • size (Union[tuple, list]): The size of the slice. Only constant value is allowed.

Outputs:

Tensor, the shape is : input size, the data type is the same as input_x.

引发

TypeError – If begin or size is neither tuple nor list.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> data = Tensor(np.array([[[1, 1, 1], [2, 2, 2]],
...                         [[3, 3, 3], [4, 4, 4]],
...                         [[5, 5, 5], [6, 6, 6]]]).astype(np.int32))
>>> slice_op = ops.Slice()
>>> output = slice_op(data, (1, 0, 0), (1, 1, 3))
>>> print(output)
[[[3 3 3]]]
>>> output = slice_op(data, (1, 0, 0), (1, 1, 2))
>>> print(output)
[[[3 3]]]
>>> output = slice_op(data, (1, 0, 0), (1, 1, 1))
>>> print(output)
[[[3]]]
>>> output = slice_op(data, (1, 1, 0), (1, 1, 3))
>>> print(output)
[[[4 4 4]]]
>>> output = slice_op(data, (1, 0, 1), (1, 1, 2))
>>> print(output)
[[[3 3]]]
class tinyms.primitives.SmoothL1Loss(*args, **kwargs)[源代码]

Computes smooth L1 loss, a robust L1 loss.

SmoothL1Loss is a Loss similar to MSELoss but less sensitive to outliers as described in the Fast R-CNN by Ross Girshick.

Given two input \(x,\ y\) of length \(N\), the unreduced SmoothL1Loss can be described as follows:

\[\begin{split}L_{i} = \begin{cases} \frac{0.5 (x_i - y_i)^{2}}{\text{beta}}, & \text{if } |x_i - y_i| < \text{beta} \\ |x_i - y_i| - 0.5 \text{beta}, & \text{otherwise. } \end{cases}\end{split}\]

Here \(\text{beta}\) controls the point where the loss function changes from quadratic to linear. Its default value is 1.0. \(N\) is the batch size. This function returns an unreduced loss Tensor.

警告

This operator does not perform the “reduce” operation on the loss value. Call other reduce operators to perform “reduce” operation on the loss if required.

参数

beta (float) – A parameter used to control the point where the function will change from quadratic to linear. Default: 1.0.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions. Data type must be float16 or float32.

  • labels (Tensor) - Ground truth data, tensor of shape \((N, *)\), same shape and dtype as the logits.

Outputs:

Tensor, loss float tensor, same shape and dtype as the logits.

引发
  • TypeError – If beta is not a float.

  • TypeError – If dtype of logits or labels is neither float16 not float32.

  • ValueError – If beta is less than or equal to 0.

  • ValueError – If shape of logits is not the same as labels.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> loss = ops.SmoothL1Loss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[0.  0.  0.5]
class tinyms.primitives.SoftMarginLoss(*args, **kwargs)[源代码]

SoftMarginLoss operation.

Creates a criterion that optimizes a two-class classification logistic loss between input tensor \(x\) and target tensor \(y\) (containing 1 or -1).

\[\text{loss}(x, y) = \sum_i \frac{\log(1 + \exp(-y[i]*x[i]))}{\text{x.nelement}()}\]
参数

reduction (str) – Apply specific reduction method to the output: ‘none’, ‘mean’, ‘sum’. Default: “mean”.

Inputs:
  • logits (Tensor) - Predict data. Data type must be float16 or float32.

  • labels (Tensor) - Ground truth data, with the same type and shape as logits.

Outputs:

Tensor or Scalar, if reduction is “none”, its shape is the same as logits. Otherwise, a scalar value will be returned.

引发
  • TypeError – If logits or labels is not a Tensor.

  • TypeError – If dtype of logits or labels is neither float16 nor float32.

  • ValueError – If shape of logits is not the same as labels.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend

实际案例

>>> loss = ops.SoftMarginLoss()
>>> logits = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
>>> labels = Tensor(np.array([[-1, 1], [1, -1]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.6764238
class tinyms.primitives.SoftShrink(*args, **kwargs)[源代码]

Applies the soft shrinkage function elementwise.

\[\begin{split}\text{SoftShrink}(x) = \begin{cases} x - \lambda, & \text{ if } x > \lambda \\ x + \lambda, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases}\end{split}\]
参数

lambd – the \(\lambda\) must be no less than zero value for the Softshrink formulation. Default: 0.5.

Inputs:
  • input_x (Tensor) - The input of SoftShrink with data type of float16 or float32. Any number of additional dimensions.

Outputs:

Tensor, has the same shape and data type as input_x.

引发
  • TypeError – If lambd is not a float.

  • TypeError – If input_x is not a Tensor.

  • TypeError – If dtype of input_x is neither float16 nor float32.

  • ValueError – If lambd is less than 0.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([[ 0.5297,  0.7871,  1.1754], [ 0.7836,  0.6218, -1.1542]]), mindspore.float16)
>>> softshrink = ops.SoftShrink()
>>> output = softshrink(input_x)
>>> print(output)
[[ 0.02979  0.287    0.676  ]
 [ 0.2837   0.1216  -0.6543 ]]
class tinyms.primitives.Softmax(*args, **kwargs)[源代码]

Softmax operation.

Applies the Softmax operation to the input tensor on the specified axis. Supposes a slice in the given aixs \(x\), then for each element \(x_i\), the Softmax function is shown as follows:

\[\text{output}(x_i) = \frac{exp(x_i)}{\sum_{j = 0}^{N-1}\exp(x_j)},\]

where \(N\) is the length of the tensor.

参数

axis (Union[int, tuple]) – The axis to perform the Softmax operation. Default: -1.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the logits.

引发
  • TypeError – If axis is neither an int nor a tuple.

  • TypeError – If dtype of logits is neither float16 nor float32.

  • ValueError – If axis is a tuple whose length is less than 1.

  • ValueError – If axis is a tuple whose elements are not all in range [-len(logits.shape), len(logits.shape)).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> logits = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32)
>>> softmax = ops.Softmax()
>>> output = softmax(logits)
>>> print(output)
[0.01165623 0.03168492 0.08612854 0.23412167 0.6364086 ]
class tinyms.primitives.SoftmaxCrossEntropyWithLogits(*args, **kwargs)[源代码]

Gets the softmax cross-entropy value between logits and labels with one-hot encoding.

The updating formulas of SoftmaxCrossEntropyWithLogits algorithm are as follows,

\[\begin{split}\begin{array}{ll} \\ p_{ij} = softmax(X_{ij}) = \frac{\exp(x_i)}{\sum_{j = 0}^{N-1}\exp(x_j)} \\ loss_{ij} = -\sum_j{Y_{ij} * ln(p_{ij})} \end{array}\end{split}\]

where \(X\) represents logits. \(Y\) represents label. \(loss\) represents output.

Inputs:
  • logits (Tensor) - Input logits, with shape \((N, C)\). Data type must be float16 or float32.

  • labels (Tensor) - Ground truth labels, with shape \((N, C)\), has the same data type with logits.

Outputs:

Tuple of 2 tensors(loss, dlogits), the loss shape is \((N,)\), and the dlogits with the same shape as logits.

引发
  • TypeError – If dtype of logits or labels is neither float16 nor float32.

  • TypeError – If logits or labels is not a Tensor.

  • ValueError – If shape of logits is not the same as labels.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> logits = Tensor([[2, 4, 1, 4, 5], [2, 1, 2, 4, 3]], mindspore.float32)
>>> labels = Tensor([[0, 0, 0, 0, 1], [0, 0, 0, 1, 0]], mindspore.float32)
>>> softmax_cross = ops.SoftmaxCrossEntropyWithLogits()
>>> loss, dlogits = softmax_cross(logits, labels)
>>> print(loss)
[0.5899297  0.52374405]
>>> print(dlogits)
[[ 0.02760027  0.20393994  0.01015357  0.20393994 -0.44563377]
 [ 0.08015892  0.02948882  0.08015892 -0.4077012   0.21789455]]
class tinyms.primitives.Softplus(*args, **kwargs)[源代码]

Softplus activation function.

Softplus is a smooth approximation to the ReLU function. It can be used to constrain the output of a machine to always be positive. The function is shown as follows:

\[\text{output} = \log(1 + \exp(\text{x})),\]
Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the input_x.

引发
  • TypeError – If input_x is not a Tensor.

  • TypeError – If the dtype of input_x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32)
>>> softplus = ops.Softplus()
>>> output = softplus(input_x)
>>> print(output)
[1.3132615 2.126928  3.0485873 4.01815   5.0067153]
class tinyms.primitives.Softsign(*args, **kwargs)[源代码]

Softsign activation function.

The function is shown as follows:

\[\text{SoftSign}(x) = \frac{x}{ 1 + |x|}\]
Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the input_x.

引发
  • TypeError – If input_x is not a Tensor.

  • TypeError – If dtype of input_x is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([0, -1, 2, 30, -30]), mindspore.float32)
>>> softsign = ops.Softsign()
>>> output = softsign(input_x)
>>> print(output)
[ 0.        -0.5         0.6666667  0.9677419 -0.9677419]
class tinyms.primitives.Sort(*args, **kwargs)[源代码]

Sorts the elements of the input tensor along a given dimension in ascending order by value.

参数
  • axis (int) – The dimension to sort along. Default: -1.

  • descending (bool) – Controls the sorting order. If descending is True then the elements are sorted in descending order by value. Default: False.

Inputs:
  • x (Tensor) - The input to sort, with float16 or float32 data type. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

Outputs:
  • y1 (Tensor) - A tensor whose values are the sorted values, with the same shape and data type as input.

  • y2 (Tensor) - The indices of the elements in the original input tensor. Data type is int32.

引发
  • TypeError – If axis is not an int.

  • TypeError – If descending is not a bool.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[8, 2, 1], [5, 9, 3], [4, 6, 7]]), mindspore.float16)
>>> sort = ops.Sort()
>>> output = sort(x)
>>> print(output)
(Tensor(shape=[3, 3], dtype=Float16, value=
[[ 1.0000e+00,  2.0000e+00,  8.0000e+00],
 [ 3.0000e+00,  5.0000e+00,  9.0000e+00],
 [ 4.0000e+00,  6.0000e+00,  7.0000e+00]]), Tensor(shape=[3, 3], dtype=Int32, value=
[[2, 1, 0],
 [2, 0, 1],
 [0, 1, 2]]))
class tinyms.primitives.SpaceToBatch(*args, **kwargs)[源代码]

Divides spatial dimensions into blocks and combines the block size with the original batch.

This operation will divide spatial dimensions (H, W) into blocks with block_size, the output tensor’s H and W dimension is the corresponding number of blocks after division. The output tensor’s batch dimension is the product of the original batch and the square of block_size. Before division, the spatial dimensions of the input are zero padded according to paddings if necessary.

参数
  • block_size (int) – The block size of dividing blocks with value greater than or euqual to 2.

  • paddings (Union[tuple, list]) – The padding values for H and W dimension, containing 2 subtraction lists. Each subtraction list contains 2 integer value. All values must be greater than 0. paddings[i] specifies the paddings for the spatial dimension i, which corresponds to the input dimension i+2. It is required that input_shape[i+2]+paddings[i][0]+paddings[i][1] is divisible by block_size.

Inputs:
  • input_x (Tensor) - The input tensor. It must be a 4-D tensor. The data type is Number.

Outputs:

Tensor, the output tensor with the same data type as input. Assume input shape is \((n, c, h, w)\) with \(block\_size\) and \(paddings\). The shape of the output tensor will be \((n', c', h', w')\), where

\(n' = n*(block\_size*block\_size)\)

\(c' = c\)

\(h' = (h+paddings[0][0]+paddings[0][1])//block\_size\)

\(w' = (w+paddings[1][0]+paddings[1][1])//block\_size\)

引发
Supported Platforms:

Ascend GPU

实际案例

>>> block_size = 2
>>> paddings = [[0, 0], [0, 0]]
>>> space_to_batch = ops.SpaceToBatch(block_size, paddings)
>>> input_x = Tensor(np.array([[[[1, 2], [3, 4]]]]), mindspore.float32)
>>> output = space_to_batch(input_x)
>>> print(output)
[[[[1.]]]
 [[[2.]]]
 [[[3.]]]
 [[[4.]]]]
class tinyms.primitives.SpaceToBatchND(*args, **kwargs)[源代码]

Divides spatial dimensions into blocks and combines the block size with the original batch.

This operation will divide spatial dimensions (H, W) into blocks with block_shape, the output tensor’s H and W dimension is the corresponding number of blocks after division. The output tensor’s batch dimension is the product of the original batch and the product of block_shape. Before division, the spatial dimensions of the input are zero padded according to paddings if necessary.

参数
  • block_shape (Union[list(int), tuple(int), int]) – The block shape of dividing block with all value greater than 1. If block_shape is a tuple or list, the length of block_shape is M corresponding to the number of spatial dimensions. If block_shape is a int, the block size of M dimendions are the same, equal to block_shape. M must be 2.

  • paddings (Union[tuple, list]) – The padding values for H and W dimension, containing 2 subtraction list. Each contains 2 integer value. All values must be greater than 0. paddings[i] specifies the paddings for the spatial dimension i, which corresponds to the input dimension i+2. It is required that input_shape[i+2]+paddings[i][0]+paddings[i][1] is divisible by block_shape[i].

Inputs:
  • input_x (Tensor) - The input tensor. It must be a 4-D tensor.

Outputs:

Tensor, the output tensor with the same data type as input. Assume input shape is \((n, c, h, w)\) with \(block\_shape\) and \(padddings\). The shape of the output tensor will be \((n', c', h', w')\), where

\(n' = n*(block\_shape[0]*block\_shape[1])\)

\(c' = c\)

\(h' = (h+paddings[0][0]+paddings[0][1])//block\_shape[0]\)

\(w' = (w+paddings[1][0]+paddings[1][1])//block\_shape[1]\)

引发
  • TypeError – If block_shape is not one of list, tuple, int.

  • TypeError – If paddings is neither list nor tuple.

  • ValueError – If length of shape of block_shape is not equal to 1.

  • ValueError – If length of block_shape or paddings is not equal to 2.

Supported Platforms:

Ascend

实际案例

>>> block_shape = [2, 2]
>>> paddings = [[0, 0], [0, 0]]
>>> space_to_batch_nd = ops.SpaceToBatchND(block_shape, paddings)
>>> input_x = Tensor(np.array([[[[1, 2], [3, 4]]]]), mindspore.float32)
>>> output = space_to_batch_nd(input_x)
>>> print(output)
[[[[1.]]]
 [[[2.]]]
 [[[3.]]]
 [[[4.]]]]
class tinyms.primitives.SpaceToDepth(*args, **kwargs)[源代码]

Rearranges blocks of spatial data into depth.

The output tensor’s height dimension is \(height / block\_size\).

The output tensor’s weight dimension is \(weight / block\_size\).

The depth of output tensor is \(block\_size * block\_size * input\_depth\).

The input tensor’s height and width must be divisible by block_size. The data format is “NCHW”.

参数

block_size (int) – The block size used to divide spatial data. It must be >= 2.

Inputs:
  • x (Tensor) - The target tensor. The data tyoe is Number. It must be a 4-D tensor.

Outputs:
Tensor, the same data type as x. It must be a 4-D tensor.Tensor of shape

\((N, ( C_{in} * \text{block_size} * 2), H_{in} / \text{block_size}, W_{in} / \text{block_size})\).

引发
  • TypeError – If block_size is not an int.

  • ValueError – If block_size is less than 2.

  • ValueError – If length of shape of x is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.random.rand(1,3,2,2), mindspore.float32)
>>> block_size = 2
>>> space_to_depth = ops.SpaceToDepth(block_size)
>>> output = space_to_depth(x)
>>> print(output.shape)
(1, 12, 1, 1)
class tinyms.primitives.SparseApplyAdagrad(*args, **kwargs)[源代码]

Updates relevant entries according to the adagrad scheme.

\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ var -= lr * grad * (1 / sqrt(accum)) \end{array}\end{split}\]

Inputs of var, accum and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • lr (float) – Learning rate.

  • update_slots (bool) – If True, accum will be updated. Default: True.

  • use_locking (bool) – If true, the var and accum tensors will be protected from being updated. Default: False.

Inputs:
  • var (Parameter) - Variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Accumulation to be updated. The shape and data type must be the same as var.

  • grad (Tensor) - Gradients has the same data type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A vector of indices into the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].

Outputs:

Tuple of 2 tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • accum (Tensor) - The same shape and data type as accum.

引发
  • TypeError – If lr is not a float.

  • TypeError – If neither update_slots nor use_locking is a bool.

  • TypeError – If dtype of var, accum or grad is neither float16 nor float32.

  • TypeError – If dtype of indices is not int32.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.sparse_apply_adagrad = ops.SparseApplyAdagrad(lr=1e-8)
...         self.var = Parameter(Tensor(np.array([[[0.2]]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[[0.1]]]).astype(np.float32)), name="accum")
...     def construct(self, grad, indices):
...         out = self.sparse_apply_adagrad(self.var, self.accum, grad, indices)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.array([[[0.7]]]).astype(np.float32))
>>> indices = Tensor([0], mindspore.int32)
>>> output = net(grad, indices)
>>> print(output)
(Tensor(shape=[1, 1, 1], dtype=Float32, value=
[[[1.99999988e-01]]]), Tensor(shape=[1, 1, 1], dtype=Float32, value=
[[[1.00000001e-01]]]))
class tinyms.primitives.SparseApplyAdagradV2(*args, **kwargs)[源代码]

Updates relevant entries according to the adagrad scheme, one more epsilon attribute than SparseApplyAdagrad.

\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ var -= lr * grad * \frac{1}{\sqrt{accum} + \epsilon} \end{array}\end{split}\]

where \(\epsilon\) represents epsilon.

Inputs of var, accum and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • lr (float) – Learning rate.

  • epsilon (float) – A small value added for numerical stability.

  • use_locking (bool) – If True, the var and accum tensors will be protected from being updated. Default: False.

  • update_slots (bool) – If True, the computation logic will be different to False. Default: True.

Inputs:
  • var (Parameter) - Variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Accumulation to be updated. The shape and data type must be the same as var.

  • grad (Tensor) - Gradients has the same data type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A vector of indices into the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].

Outputs:

Tuple of 2 tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • accum (Tensor) - The same shape and data type as accum.

引发
  • TypeError – If neither lr nor epsilon is a float.

  • TypeError – If neither update_slots nor use_locking is a bool.

  • TypeError – If dtype of var, accum or grad is neither float16 nor float32.

  • TypeError – If dtype of indices is not int32.

Supported Platforms:

Ascend

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.sparse_apply_adagrad_v2 = ops.SparseApplyAdagradV2(lr=1e-8, epsilon=1e-6)
...         self.var = Parameter(Tensor(np.array([[0.2]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.1]]).astype(np.float32)), name="accum")
...
...     def construct(self, grad, indices):
...         out = self.sparse_apply_adagrad_v2(self.var, self.accum, grad, indices)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.array([[0.7]]).astype(np.float32))
>>> indices = Tensor(np.ones([1]), mindspore.int32)
>>> output = net(grad, indices)
>>> print(output)
(Tensor(shape=[1, 1], dtype=Float32, value=
[[ 2.00000003e-01]]), Tensor(shape=[1, 1], dtype=Float32, value=
[[ 1.00000001e-01]]))
class tinyms.primitives.SparseApplyFtrl(*args, **kwargs)[源代码]

Updates relevant entries according to the FTRL-proximal scheme.

For more details, please refer to nn.FTRL.

All of inputs except indices comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • lr (float) – The learning rate value, must be positive.

  • l1 (float) – l1 regularization strength, must be greater than or equal to zero.

  • l2 (float) – l2 regularization strength, must be greater than or equal to zero.

  • lr_power (float) – Learning rate power controls how the learning rate decreases during training, must be less than or equal to zero. Use fixed learning rate if lr_power is zero.

  • use_locking (bool) – Use locks for updating operation if true . Default: False.

Inputs:
  • var (Parameter) - The variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - The accumulation to be updated, must be same data type and shape as var.

  • linear (Parameter) - The linear coefficient to be updated, must be the same data type and shape as var.

  • grad (Tensor) - A tensor of the same type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A tensor of indices in the first dimension of var and accum. If there are duplicates in indices, the behavior is undefined. The type must be int32 or int64 and indices.shape[0] = grad.shape[0].

Outputs:
  • var (Tensor) - Tensor, has the same shape and data type as var.

  • accum (Tensor) - Tensor, has the same shape and data type as accum.

  • linear (Tensor) - Tensor, has the same shape and data type as linear.

引发
  • TypeError – If lr, l1, l2 or lr_power is not a float.

  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of var, accum, linear or grad is neither float16 nor float32.

  • TypeError – If dtype of indices is neither int32 nor int64.

Supported Platforms:

Ascend GPU

实际案例

>>> class SparseApplyFtrlNet(nn.Cell):
...     def __init__(self):
...         super(SparseApplyFtrlNet, self).__init__()
...         self.sparse_apply_ftrl = ops.SparseApplyFtrl(lr=0.01, l1=0.0, l2=0.0, lr_power=-0.5)
...         self.var = Parameter(Tensor(np.array([[0.2]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.1]]).astype(np.float32)), name="accum")
...         self.linear = Parameter(Tensor(np.array([[0.6]]).astype(np.float32)), name="linear")
...
...     def construct(self, grad, indices):
...         out = self.sparse_apply_ftrl(self.var, self.accum, self.linear, grad, indices)
...         return out
...
>>> net = SparseApplyFtrlNet()
>>> grad = Tensor(np.array([[0.7]]).astype(np.float32))
>>> indices = Tensor(np.ones([1]), mindspore.int32)
>>> output = net(grad, indices)
>>> print(output)
(Tensor(shape=[1, 1], dtype=Float32, value=
[[2.00000003e-01]]), Tensor(shape=[1, 1], dtype=Float32, value=
[[1.00000001e-01]]), Tensor(shape=[1, 1], dtype=Float32, value=
[[6.00000024e-01]]))
class tinyms.primitives.SparseApplyFtrlV2(*args, **kwargs)[源代码]

Updates relevant entries according to the FTRL-proximal scheme. This class has one more attribute, named l2_shrinkage, than class SparseApplyFtrl.

All of inputs except indices comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • lr (float) – The learning rate value, must be positive.

  • l1 (float) – l1 regularization strength, must be greater than or equal to zero.

  • l2 (float) – l2 regularization strength, must be greater than or equal to zero.

  • l2_shrinkage (float) – L2 shrinkage regularization.

  • lr_power (float) – Learning rate power controls how the learning rate decreases during training, must be less than or equal to zero. Use fixed learning rate if lr_power is zero.

  • use_locking (bool) – If True, the var and accumulation tensors will be protected from being updated. Default: False.

Inputs:
  • var (Parameter) - The variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - The accumulation to be updated, must be same data type and shape as var.

  • linear (Parameter) - the linear coefficient to be updated, must be same data type and shape as var.

  • grad (Tensor) - A tensor of the same type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A vector of indices in the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].

Outputs:

Tuple of 3 Tensor, the updated parameters.

  • var (Tensor) - Tensor, has the same shape and data type as var.

  • accum (Tensor) - Tensor, has the same shape and data type as accum.

  • linear (Tensor) - Tensor, has the same shape and data type as linear.

引发
  • TypeError – If lr, l1, l2, lr_power or use_locking is not a float.

  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of var, accum, linear or grad is neither float16 nor float32.

  • TypeError – If dtype of indices is not int32.

Supported Platforms:

Ascend

实际案例

>>> class SparseApplyFtrlV2Net(nn.Cell):
...     def __init__(self):
...         super(SparseApplyFtrlV2Net, self).__init__()
...         self.sparse_apply_ftrl_v2 = ops.SparseApplyFtrlV2(lr=0.01, l1=0.0, l2=0.0,
...                                                         l2_shrinkage=0.0, lr_power=-0.5)
...         self.var = Parameter(Tensor(np.array([[0.2, 0.3]]).astype(np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0.5, 0.9]]).astype(np.float32)), name="accum")
...         self.linear = Parameter(Tensor(np.array([[0.7, 0.5]]).astype(np.float32)), name="linear")
...
...     def construct(self, grad, indices):
...         out = self.sparse_apply_ftrl_v2(self.var, self.accum, self.linear, grad, indices)
...         return out
...
>>> net = SparseApplyFtrlV2Net()
>>> grad = Tensor(np.array([[0.8, 0.5]]).astype(np.float32))
>>> indices = Tensor(np.ones([1]), mindspore.int32)
>>> output = net(grad, indices)
>>> print(output)
(Tensor(shape=[1, 2], dtype=Float32, value=
[[ 2.00000003e-01,  3.00000012e-01]]), Tensor(shape=[1, 2], dtype=Float32, value=
[[ 5.00000000e-01,  8.99999976e-01]]), Tensor(shape=[1, 2], dtype=Float32, value=
[[ 6.99999988e-01,  5.00000000e-01]]))
class tinyms.primitives.SparseApplyProximalAdagrad(*args, **kwargs)[源代码]

Updates relevant entries according to the proximal adagrad algorithm. Compared with ApplyProximalAdagrad, an additional index tensor is input.

\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ \text{prox_v} = var - lr * grad * \frac{1}{\sqrt{accum}} \\ var = \frac{sign(\text{prox_v})}{1 + lr * l2} * \max(\left| \text{prox_v} \right| - lr * l1, 0) \end{array}\end{split}\]

Inputs of var, accum and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数

use_locking (bool) – If true, the var and accum tensors will be protected from being updated. Default: False.

Inputs:
  • var (Parameter) - Variable tensor to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • accum (Parameter) - Variable tensor to be updated, has the same shape and dtype as var.

  • lr (Union[Number, Tensor]) - The learning rate value, must be a float number or a scalar tensor with float16 or float32 data type.

  • l1 (Union[Number, Tensor]) - l1 regularization strength, must be a float number or a scalar tensor with float16 or float32 data type.

  • l2 (Union[Number, Tensor]) - l2 regularization strength, must be a float number or a scalar tensor with float16 or float32 data type..

  • grad (Tensor) - A tensor of the same type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.

  • indices (Tensor) - A tensor of indices in the first dimension of var and accum. If there are duplicates in indices, the behavior is undefined. Must be one of the following types: int32, int64 and indices.shape[0] = grad.shape[0].

Outputs:

Tuple of 2 tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • accum (Tensor) - The same shape and data type as accum.

引发
  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of var, accum, lr, l1, l2, scalar or grad is neither float16 nor float32.

  • TypeError – If dtype of indices is neither int32 nor int64.

Supported Platforms:

Ascend GPU

实际案例

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.sparse_apply_proximal_adagrad = ops.SparseApplyProximalAdagrad()
...         self.var = Parameter(Tensor(np.array([[4.1, 7.2], [1.1, 3.0]], np.float32)), name="var")
...         self.accum = Parameter(Tensor(np.array([[0, 0], [0, 0]], np.float32)), name="accum")
...         self.lr = 1.0
...         self.l1 = 1.0
...         self.l2 = 0.0
...     def construct(self, grad, indices):
...         out = self.sparse_apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1,
...                                                  self.l2, grad, indices)
...         return out
...
>>> net = Net()
>>> grad = Tensor(np.array([[1, 1], [1, 1]], np.float32))
>>> indices = Tensor(np.array([0, 1], np.int32))
>>> output = net(grad, indices)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 2.09999990e+00,  5.19999981e+00],
 [ 0.00000000e+00,  1.00000000e+00]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 1.00000000e+00,  1.00000000e+00],
 [ 1.00000000e+00,  1.00000000e+00]]))
class tinyms.primitives.SparseApplyRMSProp(*args, **kwargs)[源代码]

Update relevant entries according to the rmsprop algorithm.

\[\begin{split}\begin{array}{ll} \\ ms = rho * ms_{t-1} + (1 - rho) * grad * grad \\ mom = momentum * mom_{t-1} + lr * grad / sqrt(ms + epsilon) \\ var = var - mom \end{array}\end{split}\]

Inputs of var, ms, mom and grad comply with the implicit type conversion rules to make the data types consistent. If they have different data types, lower priority data type will be converted to relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.

参数
  • rho (float) – Decay rate. The value should between 0 and 1, otherwise the behavior is undefined.

  • momentum (float) – Momentum. The value should be greater or equal to 0, otherwise the behavior is undefined.

  • epsilon (float) – A small value added for numerical stability. The value should be greater than 0, otherwise the behavior is undefined.

  • use_locking (bool) – If True, updating of the var, ms, and mom tensors is protected by a lock; otherwise the behavior is undefined, but may exhibit less contention. Default: False.

Inputs:
  • var (Parameter) - Variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.

  • ms (Parameter) - The dict of mutable tensor ms. Must have the same shape and dtype as var.

  • mom (Parameter) - The dict of mutable tensor mom. Must have the same shape and dtype as var.

  • lr ([Number, Tensor]) - Learning rate. Must be a scalar. With float16 or float32 data type.

  • grad (Tensor) - A tensor for gradient. Must have the same shape and dtype as var.

  • indices (Tensor) - A tensor of indices in the first dimension of var, ms and mom. If there are duplicates in indices, the behavior is undefined. Must be one of the following types: int32, int64 and indices.shape[0] = var.shape[0].

Outputs:

Tuple of 3 Tensors, the updated parameters.

  • var (Tensor) - The same shape and data type as var.

  • ms (Tensor) - The same shape and data type as ms.

  • mom (Tensor) - The same shape and data type as mom.

引发
  • TypeError – If var, ms or mom is not a Parameter.

  • TypeError – If grad or indices is not a Tensor.

  • TypeError – If dtype of var, ms, mom, lr, grad is neither float16 nor float32.

  • TypeError – If dtype of indices is neither int32 nor int64.

  • TypeError – If lr is neither a Number or a Tensor.

  • TypeError – If use_locking is not a bool.

  • TypeError – If dtype of epsilon, rho, momentum is not a float.

  • ValueError – If shape of ms, mom, grad is not same as var.

  • ValueError – If the shape size of lr is not 0.

  • ValueError – If shape of indices is not same as shape of first dimension of var.

  • ValueError – If epsilon is less than or equal to 0.

  • ValueError – If momentum is less than 0.

  • ValueError – If rho is less than 0 or greater than 1.

  • ValueError – If dimension of var is less than 1.

Supported Platforms:

Ascend

实际案例

>>> class SparseApplyRMSPropNet(nn.Cell):
...     def __init__(self, rho, momentum, epsilon, use_locking=False):
...         super(SparseApplyRMSPropNet, self).__init__()
...         self.sparse_apply_r_m_s_prop = P.SparseApplyRMSProp(rho, momentum, epsilon, use_locking)
...         self.var = Parameter(Tensor(np.array([[0.6, 0.3], [0.1, 0.5]]).astype(np.float32)), name="var")
...         self.ms = Parameter(Tensor(np.array([[0.2, 0.4], [0.1, 0.3]]).astype(np.float32)), name="ms")
...         self.mom = Parameter(Tensor(np.array([[0.3, 0.1], [0.3, 0.6]]).astype(np.float32)), name="mom")
...     def construct(self, lr, grad, indices):
...         out = self.sparse_apply_r_m_s_prop(self.var, self.ms, self.mom, lr, grad, indices)
...         return out
...
>>> rho = 0.2
>>> momentum = 0.01
>>> epsilon = 1e-6
>>> net = SparseApplyRMSPropNet(rho, momentum, epsilon)
>>> lr = 0.01
>>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
>>> indices = Tensor(np.array([0, 1], dtype=np.int32))
>>> out = net(lr, grad, indices)
>>> print(out)
(Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.88035822e-01,  2.88811117e-01],
 [ 9.10239667e-02,  4.83422279e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 1.12000003e-01,  4.72000003e-01],
 [ 2.80000009e-02,  5.72000027e-01]]), Tensor(shape=[2, 2], dtype=Float32, value=
[[ 1.19641740e-02,  1.11888833e-02],
 [ 8.97603668e-03,  1.65777095e-02]]))
class tinyms.primitives.SparseGatherV2(*args, **kwargs)[源代码]

Returns a slice of input tensor based on the specified indices and axis.

Inputs:
  • input_params (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • input_indices (Tensor) - The shape of tensor is \((y_1, y_2, ..., y_S)\). Specifies the indices of elements of the original Tensor, must be in the range [0, input_param.shape[axis]).

  • axis (int) - Specifies the dimension index to gather indices.

Outputs:

Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\).

引发

TypeError – If axis is not an int.

Supported Platforms:

Ascend GPU

实际案例

>>> input_params = Tensor(np.array([[1, 2, 7, 42], [3, 4, 54, 22], [2, 2, 55, 3]]), mindspore.float32)
>>> input_indices = Tensor(np.array([1, 2]), mindspore.int32)
>>> axis = 1
>>> out = ops.SparseGatherV2()(input_params, input_indices, axis)
>>> print(out)
[[2. 7.]
 [4. 54.]
 [2. 55.]]
class tinyms.primitives.SparseSoftmaxCrossEntropyWithLogits(*args, **kwargs)[源代码]

Computes the softmax cross-entropy value between logits and sparse encoding labels.

Sets input logits as X, input label as Y, output as loss. Then,

\[\begin{split}\begin{array}{ll} \\ p_{ij} = softmax(X_{ij}) = \frac{\exp(x_i)}{\sum_{j = 0}^{N-1}\exp(x_j)} \\ loss_{ij} = \begin{cases} -ln(p_{ij}), &j = y_i \cr -ln(1 - p_{ij}), & j \neq y_i \end{cases} \\ loss = \sum_{ij} loss_{ij} \end{array}\end{split}\]
参数

is_grad (bool) – If true, this operation returns the computed gradient. Default: False.

Inputs:
  • logits (Tensor) - Input logits, with shape \((N, C)\). Data type must be float16 or float32.

  • labels (Tensor) - Ground truth labels, with shape \((N)\). Data type must be int32 or int64.

Outputs:

Tensor, if is_grad is False, the output tensor is the value of loss which is a scalar tensor; if is_grad is True, the output tensor is the gradient of input with the same shape as logits.

引发
  • TypeError – If is_grad is not a bool.

  • TypeError – If dtype of logits is neither float16 nor float32.

  • TypeError – If dtype of labels is neither int32 nor int64.

  • ValueError – If logits.shape[0] != labels.shape[0].

Supported Platforms:

GPU CPU

实际案例

>>> logits = Tensor([[2, 3, 1, 4, 5], [2, 1, 2, 4, 3]], mindspore.float32)
>>> labels = Tensor([0, 1], mindspore.int32)
>>> sparse_softmax_cross = ops.SparseSoftmaxCrossEntropyWithLogits()
>>> loss = sparse_softmax_cross(logits, labels)
>>> print(loss)
3.4878292
>>> sparse_softmax_cross_grad = ops.SparseSoftmaxCrossEntropyWithLogits(is_grad=True)
>>> loss_grad = sparse_softmax_cross_grad(logits, labels)
>>> print(loss_grad)
[[-0.48415753  0.04306427  0.00582811  0.11706084  0.3182043 ]
 [ 0.04007946 -0.4852556   0.04007946  0.2961494   0.10894729]]
class tinyms.primitives.SparseTensorDenseMatmul(*args, **kwargs)[源代码]

Multiplies sparse matrix A by dense matrix B. The rank of sparse matrix and dense matrix must be equal to 2.

参数
  • adjoint_st (bool) – If true, sparse tensor is transposed before multiplication. Default: False.

  • adjoint_dt (bool) – If true, dense tensor is transposed before multiplication. Default: False.

Inputs:
  • indices (Tensor) - A 2-D Tensor, represents the position of the element in the sparse tensor. Support int32, int64, each element value should be a non-negative int number. The shape is \((n, 2)\).

  • values (Tensor) - A 1-D Tensor, represents the value corresponding to the position in the indices. Support float16, float32, float64, int32, int64. The shape should be \((n,)\).

  • sparse_shape (tuple(int)) - A positive int tuple which specifies the shape of sparse tensor, should have 2 elements, represent sparse tensor shape is \((N, C)\).

  • dense (Tensor) - A 2-D Tensor, the dtype is same as values. If adjoint_st is False and adjoint_dt is False, the shape must be \((C, M)\). If adjoint_st is False and adjoint_dt is True, the shape must be \((M, C)\). If adjoint_st is True and adjoint_dt is False, the shape must be \((N, M)\). If adjoint_st is True and adjoint_dt is True, the shape must be \((M, N)\).

Outputs:

Tensor, the dtype is the same as values. If adjoint_st is False, the shape is \((N, M)\). If adjoint_st is True, the shape is \((C, M)\).

引发
  • TypeError – If the type of adjoint_st or adjoint_dt is not bool, or the dtype of indices, dtype of values and dtype of dense don’t meet the parameter description.

  • ValueError – If sparse_shape, shape of indices, shape of `values, and shape of dense don’t meet the parameter description.

Supported Platforms:

CPU

实际案例

>>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> sparse_shape = (3, 4)
>>> dense = Tensor([[1,1], [2,2], [3,3 ], [4, 4]], dtype=ms.float32)
>>> sparse_dense_matmul = ops.SparseTensorDenseMatmul()
>>> out = sparse_dense_matmul(indices, values, sparse_shape, dense)
>>> print(out)
[[2 2]
 [6 6]
 [0 0]]
class tinyms.primitives.SparseToDense(*args, **kwargs)[源代码]

Converts a sparse representation into a dense tensor.

Inputs:
  • indices (Tensor) - A 2-D Tensor, represents the position of the element in the sparse tensor. Support int32, int64, each element value should be a non-negative int number. The shape is \((n, 2)\).

  • values (Tensor) - A 1-D Tensor, represents the value corresponding to the position in the indices. The shape should be \((n,)\).

  • sparse_shape (tuple(int)) - A positive int tuple which specifies the shape of sparse tensor, should have 2 elements, represent sparse tensor shape is \((N, C)\).

返回

Tensor, converted from sparse tensor. The dtype is same as values, and the shape is sparse_shape.

引发
  • TypeError – If the dtype of indices is neither int32 nor int64.

  • ValueError – If sparse_shape, shape of indices and shape of `values don’t meet the parameter description.

Supported Platforms:

CPU

实际案例

>>> indices = Tensor([[0, 1], [1, 2]])
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> sparse_shape = (3, 4)
>>> sparse_to_dense = ops.SparseToDense()
>>> out = sparse_to_dense(indices, values, sparse_shape)
>>> print(out)
[[0 1 0 0]
 [0 0 2 0]
 [0 0 0 0]]
class tinyms.primitives.Split(*args, **kwargs)[源代码]

Splits the input tensor into output_num of tensors along the given axis and output numbers.

The input_x tensor will be split into equally sized sub-tensors. This requires that input_x.shape(axis) is divisible by output_num.

参数
  • axis (int) – Index of the split position. Default: 0.

  • output_num (int) – The number of output tensors. Must be positive int. Default: 1.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

tuple[Tensor], the shape of each output tensor is the same, which is \((y_1, y_2, ..., y_S)\). And the data type is the same with input_x.

引发
  • TypeError – If axis or output_num is not an int.

  • ValueError – If axis is out of the range [-len(input_x.shape), len(input_x.shape)), or if the output_num is less than or equal to 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> split = ops.Split(1, 2)
>>> x = Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]), mindspore.int32)
>>> print(x)
[[1 1 1 1]
 [2 2 2 2]]
>>> output = split(x)
>>> print(output)
(Tensor(shape=[2, 2], dtype=Int32, value=
[[1, 1],
 [2, 2]]), Tensor(shape=[2, 2], dtype=Int32, value=
[[1, 1],
 [2, 2]]))
>>> split = ops.Split(1, 4)
>>> output = split(x)
>>> print(output)
(Tensor(shape=[2, 1], dtype=Int32, value=
[[1],
 [2]]), Tensor(shape=[2, 1], dtype=Int32, value=
[[1],
 [2]]), Tensor(shape=[2, 1], dtype=Int32, value=
[[1],
 [2]]), Tensor(shape=[2, 1], dtype=Int32, value=
[[1],
 [2]]))
class tinyms.primitives.SplitV(*args, **kwargs)[源代码]

Splits the input tensor into num_split tensors along the given dimension.

The input_x tensor will be split into sub-tensors with individual shapes given by size_splits along the split dimension. This requires that input_x.shape(split_dim) is equal to the sum of size_splits.

The shape of input_x is \((x_1, x_2, ..., x_M, ..., x_R)\). The rank of input_x is R. Set the given split_dim as M, and \(-R \le M < R\). Set the given num_split as N, the given size_splits as \((x_{m_1}, x_{m_2}, ..., x_{m_N})\), \(x_M=\sum_{i=1}^Nx_{m_i}\). The output is a list of tensor objects, for the \(i\)-th tensor, it has the shape of \((x_1, x_2, ..., x_{m_i}, ..., x_R)\). \(x_{m_i}\) is the \(M\)-th dimension of the \(i\)-th tensor. Then, the shape of the output tensor is

\[((x_1, x_2, ..., x_{m_1}, ..., x_R), (x_1, x_2, ..., x_{m_2}, ..., x_R), ..., (x_1, x_2, ..., x_{m_N}, ..., x_R))\]
参数
  • size_splits (Union[tuple, list]) – The list containing the sizes of each output tensor along the split dimension. Must sum to the dimension of value along split_dim. Can contain one -1 indicating that dimension is to be inferred.

  • split_dim (int) – The dimension along which to split. Must be in the range [-len(input_x.shape), len(input_x.shape)).

  • num_split (int) – The number of output tensors. Must be positive int.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ...,x_M ..., x_R)\).

Outputs:

Tensor, a list of num_split Tensor objects with the shape \(((x_1, x_2, ..., x_{m_1}, ..., x_R), (x_1, x_2, ..., x_{m_2}, ..., x_R), ..., (x_1, x_2, ..., x_{m_N}, ..., x_R))\), \(x_M=\sum_{i=1}^Nx_{m_i}\). The data type is the same with input_x.

引发
  • TypeError – If input_x is not a Tensor.

  • TypeError – If size_splits is not a tuple or a list.

  • TypeError – If element of size_splits is not an int.

  • TypeError – If split_dim or num_split is not an int.

  • ValueError – If rank of the size_splits is not equal to num_split.

  • ValueError – If sum of the size_splits is not equal to the dimension of value along split_dim.

  • ValueError – If split_dim is out of the range [-len(input_x.shape), len(input_x.shape)).

  • ValueError – If the num_split is less than or equal to 0.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.int32)
>>> op = ops.SplitV(size_splits=[1, -1], split_dim=1, num_split=2)
>>> output = op(input_x)
>>> print(output)
(Tensor(shape=[3, 1], dtype=Int32, value=
[[1],
 [4],
 [7]]), Tensor(shape=[3, 2], dtype=Int32, value=
[[2, 3],
 [5, 6],
 [8, 9]]))
>>> input_x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.int32)
>>> op = ops.SplitV(size_splits=[2, 1], split_dim=0, num_split=2)
>>> output = op(input_x)
>>> print(output)
(Tensor(shape=[2, 3], dtype=Int32, value=
[[1, 2, 3],
 [4, 5, 6]]), Tensor(shape=[1, 3], dtype=Int32, value=
[[7, 8, 9]]))
class tinyms.primitives.Sqrt(*args, **kwargs)[源代码]

Returns square root of a tensor element-wise.

\[out_{i} = \sqrt{x_{i}}\]
Inputs:
  • x (Tensor) - The input tensor whose dtype is number. \((N,*)\) where \(*\) means ,any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape and data type as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 4.0, 9.0]), mindspore.float32)
>>> sqrt = ops.Sqrt()
>>> output = sqrt(x)
>>> print(output)
[1. 2. 3.]
infer_value(x)[源代码]

Infer the value of input for Sqrt.

class tinyms.primitives.Square(*args, **kwargs)[源代码]

Returns square of a tensor element-wise.

\[out_{i} = (x_{i})^2\]
Inputs:
  • x (Tensor) - The input tensor whose dtype is number. \((N,*)\) where \(*\) means ,any number of additional dimensions, its rank should less than 8.

Outputs:

Tensor, has the same shape and dtype as the x.

引发

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
>>> square = ops.Square()
>>> output = square(x)
>>> print(output)
[1. 4. 9.]
class tinyms.primitives.SquareSumAll(*args, **kwargs)[源代码]

Returns the square sum of a tensor element-wise

\[\begin{split}\left\{\begin{matrix}out_{x} = {\textstyle \sum_{0}^{N}} (x_{i})^2 \\out_{y} = {\textstyle \sum_{0}^{N}} (y_{i})^2 \end{matrix}\right.\end{split}\]
Inputs:
  • x (Tensor) - The input tensor. The data type must be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions.

  • y (Tensor) - The input tensor has the same type and shape as the x.

注解

SquareSumAll only supports float16 and float32 data type.

Outputs:
  • output_y1 (Tensor) - The same type as the x.

  • output_y2 (Tensor) - The same type as the x.

引发
  • TypeError – If neither x nor y is a Tensor.

  • ValueError – If x and y are not the same shape.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([0, 0, 2, 0]), mindspore.float32)
>>> y = Tensor(np.array([0, 0, 2, 4]), mindspore.float32)
>>> square_sum_all = ops.SquareSumAll()
>>> output = square_sum_all(x, y)
>>> print(output)
(Tensor(shape=[], dtype=Float32, value= 4),
 Tensor(shape=[], dtype=Float32, value= 20))
class tinyms.primitives.SquaredDifference(*args, **kwargs)[源代码]

Subtracts the second input tensor from the first input tensor element-wise and returns square of it.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = (x_{i} - y_{i}) * (x_{i} - y_{i}) = (x_{i} - y_{i})^2\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number, or a bool, or a tensor whose data type is float16, float32, int32 or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number, or a bool when the first input is a tensor or a tensor whose data type is float16, float32, int32 or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – if x and y is not a Number or a bool or a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
>>> y = Tensor(np.array([2.0, 4.0, 6.0]), mindspore.float32)
>>> squared_difference = ops.SquaredDifference()
>>> output = squared_difference(x, y)
>>> print(output)
[1. 4. 9.]
class tinyms.primitives.Squeeze(*args, **kwargs)[源代码]

Returns a tensor with the same data type but dimensions of 1 are removed based on axis.

If axis is specified, it will remove the dimensions of size 1 in the given axis. It axis is None, it will remove all the dimensions of size 1.

注解

The dimension index starts at 0 and must be in the range [-input.ndim, input.ndim).

参数

axis (Union[int, tuple(int)]) – Specifies the dimension indexes of shape to be removed, which will remove all the dimensions that are equal to 1. If specified, it must be int32 or int64. Default: (), an empty tuple.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

Outputs:

Tensor, the shape of tensor is \((x_1, x_2, ..., x_S)\).

引发
  • TypeError – If axis is neither an int nor tuple.

  • TypeError – If axis is a tuple whose elements are not all int.

  • ValueError – If the corresponding dimension of the specified axis does not equal to 1.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
>>> squeeze = ops.Squeeze(2)
>>> output = squeeze(input_x)
>>> print(output)
[[1. 1.]
 [1. 1.]
 [1. 1.]]
class tinyms.primitives.Stack(*args, **kwargs)[源代码]

Stacks a list of tensors in specified axis.

Stacks the list of input tensors with the same rank R, output is a tensor of rank (R+1).

Given input tensors of shape \((x_1, x_2, ..., x_R)\). Set the number of input tensors as N. If \(0 \le axis\), the shape of the output tensor is \((x_1, x_2, ..., x_{axis}, N, x_{axis+1}, ..., x_R)\).

参数

axis (int) – Dimension to stack. Default: 0. Negative values wrap around. The range is [-(R+1), R+1).

Inputs:
  • input_x (Union[tuple, list]) - A Tuple or list of Tensor objects with the same shape and type.

Outputs:

Tensor. A stacked Tensor with the same type as input_x.

引发
  • TypeError – If the data types of elements in input_x are not the same.

  • ValueError – If the length of input_x is not greater than 1; or if axis is out of the range [-(R+1), R+1); or if the shapes of elements in input_x are not the same.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> data1 = Tensor(np.array([0, 1]).astype(np.float32))
>>> data2 = Tensor(np.array([2, 3]).astype(np.float32))
>>> stack = ops.Stack()
>>> output = stack([data1, data2])
>>> print(output)
[[0. 1.]
 [2. 3.]]
class tinyms.primitives.StandardLaplace(*args, **kwargs)[源代码]

Generates random numbers according to the Laplace random number distribution (mean=0, lambda=1). It is defined as:

\[\text{f}(x;0,1) = \frac{1}{2}\exp(-|x|),\]
参数
  • seed (int) – Random seed. Default: 0.

  • seed2 (int) – Random seed2. Default: 0.

Inputs:
  • shape (tuple) - The shape of random tensor to be generated. Only constant value is allowed.

Outputs:

Tensor. The shape that the input ‘shape’ denotes. The dtype is float32.

引发
  • TypeError – If neither seed nor seed2 is an int.

  • TypeError – If shape is not a tuple.

  • ValueError – If shape is not a constant value.

Supported Platforms:

Ascend

实际案例

>>> shape = (4, 16)
>>> stdlaplace = ops.StandardLaplace(seed=2)
>>> output = stdlaplace(shape)
>>> result = output.shape
>>> print(result)
(4, 16)
class tinyms.primitives.StandardNormal(*args, **kwargs)[源代码]

Generates random numbers according to the standard Normal (or Gaussian) random number distribution.

Returns the tensor with the given shape, the random numbers in it drawn from normal distributions whose mean is 0 and standard deviation is 1.

\[f(x)=\frac{1}{\sqrt{2 \pi}} e^{\left(-\frac{x^{2}}{2}\right)}\]
参数
  • seed (int) – Random seed, must be non-negative. Default: 0.

  • seed2 (int) – Random seed2, must be non-negative. Default: 0.

Inputs:
  • shape (tuple) - The shape of random tensor to be generated. Only constant value is allowed.

Outputs:

Tensor. The shape is the same as the input shape. The dtype is float32.

引发
  • TypeError – If neither seed nor seed2 is an int.

  • TypeError – If shape is not a tuple.

  • ValueError – If shape is not a constant value.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> shape = (3, 4)
>>> stdnormal = ops.StandardNormal(seed=2)
>>> output = stdnormal(shape)
>>> print(output)
[[-1.3031056   0.64198005 -0.65207404 -1.767485  ]
 [-0.91792876  0.6508565  -0.9098478  -0.14092612]
 [ 0.7806437   1.1585592   1.9676613  -0.00440959]]
class tinyms.primitives.StridedSlice(*args, **kwargs)[源代码]

Extracts a strided slice of a tensor.

Given an input tensor, this operation inserts a dimension of length 1 at the dimension. This operation extracts a fragment of size (end-begin)/stride from the given ‘input_tensor’. Starting from the beginning position, the fragment continues adding stride to the index until all dimensions are not less than the ending position.

Given a input_x[m1, m2, …, mn], begin, end and strides will be vectors of length n.

In each mask field (begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask) the ith bit will correspond to the ith m.

If the ith bit of begin_mask is set, begin[i] is ignored and the fullest possible range in that dimension is used instead. end_mask is analogous, except with the end range.

As for a 5*6*7 tensor, x[2:,:3,:] is equivalent to x[2:5,0:3,0:7].

If the ith bit of ellipsis_mask is set, as many unspecified dimensions as needed will be inserted between other dimensions. Only one non-zero bit is allowed in ellipsis_mask.

As for a 5*6*7*8 tensor, x[2:,…,:6] is equivalent to x[2:5,:,:,0:6]. x[2:,…] is equivalent to x[2:5,:,:,:].

If the ith bit of new_axis_mask is set, begin, end and strides are ignored and a new length 1 dimension is added at the specified position in tthe output tensor.

As for a 5*6*7 tensor, x[:2, newaxis, :6] will produce a tensor with shape (2, 1, 7).

If the ith bit of shrink_axis_mask is set, ith size shrinks the dimension by 1, taking on the value at index begin[i], end[i] and strides[i] are ignored.

As for a 5*6*7 tensor, x[:, 5, :] will result in shrink_axis_mask equal to 4.

注解

The stride may be negative value, which causes reverse slicing. The shape of begin, end and strides must be the same. begin and end are zero-indexed. The element of strides must be non-zero.

参数
  • begin_mask (int) – Starting index of the slice. Default: 0.

  • end_mask (int) – Ending index of the slice. Default: 0.

  • ellipsis_mask (int) – An int mask. Default: 0.

  • new_axis_mask (int) – An int mask. Default: 0.

  • shrink_axis_mask (int) – An int mask. Default: 0.

Inputs:
  • input_x (Tensor) - The input Tensor.

  • begin (tuple[int]) - A tuple which represents the location where to start. Only constant value is allowed.

  • end (tuple[int]) - A tuple or which represents the maximum location where to end. Only constant value is allowed.

  • strides (tuple[int]) - A tuple which represents the stride is continuously added before reaching the maximum location. Only constant value is allowed.

Outputs:

Tensor, The output is explained by following example.

In the 0th dimension, begin is 1, end is 2, and strides is 1, because \(1+1=2\geq2\), the interval is \([1,2)\). Thus, return the element with \(index = 1\) in 0th dimension, i.e., [[3, 3, 3], [4, 4, 4]].

In the 1st dimension, similarly, the interval is \([0,1)\). Based on the return value of the 0th dimension, return the element with \(index = 0\), i.e., [3, 3, 3].

In the 2nd dimension, similarly, the interval is \([0,3)\). Based on the return value of the 1st dimension, return the element with \(index = 0,1,2\), i.e., [3, 3, 3].

Finally, the output is [3, 3, 3].

引发
  • TypeError – If begin_mask, end_mask, ellipsis_mask, new_axis_mask or shrink_axis_mask is not an int.

  • TypeError – If begin, end or strides is not a tuple.

  • ValueError – If begin_mask, end_mask, ellipsis_mask, new_axis_mask or shrink_axis_mask is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]],
...                   [[5, 5, 5], [6, 6, 6]]], mindspore.float32)
>>> #         [[[1. 1. 1.]
>>> #           [2. 2. 2.]]
>>> #
>>> #          [[3. 3. 3.]
>>> #           [4. 4. 4.]]
>>> #
>>> #          [[5. 5. 5.]
>>> #           [6. 6. 6.]]]
>>> # In order to visually view the multi-dimensional array, write the above as follows:
>>> #         [
>>> #             [
>>> #                 [1,1,1]
>>> #                 [2,2,2]
>>> #             ]
>>> #             [
>>> #                 [3,3,3]
>>> #                 [4,4,4]
>>> #             ]
>>> #             [
>>> #                 [5,5,5]
>>> #                 [6,6,6]
>>> #             ]
>>> #         ]
>>> strided_slice = ops.StridedSlice()
>>> output = strided_slice(input_x, (1, 0, 2), (3, 1, 3), (1, 1, 1))
>>> # Take this " output = strided_slice(input_x, (1, 0, 2), (3, 1, 3), (1, 1, 1)) " as an example,
>>> # start = [1, 0, 2] , end = [3, 1, 3], stride = [1, 1, 1], Find a segment of (start, end),
>>> # note that end is an open interval
>>> # To facilitate understanding, this operator can be divided into three steps:
>>> # Step 1: Calculation of the first dimension:
>>> # start = 1, end = 3, stride = 1, So can take 1st, 2nd rows, and then gets the final output at this time.
>>> # output_1th =
>>> # [
>>> #     [
>>> #         [3,3,3]
>>> #         [4,4,4]
>>> #     ]
>>> #     [
>>> #         [5,5,5]
>>> #         [6,6,6]
>>> #     ]
>>> # ]
>>> # Step 2: Calculation of the second dimension
>>> # 2nd dimension, start = 0, end = 1, stride = 1. So only 0th rows can be taken, and the output at this time.
>>> # output_2nd =
>>> # [
>>> #     [
>>> #         [3,3,3]
>>> #     ]
>>> #     [
>>> #         [5,5,5]
>>> #     ]
>>> # ]
>>> # Step 3: Calculation of the third dimension
>>> # 3nd dimension,start = 2, end = 3, stride = 1, So can take 2th cols,
>>> # and you get the final output at this time.
>>> # output_3ed =
>>> # [
>>> #     [
>>> #         [3]
>>> #     ]
>>> #     [
>>> #         [5]
>>> #     ]
>>> # ]
>>> # The final output after finishing is:
>>> print(output)
[[[3.]]
 [[5.]]]
>>> # another example like :
>>> output = strided_slice(input_x, (1, 0, 0), (2, 1, 3), (1, 1, 1))
>>> print(output)
[[[3. 3. 3.]]]
class tinyms.primitives.Sub(*args, **kwargs)[源代码]

Subtracts the second input tensor from the first input tensor element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

\[out_{i} = x_{i} - y_{i}\]
Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number, or a bool, or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If x and y is not a Number or a bool or a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32)
>>> y = Tensor(np.array([4, 5, 6]), mindspore.int32)
>>> sub = ops.Sub()
>>> output = sub(x, y)
>>> print(output)
[-3 -3 -3]
class tinyms.primitives.Tan(*args, **kwargs)[源代码]

Computes tangent of x element-wise.

\[out_i = tan(x_i)\]
Inputs:
  • x (Tensor) - The shape of tensor is \((N,*)\) where \(*\) means, any number of additional dimensions. Data type must be float16, float32 or int32.

Outputs:

Tensor, has the same shape as x.

引发
  • TypeError – If dtype of x is not one of the following: float16, float32, int32.

  • TypeError – If x is not a Tensor.

Supported Platforms:

Ascend CPU

实际案例

>>> tan = ops.Tan()
>>> x = Tensor(np.array([-1.0, 0.0, 1.0]), mindspore.float32)
>>> output = tan(x)
>>> print(output)
[-1.5574081 0. 1.5574081]
class tinyms.primitives.Tanh(*args, **kwargs)[源代码]

Tanh activation function.

Computes hyperbolic tangent of input element-wise. The Tanh function is defined as:

\[tanh(x_i) = \frac{\exp(x_i) - \exp(-x_i)}{\exp(x_i) + \exp(-x_i)} = \frac{\exp(2x_i) - 1}{\exp(2x_i) + 1},\]

where \(x_i\) is an element of the input Tensor.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions, with float16 or float32 data type.

Outputs:

Tensor, with the same type and shape as the input_x.

引发
  • TypeError – If dtype of input_x is neither float16 nor float32.

  • TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32)
>>> tanh = ops.Tanh()
>>> output = tanh(input_x)
>>> print(output)
[0.7615941 0.9640276 0.9950547 0.9993293 0.9999092]
class tinyms.primitives.TensorAdd(**kwargs)[源代码]

Same as operator Add. TensorAdd will be deprecated in the future. Please use Add instead.

class tinyms.primitives.TensorScatterAdd(*args, **kwargs)[源代码]

Creates a new tensor by adding the values from the positions in input_x indicicated by indices, with values from updates. When multiple values are given for the same index, the updated result will be the sum of all values. This operation is almost equivalent to using ScatterNdAdd, except that the updates are applied on Tensor instead of Parameter.

The last axis of indices is the depth of each index vectors. For each index vector, there must be a corresponding value in updates. The shape of updates should be equal to the shape of input_x[indices]. For more details, see use cases.

注解

If some values of the indices are out of bound, instead of raising an index error, the corresponding updates will not be updated to input_x.

Inputs:
  • input_x (Tensor) - The target tensor. The dimension of input_x must be no less than indices.shape[-1].

  • indices (Tensor) - The index of input tensor whose data type is int32 or int64. The rank must be at least 2.

  • updates (Tensor) - The tensor to update the input tensor, has the same type as input, and updates.shape should be equal to indices.shape[:-1] + input_x.shape[indices.shape[-1]:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If dtype of indices is neither int32 nor int64.

  • ValueError – If length of shape of input_x is less than the last dimension of shape of indices.

Supported Platforms:

GPU

实际案例

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> indices = Tensor(np.array([[0, 0], [0, 0]]), mindspore.int32)
>>> updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> # Next, demonstrate the approximate operation process of this operator:
>>> # 1, indices[0] = [0, 0], indices[1] = [0, 0]
>>> # 2, And input_x[0, 0] = -0.1
>>> # 3, So input_x[indices] = [-0.1, -0.1]
>>> # 4, Satisfy the above formula: input_x[indices].shape=(2) == updates.shape=(2)
>>> op = ops.TensorScatterAdd()
>>> # 5, Perform the addition operation for the first time:
>>> #      first_input_x = input_x[0][0] + updates[0] = [[0.9, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> # 6, Perform the addition operation for the second time:
>>> #      second_input_x = input_x[0][0] + updates[1] = [[3.1, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> output = op(input_x, indices, updates)
>>> print(output)
[[ 3.1  0.3  3.6]
 [ 0.4  0.5 -3.2]]
class tinyms.primitives.TensorScatterMax(*args, **kwargs)[源代码]

By comparing the value at the position indicated by the index in input_x with the value in the update, the value at the index will eventually be equal to the largest one to create a new tensor.

The last axis of the index is the depth of each index vector. For each index vector, there must be a corresponding value in updates. The shape of updates should be equal to the shape of input_x[indices]. For more details, see use cases.

注解

If some values of the indices are out of bound, instead of raising an index error, the corresponding updates will not be updated to input_x.

Inputs:
  • input_x (Tensor) - The target tensor. The dimension of input_x must be no less than indices.shape[-1].

  • indices (Tensor) - The index of input tensor whose data type is int32 or int64. The rank must be at least 2.

  • updates (Tensor) - The tensor to update the input tensor, has the same type as input, and updates.shape should be equal to indices.shape[:-1] + input_x.shape[indices.shape[-1]:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If dtype of indices is neither int32 nor int64.

  • ValueError – If length of shape of input_x is less than the last dimension of shape of indices.

Supported Platforms:

GPU

实际案例

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> indices = Tensor(np.array([[0, 0], [0, 0]]), mindspore.int32)
>>> updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> # Next, demonstrate the approximate operation process of this operator:
>>> # 1, indices[0] = [0, 0], indices[1] = [0, 0]
>>> # 2, And input_x[0, 0] = -0.1
>>> # 3, So input_x[indices] = [-0.1, -0.1]
>>> # 4, Satisfy the above formula: input_x[indices].shape=(2) == updates.shape=(2)
>>> op = ops.TensorScatterMax()
>>> # 5, Perform the max operation for the first time:
>>> #      first_input_x = Max(input_x[0][0], updates[0]) = [[2.2, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> # 6, Perform the max operation for the second time:
>>> #      second_input_x = Max(input_x[0][0], updates[0]) = [[2.2, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> output = op(input_x, indices, updates)
>>> print(output)
[[ 2.2  0.3  3.6]
 [ 0.4  0.5 -3.2]]
class tinyms.primitives.TensorScatterMin(*args, **kwargs)[源代码]

By comparing the value at the position indicated by the index in input_x with the value in the updates, the value at the index will eventually be equal to the smallest one to create a new tensor.

The last axis of the index is the depth of each index vector. For each index vector, there must be a corresponding value in updates. The shape of updates should be equal to the shape of input_x[indices]. For more details, see use cases.

注解

If some values of the indices are out of bound, instead of raising an index error, the corresponding updates will not be updated to input_x.

Inputs:
  • input_x (Tensor) - The target tensor. The dimension of input_x must be no less than indices.shape[-1].

  • indices (Tensor) - The index of input tensor whose data type is int32 or int64. The rank must be at least 2.

  • updates (Tensor) - The tensor to update the input tensor, has the same type as input, and updates.shape should be equal to indices.shape[:-1] + input_x.shape[indices.shape[-1]:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If dtype of indices is neither int32 nor int64.

  • ValueError – If length of shape of input_x is less than the last dimension of shape of indices.

Supported Platforms:

GPU

实际案例

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> indices = Tensor(np.array([[0, 0], [0, 0]]), mindspore.int32)
>>> updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> # Next, demonstrate the approximate operation process of this operator:
>>> # 1, indices[0] = [0, 0], indices[1] = [0, 0]
>>> # 2, And input_x[0, 0] = -0.1
>>> # 3, So input_x[indices] = [-0.1, -0.1]
>>> # 4, Satisfy the above formula: input_x[indices].shape=(2) == updates.shape=(2)
>>> op = ops.TensorScatterMin()
>>> # 5, Perform the min operation for the first time:
>>> #      first_input_x = Min(input_x[0][0], updates[0]) = [[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> # 6, Perform the min operation for the second time:
>>> #      second_input_x = Min(input_x[0][0], updates[1]) = [[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> output = op(input_x, indices, updates)
>>> print(output)
[[ -0.1  0.3  3.6]
 [ 0.4  0.5 -3.2]]
class tinyms.primitives.TensorScatterSub(*args, **kwargs)[源代码]

Creates a new tensor by subtracting the values from the positions in input_x indicicated by indices, with values from updates. When multiple values are provided for the same index, the result of the update will be to subtract these values respectively. This operation is almost equivalent to using ScatterNdSub, except that the updates are applied on Tensor instead of Parameter.

The last axis of indices is the depth of each index vectors. For each index vector, there must be a corresponding value in updates. The shape of updates should be equal to the shape of input_x[indices]. For more details, see use cases.

注解

If some values of the indices are out of bound, instead of raising an index error, the corresponding updates will not be updated to input_x.

Inputs:
  • input_x (Tensor) - The target tensor. The dimension of input_x must be no less than indices.shape[-1].

  • indices (Tensor) - The index of input tensor whose data type is int32 or int64. The rank must be at least 2.

  • updates (Tensor) - The tensor to update the input tensor, has the same type as input, and updates.shape should be equal to indices.shape[:-1] + input_x.shape[indices.shape[-1]:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If dtype of indices is neither int32 nor int64.

  • ValueError – If length of shape of input_x is less than the last dimension of shape of indices.

Supported Platforms:

GPU

实际案例

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> indices = Tensor(np.array([[0, 0], [0, 0]]), mindspore.int32)
>>> updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> # Next, demonstrate the approximate operation process of this operator:
>>> # 1, indices[0] = [0, 0], indices[1] = [0, 0]
>>> # 2, And input_x[0, 0] = -0.1
>>> # 3, So input_x[indices] = [-0.1, -0.1]
>>> # 4, Satisfy the above formula: input_x[indices].shape=(2) == updates.shape=(2)
>>> op = ops.TensorScatterSub()
>>> # 5, Perform the subtract operation for the first time:
>>> #      first_input_x = input_x[0][0] - updates[0] = [[-1.1, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> # 6, Perform the subtract operation for the second time:
>>> #      second_input_x = input_x[0][0] - updates[1] = [[-3.3, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> output = op(input_x, indices, updates)
>>> print(output)
[[-3.3000002  0.3        3.6      ]
 [ 0.4        0.5       -3.2      ]]
class tinyms.primitives.TensorScatterUpdate(*args, **kwargs)[源代码]

Creates a new tensor by updating the positions in input_x indicicated by indices, with values from update. This operation is almost equivalent to using ScatterNd, except that the updates are applied on input_x instead of a zero tensor.

indices must have rank at least 2, the last axis is the depth of each index vectors. For each index vector, there must be a corresponding value in update. If the depth of each index tensor matches the rank of input_x, then each index vector corresponds to a scalar in input_x and each update updates a scalar. If the depth of each index tensor is less than the rank of input_x, then each index vector corresponds to a slice in input_x, and each update updates a slice.

The order in which updates are applied is nondeterministic, meaning that if there are multiple index vectors in indices that correspond to the same position, the value of that position in the output will be nondeterministic.

Inputs:
  • input_x (Tensor) - The target tensor. The dimension of input_x must be no less than indices.shape[-1]. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions. The data type is Number.

  • indices (Tensor) - The index of input tensor whose data type is int32 or int64. The rank must be at least 2.

  • update (Tensor) - The tensor to update the input tensor, has the same type as input, and update.shape = indices.shape[:-1] + input_x.shape[indices.shape[-1]:].

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If dtype of indices is neither int32 nor int64.

  • ValueError – If length of shape of input_x is less than the last dimension of shape of indices.

  • ValueError – If the value of input_x are not match with input indices.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
>>> update = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> op = ops.TensorScatterUpdate()
>>> output = op(input_x, indices, update)
>>> print(output)
[[ 1.   0.3  3.6]
 [ 0.4  2.2 -3.2]]
class tinyms.primitives.TensorSummary(*args, **kwargs)[源代码]

Outputs a tensor to a protocol buffer through a tensor summary operator.

Inputs:
  • name (str) - The name of the input variable.

  • value (Tensor) - The value of tensor, and the rank of tensor must be greater than 0.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>>
>>>
>>> class SummaryDemo(nn.Cell):
...     def __init__(self,):
...         super(SummaryDemo, self).__init__()
...         self.summary = ops.TensorSummary()
...         self.add = ops.Add()
...
...     def construct(self, x, y):
...         x = self.add(x, y)
...         name = "x"
...         self.summary(name, x)
...         return x
...
class tinyms.primitives.Tile(*args, **kwargs)[源代码]

Replicates a tensor with given multiples times.

Creates a new tensor by replicating input_x multiples times. The i’th dimension of output tensor has input_x.shape(i) * multiples[i] elements, and the values of input_x are replicated multiples[i] times along the i’th dimension.

注解

The length of multiples must be greater or equal to the length of dimension in input_x.

Inputs:
  • input_x (Tensor) - 1-D or higher Tensor. Set the shape of input tensor as \((x_1, x_2, ..., x_S)\).

  • multiples (tuple[int]) - The input tuple is constructed by multiple integers, i.e., \((y_1, y_2, ..., y_S)\). The length of multiples cannot be smaller than the length of the shape of input_x. Only constant value is allowed.

Outputs:

Tensor, has the same data type as the input_x.

  • If the length of multiples is the same as the length of shape of input_x, then the shape of their corresponding positions can be multiplied, and the shape of Outputs is \((x_1*y_1, x_2*y_2, ..., x_S*y_R)\).

  • If the length of multiples is larger than the length of shape of input_x, fill in multiple 1 in the length of the shape of input_x until their lengths are consistent. Such as set the shape of input_x as \((1, ..., x_1, x_2, ..., x_S)\), then the shape of their corresponding positions can be multiplied, and the shape of Outputs is \((1*y_1, ..., x_S*y_R)\).

引发
  • TypeError – If multiples is not a tuple or its elements are not all int.

  • ValueError – If the elements of multiples are not all greater than 0.

  • ValueError – If the length of multiples are smaller than the length of dimension in input_x.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> tile = ops.Tile()
>>> input_x = Tensor(np.array([[1, 2], [3, 4]]), mindspore.float32)
>>> multiples = (2, 3)
>>> output = tile(input_x, multiples)
>>> print(output)
[[1.  2.  1.  2.  1.  2.]
 [3.  4.  3.  4.  3.  4.]
 [1.  2.  1.  2.  1.  2.]
 [3.  4.  3.  4.  3.  4.]]
>>> multiples = (2, 3, 2)
>>> output = tile(input_x, multiples)
>>> print(output)
[[[1. 2. 1. 2.]
  [3. 4. 3. 4.]
  [1. 2. 1. 2.]
  [3. 4. 3. 4.]
  [1. 2. 1. 2.]
  [3. 4. 3. 4.]]
 [[1. 2. 1. 2.]
  [3. 4. 3. 4.]
  [1. 2. 1. 2.]
  [3. 4. 3. 4.]
  [1. 2. 1. 2.]
  [3. 4. 3. 4.]]]
class tinyms.primitives.TopK(*args, **kwargs)[源代码]

Finds values and indices of the k largest entries along the last dimension.

警告

  • If sorted set to ‘False’, it will use aicpu operator, performance may be reduced.

If the input_x is a one-dimensional Tensor, finds the k largest entries in the Tensor, and outputs its value and index as a Tensor. Therefore, values[k] is the k largest item in input_x, and its index is indices [k].

For a multi-dimensional matrix, calculates the first k entries in each row (corresponding vector along the last dimension), therefore:

\[values.shape = indices.shape = input.shape[:-1] + [k].\]

If the two compared elements are the same, the one with the smaller index value is returned first.

参数

sorted (bool) – If true, the obtained elements will be sorted by the values in descending order. Default: True.

Inputs:
  • input_x (Tensor) - Input to be computed, data type must be float16, float32 or int32.

  • k (int) - The number of top elements to be computed along the last dimension, constant input is needed.

Outputs:

Tuple of 2 tensors, the values and the indices.

  • values (Tensor) - The k largest elements in each slice of the last dimensional.

  • indices (Tensor) - The indices of values within the last dimension of input.

引发
  • TypeError – If sorted is not a bool.

  • TypeError – If input_x is not a Tensor.

  • TypeError – If k is not an int.

  • TypeError – If dtype of input_x is not one of the following: float16, float32 or int32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> topk = ops.TopK(sorted=True)
>>> input_x = Tensor([1, 2, 3, 4, 5], mindspore.float16)
>>> k = 3
>>> values, indices = topk(input_x, k)
>>> print((values, indices))
(Tensor(shape=[3], dtype=Float16, value= [ 5.0000e+00,  4.0000e+00,  3.0000e+00]), Tensor(shape=[3],
  dtype=Int32, value= [4, 3, 2]))
class tinyms.primitives.Totalc6get(*args, **kwargs)[源代码]

Get the average dispersion constant of short range Lennard-Jones interaction, for the subsequent long range correction energy and virial. Assume system has m Lennard-Jones types of atoms.

警告

This is an experimental prototype that is subject to change and/or deletion.

参数

atom_numbers (int32) – the number of atoms n.

Inputs:
  • atom_lj_type (Tensor) - The Lennard-Jones type of each atom. The data type is float32 and the shape is \((n,)\).

  • lj_b (Tensor) - The attraction coefficient of each type. the number of pair atoms is m. The data type is float32 and the shape is \((m,)\).

Outputs:
  • factor (Tensor) - The average dispersion constant of Lennard-Jones interaction. The data type is float32 and the shape is \((1,)\).

Supported Platforms:

GPU

class tinyms.primitives.TransferCrd(*args, **kwargs)[源代码]

Transfer the coordinates to angular and radial.

Because there is a large amount of inputs and each of them are related, there is no way to construct Examples using random methods. For details, refer the webpage SPONGE in MindSpore.

参数
  • start_serial (int32) – the index start position.

  • end_serial (int32) – the index end position.

  • number (int32) – the length of angular and radial.

Inputs:
  • crd (Tensor) - The coordinate of each atom. n is the number of atoms. The data type is float32 and the shape is \((n, 3)\).

  • old_crd (Tensor) - The last coordinate of each atom. n is the number of atoms. The data type is float32 and the shape is \((n, 3)\).

  • box (Tensor) - The length of 3 dimensions of the simulation box. The data type is float32 and the shape is \((3,)\).

Outputs:
  • radial (Tensor) - The array of radial transferred from coordinates. The data type is float32 and the shape is \((number,)\).

  • angular (Tensor) - The array of angular transferred from coordinates. The data type is float32 and the shape is \((number,)\).

  • nowarp_crd (Tensor) - The modified coordinate of each atom for computing radial and angular. The data type is float32 and the shape is \((n, 3)\).

  • box_map_times (Tensor) - The box map times for radial and angular. The data type is int32 and the shape is \((n, 3)\).

Supported Platforms:

GPU

class tinyms.primitives.Transpose(*args, **kwargs)[源代码]

Permutes the dimensions of the input tensor according to input permutation.

Inputs:
  • input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • input_perm (tuple[int]) - The permutation to be converted. The elements in input_perm are composed of the indexes of each dimension of input_x. The length of input_perm and the shape of input_x must be the same. Only constant value is allowed. Must be in the range [0, rank(input_x)).

Outputs:

Tensor, the type of output tensor is the same as input_x and the shape of output tensor is decided by the shape of input_x and the value of input_perm.

引发
  • TypeError – If input_perm is not a tuple.

  • ValueError – If length of shape of input_x is not equal to length of shape of input_perm.

  • ValueError – If the same element exists in input_perm.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]), mindspore.float32)
>>> input_perm = (0, 2, 1)
>>> transpose = ops.Transpose()
>>> output = transpose(input_x, input_perm)
>>> print(output)
[[[ 1.  4.]
  [ 2.  5.]
  [ 3.  6.]]
 [[ 7. 10.]
  [ 8. 11.]
  [ 9. 12.]]]
class tinyms.primitives.TruncateDiv(*args, **kwargs)[源代码]

Divides the first input tensor by the second input tensor element-wise for integer types, negative numbers will round fractional quantities towards zero.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

注解

Broadcasting is supported.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number, or a bool, or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If x and y is not one of the following: Tensor, Number, bool.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([2, 4, -1]), mindspore.int32)
>>> y = Tensor(np.array([3, 3, 3]), mindspore.int32)
>>> truncate_div = ops.TruncateDiv()
>>> output = truncate_div(x, y)
>>> print(output)
[0 1 0]
class tinyms.primitives.TruncateMod(*args, **kwargs)[源代码]

Returns the remainder of division element-wise.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

警告

  • The input data does not support 0.

  • When the elements of input exceeds 2048 , the accuracy of operator cannot guarantee the requirement of double thousandths in the mini form.

  • Due to different architectures, the calculation results of this operator on NPU and CPU may be inconsistent.

  • If shape is expressed as (D1,D2… ,Dn), then D1*D2… *DN<=1000000,n<=8.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number, or a bool, or a tensor whose data type is number or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is number or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If neither x nor y is one of the following: Tensor, Number, bool.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([2, 4, -1]), mindspore.int32)
>>> y = Tensor(np.array([3, 3, 3]), mindspore.int32)
>>> truncate_mod = ops.TruncateMod()
>>> output = truncate_mod(x, y)
>>> print(output)
[ 2  1 -1]
class tinyms.primitives.TruncatedNormal(*args, **kwargs)[源代码]

Returns a tensor of the specified shape filled with truncated normal values.

The generated values follow a normal distribution.

参数
  • seed (int) – A integer number used to create random seed. Default: 0.

  • dtype (mindspore.dtype) – Data type. Default: mindspore.float32.

Inputs:
  • shape (tuple[int]) - The shape of the output tensor, is a tuple of positive integer.

Outputs:

Tensor, the data type of output tensor is the same as attribute dtype.

实际案例

>>> shape = (1, 2, 3)
>>> truncated_normal = ops.TruncatedNormal()
>>> output = truncated_normal(shape)
class tinyms.primitives.TupleToArray(*args, **kwargs)[源代码]

Converts a tuple to a tensor.

If the type of the first number in the tuple is integer, the data type of the output tensor is int. Otherwise, the data type of the output tensor is float.

Inputs:
  • input_x (tuple) - A tuple of numbers. These numbers have the same type. Only constant value is allowed. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

Outputs:

Tensor, if the input tuple contains N numbers, then the shape of the output tensor is (N,).

引发
  • TypeError – If input_x is not a tuple.

  • ValueError – If length of input_x is less than or equal to 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = (1,2,3)
>>> print(type(input_x))
<class 'tuple'>
>>> output = ops.TupleToArray()(input_x)
>>> print(type(output))
<class 'mindspore.common.tensor.Tensor'>
>>> print(output)
[1 2 3]
class tinyms.primitives.UniformCandidateSampler(*args, **kwargs)[源代码]

Uniform candidate sampler.

This function samples a set of classes(sampled_candidates) from [0, range_max-1] based on uniform distribution. If unique=True, candidates are drawn without replacement, else unique=False with replacement.

参数
  • num_true (int) – The number of target classes in each training example.

  • num_sampled (int) – The number of classes to randomly sample. The sampled_candidates will have a shape of num_sampled. If unique=True, num_sampled must be less than or equal to range_max.

  • unique (bool) – Whether all sampled classes in a batch are unique.

  • range_max (int) – The number of possible classes, must be non-negative.

  • seed (int) – Used for random number generation, must be non-negative. If seed has a value of 0, seed will be replaced with a randomly generated value. Default: 0.

  • remove_accidental_hits (bool) – Whether accidental hit is removed. Default: False.

Inputs:
  • true_classes (Tensor) - A Tensor. The target classes with a Tensor shape of (batch_size, num_true).

Outputs:
  • sampled_candidates (Tensor) - The sampled_candidates is independent of the true classes. Shape: (num_sampled, ).

  • true_expected_count (Tensor) - The expected counts under the sampling distribution of each of true_classes. Shape: (batch_size, num_true).

  • sampled_expected_count (Tensor) - The expected counts under the sampling distribution of each of sampled_candidates. Shape: (num_sampled, ).

引发
  • TypeError – If neither num_true nor num_sampled is an int.

  • TypeError – If neither unique nor remove_accidental_hits is a bool.

  • TypeError – If neither range_max nor seed is a int.

  • TypeError – If true_classes is not a Tensor.

Supported Platforms:

GPU

实际案例

>>> sampler = ops.UniformCandidateSampler(1, 3, False, 4)
>>> output1, output2, output3 = sampler(Tensor(np.array([[1], [3], [4], [6], [3]], dtype=np.int32)))
>>> print(output1, output2, output3)
[1, 1, 3], [[0.75], [0.75], [0.75], [0.75], [0.75]], [0.75, 0.75, 0.75]
class tinyms.primitives.UniformInt(*args, **kwargs)[源代码]

Produces random integer values i, uniformly distributed on the closed interval [minval, maxval), that is, distributed according to the discrete probability function:

\[\text{P}(i|a,b) = \frac{1}{b-a+1},\]

where the \(a\) indicates the min distribution parameter, the \(b\) indicates the max distribution parameter.

注解

The number in tensor minval must be strictly less than maxval at any position after broadcasting.

参数
  • seed (int) – Random seed, must be non-negative. Default: 0.

  • seed2 (int) – Random seed2, must be non-negative. Default: 0.

Inputs:
  • shape (tuple) - The shape of random tensor to be generated. Only constant value is allowed.

  • minval (Tensor) - The distribution parameter, a. It defines the minimum possibly generated value, with int32 data type. Only one number is supported.

  • maxval (Tensor) - The distribution parameter, b. It defines the maximum possibly generated value, with int32 data type. Only one number is supported.

引发
  • TypeError – If neither seed nor seed2 is an int.

  • TypeError – If shape is not a tuple.

  • TypeError – If neither minval nor maxval is a Tensor.

  • ValueError – If shape is not a constant value.

Outputs:

Tensor. The shape is the same as the input ‘shape’, and the data type is int32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> shape = (2, 4)
>>> minval = Tensor(1, mstype.int32)
>>> maxval = Tensor(5, mstype.int32)
>>> uniform_int = ops.UniformInt(seed=10)
>>> output = uniform_int(shape, minval, maxval)
>>> result = output.shape
>>> print(result)
(2, 4)
class tinyms.primitives.UniformReal(*args, **kwargs)[源代码]

Produces random floating-point values i, uniformly distributed to the interval [0, 1).

参数
  • seed (int) – Random seed, must be non-negative. Default: 0.

  • seed2 (int) – Random seed2, must be non-negative. Default: 0.

Inputs:
  • shape (tuple) - The shape of random tensor to be generated. Only constant value is allowed.

Outputs:

Tensor. The shape that the input ‘shape’ denotes. The dtype is float32.

引发
  • TypeError – If neither seed nor seed2 is an int.

  • TypeError – If shape is not a tuple.

  • ValueError – If shape is not a constant value.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> shape = (2, 2)
>>> uniformreal = ops.UniformReal(seed=2)
>>> output = uniformreal(shape)
>>> result = output.shape
>>> print(result)
(2, 2)
class tinyms.primitives.Unique(*args, **kwargs)[源代码]

Returns the unique elements of input tensor and also return a tensor containing the index of each value of input tensor corresponding to the output unique tensor.

The output contains Tensor y and Tensor idx, the format is probably similar to (y, idx). The shape of Tensor y and Tensor idx is different in most cases, because Tensor y will be deduplicated, and the shape of Tensor idx is consistent with the input.

To get the same shape between idx and y, please ref to ‘UniqueWithPad’ operator.

Inputs:
  • input_x (Tensor) - The input tensor. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tuple, containing Tensor objects (y, idx), `y is a tensor with the same type as input_x, and contains the unique elements in x, sorted in ascending order. idx is a tensor containing indices of elements in the input corresponding to the output tensor.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor(np.array([1, 2, 5, 2]), mindspore.int32)
>>> output = ops.Unique()(input_x)
>>> print(output)
(Tensor(shape=[3], dtype=Int32, value= [1, 2, 5]), Tensor(shape=[4], dtype=Int32, value= [0, 1, 2, 1]))
>>> y = output[0]
>>> print(y)
[1 2 5]
>>> idx = output[1]
>>> print(idx)
[0 1 2 1]
>>> # As can be seen from the above, y and idx shape
>>> # note that for GPU, this operator must be wrapped inside a model, and executed in graph mode.
>>> class UniqueNet(nn.Cell):
...     def __init__(self):
...         super(UniqueNet, self).__init__()
...         self.unique_op = ops.Unique()
...
...     def construct(self, x):
...         output, indices = self.unique_op(x)
...         return output, indices
...
>>> input_x = Tensor(np.array([1, 2, 5, 2]), mindspore.int32)
>>> net = UniqueNet()
>>> output = net(input_x)
>>> print(output)
(Tensor(shape=[3], dtype=Int32, value= [1, 2, 5]), Tensor(shape=[4], dtype=Int32, value= [0, 1, 2, 1]))
class tinyms.primitives.UniqueWithPad(*args, **kwargs)[源代码]

Returns unique elements and relative indexes in 1-D tensor, filled with padding num.

The basic function is the same as the Unique operator, but the UniqueWithPad operator adds a Pad function. The returned tuple(y,`idx`) after the input Tensor x is processed by the unique operator, in which the shapes of y and idx are mostly not equal. Therefore, in order to solve the above situation, the UniqueWithPad operator will fill the y Tensor with the pad_num specified by the user to make it have the same shape as the Tensor idx.

Inputs:
  • x (Tensor) - The tensor need to be unique. Must be 1-D vector with types: int32, int64.

  • pad_num (int) - Pad num. The data type is an int.

Outputs:

tuple(Tensor), tuple of 2 tensors, y and idx. - y (Tensor) - The unique elements filled with pad_num, the shape and data type same as x. - idx (Tensor) - The index of each value of x in the unique output y, the shape and data type same as x.

引发
  • TypeError – If dtype of x is neither int32 nor int64.

  • ValueError – If length of shape of x is not equal to 1.

Supported Platforms:

Ascend CPU

实际案例

>>> x = Tensor(np.array([1, 1, 5, 5, 4, 4, 3, 3, 2, 2,]), mindspore.int32)
>>> pad_num = 8
>>> output = ops.UniqueWithPad()(x, pad_num)
>>> print(output)
(Tensor(shape=[10], dtype=Int32, value= [1, 5, 4, 3, 2, 8, 8, 8, 8, 8]),
 Tensor(shape=[10], dtype=Int32, value= [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]))
class tinyms.primitives.Unpack(**kwargs)[源代码]

Same as operator Unstack. Unpack will be deprecated in the future. Please use Unstack instead.

class tinyms.primitives.UnsortedSegmentMax(*args, **kwargs)[源代码]

Computes the maximum along segments of a tensor.

The following figure shows the calculation process of UnsortedSegmentMax:

tinyms/api_img/UnsortedSegmentMax.png
\[\text { output }_i=\text{max}_{j \ldots} \text { data }[j \ldots]\]

where \(max\) over tuples \(j...\) such that \(segment\_ids[j...] == i\).

注解

If the segment_id i is absent in the segment_ids, then output[i] will be filled with the minimum value of the input_x’s type.

Inputs:
  • input_x (Tensor) - The shape is \((x_1, x_2, ..., x_R)\). The data type must be float16, float32 or int32.

  • segment_ids (Tensor) - A 1-D tensor whose shape is \((x_1)\), the value must be non-negative tensor. The data type must be int32.

  • num_segments (int) - The value specifies the number of distinct segment_ids.

Outputs:

Tensor, set the number of num_segments as N, the shape is \((N, x_2, ..., x_R)\).

引发
  • TypeError – If num_segments is not an int.

  • ValueError – If length of shape of segment_ids is not equal to 1.

Supported Platforms:

Ascend GPU

实际案例

>>> # case 1: Only have two num_segments, where is 0 and 1, and segment_ids=[0, 1, 1]
>>> # num_segments = 2 indicates that there are two types of segment_id,
>>> # the first number '0' in [0, 1, 1] indicates input_x[0],
>>> # the second number '1' in [0, 1, 1] indicates input_x[1],
>>> # the third number '1' in [0, 1, 1] indicates input_x[2],
>>> # input_x[0], which is [1, 2, 3] will not be compared to other segment_id.
>>> # Only the same segment_id will be compared.
>>> input_x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [4, 2, 1]]).astype(np.float32))
>>> segment_ids = Tensor(np.array([0, 1, 1]).astype(np.int32))
>>> num_segments = 2
>>> unsorted_segment_max = ops.UnsortedSegmentMax()
>>> output = unsorted_segment_max(input_x, segment_ids, num_segments)
>>> print(output)
[[1. 2. 3.]
 [4. 5. 6.]]
>>>
>>> # case 2: The segment_ids=[0, 0, 1, 1].
>>> # [1, 2, 3] will compare with [4, 2, 0],
>>> # and [4, 5, 6] will compare with [4, 2, 1].
>>> input_x = Tensor(np.array([[1, 2, 3], [4, 2, 0], [4, 5, 6], [4, 2, 1]]).astype(np.float32))
>>> segment_ids = Tensor(np.array([0, 0, 1, 1]).astype(np.int32))
>>> num_segments = 2
>>> unsorted_segment_max = ops.UnsortedSegmentMax()
>>> output = unsorted_segment_max(input_x, segment_ids, num_segments)
>>> print(input_x.shape)
    (4, 3)
>>> print(output)
    [[4. 2. 3.]
     [4. 5. 6.]]
>>> # case 3: If the input_x have three dimensions even more, what will happen?
>>> # The shape of input_x is (2, 4, 3),
>>> # and the length of segment_ids should be the same as the first dimension of input_x.
>>> # Because the segment_ids are different, input_x[0] will not be compared to input_x[1].
>>> input_x = Tensor(np.array([[[1, 2, 3], [4, 2, 0], [4, 5, 6], [4, 2, 1]],
>>>                            [[1, 2, 3], [4, 2, 0], [4, 5, 6], [4, 2, 1]]]).astype(np.float32))
>>> segment_ids = Tensor(np.array([0, 1]).astype(np.int32))
>>> num_segments = 2
>>> unsorted_segment_max = ops.UnsortedSegmentMax()
>>> output = unsorted_segment_max(input_x, segment_ids, num_segments)
>>> print(input_x.shape)
    (2, 4, 3)
>>> print(output)
    [[[1. 2. 3.]
      [4. 2. 0.]
      [4. 5. 6.]
      [4. 2. 1.]]
     [[1. 2. 3.]
      [4. 2. 0.]
      [4. 5. 6.]
      [4. 2. 1.]]]
>>> # case 4: It has the same input with the 3rd case.
>>> # Because num_segments is equal to 2, there are two segment_ids, but currently only one 0 is used.
>>> # the segment_id i is absent in the segment_ids, then output[i] will be filled with
>>> # the smallest possible value of the input_x's type.
>>> segment_ids = Tensor(np.array([0, 0]).astype(np.int32))
>>> output = unsorted_segment_max(input_x, segment_ids, num_segments)
>>> print(output)
    [[[ 1.0000000e+00  2.0000000e+00  3.0000000e+00]
      [ 4.0000000e+00  2.0000000e+00  0.0000000e+00]
      [ 4.0000000e+00  5.0000000e+00  6.0000000e+00]
      [ 4.0000000e+00  2.0000000e+00  1.0000000e+00]]
     [[-3.4028235e+38 -3.4028235e+38 -3.4028235e+38]
      [-3.4028235e+38 -3.4028235e+38 -3.4028235e+38]
      [-3.4028235e+38 -3.4028235e+38 -3.4028235e+38]
      [-3.4028235e+38 -3.4028235e+38 -3.4028235e+38]]]
class tinyms.primitives.UnsortedSegmentMin(*args, **kwargs)[源代码]

Computes the minimum of a tensor along segments.

The following figure shows the calculation process of UnsortedSegmentMin:

tinyms/api_img/UnsortedSegmentMin.png
\[\text { output }_i=\text{min}_{j \ldots} \text { data }[j \ldots]\]

where \(min\) over tuples \(j...\) such that \(segment_ids[j...] == i\).

注解

If the segment_id i is absent in the segment_ids, then output[i] will be filled with the maximum value of the input_x’s type. The segment_ids must be non-negative tensor.

Inputs:
  • input_x (Tensor) - The shape is \((x_1, x_2, ..., x_R)\). The data type must be float16, float32 or int32.

  • segment_ids (Tensor) - A 1-D tensor whose shape is \((x_1)\), the value must be non-negative tensor. The data type must be int32.

  • num_segments (int) - The value specifies the number of distinct segment_ids.

Outputs:

Tensor, set the number of num_segments as N, the shape is \((N, x_2, ..., x_R)\).

引发
  • TypeError – If num_segments is not an int.

  • ValueError – If length of shape of segment_ids is not equal to 1.

Supported Platforms:

Ascend GPU

实际案例

>>> input_x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [4, 2, 1]]).astype(np.float32))
>>> segment_ids = Tensor(np.array([0, 1, 1]).astype(np.int32))
>>> num_segments = 2
>>> unsorted_segment_min = ops.UnsortedSegmentMin()
>>> output = unsorted_segment_min(input_x, segment_ids, num_segments)
>>> print(output)
[[1. 2. 3.]
 [4. 2. 1.]]
class tinyms.primitives.UnsortedSegmentProd(*args, **kwargs)[源代码]

Computes the product of a tensor along segments.

The following figure shows the calculation process of UnsortedSegmentProd:

tinyms/api_img/UnsortedSegmentProd.png
Inputs:
  • input_x (Tensor) - The shape is \((x_1, x_2, ..., x_R)\). With float16, float32 or int32 data type.

  • segment_ids (Tensor) - A 1-D tensor whose shape is \((x_1)\), the value must be non-negative tensor. Data type must be int32.

  • num_segments (int) - The value specifies the number of distinct segment_ids, must be greater than 0.

Outputs:

Tensor, set the number of num_segments as N, the shape is \((N, x_2, ..., x_R)\).

引发
  • TypeError – If num_segments is not an int.

  • ValueError – If length of shape of segment_ids is not equal to 1.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [4, 2, 1]]).astype(np.float32))
>>> segment_ids = Tensor(np.array([0, 1, 0]).astype(np.int32))
>>> num_segments = 2
>>> unsorted_segment_prod = ops.UnsortedSegmentProd()
>>> output = unsorted_segment_prod(input_x, segment_ids, num_segments)
>>> print(output)
[[4. 4. 3.]
 [4. 5. 6.]]
class tinyms.primitives.UnsortedSegmentSum(*args, **kwargs)[源代码]

Computes the sum of a tensor along segments.

Calculates a tensor such that \(\text{output}[i] = \sum_{segment\_ids[j] == i} \text{data}[j, \ldots]\), where \(j\) is a tuple describing the index of element in data. segment_ids selects which elements in data to sum up. Segment_ids does not need to be sorted, and it does not need to cover all values in the entire valid value range.

The following figure shows the calculation process of UnsortedSegmentSum:

tinyms/api_img/UnsortedSegmentSum.png

注解

If the segment_id i is absent in the segment_ids, then output[i] will be filled with 0.

If the sum of the given segment_ids \(i\) is empty, then \(\text{output}[i] = 0\). If the given segment_ids is negative, the value will be ignored. ‘num_segments’ must be equal to the number of different segment_ids.

Inputs:
  • input_x (Tensor) - The shape is \((x_1, x_2, ..., x_R)\).

  • segment_ids (Tensor) - Set the shape as \((x_1, x_2, ..., x_N)\), where 0 < N <= R.

  • num_segments (int) - Set \(z\) as num_segments.

Outputs:

Tensor, the shape is \((z, x_{N+1}, ..., x_R)\).

引发
  • TypeError – If num_segments is not an int.

  • ValueError – If length of shape of segment_ids is less than 1.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_x = Tensor([1, 2, 3, 4], mindspore.float32)
>>> segment_ids = Tensor([0, 0, 1, 2], mindspore.int32)
>>> num_segments = 4
>>> output = ops.UnsortedSegmentSum()(input_x, segment_ids, num_segments)
>>> print(output)
[3. 3. 4. 0.]
>>> input_x = Tensor([1, 2, 3, 4, 2, 5], mindspore.float32)
>>> segment_ids = Tensor([0, 0, 1, 2, 3, 4], mindspore.int32)
>>> num_segments = 6
>>> output = ops.UnsortedSegmentSum()(input_x, segment_ids, num_segments)
>>> print(output)
[3. 3. 4. 2. 5. 0.]
class tinyms.primitives.Unstack(*args, **kwargs)[源代码]

Unstacks tensor in specified axis.

Unstacks a tensor of rank R along axis dimension, output tensors will have rank (R-1).

Given a tensor of shape \((x_1, x_2, ..., x_R)\). If \(0 \le axis\), the shape of tensor in output is \((x_1, x_2, ..., x_{axis}, x_{axis+2}, ..., x_R)\).

This is the opposite of pack.

参数

axis (int) – Dimension along which to pack. Default: 0. Negative values wrap around. The range is [-R, R).

Inputs:
  • input_x (Tensor) - The shape is \((x_1, x_2, ..., x_R)\). A tensor to be unstacked and the rank of the tensor must be greater than 0.

Outputs:

A tuple of tensors, the shape of each objects is the same.

引发

ValueError – If axis is out of the range [-len(input_x.shape), len(input_x.shape)).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> unstack = ops.Unstack()
>>> input_x = Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]))
>>> output = unstack(input_x)
>>> print(output)
(Tensor(shape=[4], dtype=Int64, value= [1, 1, 1, 1]), Tensor(shape=[4], dtype=Int64, value= [2, 2, 2, 2]))
class tinyms.primitives.UpdateState(*args, **kwargs)[源代码]

UpdateState is used for update side-effect state.

Inputs:
  • value (State) - the state value to be updated.

  • expr (Expression) - the expression to evaluate before state changes.

Outputs:

State, the updated state value.

class tinyms.primitives.Xdivy(*args, **kwargs)[源代码]

Divides the first input tensor by the second input tensor element-wise. Returns zero when x is zero.

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number, or a bool, or a tensor whose data type is float16, float32 or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is float16, float32 or bool.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If x and y is not one of the following: Tensor, Number, bool.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([2, 4, -1]), mindspore.float32)
>>> y = Tensor(np.array([2, 2, 2]), mindspore.float32)
>>> xdivy = ops.Xdivy()
>>> output = xdivy(x, y)
>>> print(output)
[ 1.   2.  -0.5]
class tinyms.primitives.Xlogy(*args, **kwargs)[源代码]

Computes the first input tensor multiplied by the logarithm of second input tensor element-wise. Returns zero when x is zero.

\[out_i = x_{i}\ln{y_{i}}\]

Inputs of x and y comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant.

Inputs:
  • x (Union[Tensor, Number, bool]) - The first input is a number or a bool or a tensor whose data type is float16, float32 or bool.

  • y (Union[Tensor, Number, bool]) - The second input is a number or a bool when the first input is a tensor or a tensor whose data type is float16, float32 or bool. The value must be positive.

Outputs:

Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs.

引发

TypeError – If x and y is not one of the following: Tensor, Number, bool.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([-5, 0, 4]), mindspore.float32)
>>> y = Tensor(np.array([2, 2, 2]), mindspore.float32)
>>> xlogy = ops.Xlogy()
>>> output = xlogy(x, y)
>>> print(output)
[-3.465736   0.        2.7725887]
class tinyms.primitives.Zeros(*args, **kwargs)[源代码]

Creates a tensor filled with value zeros.

Creates a tensor with shape described by the first argument and fills it with value zeros in type of the second argument.

Inputs:
  • shape (Union[tuple[int], int]) - The specified shape of output tensor. Only constant positive int is allowed.

  • type (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed.

Outputs:

Tensor, has the same type and shape as input shape value.

引发
  • TypeError – If shape is neither int nor tuple.

  • TypeError – If shape is a tuple whose elements are not all int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> zeros = ops.Zeros()
>>> output = zeros((2, 2), mindspore.float32)
>>> print(output)
[[0. 0.]
 [0. 0.]]
class tinyms.primitives.ZerosLike(*args, **kwargs)[源代码]

Creates a new tensor. All elements value are 0.

Returns a tensor of zeros with the same shape and data type as the input tensor.

Inputs:
  • input_x (Tensor) - Input tensor. The data type is int32, int64, float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same shape and data type as input_x but filled with zeros.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> zeroslike = ops.ZerosLike()
>>> input_x = Tensor(np.array([[0, 1], [2, 1]]).astype(np.float32))
>>> output = zeroslike(input_x)
>>> print(output)
[[0. 0.]
 [0. 0.]]
class tinyms.primitives.GradOperation(get_all=False, get_by_list=False, sens_param=False)[源代码]

A higher-order function which is used to generate the gradient function for the input function.

The gradient function generated by GradOperation higher-order function can be customized by construction arguments.

Given an input function net = Net() that takes x and y as inputs, and has a parameter z, see Net in Examples.

To generate a gradient function that returns gradients with respect to the first input (see GradNetWrtX in Examples).

  1. Construct a GradOperation higher-order function with default arguments: grad_op = GradOperation().

  2. Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).

  3. Call the gradient function with input function’s inputs to get the gradients with respect to the first input: grad_op(net)(x, y).

To generate a gradient function that returns gradients with respect to all inputs (see GradNetWrtXY in Examples).

  1. Construct a GradOperation higher-order function with get_all=True which indicates getting gradients with respect to all inputs, they are x and y in example function Net(): grad_op = GradOperation(get_all=True).

  2. Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).

  3. Call the gradient function with input function’s inputs to get the gradients with respect to all inputs: gradient_function(x, y).

To generate a gradient function that returns gradients with respect to given parameters (see GradNetWithWrtParams in Examples).

  1. Construct a GradOperation higher-order function with get_by_list=True: grad_op = GradOperation(get_by_list=True).

  2. Construct a ParameterTuple that will be passed to the input function when constructing GradOperation higher-order function, it will be used as a parameter filter that determine which gradient to return: params = ParameterTuple(net.trainable_params()).

  3. Call it with input function and params as arguments to get the gradient function: gradient_function = grad_op(net, params).

  4. Call the gradient function with input function’s inputs to get the gradients with respect to given parameters: gradient_function(x, y).

To generate a gradient function that returns gradients with respect to all inputs and given parameters in the format of ((dx, dy), (dz))(see GradNetWrtInputsAndParams in Examples).

  1. Construct a GradOperation higher-order function with get_all=True and get_by_list=True: grad_op = GradOperation(get_all=True, get_by_list=True).

  2. Construct a ParameterTuple that will be passed along input function when constructing GradOperation higher-order function: params = ParameterTuple(net.trainable_params()).

  3. Call it with input function and params as arguments to get the gradient function: gradient_function = grad_op(net, params).

  4. Call the gradient function with input function’s inputs to get the gradients with respect to all inputs and given parameters: gradient_function(x, y).

We can configure the sensitivity(gradient with respect to output) by setting sens_param as True and passing an extra sensitivity input to the gradient function, the sensitivity input should has the same shape and type with input function’s output(see GradNetWrtXYWithSensParam in Examples).

  1. Construct a GradOperation higher-order function with get_all=True and sens_param=True: grad_op = GradOperation(get_all=True, sens_param=True).

  2. Define grad_wrt_output as sens_param which works as the gradient with respect to output: grad_wrt_output = Tensor(np.ones([2, 2]).astype(np.float32)).

  3. Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).

  4. Call the gradient function with input function’s inputs and sens_param to get the gradients with respect to all inputs: gradient_function(x, y, grad_wrt_output).

参数
  • get_all (bool) – If True, get all the gradients with respect to inputs. Default: False.

  • get_by_list (bool) – If True, get all the gradients with respect to Parameter variables. If get_all and get_by_list are both False, get the gradient with respect to first input. If get_all and get_by_list are both True, get the gradients with respect to inputs and Parameter variables at the same time in the form of ((gradients with respect to inputs), (gradients with respect to parameters)). Default: False.

  • sens_param (bool) – Whether to append sensitivity (gradient with respect to output) as input. If sens_param is False, a ‘ones_like(outputs)’ sensitivity will be attached automatically. Default: False. If the sensor_param is True, a sensitivity (gradient with respect to output) needs to be transferred through the location parameter or key-value pair parameter. If the value is transferred through the key-value pair parameter, the key must be sens.

返回

The higher-order function which takes a function as argument and returns gradient function for it.

引发

TypeError – If get_all, get_by_list or sens_param is not a bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import ParameterTuple
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.matmul = P.MatMul()
...         self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z')
...     def construct(self, x, y):
...         x = x * self.z
...         out = self.matmul(x, y)
...         return out
...
>>> class GradNetWrtX(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtX, self).__init__()
...         self.net = net
...         self.grad_op = GradOperation()
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net)
...         return gradient_function(x, y)
...
>>> x = Tensor([[0.5, 0.6, 0.4], [1.2, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.01, 0.3, 1.1], [0.1, 0.2, 1.3], [2.1, 1.2, 3.3]], dtype=mstype.float32)
>>> output = GradNetWrtX(Net())(x, y)
>>> print(output)
[[1.4100001 1.5999999 6.6      ]
 [1.4100001 1.5999999 6.6      ]]
>>>
>>> class GradNetWrtXY(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtXY, self).__init__()
...         self.net = net
...         self.grad_op = GradOperation(get_all=True)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net)
...         return gradient_function(x, y)
>>>
>>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWrtXY(Net())(x, y)
>>> print(output)
(Tensor(shape=[2, 3], dtype=Float32, value=
[[ 4.50999975e+00,  2.70000005e+00,  3.60000014e+00],
 [ 4.50999975e+00,  2.70000005e+00,  3.60000014e+00]]), Tensor(shape=[3, 3], dtype=Float32, value=
[[ 2.59999990e+00,  2.59999990e+00,  2.59999990e+00],
 [ 1.89999998e+00,  1.89999998e+00,  1.89999998e+00],
 [ 1.30000007e+00,  1.30000007e+00,  1.30000007e+00]]))
>>>
>>> class GradNetWrtXYWithSensParam(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtXYWithSensParam, self).__init__()
...         self.net = net
...         self.grad_op = GradOperation(get_all=True, sens_param=True)
...         self.grad_wrt_output = Tensor([[0.1, 0.6, 0.2], [0.8, 1.3, 1.1]], dtype=mstype.float32)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net)
...         return gradient_function(x, y, self.grad_wrt_output)
>>>
>>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWrtXYWithSensParam(Net())(x, y)
>>> print(output)
(Tensor(shape=[2, 3], dtype=Float32, value=
[[ 2.21099997e+00,  5.09999990e-01,  1.49000001e+00],
 [ 5.58800030e+00,  2.68000007e+00,  4.07000017e+00]]), Tensor(shape=[3, 3], dtype=Float32, value=
[[ 1.51999998e+00,  2.81999993e+00,  2.14000010e+00],
 [ 1.09999990e+00,  2.04999995e+00,  1.54999995e+00],
 [ 9.00000036e-01,  1.54999995e+00,  1.25000000e+00]]))
>>>
>>> class GradNetWithWrtParams(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWithWrtParams, self).__init__()
...         self.net = net
...         self.params = ParameterTuple(net.trainable_params())
...         self.grad_op = GradOperation(get_by_list=True)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net, self.params)
...         return gradient_function(x, y)
>>>
>>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32)
>>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWithWrtParams(Net())(x, y)
>>> print(output)
(Tensor(shape=[1], dtype=Float32, value= [ 2.15359993e+01]),)
>>>
>>> class GradNetWrtInputsAndParams(nn.Cell):
...     def __init__(self, net):
...         super(GradNetWrtInputsAndParams, self).__init__()
...         self.net = net
...         self.params = ParameterTuple(net.trainable_params())
...         self.grad_op = GradOperation(get_all=True, get_by_list=True)
...     def construct(self, x, y):
...         gradient_function = self.grad_op(self.net, self.params)
...         return gradient_function(x, y)
>>>
>>> x = Tensor([[0.1, 0.6, 1.2], [0.5, 1.3, 0.1]], dtype=mstype.float32)
>>> y = Tensor([[0.12, 2.3, 1.1], [1.3, 0.2, 2.4], [0.1, 2.2, 0.3]], dtype=mstype.float32)
>>> output = GradNetWrtInputsAndParams(Net())(x, y)
>>> print(output)
((Tensor(shape=[2, 3], dtype=Float32, value=
[[ 3.51999998e+00,  3.90000010e+00,  2.59999990e+00],
 [ 3.51999998e+00,  3.90000010e+00,  2.59999990e+00]]), Tensor(shape=[3, 3], dtype=Float32, value=
[[ 6.00000024e-01,  6.00000024e-01,  6.00000024e-01],
 [ 1.89999998e+00,  1.89999998e+00,  1.89999998e+00],
 [ 1.30000007e+00,  1.30000007e+00,  1.30000007e+00]])), (Tensor(shape=[1], dtype=Float32, value=
 [ 1.29020004e+01]),))
tinyms.primitives.grad(fn, grad_first_param=False)[源代码]

A wrapper function to generate the gradient function for the input function.

参数
  • fn (Function) – Function to do GradOperation.

  • grad_first_param (bool) – If True, get the gradient with respect to first input. If False, get all the gradients with respect to inputs. Default: False.

tinyms.primitives.pack(x)[源代码]

Call stack in this pack function.

tinyms.layers

Layer module contains pre-defined building blocks or computing units to construct neural networks.

The high-level components (Layers) used to construct the neural network.

class tinyms.layers.Layer(auto_prefix=True, flags=None)[源代码]

Base class for all neural networks.

A ‘Layer’ could be a single neural network layer, such as conv2d, relu, batch_norm, etc. or a composition of cells to constructing a network.

注解

In general, the autograd algorithm will automatically generate the implementation of the gradient function, but if back-propagation(bprop) method is implemented, the gradient function will be replaced by the bprop. The bprop implementation will receive a Tensor dout containing the gradient of the loss w.r.t. the output, and a Tensor out containing the forward result. The bprop needs to compute the gradient of the loss w.r.t. the inputs, gradient of the loss w.r.t. Parameter variables are not supported currently. The bprop method must contain the self parameter.

参数

auto_prefix (bool) – Recursively generate namespaces. Default: True.

实际案例

>>> from tinyms import layers, primitives as P
>>>
>>> class MyNet(layers.Layer):
...    def __init__(self):
...        super(MyNet, self).__init__()
...        self.relu = P.ReLU()
...
...    def construct(self, x):
...        return self.relu(x)
add_flags(**flags)

Add customized attributes for cell.

This method is also called when the cell class is instantiated and the class parameter ‘flag’ is set to True.

add_flags_recursive(**flags)

If a cell contains child cells, this method can recursively customize attributes of all cells.

auto_parallel_compile_and_run()

Whether or not to execute compile and run.

返回

bool, _auto_parallel_compile_and_run value.

property bprop_debug

Get whether cell custom bprop debug is enabled.

cast_inputs(inputs, dst_type)

Cast inputs to specified type.

cast_param(param)

Cast parameter according to auto mix precision level in pynative mode.

This interface is currently used in the case of auto mix precision and usually need not to be used explicitly.

参数

param (Parameter) – Parameters, the type of which should be cast.

返回

Parameter, the input parameter with type automatically cast.

cells()

Returns an iterator over immediate cells.

返回

Iteration, all the child cells in the cell.

cells_and_names(cells=None, name_prefix='')

Returns an iterator over all cells in the network.

Includes the cell’s name and itself.

参数
  • cells (str) – Cells to iterate over. Default: None.

  • name_prefix (str) – Namespace. Default: ‘’.

返回

Iteration, all the child cells and corresponding names in the cell.

实际案例

>>> n = Net()
>>> names = []
>>> for m in n.cells_and_names():
...     if m[0]:
...         names.append(m[0])
check_names()

Check the names of cell parameters.

compile(*inputs)

Compiles cell.

参数

inputs (tuple) – Inputs of the Cell object.

compile_and_run(*inputs)

Compiles and runs cell.

参数

inputs (tuple) – Inputs of the Cell object.

返回

Object, the result of executing.

construct(*inputs, **kwargs)

Defines the computation to be performed. This method must be overridden by all subclasses.

返回

Tensor, returns the computed result.

exec_checkpoint_graph()

Executes saving checkpoint graph operation.

extend_repr()

Sets the extended representation of the Cell.

To print customized extended information, re-implement this method in your own cells.

generate_scope()

Generate the scope for each cell object in the network.

get_flags()

Get the attributes of cell’s flags.

get_func_graph_proto()

Return graph binary proto.

get_parameters(expand=True)

Returns an iterator over cell parameters.

Yields parameters of this cell. If expand is true, yield parameters of this cell and all subcells.

参数

expand (bool) – If true, yields parameters of this cell and all subcells. Otherwise, only yield parameters that are direct members of this cell. Default: True.

返回

Iteration, all parameters at the cell.

实际案例

>>> net = Net()
>>> parameters = []
>>> for item in net.get_parameters():
...     parameters.append(item)
get_scope()

Returns the scope of a cell object in one network.

返回

String, scope of the cell.

infer_param_pipeline_stage()

Infer pipeline stages of all parameters in the cell.

注解

  • If a parameter does not belong to any cell which has been set pipeline_stage, the parameter should use add_pipeline_stage to add it’s pipeline_stage information.

  • If a parameter P has been used by two operator in different stages “stageA” and “stageB”, the parameter P should use P.add_pipeline_stage(stageA) and P.add_pipeline_stage(stageB) to add it’s stage information before use infer_param_pipeline_stage.

返回

The params belong to current stage in pipeline parallel.

引发

RuntimeError – If there is a parameter does not belong to any stage.

init_parameters_data(auto_parallel_mode=False)

Initialize all parameters and replace the original saved parameters in cell.

注解

trainable_params() and other similar interfaces may return different parameter instance after init_parameters_data, do not save these result.

参数

auto_parallel_mode (bool) – If running in auto_parallel_mode.

返回

Dict[Parameter, Parameter], returns a dict of original parameter and replaced parameter.

insert_child_to_cell(child_name, child_cell)

Adds a child cell to the current cell with a given name.

参数
  • child_name (str) – Name of the child cell.

  • child_cell (Cell) – The child cell to be inserted.

引发
  • KeyError – Child Cell’s name is incorrect or duplicated with the other child name.

  • TypeError – Child Cell’s type is incorrect.

insert_param_to_cell(param_name, param, check_name=True)

Adds a parameter to the current cell.

Inserts a parameter with given name to the cell. Please refer to the usage in source code of mindspore.nn.Cell.__setattr__.

参数
  • param_name (str) – Name of the parameter.

  • param (Parameter) – Parameter to be inserted to the cell.

  • check_name (bool) – Determines whether the name input is compatible. Default: True.

引发
  • KeyError – If the name of parameter is null or contains dot.

  • AttributeError – If user did not call init() first.

  • TypeError – If the type of parameter is not Parameter.

load_parameter_slice(params)

Replace parameters with sliced tensors by parallel strategies.

Please refer to the usage in source code of mindspore.common._CellGraphExecutor.compile.

参数

params (dict) – The parameters dictionary used for initializing the data graph.

name_cells()

Returns an iterator over all cells in the network.

Include name of the cell and cell itself.

返回

Dict[String, Cell], all the child cells and corresponding names in the cell.

property param_prefix

Param prefix is the prefix of current cell’s direct child parameter.

property parameter_layout_dict

parameter_layout_dict represents the tensor layout of a parameter, which is inferred by shard strategy and distributed operator information.

parameters_and_names(name_prefix='', expand=True)

Returns an iterator over cell parameters.

Includes the parameter’s name and itself.

参数
  • name_prefix (str) – Namespace. Default: ‘’.

  • expand (bool) – If true, yields parameters of this cell and all subcells. Otherwise, only yield parameters that are direct members of this cell. Default: True.

返回

Iteration, all the names and corresponding parameters in the cell.

实际案例

>>> n = Net()
>>> names = []
>>> for m in n.parameters_and_names():
...     if m[0]:
...         names.append(m[0])
parameters_broadcast_dict(recurse=True)

Gets the parameters broadcast dictionary of this cell.

参数

recurse (bool) – Whether contains the parameters of subcells. Default: True.

返回

OrderedDict, return parameters broadcast dictionary.

parameters_dict(recurse=True)

Gets parameters dictionary.

Gets the parameters dictionary of this cell.

参数

recurse (bool) – Whether contains the parameters of subcells. Default: True.

返回

OrderedDict, return parameters dictionary.

recompute(**kwargs)

Set the cell recomputed. All the primitive in the cell will be set recomputed. If a primitive set recomputed feeds into some backward nodes for computing gradient, rather than storing the intermediate activation computed in forward pass, we will recompute it in backward pass.

注解

  • If the computation involves something like randomization or global variable, the equivalence is not guaranteed currently.

  • If the recompute api of a primitive in this cell is also called, the recompute mode of this primitive is subject to the recompute api of the primitive.

  • The interface can be configured only once. Therefore, when the parent cell is configured, the child cell should not be configured.

  • When the memory remains after applying the recompute, configuring ‘mp_comm_recompute=False’ to improve performance if necessary.

  • When the memory still not enough after applying the recompute, configuring ‘parallel_optimizer_comm_recompute=True’ to save more memory if necessary. Cells in the same fusion group should has the same parallel_optimizer_comm_recompute configures.

参数
  • mp_comm_recompute (bool) – Specifies whether the model parallel communication operators in the cell are recomputed in auto parallel or semi auto parallel mode. Default: True.

  • parallel_optimizer_comm_recompute (bool) – Specifies whether the communication operator allgathers introduced by optimizer shard are recomputed in auto parallel or semi auto parallel mode. Default: False.

register_backward_hook(fn)

Set the cell backward hook function. Note that this function is only supported in pynative mode.

注解

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 next cell or primitive, which may be modified and returned. hook_fn(cell_name, grad_input, grad_output) -> Tensor or None.

参数

fn (function) – Specifies the hook function with grad as input.

remove_redundant_parameters()

Remove the redundant parameters.

This interface usually need not to be used explicitly.

set_auto_parallel()

Set the cell to auto parallel mode.

注解

If a cell needs to use the auto parallel or semi auto parallel mode for training, evaluation or prediction, this interface needs to be called by the cell.

set_boost(boost_type)

In order to improve the network performance, configure the network auto enable to accelerate the algorithm in the algorithm library.

If boost_type is not in the algorithm library, Please view the algorithm in the algorithm library through algorithm library.

注解

Some acceleration algorithms may affect the accuracy of the network, please choose carefully.

参数

boost_type (str) – accelerate algorithm.

返回

Cell, the cell itself.

引发

ValueError – If boost_type is not in the algorithm library.

set_broadcast_flag(mode=True)

Set the cell to data_parallel mode.

The cell can be accessed as an attribute using the given name.

参数

mode (bool) – Specifies whether the model is data_parallel. Default: True.

set_comm_fusion(fusion_type, recurse=True)

Set comm_fusion for all the parameters in the Net. Please refer to the description of mindspore.common.parameter.comm_fusion.

注解

The value of attribute will be overwritten when the function is called multiply.

参数
  • fusion_type (int) – The value of comm_fusion.

  • recurse (bool) – Whether sets the trainable parameters of subcells. Default: True.

set_grad(requires_grad=True)

Sets the cell flag for gradient. In pynative mode, this parameter specifies whether the network require gradients. If true, the backward network needed to compute the gradients will be generated when the forward network is executed.

参数

requires_grad (bool) – Specifies if the net need to grad, if it is true, the cell will construct backward network in pynative mode. Default: True.

返回

Cell, the cell itself.

set_parallel_input_with_inputs(*inputs)

Slice inputs tensors by parallel strategies, and set the sliced inputs to _parallel_input_run

参数

inputs (tuple) – inputs of construct method.

set_param_fl(push_to_server=False, pull_from_server=False, requires_aggr=True)

Set the way of parameter and server interaction.

参数
  • push_to_server (bool) – Whether the parameter should be pushed to server. Default: False.

  • pull_from_server (bool) – Whether the parameter should be pulled from server. Default: False.

  • requires_aggr (bool) – Whether the parameter should be aggregated in the server. Default: True.

set_param_ps(recurse=True, init_in_server=False)

Set whether the trainable parameters are updated by parameter server and whether the trainable parameters are initialized on server.

注解

It only works when a running task is in the parameter server mode.

参数
  • recurse (bool) – Whether sets the trainable parameters of subcells. Default: True.

  • init_in_server (bool) – Whether trainable parameters updated by parameter server are initialized on server. Default: False.

set_train(mode=True)

Sets the cell to training mode.

The cell itself and all children cells will be set to training mode. Layers that have different constructions for training and predicting, such as BatchNorm, will distinguish between the branches by this attribute. If set to true, the training branch will be executed, otherwise another branch.

参数

mode (bool) – Specifies whether the model is training. Default: True.

返回

Cell, the cell itself.

to_float(dst_type)

Add cast on all inputs of cell and child cells to run with certain float type.

If dst_type is mindspore.dtype.float16, all the inputs of Cell including input, Parameter, Tensor as const will be cast to float16. Please refer to the usage in source code of mindspore.train.amp.build_train_network.

注解

Multiple calls will overwrite.

参数

dst_type (mindspore.dtype) – Transfer cell to run with dst_type. dst_type can be mindspore.dtype.float16 or mindspore.dtype.float32.

返回

Cell, the cell itself.

引发

ValueError – If dst_type is not float32 or float16.

trainable_params(recurse=True)

Returns all trainable parameters.

Returns a list of all trainable parameters.

参数

recurse (bool) – Whether contains the trainable parameters of subcells. Default: True.

返回

List, the list of trainable parameters.

untrainable_params(recurse=True)

Returns all untrainable parameters.

Returns a list of all untrainable parameters.

参数

recurse (bool) – Whether contains the untrainable parameters of subcells. Default: True.

返回

List, the list of untrainable parameters.

update_cell_prefix()

Update the all child cells’ self.param_prefix.

After being invoked, it can get all the cell’s children’s name prefix by ‘_param_prefix’.

update_cell_type(cell_type)

The current cell type is updated when a quantization aware training network is encountered.

After being invoked, it can set the cell type to ‘cell_type’.

update_parameters_name(prefix='', recurse=True)

Updates the names of parameters with given prefix string.

Adds the given prefix to the names of parameters.

参数
  • prefix (str) – The prefix string. Default: ‘’.

  • recurse (bool) – Whether contains the parameters of subcells. Default: True.

Layer module contains pre-defined building blocks or computing units to construct neural networks.

The high-level components (Layers) used to construct the neural network.

class tinyms.layers.SequentialLayer(*args)[源代码]

Sequential layer container.

A list of Layers will be added to it in the order they are passed in the constructor. Alternatively, an ordered dict of cells can also be passed in.

参数

args (Union[list, OrderedDict]) – List of subclass of Layer.

引发

TypeError – If the type of the argument is not list or OrderedDict.

Inputs:
  • input (Tensor) - Tensor with shape according to the first Cell in the sequence.

Outputs:

Tensor, the output Tensor with shape depending on the input and defined sequence of Layers.

实际案例

>>> import tinyms as ts
>>> from tinyms.layers import SequentialLayer, Conv2d, ReLU
>>>
>>> seq_layer = SequentialLayer([Conv2d(3, 2, 3, pad_mode='valid', weight_init="ones"), ReLU()])
>>> x = ts.ones([1, 3, 4, 4])
>>> print(seq_layer(x))
[[[[27. 27.]
   [27. 27.]]
  [[27. 27.]
   [27. 27.]]]]
class tinyms.layers.LayerList(*args, **kwargs)[源代码]

Holds Layers in a list.

LayerList can be used like a regular Python list, support ‘__getitem__’, ‘__setitem__’, ‘__delitem__’, ‘__len__’, ‘__iter__’ and ‘__iadd__’, but layers it contains are properly registered, and will be visible by all Layer methods.

参数

args (list, optional) – List of subclass of Layer.

实际案例

>>> from tinyms.layers import LayerList, Conv2d, BatchNorm2d, ReLU
>>>
>>> conv = nn.Conv2d(100, 20, 3)
>>> layers = LayerList([BatchNorm2d(20)])
>>> layers.insert(0, Conv2d(100, 20, 3))
>>> layers.append(ReLU())
>>> layers
LayerList<
  (0): Conv2d<input_channels=100, ..., bias_init=None>
  (1): BatchNorm2d<num_features=20, ..., moving_variance=Parameter (name=variance)>
  (2): ReLU<>
  >
class tinyms.layers.TimeDistributed(layer, time_axis, reshape_with_axis=None)[源代码]

The time distributed layer.

Time distributed is a wrapper which allows to apply a layer to every temporal slice of an input. And the x should be at least 3D. There are two cases in the implementation. When reshape_with_axis provided, the reshape method will be chosen, which is more efficient; otherwise, the method of dividing the inputs along time axis will be used, which is more general. For example, reshape_with_axis could not be provided when deal with Batch Normalization.

参数
  • layer (Union[Cell, Primitive]) – The Cell or Primitive which will be wrapped.

  • time_axis (int) – The axis of time_step.

  • reshape_with_axis (int) – The axis which will be reshaped with time_axis. Default: None.

Inputs:
  • x (Tensor) - Tensor of shape \((N, T, *)\), where \(*\) means any number of additional dimensions.

Outputs:

Tensor of shape \((N, T, *)\)

Supported Platforms:

Ascend GPU CPU

引发

TypeError – If layer is not a Cell or Primitive.

实际案例

>>> x = Tensor(np.random.random([32, 10, 3]), mindspore.float32)
>>> dense = nn.Dense(3, 6)
>>> net = nn.TimeDistributed(dense, time_axis=1, reshape_with_axis=0)
>>> output = net(x)
>>> print(output.shape)
(32, 10, 6)
class tinyms.layers.ForwardValueAndGrad(network, weights=None, get_all=False, get_by_list=False, sens_param=False)[源代码]

Network training package class.

Including the network and a gradient function. The resulting Cell is trained with input ‘*inputs’. The backward graph will be created in the gradient function to calculating gradient.

参数
  • network (Cell) – The training network.

  • weights (ParameterTuple) – The parameters of the training network that need to calculate the gradient.

  • get_all (bool) – If True, get all the gradients with respect to inputs. Default: False.

  • get_by_list (bool) – If True, get all the gradients with respect to Parameter variables. If get_all and get_by_list are both False, get the gradient with respect to first input. If get_all and get_by_list are both True, get the gradients with respect to inputs and Parameter variables at the same time in the form of ((gradients with respect to inputs), (gradients with respect to parameters)). Default: False.

  • sens_param (bool) – Whether to append sensitivity (gradient with respect to output) as input. If sens_param is False, a ‘ones_like(outputs)’ sensitivity will be attached automatically. Default: False. If the sens_param is True, a sensitivity (gradient with respect to output) needs to be transferred through the input parameter.

Inputs:
  • (*inputs) (Tuple(Tensor…)) - Tuple of inputs with shape \((N, \ldots)\).

  • (sens) - A sensitivity (gradient with respect to output) as the input of backpropagation. If network has single output, the sens is a tensor. If network has multiple outputs, the sens is the tuple(tensor).

Outputs:
  • forward value - The result of network forward running.

  • gradients (tuple(tensor)) - The gradients of network parameters and inputs.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> class Net(nn.Cell):
...    def __init__(self):
...        super(Net, self).__init__()
...        self.weight = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="weight")
...        self.matmul = P.MatMul()
...
...    def construct(self, x):
...        out = self.matmul(x, self.weight)
...        return out
...
>>> net = Net()
>>> criterion = nn.SoftmaxCrossEntropyWithLogits()
>>> net_with_criterion = nn.WithLossCell(net, criterion)
>>> weight = ParameterTuple(net.trainable_params())
>>> train_network = nn.ForwardValueAndGrad(net_with_criterion, weights=weight, get_all=True, get_by_list=True)
>>> inputs = Tensor(np.ones([1, 2]).astype(np.float32))
>>> labels = Tensor(np.zeros([1, 2]).astype(np.float32))
>>> result = train_network(inputs, labels)
>>> print(result)
(Tensor(shape=[1], dtype=Float32, value=[0.00000000e+00]), ((Tensor(shape=[1, 2], dtype=Float32, value=
[[1.00000000e+00, 1.00000000e+00]]), Tensor(shape=[1, 2], dtype=Float32, value=
[[0.00000000e+00, 0.00000000e+00]])), (Tensor(shape=[2, 2], dtype=Float32, value=
[[5.00000000e-01, 5.00000000e-01],
 [5.00000000e-01, 5.00000000e-01]]),)))
class tinyms.layers.TrainOneStepCell(network, optimizer, sens=1.0)[源代码]

Network training package class.

Wraps the network with an optimizer. The resulting Cell is trained with input ‘*inputs’. The backward graph will be created in the construct function to update the parameter. Different parallel modes are available for training.

参数
  • network (Cell) – The training network. The network only supports single output.

  • optimizer (Union[Cell]) – Optimizer for updating the weights.

  • sens (numbers.Number) – The scaling number to be filled as the input of backpropagation. Default value is 1.0.

Inputs:
  • (*inputs) (Tuple(Tensor)) - Tuple of input tensors with shape \((N, \ldots)\).

Outputs:

Tensor, a tensor means the loss value, the shape of which is usually \(()\).

引发

TypeError – If sens is not a number.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> loss_fn = nn.SoftmaxCrossEntropyWithLogits()
>>> optim = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> #1) Using the WithLossCell existing provide
>>> loss_net = nn.WithLossCell(net, loss_fn)
>>> train_net = nn.TrainOneStepCell(loss_net, optim)
>>>
>>> #2) Using user-defined WithLossCell
>>> class MyWithLossCell(Cell):
...    def __init__(self, backbone, loss_fn):
...        super(MyWithLossCell, self).__init__(auto_prefix=False)
...        self._backbone = backbone
...        self._loss_fn = loss_fn
...
...    def construct(self, x, y, label):
...        out = self._backbone(x, y)
...        return self._loss_fn(out, label)
...
...    @property
...    def backbone_network(self):
...        return self._backbone
...
>>> loss_net = MyWithLossCell(net, loss_fn)
>>> train_net = nn.TrainOneStepCell(loss_net, optim)
class tinyms.layers.WithLossCell(backbone, loss_fn)[源代码]

Cell with loss function.

Wraps the network with loss function. This Cell accepts data and label as inputs and the computed loss will be returned.

参数
  • backbone (Cell) – The target network to wrap.

  • loss_fn (Cell) – The loss function used to compute loss.

Inputs:
  • data (Tensor) - Tensor of shape \((N, \ldots)\).

  • label (Tensor) - Tensor of shape \((N, \ldots)\).

Outputs:

Tensor, a tensor means the loss value, the shape of which is usually \(()\).

引发

TypeError – If dtype of data or label is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
>>> net_with_criterion = nn.WithLossCell(net, loss_fn)
>>>
>>> batch_size = 2
>>> data = Tensor(np.ones([batch_size, 1, 32, 32]).astype(np.float32) * 0.01)
>>> label = Tensor(np.ones([batch_size, 10]).astype(np.float32))
>>>
>>> output_data = net_with_criterion(data, label)
property backbone_network

Get the backbone network.

返回

Cell, the backbone network.

class tinyms.layers.WithGradCell(network, loss_fn=None, sens=None)[源代码]

Cell that returns the gradients.

Wraps the network with backward cell to compute gradients. A network with a loss function is necessary as argument. If loss function in None, the network must be a wrapper of network and loss function. This Cell accepts ‘*inputs’ as inputs and returns gradients for each trainable parameter.

注解

Run in PyNative mode.

参数
  • network (Cell) – The target network to wrap. The network only supports single output.

  • loss_fn (Cell) – Primitive loss function used to compute gradients. Default: None.

  • sens (Union[None, Tensor, Scalar, Tuple ...]) – The sensitive for backpropagation, the type and shape must be same as the network output. If None, we will fill one to a same type shape of output value. Default: None.

Inputs:
  • (*inputs) (Tuple(Tensor)) - Tuple of input tensors with shape \((N, \ldots)\).

Outputs:

list, a list of Tensors with identical shapes as trainable weights.

引发

TypeError – If sens is not one of None, Tensor, Scalar or Tuple.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # For a defined network Net without loss function
>>> net = Net()
>>> loss_fn = nn.SoftmaxCrossEntropyWithLogits()
>>> grad_net = nn.WithGradCell(net, loss_fn)
>>>
>>> # For a network wrapped with loss function
>>> net = Net()
>>> net_with_criterion = nn.WithLossCell(net, loss_fn)
>>> grad_net = nn.WithGradCell(net_with_criterion)
class tinyms.layers.PipelineCell(network, micro_size)[源代码]

Wrap the network with Micro Batch.

注解

micro_size must be greater or equal to pipeline stages.

参数
  • network (Cell) – The target network to wrap.

  • micro_size (int) – MicroBatch size.

实际案例

>>> net = Net()
>>> net = PipelineCell(net, 4)
class tinyms.layers.WithEvalCell(network, loss_fn, add_cast_fp32=False)[源代码]

Cell that returns loss, output and label for evaluation.

This Cell accepts a network and loss function as arguments and computes loss for model. It returns loss, output and label to calculate the metrics.

参数
  • network (Cell) – The network Cell.

  • loss_fn (Cell) – The loss Cell.

  • add_cast_fp32 (bool) – Adjust the data type to float32.

Inputs:
  • data (Tensor) - Tensor of shape \((N, \ldots)\).

  • label (Tensor) - Tensor of shape \((N, \ldots)\).

Outputs:

Tuple, containing a scalar loss Tensor, a network output Tensor of shape \((N, \ldots)\) and a label Tensor of shape \((N, \ldots)\).

引发

TypeError – If add_cast_fp32 is not a bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # For a defined network Net without loss function
>>> net = Net()
>>> loss_fn = nn.SoftmaxCrossEntropyWithLogits()
>>> eval_net = nn.WithEvalCell(net, loss_fn)
class tinyms.layers.GetNextSingleOp(dataset_types, dataset_shapes, queue_name)[源代码]

Cell to run for getting the next operation.

参数
  • dataset_types (list[mindspore.dtype]) – The types of dataset.

  • dataset_shapes (list[tuple[int]]) – The shapes of dataset.

  • queue_name (str) – Queue name to fetch the data.

For detailed information, refer to ops.operations.GetNext.

Inputs:

No inputs.

Outputs:

tuple[Tensor], the data get from Dataset.

Supported Platforms:

Ascend GPU

实际案例

>>> train_dataset = create_custom_dataset()
>>> dataset_helper = mindspore.DatasetHelper(train_dataset, dataset_sink_mode=True)
>>> dataset = dataset_helper.iter.dataset
>>> dataset_types, dataset_shapes = dataset_helper.types_shapes()
>>> queue_name = dataset.__transfer_dataset__.queue_name
>>> get_next_single_op_net = nn.GetNextSingleOp(dataset_types, dataset_shapes, queue_name)
>>> data, label = get_next_single_op_net()
>>> relu = P.ReLU()
>>> result = relu(data).asnumpy()
>>> print(result.shape)
(32, 1, 32, 32)
class tinyms.layers.TrainOneStepWithLossScaleCell(network, optimizer, scale_sense)[源代码]

Network training with loss scaling.

This is a training step with loss scaling. It takes a network, an optimizer and possibly a scale update Cell as args. The loss scale value can be updated in both host side or device side. The TrainOneStepWithLossScaleCell will be compiled to be graph which takes *inputs as input data. The Tensor type of scale_sense is acting as loss scaling value. If you want to update it on host side, the value must be provided. If the Tensor type of scale_sense is not given, the loss scale update logic must be provied by Cell type of scale_sense.

参数
  • network (Cell) – The training network. The network only supports single output.

  • optimizer (Cell) – Optimizer for updating the weights.

  • scale_sense (Union[Tensor, Cell]) – If this value is Cell type, the loss scaling update logic cell.If this value is Tensor type, Tensor with shape \(()\) or \((1,)\).

Inputs:
  • (*inputs) (Tuple(Tensor)) - Tuple of input tensors with shape \((N, \ldots)\).

Outputs:

Tuple of 3 Tensor, the loss, overflow flag and current loss scaling value.

  • loss (Tensor) - Tensor with shape \(()\).

  • overflow (Tensor) - Tensor with shape \(()\), type is bool.

  • loss scaling value (Tensor) - Tensor with shape \(()\)

引发
  • TypeError – If scale_sense is neither Cell nor Tensor.

  • ValueError – If shape of scale_sense is neither (1,) nor ().

Supported Platforms:

Ascend GPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor, Parameter, nn, ops
>>> from mindspore import dtype as mstype
>>>
>>> class Net(nn.Cell):
...     def __init__(self, in_features, out_features):
...         super(Net, self).__init__()
...         self.weight = Parameter(Tensor(np.ones([in_features, out_features]).astype(np.float32)),
...                                 name='weight')
...         self.matmul = ops.MatMul()
...
...     def construct(self, x):
...         output = self.matmul(x, self.weight)
...         return output
...
>>> size, in_features, out_features = 16, 16, 10
>>> #1) when the type of scale_sense is Cell:
>>> net = Net(in_features, out_features)
>>> loss = nn.MSELoss()
>>> optimizer = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> net_with_loss = nn.WithLossCell(net, loss)
>>> manager = nn.DynamicLossScaleUpdateCell(loss_scale_value=2**12, scale_factor=2, scale_window=1000)
>>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_sense=manager)
>>> input = Tensor(np.ones([out_features, in_features]), mindspore.float32)
>>> labels = Tensor(np.ones([out_features,]), mindspore.float32)
>>> output = train_network(input, labels)
>>>
>>> #2) when the type of scale_sense is Tensor:
>>> net = Net(in_features, out_features)
>>> loss = nn.MSELoss()
>>> optimizer = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> net_with_loss = nn.WithLossCell(net, loss)
>>> inputs = Tensor(np.ones([size, in_features]).astype(np.float32))
>>> label = Tensor(np.zeros([size, out_features]).astype(np.float32))
>>> scaling_sens = Tensor(np.full((1), np.finfo(np.float32).max), dtype=mstype.float32)
>>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_sense=scaling_sens)
>>> output = train_network(inputs, label)
get_overflow_status(status, compute_output)[源代码]

Get floating-point overflow status.

Get overflow results after executing the target process for overflow detection.

Inputs:
  • status (object) - A status instance used to detect the overflow.

  • compute_output - Overflow detection should be performed on a certain computation. Set compute_output as the output of the computation, to ensure overflow status is acquired before executing the computation.

Outputs:

bool, whether the overflow occurs or not.

process_loss_scale(overflow)[源代码]

Calculate loss scale according to the overflow.

Inputs:
  • overflow (bool) - Whether the overflow occurs or not.

Outputs:

bool, overflow value.

set_sense_scale(sens)[源代码]

If the user has set the sens in the training process and wants to reassign the value, he can call this function again to make modification, and sens needs to be of type Tensor.

Inputs:
  • sens (Tensor) - The new sense whose shape and type are the same with original scale_sense.

start_overflow_check(pre_cond, compute_input)[源代码]

Start floating-point overflow detection. Create and clear the overflow detection state.

Specify the argument ‘pre_cond’ and ‘compute_input’ to make sure overflow status is cleared at the right time. Taking this situation as an example, we need to execute state clearing after loss calculation and then detect overflow in the process of gradient calculation. In this case, pre_cond should be the output of the loss function, and compute_input should be the input of gradients-computing function.

Inputs:
  • pre_cond (Tensor) - A precondition for starting overflow detection. It determines the executing order of overflow state clearing and prior processions. It makes sure that the function ‘start_overflow’ clears status after finishing the process of precondition.

  • compute_input (object) - The input of subsequent process. Overflow detection should be performed on a certain computation. Set compute_input as the input of the computation, to ensure overflow status is cleared before executing the computation.

Outputs:

Tuple[object, object], the first value is False for GPU backend, while it is a instance of NPUAllocFloatStatus for other backend. The status is used to detect overflow during overflow detection. The second value is the same as the input of compute_input, but contains some information about the execution order.

class tinyms.layers.DistributedGradReducer(parameters, mean=True, degree=None, fusion_type=1, group='hccl_world_group')[源代码]

A distributed optimizer.

Constructs a gradient reducer Cell, which applies communication and average operations on single-process gradient values.

参数
  • parameters (list) – the parameters to be updated.

  • mean (bool) – When mean is true, the mean coefficient (degree) would apply on gradients. Default: False.

  • degree (int) – The mean coefficient. Usually it equals to device number. Default: None.

  • fusion_type (int) – The type of all reduce fusion. Default: 1.

引发

ValueError – If degree is not a int or less than 0.

Supported Platforms:

Ascend GPU

实际案例

>>> # This example should be run with multiple processes.
>>> # Please refer to the tutorial > Distributed Training on mindspore.cn.
>>> import numpy as np
>>> from mindspore.communication import init
>>> from mindspore import ops
>>> from mindspore import context
>>> from mindspore.context import ParallelMode
>>> from mindspore import Parameter, Tensor
>>> from mindspore import nn
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
>>> context.reset_auto_parallel_context()
>>> context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL)
>>>
>>> class TrainingWrapper(nn.Cell):
...     def __init__(self, network, optimizer, sens=1.0):
...         super(TrainingWrapper, self).__init__(auto_prefix=False)
...         self.network = network
...         self.network.add_flags(defer_inline=True)
...         self.weights = optimizer.parameters
...         self.optimizer = optimizer
...         self.grad = ops.GradOperation(get_by_list=True, sens_param=True)
...         self.sens = sens
...         self.reducer_flag = False
...         self.grad_reducer = None
...         self.parallel_mode = context.get_auto_parallel_context("parallel_mode")
...         if self.parallel_mode in [ParallelMode.DATA_PARALLEL, ParallelMode.HYBRID_PARALLEL]:
...             self.reducer_flag = True
...         if self.reducer_flag:
...             mean = context.get_auto_parallel_context("gradients_mean")
...             degree = context.get_auto_parallel_context("device_num")
...             self.grad_reducer = nn.DistributedGradReducer(optimizer.parameters, mean, degree)
...
...     def construct(self, *args):
...         weights = self.weights
...         loss = self.network(*args)
...         sens = ops.Fill()(ops.DType()(loss), ops.Shape()(loss), self.sens)
...         grads = self.grad(self.network, weights)(*args, sens)
...         if self.reducer_flag:
...             # apply grad reducer on grads
...             grads = self.grad_reducer(grads)
...         return ops.Depend(loss, self.optimizer(grads))
>>>
>>> class Net(nn.Cell):
...     def __init__(self, in_features, out_features):
...         super(Net, self).__init__()
...         self.weight = Parameter(Tensor(np.ones([in_features, out_features]).astype(np.float32)),
...                                 name='weight')
...         self.matmul = ops.MatMul()
...
...     def construct(self, x):
...         output = self.matmul(x, self.weight)
...         return output
>>>
>>> size, in_features, out_features = 16, 16, 10
>>> network = Net(in_features, out_features)
>>> loss = nn.MSELoss()
>>> net_with_loss = nn.WithLossCell(network, loss)
>>> optimizer = nn.Momentum(net_with_loss.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> train_cell = TrainingWrapper(net_with_loss, optimizer)
>>> inputs = Tensor(np.ones([size, in_features]).astype(np.float32))
>>> label = Tensor(np.zeros([size, out_features]).astype(np.float32))
>>> grads = train_cell(inputs, label)
>>> print(grads)
256.0
construct(grads)[源代码]

Under certain circumstances, the data precision of grads could be mixed with float16 and float32. Thus, the result of AllReduce is unreliable. To solve the problem, grads must be cast to float32 before AllReduce, and cast back after the operation.

参数

grads (Union[Tensor, tuple[Tensor]]) – The gradient tensor or tuple before operation.

返回

new_grads (Union[Tensor, tuple[Tensor]]), the gradient tensor or tuple after operation.

class tinyms.layers.ParameterUpdate(param)[源代码]

Cell that updates parameter.

With this Cell, one can manually update param with the input Tensor.

参数

param (Parameter) – The parameter to be updated manually.

Inputs:
  • x (Tensor) - A tensor whose shape and type are the same with param.

Outputs:

Tensor, the input x.

引发

KeyError – If parameter with the specified name does not exist.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> network = nn.Dense(3, 4)
>>> param = network.parameters_dict()['weight']
>>> update = nn.ParameterUpdate(param)
>>> update.phase = "update_param"
>>> weight = Tensor(np.arange(12).reshape((4, 3)), mindspore.float32)
>>> output = update(weight)
class tinyms.layers.DynamicLossScaleUpdateCell(loss_scale_value, scale_factor, scale_window)[源代码]

Dynamic Loss scale update cell.

For loss scaling training, the initial loss scaling value will be set to be loss_scale_value. In each training step, the loss scaling value will be updated by loss scaling value/scale_factor when there is an overflow. And it will be increased by loss scaling value * scale_factor if there is no overflow for a continuous scale_window steps. This cell is used for Graph mode training in which all logic will be executed on device side(Another training mode is normal(non-sink) mode in which some logic will be executed on host).

参数
  • loss_scale_value (float) – Initializes loss scale.

  • scale_factor (int) – Coefficient of increase and decrease.

  • scale_window (int) – Maximum continuous training steps that do not have overflow.

Inputs:
  • loss_scale (Tensor) - The loss scale value during training with shape \(()\).

  • overflow (bool) - Whether the overflow occurs or not.

Outputs:

bool, the input overflow.

引发

TypeError – If dtype of inputs or label is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor, Parameter, nn
>>> import mindspore.ops as ops
>>>
>>> class Net(nn.Cell):
...     def __init__(self, in_features, out_features):
...         super(Net, self).__init__()
...         self.weight = Parameter(Tensor(np.ones([in_features, out_features]).astype(np.float32)),
...                                 name='weight')
...         self.matmul = ops.MatMul()
...
...     def construct(self, x):
...         output = self.matmul(x, self.weight)
...         return output
...
>>> in_features, out_features = 16, 10
>>> net = Net(in_features, out_features)
>>> loss = nn.MSELoss()
>>> optimizer = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> net_with_loss = nn.WithLossCell(net, loss)
>>> manager = nn.DynamicLossScaleUpdateCell(loss_scale_value=2**12, scale_factor=2, scale_window=1000)
>>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_sense=manager)
>>> input = Tensor(np.ones([out_features, in_features]), mindspore.float32)
>>> labels = Tensor(np.ones([out_features,]), mindspore.float32)
>>> output = train_network(input, labels)
get_loss_scale()[源代码]

Get Loss Scale value.

class tinyms.layers.FixedLossScaleUpdateCell(loss_scale_value)[源代码]

Static scale update cell, the loss scaling value will not be updated.

For usage, refer to DynamicLossScaleUpdateCell.

参数

loss_scale_value (float) – Initializes loss scale.

Inputs:
  • loss_scale (Tensor) - The loss scale value during training with shape \(()\), that will be ignored.

  • overflow (bool) - Whether the overflow occurs or not.

Outputs:

bool, the input overflow.

Supported Platforms:

Ascend GPU

实际案例

>>> import numpy as np
>>> from mindspore import Tensor, Parameter, nn, ops
>>>
>>> class Net(nn.Cell):
...     def __init__(self, in_features, out_features):
...         super(Net, self).__init__()
...         self.weight = Parameter(Tensor(np.ones([in_features, out_features]).astype(np.float32)),
...                                 name='weight')
...         self.matmul = ops.MatMul()
...
...     def construct(self, x):
...         output = self.matmul(x, self.weight)
...         return output
...
>>> in_features, out_features = 16, 10
>>> net = Net(in_features, out_features)
>>> loss = nn.MSELoss()
>>> optimizer = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> net_with_loss = nn.WithLossCell(net, loss)
>>> manager = nn.FixedLossScaleUpdateCell(loss_scale_value=2**12)
>>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_sense=manager)
>>> input = Tensor(np.ones([out_features, in_features]), mindspore.float32)
>>> labels = Tensor(np.ones([out_features,]), mindspore.float32)
>>> output = train_network(input, labels)
get_loss_scale()[源代码]

Get Loss Scale value.

class tinyms.layers.VirtualDatasetCellTriple(backbone)[源代码]

Wrap the network with virtual dataset to convert data parallel layout to model parallel layout.

VirtualDatasetCellTriple is a virtual Primitive, it does not exist in the final executing graph. Inputs and outputs of VirtualDatasetCellTriple are distributed in data parallel pattern, tensor redistribution Primitives is inserted dynamically during the graph compile process.

注解

Only used in semi auto parallel and auto parallel mode. There are three inputs, as contrary to two inputs in _VirtualDatasetCell.

参数

backbone (Cell) – The target network to wrap.

实际案例

>>> net = Net()
>>> net = VirtualDatasetCellTriple(net)
class tinyms.layers.Softmax(axis=-1)[源代码]

Softmax activation function.

Applies the Softmax function to an n-dimensional input Tensor.

The input is a Tensor of logits transformed with exponential function and then normalized to lie in range [0, 1] and sum up to 1.

Softmax is defined as:

\[\text{softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_{j=0}^{n-1}\exp(x_j)},\]

where \(x_{i}\) is the \(i\)-th slice in the given dimension of the input Tensor.

参数

axis (Union[int, tuple[int]]) – The axis to apply Softmax operation, -1 means the last dimension. Default: -1.

Inputs:
  • x (Tensor) - The input of Softmax with data type of float16 or float32.

Outputs:

Tensor, which has the same type and shape as x with values in the range[0,1].

引发
  • TypeError – If axis is neither an int nor a tuple.

  • TypeError – If dtype of x is neither float16 nor float32.

  • ValueError – If axis is a tuple whose length is less than 1.

  • ValueError – If axis is a tuple whose elements are not all in range [-len(x), len(x)).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> softmax = nn.Softmax()
>>> output = softmax(x)
>>> print(output)
[0.03168 0.01166 0.0861  0.636   0.2341 ]
class tinyms.layers.LogSoftmax(axis=-1)[源代码]

LogSoftmax activation function.

Applies the LogSoftmax function to n-dimensional input tensor.

The input is transformed by the Softmax function and then by the log function to lie in range[-inf,0).

Logsoftmax is defined as:

\[\text{logsoftmax}(x_i) = \log \left(\frac{\exp(x_i)}{\sum_{j=0}^{n-1} \exp(x_j)}\right),\]

where \(x_{i}\) is the \(i\)-th slice in the given dimension of the input Tensor.

参数

axis (int) – The axis to apply LogSoftmax operation, -1 means the last dimension. Default: -1.

Inputs:
  • x (Tensor) - The input of LogSoftmax, with float16 or float32 data type.

Outputs:

Tensor, which has the same type and shape as the input as x with values in the range[-inf,0).

引发
  • TypeError – If axis is not an int.

  • TypeError – If dtype of x is neither float16 nor float32.

  • ValueError – If axis is not in range [-len(x), len(x)).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> log_softmax = nn.LogSoftmax()
>>> output = log_softmax(x)
>>> print(output)
[[-5.00672150e+00 -6.72150636e-03 -1.20067215e+01]
 [-7.00091219e+00 -1.40009127e+01 -9.12250078e-04]]
class tinyms.layers.ReLU[源代码]

Rectified Linear Unit activation function.

Applies the rectified linear unit function element-wise.

\[\text{ReLU}(x) = (x)^+ = \max(0, x),\]

It returns element-wise \(\max(0, x)\), specially, the neurons with the negative output will be suppressed and the active neurons will stay the same.

The picture about ReLU looks like this ReLU.

Inputs:
  • x (Tensor) - The input of ReLU. The data type is Number. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发

TypeError – If dtype of x is not a number.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-1, 2, -3, 2, -1]), mindspore.float16)
>>> relu = nn.ReLU()
>>> output = relu(x)
>>> print(output)
[0. 2. 0. 2. 0.]
class tinyms.layers.ReLU6[源代码]

Compute ReLU6 activation function.

ReLU6 is similar to ReLU with a upper limit of 6, which if the inputs are greater than 6, the outputs will be suppressed to 6. It computes element-wise as

\[\min(\max(0, x), 6).\]

The input is a Tensor of any valid shape.

Inputs:
  • x (Tensor) - The input of ReLU6 with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, which has the same type as x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> relu6 = nn.ReLU6()
>>> output = relu6(x)
>>> print(output)
[0. 0. 0. 2. 1.]
class tinyms.layers.Tanh[源代码]

Tanh activation function.

Applies the Tanh function element-wise, returns a new tensor with the hyperbolic tangent of the elements of input, The input is a Tensor with any valid shape.

Tanh function is defined as:

\[tanh(x_i) = \frac{\exp(x_i) - \exp(-x_i)}{\exp(x_i) + \exp(-x_i)} = \frac{\exp(2x_i) - 1}{\exp(2x_i) + 1},\]

where \(x_i\) is an element of the input Tensor.

Inputs:
  • x (Tensor) - The input of Tanh with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([1, 2, 3, 2, 1]), mindspore.float16)
>>> tanh = nn.Tanh()
>>> output = tanh(x)
>>> print(output)
[0.7617 0.964  0.995  0.964  0.7617]
class tinyms.layers.GELU[源代码]

Gaussian error linear unit activation function.

Applies GELU function to each element of the input. The input is a Tensor with any valid shape.

GELU is defined as:

\[GELU(x_i) = x_i*P(X < x_i),\]

where \(P\) is the cumulative distribution function of standard Gaussian distribution and \(x_i\) is the element of the input.

The picture about GELU looks like this GELU.

Inputs:
  • x (Tensor) - The input of GELU with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> gelu = nn.GELU()
>>> output = gelu(x)
>>> print(output)
[[-1.5880802e-01  3.9999299e+00 -3.1077917e-21]
 [ 1.9545976e+00 -2.2918017e-07  9.0000000e+00]]
class tinyms.layers.FastGelu[源代码]

Fast Gaussian error linear unit activation function.

Applies FastGelu function to each element of the input. The input is a Tensor with any valid shape.

FastGelu is defined as:

\[FastGelu(x_i) = \frac {x_i} {1 + \exp(-1.702 * \left| x_i \right|)} * \exp(0.851 * (x_i - \left| x_i \right|))\]

where \(x_i\) is the element of the input.

Inputs:
  • x (Tensor) - The input of FastGelu with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> fast_gelu = nn.FastGelu()
>>> output = fast_gelu(x)
>>> print(output)
[[-1.5418735e-01  3.9921875e+00 -9.7473649e-06]
 [ 1.9375000e+00 -1.0052517e-03  8.9824219e+00]]
class tinyms.layers.Sigmoid[源代码]

Sigmoid activation function.

Applies sigmoid-type activation element-wise.

Sigmoid function is defined as:

\[\text{sigmoid}(x_i) = \frac{1}{1 + \exp(-x_i)},\]

where \(x_i\) is the element of the input.

The picture about Sigmoid looks like this Sigmoid.

Inputs:
  • x (Tensor) - The input of Sigmoid with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> sigmoid = nn.Sigmoid()
>>> output = sigmoid(x)
>>> print(output)
[0.2688  0.11914 0.5     0.881   0.7305 ]
class tinyms.layers.PReLU(channel=1, w=0.25)[源代码]

PReLU activation function.

Applies the PReLU function element-wise.

PReLU is defined as:

\[prelu(x_i)= \max(0, x_i) + w * \min(0, x_i),\]

where \(x_i\) is an element of an channel of the input.

Here \(w\) is a learnable parameter with a default initial value 0.25. Parameter \(w\) has dimensionality of the argument channel. If called without argument channel, a single parameter \(w\) will be shared across all channels.

The picture about PReLU looks like this PReLU.

参数
  • channel (int) – The elements number of parameter. It could be an int, and the value is 1 or the channels number of input tensor x. Default: 1.

  • w (Union[float, list, Tensor]) – The initial value of parameter. It could be a float, a float list or a tensor has the same dtype as the input tensor x. Default: 0.25.

Inputs:
  • x (Tensor) - The input of PReLU with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same dtype and shape as the x.

引发
  • TypeError – If channel is not an int.

  • TypeError – If w is not one of a float, a float list, a float Tensor.

  • TypeError – If dtype of x is neither float16 nor float32.

  • ValueError – If the x is a 0-D or 1-D Tensor on Ascend.

  • ValueError – If channel is less than 1.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([[[[0.1, 0.6], [0.9, 0.9]]]]), mindspore.float32)
>>> prelu = nn.PReLU()
>>> output = prelu(x)
>>> print(output)
[[[[0.1 0.6]
   [0.9 0.9]]]]
tinyms.layers.get_activation(name, prim_name=None)[源代码]

Gets the activation function.

参数

name (str) – The name of the activation function.

返回

Function, the activation function.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> sigmoid = nn.get_activation('sigmoid')
>>> print(sigmoid)
Sigmoid<>
class tinyms.layers.LeakyReLU(alpha=0.2)[源代码]

Leaky ReLU activation function.

LeakyReLU is similar to ReLU, but LeakyReLU has a slope that makes it not equal to 0 at x < 0. The activation function is defined as:

\[\text{leaky_relu}(x) = \begin{cases}x, &\text{if } x \geq 0; \cr \text{alpha} * x, &\text{otherwise.}\end{cases}\]

See https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf

参数

alpha (Union[int, float]) – Slope of the activation function at x < 0. Default: 0.2.

Inputs:
  • x (Tensor) - The input of LeakyReLU. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same type and shape as the x.

引发

TypeError – If alpha is not a float or an int.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32)
>>> leaky_relu = nn.LeakyReLU()
>>> output = leaky_relu(x)
>>> print(output)
[[-0.2  4.  -1.6]
 [ 2.  -1.   9. ]]
class tinyms.layers.HSigmoid[源代码]

Hard sigmoid activation function.

Applies hard sigmoid activation element-wise. The input is a Tensor with any valid shape.

Hard sigmoid is defined as:

\[\text{hsigmoid}(x_{i}) = max(0, min(1, \frac{x_{i} + 3}{6})),\]

where \(x_{i}\) is the \(i\)-th slice in the given dimension of the input Tensor.

Inputs:
  • input_x (Tensor) - The input of HSigmoid. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the input_x.

引发

TypeError – If input_x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> hsigmoid = nn.HSigmoid()
>>> result = hsigmoid(x)
>>> print(result)
[0.3333 0.1666 0.5    0.8335 0.6665]
class tinyms.layers.HSwish[源代码]

Hard swish activation function.

Applies hswish-type activation element-wise. The input is a Tensor with any valid shape.

Hard swish is defined as:

\[\text{hswish}(x_{i}) = x_{i} * \frac{ReLU6(x_{i} + 3)}{6},\]

where \(x_{i}\) is the \(i\)-th slice in the given dimension of the input Tensor.

Inputs:
  • x (Tensor) - The input of HSwish, data type must be float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

GPU CPU

实际案例

>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> hswish = nn.HSwish()
>>> result = hswish(x)
>>> print(result)
[-0.3333  -0.3333  0  1.666  0.6665]
class tinyms.layers.ELU(alpha=1.0)[源代码]

Exponential Linear Uint activation function.

Applies the exponential linear unit function element-wise. The activation function is defined as:

\[E_{i} = \begin{cases} x, &\text{if } x \geq 0; \cr \text{alpha} * (\exp(x_i) - 1), &\text{otherwise.} \end{cases}\]

The picture about ELU looks like this ELU.

参数

alpha (float) – The coefficient of negative factor whose type is float. Default: 1.0.

Inputs:
  • x (Tensor) - The input of ELU with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发
  • TypeError – If alpha is not a float.

  • TypeError – If dtype of x is neither float16 nor float32.

  • ValueError – If alpha is not equal to 1.0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float32)
>>> elu = nn.ELU()
>>> result = elu(x)
>>> print(result)
[-0.63212055  -0.86466473  0.  2.  1.]
class tinyms.layers.LogSigmoid[源代码]

Logsigmoid activation function.

Applies logsigmoid activation element-wise. The input is a Tensor with any valid shape.

Logsigmoid is defined as:

\[\text{logsigmoid}(x_{i}) = log(\frac{1}{1 + \exp(-x_i)}),\]

where \(x_{i}\) is the element of the input.

Inputs:
  • x (Tensor) - The input of LogSigmoid with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, with the same type and shape as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.LogSigmoid()
>>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
>>> output = net(x)
>>> print(output)
[-0.31326166 -0.12692806 -0.04858734]
class tinyms.layers.SoftShrink(lambd=0.5)[源代码]

Applies the soft shrinkage function elementwise.

\[\begin{split}\text{SoftShrink}(x) = \begin{cases} x - \lambda, & \text{ if } x > \lambda \\ x + \lambda, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases}\end{split}\]
参数

lambd – the \(\lambda\) must be no less than zero value for the Softshrink formulation. Default: 0.5.

Inputs:
  • input_x (Tensor) - The input of SoftShrink with data type of float16 or float32. Any number of additional dimensions.

Outputs:

Tensor, has the same shape and data type as input_x.

引发
  • TypeError – If lambd is not a float.

  • TypeError – If input_x is not a Tensor.

  • TypeError – If dtype of input_x is neither float16 nor float32.

  • ValueError – If lambd is less than 0.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([[ 0.5297,  0.7871,  1.1754], [ 0.7836,  0.6218, -1.1542]]), mstype.float16)
>>> softshrink = nn.SoftShrink()
>>> output = softshrink(input_x)
>>> print(output)
[[ 0.02979  0.287    0.676  ]
 [ 0.2837   0.1216  -0.6543 ]]
class tinyms.layers.HShrink(lambd=0.5)[源代码]

Applies the hard shrinkage function element-wise, each element complies the follow function:

\[\begin{split}\text{HardShrink}(x) = \begin{cases} x, & \text{ if } x > \lambda \\ x, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases}\end{split}\]
参数

lambd (float) – The value for the HardShrink formulation. Default: 0.5

Inputs:
  • input_x (Tensor) - The input of HardShrink with data type of float16 or float32.

Outputs:

Tensor, the same shape and data type as the input.

Supported Platforms:

Ascend

引发
  • TypeError – If lambd is not a float.

  • TypeError – If dtype of input_x is neither float16 nor float32.

实际案例

>>> input_x = Tensor(np.array([[ 0.5,  1,  2.0],[0.0533,0.0776,-2.1233]]),mstype.float32)
>>> hshrink = nn.HShrink()
>>> output = hshrink(input_x)
>>> print(output)
[[ 0.      1.      2.    ]
[ 0.      0.     -2.1233]]
class tinyms.layers.BatchNorm1d(num_features, eps=1e-05, momentum=0.9, affine=True, gamma_init='ones', beta_init='zeros', moving_mean_init='zeros', moving_var_init='ones', use_batch_statistics=None)[源代码]

Batch Normalization layer over a 2D input.

Batch Normalization is widely used in convolutional networks. This layer applies Batch Normalization over a 2D input (a mini-batch of 1D inputs) to reduce internal covariate shift as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. It rescales and recenters the feature using a mini-batch of data and the learned parameters which can be described in the following formula.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]

注解

The implementation of BatchNorm is different in graph mode and pynative mode, therefore the mode is not recommended to be changed after net was initialized.

参数
  • num_features (int) – C from an expected input of size (N, C).

  • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

  • momentum (float) – A floating hyperparameter of the momentum for the running_mean and running_var computation. Default: 0.9.

  • affine (bool) – A bool value. When set to True, gamma and beta can be learned. Default: True.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘ones’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘zeros’.

  • moving_mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving mean. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘zeros’.

  • moving_var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving variance. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘ones’.

  • use_batch_statistics (bool) – If true, use the mean value and variance value of current batch data. If false, use the mean value and variance value of specified value. If None, the training process will use the mean and variance of current batch data and track the running mean and variance, the evaluation process will use the running mean and variance. Default: None.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in})\).

Outputs:

Tensor, the normalized, scaled, offset tensor, of shape \((N, C_{out})\).

Supported Platforms:

Ascend GPU CPU

引发

实际案例

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor
>>> net = nn.BatchNorm1d(num_features=4)
>>> x = Tensor(np.array([[0.7, 0.5, 0.5, 0.6],
...                      [0.5, 0.4, 0.6, 0.9]]).astype(np.float32))
>>> output = net(x)
>>> print(output)
[[ 0.6999965   0.4999975  0.4999975  0.59999704 ]
 [ 0.4999975   0.399998   0.59999704 0.89999545 ]]
class tinyms.layers.BatchNorm2d(num_features, eps=1e-05, momentum=0.9, affine=True, gamma_init='ones', beta_init='zeros', moving_mean_init='zeros', moving_var_init='ones', use_batch_statistics=None, data_format='NCHW')[源代码]

Batch Normalization layer over a 4D input.

Batch Normalization is widely used in convolutional networks. This layer applies Batch Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) to avoid internal covariate shift as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. It rescales and recenters the feature using a mini-batch of data and the learned parameters which can be described in the following formula.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]

注解

The implementation of BatchNorm is different in graph mode and pynative mode, therefore that mode can not be changed after net was initialized. Note that the formula for updating the running_mean and running_var is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times x_t + \text{momentum} \times \hat{x}\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.

参数
  • num_features (int) – C from an expected input of size (N, C, H, W).

  • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

  • momentum (float) – A floating hyperparameter of the momentum for the running_mean and running_var computation. Default: 0.9.

  • affine (bool) – A bool value. When set to True, gamma and beta can be learned. Default: True.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘ones’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘zeros’.

  • moving_mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving mean. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘zeros’.

  • moving_var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving variance. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘ones’.

  • use_batch_statistics (bool) –

    • If true, use the mean value and variance value of current batch data and track running mean and running varance.

    • If false, use the mean value and variance value of specified value, and not track statistical value.

    • If None, The use_batch_statistics is automatically assigned process according to the training and eval mode. During training, batchnorm2d process will be the same with use_batch_statistics=True. Contrarily, in eval, batchnorm2d process will be the same with use_batch_statistics=False. Default: None.

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, the normalized, scaled, offset tensor, of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If num_features is not an int.

  • TypeError – If eps is not a float.

  • ValueError – If num_features is less than 1.

  • ValueError – If momentum is not in range [0, 1].

  • ValueError – If data_format is neither ‘NHWC’ not ‘NCHW’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor
>>> net = nn.BatchNorm2d(num_features=3)
>>> x = Tensor(np.ones([1, 3, 2, 2]).astype(np.float32))
>>> output = net(x)
>>> print(output)
[[[[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]
  [[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]
  [[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]]]
class tinyms.layers.BatchNorm3d(num_features, eps=1e-05, momentum=0.9, affine=True, gamma_init='ones', beta_init='zeros', moving_mean_init='zeros', moving_var_init='ones', use_batch_statistics=None, data_format='NCDHW')[源代码]

Batch Normalization layer over a 5D input.

Batch Normalization is widely used in convolutional networks. This layer applies Batch Normalization over a 5D input (a mini-batch of 3D inputs with additional channel dimension) to avoid internal covariate shift.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]

注解

The implementation of BatchNorm is different in graph mode and pynative mode, therefore that mode can not be changed after net was initialized. Note that the formula for updating the running_mean and running_var is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times x_t + \text{momentum} \times \hat{x}\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.

参数
  • num_features (int) – C from an expected input of size (N, C, D, H, W).

  • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

  • momentum (float) – A floating hyperparameter of the momentum for the running_mean and running_var computation. Default: 0.9.

  • affine (bool) – A bool value. When set to True, gamma and beta can be learned. Default: True.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘ones’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘zeros’.

  • moving_mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving mean. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘zeros’.

  • moving_var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving variance. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘ones’.

  • use_batch_statistics (bool) – If true, use the mean value and variance value of current batch data. If false, use the mean value and variance value of specified value. If None, the training process will use the mean and variance of current batch data and track the running mean and variance, the evaluation process will use the running mean and variance. Default: None.

  • data_format (str) – The optional value for data format is ‘NCDHW’. Default: ‘NCDHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, the normalized, scaled, offset tensor, of shape \((N, C_{out}, D_{out},H_{out}, W_{out})\).

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor
>>> net = nn.BatchNorm3d(num_features=3)
>>> x = Tensor(np.ones([16, 3, 10, 32, 32]).astype(np.float32))
>>> output = net(x)
>>> print(output.shape)
(16, 3, 10, 32, 32)
class tinyms.layers.LayerNorm(normalized_shape, begin_norm_axis=-1, begin_params_axis=-1, gamma_init='ones', beta_init='zeros', epsilon=1e-07)[源代码]

Applies Layer Normalization over a mini-batch of inputs.

Layer Normalization is widely used in recurrent neural networks. It applies normalization on a mini-batch of inputs for each single training case as described in the paper Layer Normalization. Unlike Batch Normalization, Layer Normalization performs exactly the same computation at training and testing time. It can be described using the following formula. It is applied across all channels and pixel but only one batch size.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]
参数
  • normalized_shape (Union(tuple[int], list[int]) – The normalization is performed over axis begin_norm_axis … R - 1.

  • begin_norm_axis (int) – The first normalization dimension: normalization will be performed along dimensions begin_norm_axis: rank(inputs), the value should be in [-1, rank(input)). Default: -1.

  • begin_params_axis (int) – The first parameter(beta, gamma)dimension: scale and centering parameters will have dimensions begin_params_axis: rank(inputs) and will be broadcast with the normalized inputs accordingly, the value should be in [-1, rank(input)). Default: -1.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘ones’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘zeros’.

  • epsilon (float) – A value added to the denominator for numerical stability. Default: 1e-7.

Inputs:
  • x (Tensor) - The shape of ‘x’ is \((x_1, x_2, ..., x_R)\), and input_shape[begin_norm_axis:] is equal to normalized_shape.

Outputs:

Tensor, the normalized and scaled offset tensor, has the same shape and data type as the x.

引发
  • TypeError – If normalized_shape is neither a list nor tuple.

  • TypeError – If begin_norm_axis or begin_params_axis is not an int.

  • TypeError – If epsilon is not a float.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.ones([20, 5, 10, 10]), mindspore.float32)
>>> shape1 = x.shape[1:]
>>> m = nn.LayerNorm(shape1,  begin_norm_axis=1, begin_params_axis=1)
>>> output = m(x).shape
>>> print(output)
(20, 5, 10, 10)
extend_repr()[源代码]

Display instance object as string.

class tinyms.layers.GroupNorm(num_groups, num_channels, eps=1e-05, affine=True, gamma_init='ones', beta_init='zeros')[源代码]

Group Normalization over a mini-batch of inputs.

Group Normalization is widely used in recurrent neural networks. It applies normalization on a mini-batch of inputs for each single training case as described in the paper Group Normalization. Group Normalization divides the channels into groups and computes within each group the mean and variance for normalization, and it performs very stable over a wide range of batch size. It can be described using the following formula.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]
参数
  • num_groups (int) – The number of groups to be divided along the channel dimension.

  • num_channels (int) – The number of channels per group.

  • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

  • affine (bool) – A bool value, this layer will have learnable affine parameters when set to true. Default: True.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘ones’. If gamma_init is a Tensor, the shape must be [num_channels].

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘zeros’. If beta_init is a Tensor, the shape must be [num_channels].

Inputs:
  • x (Tensor) - The input feature with shape [N, C, H, W].

Outputs:

Tensor, the normalized and scaled offset tensor, has the same shape and data type as the x.

引发
  • TypeError – If num_groups or num_channels is not an int.

  • TypeError – If eps is not a float.

  • TypeError – If affine is not a bool.

  • ValueError – If num_groups or num_channels is less than 1.

  • ValueError – If num_channels is not divided by num_groups.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> group_norm_op = nn.GroupNorm(2, 2)
>>> x = Tensor(np.ones([1, 2, 4, 4], np.float32))
>>> output = group_norm_op(x)
>>> print(output)
[[[[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]
  [[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]]]
extend_repr()[源代码]

Display instance object as string.

class tinyms.layers.GlobalBatchNorm(**kwargs)[源代码]

Global Batch Normalization layer over a N-dimension input.

Global Batch Normalization is cross device synchronized Batch Normalization. The implementation of Batch Normalization only normalizes the data within each device. Global Normalization will normalize the input within the group.It has been described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. It rescales and recenters the feature using a mini-batch of data and the learned parameters which can be described in the following formula.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]

注解

Currently, GlobalBatchNorm only supports 2D and 4D inputs.

参数
  • num_features (int) – C from an expected input of size (N, C, H, W).

  • device_num_each_group (int) – The number of devices in each group. Default: 2.

  • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

  • momentum (float) – A floating hyperparameter of the momentum for the running_mean and running_var computation. Default: 0.9.

  • affine (bool) – A bool value. When set to True, gamma and beta can be learned. Default: True.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘ones’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘zeros’.

  • moving_mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving mean. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘zeros’.

  • moving_var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving variance. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘ones’.

  • use_batch_statistics (bool) – If true, use the mean value and variance value of current batch data. If false, use the mean value and variance value of specified value. If None, training process will use the mean and variance of current batch data and track the running mean and variance, eval process will use the running mean and variance. Default: None.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, the normalized, scaled, offset tensor, of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If num_features or device_num_each_group is not an int.

  • TypeError – If eps is not a float.

  • ValueError – If num_features is less than 1.

  • ValueError – If momentum is not in range [0, 1].

  • ValueError – If device_num_each_group is less than 2.

Supported Platforms:

Ascend

实际案例

>>> # This example should be run with multiple processes.
>>> # Please refer to the tutorial > Distributed Training on mindspore.cn.
>>> import numpy as np
>>> from mindspore.communication import init
>>> from mindspore import context
>>> from mindspore.context import ParallelMode
>>> from mindspore import nn
>>> from mindspore import Tensor
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
>>> context.reset_auto_parallel_context()
>>> context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL)
>>> global_bn_op = nn.GlobalBatchNorm(num_features=3, device_num_each_group=2)
>>> x = Tensor(np.ones([1, 3, 2, 2]).astype(np.float32))
>>> output = global_bn_op(x)
>>> print(output)
[[[[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]
  [[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]
  [[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]]]
class tinyms.layers.SyncBatchNorm(num_features, eps=1e-05, momentum=0.9, affine=True, gamma_init='ones', beta_init='zeros', moving_mean_init='zeros', moving_var_init='ones', use_batch_statistics=None, process_groups=None)[源代码]

Sync Batch Normalization layer over a N-dimension input.

Sync Batch Normalization is cross device synchronized Batch Normalization. The implementation of Batch Normalization only normalizes the data within each device. Sync Batch Normalization will normalize the input within the group. It has been described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. It rescales and recenters the feature using a mini-batch of data and the learned parameters which can be described in the following formula.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]

注解

Currently, SyncBatchNorm only supports 2D and 4D inputs.

参数
  • num_features (int) – C from an expected input of size (N, C, H, W).

  • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

  • momentum (float) – A floating hyperparameter of the momentum for the running_mean and running_var computation. Default: 0.9.

  • affine (bool) – A bool value. When set to True, gamma and beta can be learned. Default: True.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘ones’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘zeros’.

  • moving_mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving mean. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘zeros’.

  • moving_var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving variance. The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’, ‘he_uniform’, etc. Default: ‘ones’.

  • use_batch_statistics (bool) – If true, use the mean value and variance value of current batch data. If false, use the mean value and variance value of specified value. If None, training process will use the mean and variance of current batch data and track the running mean and variance, eval process will use the running mean and variance. Default: None.

  • process_groups (list) – A list to divide devices into different sync groups, containing N subtraction lists. Each subtraction list contains int numbers identifying rank ids which need to be synchronized in the same group. All int values must be in [0, rank_size) and different from each other. Default: None, indicating synchronization across all devices.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor, the normalized, scaled, offset tensor, of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If num_features is not an int.

  • TypeError – If eps is not a float.

  • TypeError – If process_groups is not a list.

  • ValueError – If num_features is less than 1.

  • ValueError – If momentum is not in range [0, 1].

  • ValueError – If rank_id in process_groups is not in range [0, rank_size).

Supported Platforms:

Ascend

实际案例

>>> # This example should be run with multiple processes.
>>> # Please refer to the tutorial > Distributed Training on mindspore.cn.
>>> import numpy as np
>>> from mindspore.communication import init
>>> from mindspore import context
>>> from mindspore.context import ParallelMode
>>> from mindspore import Tensor
>>> from mindspore import nn
>>> from mindspore import dtype as mstype
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
>>> context.reset_auto_parallel_context()
>>> context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL)
>>> sync_bn_op = nn.SyncBatchNorm(num_features=3, process_groups=[[0, 1], [2, 3]])
>>> x = Tensor(np.ones([1, 3, 2, 2]), mstype.float32)
>>> output = sync_bn_op(x)
>>> print(output)
[[[[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]
  [[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]
  [[ 0.999995 0.999995 ]
   [ 0.999995 0.999995 ]]]]
class tinyms.layers.InstanceNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, gamma_init='ones', beta_init='zeros')[源代码]

Instance Normalization layer over a 4D input.

This layer applies Instance Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization. It rescales and recenters the feature using a mini-batch of data and the learned parameters which can be described in the following formula.

\[y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta\]

gamma and beta are learnable parameter vectors of size num_features if affine is True. The standard-deviation is calculated via the biased estimator.

This layer uses instance statistics computed from input data in both training and evaluation modes.

InstanceNorm2d and BatchNorm2d are very similar, but have some differences. InstanceNorm2d is applied on each channel of channeled data like RGB images, but BatchNorm2d is usually applied on each batch of batched data.

注解

Note that the formula for updating the running_mean and running_var is \(\hat{x}_\text{new} = (1 - \text{momentum}) \times x_t + \text{momentum} \times \hat{x}\), where \(\hat{x}\) is the estimated statistic and \(x_t\) is the new observed value.

参数
  • num_features (int) – C from an expected input of size (N, C, H, W).

  • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

  • momentum (float) – A floating hyperparameter of the momentum for the running_mean and running_var computation. Default: 0.1.

  • affine (bool) – A bool value. When set to True, gamma and beta can be learned. Default: True.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘ones’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight. The values of str refer to the function initializer including ‘zeros’, ‘ones’, etc. Default: ‘zeros’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C, H, W)\). Data type: float16 or float32.

Outputs:

Tensor, the normalized, scaled, offset tensor, of shape \((N, C, H, W)\). Same type and shape as the x.

Supported Platforms:

GPU

引发
  • TypeError – If num_features is not an int.

  • TypeError – If eps is not a float.

  • TypeError – If momentum is not a float.

  • TypeError – If affine is not a bool.

  • TypeError – If the type of gamma_init/beta_init is not same, or if the initialized element type is not float32.

  • ValueError – If num_features is less than 1.

  • ValueError – If momentum is not in range [0, 1].

  • KeyError – If any of gamma_init/beta_init is str and the homonymous class inheriting from Initializer not exists.

实际案例

>>> import mindspore
>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor
>>> net = nn.InstanceNorm2d(3)
>>> x = Tensor(np.ones([2, 3, 2, 2]), mindspore.float32)
>>> output = net(x)
>>> print(output.shape)
(2, 3, 2, 2)
class tinyms.layers.SequentialCell(*args)[源代码]

Sequential cell container.

A list of Cells will be added to it in the order they are passed in the constructor. Alternatively, an ordered dict of cells can also be passed in.

参数

args (list, OrderedDict) – List of subclass of Cell.

Inputs:
  • x (Tensor) - Tensor with shape according to the first Cell in the sequence.

Outputs:

Tensor, the output Tensor with shape depending on the input x and defined sequence of Cells.

引发

TypeError – If the type of the args is not list or OrderedDict.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> conv = nn.Conv2d(3, 2, 3, pad_mode='valid', weight_init="ones")
>>> relu = nn.ReLU()
>>> seq = nn.SequentialCell([conv, relu])
>>> x = Tensor(np.ones([1, 3, 4, 4]), dtype=mindspore.float32)
>>> output = seq(x)
>>> print(output)
[[[[27. 27.]
   [27. 27.]]
  [[27. 27.]
   [27. 27.]]]]
append(cell)[源代码]

Appends a given cell to the end of the list.

实际案例

>>> conv = nn.Conv2d(3, 2, 3, pad_mode='valid', weight_init="ones")
>>> bn = nn.BatchNorm2d(2)
>>> relu = nn.ReLU()
>>> seq = nn.SequentialCell([conv, bn])
>>> seq.append(relu)
>>> x = Tensor(np.ones([1, 3, 4, 4]), dtype=mindspore.float32)
>>> output = seq(x)
>>> print(output)
[[[[26.999863 26.999863]
   [26.999863 26.999863]]
  [[26.999863 26.999863]
   [26.999863 26.999863]]]]
class tinyms.layers.CellList(*args, **kwargs)[源代码]

Holds Cells in a list.

CellList can be used like a regular Python list, support ‘__getitem__’, ‘__setitem__’, ‘__delitem__’, ‘__len__’, ‘__iter__’ and ‘__iadd__’, but cells it contains are properly registered, and will be visible by all Cell methods.

参数

args (list, optional) – List of subclass of Cell.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> conv = nn.Conv2d(100, 20, 3)
>>> bn = nn.BatchNorm2d(20)
>>> relu = nn.ReLU()
>>> cell_ls = nn.CellList([bn])
>>> cell_ls.insert(0, conv)
>>> cell_ls.append(relu)
>>> print(cell_ls)
CellList<
  (0): Conv2d<input_channels=100, output_channels=20, kernel_size=(3, 3),stride=(1, 1),  pad_mode=same,
  padding=0, dilation=(1, 1), group=1, has_bias=False, weight_init=normal, bias_init=zeros, format=NCHW>
  (1): BatchNorm2d<num_features=20, eps=1e-05, momentum=0.09999999999999998, gamma=Parameter (name=1.gamma,
  shape=(20,), dtype=Float32, requires_grad=True), beta=Parameter (name=1.beta, shape=(20,), dtype=Float32,
  requires_grad=True), moving_mean=Parameter (name=1.moving_mean, shape=(20,), dtype=Float32,
  requires_grad=False), moving_variance=Parameter (name=1.moving_variance, shape=(20,), dtype=Float32,
  requires_grad=False)>
  (2): ReLU<>
  >
append(cell)[源代码]

Appends a given cell to the end of the list.

extend(cells)[源代码]

Appends cells from a Python iterable to the end of the list.

引发

TypeError – If the cells are not a list of subcells.

insert(index, cell)[源代码]

Inserts a given cell before a given index in the list.

class tinyms.layers.Conv2d(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros', data_format='NCHW')[源代码]

2D convolution layer.

Applies a 2D convolution over an input tensor which is typically of shape \((N, C_{in}, H_{in}, W_{in})\), where \(N\) is batch size, \(C_{in}\) is channel number, and \(H_{in}, W_{in}\) are height and width. For each batch of shape \((C_{in}, H_{in}, W_{in})\), the formula is defined as:

\[out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,\]

where \(ccor\) is the cross-correlation operator, \(C_{in}\) is the input channel number, \(j\) ranges from \(0\) to \(C_{out} - 1\), \(W_{ij}\) corresponds to the \(i\)-th channel of the \(j\)-th filter and \(out_{j}\) corresponds to the \(j\)-th channel of the output. \(W_{ij}\) is a slice of kernel and it has shape \((\text{kernel_size[0]}, \text{kernel_size[1]})\), where \(\text{kernel_size[0]}\) and \(\text{kernel_size[1]}\) are the height and width of the convolution kernel. The full kernel has shape \((C_{out}, C_{in} // \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]})\), where group is the group number to split the input x in the channel dimension.

If the ‘pad_mode’ is set to be “valid”, the output height and width will be \(\left \lfloor{1 + \frac{H_{in} + \text{padding[0]} + \text{padding[1]} - \text{kernel_size[0]} - (\text{kernel_size[0]} - 1) \times (\text{dilation[0]} - 1) }{\text{stride[0]}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{W_{in} + \text{padding[2]} + \text{padding[3]} - \text{kernel_size[1]} - (\text{kernel_size[1]} - 1) \times (\text{dilation[1]} - 1) }{\text{stride[1]}}} \right \rfloor\) respectively.

The first introduction can be found in paper Gradient Based Learning Applied to Document Recognition.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means the value is for both the height and the width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input x. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side. If this mode is set, padding must be 0.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, padding must be 0.

    • pad: Implicit paddings on both sides of the input x. The number of padding will be padded to the input Tensor borders. padding must be greater than or equal to 0.

  • padding (Union[int, tuple[int]]) – Implicit paddings on both sides of the input x. If padding is one integer, the paddings of top, bottom, left and right are the same, equal to padding. If padding is a tuple with four integers, the paddings of top, bottom, left and right will be equal to padding[0], padding[1], padding[2], and padding[3] accordingly. Default: 0.

  • dilation (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. If the group is equal to in_channels and out_channels, this 2D convolution layer also can be called 2D depthwise convolution layer. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\) or \((N, H_{in}, W_{in}, C_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\) or \((N, H_{out}, W_{out}, C_{out})\).

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding or dilation is neither an int not a tuple.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If padding is a tuple whose length is not equal to 4.

  • ValueError – If pad_mode is not equal to ‘pad’ and padding is not equal to (0, 0, 0, 0).

  • ValueError – If data_format is neither ‘NCHW’ not ‘NHWC’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Conv2d(120, 240, 4, has_bias=False, weight_init='normal')
>>> x = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float32)
>>> output = net(x).shape
>>> print(output)
(1, 240, 1024, 640)
class tinyms.layers.Conv2dTranspose(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros')[源代码]

2D transposed convolution layer.

Compute a 2D transposed convolution, which is also known as a deconvolution (although it is not an actual deconvolution). This module can be seen as the gradient of Conv2d with respect to its input.

x is typically of shape \((N, C, H, W)\), where \(N\) is batch size, \(C\) is channel number, \(H\) is the height of the characteristic layer and \(W\) is the width of the characteristic layer.

The pad_mode argument effectively adds \(dilation * (kernel\_size - 1) - padding\) amount of zero padding to both sizes of the input. So that when a Conv2d and a ConvTranspose2d are initialized with same parameters, they are inverses of each other in regard to the input and output shapes. However, when stride > 1, Conv2d maps multiple input shapes to the same output shape. ConvTranspose2d provide padding argument to increase the calculated output shape on one or more side.

The height and width of output are defined as:

if the ‘pad_mode’ is set to be “pad”,

\[ \begin{align}\begin{aligned}H_{out} = (H_{in} - 1) \times \text{stride[0]} - \left (\text{padding[0]} + \text{padding[1]}\right ) + \text{dilation[0]} \times (\text{kernel_size[0]} - 1) + 1\\W_{out} = (W_{in} - 1) \times \text{stride[1]} - \left (\text{padding[2]} + \text{padding[3]}\right ) + \text{dilation[1]} \times (\text{kernel_size[1]} - 1) + 1\end{aligned}\end{align} \]

if the ‘pad_mode’ is set to be “same”,

\[\begin{split}H_{out} = (H_{in} + \text{stride[0]} - 1)/\text{stride[0]} \\ W_{out} = (W_{in} + \text{stride[1]} - 1)/\text{stride[1]}\end{split}\]

if the ‘pad_mode’ is set to be “valid”,

\[\begin{split}H_{out} = (H_{in} - 1) \times \text{stride[0]} + \text{dilation[0]} \times (\text{ks_w[0]} - 1) + 1 \\ W_{out} = (W_{in} - 1) \times \text{stride[1]} + \text{dilation[1]} \times (\text{ks_w[1]} - 1) + 1\end{split}\]

where \(\text{kernel_size[0]}\) is the height of the convolution kernel and \(\text{kernel_size[1]}\) is the width of the convolution kernel.

参数
  • in_channels (int) – The number of channels in the input space.

  • out_channels (int) – The number of channels in the output space.

  • kernel_size (Union[int, tuple]) – int or a tuple of 2 integers, which specifies the height and width of the 2D convolution window. Single int means the value is for both the height and the width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Its value must be equal to or greater than 1. Default: 1.

  • pad_mode (str) –

    Select the mode of the pad. The optional values are “pad”, “same”, “valid”. Default: “same”.

    • pad: Implicit paddings on both sides of the input x.

    • same: Adopted the way of completion.

    • valid: Adopted the way of discarding.

  • padding (Union[int, tuple[int]]) – Implicit paddings on both sides of the input x. If padding is one integer, the paddings of top, bottom, left and right are the same, equal to padding. If padding is a tuple with four integers, the paddings of top, bottom, left and right will be equal to padding[0], padding[1], padding[2], and padding[3] accordingly. Default: 0.

  • dilation (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater than or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits filter into groups, in_channels and out_channels must be divisible by the number of groups. This does not support for Davinci devices when group > 1. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding or dilation is neither an int not a tuple.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If padding is a tuple whose length is not equal to 4.

  • ValueError – If pad_mode is not equal to ‘pad’ and padding is not equal to (0, 0, 0, 0).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Conv2dTranspose(3, 64, 4, has_bias=False, weight_init='normal', pad_mode='pad')
>>> x = Tensor(np.ones([1, 3, 16, 50]), mindspore.float32)
>>> output = net(x).shape
>>> print(output)
(1, 64, 19, 53)
class tinyms.layers.Conv1d(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros')[源代码]

1D convolution layer.

Applies a 1D convolution over an input tensor which is typically of shape \((N, C_{in}, W_{in})\), where \(N\) is batch size and \(C_{in}\) is channel number. For each batch of shape \((C_{in}, W_{in})\), the formula is defined as:

\[out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,\]

where \(ccor\) is the cross correlation operator, \(C_{in}\) is the input channel number, \(j\) ranges from \(0\) to \(C_{out} - 1\), \(W_{ij}\) corresponds to the \(i\)-th channel of the \(j\)-th filter and \(out_{j}\) corresponds to the \(j\)-th channel of the output. \(W_{ij}\) is a slice of kernel and it has shape \((\text{ks_w})\), where \(\text{ks_w}\) is the width of the convolution kernel. The full kernel has shape \((C_{out}, C_{in} // \text{group}, \text{ks_w})\), where group is the group number to split the input x in the channel dimension.

If the ‘pad_mode’ is set to be “valid”, the output width will be \(\left \lfloor{1 + \frac{W_{in} + 2 \times \text{padding} - \text{ks_w} - (\text{ks_w} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) respectively.

The first introduction of convolution layer can be found in paper Gradient Based Learning Applied to Document Recognition.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (int) – The data type is int. Specifies the width of the 1D convolution window.

  • stride (int) – The distance of kernel moving, an int number that represents the width of movement. Default: 1.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

    • same: Adopts the way of completion. The output width will be the same as the input x. The total number of padding will be calculated in the horizontal direction and evenly distributed to left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side. If this mode is set, padding must be 0.

    • valid: Adopts the way of discarding. The possible largest width of the output will be returned without padding. Extra pixels will be discarded. If this mode is set, padding must be 0.

    • pad: Implicit paddings on both sides of the input x. The number of padding will be padded to the input Tensor borders. padding must be greater than or equal to 0.

  • padding (int) – Implicit paddings on both sides of the input x. Default: 0.

  • dilation (int) – The data type is int. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – An initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels, kernel_size, stride, padding or dilation is not an int.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Conv1d(120, 240, 4, has_bias=False, weight_init='normal')
>>> x = Tensor(np.ones([1, 120, 640]), mindspore.float32)
>>> output = net(x).shape
>>> print(output)
(1, 240, 640)
class tinyms.layers.Conv1dTranspose(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros')[源代码]

1D transposed convolution layer.

Compute a 1D transposed convolution, which is also known as a deconvolution (although it is not an actual deconvolution). This module can be seen as the gradient of Conv1d with respect to its input.

x is typically of shape \((N, C, W)\), where \(N\) is batch size, \(C\) is channel number and \(W\) is the characteristic length.

The padding argument effectively adds \(dilation * (kernel\_size - 1) - padding\) amount of zero padding to both sizes of the input. So that when a Conv1d and a ConvTranspose1d are initialized with same parameters, they are inverses of each other in regard to the input and output shapes. However, when stride > 1, Conv1d maps multiple input shapes to the same output shape.

The width of output is defined as:

\[\begin{split}W_{out} = \begin{cases} (W_{in} - 1) \times \text{stride} - 2 \times \text{padding} + \text{dilation} \times (\text{ks_w} - 1) + 1, & \text{if pad_mode='pad'}\\ (W_{in} + \text{stride} - 1)/\text{stride}, & \text{if pad_mode='same'}\\ (W_{in} - 1) \times \text{stride} + \text{dilation} \times (\text{ks_w} - 1) + 1, & \text{if pad_mode='valid'} \end{cases}\end{split}\]

where \(\text{ks_w}\) is the width of the convolution kernel.

参数
  • in_channels (int) – The number of channels in the input space.

  • out_channels (int) – The number of channels in the output space.

  • kernel_size (int) – int, which specifies the width of the 1D convolution window.

  • stride (int) – The distance of kernel moving, an int number that represents the width of movement. Default: 1.

  • pad_mode (str) –

    Select the mode of the pad. The optional values are “pad”, “same”, “valid”. Default: “same”.

    • pad: Implicit paddings on both sides of the input x.

    • same: Adopted the way of completion.

    • valid: Adopted the way of discarding.

  • padding (int) – Implicit paddings on both sides of the input x. Default: 0.

  • dilation (int) – The data type is int. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the width of the input x. Default: 1.

  • group (int) – Splits filter into groups, in_channels and out_channels must be divisible by the number of groups. This is not support for Davinci devices when group > 1. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a numbers.Number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels, kernel_size, stride, padding or dilation is not an int.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Conv1dTranspose(3, 64, 4, has_bias=False, weight_init='normal', pad_mode='pad')
>>> x = Tensor(np.ones([1, 3, 50]), mindspore.float32)
>>> output = net(x).shape
>>> print(output)
(1, 64, 53)
class tinyms.layers.Conv3d(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros', data_format='NCDHW')[源代码]

3D convolution layer.

Applies a 3D convolution over an input tensor which is typically of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\) and output shape \((N, C_{out}, D_{out}, H_{out}, W_{out})\). where \(N\) is batch size. \(C\) is channel number. the formula is defined as:

\[\operatorname{out}\left(N_{i}, C_{\text {out}_j}\right)=\operatorname{bias}\left(C_{\text {out}_j}\right)+ \sum_{k=0}^{C_{in}-1} ccor(\text {weight}\left(C_{\text {out}_j}, k\right), \operatorname{input}\left(N_{i}, k\right))\]

where \(ccor\) is the cross-correlation operator.

If the ‘pad_mode’ is set to be “valid”, the output depth, height and width will be \(\left \lfloor{1 + \frac{D_{in} + \text{padding[0]} + \text{padding[1]} - \text{kernel_size[0]} - (\text{kernel_size[0]} - 1) \times (\text{dilation[0]} - 1) }{\text{stride[0]}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{H_{in} + \text{padding[2]} + \text{padding[3]} - \text{kernel_size[1]} - (\text{kernel_size[1]} - 1) \times (\text{dilation[1]} - 1) }{\text{stride[1]}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{W_{in} + \text{padding[4]} + \text{padding[5]} - \text{kernel_size[2]} - (\text{kernel_size[2]} - 1) \times (\text{dilation[2]} - 1) }{\text{stride[2]}}} \right \rfloor\) respectively.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 3 integers. Specifies the depth, height and width of the 3D convolution window. Single int means the value is for the depth, height and the width of the kernel. A tuple of 3 ints means the first value is for the depth, second value is for height and the other is for the width of the kernel.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

    • same: Adopts the way of completion. The depth, height and width of the output will be the same as the input x. The total number of padding will be calculated in depth, horizontal and vertical directions and evenly distributed to head and tail, top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the tail, bottom and the right side. If this mode is set, padding must be 0.

    • valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, padding must be 0.

    • pad: Implicit paddings on both sides of the input x in depth, height, width. The number of padding will be padded to the input Tensor borders. padding must be greater than or equal to 0.

  • padding (Union(int, tuple[int])) – Implicit paddings on both sides of the input x. The data type is int or a tuple of 6 integers. Default: 0. If padding is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to padding. If paddings is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to padding[0], padding[1], padding[2], padding[3], padding[4] and padding[5] correspondingly.

  • dilation (Union[int, tuple[int]]) – The data type is int or a tuple of 3 integers : math:(dilation_d, dilation_h, dilation_w). Currently, dilation on depth only supports the case of 1. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1. Only 1 is currently supported.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

  • data_format (str) – The optional value for data format. Currently only support “NCDHW”.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\). Currently input data type only support float16 and float32.

Outputs:

Tensor, the value that applied 3D convolution. The shape is \((N, C_{out}, D_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding or dilation is neither an int nor a tuple.

  • ValueError – If out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If padding is a tuple whose length is not equal to 6.

  • ValueError – If pad_mode is not equal to ‘pad’ and padding is not equal to (0, 0, 0, 0, 0, 0).

  • ValueError – If data_format is not ‘NCDHW’.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.ones([16, 3, 10, 32, 32]), mindspore.float32)
>>> conv3d = nn.Conv3d(in_channels=3, out_channels=32, kernel_size=(4, 3, 3))
>>> output = conv3d(x)
>>> print(output.shape)
(16, 32, 10, 32, 32)
class tinyms.layers.Conv3dTranspose(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, output_padding=0, has_bias=False, weight_init='normal', bias_init='zeros', data_format='NCDHW')[源代码]

Compute a 3D transposed convolution, which is also known as a deconvolution (although it is not an actual deconvolution). The transposed convolution operator multiplies each input value element-wise by a learnable kernel, and sums over the outputs from all input feature planes. This module can be seen as the gradient of Conv3d with respect to its input.

x is typically of shape \((N, C, D, H, W)\), where \(N\) is batch size, \(C\) is channel number, \(D\) is the characteristic depth, \(H\) is the height of the characteristic layer, and \(W\) is the width of the characteristic layer. The calculation process of transposed convolution is equivalent to the reverse calculation of convolution.

The pad_mode argument effectively adds \(dilation * (kernel\_size - 1) - padding\) amount of zero padding to both sizes of the input. So that when a Conv3d and a ConvTranspose3d are initialized with same parameters, they are inverses of each other in regard to the input and output shapes. However, when stride > 1, Conv3d maps multiple input shapes to the same output shape. ConvTranspose3d provide padding argument to increase the calculated output shape on one or more side.

The height and width of output are defined as:

if the ‘pad_mode’ is set to be “pad”,

\[ \begin{align}\begin{aligned}D_{out} = (D_{in} - 1) \times \text{stride_d} - 2 \times \text{padding_d} + \text{dilation_d} \times (\text{kernel_size_d} - 1) + \text{output_padding_d} + 1\\H_{out} = (H_{in} - 1) \times \text{stride_h} - 2 \times \text{padding_h} + \text{dilation_h} \times (\text{kernel_size_h} - 1) + \text{output_padding_h} + 1\\W_{out} = (W_{in} - 1) \times \text{stride_w} - 2 \times \text{padding_w} + \text{dilation_w} \times (\text{kernel_size_w} - 1) + \text{output_padding_w} + 1\end{aligned}\end{align} \]

if the ‘pad_mode’ is set to be “same”,

\[\begin{split}D_{out} = (D_{in} + \text{stride_d} - 1)/\text{stride_d} \\ H_{out} = (H_{in} + \text{stride_h} - 1)/\text{stride_h} \\ W_{out} = (W_{in} + \text{stride_w} - 1)/\text{stride_w}\end{split}\]

if the ‘pad_mode’ is set to be “valid”,

\[\begin{split}D_{out} = (D_{in} - 1) \times \text{stride_d} + \text{dilation_d} \times (\text{kernel_size_d} - 1) + 1 \\ H_{out} = (H_{in} - 1) \times \text{stride_h} + \text{dilation_h} \times (\text{kernel_size_h} - 1) + 1 \\ W_{out} = (W_{in} - 1) \times \text{stride_w} + \text{dilation_w} \times (\text{kernel_size_w} - 1) + 1\end{split}\]
参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – The kernel size of the 3D convolution.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Its value must be equal to or greater than 1. Default: 1.

  • pad_mode (str) –

    Select the mode of the pad. The optional values are “pad”, “same”, “valid”. Default: “same”.

    • same: Adopts the way of completion. The depth, height and width of the output will be the same as the input x. The total number of padding will be calculated in depth, horizontal and vertical directions and evenly distributed to head and tail, top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the tail, bottom and the right side. If this mode is set, padding and output_padding must be 0.

    • valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, padding and output_padding must be 0.

    • pad: Implicit paddings on both sides of the input x in depth, height, width. The number of pad will be padded to the input Tensor borders. padding must be greater than or equal to 0.

  • padding (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If padding is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to padding. If padding is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to padding[0], padding[1], padding[2], padding[3], padding[4] and padding[5] correspondingly.

  • dilation (Union(int, tuple[int])) – The data type is int or a tuple of 3 integers : math:(dilation_d, dilation_h, dilation_w). Currently, dilation on depth only supports the case of 1. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1. Only 1 is currently supported.

  • output_padding (Union(int, tuple[int])) – Add extra size to each dimension of the output. Default: 0. Must be greater than or equal to 0.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

  • data_format (str) – The optional value for data format. Currently only support ‘NCDHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\). Currently input data type only support float16 and float32.

Outputs:

Tensor, the shape is \((N, C_{out}, D_{out}, H_{out}, W_{out})\).

Supported Platforms:

Ascend GPU

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding , dilation or output_padding is neither an int not a tuple of three.

  • TypeError – If input data type is not float16 or float32.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If padding is a tuple whose length is not equal to 6.

  • ValueError – If pad_mode is not equal to ‘pad’ and padding is not equal to (0, 0, 0, 0, 0, 0).

  • ValueError – If data_format is not ‘NCDHW’.

实际案例

>>> x = Tensor(np.ones([32, 16, 10, 32, 32]), mindspore.float32)
>>> conv3d_transpose = nn.Conv3dTranspose(in_channels=16, out_channels=3, kernel_size=(4, 6, 2),
...                                       pad_mode='pad')
>>> output = conv3d_transpose(x)
>>> print(output.shape)
(32, 3, 13, 37, 33)
class tinyms.layers.LSTM(input_size, hidden_size, num_layers=1, has_bias=True, batch_first=False, dropout=0, bidirectional=False)[源代码]

Stacked LSTM (Long Short-Term Memory) layers.

Apply LSTM layer to the input.

There are two pipelines connecting two consecutive cells in a LSTM model; one is cell state pipeline and the other is hidden state pipeline. Denote two consecutive time nodes as \(t-1\) and \(t\). Given an input \(x_t\) at time \(t\), an hidden state \(h_{t-1}\) and an cell state \(c_{t-1}\) of the layer at time \({t-1}\), the cell state and hidden state at time \(t\) is computed using an gating mechanism. Input gate \(i_t\) is designed to protect the cell from perturbation by irrelevant inputs. Forget gate \(f_t\) affords protection of the cell by forgetting some information in the past, which is stored in \(h_{t-1}\). Output gate \(o_t\) protects other units from perturbation by currently irrelevant memory contents. Candidate cell state \(\tilde{c}_t\) is calculated with the current input, on which the input gate will be applied. Finally, current cell state \(c_{t}\) and hidden state \(h_{t}\) are computed with the calculated gates and cell states. The complete formulation is as follows.

\[\begin{split}\begin{array}{ll} \\ i_t = \sigma(W_{ix} x_t + b_{ix} + W_{ih} h_{(t-1)} + b_{ih}) \\ f_t = \sigma(W_{fx} x_t + b_{fx} + W_{fh} h_{(t-1)} + b_{fh}) \\ \tilde{c}_t = \tanh(W_{cx} x_t + b_{cx} + W_{ch} h_{(t-1)} + b_{ch}) \\ o_t = \sigma(W_{ox} x_t + b_{ox} + W_{oh} h_{(t-1)} + b_{oh}) \\ c_t = f_t * c_{(t-1)} + i_t * \tilde{c}_t \\ h_t = o_t * \tanh(c_t) \\ \end{array}\end{split}\]

Here \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product. \(W, b\) are learnable weights between the output and the input in the formula. For instance, \(W_{ix}, b_{ix}\) are the weight and bias used to transform from input \(x\) to \(i\). Details can be found in paper LONG SHORT-TERM MEMORY and Long Short-Term Memory Recurrent Neural Network Architectures for Large Scale Acoustic Modeling.

参数
  • input_size (int) – Number of features of input.

  • hidden_size (int) – Number of features of hidden layer.

  • num_layers (int) – Number of layers of stacked LSTM . Default: 1.

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh. Default: True.

  • batch_first (bool) – Specifies whether the first dimension of input x is batch_size. Default: False.

  • dropout (float, int) – If not 0, append Dropout layer on the outputs of each LSTM layer except the last layer. Default 0. The range of dropout is [0.0, 1.0].

  • bidirectional (bool) – Specifies whether it is a bidirectional LSTM. Default: False.

Inputs:
  • x (Tensor) - Tensor of shape (seq_len, batch_size, input_size) or (batch_size, seq_len, input_size).

  • hx (tuple) - A tuple of two Tensors (h_0, c_0) both of data type mindspore.float32 or mindspore.float16 and shape (num_directions * num_layers, batch_size, hidden_size). Data type of hx must be the same as x.

Outputs:

Tuple, a tuple contains (output, (h_n, c_n)).

  • output (Tensor) - Tensor of shape (seq_len, batch_size, num_directions * hidden_size).

  • hx_n (tuple) - A tuple of two Tensor (h_n, c_n) both of shape (num_directions * num_layers, batch_size, hidden_size).

引发
  • TypeError – If input_size, hidden_size or num_layers is not an int.

  • TypeError – If has_bias, batch_first or bidirectional is not a bool.

  • TypeError – If dropout is neither a float nor an int.

  • ValueError – If dropout is not in range [0.0, 1.0].

Supported Platforms:

Ascend GPU

实际案例

>>> net = nn.LSTM(10, 16, 2, has_bias=True, batch_first=True, bidirectional=False)
>>> x = Tensor(np.ones([3, 5, 10]).astype(np.float32))
>>> h0 = Tensor(np.ones([1 * 2, 3, 16]).astype(np.float32))
>>> c0 = Tensor(np.ones([1 * 2, 3, 16]).astype(np.float32))
>>> output, (hn, cn) = net(x, (h0, c0))
>>> print(output.shape)
(3, 5, 16)
class tinyms.layers.LSTMCell(input_size, hidden_size, has_bias=True, batch_first=False, dropout=0, bidirectional=False)[源代码]

LSTM (Long Short-Term Memory) layer.

Apply LSTM layer to the input.

There are two pipelines connecting two consecutive cells in a LSTM model; one is cell state pipeline and the other is hidden state pipeline. Denote two consecutive time nodes as \(t-1\) and \(t\). Given an input \(x_t\) at time \(t\), an hidden state \(h_{t-1}\) and an cell state \(c_{t-1}\) of the layer at time \({t-1}\), the cell state and hidden state at time \(t\) is computed using an gating mechanism. Input gate \(i_t\) is designed to protect the cell from perturbation by irrelevant inputs. Forget gate \(f_t\) affords protection of the cell by forgetting some information in the past, which is stored in \(h_{t-1}\). Output gate \(o_t\) protects other units from perturbation by currently irrelevant memory contents. Candidate cell state \(\tilde{c}_t\) is calculated with the current input, on which the input gate will be applied. Finally, current cell state \(c_{t}\) and hidden state \(h_{t}\) are computed with the calculated gates and cell states. The complete formulation is as follows.

\[\begin{split}\begin{array}{ll} \\ i_t = \sigma(W_{ix} x_t + b_{ix} + W_{ih} h_{(t-1)} + b_{ih}) \\ f_t = \sigma(W_{fx} x_t + b_{fx} + W_{fh} h_{(t-1)} + b_{fh}) \\ \tilde{c}_t = \tanh(W_{cx} x_t + b_{cx} + W_{ch} h_{(t-1)} + b_{ch}) \\ o_t = \sigma(W_{ox} x_t + b_{ox} + W_{oh} h_{(t-1)} + b_{oh}) \\ c_t = f_t * c_{(t-1)} + i_t * \tilde{c}_t \\ h_t = o_t * \tanh(c_t) \\ \end{array}\end{split}\]

Here \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product. \(W, b\) are learnable weights between the output and the input in the formula. For instance, \(W_{ix}, b_{ix}\) are the weight and bias used to transform from input \(x\) to \(i\). Details can be found in paper LONG SHORT-TERM MEMORY and Long Short-Term Memory Recurrent Neural Network Architectures for Large Scale Acoustic Modeling.

注解

LSTMCell is a single-layer RNN, you can achieve multi-layer RNN by stacking LSTMCell.

参数
  • input_size (int) – Number of features of input.

  • hidden_size (int) – Number of features of hidden layer.

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh. Default: True.

  • batch_first (bool) – Specifies whether the first dimension of input x is batch_size. Default: False.

  • dropout (float, int) – If not 0, append Dropout layer on the outputs of each LSTM layer except the last layer. Default 0. The range of dropout is [0.0, 1.0].

  • bidirectional (bool) – Specifies whether this is a bidirectional LSTM. If set True, number of directions will be 2 otherwise number of directions is 1. Default: False.

Inputs:
  • x (Tensor) - Tensor of shape (seq_len, batch_size, input_size).

  • h - data type mindspore.float32 or mindspore.float16 and shape (num_directions, batch_size, hidden_size).

  • c - data type mindspore.float32 or mindspore.float16 and shape (num_directions, batch_size, hidden_size). Data type of h’ and ‘c’ must be the same of `x.

  • w - data type mindspore.float32 or mindspore.float16 and shape (weight_size, 1, 1). The value of weight_size depends on input_size, hidden_size and bidirectional

Outputs:

output, h_n, c_n, ‘reserve’, ‘state’.

  • output (Tensor) - Tensor of shape (seq_len, batch_size, num_directions * hidden_size).

  • h - A Tensor with shape (num_directions, batch_size, hidden_size).

  • c - A Tensor with shape (num_directions, batch_size, hidden_size).

  • reserve - reserved

  • state - reserved

引发
  • TypeError – If input_size or hidden_size or num_layers is not an int.

  • TypeError – If has_bias or batch_first or bidirectional is not a bool.

  • TypeError – If dropout is neither a float nor an int.

  • ValueError – If dropout is not in range [0.0, 1.0].

Supported Platforms:

GPU CPU

实际案例

>>> net = nn.LSTMCell(10, 12, has_bias=True, batch_first=True, bidirectional=False)
>>> x = Tensor(np.ones([3, 5, 10]).astype(np.float32))
>>> h = Tensor(np.ones([1, 3, 12]).astype(np.float32))
>>> c = Tensor(np.ones([1, 3, 12]).astype(np.float32))
>>> w = Tensor(np.ones([1152, 1, 1]).astype(np.float32))
>>> output, h, c, _, _ = net(x, h, c, w)
>>> print(output.shape)
(3, 5, 12)
class tinyms.layers.GRU(*args, **kwargs)[源代码]

Stacked GRU (Gated Recurrent Unit) layers.

Apply GRU layer to the input.

There are two gates in a GRU model; one is update gate and the other is reset gate. Denote two consecutive time nodes as \(t-1\) and \(t\). Given an input \(x_t\) at time \(t\), an hidden state \(h_{t-1}\), the update and reset gate at time \(t\) is computed using an gating mechanism. Update gate \(z_t\) is designed to protect the cell from perturbation by irrelevant inputs and past hidden state. Reset gate \(r_t\) determines how much information should be reset from old hidden state. New memory state \({n}_t\) is calculated with the current input, on which the reset gate will be applied. Finally, current hidden state \(h_{t}\) is computed with the calculated update grate and new memory state. The complete formulation is as follows.

\[\begin{split}\begin{array}{ll} r_t = \sigma(W_{ir} x_t + b_{ir} + W_{hr} h_{(t-1)} + b_{hr}) \\ z_t = \sigma(W_{iz} x_t + b_{iz} + W_{hz} h_{(t-1)} + b_{hz}) \\ n_t = \tanh(W_{in} x_t + b_{in} + r_t * (W_{hn} h_{(t-1)}+ b_{hn})) \\ h_t = (1 - z_t) * n_t + z_t * h_{(t-1)} \end{array}\end{split}\]

Here \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product. \(W, b\) are learnable weights between the output and the input in the formula. For instance, \(W_{ir}, b_{ir}\) are the weight and bias used to transform from input \(x\) to \(r\). Details can be found in paper Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation.

参数
  • input_size (int) – Number of features of input.

  • hidden_size (int) – Number of features of hidden layer.

  • num_layers (int) – Number of layers of stacked GRU. Default: 1.

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh. Default: True.

  • batch_first (bool) – Specifies whether the first dimension of input x is batch_size. Default: False.

  • dropout (float) – If not 0.0, append Dropout layer on the outputs of each GRU layer except the last layer. Default 0.0. The range of dropout is [0.0, 1.0).

  • bidirectional (bool) – Specifies whether it is a bidirectional GRU, num_directions=2 if bidirectional=True otherwise 1. Default: False.

Inputs:
  • x (Tensor) - Tensor of data type mindspore.float32 and shape (seq_len, batch_size, input_size) or (batch_size, seq_len, input_size).

  • hx (Tensor) - Tensor of data type mindspore.float32 and shape (num_directions * num_layers, batch_size, hidden_size). Data type of hx must be the same as x.

  • seq_length (Tensor) - The length of each sequence in a input batch. Tensor of shape \((\text{batch_size})\). Default: None. This input indicates the real sequence length before padding to avoid padded elements have been used to compute hidden state and affect the final output. It is recommend to use this input when x has padding elements.

Outputs:

Tuple, a tuple contains (output, h_n).

  • output (Tensor) - Tensor of shape (seq_len, batch_size, num_directions * hidden_size) or (batch_size, seq_len, num_directions * hidden_size).

  • hx_n (Tensor) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

引发
  • TypeError – If input_size, hidden_size or num_layers is not an int.

  • TypeError – If has_bias, batch_first or bidirectional is not a bool.

  • TypeError – If dropout is neither a float nor an int.

  • ValueError – If dropout is not in range [0.0, 1.0).

Supported Platforms:

Ascend GPU

实际案例

>>> net = nn.GRU(10, 16, 2, has_bias=True, batch_first=True, bidirectional=False)
>>> x = Tensor(np.ones([3, 5, 10]).astype(np.float32))
>>> h0 = Tensor(np.ones([1 * 2, 3, 16]).astype(np.float32))
>>> output, hn = net(x, h0)
>>> print(output.shape)
(3, 5, 16)
class tinyms.layers.RNN(*args, **kwargs)[源代码]

Stacked Elman RNN layers.

Apply RNN layer with \(\tanh\) or \(\text{ReLU}\) non-linearity to the input.

For each element in the input sequence, each layer computes the following function:

\[h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh})\]

Here \(h_t\) is the hidden state at time t, \(x_t\) is the input at time t, and \(h_{(t-1)}\) is the hidden state of the previous layer at time t-1 or the initial hidden state at time 0. If nonlinearity is 'relu', then \(\text{ReLU}\) is used instead of \(\tanh\).

参数
  • input_size (int) – Number of features of input.

  • hidden_size (int) – Number of features of hidden layer.

  • num_layers (int) – Number of layers of stacked RNN. Default: 1.

  • nonlinearity (str) – The non-linearity to use. Can be either 'tanh' or 'relu'. Default: 'tanh'

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh. Default: True.

  • batch_first (bool) – Specifies whether the first dimension of input x is batch_size. Default: False.

  • dropout (float) – If not 0.0, append Dropout layer on the outputs of each RNN layer except the last layer. Default 0.0. The range of dropout is [0.0, 1.0).

  • bidirectional (bool) – Specifies whether it is a bidirectional RNN, num_directions=2 if bidirectional=True otherwise 1. Default: False.

Inputs:
  • x (Tensor) - Tensor of data type mindspore.float32 and shape (seq_len, batch_size, input_size) or (batch_size, seq_len, input_size).

  • hx (Tensor) - Tensor of data type mindspore.float32 and shape (num_directions * num_layers, batch_size, hidden_size). Data type of hx must be the same as x.

  • seq_length (Tensor) - The length of each sequence in a input batch. Tensor of shape \((\text{batch_size})\). Default: None. This input indicates the real sequence length before padding to avoid padded elements have been used to compute hidden state and affect the final output. It is recommend to use this input when x has padding elements.

Outputs:

Tuple, a tuple contains (output, h_n).

  • output (Tensor) - Tensor of shape (seq_len, batch_size, num_directions * hidden_size) or (batch_size, seq_len, num_directions * hidden_size).

  • hx_n (Tensor) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

引发
  • TypeError – If input_size, hidden_size or num_layers is not an int.

  • TypeError – If has_bias, batch_first or bidirectional is not a bool.

  • TypeError – If dropout is neither a float nor an int.

  • ValueError – If dropout is not in range [0.0, 1.0).

  • ValueError – If nonlinearity is not in [‘tanh’, ‘relu’].

Supported Platforms:

Ascend GPU

实际案例

>>> net = nn.RNN(10, 16, 2, has_bias=True, batch_first=True, bidirectional=False)
>>> x = Tensor(np.ones([3, 5, 10]).astype(np.float32))
>>> h0 = Tensor(np.ones([1 * 2, 3, 16]).astype(np.float32))
>>> output, hn = net(x, h0)
>>> print(output.shape)
(3, 5, 16)
class tinyms.layers.GRUCell(input_size: int, hidden_size: int, has_bias: bool = True)[源代码]

A GRU(Gated Recurrent Unit) cell.

\[\begin{split}\begin{array}{ll} r = \sigma(W_{ir} x + b_{ir} + W_{hr} h + b_{hr}) \\ z = \sigma(W_{iz} x + b_{iz} + W_{hz} h + b_{hz}) \\ n = \tanh(W_{in} x + b_{in} + r * (W_{hn} h + b_{hn})) \\ h' = (1 - z) * n + z * h \end{array}\end{split}\]

Here \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product. \(W, b\) are learnable weights between the output and the input in the formula. For instance, \(W_{ir}, b_{ir}\) are the weight and bias used to transform from input \(x\) to \(r\). Details can be found in paper Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation.

参数
  • input_size (int) – Number of features of input.

  • hidden_size (int) – Number of features of hidden layer.

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh. Default: True.

Inputs:
  • x (Tensor) - Tensor of shape (batch_size, input_size).

  • hx (Tensor) - Tensor of data type mindspore.float32 and shape (batch_size, hidden_size). Data type of hx must be the same as x.

Outputs:
  • h’ (Tensor) - Tensor of shape (batch_size, hidden_size).

引发
  • TypeError – If input_size, hidden_size is not an int.

  • TypeError – If has_bias is not a bool.

Supported Platforms:

Ascend GPU

实际案例

>>> net = nn.GRUCell(10, 16)
>>> x = Tensor(np.ones([5, 3, 10]).astype(np.float32))
>>> hx = Tensor(np.ones([3, 16]).astype(np.float32))
>>> output = []
>>> for i in range(5):
>>>     hx = net(x[i], hx)
>>>     output.append(hx)
>>> print(output[0].shape)
(3, 16)
class tinyms.layers.RNNCell(input_size: int, hidden_size: int, has_bias: bool = True, nonlinearity: str = 'tanh')[源代码]

An Elman RNN cell with tanh or ReLU non-linearity.

\[h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh})\]

Here \(h_t\) is the hidden state at time t, \(x_t\) is the input at time t, and \(h_{(t-1)}\) is the hidden state of the previous layer at time t-1 or the initial hidden state at time 0. If nonlinearity is relu, then relu is used instead of tanh.

参数
  • input_size (int) – Number of features of input.

  • hidden_size (int) – Number of features of hidden layer.

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh. Default: True.

  • nonlinearity (str) – The non-linearity to use. Can be either tanh or relu. Default: tanh.

Inputs:
  • x (Tensor) - Tensor of shape (batch_size, input_size).

  • hx (Tensor) - Tensor of data type mindspore.float32 and shape (batch_size, hidden_size). Data type of hx must be the same as x.

Outputs:
  • h’ (Tensor) - Tensor of shape (batch_size, hidden_size).

引发
  • TypeError – If input_size or hidden_size is not an int or not greater than 0.

  • TypeError – If has_bias is not a bool.

  • ValueError – If nonlinearity is not in [‘tanh’, ‘relu’].

Supported Platforms:

Ascend GPU

实际案例

>>> net = nn.RNNCell(10, 16)
>>> x = Tensor(np.ones([5, 3, 10]).astype(np.float32))
>>> hx = Tensor(np.ones([3, 16]).astype(np.float32))
>>> output = []
>>> for i in range(5):
>>>     hx = net(x[i], hx)
>>>     output.append(hx)
>>> print(output[0].shape)
(3, 16)
class tinyms.layers.Dropout(keep_prob=0.5, dtype=mindspore.float32)[源代码]

Dropout layer for the input.

Randomly set some elements of the input tensor to zero with probability \(1 - keep\_prob\) during training using samples from a Bernoulli distribution.

The outputs are scaled by a factor of \(\frac{1}{keep\_prob}\) during training so that the output layer remains at a similar scale. During inference, this layer returns the same tensor as the x.

This technique is proposed in paper Dropout: A Simple Way to Prevent Neural Networks from Overfitting and proved to be effective to reduce over-fitting and prevents neurons from co-adaptation. See more details in Improving neural networks by preventing co-adaptation of feature detectors.

注解

Each channel will be zeroed out independently on every construct call.

参数
  • keep_prob (float) – The keep rate, greater than 0 and less equal than 1. E.g. rate=0.9, dropping out 10% of input units. Default: 0.5.

  • dtype (mindspore.dtype) – Data type of x. Default: mindspore.float32.

Inputs:
  • x (Tensor) - The input of Dropout with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, output tensor with the same shape as the x.

引发
  • TypeError – If keep_prob is not a float.

  • TypeError – If dtype of x is not neither float16 nor float32.

  • ValueError – If keep_prob is not in range (0, 1].

  • ValueError – If length of shape of x is less than 1.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.ones([2, 2, 3]), mindspore.float32)
>>> net = nn.Dropout(keep_prob=0.8)
>>> net.set_train()
Dropout<keep_prob=0.8>
>>> output = net(x)
>>> print(output.shape)
(2, 2, 3)
class tinyms.layers.Flatten[源代码]

Flatten layer for the input.

Flattens a tensor without changing dimension of batch size on the 0-th axis.

Inputs:
  • x (Tensor) - Tensor of shape \((N, \ldots)\) to be flattened. The data type is Number. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions and the shape can’t be ().

Outputs:

Tensor, the shape of the output tensor is \((N, X)\), where \(X\) is the product of the remaining dimensions.

引发

TypeError – If x is not a subclass of Tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[[1.2, 1.2], [2.1, 2.1]], [[2.2, 2.2], [3.2, 3.2]]]), mindspore.float32)
>>> net = nn.Flatten()
>>> output = net(x)
>>> print(output)
[[1.2 1.2 2.1 2.1]
 [2.2 2.2 3.2 3.2]]
>>> print(f"before flatten the x shape is {x.shape}")
before flatten the x shape is  (2, 2, 2)
>>> print(f"after flatten the output shape is {output.shape}")
after flatten the output shape is (2, 4)
class tinyms.layers.Dense(in_channels, out_channels, weight_init='normal', bias_init='zeros', has_bias=True, activation=None)[源代码]

The dense connected layer.

Applies dense connected layer for the input. This layer implements the operation as:

\[\text{outputs} = \text{activation}(\text{X} * \text{kernel} + \text{bias}),\]

where \(X\) is the input tensors, \(\text{activation}\) is the activation function passed as the activation argument (if passed in), \(\text{kernel}\) is a weight matrix with the same data type as the \(X\) created by the layer, and \(\text{bias}\) is a bias vector with the same data type as the \(X\) created by the layer (only if has_bias is True).

参数
  • in_channels (int) – The number of channels in the input space.

  • out_channels (int) – The number of channels in the output space.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable weight_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable bias_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘zeros’.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: True.

  • activation (Union[str, Cell, Primitive]) – activate function applied to the output of the fully connected layer, eg. ‘ReLU’.Default: None.

Inputs:
  • x (Tensor) - Tensor of shape \((*, in\_channels)\). The in_channels in Args should be equal to \(in\_channels\) in Inputs.

Outputs:

Tensor of shape \((*, out\_channels)\).

引发
  • TypeError – If in_channels or out_channels is not an int.

  • TypeError – If has_bias is not a bool.

  • TypeError – If activation is not one of str, Cell, Primitive, None.

  • ValueError – If length of shape of weight_init is not equal to 2 or shape[0] of weight_init is not equal to out_channels or shape[1] of weight_init is not equal to in_channels.

  • ValueError – If length of shape of bias_init is not equal to 1 or shape[0] of bias_init is not equal to out_channels.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[180, 234, 154], [244, 48, 247]]), mindspore.float32)
>>> net = nn.Dense(3, 4)
>>> output = net(x)
>>> print(output.shape)
(2, 4)
class tinyms.layers.ClipByNorm(axis=None)[源代码]

Clips tensor values to a maximum \(L_2\)-norm.

The output of this layer remains the same if the \(L_2\)-norm of the input tensor is not greater than the argument clip_norm. Otherwise the tensor will be normalized as:

\[\text{output}(X) = \frac{\text{clip_norm} * X}{L_2(X)},\]

where \(L_2(X)\) is the \(L_2\)-norm of \(X\).

参数

axis (Union[None, int, tuple(int)]) – Compute the L2-norm along the Specific dimension. Default: None, all dimensions to calculate.

Inputs:
  • x (Tensor) - Tensor of shape N-D. The type must be float32 or float16.

  • clip_norm (Tensor) - A scalar Tensor of shape \(()\) or \((1)\). Or a tensor shape can be broadcast to input shape.

Outputs:

Tensor, clipped tensor with the same shape as the x, whose type is float32.

引发
  • TypeError – If axis is not one of None, int, tuple.

  • TypeError – If dtype of x is neither float32 nor float16.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.ClipByNorm()
>>> x = Tensor(np.random.randint(0, 10, [4, 16]), mindspore.float32)
>>> clip_norm = Tensor(np.array([100]).astype(np.float32))
>>> output = net(x, clip_norm)
>>> print(output.shape)
(4, 16)
class tinyms.layers.Norm(axis=(), keep_dims=False)[源代码]

Computes the norm of vectors, currently including Euclidean norm, i.e., \(L_2\)-norm.

\[norm(x) = \sqrt{\sum_{i=1}^{n} (x_i^2)}\]
参数
  • axis (Union[tuple, int]) – The axis over which to compute vector norms. Default: ().

  • keep_dims (bool) – If true, the axis indicated in axis are kept with size 1. Otherwise, the dimensions in axis are removed from the output shape. Default: False.

Inputs:
  • x (Tensor) - Tensor which is not empty. The data type should be float16 or float32. \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, output tensor with dimensions in ‘axis’ reduced to 1 will be returned if ‘keep_dims’ is True; otherwise a Tensor with dimensions in ‘axis’ removed is returned. The data type is the same with x.

引发
  • TypeError – If axis is neither an int nor a tuple.

  • TypeError – If keep_dims is not a bool.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Norm(axis=0)
>>> x = Tensor(np.array([[4, 4, 9, 1], [2, 1, 3, 6]]), mindspore.float32)
>>> print(x.shape)
(2, 4)
>>> output = net(x)
>>> print(output)
[4.472136 4.1231055 9.486833 6.0827627]
>>> print(output.shape)
(4,)
>>> net = nn.Norm(axis=0, keep_dims=True)
>>> x = Tensor(np.array([[4, 4, 9, 1], [2, 1, 3, 6]]), mindspore.float32)
>>> print(x.shape)
(2, 4)
>>> output = net(x)
>>> print(output)
[4.472136 4.1231055 9.486833 6.0827627]
>>> print(output.shape)
(1, 4)
>>> net = nn.Norm(axis=1)
>>> x = Tensor(np.array([[4, 4, 9, 1], [2, 1, 3, 6]]), mindspore.float32)
>>> print(x.shape)
(2, 4)
>>> output = net(x)
>>> print(output)
[10.677078 7.071068]
>>> print(output.shape)
(2,)
class tinyms.layers.OneHot(axis=-1, depth=1, on_value=1.0, off_value=0.0, dtype=mindspore.float32)[源代码]

Returns a one-hot tensor.

The locations represented by indices in argument indices take value on_value, while all other locations take value off_value.

注解

If the input indices is rank \(N\), the output will have rank \(N+1\). The new axis is created at dimension axis.

If indices is a scalar, the output shape will be a vector of length depth.

If indices is a vector of length features, the output shape will be:

features * depth if axis == -1

depth * features if axis == 0

If indices is a matrix with shape [batch, features], the output shape will be:

batch * features * depth if axis == -1

batch * depth * features if axis == 1

depth * batch * features if axis == 0
参数
  • axis (int) – Features x depth if axis is -1, depth x features if axis is 0. Default: -1.

  • depth (int) – A scalar defining the depth of the one hot dimension. Default: 1.

  • on_value (float) – A scalar defining the value to fill in output[i][j] when indices[j] = i. Default: 1.0.

  • off_value (float) – A scalar defining the value to fill in output[i][j] when indices[j] != i. Default: 0.0.

  • dtype (mindspore.dtype) – Data type of ‘on_value’ and ‘off_value’, not the data type of indices. Default: mindspore.float32.

Inputs:
  • indices (Tensor) - A tensor of indices with data type of int32 or int64. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, the one-hot tensor of data type dtype with dimension at axis expanded to depth and filled with on_value and off_value. The dimension of the Outputs is equal to the dimension of the indices plus one.

引发
  • TypeError – If axis or depth is not an int.

  • TypeError – If dtype of indices is neither int32 nor int64.

  • ValueError – If axis is not in range [-1, len(indices_shape)].

  • ValueError – If depth is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # 1st sample: add new coordinates at axis 1
>>> net = nn.OneHot(depth=4, axis=1)
>>> indices = Tensor([[1, 3], [0, 2]], dtype=mindspore.int32)
>>> output = net(indices)
>>> print(output)
[[[0. 0.]
  [1. 0.]
  [0. 0.]
  [0. 1.]]
 [[1. 0.]
  [0. 0.]
  [0. 1.]
  [0. 0.]]]
>>> # The results are shown below:
>>> print(output.shape)
(2, 4, 2)
>>> # 2nd sample: add new coordinates at axis 0
>>> net = nn.OneHot(depth=4, axis=0)
>>> indices = Tensor([[1, 3], [0, 2]], dtype=mindspore.int32)
>>> output = net(indices)
>>> print(output)
[[[0. 0.]
  [1. 0.]]
 [[1. 0.]
  [0. 0.]]
 [[0. 0.]
  [0. 1.]]
 [[0. 1.]
  [0. 0.]]]
>>> # The results are shown below:
>>> print(output.shape)
(4, 2, 2)
>>> # 3rd sample: add new coordinates at the last dimension.
>>> net = nn.OneHot(depth=4, axis=-1)
>>> indices = Tensor([[1, 3], [0, 2]], dtype=mindspore.int32)
>>> output = net(indices)
>>> # The results are shown below:
>>> print(output)
[[[0. 1. 0. 0.]
  [0. 0. 0. 1.]]
 [[1. 0. 0. 0.]
  [0. 0. 1. 0.]]]
>>> print(output.shape)
(2, 2, 4)
>>> indices = Tensor([1, 3, 0, 2], dtype=mindspore.int32)
>>> output = net(indices)
>>> print(output)
[[0. 1. 0. 0.]
 [0. 0. 0. 1.]
 [1. 0. 0. 0.]
 [0. 0. 1. 0.]]
>>> print(output.shape)
(4, 4)
class tinyms.layers.Pad(paddings, mode='CONSTANT')[源代码]

Pads the input tensor according to the paddings and mode.

参数
  • paddings (tuple) –

    The shape of parameter paddings is (N, 2). N is the rank of input data. All elements of paddings are int type. For D th dimension of the x, paddings[D, 0] indicates how many sizes to be extended ahead of the D th dimension of the input tensor, and paddings[D, 1] indicates how many sizes to be extended behind of the D th dimension of the input tensor. The padded size of each dimension D of the output is: \(paddings[D, 0] + input\_x.dim\_size(D) + paddings[D, 1]\), e.g.:

    mode = "CONSTANT".
    paddings = [[1,1], [2,2]].
    x = [[1,2,3], [4,5,6], [7,8,9]].
    # The above can be seen: 1st dimension of `x` is 3, 2nd dimension of `x` is 3.
    # Substitute into the formula to get:
    # 1st dimension of output is paddings[0][0] + 3 + paddings[0][1] = 1 + 3 + 1 = 5.
    # 2nd dimension of output is paddings[1][0] + 3 + paddings[1][1] = 2 + 3 + 2 = 7.
    # So the shape of output is (5, 7).
    

  • mode (str) – Specifies padding mode. The optional values are “CONSTANT”, “REFLECT”, “SYMMETRIC”. Default: “CONSTANT”.

Inputs:
  • x (Tensor) - The input tensor.

Outputs:

Tensor, the tensor after padding.

  • If mode is “CONSTANT”, it fills the edge with 0, regardless of the values of the x. If the x is [[1,2,3], [4,5,6], [7,8,9]] and paddings is [[1,1], [2,2]], then the Outputs is [[0,0,0,0,0,0,0], [0,0,1,2,3,0,0], [0,0,4,5,6,0,0], [0,0,7,8,9,0,0], [0,0,0,0,0,0,0]].

  • If mode is “REFLECT”, it uses a way of symmetrical copying through the axis of symmetry to fill in. If the x is [[1,2,3], [4,5,6], [7,8,9]] and paddings is [[1,1], [2,2]], then the Outputs is [[6,5,4,5,6,5,4], [3,2,1,2,3,2,1], [6,5,4,5,6,5,4], [9,8,7,8,9,8,7], [6,5,4,5,6,5,4]].

  • If mode is “SYMMETRIC”, the filling method is similar to the “REFLECT”. It is also copied according to the symmetry axis, except that it includes the symmetry axis. If the x is [[1,2,3], [4,5,6], [7,8,9]] and paddings is [[1,1], [2,2]], then the Outputs is [[2,1,1,2,3,3,2], [2,1,1,2,3,3,2], [5,4,4,5,6,6,5], [8,7,7,8,9,9,8], [8,7,7,8,9,9,8]].

引发
  • TypeError – If paddings is not a tuple.

  • ValueError – If length of paddings is more than 4 or its shape is not (n, 2).

  • ValueError – If mode is not one of ‘CONSTANT’, ‘REFLECT’, ‘SYMMETRIC’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> from mindspore import Tensor
>>> import mindspore.nn as nn
>>> import numpy as np
>>> # If `mode` is "CONSTANT"
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.pad = nn.Pad(paddings=((1, 1), (2, 2)), mode="CONSTANT")
...     def construct(self, x):
...         return self.pad(x)
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]]), mindspore.float32)
>>> pad = Net()
>>> output = pad(x)
>>> print(output)
[[0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 2. 3. 0. 0.]
 [0. 0. 4. 5. 6. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]
>>> # Another way to call
>>> pad = ops.Pad(paddings=((1, 1), (2, 2)))
>>> # From the above code, we can see following:
>>> # "paddings=((1, 1), (2, 2))",
>>> # paddings[0][0] = 1, indicates a row of values is filled top of the input data in the 1st dimension.
>>> # Shown as follows:
>>> # [[0. 0. 0.]
>>> #  [1. 2. 3.]
>>> #  [4. 5. 6.]]
>>> # paddings[0][1] = 1 indicates a row of values is filled below input data in the 1st dimension.
>>> # Shown as follows:
>>> # [[0. 0. 0.]
>>> #  [1. 2. 3.]
>>> #  [4. 5. 6.]
>>> #  [0. 0. 0.]]
>>> # paddings[1][0] = 2, indicates 2 rows of values is filled in front of input data in the 2nd dimension.
>>> # Shown as follows:
>>> # [[0. 0. 0. 0. 0.]
>>> #  [0. 0. 1. 2. 3.]
>>> #  [0. 0. 4. 5. 6.]
>>> #  [0. 0. 0. 0. 0.]]
>>> # paddings[1][1] = 2, indicates 2 rows of values is filled in front of input data in the 2nd dimension.
>>> # Shown as follows:
>>> # [[0. 0. 0. 0. 0. 0. 0.]
>>> #  [0. 0. 1. 2. 3. 0. 0.]
>>> #  [0. 0. 4. 5. 6. 0. 0.]
>>> #  [0. 0. 0. 0. 0. 0. 0.]]
>>> output = pad(x)
>>> print(output)
[[0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 2. 3. 0. 0.]
 [0. 0. 4. 5. 6. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0.]]
>>> # if mode is "REFLECT"
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.pad = nn.Pad(paddings=((1, 1), (2, 2)), mode="REFLECT")
...     def construct(self, x):
...         return self.pad(x)
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]]), mindspore.float32)
>>> pad = Net()
>>> output = pad(x)
>>> print(output)
[[6. 5. 4. 5. 6. 5. 4.]
 [3. 2. 1. 2. 3. 2. 1.]
 [6. 5. 4. 5. 6. 5. 4.]
 [3. 2. 1. 2. 3. 2. 1.]]
>>> # if mode is "SYMMETRIC"
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.pad = nn.Pad(paddings=((1, 1), (2, 2)), mode="SYMMETRIC")
...     def construct(self, x):
...         return self.pad(x)
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]]), mindspore.float32)
>>> pad = Net()
>>> output = pad(x)
>>> print(output)
[[2. 1. 1. 2. 3. 3. 2.]
 [2. 1. 1. 2. 3. 3. 2.]
 [5. 4. 4. 5. 6. 6. 5.]
 [5. 4. 4. 5. 6. 6. 5.]]
class tinyms.layers.Unfold(ksizes, strides, rates, padding='valid')[源代码]

Extracts patches from images. The input tensor must be a 4-D tensor and the data format is NCHW.

参数
  • ksizes (Union[tuple[int], list[int]]) – The size of sliding window, must be a tuple or a list of integers, and the format is [1, ksize_row, ksize_col, 1].

  • strides (Union[tuple[int], list[int]]) – Distance between the centers of the two consecutive patches, must be a tuple or list of int, and the format is [1, stride_row, stride_col, 1].

  • rates (Union[tuple[int], list[int]]) – In each extracted patch, the gap between the corresponding dimension pixel positions, must be a tuple or a list of integers, and the format is [1, rate_row, rate_col, 1].

  • padding (str) –

    The type of padding algorithm, is a string whose value is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Means that the patch can take the part beyond the original image, and this part is filled with 0.

    • valid: Means that the taken patch area must be completely covered in the original image.

Inputs:
  • x (Tensor) - A 4-D tensor whose shape is [in_batch, in_depth, in_row, in_col] and data type is number.

Outputs:

Tensor, a 4-D tensor whose data type is same as x, and the shape is [out_batch, out_depth, out_row, out_col] where out_batch is the same as the in_batch.

\(out\_depth = ksize\_row * ksize\_col * in\_depth\)

\(out\_row = (in\_row - (ksize\_row + (ksize\_row - 1) * (rate\_row - 1))) // stride\_row + 1\)

\(out\_col = (in\_col - (ksize\_col + (ksize\_col - 1) * (rate\_col - 1))) // stride\_col + 1\)

引发
  • TypeError – If ksizes, strides or rates is neither a tuple nor list.

  • ValueError – If shape of ksizes, strides or rates is not (1, x_row, x_col, 1).

  • ValueError – If the second and third element of ksizes, strides or rates is less than 1.

Supported Platforms:

Ascend

实际案例

>>> net = Unfold(ksizes=[1, 2, 2, 1], strides=[1, 2, 2, 1], rates=[1, 2, 2, 1])
>>> # As stated in the above code:
>>> # ksize_row = 2, ksize_col = 2, rate_row = 2, rate_col = 2, stride_row = 2, stride_col = 2.
>>> image = Tensor(np.ones([2, 3, 6, 6]), dtype=mstype.float16)
>>> # in_batch = 2, in_depth = 3, in_row = 6, in_col = 6.
>>> # Substituting the formula to get:
>>> # out_batch = in_batch = 2
>>> # out_depth = 2 * 2 * 3 = 12
>>> # out_row = (6 - (2 + (2 - 1) * (2 - 1))) // 2 + 1 = 2
>>> # out_col = (6 - (2 + (2 - 1) * (2 - 1))) // 2 + 1 = 2
>>> output = net(image)
>>> print(output.shape)
(2, 12, 2, 2)
class tinyms.layers.Tril[源代码]

Returns a tensor with elements above the kth diagonal zeroed.

Inputs:
  • x (Tensor) - The input tensor. The data type is Number. \((N,*)\) where \(*\) means, any number of additional dimensions.

  • k (Int) - The index of diagonal. Default: 0

Outputs:

Tensor, has the same shape and type as input x.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x)
>>> print(result)
[[ 1  0  0  0]
 [ 5  6  0  0]
 [10 11 12  0]
 [14 15 16 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x, 1)
>>> print(result)
[[ 1  2  0  0]
 [ 5  6  7  0]
 [10 11 12 13]
 [14 15 16 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x, 2)
>>> print(result)
[[ 1  2  3  0]
 [ 5  6  7  8]
 [10 11 12 13]
 [14 15 16 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> tril = nn.Tril()
>>> result = tril(x, -1)
>>> print(result)
[[ 0  0  0  0]
 [ 5  0  0  0]
 [10 11  0  0]
 [14 15 16  0]]
class tinyms.layers.Triu[源代码]

Returns a tensor with elements below the kth diagonal zeroed.

Inputs:
  • x (Tensor) - The input tensor. The data type is Number. \((N,*)\) where \(*\) means, any number of additional dimensions.

  • k (Int) - The index of diagonal. Default: 0

Outputs:

Tensor, has the same type and shape as input x.

引发
Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> triu = nn.Triu()
>>> result = triu(x)
>>> print(result)
[[ 1  2  3  4]
 [ 0  6  7  8]
 [ 0  0 12 13]
 [ 0  0  0 17]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> triu = nn.Triu()
>>> result = triu(x, 1)
>>> print(result)
[[ 0  2  3  4]
 [ 0  0  7  8]
 [ 0  0  0 13]
 [ 0  0  0  0]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> triu = nn.Triu()
>>> result = triu(x, 2)
>>> print(result)
[[ 0  0  3  4]
 [ 0  0  0  8]
 [ 0  0  0  0]
 [ 0  0  0  0]]
>>> x = Tensor(np.array([[ 1,  2,  3,  4],
...                      [ 5,  6,  7,  8],
...                      [10, 11, 12, 13],
...                      [14, 15, 16, 17]]))
>>> triu = nn.Triu()
>>> result = triu(x, -1)
>>> print(result)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 0 11 12 13]
 [ 0  0 16 17]]
class tinyms.layers.ResizeBilinear[源代码]

Samples the input tensor to the given size or scale_factor by using bilinear interpolate.

Inputs:
  • x (Tensor) - Tensor to be resized. Input tensor must be a 4-D tensor with shape \((batch, channels, height, width)\), with data type of float16 or float32.

  • size (Union[tuple[int], list[int]]): A tuple or list of 2 int elements \((new\_height, new\_width)\),the new size of the tensor. One and only one of size and scale_factor can be set to None. Default: None.

  • scale_factor (int): The scale factor of new size of the tensor. The value should be positive integer. One and only one of size and scale_factor can be set to None. Default: None.

  • align_corners (bool): If true, rescale input by \((new\_height - 1) / (height - 1)\), which exactly aligns the 4 corners of images and resized images. If false, rescale by \(new\_height / height\). Default: False.

Outputs:

Resized tensor. If size is set, the result is 4-D tensor with shape \((batch, channels, new\_height, new\_width)\), and the data type is the same as x. If scale is set, the result is 4-D tensor with shape \((batch, channels, scale\_factor * height, scale\_factor * width)\) and the data type is the same as x.

引发
  • TypeError – If size is not one of tuple, list, None.

  • TypeError – If scale_factor is neither int nor None.

  • TypeError – If align_corners is not a bool.

  • TypeError – If dtype of x is neither float16 nor float32.

  • ValueError – If size and scale_factor are both None or not None.

  • ValueError – If length of shape of x is not equal to 4.

  • ValueError – If scale_factor is an int which is less than 0.

  • ValueError – If size is a list or tuple whose length is not equal to 2.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> x = Tensor([[[[1, 2, 3, 4], [5, 6, 7, 8]]]], mindspore.float32)
>>> resize_bilinear = nn.ResizeBilinear()
>>> result = resize_bilinear(x, size=(5,5))
>>> print(x)
[[[[1. 2. 3. 4.]
   [5. 6. 7. 8.]]]]
>>> print(result)
[[[[1.        1.8       2.6       3.4       4.       ]
   [2.6       3.4       4.2000003 5.        5.6000004]
   [4.2       5.0000005 5.8       6.6       7.2      ]
   [5.        5.8       6.6       7.4       8.       ]
   [5.        5.8       6.6       7.4000006 8.       ]]]]
>>> print(result.shape)
(1, 1, 5, 5)
class tinyms.layers.MatrixDiag[源代码]

Returns a batched diagonal tensor with a given batched diagonal values.

Assume x has \(k\) dimensions \([I, J, K, ..., N]\), then the output is a tensor of rank \(k+1\) with dimensions \([I, J, K, ..., N, N]\) where: \(output[i, j, k, ..., m, n] = 1\{m=n\} * x[i, j, k, ..., n]\)

Inputs:
  • x (Tensor) - The diagonal values. It can be one of the following data types: float32, float16, int32, int8, and uint8. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, has the same type as input x. The shape must be x.shape + (x.shape[-1], ).

引发

TypeError – If dtype of x is not one of float32, float16, int32, int8 or uint8.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor(np.array([1, -1]), mindspore.float32)
>>> matrix_diag = nn.MatrixDiag()
>>> output = matrix_diag(x)
>>> print(x.shape)
(2,)
>>> print(output)
[[ 1.  0.]
 [ 0. -1.]]
>>> print(output.shape)
(2, 2)
>>> x = Tensor(np.array([[1, -1], [1, -1]]), mindspore.float32)
>>> matrix_diag = nn.MatrixDiag()
>>> output = matrix_diag(x)
>>> print(x.shape)
(2, 2)
>>> print(output)
[[[ 1.  0.]
  [ 0. -1.]]
 [[ 1.  0.]
  [ 0. -1.]]]
>>> print(output.shape)
(2, 2, 2)
>>> x = Tensor(np.array([[1, -1, 1], [1, -1, 1]]), mindspore.float32)
>>> matrix_diag = nn.MatrixDiag()
>>> output = matrix_diag(x)
>>> print(x.shape)
(2, 3)
>>> print(output)
[[[ 1.  0.  0.]
  [ 0. -1.  0.]
  [ 0.  0.  1.]
 [[ 1.  0.  0.]
  [ 0. -1.  0.]
  [ 0.  0.  1.]]]
>>> print(output.shape)
(2, 3, 3)
class tinyms.layers.MatrixDiagPart[源代码]

Returns the batched diagonal part of a batched tensor.

Assume x has \(k\) dimensions \([I, J, K, ..., M, N]\), then the output is a tensor of rank \(k-1\) with dimensions \([I, J, K, ..., min(M, N)]\) where: \(output[i, j, k, ..., n] = x[i, j, k, ..., n, n]\)

Inputs:
  • x (Tensor) - The batched tensor. It can be one of the following data types: float32, float16, int32, int8, and uint8.

Outputs:

Tensor, has the same type as input x. The shape must be x.shape[:-2] + [min(x.shape[-2:])].

引发

TypeError – If dtype of x is not one of float32, float16, int32, int8 or uint8.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor([[[-1, 0], [0, 1]],
...             [[-1, 0], [0, 1]],
...             [[-1, 0], [0, 1]]], mindspore.float32)
>>> matrix_diag_part = nn.MatrixDiagPart()
>>> output = matrix_diag_part(x)
>>> print(output)
[[-1.  1.]
 [-1.  1.]
 [-1.  1.]]
>>> x = Tensor([[-1, 0, 0, 1],
...             [-1, 0, 0, 1],
...             [-1, 0, 0, 1],
...             [-1, 0, 0, 1]], mindspore.float32)
>>> matrix_diag_part = nn.MatrixDiagPart()
>>> output = matrix_diag_part(x)
>>> print(output)
[-1 0 0 1]
class tinyms.layers.MatrixSetDiag[源代码]

Modifies the batched diagonal part of a batched tensor.

Assume x has \(k+1\) dimensions \([I, J, K, ..., M, N]\) and diagonal has \(k\) dimensions \([I, J, K, ..., min(M, N)]\). Then the output is a tensor of rank \(k+1\) with dimensions \([I, J, K, ..., M, N]\) where:

\[output[i, j, k, ..., m, n] = diagnoal[i, j, k, ..., n]\ for\ m == n\]
\[output[i, j, k, ..., m, n] = x[i, j, k, ..., m, n]\ for\ m != n\]
Inputs:
  • x (Tensor) - The batched tensor. Rank k+1, where k >= 1. It can be one of the following data types: float32, float16, int32, int8, and uint8.

  • diagonal (Tensor) - The diagonal values. Must have the same type as input x. Rank k, where k >= 1.

Outputs:

Tensor, has the same type and shape as input x.

引发
  • TypeError – If dtype of x or diagonal is not one of float32, float16, int32, int8 or uint8.

  • ValueError – If length of shape of x is less than 2.

  • ValueError – If x_shape[-2] < x_shape[-1] and x_shape[:-1] != diagonal_shape.

  • ValueError – If x_shape[-2] >= x_shape[-1] and x_shape[:-2] + x_shape[-1:] != diagonal_shape.

Supported Platforms:

Ascend

实际案例

>>> x = Tensor([[[-1, 0], [0, 1]], [[-1, 0], [0, 1]], [[-1, 0], [0, 1]]], mindspore.float32)
>>> diagonal = Tensor([[-1., 2.], [-1., 1.], [-1., 1.]], mindspore.float32)
>>> matrix_set_diag = nn.MatrixSetDiag()
>>> output = matrix_set_diag(x, diagonal)
>>> print(output)
[[[-1.  0.]
  [ 0.  2.]]
 [[-1.  0.]
  [ 0.  1.]]
 [[-1.  0.]
  [ 0.  1.]]]
class tinyms.layers.L1Regularizer(scale)[源代码]

Applies l1 regularization to weights.

l1 regularization makes weights sparsity

\[\text{loss}=\lambda * \text{reduce_sum}(\text{abs}(\omega))\]

注解

scale(regularization factor) should be a number which greater than 0

参数

scale (int, float) – l1 regularization factor which greater than 0.

Inputs:
  • weights (Tensor) - The input of L1Regularizer with data type of float16 or float32. The shape is \((N,*)\) where \(*\) means, any number of additional dimensions.

Outputs:

Tensor, which dtype is higher precision data type between mindspore.float32 and weights dtype, and Tensor shape is ()

引发
  • TypeError – If scale is neither an int nor float.

  • ValueError – If scale is not greater than 0.

  • ValueError – If scale is math.inf or math.nan.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> scale = 0.5
>>> net = nn.L1Regularizer(scale)
>>> weights = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
>>> output = net(weights)
>>> print(output.asnumpy())
5.0
class tinyms.layers.Roll(shift, axis)[源代码]

Rolls the elements of a tensor along an axis.

The elements are shifted positively (towards larger indices) by the offset of shift along the dimension of axis. Negative shift values will shift elements in the opposite direction. Elements that roll passed the last position will wrap around to the first and vice versa. Multiple shifts along multiple axes may be specified.

参数
  • shift (Union[list(int), tuple(int), int]) – Specifies the number of places by which elements are shifted positively (towards larger indices) along the specified dimension. Negative shifts will roll the elements in the opposite direction.

  • axis (Union[list(int), tuple(int), int]) – Specifies the dimension indexes of shape to be rolled.

Inputs:
  • input_x (Tensor) - Input tensor.

Outputs:

Tensor, has the same shape and type as input_x.

引发
  • TypeError – If shift is not an int, a tuple or a list.

  • TypeError – If axis is not an int, a tuple or a list.

  • TypeError – If element of shift is not an int.

  • TypeError – If element of axis is not an int.

  • ValueError – If axis is out of the range [-len(input_x.shape), len(input_x.shape)).

  • ValueError – If length of shape of shift is not equal to length of shape of axis.

Supported Platforms:

Ascend

实际案例

>>> input_x = Tensor(np.array([0, 1, 2, 3, 4]).astype(np.float32))
>>> op = nn.Roll(shift=2, axis=0)
>>> output = op(input_x)
>>> print(output)
[3. 4. 0. 1. 2.]
>>> input_x = Tensor(np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]).astype(np.float32))
>>> op = nn.Roll(shift=[1, -2], axis=[0, 1])
>>> output = op(input_x)
>>> print(output)
[[7. 8. 9. 5. 6.]
 [2. 3. 4. 0. 1.]]
class tinyms.layers.Embedding(vocab_size, embedding_size, use_one_hot=False, embedding_table='normal', dtype=mindspore.float32, padding_idx=None)[源代码]

A simple lookup table that stores embeddings of a fixed dictionary and size.

This module is often used to store word embeddings and retrieve them using indices. The input to the module is a list of indices, and the output is the corresponding word embeddings.

注解

When ‘use_one_hot’ is set to True, the type of the x must be mindspore.int32.

参数
  • vocab_size (int) – Size of the dictionary of embeddings.

  • embedding_size (int) – The size of each embedding vector.

  • use_one_hot (bool) – Specifies whether to apply one_hot encoding form. Default: False.

  • embedding_table (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the embedding_table. Refer to class initializer for the values of string when a string is specified. Default: ‘normal’.

  • dtype (mindspore.dtype) – Data type of x. Default: mindspore.float32.

  • padding_idx (int, None) – When the padding_idx encounters index, the output embedding vector of this index will be initialized to zero. Default: None. The feature is inactivated.

Inputs:
  • x (Tensor) - Tensor of shape \((\text{batch_size}, \text{x_length})\). The elements of the Tensor must be integer and not larger than vocab_size. Otherwise the corresponding embedding vector will be zero. The data type is int32 or int64.

Outputs:

Tensor of shape \((\text{batch_size}, \text{x_length}, \text{embedding_size})\).

引发
  • TypeError – If vocab_size or embedding_size is not an int.

  • TypeError – If use_one_hot is not a bool.

  • ValueError – If padding_idx is an int which not in range [0, vocab_size].

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Embedding(20000, 768,  True)
>>> x = Tensor(np.ones([8, 128]), mindspore.int32)
>>> # Maps the input word IDs to word embedding.
>>> output = net(x)
>>> result = output.shape
>>> print(result)
(8, 128, 768)
class tinyms.layers.EmbeddingLookup(vocab_size, embedding_size, param_init='normal', target='CPU', slice_mode='batch_slice', manual_shapes=None, max_norm=None, sparse=True, vocab_cache_size=0)[源代码]

Returns a slice of the input tensor based on the specified indices.

注解

When ‘target’ is set to ‘CPU’, this module will use P.EmbeddingLookup().add_prim_attr(‘primitive_target’, ‘CPU’) which specified ‘offset = 0’ to lookup table. When ‘target’ is set to ‘DEVICE’, this module will use P.Gather() which specified ‘axis = 0’ to lookup table. In field slice mode, the manual_shapes must be given. It is a tuple ,where the element is vocab[i], vocab[i] is the row numbers for i-th part.

参数
  • vocab_size (int) – Size of the dictionary of embeddings.

  • embedding_size (int) – The size of each embedding vector.

  • param_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the embedding_table. Refer to class initializer for the values of string when a string is specified. Default: ‘normal’.

  • target (str) – Specifies the target where the op is executed. The value must in [‘DEVICE’, ‘CPU’]. Default: ‘CPU’.

  • slice_mode (str) – The slicing way in semi_auto_parallel/auto_parallel. The value must get through nn.EmbeddingLookup. Default: nn.EmbeddingLookup.BATCH_SLICE.

  • manual_shapes (tuple) – The accompaniment array in field slice mode.

  • max_norm (Union[float, None]) – A maximum clipping value. The data type must be float16, float32 or None. Default: None

  • sparse (bool) – Using sparse mode. When ‘target’ is set to ‘CPU’, ‘sparse’ has to be true. Default: True.

  • vocab_cache_size (int) – Cache size of the dictionary of embeddings. Default: 0. It is valid only in ‘DEVICE’ target. And the moment parameter of corresponding optimizer will also be set to the cache size. In addition, it should be noted that it will cost the ‘DEVICE’ memory, so suggests setting a reasonable value to avoid insufficient memory.

Inputs:
  • input_indices (Tensor) - The shape of tensor is \((y_1, y_2, ..., y_S)\). Specifies the indices of elements of the original Tensor. Values can be out of range of embedding_table, and the exceeding part will be filled with 0 in the output. Values does not support negative and the result is undefined if values are negative. Input_indices must only be a 2d tensor in this interface when run in semi auto parallel/auto parallel mode.

Outputs:

Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\).

引发
  • TypeError – If vocab_size or embedding_size or vocab_cache_size is not an int.

  • TypeError – If sparse is not a bool or manual_shapes is not a tuple.

  • ValueError – If vocab_size or embedding_size is less than 1.

  • ValueError – If vocab_cache_size is less than 0.

  • ValueError – If target is neither ‘CPU’ nor ‘DEVICE’.

  • ValueError – If slice_mode is not one of ‘batch_slice’ or ‘field_slice’ or ‘table_row_slice’ or ‘table_column_slice’.

  • ValueError – If sparse is False and target is ‘CPU’.

  • ValueError – If slice_mode is ‘field_slice’ and manual_shapes is None.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> input_indices = Tensor(np.array([[1, 0], [3, 2]]), mindspore.int32)
>>> result = nn.EmbeddingLookup(4,2)(input_indices)
>>> print(result.shape)
(2, 2, 2)
class tinyms.layers.MultiFieldEmbeddingLookup(vocab_size, embedding_size, field_size, param_init='normal', target='CPU', slice_mode='batch_slice', feature_num_list=None, max_norm=None, sparse=True, operator='SUM')[源代码]

Returns a slice of input tensor based on the specified indices and the field ids. This operation supports looking up embeddings using multi hot and one hot fields simultaneously.

注解

When ‘target’ is set to ‘CPU’, this module will use P.EmbeddingLookup().add_prim_attr(‘primitive_target’, ‘CPU’) which specified ‘offset = 0’ to lookup table. When ‘target’ is set to ‘DEVICE’, this module will use P.Gather() which specified ‘axis = 0’ to lookup table. The vectors with the same field_ids will be combined by the ‘operator’, such as ‘SUM’, ‘MAX’ and ‘MEAN’. Ensure the input_values of the padded id is zero, so that they can be ignored. The final output will be zeros if the sum of absolute weight of the field is zero. This class only supports [‘table_row_slice’, ‘batch_slice’ and ‘table_column_slice’]. For the operation ‘MAX’ on device Ascend, there is a constrain where batch_size * (seq_length + field_size) < 3500.

参数
  • vocab_size (int) – The size of the dictionary of embeddings.

  • embedding_size (int) – The size of each embedding vector.

  • field_size (int) – The field size of the final outputs.

  • param_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the embedding_table. Refer to class initializer for the values of string when a string is specified. Default: ‘normal’.

  • target (str) – Specifies the target where the op is executed. The value must in [‘DEVICE’, ‘CPU’]. Default: ‘CPU’.

  • slice_mode (str) – The slicing way in semi_auto_parallel/auto_parallel. The value must get through nn.EmbeddingLookup. Default: nn.EmbeddingLookup.BATCH_SLICE.

  • feature_num_list (tuple) – The accompaniment array in field slice mode. This is unused currently. Default: None.

  • max_norm (Union[float, None]) – A maximum clipping value. The data type must be float16, float32 or None. Default: None

  • sparse (bool) – Using sparse mode. When ‘target’ is set to ‘CPU’, ‘sparse’ has to be true. Default: True.

  • operator (str) – The pooling method for the features in one field. Support ‘SUM’, ‘MEAN’ and ‘MAX’. Default: ‘SUM’.

Inputs:
  • input_indices (Tensor) - The shape of tensor is \((batch\_size, seq\_length)\). Specifies the indices of elements of the original Tensor. Input_indices must be a 2d tensor in this interface. Type is Int32, Int64.

  • input_values (Tensor) - The shape of tensor is \((batch\_size, seq\_length)\). Specifies the weights of elements of the input_indices. The lookout vector will multiply with the input_values. Type is Float32.

  • field_ids (Tensor) - The shape of tensor is \((batch\_size, seq\_length)\). Specifies the field id of elements of the input_indices. Type is Int32.

Outputs:

Tensor, the shape of tensor is \((batch\_size, field\_size, embedding\_size)\). Type is Float32.

引发
  • TypeError – If vocab_size or embedding_size or field_size is not an int.

  • TypeError – If sparse is not a bool or feature_num_list is not a tuple.

  • ValueError – If vocab_size or embedding_size or field_size is less than 1.

  • ValueError – If target is neither ‘CPU’ nor ‘DEVICE’.

  • ValueError – If slice_mode is not one of ‘batch_slice’, ‘field_slice’, ‘table_row_slice’, ‘table_column_slice’.

  • ValueError – If sparse is False and target is ‘CPU’.

  • ValueError – If slice_mode is ‘field_slice’ and feature_num_list is None.

  • ValueError – If operator is not one of ‘SUM’, ‘MAX’, ‘MEAN’.

Supported Platforms:

Ascend GPU

实际案例

>>> input_indices = Tensor([[2, 4, 6, 0, 0], [1, 3, 5, 0, 0]], mindspore.int32)
>>> input_values = Tensor([[1, 1, 1, 0, 0], [1, 1, 1, 0, 0]], mindspore.float32)
>>> field_ids = Tensor([[0, 1, 1, 0, 0], [0, 0, 1, 0, 0]], mindspore.int32)
>>> net = nn.MultiFieldEmbeddingLookup(10, 2, field_size=2, operator='SUM', target='DEVICE')
>>> out = net(input_indices, input_values, field_ids)
>>> print(out.shape)
(2, 2, 2)
class tinyms.layers.AvgPool2d(kernel_size=1, stride=1, pad_mode='valid', data_format='NCHW')[源代码]

2D average pooling for temporal data.

Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes.

Typically the input is of shape \((N_{in}, C_{in}, H_{in}, W_{in})\), AvgPool2d outputs regional average in the \((H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1)\), the operation is as follows.

\[\text{output}(N_i, C_j, h, w) = \frac{1}{h_{ker} * w_{ker}} \sum_{m=0}^{h_{ker}-1} \sum_{n=0}^{w_{ker}-1} \text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)\]

注解

pad_mode for training only supports “same” and “valid”.

参数
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the average value. The data type of kernel_size must be int and the value represents the height and width, or a tuple of two int numbers that represent height and width respectively. Default: 1.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If kernel_size or strides is neither int nor tuple.

  • ValueError – If pad_mode is neither ‘valid’ nor ‘same’ with not case sensitive.

  • ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.

  • ValueError – If kernel_size or strides is less than 1.

  • ValueError – If length of shape of x is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> pool = nn.AvgPool2d(kernel_size=3, stride=1)
>>> x = Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), mindspore.float32)
>>> output = pool(x)
>>> print(output.shape)
(1, 2, 2, 2)
class tinyms.layers.MaxPool2d(kernel_size=1, stride=1, pad_mode='valid', data_format='NCHW')[源代码]

2D max pooling operation for temporal data.

Applies a 2D max pooling over an input Tensor which can be regarded as a composition of 2D planes.

Typically the input is of shape \((N_{in}, C_{in}, H_{in}, W_{in})\), MaxPool2d outputs regional maximum in the \((H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1)\), the operation is as follows.

\[\text{output}(N_i, C_j, h, w) = \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1} \text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n)\]

注解

pad_mode for training only supports “same” and “valid”.

参数
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the max value, is an int number that represents height and width are both kernel_size, or a tuple of two int numbers that represent height and width respectively. Default: 1.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

  • data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If kernel_size or strides is neither int nor tuple.

  • ValueError – If pad_mode is neither ‘valid’ nor ‘same’ with not case sensitive.

  • ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.

  • ValueError – If kernel_size or strides is less than 1.

  • ValueError – If length of shape of x is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> pool = nn.MaxPool2d(kernel_size=3, stride=1)
>>> x = Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), mindspore.float32)
>>> output = pool(x)
>>> print(output.shape)
(1, 2, 2, 2)
class tinyms.layers.AvgPool1d(kernel_size=1, stride=1, pad_mode='valid')[源代码]

1D average pooling for temporal data.

Applies a 1D average pooling over an input Tensor which can be regarded as a composition of 1D input planes.

Typically the input is of shape \((N_{in}, C_{in}, L_{in})\), AvgPool1d outputs regional average in the \((L_{in})\)-dimension. Given kernel size \(ks = l_{ker}\) and stride \(s = s_0\), the operation is as follows.

\[\text{output}(N_i, C_j, l) = \frac{1}{l_{ker}} \sum_{n=0}^{l_{ker}-1} \text{input}(N_i, C_j, s_0 \times l + n)\]

注解

pad_mode for training only supports “same” and “valid”.

参数
  • kernel_size (int) – The size of kernel window used to take the average value, Default: 1.

  • stride (int) – The distance of kernel moving, an int number that represents the width of movement is strides, Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The height and width of the output will be the same as the input. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, L_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, L_{out})\).

引发
  • TypeError – If kernel_size or stride is not an int.

  • ValueError – If pad_mode is neither ‘same’ nor ‘valid’ with not case sensitive.

  • ValueError – If kernel_size or strides is less than 1.

  • ValueError – If length of shape of x is not equal to 3.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> pool = nn.AvgPool1d(kernel_size=6, stride=1)
>>> x = Tensor(np.random.randint(0, 10, [1, 3, 6]), mindspore.float32)
>>> output = pool(x)
>>> result = output.shape
>>> print(result)
(1, 3, 1)
class tinyms.layers.MaxPool1d(kernel_size=1, stride=1, pad_mode='valid')[源代码]

1D max pooling operation for temporal data.

Applies a 1D max pooling over an input Tensor which can be regarded as a composition of 1D planes.

Typically the input is of shape \((N_{in}, C_{in}, L_{in})\), MaxPool1d outputs regional maximum in the \((L_{in})\)-dimension. Given kernel size \(ks = (l_{ker})\) and stride \(s = (s_0)\), the operation is as follows.

\[\text{output}(N_i, C_j, l) = \max_{n=0, \ldots, l_{ker}-1} \text{input}(N_i, C_j, s_0 \times l + n)\]

注解

pad_mode for training only supports “same” and “valid”.

参数
  • kernel_size (int) – The size of kernel used to take the max value, Default: 1.

  • stride (int) – The distance of kernel moving, an int number that represents the width of movement is stride, Default: 1.

  • pad_mode (str) –

    The optional value for pad mode, is “same” or “valid”, not case sensitive. Default: “valid”.

    • same: Adopts the way of completion. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C, L_{in})\).

Outputs:

Tensor of shape \((N, C, L_{out}))\).

引发
  • TypeError – If kernel_size or strides is not an int.

  • ValueError – If pad_mode is neither ‘valid’ nor ‘same’ with not case sensitive.

  • ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.

  • ValueError – If kernel_size or strides is less than 1.

  • ValueError – If length of shape of x is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> max_pool = nn.MaxPool1d(kernel_size=3, stride=1)
>>> x = Tensor(np.random.randint(0, 10, [1, 2, 4]), mindspore.float32)
>>> output = max_pool(x)
>>> result = output.shape
>>> print(result)
(1, 2, 2)
class tinyms.layers.ImageGradients[源代码]

Returns two tensors, the first is along the height dimension and the second is along the width dimension.

Assume an image shape is \(h*w\). The gradients along the height and the width are \(dy\) and \(dx\), respectively.

\[ \begin{align}\begin{aligned}dy[i] = \begin{cases} image[i+1, :]-image[i, :], &if\ 0<=i<h-1 \cr 0, &if\ i==h-1\end{cases}\\dx[i] = \begin{cases} image[:, i+1]-image[:, i], &if\ 0<=i<w-1 \cr 0, &if\ i==w-1\end{cases}\end{aligned}\end{align} \]
Inputs:
  • images (Tensor) - The input image data, with format ‘NCHW’.

Outputs:
  • dy (Tensor) - vertical image gradients, the same type and shape as input.

  • dx (Tensor) - horizontal image gradients, the same type and shape as input.

引发

ValueError – If length of shape of images is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.ImageGradients()
>>> image = Tensor(np.array([[[[1, 2], [3, 4]]]]), dtype=mindspore.int32)
>>> output = net(image)
>>> print(output)
(Tensor(shape=[1, 1, 2, 2], dtype=Int32, value=
[[[[2, 2],
   [0, 0]]]]), Tensor(shape=[1, 1, 2, 2], dtype=Int32, value=
[[[[1, 0],
   [1, 0]]]]))
class tinyms.layers.SSIM(max_val=1.0, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03)[源代码]

Returns SSIM index between two images.

Its implementation is based on Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2004). Image quality assessment: from error visibility to structural similarity. IEEE transactions on image processing.

SSIM is a measure of the similarity of two pictures. Like PSNR, SSIM is often used as an evaluation of image quality. SSIM is a number between 0 and 1.The larger it is, the smaller the gap between the output image and the undistorted image, that is, the better the image quality. When the two images are exactly the same, SSIM=1.

\[\begin{split}l(x,y)&=\frac{2\mu_x\mu_y+C_1}{\mu_x^2+\mu_y^2+C_1}, C_1=(K_1L)^2.\\ c(x,y)&=\frac{2\sigma_x\sigma_y+C_2}{\sigma_x^2+\sigma_y^2+C_2}, C_2=(K_2L)^2.\\ s(x,y)&=\frac{\sigma_{xy}+C_3}{\sigma_x\sigma_y+C_3}, C_3=C_2/2.\\ SSIM(x,y)&=l*c*s\\&=\frac{(2\mu_x\mu_y+C_1)(2\sigma_{xy}+C_2}{(\mu_x^2+\mu_y^2+C_1)(\sigma_x^2+\sigma_y^2+C_2)}.\end{split}\]
参数
  • max_val (Union[int, float]) – The dynamic range of the pixel values (255 for 8-bit grayscale images). Default: 1.0.

  • filter_size (int) – The size of the Gaussian filter. Default: 11. The value must be greater than or equal to 1.

  • filter_sigma (float) – The standard deviation of Gaussian kernel. Default: 1.5. The value must be greater than 0.

  • k1 (float) – The constant used to generate c1 in the luminance comparison function. Default: 0.01.

  • k2 (float) – The constant used to generate c2 in the contrast comparison function. Default: 0.03.

Inputs:
  • img1 (Tensor) - The first image batch with format ‘NCHW’. It must be the same shape and dtype as img2.

  • img2 (Tensor) - The second image batch with format ‘NCHW’. It must be the same shape and dtype as img1.

Outputs:

Tensor, has the same dtype as img1. It is a 1-D tensor with shape N, where N is the batch num of img1.

引发
  • TypeError – If max_val is neither int nor float.

  • TypeError – If k1, k2 or filter_sigma is not a float.

  • TypeError – If filter_size is not an int.

  • ValueError – If max_val or filter_sigma is less than or equal to 0.

  • ValueError – If filter_size is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor
>>> net = nn.SSIM()
>>> img1 = Tensor(np.ones([1, 3, 16, 16]).astype(np.float32))
>>> img2 = Tensor(np.ones([1, 3, 16, 16]).astype(np.float32))
>>> output = net(img1, img2)
>>> print(output)
[1.]
class tinyms.layers.MSSSIM(max_val=1.0, power_factors=(0.0448, 0.2856, 0.3001, 0.2363, 0.1333), filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03)[源代码]

Returns MS-SSIM index between two images.

Its implementation is based on Wang, Zhou, Eero P. Simoncelli, and Alan C. Bovik. Multiscale structural similarity for image quality assessment. Signals, Systems and Computers, 2004.

\[\begin{split}l(x,y)&=\frac{2\mu_x\mu_y+C_1}{\mu_x^2+\mu_y^2+C_1}, C_1=(K_1L)^2.\\ c(x,y)&=\frac{2\sigma_x\sigma_y+C_2}{\sigma_x^2+\sigma_y^2+C_2}, C_2=(K_2L)^2.\\ s(x,y)&=\frac{\sigma_{xy}+C_3}{\sigma_x\sigma_y+C_3}, C_3=C_2/2.\\ MSSSIM(x,y)&=l^\alpha_M*{\prod_{1\leq j\leq M} (c^\beta_j*s^\gamma_j)}.\end{split}\]
参数
  • max_val (Union[int, float]) – The dynamic range of the pixel values (255 for 8-bit grayscale images). Default: 1.0.

  • power_factors (Union[tuple, list]) – Iterable of weights for each scal e. Default: (0.0448, 0.2856, 0.3001, 0.2363, 0.1333). Default values obtained by Wang et al.

  • filter_size (int) – The size of the Gaussian filter. Default: 11.

  • filter_sigma (float) – The standard deviation of Gaussian kernel. Default: 1.5.

  • k1 (float) – The constant used to generate c1 in the luminance comparison function. Default: 0.01.

  • k2 (float) – The constant used to generate c2 in the contrast comparison function. Default: 0.03.

Inputs:
  • img1 (Tensor) - The first image batch with format ‘NCHW’. It must be the same shape and dtype as img2.

  • img2 (Tensor) - The second image batch with format ‘NCHW’. It must be the same shape and dtype as img1.

Outputs:

Tensor, the value is in range [0, 1]. It is a 1-D tensor with shape N, where N is the batch num of img1.

引发
  • TypeError – If max_val is neither int nor float.

  • TypeError – If power_factors is neither tuple nor list.

  • TypeError – If k1, k2 or filter_sigma is not a float.

  • TypeError – If filter_size is not an int.

  • ValueError – If max_val or filter_sigma is less than or equal to 0.

  • ValueError – If filter_size is less than 0.

  • ValueError – If length of shape of img1 or img2 is not equal to 4.

Supported Platforms:

Ascend GPU

实际案例

>>> import numpy as np
>>> import mindspore.nn as nn
>>> from mindspore import Tensor
>>> net = nn.MSSSIM(power_factors=(0.033, 0.033, 0.033))
>>> img1 = Tensor(np.ones((1, 3, 128, 128)).astype(np.float32))
>>> img2 = Tensor(np.ones((1, 3, 128, 128)).astype(np.float32))
>>> output = net(img1, img2)
>>> print(output)
[1.]
class tinyms.layers.PSNR(max_val=1.0)[源代码]

Returns Peak Signal-to-Noise Ratio of two image batches.

It produces a PSNR value for each image in batch. Assume inputs are \(I\) and \(K\), both with shape \(h*w\). \(MAX\) represents the dynamic range of pixel values.

\[\begin{split}MSE&=\frac{1}{hw}\sum\limits_{i=0}^{h-1}\sum\limits_{j=0}^{w-1}[I(i,j)-K(i,j)]^2\\ PSNR&=10*log_{10}(\frac{MAX^2}{MSE})\end{split}\]
参数

max_val (Union[int, float]) – The dynamic range of the pixel values (255 for 8-bit grayscale images). The value must be greater than 0. Default: 1.0.

Inputs:
  • img1 (Tensor) - The first image batch with format ‘NCHW’. It must be the same shape and dtype as img2.

  • img2 (Tensor) - The second image batch with format ‘NCHW’. It must be the same shape and dtype as img1.

Outputs:

Tensor, with dtype mindspore.float32. It is a 1-D tensor with shape N, where N is the batch num of img1.

引发
  • TypeError – If max_val is neither int nor float.

  • ValueError – If max_val is less than or equal to 0.

  • ValueError – If length of shape of img1 or img2 is not equal to 4.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.PSNR()
>>> img1 = Tensor([[[[1, 2, 3, 4], [1, 2, 3, 4]]]])
>>> img2 = Tensor([[[[3, 4, 5, 6], [3, 4, 5, 6]]]])
>>> output = net(img1, img2)
>>> print(output)
[-6.0206]
class tinyms.layers.CentralCrop(central_fraction)[源代码]

Crops the central region of the images with the central_fraction.

参数

central_fraction (float) – Fraction of size to crop. It must be float and in range (0.0, 1.0].

Inputs:
  • image (Tensor) - A 3-D tensor of shape [C, H, W], or a 4-D tensor of shape [N, C, H, W].

Outputs:

Tensor, 3-D or 4-D float tensor, according to the input.

引发
  • TypeError – If central_fraction is not a float.

  • ValueError – If central_fraction is not in range (0, 1.0].

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.CentralCrop(central_fraction=0.5)
>>> image = Tensor(np.random.random((4, 3, 4, 4)), mindspore.float32)
>>> output = net(image)
>>> print(output.shape)
(4, 3, 2, 2)
class tinyms.layers.FakeQuantWithMinMaxObserver(min_init=-6, max_init=6, ema=False, ema_decay=0.999, per_channel=False, channel_axis=1, num_channels=1, quant_dtype=<QuantDtype.INT8: 'INT8'>, symmetric=False, narrow_range=False, quant_delay=0, neg_trunc=False, mode='DEFAULT')[源代码]

Quantization aware operation which provides the fake quantization observer function on data with min and max.

The detail of the quantization mode DEFAULT is described as below:

The running min/max \(x_{min}\) and \(x_{max}\) are computed as:

\[\begin{split}\begin{array}{ll} \\ x_{min} = \begin{cases} \min(\min(X), 0) & \text{ if } ema = \text{False} \\ \min((1 - c) \min(X) + \text{c } x_{min}, 0) & \text{ if } \text{otherwise} \end{cases}\\ x_{max} = \begin{cases} \max(\max(X), 0) & \text{ if } ema = \text{False} \\ \max((1 - c) \max(X) + \text{c } x_{max}, 0) & \text{ if } \text{otherwise} \end{cases} \end{array}\end{split}\]

where X is the input tensor, and \(c\) is the ema_decay.

The scale and zero point zp is computed as:

\[\begin{split}\begin{array}{ll} \\ scale = \begin{cases} \frac{x_{max} - x_{min}}{Q_{max} - Q_{min}} & \text{ if } symmetric = \text{False} \\ \frac{2\max(x_{max}, \left | x_{min} \right |) }{Q_{max} - Q_{min}} & \text{ if } \text{otherwise} \end{cases}\\ zp\_min = Q_{min} - \frac{x_{min}}{scale} \\ zp = \left \lfloor \min(Q_{max}, \max(Q_{min}, zp\_min)) + 0.5 \right \rfloor \end{array}\end{split}\]

where \(Q_{max}\) and \(Q_{min}\) is decided by quant_dtype, for example, if quant_dtype=INT8, then \(Q_{max} = 127\) and \(Q_{min} = -128\).

The fake quant output is computed as:

\[\begin{split}\begin{array}{ll} \\ u_{min} = (Q_{min} - zp) * scale \\ u_{max} = (Q_{max} - zp) * scale \\ u_X = \left \lfloor \frac{\min(u_{max}, \max(u_{min}, X)) - u_{min}}{scale} + 0.5 \right \rfloor \\ output = u_X * scale + u_{min} \end{array}\end{split}\]

The detail of the quantization mode LEARNED_SCALE is described as below:

The fake quant output is computed as:

\[ \begin{align}\begin{aligned}\begin{split}\bar{X}=\left\{\begin{matrix} clip\left ( \frac{X}{maxq},0,1\right ) \qquad \quad if\quad neg\_trunc\\ clip\left ( \frac{X}{maxq},-1,1\right )\qquad \ if\quad otherwise \end{matrix}\right. \\\end{split}\\output=\frac{floor\left ( \bar{X}\ast Q_{max}+0.5 \right ) \ast scale }{Q_{max}}\end{aligned}\end{align} \]

where X is the input tensor. where \(Q_{max}\) (quant_max) is decided by quant_dtype and neg_trunc, for example, if quant_dtype=INT8 and neg_trunc works, \(Q_{max} = 256\) , otherwise math:Q_{max} = 127.

The maxq is updated by training, and its gradient is calculated as follows:

\[ \begin{align}\begin{aligned}\begin{split}\frac{\partial \ output}{\partial \ maxq} = \left\{\begin{matrix} -\frac{X}{maxq}+\left \lfloor \frac{X}{maxq} \right \rceil \qquad if\quad bound_{lower}< \frac{X}{maxq}< 1\\ -1 \qquad \quad \qquad \quad if\quad \frac{X}{maxq}\le bound_{lower}\\ 1 \qquad \quad \qquad \quad if\quad \frac{X}{maxq}\ge 1 \qquad \quad \end{matrix}\right. \\\end{split}\\\begin{split}bound_{lower}= \left\{\begin{matrix} 0\qquad \quad if\quad neg\_trunc\\ -1\qquad if\quad otherwise \end{matrix}\right.\end{split}\end{aligned}\end{align} \]

Then minq is computed as:

\[\begin{split}minq=\left\{\begin{matrix} 0 \qquad \qquad \quad if\quad neg\_trunc\\ -maxq\qquad if\quad otherwise \end{matrix}\right.\end{split}\]

When exporting, the scale and zero point zp is computed as:

\[\begin{split}scale=\frac{maxq}{quant\_max} ,\quad zp=0 \\\end{split}\]

zp is equal to 0 consistently, due to the LEARNED_SCALE`s symmetric nature.

参数
  • min_init (int, float, list) – The initialized min value. Default: -6.

  • max_init (int, float, list) – The initialized max value. Default: 6.

  • ema (bool) – The exponential Moving Average algorithm updates min and max. Default: False.

  • ema_decay (float) – Exponential Moving Average algorithm parameter. Default: 0.999.

  • per_channel (bool) – Quantization granularity based on layer or on channel. Default: False.

  • channel_axis (int) – Quantization by channel axis. Default: 1.

  • num_channels (int) – declarate the min and max channel size, Default: 1.

  • quant_dtype (QuantDtype) – The datatype of quantization, supporting 4 and 8bits. Default: QuantDtype.INT8.

  • symmetric (bool) – Whether the quantization algorithm is symmetric or not. Default: False.

  • narrow_range (bool) – Whether the quantization algorithm uses narrow range or not. Default: False.

  • quant_delay (int) – Quantization delay parameters according to the global step. Default: 0.

  • neg_trunc (bool) – Whether the quantization algorithm uses negative truncation or not. Default: False.

  • mode (str) – Optional quantization mode, currently only DEFAULT`(QAT) and `LEARNED_SCALE are supported. Default: (“DEFAULT”)

Inputs:
  • x (Tensor) - The input of FakeQuantWithMinMaxObserver. The input dimension is preferably 2D or 4D.

Outputs:

Tensor, with the same type and shape as the x.

引发
  • TypeError – If min_init or max_init is not int, float or list.

  • TypeError – If quant_delay is not an int.

  • ValueError – If quant_delay is less than 0.

  • ValueError – If min_init is not less than max_init.

  • ValueError – If mode is neither DEFAULT nor LEARNED_SCALE.

  • ValueError – If mode is LEARNED_SCALE and symmetric is not True.

  • ValueError – If mode is LEARNED_SCALE, and narrow_range is not True unless when neg_trunc is True.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore import Tensor
>>> fake_quant = nn.FakeQuantWithMinMaxObserver()
>>> x = Tensor(np.array([[1, 2, 1], [-2, 0, -1]]), mindspore.float32)
>>> result = fake_quant(x)
>>> print(result)
[[ 0.9882355  1.9764705  0.9882355]
 [-1.9764705  0.        -0.9882355]]
extend_repr()[源代码]

Display instance object as string.

reset(quant_dtype=<QuantDtype.INT8: 'INT8'>, min_init=-6, max_init=6)[源代码]

Reset the quant max parameter (eg. 256) and the initial value of the minq parameter and maxq parameter, this function is currently only valid for LEARNED_SCALE mode.

class tinyms.layers.Conv2dBnFoldQuantOneConv(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, eps=1e-05, momentum=0.997, has_bias=False, weight_init='normal', bias_init='zeros', beta_init='zeros', gamma_init='ones', mean_init='zeros', var_init='ones', fake=True, quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>)[源代码]

2D convolution which use the convolution layer statistics once to calculate Batch Normalization operation folded construct.

This part is a more detailed overview of Conv2d operation. For more details about Quantization, please refer to the implementation of class of FakeQuantWithMinMaxObserver, FakeQuantWithMinMaxObserver.

\[ \begin{align}\begin{aligned}w_{q}=quant(\frac{w}{\sqrt{var_{G}+\epsilon}}*\gamma )\\b=\frac{-\mu _{G} }{\sqrt{var_{G}+\epsilon }}*\gamma +\beta\\y=w_{q}\times x+b\end{aligned}\end{align} \]

where \(quant\) is the continuous execution of quant and dequant, you can refer to the implementation of subclass of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver. mu _{G} and var_{G} represent the global mean and variance respectively.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – Specifies the height and width of the 2D convolution window.

  • stride (Union[int, tuple[int]]) – Specifies stride for all spatial dimensions with the same value. Default: 1.

  • pad_mode (str) – Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

  • padding (Union[int, tuple[int]]) – Implicit paddings on both sides of the x. Default: 0.

  • dilation (Union[int, tuple[int]]) – Specifies the dilation rate to use for dilated convolution. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1.

  • eps (float) – Parameters for Batch Normalization. Default: 1e-5.

  • momentum (float) – Parameters for Batch Normalization op. Default: 0.997.

  • has_bias (bool) – Specifies whether the layer uses a bias vector, which is temporarily invalid. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Default: ‘zeros’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta vector. Default: ‘zeros’.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma vector. Default: ‘ones’.

  • mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the mean vector. Default: ‘zeros’.

  • var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the variance vector. Default: ‘ones’.

  • fake (bool) – Whether Conv2dBnFoldQuant Cell adds FakeQuantWithMinMaxObserver. Default: True.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding or dilation is neither an int nor a tuple.

  • TypeError – If has_bias or fake is not a bool.

  • TypeError – If data_format is not a string.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> conv2d_bnfold = nn.Conv2dBnFoldQuantOneConv(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
...                                             weight_init="ones", quant_config=qconfig)
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
>>> result = conv2d_bnfold(x)
>>> print(result)
[[[[5.9296875 13.8359375]
   [11.859375 17.78125]]]]
extend_repr()[源代码]

Display instance object as string.

class tinyms.layers.Conv2dBnFoldQuant(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, eps=1e-05, momentum=0.997, has_bias=False, weight_init='normal', bias_init='zeros', beta_init='zeros', gamma_init='ones', mean_init='zeros', var_init='ones', fake=True, quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>, freeze_bn=100000)[源代码]

2D convolution with Batch Normalization operation folded construct.

This part is a more detailed overview of Conv2d operation. For more details about Quantization, please refer to the implementation of class of FakeQuantWithMinMaxObserver, FakeQuantWithMinMaxObserver.

\[ \begin{align}\begin{aligned}y = x\times w+ b\\w_{q}=quant(\frac{w}{\sqrt{Var[y]+\epsilon}}*\gamma )\\y_{out}= w_{q}\times x+\frac{b-E[y]}{\sqrt{Var[y]+\epsilon}}*\gamma +\beta\end{aligned}\end{align} \]

where \(quant\) is the continuous execution of quant and dequant. Two convolution and Batch Normalization operation are used here, the purpose of the first convolution and Batch Normalization is to count the mean E[y] and variance Var[y] of current batch output for quantization.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – Specifies the height and width of the 2D convolution window.

  • stride (Union[int, tuple[int]]) – Specifies stride for all spatial dimensions with the same value. Default: 1.

  • pad_mode (str) – Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

  • padding (Union[int, tuple[int]]) – Implicit paddings on both sides of the x. Default: 0.

  • dilation (Union[int, tuple[int]]) – Specifies the dilation rate to use for dilated convolution. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1.

  • eps (float) – Parameters for Batch Normalization. Default: 1e-5.

  • momentum (float) – Parameters for Batch Normalization op. Default: 0.997.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Default: ‘zeros’.

  • beta_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta vector. Default: ‘zeros’.

  • gamma_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma vector. Default: ‘ones’.

  • mean_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the mean vector. Default: ‘zeros’.

  • var_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the variance vector. Default: ‘ones’.

  • fake (bool) – Whether Conv2dBnFoldQuant Cell adds FakeQuantWithMinMaxObserver. Default: True.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

  • freeze_bn (int) – The quantization freeze Batch Normalization op is according to the global step. Default: 100000.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding or dilation is neither an int nor a tuple.

  • TypeError – If has_bias or fake is not a bool.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

  • ValueError – If device_target in context is neither Ascend nor GPU.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> conv2d_bnfold = nn.Conv2dBnFoldQuant(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
...                                      weight_init="ones", quant_config=qconfig)
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
>>> result = conv2d_bnfold(x)
>>> print(result)
[[[[5.9296875 13.8359375]
   [11.859375 17.78125]]]]
extend_repr()[源代码]

Display instance object as string.

class tinyms.layers.Conv2dBnWithoutFoldQuant(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, eps=1e-05, momentum=0.997, weight_init='normal', bias_init='zeros', quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>)[源代码]

2D convolution and batchnorm without fold with fake quantized construct.

This part is a more detailed overview of Conv2d operation. For more details about Quantization, please refer to the implementation of class of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver.

\[ \begin{align}\begin{aligned}y =x\times quant(w)+ b\\y_{bn} =\frac{y-E[y] }{\sqrt{Var[y]+ \epsilon } } *\gamma + \beta\end{aligned}\end{align} \]

where \(quant\) is the continuous execution of quant and dequant, you can refer to the implementation of class of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – Specifies the height and width of the 2D convolution window.

  • stride (Union[int, tuple[int]]) – Specifies stride for all spatial dimensions with the same value. Default: 1.

  • pad_mode (str) – Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

  • padding (Union[int, tuple[int]]) – Implicit paddings on both sides of the x. Default: 0.

  • dilation (Union[int, tuple[int]]) – Specifies the dilation rate to use for dilated convolution. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • eps (float) – Parameters for Batch Normalization. Default: 1e-5.

  • momentum (float) – Parameters for Batch Normalization op. Default: 0.997.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Default: ‘zeros’.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

Supported Platforms:

Ascend GPU

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding or dilation is neither an int nor a tuple.

  • TypeError – If has_bias is not a bool.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> conv2d_no_bnfold = nn.Conv2dBnWithoutFoldQuant(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
...                                                weight_init='ones', quant_config=qconfig)
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
>>> result = conv2d_no_bnfold(x)
>>> print(result)
[[[[5.929658  13.835868]
   [11.859316  17.78116]]]]
extend_repr()[源代码]

Display instance object as string.

class tinyms.layers.Conv2dQuant(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros', quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>)[源代码]

2D convolution with fake quantized operation layer.

This part is a more detailed overview of Conv2d operation. For more details about Quantization, please refer to the implementation of class of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – Specifies the height and width of the 2D convolution window.

  • stride (Union[int, tuple[int]]) – Specifies stride for all spatial dimensions with the same value. Default: 1.

  • pad_mode (str) – Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

  • padding (Union[int, tuple[int]]) – Implicit paddings on both sides of the x. Default: 0.

  • dilation (Union[int, tuple[int]]) – Specifies the dilation rate to use for dilated convolution. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Default: ‘zeros’.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\). The input dimension is preferably 2D or 4D.

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels or group is not an int.

  • TypeError – If kernel_size, stride, padding or dilation is neither an int nor a tuple.

  • TypeError – If has_bias is not a bool.

  • ValueError – If in_channels, out_channels, kernel_size, stride or dilation is less than 1.

  • ValueError – If padding is less than 0.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> conv2d_quant = nn.Conv2dQuant(1, 1, kernel_size=(2, 2), stride=(1, 1), pad_mode="valid",
...                               weight_init='ones', quant_config=qconfig)
>>> x = Tensor(np.array([[[[1, 0, 3], [1, 4, 7], [2, 5, 2]]]]), mindspore.float32)
>>> result = conv2d_quant(x)
>>> print(result)
[[[[5.9296875  13.8359375]
   [11.859375  17.78125]]]]
extend_repr()[源代码]

Display instance object as string.

class tinyms.layers.DenseQuant(in_channels, out_channels, weight_init='normal', bias_init='zeros', has_bias=True, activation=None, quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>)[源代码]

The fully connected layer with fake quantized operation.

This part is a more detailed overview of Dense operation. For more details about Quantization, please refer to the implementation of class of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver.

参数
  • in_channels (int) – The dimension of the input space.

  • out_channels (int) – The dimension of the output space.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable weight_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable bias_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘zeros’.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: True.

  • activation (Union[str, Cell, Primitive]) – The regularization function applied to the output of the layer, eg. ‘relu’. Default: None.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\). The input dimension is preferably 2D or 4D.

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

引发
  • TypeError – If in_channels, out_channels is not an int.

  • TypeError – If has_bias is not a bool.

  • TypeError – If activation is not str, Cell and Primitive.

  • ValueError – If in_channels or out_channels is less than 1.

  • ValueError – If the dims of weight_init is not equal to 2 or the first element of weight_init is not equal to out_channels or the second element of weight_init is not equal to in_channels.

  • ValueError – If the dims of bias_init is not equal to 1 or the element of bias_init is not equal to out_channels.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> dense_quant = nn.DenseQuant(2, 1, weight_init='ones', quant_config=qconfig)
>>> x = Tensor(np.array([[1, 5], [3, 4]]), mindspore.float32)
>>> result = dense_quant(x)
>>> print(result)
[[5.929413]
 [6.9176483]]
construct(x)[源代码]

Use operators to construct the Dense layer.

extend_repr()[源代码]

A pretty print for Dense layer.

class tinyms.layers.ActQuant(activation, ema=False, ema_decay=0.999, fake_before=False, quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>)[源代码]

Quantization aware training activation function.

Add the fake quantized operation to the end of activation operation, by which the output of activation operation will be truncated. For more details about Quantization, please refer to the implementation of subclass of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver.

参数
  • activation (Cell) – Activation cell.

  • ema (bool) – The exponential Moving Average algorithm updates min and max. Default: False.

  • ema_decay (float) – Exponential Moving Average algorithm parameter. Default: 0.999.

  • fake_before (bool) – Whether add fake quantized operation before activation. Default: False.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • x (Tensor) - The input of ActQuant. The input dimension is preferably 2D or 4D.

Outputs:

Tensor, with the same type and shape as the x.

引发
  • TypeError – If activation is not an instance of Cell.

  • TypeError – If fake_before is not a bool.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> act_quant = nn.ActQuant(nn.ReLU(), quant_config=qconfig)
>>> x = Tensor(np.array([[1, 2, -1], [-2, 0, -1]]), mindspore.float32)
>>> result = act_quant(x)
>>> print(result)
[[0.9882355 1.9764705 0.       ]
 [0.        0.        0.       ]]
class tinyms.layers.TensorAddQuant(ema_decay=0.999, quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>)[源代码]

Adds fake quantized operation after TensorAdd operation.

This part is a more detailed overview of TensorAdd operation. For more details about Quantization, please refer to the implementation of class of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver.

参数
  • ema_decay (float) – Exponential Moving Average algorithm parameter. Default: 0.999.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • x1 (Tensor) - The first tensor of TensorAddQuant. The input dimension is preferably 2D or 4D.

  • x2 (Tensor) - The second tensor of TensorAddQuant. Has the same shape with x1.

Outputs:

Tensor, with the same type and shape as the x1.

引发
  • TypeError – If ema_decay is not a float.

  • ValueError – If the shape of x2 is different with x1.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> add_quant = nn.TensorAddQuant(quant_config=qconfig)
>>> x1 = Tensor(np.array([[1, 2, 1], [-2, 0, -1]]), mindspore.float32)
>>> x2 = Tensor(np.ones((2, 3)), mindspore.float32)
>>> output = add_quant(x1, x2)
>>> print(output)
[[ 1.9764705  3.011765   1.9764705]
 [-0.9882355  0.9882355  0.       ]]
class tinyms.layers.MulQuant(ema_decay=0.999, quant_config=QuantConfig(weight=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>), activation=functools.partial(<class 'mindspore.nn.layer.quant.FakeQuantWithMinMaxObserver'>)), quant_dtype=<QuantDtype.INT8: 'INT8'>)[源代码]

Adds fake quantized operation after Mul operation.

This part is a more detailed overview of Mul operation. For more details about Quantization, please refer to the implementation of class of FakeQuantWithMinMaxObserver, mindspore.nn.FakeQuantWithMinMaxObserver.

参数
  • ema_decay (float) – Exponential Moving Average algorithm parameter. Default: 0.999.

  • quant_config (QuantConfig) – Configures the types of quant observer and quant settings of weight and activation. Note that, QuantConfig is a special namedtuple, which is designed for quantization and can be generated by mindspore.compression.quant.create_quant_config() method. Default: QuantConfig with both items set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • x1 (Tensor) - The first tensor of MulQuant. The input dimension is preferably 2D or 4D.

  • x2 (Tensor) - The second tensor of MulQuant. Has the same shape with x1.

Outputs:

Tensor, with the same type and shape as the x1.

引发
  • TypeError – If ema_decay is not a float.

  • ValueError – If the shape of x2 is different with x1.

Supported Platforms:

Ascend GPU

实际案例

>>> import mindspore
>>> from mindspore.compression import quant
>>> from mindspore import Tensor
>>> qconfig = quant.create_quant_config()
>>> mul_quant = nn.MulQuant(quant_config=qconfig)
>>> x1 = Tensor(np.array([[1, 2, 1], [-2, 0, -1]]), mindspore.float32)
>>> x2 = Tensor(np.ones((2, 3)) * 2, mindspore.float32)
>>> output = mul_quant(x1, x2)
>>> print(output)
[[ 1.9764705  4.0000005  1.9764705]
 [-4.         0.        -1.9764705]]
class tinyms.layers.ReduceLogSumExp(axis, keep_dims=False)[源代码]

Reduces a dimension of a tensor by calculating exponential for all elements in the dimension, then calculate logarithm of the sum.

\[ReduceLogSumExp(x) = \log(\sum(e^x))\]
参数
  • axis (Union[int, tuple(int), list(int)]) – (), reduce all dimensions. Only constant value is allowed.

  • keep_dims (bool) – If True, keep these reduced dimensions and the length is 1. If False, don’t keep these dimensions. Default : False.

Inputs:
  • x (Tensor) - The input tensor. With float16 or float32 data type.

Outputs:

Tensor, has the same dtype as the x.

  • If axis is (), and keep_dims is False, the output is a 0-D tensor representing the sum of all elements in the input tensor.

  • If axis is int, set as 2, and keep_dims is False, the shape of output is \((x_1, x_3, ..., x_R)\).

  • If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is \((x_1, x_4, ..., x_R)\).

引发
  • TypeError – If axis is not one of int, list, tuple.

  • TypeError – If keep_dims is not bool.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
>>> op = nn.ReduceLogSumExp(1, keep_dims=True)
>>> output = op(x)
>>> print(output.shape)
(3, 1, 5, 6)
class tinyms.layers.Range(start, limit=None, delta=1)[源代码]

Creates a sequence of numbers in range [start, limit) with step size delta.

The size of output is \(\left \lfloor \frac{limit-start}{delta} \right \rfloor + 1\) and delta is the gap between two values in the tensor.

\[out_{i+1} = out_{i} +delta\]
参数
  • start (Union[int, float]) – If limit is None, the value acts as limit in the range and first entry defaults to 0. Otherwise, it acts as first entry in the range.

  • limit (Union[int, float]) – Acts as upper limit of sequence. If None, defaults to the value of start while set the first entry of the range to 0. It can not be equal to start. Default: None.

  • delta (Union[int, float]) – Increment of the range. It can not be equal to zero. Default: 1.

Outputs:

Tensor, the dtype is int if the dtype of start, limit and delta all are int. Otherwise, dtype is float.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Range(1, 8, 2)
>>> output = net()
>>> print(output)
[1 3 5 7]
class tinyms.layers.LGamma[源代码]

Calculates LGamma using Lanczos’ approximation referring to “A Precision Approximation of the Gamma Function”. The algorithm is:

\[\begin{split}\begin{array}{ll} \\ lgamma(z + 1) = \frac{(\log(2) + \log(pi))}{2} + (z + 1/2) * log(t(z)) - t(z) + A(z) \\ t(z) = z + kLanczosGamma + 1/2 \\ A(z) = kBaseLanczosCoeff + \sum_{k=1}^n \frac{kLanczosCoefficients[i]}{z + k} \end{array}\end{split}\]

However, if the input is less than 0.5 use Euler’s reflection formula:

\[lgamma(x) = \log(pi) - lgamma(1-x) - \log(abs(sin(pi * x)))\]

And please note that

\[lgamma(+/-inf) = +inf\]

Thus, the behaviour of LGamma follows:

  • when x > 0.5, return log(Gamma(x))

  • when x < 0.5 and is not an integer, return the real part of Log(Gamma(x)) where Log is the complex logarithm

  • when x is an integer less or equal to 0, return +inf

  • when x = +/- inf, return +inf

Inputs:
  • x (Tensor) - The input tensor. Only float16, float32 are supported.

Outputs:

Tensor, has the same shape and dtype as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([2, 3, 4]).astype(np.float32))
>>> op = nn.LGamma()
>>> output = op(x)
>>> print(output)
[3.5762787e-07 6.9314754e-01 1.7917603e+00]
class tinyms.layers.DiGamma[源代码]

Calculates Digamma using Lanczos’ approximation referring to “A Precision Approximation of the Gamma Function”. The algorithm is:

\[\begin{split}\begin{array}{ll} \\ digamma(z + 1) = log(t(z)) + A'(z) / A(z) - kLanczosGamma / t(z) \\ t(z) = z + kLanczosGamma + 1/2 \\ A(z) = kBaseLanczosCoeff + \sum_{k=1}^n \frac{kLanczosCoefficients[i]}{z + k} \\ A'(z) = \sum_{k=1}^n \frac{kLanczosCoefficients[i]}{{z + k}^2} \end{array}\end{split}\]

However, if the input is less than 0.5 use Euler’s reflection formula:

\[digamma(x) = digamma(1 - x) - pi * cot(pi * x)\]
Inputs:
  • x (Tensor[Number]) - The input tensor. Only float16, float32 are supported.

Outputs:

Tensor, has the same shape and dtype as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([2, 3, 4]).astype(np.float32))
>>> op = nn.DiGamma()
>>> output = op(x)
>>> print(output)
[0.42278463  0.92278427 1.2561178]
class tinyms.layers.IGamma[源代码]

Calculates lower regularized incomplete Gamma function. The lower regularized incomplete Gamma function is defined as:

\[P(a, x) = gamma(a, x) / Gamma(a) = 1 - Q(a, x)\]

where

\[gamma(a, x) = \int_0^x t^{a-1} \exp^{-t} dt\]

is the lower incomplete Gamma function.

Above \(Q(a, x)\) is the upper regularized complete Gamma function.

Inputs:
  • a (Tensor) - The input tensor. With float32 data type. a should have the same dtype with x.

  • x (Tensor) - The input tensor. With float32 data type. x should have the same dtype with a.

Outputs:

Tensor, has the same dtype as a and x.

引发

TypeError – If dtype of input x and a is not float16 nor float32, or if x has different dtype with a.

Supported Platforms:

Ascend GPU

实际案例

>>> a = Tensor(np.array([2.0, 4.0, 6.0, 8.0]).astype(np.float32))
>>> x = Tensor(np.array([2.0, 3.0, 4.0, 5.0]).astype(np.float32))
>>> igamma = nn.IGamma()
>>> output = igamma(a, x)
>>> print (output)
[0.593994  0.35276785  0.21486944  0.13337152]
class tinyms.layers.LBeta[源代码]

This method avoids the numeric cancellation by explicitly decomposing lgamma into the Stirling approximation and an explicit log_gamma_correction, and cancelling the large terms from the Striling analytically.

This is semantically equal to

\[P(x, y) = lgamma(x) + lgamma(y) - lgamma(x + y).\]

The method is more accurate for arguments above 8. The reason for accuracy loss in the naive computation is catastrophic cancellation between the lgammas.

Inputs:
  • x (Tensor) - The input tensor. With float16 or float32 data type. x should have the same dtype with y.

  • y (Tensor) - The input tensor. With float16 or float32 data type. y should have the same dtype with x.

Outputs:

Tensor, has the same dtype as x and y.

引发

TypeError – If dtype of x or y is neither float16 nor float32, or if x has different dtype with y.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([2.0, 4.0, 6.0, 8.0]).astype(np.float32))
>>> y = Tensor(np.array([2.0, 3.0, 14.0, 15.0]).astype(np.float32))
>>> lbeta = nn.LBeta()
>>> output = lbeta(y, x)
>>> print(output)
[-1.7917596  -4.094345  -12.000229  -14.754799]
class tinyms.layers.MatMul(**kwargs)[源代码]

Multiplies matrix x1 by matrix x2.

nn.MatMul will be deprecated in future versions. Please use ops.matmul instead.

  • If both x1 and x2 are 1-dimensional, the dot product is returned.

  • If the dimensions of x1 and x2 are all not greater than 2, the matrix-matrix product will be returned. Note if one of ‘x1’ and ‘x2’ is 1-dimensional, the argument will first be expanded to 2 dimension. After the matrix multiply, the expanded dimension will be removed.

  • If at least one of x1 and x2 is N-dimensional (N>2), the none-matrix dimensions(batch) of inputs will be broadcasted and must be broadcastable. Note if one of ‘x1’ and ‘x2’ is 1-dimensional, the argument will first be expanded to 2 dimension and then the none-matrix dimensions will be broadcasted. after the matrix multiply, the expanded dimension will be removed. For example, if x1 is a \((j \times 1 \times n \times m)\) tensor and x2 is b \((k \times m \times p)\) tensor, the output will be a \((j \times k \times n \times p)\) tensor.

参数
  • transpose_x1 (bool) – If true, a is transposed before multiplication. Default: False.

  • transpose_x2 (bool) – If true, b is transposed before multiplication. Default: False.

Inputs:
  • x1 (Tensor) - The first tensor to be multiplied.

  • x2 (Tensor) - The second tensor to be multiplied.

Outputs:

Tensor, the shape of the output tensor depends on the dimension of input tensors.

引发
  • TypeError – If transpose_x1 or transpose_x2 is not a bool.

  • ValueError – If the column of matrix dimensions of x1 is not equal to the row of matrix dimensions of x2.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.MatMul()
>>> x1 = Tensor(np.ones(shape=[3, 2, 3]), mindspore.float32)
>>> x2 = Tensor(np.ones(shape=[3, 4]), mindspore.float32)
>>> output = net(x1, x2)
>>> print(output.shape)
(3, 2, 4)
class tinyms.layers.Moments(axis=None, keep_dims=None)[源代码]

Calculates the mean and variance of x.

The mean and variance are calculated by aggregating the contents of input_x across axes. If input_x is 1-D and axes = [0] this is just the mean and variance of a vector.

参数
  • axis (Union[int, tuple(int)]) – Calculates the mean and variance along the specified axis. Default: None.

  • keep_dims (bool) – If true, The dimension of mean and variance are identical with input’s. If false, don’t keep these dimensions. Default: None.

Inputs:
  • x (Tensor) - The tensor to be calculated. Only float16 and float32 are supported. \((N,*)\) where \(*\) means,any number of additional dimensions.

Outputs:
  • mean (Tensor) - The mean of x, with the same date type as input x.

  • variance (Tensor) - The variance of x, with the same date type as input x.

引发
  • TypeError – If axis is not one of int, tuple, None.

  • TypeError – If keep_dims is neither bool nor None.

  • TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> x = Tensor(np.array([[[[1, 2, 3, 4], [3, 4, 5, 6]]]]), mindspore.float32)
>>> net = nn.Moments(axis=0, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00],
   [ 3.00000000e+00, 4.00000000e+00, 5.00000000e+00, 6.00000000e+00]]]]),
Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
   [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]]]))
>>> net = nn.Moments(axis=1, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00, 4.00000000e+00],
   [ 3.00000000e+00, 4.00000000e+00, 5.00000000e+00, 6.00000000e+00]]]]),
Tensor(shape=[1, 1, 2, 4], dtype=Float32, value=
[[[[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
   [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]]]))
>>> net = nn.Moments(axis=2, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 1, 4], dtype=Float32, value=
[[[[ 2.00000000e+00, 3.00000000e+00, 4.00000000e+00, 5.00000000e+00]]]]),
Tensor(shape=[1, 1, 1, 4], dtype=Float32, value=
[[[[ 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]]]))
>>> net = nn.Moments(axis=3, keep_dims=True)
>>> output = net(x)
>>> print(output)
(Tensor(shape=[1, 1, 2, 1], dtype=Float32, value=
[[[[ 2.50000000e+00],
   [ 4.50000000e+00]]]]), Tensor(shape=[1, 1, 2, 1], dtype=Float32, value=
[[[[ 1.25000000e+00],
   [ 1.25000000e+00]]]]))
class tinyms.layers.MatInverse[源代码]

Calculates the inverse of Positive-Definite Hermitian matrix using Cholesky decomposition.

Inputs:
  • x (Tensor[Number]) - The input tensor. It must be a positive-definite matrix. With float16 or float32 data type.

Outputs:

Tensor, has the same dtype as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

GPU

实际案例

>>> x = Tensor(np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]]).astype(np.float32))
>>> op = nn.MatInverse()
>>> output = op(x)
>>> print(output)
[[49.36112  -13.555558  2.1111116]
 [-13.555558  3.7777784  -0.5555557]
 [2.1111116  -0.5555557  0.11111113]]
class tinyms.layers.MatDet[源代码]

Calculates the determinant of Positive-Definite Hermitian matrix using Cholesky decomposition.

Inputs:
  • x (Tensor[Number]) - The input tensor. It must be a positive-definite matrix. With float16 or float32 data type.

Outputs:

Tensor, has the same dtype as the x.

引发

TypeError – If dtype of x is neither float16 nor float32.

Supported Platforms:

GPU

实际案例

>>> x = Tensor(np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]]).astype(np.float32))
>>> op = nn.MatDet()
>>> output = op(x)
>>> print(output)
35.999996
class tinyms.layers.Conv2dBnAct(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros', has_bn=False, momentum=0.997, eps=1e-05, activation=None, alpha=0.2, after_fake=True)[源代码]

A combination of convolution, Batchnorm, and activation layer.

This part is a more detailed overview of Conv2d operation.

参数
  • in_channels (int) – The number of input channel \(C_{in}\).

  • out_channels (int) – The number of output channel \(C_{out}\).

  • kernel_size (Union[int, tuple]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means the value is for both height and width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.

  • stride (int) – Specifies stride for all spatial dimensions with the same value. The value of stride must be greater than or equal to 1 and lower than any one of the height and width of the x. Default: 1.

  • pad_mode (str) – Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

  • padding (int) – Implicit paddings on both sides of the x. Default: 0.

  • dilation (int) – Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater than or equal to 1 and lower than any one of the height and width of the x. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

  • has_bn (bool) – Specifies to used batchnorm or not. Default: False.

  • momentum (float) – Momentum for moving average for batchnorm, must be [0, 1]. Default:0.997

  • eps (float) – Term added to the denominator to improve numerical stability for batchnorm, should be greater than 0. Default: 1e-5.

  • activation (Union[str, Cell, Primitive]) – Specifies activation type. The optional values are as following: ‘softmax’, ‘logsoftmax’, ‘relu’, ‘relu6’, ‘tanh’, ‘gelu’, ‘sigmoid’, ‘prelu’, ‘leakyrelu’, ‘hswish’, ‘hsigmoid’. Default: None.

  • alpha (float) – Slope of the activation function at x < 0 for LeakyReLU. Default: 0.2.

  • after_fake (bool) – Determine whether there must be a fake quantization operation after Cond2dBnAct. Default: True.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\). The data type is float32.

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\). The data type is float32.

引发
  • TypeError – If in_channels, out_channels, stride, padding or dilation is not an int.

  • TypeError – If has_bias is not a bool.

  • ValueError – If in_channels or out_channels stride, padding or dilation is less than 1.

  • ValueError – If pad_mode is not one of ‘same’, ‘valid’, ‘pad’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.Conv2dBnAct(120, 240, 4, has_bn=True, activation='relu')
>>> x = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float32)
>>> result = net(x)
>>> output = result.shape
>>> print(output)
(1, 240, 1024, 640)
class tinyms.layers.DenseBnAct(in_channels, out_channels, weight_init='normal', bias_init='zeros', has_bias=True, has_bn=False, momentum=0.9, eps=1e-05, activation=None, alpha=0.2, after_fake=True)[源代码]

A combination of Dense, Batchnorm, and the activation layer.

This part is a more detailed overview of Dense op.

参数
  • in_channels (int) – The number of channels in the input space.

  • out_channels (int) – The number of channels in the output space.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable weight_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable bias_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘zeros’.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: True.

  • has_bn (bool) – Specifies to use batchnorm or not. Default: False.

  • momentum (float) – Momentum for moving average for batchnorm, must be [0, 1]. Default:0.9

  • eps (float) – Term added to the denominator to improve numerical stability for batchnorm, should be greater than 0. Default: 1e-5.

  • activation (Union[str, Cell, Primitive]) – Specifies activation type. The optional values are as following: ‘softmax’, ‘logsoftmax’, ‘relu’, ‘relu6’, ‘tanh’, ‘gelu’, ‘sigmoid’, ‘prelu’, ‘leakyrelu’, ‘hswish’, ‘hsigmoid’. Default: None.

  • alpha (float) – Slope of the activation function at x < 0 for LeakyReLU. Default: 0.2.

  • after_fake (bool) – Determine whether there must be a fake quantization operation after DenseBnAct. Default: True.

Inputs:
  • x (Tensor) - Tensor of shape \((N, in\_channels)\). The data type is float32.

Outputs:

Tensor of shape \((N, out\_channels)\). The data type is float32.

引发
  • TypeError – If in_channels or out_channels is not an int.

  • TypeError – If has_bias, has_bn or after_fake is not a bool.

  • TypeError – If momentum or eps is not a float.

  • ValueError – If momentum is not in range [0, 1.0].

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = nn.DenseBnAct(3, 4)
>>> x = Tensor(np.random.randint(0, 255, [2, 3]), mindspore.float32)
>>> result = net(x)
>>> output = result.shape
>>> print(output)
(2, 4)
class tinyms.layers.TimeDistributed(layer, time_axis, reshape_with_axis=None)[源代码]

The time distributed layer.

Time distributed is a wrapper which allows to apply a layer to every temporal slice of an input. And the x should be at least 3D. There are two cases in the implementation. When reshape_with_axis provided, the reshape method will be chosen, which is more efficient; otherwise, the method of dividing the inputs along time axis will be used, which is more general. For example, reshape_with_axis could not be provided when deal with Batch Normalization.

参数
  • layer (Union[Cell, Primitive]) – The Cell or Primitive which will be wrapped.

  • time_axis (int) – The axis of time_step.

  • reshape_with_axis (int) – The axis which will be reshaped with time_axis. Default: None.

Inputs:
  • x (Tensor) - Tensor of shape \((N, T, *)\), where \(*\) means any number of additional dimensions.

Outputs:

Tensor of shape \((N, T, *)\)

Supported Platforms:

Ascend GPU CPU

引发

TypeError – If layer is not a Cell or Primitive.

实际案例

>>> x = Tensor(np.random.random([32, 10, 3]), mindspore.float32)
>>> dense = nn.Dense(3, 6)
>>> net = nn.TimeDistributed(dense, time_axis=1, reshape_with_axis=0)
>>> output = net(x)
>>> print(output.shape)
(32, 10, 6)
class tinyms.layers.DenseThor(in_channels, out_channels, weight_init='normal', bias_init='zeros', has_bias=True, activation=None)[源代码]

The dense connected layer and saving the information needed for THOR.

Applies dense connected layer for the input and saves the information A and G in the dense connected layer needed for THOR, the detail can be seen in paper: https://www.aaai.org/AAAI21Papers/AAAI-6611.ChenM.pdf This layer implements the operation as:

\[\text{outputs} = \text{activation}(\text{inputs} * \text{kernel} + \text{bias}),\]

where \(\text{activation}\) is the activation function , \(\text{kernel}\) is a weight matrix with the same data type as the inputs created by the layer, and \(\text{bias}\) is a bias vector with the same data type as the inputs created by the layer (only if has_bias is True).

参数
  • in_channels (int) – The number of the input channels.

  • out_channels (int) – The number of the output channels.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable weight_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – The trainable bias_init parameter. The dtype is same as x. The values of str refer to the function initializer. Default: ‘zeros’.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: True.

  • activation (str) – activate function applied to the output of the fully connected layer, eg. ‘ReLU’. Default: None.

Inputs:
  • x (Tensor) - Tensor of shape \((N, in\_channels)\).

Outputs:

Tensor of shape \((N, out\_channels)\).

引发

ValueError – If the shape of weight_init or bias_init is incorrect.

Supported Platforms:

Ascend GPU

实际案例

>>> x = Tensor(np.array([[1, 2, 3], [3, 4, 5]]), mindspore.float32)
>>> net = nn.DenseThor(3, 4, weight_init="ones")
>>> output = net(x)
>>> print(output)
[[  6.  6.  6.  6.]
 [ 12. 12. 12. 12. ]]
save_gradient(dout)[源代码]

this function only for thor optimizer save_gradient

class tinyms.layers.Conv2dThor(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros')[源代码]

2D convolution layer and saving the information needed for THOR.

Applies a 2D convolution over an input tensor which is typically of shape \((N, C_{in}, H_{in}, W_{in})\), where \(N\) is batch size, \(C_{in}\) is channel number, and \(H_{in}, W_{in})\) are height and width. And saves the information A and G in the 2D convolution layer needed for THOR. The detail can be seen in paper: https://www.aaai.org/AAAI21Papers/AAAI-6611.ChenM.pdf

For each batch of shape \((C_{in}, H_{in}, W_{in})\), the formula is defined as:

\[out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,\]

where \(ccor\) is the cross-correlation operator, \(C_{in}\) is the input channel number, \(j\) ranges from \(0\) to \(C_{out} - 1\), \(W_{ij}\) corresponds to the \(i\)-th channel of the \(j\)-th filter and \(out_{j}\) corresponds to the \(j\)-th channel of the output. \(W_{ij}\) is a slice of kernel and it has shape \((\text{ks_h}, \text{ks_w})\), where \(\text{ks_h}\) and \(\text{ks_w}\) are the height and width of the convolution kernel. The full kernel has shape \((C_{out}, C_{in} // \text{group}, \text{ks_h}, \text{ks_w})\), where group is the group number to split the input x in the channel dimension.

If the ‘pad_mode’ is set to be “valid”, the output height and width will be \(\left \lfloor{1 + \frac{H_{in} + 2 \times \text{padding} - \text{ks_h} - (\text{ks_h} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{W_{in} + 2 \times \text{padding} - \text{ks_w} - (\text{ks_w} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) respectively.

注解

For Ascend, the type of inputs should be subclass of Tensor[Float16], Tensor[Int8]. For GPU, the type of inputs should be subclass of Tensor[Float32].

参数
  • in_channels (int) – The number of the input channel \(C_{in}\).

  • out_channels (int) – The number of the output channel \(C_{out}\).

  • kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means that the value is not only the height, but also the width of the kernel. A tuple of 2 integers means the height and the width of the kernel respectively.

  • stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number represents the height and width of movement, or a tuple of two int numbers that represent height and width of movement, respectively. Default: 1.

  • pad_mode (str) –

    Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “same”.

    • same: Adopts the way of completion. The shape of the output will be the same as the x. The total number of padding will be calculated in horizontal and vertical directions and evenly distributed to top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the bottom and the right side. If this mode is set, padding must be 0.

    • valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, padding must be 0.

    • pad: Implicit paddings on both sides of the input x. The number of padding will be padded to the input Tensor borders. padding must be greater than or equal to 0.

  • padding (Union[int, tuple[int]]) – Implicit paddings on both sides of the input x. If padding is an integer, the paddings of top, bottom, left and right are the same, equal to padding. If padding is a tuple with four integers, the paddings of top, bottom, left and right will be equal to padding[0], padding[1], padding[2], and padding[3] accordingly. Default: 0.

  • dilation (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x. Default: 1.

  • group (int) – Splits filter into groups, in_ channels and out_channels must be divisible by the number of groups. If the group is equal to in_channels and out_channels, this 2D convolution layer also can be called 2D depthwise convolution layer. Default: 1.

  • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  • weight_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializes the convolution kernel. It can be a Tensor, a string, an Initializer or a number. When a string is specified, values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as well as constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’ and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of Initializer for more details. Default: ‘normal’.

  • bias_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializes the bias vector. Possible Initializer and string are the same as ‘weight_init’. Refer to the values of Initializer for more details. Default: ‘zeros’.

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, H_{out}, W_{out})\).

Supported Platforms:

Ascend GPU

实际案例

>>> net = nn.Conv2dThor(120, 240, 4, has_bias=False, weight_init='normal')
>>> # for Ascend
>>> x = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float16)
>>> print(net(x).shape)
(1, 240, 1024, 640)
save_gradient(dout)[源代码]
class tinyms.layers.EmbeddingThor(vocab_size, embedding_size, use_one_hot=False, embedding_table='normal', dtype=mindspore.float32, padding_idx=None)[源代码]

A simple lookup table that stores embeddings of a fixed dictionary and size and saving the information needed for THOR.

This module is often used to store word embeddings and retrieve them using indices. The input to the module is a list of indices, and the output is the corresponding word embeddings. And saves the information A and G in the dense connected layer needed for THOR, the detail can be seen in paper: https://www.aaai.org/AAAI21Papers/AAAI-6611.ChenM.pdf

注解

When ‘use_one_hot’ is set to True, the type of the input x must be mindspore.int32.

参数
  • vocab_size (int) – The size of the dictionary of embeddings.

  • embedding_size (int) – The size of each embedding vector.

  • use_one_hot (bool) – Specifies whether to apply one_hot encoding form. Default: False.

  • embedding_table (Union[Tensor, str, Initializer, numbers.Number]) – Initializes the embedding_table. Refer to class initializer for the values of string when a string is specified. Default: ‘normal’.

  • dtype (mindspore.dtype) – Data type of input x. Default: mindspore.float32.

  • padding_idx (int, None) – When the padding_idx encounters index, the output embedding vector of this index will be initialized to zero. Default: None. The feature is inactivated.

Inputs:
  • x (Tensor) - Tensor of input shape \((\text{batch_size}, \text{x_length})\). The elements of the Tensor must be integer and not larger than vocab_size. Otherwise the corresponding embedding vector will be zero.

Outputs:

Tensor of output shape \((\text{batch_size}, \text{x_length}, \text{embedding_size})\).

Supported Platforms:

Ascend GPU

实际案例

>>> net = nn.EmbeddingThor(20000, 768,  True)
>>> x = Tensor(np.ones([8, 128]), mindspore.int32)
>>>
>>> # Maps the input word IDs to word embedding.
>>> output = net(x)
>>> output.shape
(8, 128, 768)
save_gradient(dout)[源代码]

this function only for thor optimizer save_gradient

class tinyms.layers.EmbeddingLookupThor(vocab_size, embedding_size, param_init='normal', target='CPU', slice_mode='batch_slice', manual_shapes=None, max_norm=None, sparse=True, vocab_cache_size=0)[源代码]

Returns a slice of the input tensor based on the specified indices and saving the information needed for THOR.

This module has the same function as EmbeddingLookup, but additionally saves the information A and G in the embeddinglookup layer needed for THOR, the detail can be seen in paper: https://www.aaai.org/AAAI21Papers/AAAI-6611.ChenM.pdf

参数
  • vocab_size (int) – The size of the dictionary of embeddings.

  • embedding_size (int) – The size of each embedding vector.

  • param_init (Union[Tensor, str, Initializer, numbers.Number]) – Initializer for the embedding_table. Refer to class initializer for the values of string when a string is specified. Default: ‘normal’.

  • target (str) – Specifies the target where the op is executed. The value must in [‘DEVICE’, ‘CPU’]. Default: ‘CPU’.

  • slice_mode (str) – The slicing way in semi_auto_parallel/auto_parallel. The value must get through nn.EmbeddingLookup. Default: nn.EmbeddingLookup.BATCH_SLICE.

  • manual_shapes (tuple) – The accompaniment array in field slice mode.

  • max_norm (Union[float, None]) – A maximum clipping value. The data type must be float16, float32 or None. Default: None

  • sparse (bool) – Using sparse mode. When ‘target’ is set to ‘CPU’, ‘sparse’ has to be true. Default: True.

  • vocab_cache_size (int) – Cache size of the dictionary of embeddings. Default: 0. It is valid only in ‘DEVICE’ target. And the moment parameter of corresponding optimizer will also be set to the cache size. In addition, it should be noted that it will cost the ‘DEVICE’ memory, so suggests setting a reasonable value to avoid insufficient memory.

Inputs:
  • input_indices (Tensor) - The shape of tensor is \((y_1, y_2, ..., y_S)\).

Outputs:

Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\).

引发
  • ValueError – If target is neither ‘CPU’ nor ‘DEVICE’.

  • ValueError – If slice_mode is not one of ‘batch_slice’ or ‘field_slice’ or ‘table_row_slice’ or ‘table_column_slice’.

  • ValueError – If sparse is False and target is ‘CPU’.

  • ValueError – If slice_mode is ‘field_slice’ and manual_shapes is None.

  • TypeError – If vocab_size or embedding_size or vocab_cache_size is not an int.

  • TypeError – If sparse is not a bool or manual_shapes is not a tuple.

  • ValueError – If vocab_size or embedding_size is less than 1.

  • ValueError – If vocab_cache_size is less than 0.

Supported Platforms:

Ascend

实际案例

>>> input_indices = Tensor(np.array([[1, 0], [3, 2]]), mindspore.int32)
>>> result = nn.EmbeddingLookup(4,2)(input_indices)
>>> print(result.shape)
(2, 2, 2)
save_gradient(dout)[源代码]

this function only for thor optimizer save_gradient

tinyms.model

class tinyms.model.Model(network)[源代码]

High-Level API for Training or Evaluation.

Model groups layers into an object with training and inference features.

参数

network (layers.Layer) – A training or testing network.

实际案例

>>> from tinyms.model import Model, lenet5
>>> form tinyms.losses import SoftmaxCrossEntropyWithLogits
>>> from tinyms.optimizers import Momentum
>>>
>>> net = lenet5(class_num=10)
>>> model = Model(net)
>>> net_loss = SoftmaxCrossEntropyWithLogits()
>>> net_opt = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> model.compile(loss_fn=net_loss, optimizer=net_opt, metrics=None)
>>> # For details about how to build the dataset, please refer to the API document on the official website.
>>> ds_train = create_custom_dataset()
>>> model.train(2, ds_train)
build(train_dataset=None, valid_dataset=None, sink_size=-1, epoch=1)[源代码]

Build computational graphs and data graphs with the sink mode.

警告

This is an experimental prototype that is subject to change and/or deletion.

注解

Pre-build process only supports GRAPH_MODE and Ascend target currently. The interface builds the computational graphs, when the interface is executed first, ‘model.train’ only performs the graphs execution. It only support dataset sink mode.

参数
  • train_dataset (Dataset) – A training dataset iterator. If train_dataset is defined, training graphs will be initialized. Default: None.

  • valid_dataset (Dataset) – An evaluating dataset iterator. If valid_dataset is defined, evaluation graphs will be initialized, and metrics in Model can not be None. Default: None.

  • sink_size (int) – Control the amount of data in each sink. Default: -1.

  • epoch (int) – Control the training epochs. Default: 1.

实际案例

>>> from mindspore import Model, nn, FixedLossScaleManager
>>>
>>> # For details about how to build the dataset, please refer to the tutorial
>>> # document on the official website.
>>> dataset = create_custom_dataset()
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> loss_scale_manager = FixedLossScaleManager()
>>> optim = nn.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.build(dataset, epoch=2)
>>> model.train(2, dataset)
compile(loss_fn=None, optimizer=None, metrics=None, eval_network=None, amp_level='O0', **kwargs)[源代码]

High-Level API for configure the train or eval network.

参数
  • loss_fn (layers.Layer) – Objective function, if loss_fn is None, the network should contain the logic of loss and grads calculation, and the logic of parallel if needed. Default: None.

  • optimizer (layers.Layer) – Optimizer for updating the weights. Default: None.

  • metrics (Union[dict, set]) – A Dictionary or a set of metrics to be evaluated by the model during training and testing. eg: {‘accuracy’, ‘recall’}. Default: None.

  • eval_network (layers.Layer) – Network for evaluation. If not defined, network and loss_fn would be wrapped as eval_network. Default: None.

  • amp_level (str) –

    Option for argument level in mindspore.amp.build_train_network, level for mixed precision training. Supports [“O0”, “O2”, “O3”, “auto”]. Default: “O0”.

    • O0: Do not change.

    • O2: Cast network to float16, keep batchnorm run in float32, using dynamic loss scale.

    • O3: Cast network to float16, with additional property ‘keep_batchnorm_fp32=False’.

    • auto: Set to level to recommended level in different devices. Set level to O2 on GPU, Set

    level to O3 Ascend. The recommended level is choose by the export experience, cannot always generalize. User should specify the level for special network.

    O2 is recommended on GPU, O3 is recommended on Ascend.

eval(valid_dataset, callbacks=None, dataset_sink_mode=True)[源代码]

Evaluation API where the iteration is controlled by python front-end.

Configure to pynative mode or CPU, the evaluating process will be performed with dataset non-sink mode.

注解

If dataset_sink_mode is True, data will be sent to device. If the device is Ascend, features of data will be transferred one by one. The limitation of data transmission per time is 256M. When dataset_sink_mode is True, the step_end method of the Callback class will be executed when the epoch_end method is called.

参数
  • valid_dataset (Dataset) – Dataset to evaluate the model.

  • callbacks (Optional[list(Callback)]) – List of callback objects which should be executed while training. Default: None.

  • dataset_sink_mode (bool) – Determines whether to pass the data through dataset channel. Default: True.

返回

Dict, the key is the metric name defined by users and the value is the metrics value for the model in the test mode.

实际案例

>>> from mindspore import Model, nn
>>>
>>> # For details about how to build the dataset, please refer to the tutorial
>>> # document on the official website.
>>> dataset = create_custom_dataset()
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=None, metrics={'acc'})
>>> acc = model.eval(dataset, dataset_sink_mode=False)
property eval_network

Get the model’s eval_network.

export(inputs, file_name, file_format='MINDIR', **kwargs)[源代码]

Export the TinyMS prediction model to a file in the specified format.

参数
  • inputs (Tensor) – Inputs of the net.

  • file_name (str) – File name of the model to be exported.

  • file_format (str) –

    MindSpore currently supports AIR, ONNX and MINDIR format for exported model. Default: MINDIR.

    • AIR: Ascend Intermediate Representation. An intermediate representation format of Ascend model.

    Recommended suffix for output file is ‘.air’.

    • ONNX: Open Neural Network eXchange. An open format built to represent machine learning models.

    Recommended suffix for output file is ‘.onnx’.

    • MINDIR: MindSpore Native Intermediate Representation for Anf. An intermediate representation format

    for MindSpore models. Recommended suffix for output file is ‘.mindir’.

  • kwargs (dict) –

    Configuration options dictionary.

    • quant_mode: The mode of quant.

    • mean: Input data mean. Default: 127.5.

    • std_dev: Input data variance. Default: 127.5.

infer_predict_layout(*predict_data)[源代码]

Generate parameter layout for the predict network in auto or semi auto parallel mode.

Data could be a single tensor or multiple tensors.

注解

Batch data should be put together in one tensor.

参数

predict_data (Tensor) – One tensor or multiple tensors of predict data.

返回

Dict, Parameter layout dictionary used for load distributed checkpoint.

引发

RuntimeError – If get_context is not GRAPH_MODE.

实际案例

>>> # This example should be run with multiple devices. Refer to the tutorial > Distributed Training on
>>> # mindspore.cn.
>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import Model, context, Tensor
>>> from mindspore.context import ParallelMode
>>> from mindspore.communication import init
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
>>> context.set_auto_parallel_context(full_batch=True, parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
>>> input_data = Tensor(np.random.randint(0, 255, [1, 1, 32, 32]), ms.float32)
>>> model = Model(Net())
>>> predict_map = model.infer_predict_layout(input_data)
infer_train_layout(train_dataset, dataset_sink_mode=True, sink_size=-1)[源代码]

Generate parameter layout for the train network in auto or semi auto parallel mode. Only dataset sink mode is supported for now.

警告

This is an experimental prototype that is subject to change and/or deletion.

注解

This is a pre-compile function. The arguments should be the same with model.train() function.

参数
  • train_dataset (Dataset) – A training dataset iterator. If there is no loss_fn, a tuple with multiple data (data1, data2, data3, …) should be returned and passed to the network. Otherwise, a tuple (data, label) should be returned. The data and label would be passed to the network and loss function respectively.

  • dataset_sink_mode (bool) – Determines whether to pass the data through dataset channel. Configure pynative mode or CPU, the training process will be performed with dataset not sink. Default: True.

  • sink_size (int) – Control the amount of data in each sink. If sink_size = -1, sink the complete dataset for each epoch. If sink_size > 0, sink sink_size data for each epoch. If dataset_sink_mode is False, set sink_size as invalid. Default: -1.

返回

Dict, Parameter layout dictionary used for load distributed checkpoint

实际案例

>>> # This example should be run with multiple devices. Refer to the tutorial > Distributed Training on
>>> # mindspore.cn.
>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import Model, context, Tensor, nn, FixedLossScaleManager
>>> from mindspore.context import ParallelMode
>>> from mindspore.communication import init
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> init()
>>> context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
>>>
>>> # For details about how to build the dataset, please refer to the tutorial
>>> # document on the official website.
>>> dataset = create_custom_dataset()
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> loss_scale_manager = FixedLossScaleManager()
>>> optim = nn.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)
>>> layout_dict = model.infer_train_layout(dataset)
load_checkpoint(ckpt_file_name, strict_load=False)[源代码]

Loads checkpoint info from a specified file.

参数
  • ckpt_file_name (str) – Checkpoint file name.

  • strict_load (bool) – Whether to strict load the parameter into net. If False, it will load parameter in the param_dict into net with the same suffix. Default: False.

返回

Dict, key is parameter name, value is a Parameter.

引发

ValueError – Checkpoint file is incorrect.

实际案例

>>> ckpt_file_name = "./checkpoint/LeNet5-1_32.ckpt"
>>> param_dict = model.load_checkpoint(ckpt_file_name)
predict(*predict_data)[源代码]

Generate output predictions for the input samples.

Data could be a single tensor, a list of tensor, or a tuple of tensor.

注解

This is a pre-compile function. The arguments should be the same with model.predict() function.

参数

predict_data (Optional[Tensor, list[Tensor], tuple[Tensor]]) – The predict data, can be a single tensor, a list of tensor, or a tuple of tensor.

返回

Tensor, array(s) of predictions.

实际案例

>>> import mindspore as ms
>>> from mindspore import Model, Tensor
>>>
>>> input_data = Tensor(np.random.randint(0, 255, [1, 1, 32, 32]), ms.float32)
>>> model = Model(Net())
>>> result = model.predict(input_data)
property predict_network

Get the model’s predict_network.

save_checkpoint(ckpt_file_name)[源代码]

Saves checkpoint info to a specified file.

参数

ckpt_file_name (str) – Checkpoint file name. If the file name already exists, it will be overwritten.

引发

TypeError – If the parameter save_obj is not layers.Layer or list type.

train(epoch, train_dataset, callbacks=None, dataset_sink_mode=True, sink_size=-1)[源代码]

Training API where the iteration is controlled by python front-end.

When setting pynative mode or CPU, the training process will be performed with dataset not sink.

注解

If dataset_sink_mode is True, data will be sent to device. If the device is Ascend, features of data will be transferred one by one. The limitation of data transmission per time is 256M. When dataset_sink_mode is True, the step_end method of the Callback class will be executed when the epoch_end method is called. If sink_size > 0, each epoch of the dataset can be traversed unlimited times until you get sink_size elements of the dataset. The next epoch continues to traverse from the end position of the previous traversal. The interface builds the computational graphs and then executes the computational graphs. However, when the ‘model.build’ is executed first, it only performs the graphs execution.

参数
  • epoch (int) – Generally, total number of iterations on the data per epoch. When dataset_sink_mode is set to true and sink_size>0, each epoch sink sink_size steps on the data instead of total number of iterations.

  • train_dataset (Dataset) – A training dataset iterator. If there is no loss_fn, a tuple with multiple data (data1, data2, data3, …) should be returned and passed to the network. Otherwise, a tuple (data, label) should be returned. The data and label would be passed to the network and loss function respectively.

  • callbacks (Optional[list[Callback], Callback]) – List of callback objects or callback object, which should be executed while training. Default: None.

  • dataset_sink_mode (bool) – Determines whether to pass the data through dataset channel. Configure pynative mode or CPU, the training process will be performed with dataset not sink. Default: True.

  • sink_size (int) – Control the amount of data in each sink. If sink_size = -1, sink the complete dataset for each epoch. If sink_size > 0, sink sink_size data for each epoch. If dataset_sink_mode is False, set sink_size as invalid. Default: -1.

实际案例

>>> from mindspore import Model, nn, FixedLossScaleManager
>>>
>>> # For details about how to build the dataset, please refer to the tutorial
>>> # document on the official website.
>>> dataset = create_custom_dataset()
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> loss_scale_manager = FixedLossScaleManager()
>>> optim = nn.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.train(2, dataset)
property train_network

Get the model’s train_network.

tinyms.model.load(file_name)[源代码]

Load MindIR graph and return the network with parameters.

The returned object is wrapperred by a GraphLayer. However, there are some limitations to the current use of GraphLayer, see class tinyms.layers.GraphLayer for more details.

参数

file_name (str) – MindIR file name.

返回

GraphLayer instance, a compiled graph with parameters.

引发

ValueError – MindIR file is incorrect.

实际案例

>>> import tinyms as ts
>>> import tinyms.layers as layers
>>> from tinyms.model import Model, load
>>>
>>> net = layers.Conv2d(1, 1, kernel_size=3)
>>> model = Model(net)
>>> input = ts.ones([1, 1, 3, 3])
>>> model.export(input, "net", file_format="MINDIR")
...
>>> net = load("net.mindir")
>>> print(net(input))
[[[[ 0.02548009  0.04010789  0.03120251]
   [ 0.00268656  0.02353744  0.03807815]
   [-0.00896441 -0.00303641  0.01502199]]]]
tinyms.model.lenet5(**kwargs)[源代码]

Get LeNet5 neural network.

参数

class_num (int) – Class number. Default: 10.

返回

layers.Layer, layer instance of LeNet5 neural network.

实际案例

>>> net = lenet5(class_num=10)
class tinyms.model.LeNet(class_num=10, channel_num=1)[源代码]

LeNet architecture.

参数
  • class_num (int) – The number of classes that the training images are belonging to.

  • channel_num (int) – The channel number.

返回

Tensor, output tensor.

实际案例

>>> LeNet(class_num=10)
tinyms.model.resnet50(**kwargs)[源代码]

Get ResNet50 neural network.

参数

class_num (int) – Class number. Default: 10.

返回

layers.Layer, layer instance of ResNet50 neural network.

实际案例

>>> net = resnet50(10)
class tinyms.model.ResNet(block, layer_nums, in_channels, out_channels, strides, num_classes)[源代码]

ResNet architecture.

参数
  • block (layers.Layer) – Block for network.

  • layer_nums (list) – Numbers of block in different layers.

  • in_channels (list) – Input channel in each layer.

  • out_channels (list) – Output channel in each layer.

  • strides (list) – Stride size in each layer.

  • num_classes (int) – The number of classes that the training images are belonging to.

返回

Tensor, output tensor.

实际案例

>>> ResNet(ResidualBlock,
>>>        [3, 4, 6, 3],
>>>        [64, 256, 512, 1024],
>>>        [256, 512, 1024, 2048],
>>>        [1, 2, 2, 2],
>>>        10)
tinyms.model.mobilenetv2(**kwargs)[源代码]

Get MobileNetV2 instance for model training, evaluation and prediction.

参数
  • class_num (int) – The number of classes. Default: 1000.

  • is_training (bool) – Whether to do training job, default: True.

返回

model.MobileNetV2, MobileNetV2 instance.

class tinyms.model.MobileNetV2(class_num=1000, width_mult=1.0, round_nearest=8, input_channel=32, last_channel=1280, is_training=True)[源代码]

MobileNetV2 architecture.

参数
  • class_num (int) – The number of classes.

  • width_mult (float) – Channels multiplier for round to 8/16 and others. Default is 1.0.

  • round_nearest (int) – Channel round to. Default is 8.

  • input_channel (int) – Input channel. Default is 32.

  • last_channel (int) – The channel of last layer. Default is 1280.

返回

Tensor, output tensor.

tinyms.model.ssd300_mobilenetv2(**kwargs)[源代码]

Get SSD300 model instance for training, evaluation and prediction.

参数
  • class_num (int) – The number of classes. Default: 21.

  • is_training (bool) – Whether to do training job, default: True.

返回

model.SSD300, SSD300 instance.

class tinyms.model.SSD300(backbone, class_num=21, is_training=True)[源代码]

SSD300 Network. Default backbone is MobileNetV2.

参数
  • backbone (layers.Layer) – backbone of ssd300 model.

  • class_num (int) – number of classes. Default: 21.

  • is_training (bool) – Specify if in training step. Default: True.

返回

Tensor, localization predictions. Tensor, class conf scores.

tinyms.model.cycle_gan(G_A, G_B)[源代码]

Get Cycle GAN network.

参数
  • G_A (layers.Layer) – The generator net, currently it should be in [resnet, unet].

  • G_B (layers.Layer) – The generator net, currently it should be in [resnet, unet].

返回

Cycle GAN instance.

实际案例

>>> gan_net = cycle_gan(G_A, G_B)
tinyms.model.cycle_gan_infer(g_model='resnet')[源代码]

Get Cycle GAN network for predict.

参数

g_model (str) – The generator network type, currently it should be in [‘resnet’, ‘unet’]. Default: resnet.

返回

Cycle GAN instance.

实际案例

>>> gan_net = cycle_gan(G_A, G_B)
tinyms.model.densenet100(**kwargs)[源代码]

Get DenseNet instance for model training, evaluation and prediction.

参数

class_num (int) – The number of classes. Default: 10.

返回

model.DenseNet, DenseNet instance.

tinyms.model.alexnet(**kwargs)[源代码]

Get AlexNet neural network.

参数

class_num (int) – Class number. Default: 10.

返回

layers.Layer, layer instance of AlexNet neural network.

实际案例

>>> from tinyms.model import alexnet
>>>
>>> net = alexnet(class_num=10)
class tinyms.model.AlexNet(class_num=1000)[源代码]

Get AlexNet neural network.

参数

class_num (int) – Class number. Default: 1000.

返回

layers.Layer, layer instance of AlexNet neural network.

实际案例

>>> from tinyms.model import AlexNet
>>>
>>> net = AlexNet(class_num=1000)
tinyms.model.sentimentnet(vocab_size, embed_size, num_hiddens, num_layers, bidirectional, num_classes, weight, batch_size)[源代码]

Sentiment network structure.

class tinyms.model.SentimentNet(vocab_size, embed_size, num_hiddens, num_layers, bidirectional, num_classes, weight, batch_size)[源代码]

Sentiment network structure.

tinyms.model.bert(config, is_training, use_one_hot_embeddings=False)[源代码]

Get bert neural network.

class tinyms.model.Bert(config, is_training, use_one_hot_embeddings=False)[源代码]

Bidirectional Encoder Representations from Transformers.

参数
  • config (Class) – Configuration for BertModel.

  • is_training (bool) – True for training mode. False for eval mode.

  • use_one_hot_embeddings (bool) – Specifies whether to use one hot encoding form. Default: False.

construct(input_ids, token_type_ids, input_mask)[源代码]

Bidirectional Encoder Representations from Transformers.

tinyms.model.vgg11(**kwargs)[源代码]

Get vgg11 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg11 neural network.

实际案例

>>> from tinyms.model import vgg11
>>>
>>> net = vgg11(class_num=10)
tinyms.model.vgg13(**kwargs)[源代码]

Get vgg13 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg13 neural network.

实际案例

>>> from tinyms.model import vgg13
>>>
>>> net = vgg13(class_num=10)
tinyms.model.vgg16(**kwargs)[源代码]

Get vgg16 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg16 neural network.

实际案例

>>> from tinyms.model import vgg16
>>>
>>> net = vgg16(class_num=10)
tinyms.model.vgg19(**kwargs)[源代码]

Get vgg19 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg19 neural network.

实际案例

>>> from tinyms.model import vgg19
>>>
>>> net = vgg19(class_num=10)
class tinyms.model.VGG(features, class_num=1000)[源代码]

Get VGG neural network.

参数
  • features (layers.Layer) – Feature extractor.

  • class_num (int) – Class number. Default: 1000.

返回

layers.Layer, layer instance of AlexNet neural network.

实际案例

>>> from tinyms.model import VGG
>>>
>>> net = VGG(features=make_layers(cfg=cfgs['A']),class_num=1000)
tinyms.model.deepfm(**kwargs)[源代码]

Get DeepFM neural network.

参数
  • field_size (int) – The field size. Default: 39.

  • vocab_size (int) – The vocabulary size. Default: 184965.

  • embed_size (int) – The embeding size. Default: 80.

  • keep_prob (float) – The keep prob value. Default: 0.9.

  • convert_dtype (bool) – Whether to convert data type. Defalut: False.

返回

layers.Layer, layer instance of DeepFM neural network.

实际案例

>>> from tinyms.model import deepfm
>>>
>>> net = deepfm(39, 184965, 80)
class tinyms.model.DeepFM(field_size, vocab_size, embed_size, keep_prob=0.9, convert_dtype=False)[源代码]

DeepFM architecture.

参数
  • field_size (int) – The field size.

  • vocab_size (int) – The vocabulary size.

  • embed_size (int) – The embeding size.

  • keep_prob (float) – The keep prob value. Default: 0.9.

  • convert_dtype (bool) – Whether to convert data type.

  • can only set to False. Defalut (CPU) – False.

返回

Tensor, output tensor.

实际案例

>>> from tinyms.model import DeepFM
>>>
>>> DeepFM(field_size=39, vocab_size=184965, embed_size=80)
class tinyms.model.DeepFMEvalModel(network)[源代码]

Provide DeepFM training network.

参数

network (layers.Layer) – The base network.

返回

logits (Tensor) - With the shape as [batch_size, vocab_size] and data type as float32. predict_probs (Tensor) - With the same shape as [batch_size, vocab_size] and data type as float32. labels (Tensor) - With the same shape as labels.

实际案例

>>> from tinyms.model import deepfm, DeepFMEvalModel
>>>
>>> net = deepfm()
>>> eval_net = DeepFMEvalModel(net)
class tinyms.model.DeepFMWithLoss(network, l2_coef=1e-06)[源代码]

Provide DeepFM training loss through network.

参数
  • network (layers.Layer) – The training network.

  • l2_coef (float) – value for l2 loss. Default: 1e-6.

返回

Tensor, the loss of the network.

实际案例

>>> from tinyms.model import deepfm, DeepFMWithLoss, DeepFMTrainModel
>>>
>>> net = deepfm()
>>> train_net = DeepFMTrainModel(DeepFMWithLoss(net))
class tinyms.model.DeepFMTrainModel(network, learning_rate=0.0005, eps=5e-08, loss_scale=1024.0)[源代码]

Provide DeepFM training network.

参数
  • network (layers.Layer) – The base network.

  • learning_rate (float) – A value or a graph for the learning rate. Default: 0.0005.

  • eps (float) – Term added to the denominator to improve numerical stability.

  • be greater than 0. Default (Should) – 0.00000005.

  • loss_scale (float) – A floating point value for the loss scale.

  • be greater than 0. Default – 1024.0.

返回

Tensor, the value passed by last operator.

实际案例

>>> from tinyms.model import deepfm, DeepFMWithLoss, DeepFMTrainModel
>>>
>>> net = deepfm()
>>> train_net = DeepFMTrainModel(DeepFMWithLoss(net))

tinyms.initializers

class tinyms.initializers.Initializer(**kwargs)[源代码]

The base class of the initializer. Initialization of tensor basic attributes and model weight values.

参数

kwargs (dict) – Keyword arguments for Initializer.

tinyms.initializers.initializer(init, shape=None, dtype=mindspore.float32)[源代码]

Create and initialize a tensor.

参数
  • init (Union[Tensor, str, Initializer, numbers.Number]) –

    Initialize value.

    • str: The init should be the alias of the class inheriting from Initializer and the corresponding class will be called. The value of ‘init’ can be “normal”, “ones” or “zeros”, etc.

    • Initializer: The init should be the class inheriting from Initializer to initialize tensor.

    • numbers.Number: The Constant will be called to initialize tensor.

  • shape (Union[tuple, list, int]) – A list of integers, a tuple of integers or an integer as the shape of output. Default: None.

  • dtype (mindspore.dtype) – The type of data in initialized tensor. Default: mindspore.float32.

返回

Union[Tensor], return is Tensor object.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, One
>>> tensor1 = initializer('ones', [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer(One(), [1, 2, 3], mindspore.float32)
>>> tensor3 = initializer(0, [1, 2, 3], mindspore.float32)
class tinyms.initializers.TruncatedNormal(sigma=0.01)[源代码]

Initialize a truncated normal distribution which is a bounded normal distribution within \({N}(\text{low}, \text{high})\).

参数

sigma (float) – The sigma of the array. Default: 0.01.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, TruncatedNormal
>>> tensor1 = initializer(TruncatedNormal(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('truncatedNormal', [1, 2, 3], mindspore.float32)
class tinyms.initializers.Normal(sigma=0.01, mean=0.0)[源代码]

Initialize a normal array, and obtain values \({N}(\text{sigma}, \text{mean})\) from the normal distribution to fill the input tensor.

\[f(x) = \frac{1} {\sqrt{2*π} * sigma}exp(-\frac{(x - mean)^2} {2*{sigma}^2})\]
参数
  • sigma (float) – The sigma of the array. Default: 0.01.

  • mean (float) – The mean of the array. Default: 0.0.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, Normal
>>> tensor1 = initializer(Normal(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('normal', [1, 2, 3], mindspore.float32)
class tinyms.initializers.Uniform(scale=0.07)[源代码]

Initialize a uniform array, and obtain values \({U}(-\text{scale}, \text{scale})\) from the uniform distribution to fill the input tensor.

参数

scale (float) – The scale of the array. Default: 0.07.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, Uniform
>>> tensor1 = initializer(Uniform(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('uniform', [1, 2, 3], mindspore.float32)
class tinyms.initializers.HeUniform(negative_slope=0, mode='fan_in', nonlinearity='leaky_relu')[源代码]

Initialize the array with HeKaiming Uniform algorithm, and from a uniform distribution collect samples within \({U}(-\text{boundary}, \text{boundary})\) where

\[boundary = \sqrt{\frac{6}{(1 + a^2) \times \text{fan_in}}}\]
  • where \(-boundary\) the lower bound of the HeUniform distribution.

  • where \(boundary\) the upper bound of the HeUniform distribution.

For details of HeUniform algorithm, please check https://arxiv.org/abs/1502.01852.

参数
  • negative_slope (int, float, bool) – The negative slope of the rectifier used after this layer (only used when nonlinearity is ‘leaky_relu’). Default: 0.

  • mode (str) – Either ‘fan_in’ or ‘fan_out’. Choosing ‘fan_in’ preserves the magnitude of the variance of the weights in the forward pass. Choosing ‘fan_out’ preserves the magnitudes in the backwards pass. Default: fan_in.

  • nonlinearity (str) – The non-linear function, recommended to use only with ‘relu’ or ‘leaky_relu’. Default: leaky_relu.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, HeUniform
>>> tensor1 = initializer(HeUniform(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('he_uniform', [1, 2, 3], mindspore.float32)
class tinyms.initializers.HeNormal(negative_slope=0, mode='fan_in', nonlinearity='leaky_relu')[源代码]

Initialize the array with HeKaiming Normal algorithm, and from a normal distribution collect samples within \({N}(0, \text{sigma}^2)\) where

\[sigma = \frac{gain} {\sqrt{mode}}\]
  • where \(gain\) is an optional scaling factor.

  • where \(mode\) is the number of input units or output units in the weight tensor.

For details of HeUniform algorithm, please check https://arxiv.org/abs/1502.01852.

参数
  • negative_slope (int, float, bool) – The negative slope of the rectifier used after this layer (only used when nonlinearity is ‘leaky_relu’). Default: 0.

  • mode (str) – Either ‘fan_in’ or ‘fan_out’. Choosing ‘fan_in’ preserves the magnitude of the variance of the weights in the forward pass. Choosing ‘fan_out’ preserves the magnitudes in the backwards pass. Default: fan_in.

  • nonlinearity (str) – The non-linear function, recommended to use only with ‘relu’ or ‘leaky_relu’. Default: leaky_relu.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, HeNormal
>>> tensor1 = initializer(HeNormal(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('he_normal', [1, 2, 3], mindspore.float32)
class tinyms.initializers.XavierUniform(gain=1)[源代码]

Initialize the array with xavier uniform algorithm, and from a uniform distribution collect samples within \({U}(-\text{boundary}, \text{boundary})\) where:

\[boundary = gain * \sqrt{\frac{6}{n_{in} + n_{out}}}\]
  • where \(gain\) is an optional scaling factor.

  • where \(n_{in}\) is the number of input units in the weight tensor.

  • where \(n_{out}\) is the number of output units in the weight tensor.

For details of XavierUniform algorithm, please check http://proceedings.mlr.press/v9/glorot10a.html.

参数

gain (float) – An optional scaling factor. Default: 1.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, XavierUniform
>>> tensor1 = initializer(XavierUniform(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('xavier_uniform', [1, 2, 3], mindspore.float32)
class tinyms.initializers.One(**kwargs)[源代码]

Fills the input array with the values one.

参数

arr (Array) – The array to be assigned.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, One
>>> tensor1 = initializer(One(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('ones', [1, 2, 3], mindspore.float32)
class tinyms.initializers.Zero(**kwargs)[源代码]

Fills the input array with the values zero.

参数

arr (Array) – The array to be assigned.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer, Zero
>>> tensor1 = initializer(Zero(), [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer('zeros', [1, 2, 3], mindspore.float32)
class tinyms.initializers.Constant(value)[源代码]

Initialize a constant.

参数

value (Union[int, numpy.ndarray]) – The value to initialize.

实际案例

>>> import mindspore
>>> from mindspore.common.initializer import initializer
>>> tensor1 = initializer(0, [1, 2, 3], mindspore.float32)
>>> tensor2 = initializer(5, [1, 2, 3], mindspore.float32)

tinyms.losses

Losses module. Loss function in machine learning is the target of the model. It shows how well the model works on a dataset and the optimization target which the optimizer is searching.

tinyms.losses.net_with_loss(net)[源代码]

This function is provided for AI beginners who are not familiar with which loss should be chosen for the network to be trained. Instead of choosing different loss function, users could directly get the best suitable loss function by specifying network.

参数

net (layers.Layer) – The instance of network to be trained.

引发

TypeError – When network type is not supported.

注解

Currently this function only supports few networks, if the network type is not supported, the system would raise TypeError exception.

实际案例

>>> from tinyms.model import ssd300
>>> from tinyms.losses import net_with_loss
>>>
>>> net = ssd300()
>>> net_loss = net_with_loss(net)
class tinyms.losses.SSD300WithLoss(network)[源代码]

Provide SSD300 training loss through network.

参数

network (layers.Layer) – The training network.

返回

Tensor, the loss of the network.

实际案例

>>> from tinyms.model import ssd300
>>> from tinyms.losses import SSD300WithLoss
>>>
>>> net = SSD300WithLoss(ssd300())
class tinyms.losses.CrossEntropyWithLabelSmooth(smooth_factor=0.0, num_classes=1000)[源代码]

CrossEntropyWith LabelSmooth.

参数
  • smooth_factor (float) – Smooth factor. Default is 0.

  • num_classes (int) – Number of classes. Default is 1000.

返回

None.

实际案例

>>> CrossEntropyWithLabelSmooth(smooth_factor=0., num_classes=1000)
class tinyms.losses.CycleGANGeneratorLoss(generator, D_A, D_B)[源代码]

Cycle GAN generator loss.

参数
  • generator (layers.Layer) – Generator of CycleGAN.

  • D_A (layers.Layer) – The discriminator network of domain A to domain B.

  • D_B (layers.Layer) – The discriminator network of domain B to domain A.

Outputs:

Tuple Tensor, the losses of generator.

construct(img_A, img_B)[源代码]

If use_identity, identity loss will be used.

class tinyms.losses.CycleGANDiscriminatorLoss(D_A, D_B, reduction='none')[源代码]

Cycle GAN discriminator loss.

参数
  • D_A (layers.Layer) – The discriminator network of domain A to domain B.

  • D_B (layers.Layer) – The discriminator network of domain B to domain A.

  • reduction (str) – The discriminator network of reduction. Default: none.

Outputs:

the loss of discriminator.

class tinyms.losses.LossBase(reduction='mean')[源代码]

Base class for other losses.

Other losses derived from this should implement their own construct and use method self.get_loss to apply reduction to loss values.

参数

reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. Default: “mean”.

引发

ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend GPU CPU

get_axis(x)[源代码]

Get a range of axis for input.

参数

x (Tensor) – Tensor of any shape.

get_loss(x, weights=1.0)[源代码]

Computes the weighted loss.

参数
  • x (Tensor) – Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.

  • weights (Union[float, Tensor]) – Optional Tensor whose rank is either 0, or the same rank as inputs, and must be broadcastable to inputs (i.e., all dimensions must be either 1, or the same as the corresponding inputs dimension).

class tinyms.losses.L1Loss(reduction='mean')[源代码]

L1Loss creates a criterion to measure the mean absolute error (MAE) between \(x\) and \(y\) element-wise, where \(x\) is the input Tensor and \(y\) is the labels Tensor.

For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(N\), the unreduced loss (i.e. with argument reduction set to ‘none’) of \(x\) and \(y\) is given as:

\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|,\]

where \(N\) is the batch size. If reduction is not ‘none’, then:

\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]
参数

reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. Default: “mean”.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.

  • labels (Tensor) - Tensor of shape \((N, *)\), same shape as the logits in common cases. However, it supports the shape of logits is different from the shape of labels and they should be broadcasted to each other.

Outputs:

Tensor, loss float tensor, the shape is zero if reduction is ‘mean’ or ‘sum’, while the shape of output is the broadcasted shape if reduction is ‘none’.

引发

ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # Case 1: logits.shape = labels.shape = (3,)
>>> loss = nn.L1Loss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.33333334
>>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
>>> loss = nn.L1Loss(reduction='none')
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[[0. 1. 2.]
 [0. 0. 1.]]
class tinyms.losses.MSELoss(reduction='mean')[源代码]

MSELoss creates a criterion to measure the mean squared error (squared L2-norm) between \(x\) and \(y\) element-wise, where \(x\) is the input and \(y\) is the labels.

For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(N\), the unreduced loss (i.e. with argument reduction set to ‘none’) of \(x\) and \(y\) is given as:

\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with} \quad l_n = (x_n - y_n)^2.\]

where \(N\) is the batch size. If reduction is not ‘none’, then:

\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}\end{split}\]
参数

reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. Default: “mean”.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.

  • labels (Tensor) - Tensor of shape \((N, *)\), same shape as the logits in common cases. However, it supports the shape of logits is different from the shape of labels and they should be broadcasted to each other.

Outputs:

Tensor, loss float tensor, the shape is zero if reduction is ‘mean’ or ‘sum’, while the shape of output is the broadcasted shape if reduction is ‘none’.

引发

ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # Case 1: logits.shape = labels.shape = (3,)
>>> loss = nn.MSELoss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 1, 1]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
1.6666667
>>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
>>> loss = nn.MSELoss(reduction='none')
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[[0. 1. 4.]
 [0. 0. 1.]]
class tinyms.losses.SmoothL1Loss(beta=1.0)[源代码]

A loss class for learning region proposals.

SmoothL1Loss can be regarded as modified version of L1Loss or a combination of L1Loss and L2Loss. L1Loss computes the element-wise absolute difference between two input tensors while L2Loss computes the squared difference between two input tensors. L2Loss often leads to faster convergence but it is less robust to outliers.

Given two input \(x,\ y\) of length \(N\), the unreduced SmoothL1Loss can be described as follows:

\[\begin{split}L_{i} = \begin{cases} \frac{0.5 (x_i - y_i)^{2}}{\text{beta}}, & \text{if } |x_i - y_i| < \text{beta} \\ |x_i - y_i| - 0.5 \text{beta}, & \text{otherwise. } \end{cases}\end{split}\]

Here \(\text{beta}\) controls the point where the loss function changes from quadratic to linear. Its default value is 1.0. \(N\) is the batch size. This function returns an unreduced loss tensor.

参数

beta (float) – A parameter used to control the point where the function will change from quadratic to linear. Default: 1.0.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions. Data type must be float16 or float32.

  • labels (Tensor) - Ground truth data, tensor of shape \((N, *)\), same shape and dtype as the logits.

Outputs:

Tensor, loss float tensor, same shape and dtype as the logits.

引发
  • TypeError – If beta is not a float.

  • TypeError – If dtype of logits or labels is neither float16 not float32.

  • ValueError – If beta is less than or equal to 0.

  • ValueError – If shape of logits is not the same as labels.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> loss = nn.SmoothL1Loss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[0.  0.  0.5]
class tinyms.losses.SoftMarginLoss(reduction='mean')[源代码]

A loss class for two-class classification problems.

SoftMarginLoss creates a criterion that optimizes a two-class classification logistic loss between input tensor \(x\) and labels tensor \(y\) (containing 1 or -1).

\[\text{loss}(x, y) = \sum_i \frac{\log(1 + \exp(-y[i]*x[i]))}{\text{x.nelement}()}\]
参数

reduction (str) – Apply specific reduction method to the output: ‘none’, ‘mean’, ‘sum’. Default: “mean”.

Inputs:
  • logits (Tensor) - Predict data. Data type must be float16 or float32.

  • labels (Tensor) - Ground truth data, with the same type and shape as logits.

Outputs:

Tensor or Scalar, if reduction is “none”, its shape is the same as logits. Otherwise, a scalar value will be returned.

引发
  • TypeError – If logits or labels is not a Tensor.

  • TypeError – If dtype of logits or labels is neither float16 nor float32.

  • ValueError – If shape of logits is not the same as labels.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend

实际案例

>>> loss = ops.SoftMarginLoss()
>>> logits = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
>>> labels = Tensor(np.array([[-1, 1], [1, -1]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.6764238
class tinyms.losses.FocalLoss(weight=None, gamma=2.0, reduction='mean')[源代码]

The loss function proposed by Kaiming team in their paper Focal Loss for Dense Object Detection improves the effect of image object detection. It is a loss function to solve the imbalance of categories and the difference of classification difficulty. If you want to learn more, please refer to the paper. Focal Loss for Dense Object Detection. The function is shown as follows:

\[FL(p_t) = -(1-p_t)^\gamma log(p_t)\]
参数
  • gamma (float) – Gamma is used to adjust the steepness of weight curve in focal loss. Default: 2.0.

  • weight (Union[Tensor, None]) – A rescaling weight applied to the loss of each batch element. The dimension of weight should be 1. If None, no weight is applied. Default: None.

  • reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. If “none”, do not perform reduction. Default: “mean”.

Inputs:
  • logits (Tensor) - Tensor of shape should be \((B, C)\) or \((B, C, H)\) or \((B, C, H, W)\). Where \(C\) is the number of classes. Its value is greater than 1. If the shape is \((B, C, H, W)\) or \((B, C, H)\), the \(H\) or product of \(H\) and \(W\) should be the same as labels.

  • labels (Tensor) - Tensor of shape should be \((B, C)\) or \((B, C, H)\) or \((B, C, H, W)\). The value of \(C\) is 1 or it needs to be the same as predict’s \(C\). If \(C\) is not 1, the shape of target should be the same as that of predict, where \(C\) is the number of classes. If the shape is \((B, C, H, W)\) or \((B, C, H)\), the \(H\) or product of \(H\) and \(W\) should be the same as logits.

Outputs:

Tensor or Scalar, if reduction is “none”, its shape is the same as logits. Otherwise, a scalar value will be returned.

引发
  • TypeError – If the data type of gamma is not a float.

  • TypeError – If weight is not a Tensor.

  • ValueError – If labels dim is different from logits.

  • ValueError – If labels channel is not 1 and labels shape is different from logits.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend

示例

>>> logits = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32)
>>> labels = Tensor([[1], [1], [0]], mstype.int32)
>>> focalloss = nn.FocalLoss(weight=Tensor([1, 2]), gamma=2.0, reduction='mean')
>>> output = focalloss(logits, labels)
>>> print(output)
0.12516622
class tinyms.losses.SoftmaxCrossEntropyWithLogits(sparse=False, reduction='none')[源代码]

Computes softmax cross entropy between logits and labels.

Measures the distribution error between the probabilities of the input (computed with softmax function) and the labels where the classes are mutually exclusive (only one class is positive) using cross entropy loss.

Typical input into this function is unnormalized scores denoted as x whose shape is (N, C), and the corresponding targets.

For each instance \(x_i\), i ranges from 0 to N-1, the loss is given as:

\[\ell(x_i, c) = - \log\left(\frac{\exp(x_i[c])}{\sum_j \exp(x_i[j])}\right) = -x_i[c] + \log\left(\sum_j \exp(x_i[j])\right)\]

where \(x_i\) is a 1D score Tensor, \(c\) is the index of 1 in one-hot.

注解

While the labels classes are mutually exclusive, i.e., only one class is positive in the labels, the predicted probabilities does not need to be exclusive. It is only required that the predicted probability distribution of entry is a valid one.

参数
  • sparse (bool) – Specifies whether labels use sparse format or not. Default: False.

  • reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. If “none”, do not perform reduction. Default: “none”.

Inputs:
  • logits (Tensor) - Tensor of shape (N, C). Data type must be float16 or float32.

  • labels (Tensor) - Tensor of shape (N, ). If sparse is True, The type of labels is int32 or int64. Otherwise, the type of labels is the same as the type of logits.

Outputs:

Tensor, a tensor of the same shape and type as logits with the component-wise logistic losses.

引发
  • TypeError – If sparse is not a bool.

  • TypeError – If sparse is True and dtype of labels is neither int32 not int64.

  • TypeError – If sparse is False and dtype of labels is neither float16 not float32.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # case 1: sparse=True
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
>>> logits = Tensor(np.array([[3, 5, 6, 9, 12, 33, 42, 12, 32, 72]]), mindspore.float32)
>>> labels_np = np.array([1]).astype(np.int32)
>>> labels = Tensor(labels_np)
>>> output = loss(logits, labels)
>>> print(output)
[67.]
>>> # case 2: sparse=False
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
>>> logits = Tensor(np.array([[3, 5, 6, 9, 12, 33, 42, 12, 32, 72]]), mindspore.float32)
>>> labels_np = np.array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]).astype(np.float32)
>>> labels = Tensor(labels_np)
>>> output = loss(logits, labels)
>>> print(output)
[30.]
class tinyms.losses.BCELoss(weight=None, reduction='none')[源代码]

BCELoss creates a criterion to measure the binary cross entropy between the true labels and predicted labels.

Set the predicted labels as \(x\), true labels as \(y\), the output loss as \(\ell(x, y)\). Let,

\[L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]\]

Then,

\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]

注解

Note that the predicted labels should always be the output of sigmoid and the true labels should be numbers between 0 and 1.

参数
  • weight (Tensor, optional) – A rescaling weight applied to the loss of each batch element. And it must have the same shape and data type as inputs. Default: None

  • reduction (str) – Specifies the reduction to be applied to the output. Its value must be one of ‘none’, ‘mean’, ‘sum’. Default: ‘none’.

Inputs:
  • logits (Tensor) - The input tensor with shape \((N, *)\) where \(*\) means, any number of additional dimensions. The data type must be float16 or float32.

  • labels (Tensor) - The label tensor with shape \((N, *)\), the same shape and data type as logits.

Outputs:

Tensor or Scalar, if reduction is ‘none’, then output is a tensor and has the same shape as logits. Otherwise, the output is a scalar.

引发
  • TypeError – If dtype of logits, labels or weight (if given) is neither float16 not float32.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

  • ValueError – If shape of logits is not the same as labels or weight (if given).

Supported Platforms:

Ascend GPU CPU

实际案例

>>> weight = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 3.3, 2.2]]), mindspore.float32)
>>> loss = nn.BCELoss(weight=weight, reduction='mean')
>>> logits = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]), mindspore.float32)
>>> labels = Tensor(np.array([[0, 1, 0], [0, 0, 1]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
1.8952923
class tinyms.losses.BCEWithLogitsLoss(reduction='mean', weight=None, pos_weight=None)[源代码]

Adds sigmoid activation function to input logits, and uses the given logits to compute binary cross entropy between the logits and the labels.

Sets input logits as \(X\), input labels as \(Y\), output as \(L\). Then,

\[p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}}\]
\[L_{ij} = -[Y_{ij} \cdot log(p_{ij}) + (1 - Y_{ij}) \cdot log(1 - p_{ij})]\]

Then,

\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]
参数
  • reduction (str) – Type of reduction to be applied to loss. The optional values are ‘mean’, ‘sum’, and ‘none’. If ‘none’, do not perform reduction. Default:’mean’.

  • weight (Tensor, optional) – A rescaling weight applied to the loss of each batch element. If not None, it can be broadcast to a tensor with shape of logits, data type must be float16 or float32. Default: None.

  • pos_weight (Tensor, optional) – A weight of positive examples. Must be a vector with length equal to the number of classes. If not None, it must can be broadcast to a tensor with shape of logits, data type must be float16 or float32. Default: None.

Inputs:
  • logits (Tensor) - Input logits with shape \((N, *)\) where \(*\) means, any number of additional dimensions. The data type must be float16 or float32.

  • labels (Tensor) - Ground truth label with shape \((N, *)\), same shape and dtype as logits.

Outputs:

Tensor or Scalar, if reduction is “none”, its shape is the same as logits. Otherwise, a scalar value will be returned.

引发
  • TypeError – If data type of logits or labels is neither float16 nor float32.

  • TypeError – If weight or pos_weight is a parameter.

  • TypeError – If data type of weight or pos_weight is neither float16 nor float32.

  • ValueError – If weight or pos_weight can not be broadcast to a tensor with shape of logits.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend GPU

实际案例

>>> logits = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
>>> labels = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32))
>>> loss = nn.BCEWithLogitsLoss()
>>> output = loss(logits, labels)
>>> print(output)
0.3463612
class tinyms.losses.CosineEmbeddingLoss(margin=0.0, reduction='mean')[源代码]

CosineEmbeddingLoss creates a criterion to measure the similarity between two tensors using cosine distance.

Given two tensors \(x1\), \(x2\), and a Tensor label \(y\) with values 1 or -1:

\[\begin{split}loss(x_1, x_2, y) = \begin{cases} 1-cos(x_1, x_2), & \text{if } y = 1\\ max(0, cos(x_1, x_2)-margin), & \text{if } y = -1\\ \end{cases}\end{split}\]
参数
  • margin (float) – Should be in [-1.0, 1.0]. Default 0.0.

  • reduction (str) – Specifies which reduction to be applied to the output. It must be one of “none”, “mean”, and “sum”, meaning no reduction, reduce mean and sum on output, respectively. Default “mean”.

Inputs:
  • logits_x1 (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.

  • logits_x2 (Tensor) - Tensor of shape \((N, *)\), same shape and dtype as logits_x1.

  • labels (Tensor) - Contains value 1 or -1. Suppose the shape of logits_x1 is \((x_1, x_2, x_3, ..., x_R)\), then the shape of labels must be \((x_1, x_3, x_4, ..., x_R)\).

Outputs:

Tensor or Scalar, if reduction is “none”, its shape is the same as labels. Otherwise, a scalar value will be returned.

引发
  • TypeError – If margin is not a float.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

  • ValueError – If margin is not in range [-1, 1].

Supported Platforms:

Ascend GPU CPU

实际案例

>>> logits_x1 = Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]), mindspore.float32)
>>> logits_x2 = Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]), mindspore.float32)
>>> labels = Tensor(np.array([1, -1]), mindspore.int32)
>>> cosine_embedding_loss = nn.CosineEmbeddingLoss()
>>> output = cosine_embedding_loss(logits_x1, logits_x2, labels)
>>> print(output)
0.0003425479
class tinyms.losses.SampledSoftmaxLoss(num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, seed=0, reduction='none')[源代码]

Computes the sampled softmax training loss. This operator can accelerate the training of the softmax classifier over a large number of classes. It is generally an underestimate of the full softmax loss.

参数
  • num_sampled (int) – The number of classes to randomly sample per batch.

  • num_classes (int) – The number of possible classes.

  • num_true (int) – The number of labels classes per training example. Default: 1.

  • sampled_values (Union[list, tuple]) – List or tuple of (sampled_candidates, true_expected_count, sampled_expected_count) returned by a *CandidateSampler function. Default to None, UniformCandidateSampler is applied.

  • remove_accidental_hits (bool) – Whether to remove “accidental hits” where a sampled class equals to one of the labels classes. Default: True.

  • seed (int) – Random seed for candidate sampling. Default: 0

  • reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. If “none”, do not perform reduction. Default: “none”.

Inputs:
  • weights (Tensor) - Tensor of shape \((C, dim)\).

  • bias (Tensor) - Tensor of shape \((C,)\). The class biases.

  • labels (Tensor) - Tensor of shape \((N, num\_true)\), type int64, int32. The labels classes.

  • logits (Tensor) - Tensor of shape \((N, dim)\). The forward activations of the input network.

Outputs:

Tensor or Scalar, if reduction is ‘none’, then output is a tensor with shape \((N,)\). Otherwise, the output is a scalar.

引发
  • TypeError – If sampled_values is not a list or tuple.

  • TypeError – If dtype of labels is neither int32 not int64.

  • ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

  • ValueError – If num_sampled or num_true is greater than num_classes.

  • ValueError – If length of sampled_values is not equal to 3.

Supported Platforms:

GPU

实际案例

>>> mindspore.set_seed(1)
>>> loss = nn.SampledSoftmaxLoss(num_sampled=4, num_classes=7, num_true=1)
>>> weights = Tensor(np.random.randint(0, 9, [7, 10]), mindspore.float32)
>>> biases = Tensor(np.random.randint(0, 9, [7]), mindspore.float32)
>>> labels = Tensor([0, 1, 2])
>>> logits = Tensor(np.random.randint(0, 9, [3, 10]), mindspore.float32)
>>> output = loss(weights, biases, labels, logits)
>>> print(output)
[4.6051701e+01 1.4000047e+01 6.1989022e-06]
class tinyms.losses.DiceLoss(smooth=1e-05)[源代码]

The Dice coefficient is a set similarity loss. It is used to calculate the similarity between two samples. The value of the Dice coefficient is 1 when the segmentation result is the best and is 0 when the segmentation result is the worst. The Dice coefficient indicates the ratio of the area between two objects to the total area. The function is shown as follows:

\[dice = 1 - \frac{2 * (pred \bigcap true)}{pred \bigcup true}\]
参数

smooth (float) – A term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-5.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions. The data type must be float16 or float32.

  • labels (Tensor) - Tensor of shape \((N, *)\), same shape as the logits. The data type must be float16 or float32.

Outputs:

Tensor, a tensor of shape with the per-example sampled Dice losses.

引发
  • ValueError – If the dimension of logits is different from labels.

  • TypeError – If the type of logits or labels is not a tensor.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> loss = nn.DiceLoss(smooth=1e-5)
>>> logits = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
>>> labels = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.38596618
class tinyms.losses.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation='softmax')[源代码]

When there are multiple classifications, label is transformed into multiple binary classifications by one hot. For each channel section in the channel, it can be regarded as a binary classification problem, so it can be obtained through the binary loss of each category, and then the average value.

参数
  • weights (Union[Tensor, None]) – Tensor of shape \((num\_classes, dim)\). The weight shape[0] should be equal to labels shape[1].

  • ignore_indiex (Union[int, None]) – Class index to ignore.

  • activation (Union[str, Cell]) – Activate function applied to the output of the fully connected layer, eg. ‘ReLU’. Default: ‘softmax’. Choose from: [‘softmax’, ‘logsoftmax’, ‘relu’, ‘relu6’, ‘tanh’,’Sigmoid’]

Inputs:
  • logits (Tensor) - Tensor of shape \((N, C, *)\) where \(*\) means, any number of additional dimensions. The logits dimension should be greater than 1. The data type must be float16 or float32.

  • labels (Tensor) - Tensor of shape \((N, C, *)\), same shape as the logits. The labels dimension should be greater than 1. The data type must be float16 or float32.

Outputs:

Tensor, a tensor of shape with the per-example sampled MultiClass Dice Losses.

引发
  • ValueError – If the shape of logits is different from labels.

  • TypeError – If the type of logits or labels is not a tensor.

  • ValueError – If the dimension of logits or labels is less than 2.

  • ValueError – If the weights.shape[0] is not equal to labels.shape[1].

  • ValueError – If weights is a tensor, but its dimension is not 2.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation="softmax")
>>> logits = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.5], [0.9, 0.6, 0.3]]), mstype.float32)
>>> labels = Tensor(np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]]), mstype.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.54958105
class tinyms.losses.RMSELoss[源代码]

RMSELoss creates a criterion to measure the root mean square error between \(x\) and \(y\) element-wise, where \(x\) is the input and \(y\) is the labels.

For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(N\), the loss of \(x\) and \(y\) is given as:

\[loss = \sqrt{\frac{1}{N}\sum_{i=1}^{N}{(x_i-y_i)^2}}\]
Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.

  • labels (Tensor) - Tensor of shape \((N, *)\), same shape as the logits in common cases. However, it supports the shape of logits is different from the shape of labels and they should be broadcasted to each other.

Outputs:

Tensor, weighted loss float tensor and its shape is zero.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # Case 1: logits.shape = labels.shape = (3,)
>>> loss = nn.RMSELoss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.57735026
>>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
>>> loss = nn.RMSELoss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
1.0
class tinyms.losses.MAELoss(reduction='mean')[源代码]

MAELoss creates a criterion to measure the average absolute error between \(x\) and \(y\) element-wise, where \(x\) is the input and \(y\) is the labels.

For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(N\), the unreduced loss (i.e. with argument reduction set to ‘none’) of \(x\) and \(y\) is given as:

\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|,\]

where \(N\) is the batch size. If reduction is not ‘none’, then:

\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]
参数

reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. Default: “mean”.

Inputs:
  • logits (Tensor) - Tensor of shape \((M, *)\) where \(*\) means, any number of additional dimensions.

  • labels (Tensor) - Tensor of shape \((N, *)\), same shape as the logits in common cases. However, it supports the shape of logits is different from the shape of labels and they should be broadcasted to each other.

Outputs:

Tensor, weighted loss float tensor, the shape is zero if reduction is ‘mean’ or ‘sum’, while the shape of output is the broadcasted shape if reduction is ‘none’.

引发

ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> # Case 1: logits.shape = labels.shape = (3,)
>>> loss = nn.MAELoss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.33333334
>>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
>>> loss = nn.MAELoss(reduction='none')
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[[0. 1. 2.]
 [0. 0. 1.]]

tinyms.optimizers

Optimizers module provides common optimizers for training, such as SGD, ADAM, Momentum. The optimizer is used to calculate and update the gradients.

class tinyms.optimizers.Optimizer(learning_rate, parameters, weight_decay=0.0, loss_scale=1.0)[源代码]

Base class for all optimizers.

注解

This class defines the API to add Ops to train a model. Never use this class directly, but instead instantiate one of its subclasses.

Different parameter groups can set different learning_rate, weight_decay and grad_centralization.

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight_decay is positive. For most optimizer, when not separating parameters, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

参数
  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float.

  • parameters (Union[list[Parameter], list[dict]]) –

    When the parameters is a list of Parameter which will be updated, the element in parameters must be class Parameter. When the parameters is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • weight_decay (Union[float, int]) – An int or a floating point value for the weight decay. It must be equal to or greater than 0. If the type of weight_decay input is int, it will be converted to float. Default: 0.0.

  • loss_scale (float) – A floating point value for the loss scale. It must be greater than 0. If the type of loss_scale input is int, it will be converted to float. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If loss_scale is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • ValueError – If loss_scale is less than or equal to 0.

  • ValueError – If weight_decay is less than 0.

  • ValueError – If learning_rate is a Tensor, but the dimension of tensor is greater than 1.

Supported Platforms:

Ascend GPU CPU

broadcast_params(optim_result)[源代码]

Apply Broadcast operations in the sequential order of parameter groups.

返回

bool, the status flag.

decay_weight(gradients)[源代码]

Weight decay.

An approach to reduce the overfitting of a deep learning neural network model.

参数

gradients (tuple[Tensor]) – The gradients of self.parameters, and have the same shape as self.parameters.

返回

tuple[Tensor], The gradients after weight decay.

get_lr()[源代码]

Get the learning rate of current step.

返回

float, the learning rate of current step.

get_lr_parameter(param)[源代码]

Get the learning rate of parameter.

参数

param (Union[Parameter, list[Parameter]]) – The Parameter or list of Parameter.

返回

Parameter, single Parameter or list[Parameter] according to the input type.

gradients_centralization(gradients)[源代码]

Gradients centralization.

A method for optimizing convolutional layer parameters to impore the training speed of a deep learning neural network model.

参数

gradients (tuple[Tensor]) – The gradients of self.parameters, and have the same shape as self.parameters.

返回

tuple[Tensor], The gradients after gradients centralization.

scale_grad(gradients)[源代码]

Loss scale for mixed precision.

An approach of mixed precision training to improve the speed and energy efficiency of training deep neural network.

参数

gradients (tuple[Tensor]) – The gradients of self.parameters, and have the same shape as self.parameters.

返回

tuple[Tensor], The gradients after loss scale.

property target

The method is used to determine whether the parameter is updated on host or device. The input type is str and can only be ‘CPU’, ‘Ascend’ or ‘GPU’.

property unique

The method is to see whether to make unique. The input type is bool. The method is read-only.

class tinyms.optimizers.Momentum(*args, **kwargs)[源代码]

Implements the Momentum algorithm.

Refer to the paper on the importance of initialization and momentum in deep learning for more details.

\[v_{t+1} = v_{t} \ast u + gradients\]

If use_nesterov is True:

\[p_{t+1} = p_{t} - (grad \ast lr + v_{t+1} \ast u \ast lr)\]

If use_nesterov is False:

\[p_{t+1} = p_{t} - lr \ast v_{t+1}\]

Here: where grad, lr, p, v and u denote the gradients, learning_rate, params, moments, and momentum respectively.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float.

  • momentum (float) – Hyperparameter of type float, means momentum for the moving average. It must be at least 0.0.

  • weight_decay (int, float) – Weight decay (L2 penalty). It must be equal to or greater than 0.0. Default: 0.0.

  • loss_scale (float) – A floating point value for the loss scale. It must be greater than 0.0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

  • use_nesterov (bool) – Enable Nesterov momentum. Default: False.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

tuple[bool]. All elements are True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If loss_scale or momentum is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_nesterov is not a bool.

  • ValueError – If loss_scale is less than or equal to 0.

  • ValueError – If weight_decay or momentum is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.Momentum(group_params, learning_rate=0.1, momentum=0.9, weight_decay=0.0)
>>> # The conv_params's parameters will use a learning rate of default value 0.1 and a weight decay of 0.01 and
>>> # grad centralization of True.
>>> # The no_conv_params's parameters will use a learning rate of 0.01 and a weight decay of default value 0.0
>>> # and grad centralization of False..
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
class tinyms.optimizers.LARS(*args, **kwargs)[源代码]

Implements the LARS algorithm with LARSUpdate Operator.

LARS is an optimization algorithm employing a large batch optimization technique. Refer to paper LARGE BATCH TRAINING OF CONVOLUTIONAL NETWORKS.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ \lambda = \frac{\theta \text{ * } || \omega || } \\ {|| g_{t} || \text{ + } \delta \text{ * } || \omega || } \\ \lambda = \begin{cases} \min(\frac{\lambda}{\alpha }, 1) & \text{ if } clip = True \\ \lambda & \text{ otherwise } \end{cases}\\ g_{t+1} = \lambda * (g_{t} + \delta * \omega) \end{array}\end{split}\]

\(\theta\) represents coefficient, \(\omega\) represents parameters, \(g\) represents gradients, \(t\) represents updating step, \(\delta\) represents weight_decay, \(\alpha\) represents learning_rate, \(clip\) represents use_clip.

参数
  • optimizer (Optimizer) – MindSpore optimizer for which to wrap and modify gradients.

  • epsilon (float) – Term added to the denominator to improve numerical stability. Default: 1e-05.

  • coefficient (float) – Trust coefficient for calculating the local learning rate. Default: 0.001.

  • use_clip (bool) – Whether to use clip operation for calculating the local learning rate. Default: False.

  • lars_filter (Function) – A function to determine whether apply the LARS algorithm. Default: lambda x: ‘LayerNorm’ not in x.name and ‘bias’ not in x.name.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params in the optimizer, the shape is the as same as the params in the optimizer.

Outputs:

Union[Tensor[bool], tuple[Parameter]], it depends on the output of optimizer.

Supported Platforms:

Ascend CPU

实际案例

>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> opt = nn.Momentum(net.trainable_params(), 0.1, 0.9)
>>> opt_lars = nn.LARS(opt, epsilon=1e-08, coefficient=0.02)
>>> model = Model(net, loss_fn=loss, optimizer=opt_lars, metrics=None)
class tinyms.optimizers.Adam(*args, **kwargs)[源代码]

Updates gradients by the Adaptive Moment Estimation (Adam) algorithm.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta_1 * m_{t} + (1 - \beta_1) * g \\ v_{t+1} = \beta_2 * v_{t} + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w_{t+1} = w_{t} - l * \frac{m_{t+1}}{\sqrt{v_{t+1}} + \epsilon} \end{array}\end{split}\]

\(m\) represents the 1st moment vector moment1, \(v\) represents the 2nd moment vector moment2, \(g\) represents gradients, \(l\) represents scaling factor, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t\) and \(beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents params, \(\epsilon\) represents eps.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters is supported.

The sparse strategy is applied while the SparseGatherV2 operator is used for forward network. The sparse feature is under continuous development. If the sparse strategy wants to be executed on the host, set the target to the CPU.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” is in the keys, the value of the corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” is in the keys, the value of the corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” is in the keys, the value must be the order of parameters and the order will be followed in the optimizer. There are no other keys in the dict and the parameters which in the ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use the dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 1e-3.

  • beta1 (float) – The exponential decay rate for the 1st moment estimations. Should be in range (0.0, 1.0). Default: 0.9.

  • beta2 (float) – The exponential decay rate for the 2nd moment estimations. Should be in range (0.0, 1.0). Default: 0.999.

  • eps (float) – Term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-8.

  • use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

  • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.

  • weight_decay (float) – Weight decay (L2 penalty). It must be equal to or greater than 0. Default: 0.0.

  • loss_scale (float) – A floating point value for the loss scale. Should be greater than 0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

Tensor[bool], the value is True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If beta1, beta2, eps or loss_scale is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_locking or use_nesterov is not a bool.

  • ValueError – If loss_scale or eps is less than or equal to 0.

  • ValueError – If beta1, beta2 is not in range (0.0, 1.0).

  • ValueError – If weight_decay is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.Adam(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.Adam(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad
>>> # centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
property target

The method is used to determine whether the parameter is updated on host or device. The input type is str and can only be ‘CPU’, ‘Ascend’ or ‘GPU’.

class tinyms.optimizers.AdamWeightDecay(params, learning_rate=0.001, beta1=0.9, beta2=0.999, eps=1e-06, weight_decay=0.0)[源代码]

Implements the Adam algorithm to fix the weight decay.

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta_1 * m_{t} + (1 - \beta_1) * g \\ v_{t+1} = \beta_2 * v_{t} + (1 - \beta_2) * g * g \\ update = \frac{m_{t+1}}{\sqrt{v_{t+1}} + eps} \\ update = \begin{cases} update + weight\_decay * w_{t} & \text{ if } weight\_decay > 0 \\ update & \text{ otherwise } \end{cases} \\ w_{t+1} = w_{t} - lr * update \end{array}\end{split}\]

\(m\) represents the 1st moment vector moment1, \(v\) represents the 2nd moment vector moment2, \(g\) represents gradients, \(lr\) represents learning_rate, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(w\) represents params.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

To improve parameter groups performance, the customized order of parameters can be supported.

There is usually no connection between a optimizer and mixed precision. But when FixedLossScaleManager is used and drop_overflow_update in FixedLossScaleManager is set to False, optimizer needs to set the ‘loss_scale’. As this optimizer has no argument of loss_scale, so loss_scale needs to be processed by other means, refer document LossScale to process loss_scale correctly.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” is in the keys, the value of the corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” is in the keys, the value of the corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” is in the keys, the value must be the order of parameters and the order will be followed in the optimizer. There are no other keys in the dict and the parameters which in the ‘order_params’ must be in one of group parameters.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use the dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 1e-3.

  • beta1 (float) – The exponential decay rate for the 1st moment estimations. Default: 0.9. Should be in range (0.0, 1.0).

  • beta2 (float) – The exponential decay rate for the 2nd moment estimations. Default: 0.999. Should be in range (0.0, 1.0).

  • eps (float) – Term added to the denominator to improve numerical stability. Default: 1e-6. Should be greater than 0.

  • weight_decay (float) – Weight decay (L2 penalty). It must be equal to or greater than 0. Default: 0.0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

tuple[bool], all elements are True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If beta1, beta2 or eps is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • ValueError – If eps is less than or equal to 0.

  • ValueError – If beta1, beta2 is not in range (0.0, 1.0).

  • ValueError – If weight_decay is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.AdamWeightDecay(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.AdamWeightDecay(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
class tinyms.optimizers.LazyAdam(*args, **kwargs)[源代码]

This optimizer will apply a lazy adam algorithm when gradient is sparse.

The original adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta_1 * m_{t} + (1 - \beta_1) * g \\ v_{t+1} = \beta_2 * v_{t} + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w_{t+1} = w_{t} - l * \frac{m_{t+1}}{\sqrt{v_{t+1}} + \epsilon} \end{array}\end{split}\]

\(m\) represents the 1st moment vector moment1, \(v\) represents the 2nd moment vector moment2, \(g\) represents gradients, \(l\) represents scaling factor, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t\) and \(beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents params, \(\epsilon\) represents eps.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

The sparse strategy is applied while the SparseGatherV2 operator being used for forward network. The sparse behavior, to be notice, is not equivalent to the original Adam algorithm, as only the current indices parames will be updated. The sparse feature is under continuous development. If the sparse strategy wants to be executed on the host, set the target to the CPU.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr” and “weight_decay” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 1e-3.

  • beta1 (float) – The exponential decay rate for the 1st moment estimations. Should be in range (0.0, 1.0). Default: 0.9.

  • beta2 (float) – The exponential decay rate for the 2nd moment estimations. Should be in range (0.0, 1.0). Default: 0.999.

  • eps (float) – Term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-8.

  • use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

  • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.

  • weight_decay (Union[float, int]) – Weight decay (L2 penalty). Default: 0.0.

  • loss_scale (float) – A floating point value for the loss scale. Should be equal to or greater than 1. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

Tensor[bool], the value is True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If beta1, beta2, eps or loss_scale is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_locking or use_nesterov is not a bool.

  • ValueError – If loss_scale or eps is less than or equal to 0.

  • ValueError – If beta1, beta2 is not in range (0.0, 1.0).

  • ValueError – If weight_decay is less than 0.

Supported Platforms:

Ascend GPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.LazyAdam(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.LazyAdam(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad
>>> # centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
property target

The method is used to determine whether the parameter is updated on host or device. The input type is str and can only be ‘CPU’, ‘Ascend’ or ‘GPU’.

class tinyms.optimizers.AdamOffload(params, learning_rate=0.001, beta1=0.9, beta2=0.999, eps=1e-08, use_locking=False, use_nesterov=False, weight_decay=0.0, loss_scale=1.0)[源代码]

This optimizer will offload Adam optimizer to host CPU and keep parameters being updated on the device, to minimize the memory cost. Although that would bring about an increase of performance overhead, the optimizer could be used to run a larger model.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta_1 * m_{t} + (1 - \beta_1) * g \\ v_{t+1} = \beta_2 * v_{t} + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w_{t+1} = w_{t} - l * \frac{m_{t+1}}{\sqrt{v_{t+1}} + \epsilon} \end{array}\end{split}\]

\(m\) represents the 1st moment vector moment1, \(v\) represents the 2nd moment vector moment2, \(g\) represents gradients, \(l\) represents scaling factor, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t\) and \(beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents params, \(\epsilon\) represents eps.

注解

This optimizer only supports GRAPH_MODE currently.

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

To improve parameter groups performance, the customized order of parameters is supported.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” is in the keys, the value of the corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” is in the keys, the value of the corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” is in the keys, the value must be the order of parameters and the order will be followed in the optimizer. There are no other keys in the dict and the parameters which in the ‘order_params’ must be in one of group parameters.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use the dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 1e-3.

  • beta1 (float) – The exponential decay rate for the 1st moment estimations. Should be in range (0.0, 1.0). Default: 0.9.

  • beta2 (float) – The exponential decay rate for the 2nd moment estimations. Should be in range (0.0, 1.0). Default: 0.999.

  • eps (float) – Term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-8.

  • use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.

  • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.

  • weight_decay (float) – Weight decay (L2 penalty). It must be equal to or greater than 0. Default: 0.0.

  • loss_scale (float) – A floating point value for the loss scale. Should be greater than 0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

Tensor[bool], the value is True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If beta1, beta2, eps or loss_scale is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_locking or use_nesterov is not a bool.

  • ValueError – If loss_scale or eps is less than or equal to 0.

  • ValueError – If beta1, beta2 is not in range (0.0, 1.0).

  • ValueError – If weight_decay is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.AdamOffload(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.AdamOffload(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
class tinyms.optimizers.Lamb(*args, **kwargs)[源代码]

Lamb(Layer-wise Adaptive Moments optimizer for Batching training) Dynamic Learning Rate.

LAMB is an optimization algorithm employing a layerwise adaptive large batch optimization technique. Refer to the paper LARGE BATCH OPTIMIZATION FOR DEEP LEARNING: TRAINING BERT IN 76 MINUTES.

The LAMB optimizer aims to increase the training batch size without reducing the accuracy, and it supports adaptive element-by-element update and accurate layered correction.

The updating of parameters follows:

\[\begin{split}\begin{gather*} m_t = \beta_1 m_{t - 1}+ (1 - \beta_1)g_t\\ v_t = \beta_2 v_{t - 1} + (1 - \beta_2)g_t^2\\ m_t = \frac{m_t}{\beta_1^t}\\ v_t = \frac{v_t}{\beta_2^t}\\ r_t = \frac{m_t}{\sqrt{v_t}+\epsilon}\\ w_t = w_{t-1} -\eta_t \frac{\| w_{t-1} \|}{\| r_t + \lambda w_{t-1} \|} (r_t + \lambda w_{t-1}) \end{gather*}\end{split}\]

where \(m\) is the 1st moment, and \(v\) the 2nd moment, \(\eta\) the learning rate, \(\lambda\) the LAMB weight decay rate.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

There is usually no connection between a optimizer and mixed precision. But when FixedLossScaleManager is used and drop_overflow_update in FixedLossScaleManager is set to False, optimizer needs to set the ‘loss_scale’. As this optimizer has no argument of loss_scale, so loss_scale needs to be processed by other means, refer document LossScale to process loss_scale correctly.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float.

  • beta1 (float) – The exponential decay rate for the 1st moment estimations. Default: 0.9. Should be in range (0.0, 1.0).

  • beta2 (float) – The exponential decay rate for the 2nd moment estimations. Default: 0.999. Should be in range (0.0, 1.0).

  • eps (float) – Term added to the denominator to improve numerical stability. Default: 1e-6. Should be greater than 0.

  • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0. Should be equal to or greater than 0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

tuple[bool], all elements are True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If beta1, beta2 or eps is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • ValueError – If eps is less than or equal to 0.

  • ValueError – If beta1, beta2 is not in range (0.0, 1.0).

  • ValueError – If weight_decay is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.Lamb(params=net.trainable_params(), learning_rate=0.1)
>>>
>>> #2) Use parameter groups and set different values
>>> poly_decay_lr = learning_rate_schedule.PolynomialDecayLR(learning_rate=0.1, end_learning_rate=0.01,
...                                                    decay_steps=4, power = 0.5)
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': poly_decay_lr},
...                 {'order_params': net.trainable_params(0.01)}]
>>> optim = nn.Lamb(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use dynamic learning rate of poly decay learning rate and default
>>> # weight decay of 0.0 and grad centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
class tinyms.optimizers.SGD(*args, **kwargs)[源代码]

Implements stochastic gradient descent. Momentum is optional.

Introduction to SGD can be found at https://en.wikipedia.org/wiki/Stochastic_gradient_descent. Nesterov momentum is based on the formula from paper On the importance of initialization and momentum in deep learning.

\[v_{t+1} = u \ast v_{t} + gradient \ast (1-dampening)\]

If nesterov is True:

\[p_{t+1} = p_{t} - lr \ast (gradient + u \ast v_{t+1})\]

If nesterov is False:

\[p_{t+1} = p_{t} - lr \ast v_{t+1}\]

To be noticed, for the first step, \(v_{t+1} = gradient\)

Here : where p, v and u denote the parameters, accum, and momentum respectively.

注解

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 0.1.

  • momentum (float) – A floating point value the momentum. must be at least 0.0. Default: 0.0.

  • dampening (float) – A floating point value of dampening for momentum. must be at least 0.0. Default: 0.0.

  • weight_decay (float) – Weight decay (L2 penalty). It must be equal to or greater than 0. Default: 0.0.

  • nesterov (bool) – Enables the Nesterov momentum. If use nesterov, momentum must be positive, and dampening must equal to 0.0. Default: False.

  • loss_scale (float) – A floating point value for the loss scale, which must be larger than 0.0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

Tensor[bool], the value is True.

引发

ValueError – If the momentum, dampening or weight_decay value is less than 0.0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.SGD(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params,'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.SGD(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 default weight decay of 0.0 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad
>>> # centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
class tinyms.optimizers.FTRL(*args, **kwargs)[源代码]

Implements the FTRL algorithm with ApplyFtrl Operator.

FTRL is an online convex optimization algorithm that adaptively chooses its regularization function based on the loss functions. Refer to paper Adaptive Bound Optimization for Online Convex Optimization. Refer to paper Ad Click Prediction: a View from the Trenches for engineering document.

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ m_{t+1} = m_{t} + g^2 \\ u_{t+1} = u_{t} + g - \frac{m_{t+1}^\text{-p} - m_{t}^\text{-p}}{\alpha } * \omega_{t} \\ \omega_{t+1} = \begin{cases} \frac{(sign(u_{t+1}) * l1 - u_{t+1})}{\frac{m_{t+1}^\text{-p}}{\alpha } + 2 * l2 } & \text{ if } |u_{t+1}| > l1 \\ 0.0 & \text{ otherwise } \end{cases}\\ \end{array}\end{split}\]

\(m\) represents accum, \(g\) represents grads, \(t\) represents updating step, \(u\) represents linear, \(p\) represents lr_power, \(\alpha\) represents learning_rate, \(\omega\) represents params.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on all of the parameters.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

The sparse strategy is applied while the SparseGatherV2 operator being used for forward network. The sparse feature is under continuous development. If the sparse strategy wants to be executed on the host, set the target to the CPU.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Using different learning rate by separating parameters is currently not supported.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • initial_accum (float) – The starting value for accumulators, must be zero or positive values. Default: 0.1.

  • learning_rate (float) – The learning rate value, must be zero or positive, dynamic learning rate is currently not supported. Default: 0.001.

  • lr_power (float) – Learning rate power controls how the learning rate decreases during training, must be less than or equal to zero. Use fixed learning rate if lr_power is zero. Default: -0.5.

  • l1 (float) – l1 regularization strength, must be greater than or equal to zero. Default: 0.0.

  • l2 (float) – l2 regularization strength, must be greater than or equal to zero. Default: 0.0.

  • use_locking (bool) – If true, use locks for updating operation. Default: False.

  • loss_scale (float) – Value for the loss scale. It must be greater than 0.0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

  • weight_decay (Union[float, int]) – Weight decay value to multiply weight, must be zero or positive value. Default: 0.0.

Inputs:
  • grads (tuple[Tensor]) - The gradients of params in the optimizer, the shape is the same as the params in optimizer.

Outputs:

tuple[Parameter], the updated parameters, the shape is the same as params.

引发
  • TypeError – If initial_accum, learning_rate, lr_power, l1, l2 or loss_scale is not a float.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_nesterov is not a bool.

  • ValueError – If lr_power is greater than 0.

  • ValueError – If loss_scale is less than or equal to 0.

  • ValueError – If initial_accum, l1 or l2 is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.FTRL(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.FTRL(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use default weight decay of 0.0 and grad centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
property target

The method is used to determine whether the parameter is updated on host or device. The input type is str and can only be ‘CPU’, ‘Ascend’ or ‘GPU’.

class tinyms.optimizers.RMSProp(*args, **kwargs)[源代码]

Implements Root Mean Squared Propagation (RMSProp) algorithm.

Update params according to the RMSProp algorithm.

The equation is as follows:

\[s_{t+1} = \rho s_{t} + (1 - \rho)(\nabla Q_{i}(w))^2\]
\[m_{t+1} = \beta m_{t} + \frac{\eta} {\sqrt{s_{t+1} + \epsilon}} \nabla Q_{i}(w)\]
\[w = w - m_{t+1}\]

The first equation calculates moving average of the squared gradient for each weight. Then dividing the gradient by \(\sqrt{ms_{t+1} + \epsilon}\).

if centered is True:

\[g_{t+1} = \rho g_{t} + (1 - \rho)\nabla Q_{i}(w)\]
\[s_{t+1} = \rho s_{t} + (1 - \rho)(\nabla Q_{i}(w))^2\]
\[m_{t+1} = \beta m_{t} + \frac{\eta} {\sqrt{s_{t+1} - g_{t+1}^2 + \epsilon}} \nabla Q_{i}(w)\]
\[w = w - m_{t+1}\]

where \(w\) represents params, which will be updated. \(g_{t+1}\) is mean gradients, \(g_{t}\) is the last moment of \(g_{t+1}\). \(s_{t+1}\) is the mean square gradients, \(s_{t}\) is the last moment of \(s_{t+1}\), \(m_{t+1}\) is moment, the delta of w, \(m_{t}\) is the last moment of \(m_{t+1}\). \(\rho\) represents decay. \(\beta\) is the momentum term, represents momentum. \(\epsilon\) is a smoothing term to avoid division by zero, represents epsilon. \(\eta\) is learning rate, represents learning_rate. \(\nabla Q_{i}(w)\) is gradients, represents gradients.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 0.1.

  • decay (float) – Decay rate. Should be equal to or greater than 0. Default: 0.9.

  • momentum (float) – Hyperparameter of type float, means momentum for the moving average. Should be equal to or greater than 0. Default: 0.0.

  • epsilon (float) – Term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-10.

  • use_locking (bool) – Whether to enable a lock to protect the variable and accumulation tensors from being updated. Default: False.

  • centered (bool) – If true, gradients are normalized by the estimated variance of the gradient. Default: False.

  • loss_scale (float) – A floating point value for the loss scale. Should be greater than 0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

  • weight_decay (Union[float, int]) – Weight decay (L2 penalty). Should be equal to or greater than 0. Default: 0.0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

Tensor[bool], the value is True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If decay, momentum, epsilon or loss_scale is not a float.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_locking or centered is not a bool.

  • ValueError – If epsilon is less than or equal to 0.

  • ValueError – If decay or momentum is less than 0.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.RMSProp(params=net.trainable_params(), learning_rate=0.1)
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.RMSProp(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad
>>> # centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
class tinyms.optimizers.ProximalAdagrad(*args, **kwargs)[源代码]

Implements the ProximalAdagrad algorithm with ApplyProximalAdagrad Operator.

ProximalAdagrad is an online Learning and Stochastic Optimization. Refer to paper Efficient Learning using Forward-Backward Splitting.

\[accum_{t+1} = accum_{t} + grad * grad\]
\[\text{prox_v} = var_{t} - lr * grad * \frac{1}{\sqrt{accum_{t+1}}}\]
\[var_{t+1} = \frac{sign(\text{prox_v})}{1 + lr * l2} * \max(\left| \text{prox_v} \right| - lr * l1, 0)\]

Here : where grad, lr, var, accum and t denote the gradients, learning_rate, params and accumulation and current step respectively.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

The sparse strategy is applied while the SparseGatherV2 operator being used for forward network. The sparse feature is under continuous development. If the sparse strategy wants to be executed on the host, set the target to the CPU.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • accum (float) – The starting value for accumulators, must be zero or positive values. Default: 0.1.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 0.001.

  • l1 (float) – l1 regularization strength, must be greater than or equal to zero. Default: 0.0.

  • l2 (float) – l2 regularization strength, must be greater than or equal to zero. Default: 0.0.

  • use_locking (bool) – If true, use locks for updating operation. Default: False.

  • loss_scale (float) – Value for the loss scale. It must be greater than 0.0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

  • weight_decay (Union[float, int]) – Weight decay value to multiply weight, must be zero or positive value. Default: 0.0.

Inputs:
  • grads (tuple[Tensor]) - The gradients of params in the optimizer, the shape is the same as the params in optimizer.

Outputs:

Tensor[bool], the value is True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If accum, l1, l2 or loss_scale is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • ValueError – If loss_scale is less than or equal to 0.

  • ValueError – If accum, l1, l2 or weight_decay is less than 0.

Supported Platforms:

Ascend

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.ProximalAdagrad(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.ProximalAdagrad(group_params, learning_rate=0.1, weight_decay=0.0)
 >>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad
>>> # centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
property target

The method is used to determine whether the parameter is updated on host or device. The input type is str and can only be ‘CPU’, ‘Ascend’ or ‘GPU’.

class tinyms.optimizers.Adagrad(*args, **kwargs)[源代码]

Implements the Adagrad algorithm with ApplyAdagrad Operator.

Adagrad is an online Learning and Stochastic Optimization. Refer to paper Efficient Learning using Forward-Backward Splitting. The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ h_{t+1} = h_{t} + g\\ w_{t+1} = w_{t} - lr*\frac{1}{\sqrt{h_{t+1}}}*g \end{array}\end{split}\]

\(h\) represents the cumulative sum of gradient squared, \(g\) represents gradients. \(lr\) represents learning_rate, \(w\) represents params.

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

参数
  • params (Union[list[Parameter], list[dict]]) –

    When the params is a list of Parameter which will be updated, the element in params must be class Parameter. When the params is a list of dict, the “params”, “lr”, “weight_decay” and “order_params” are the keys can be parsed.

    • params: Required. The value must be a list of Parameter.

    • lr: Optional. If “lr” in the keys, the value of corresponding learning rate will be used. If not, the learning_rate in the API will be used.

    • weight_decay: Optional. If “weight_decay” in the keys, the value of corresponding weight decay will be used. If not, the weight_decay in the API will be used.

    • order_params: Optional. If “order_params” in the keys, the value must be the order of parameters and the order will be followed in optimizer. There are no other keys in the dict and the parameters which in the value of ‘order_params’ must be in one of group parameters.

    • grad_centralization: Optional. The data type of “grad_centralization” is Bool. If “grad_centralization” is in the keys, the set value will be used. If not, the grad_centralization is False by default. This parameter only works on the convolution layer.

  • accum (float) – The starting value for accumulators, must be zero or positive values. Default: 0.1.

  • learning_rate (Union[float, Tensor, Iterable, LearningRateSchedule]) – A value or a graph for the learning rate. When the learning_rate is an Iterable or a Tensor in a 1D dimension, use dynamic learning rate, then the i-th step will take the i-th value as the learning rate. When the learning_rate is LearningRateSchedule, use dynamic learning rate, the i-th learning rate will be calculated during the process of training according to the formula of LearningRateSchedule. When the learning_rate is a float or a Tensor in a zero dimension, use fixed learning rate. Other cases are not supported. The float learning rate must be equal to or greater than 0. If the type of learning_rate is int, it will be converted to float. Default: 0.001.

  • update_slots (bool) – If true, update accumulation. Default: True.

  • loss_scale (float) – Value for the loss scale. It must be greater than 0.0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

  • weight_decay (Union[float, int]) – Weight decay value to multiply weight, must be zero or positive value. Default: 0.0.

Inputs:
  • grads (tuple[Tensor]) - The gradients of params in the optimizer, the shape is the same as the params in optimizer.

Outputs:

Tensor[bool], the value is True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If accum or loss_scale is not a float.

  • TypeError – If update_slots is not a bool.

  • TypeError – If weight_decay is neither float nor int.

  • ValueError – If loss_scale is less than or equal to 0.

  • ValueError – If accum or weight_decay is less than 0.

Supported Platforms:

Ascend CPU GPU

实际案例

>>> net = Net()
>>> #1) All parameters use the same learning rate and weight decay
>>> optim = nn.Adagrad(params=net.trainable_params())
>>>
>>> #2) Use parameter groups and set different values
>>> conv_params = list(filter(lambda x: 'conv' in x.name, net.trainable_params()))
>>> no_conv_params = list(filter(lambda x: 'conv' not in x.name, net.trainable_params()))
>>> group_params = [{'params': conv_params, 'weight_decay': 0.01, 'grad_centralization':True},
...                 {'params': no_conv_params, 'lr': 0.01},
...                 {'order_params': net.trainable_params()}]
>>> optim = nn.Adagrad(group_params, learning_rate=0.1, weight_decay=0.0)
>>> # The conv_params's parameters will use default learning rate of 0.1 and weight decay of 0.01 and grad
>>> # centralization of True.
>>> # The no_conv_params's parameters will use learning rate of 0.01 and default weight decay of 0.0 and grad
>>> # centralization of False.
>>> # The final parameters order in which the optimizer will be followed is the value of 'order_params'.
>>>
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
tinyms.optimizers.thor(net, learning_rate, damping, momentum, weight_decay=0.0, loss_scale=1.0, batch_size=32, use_nesterov=False, decay_filter=<function <lambda>>, split_indices=None, enable_clip_grad=False, frequency=100)[源代码]

Updates gradients by second-order algorithm–THOR.

Trace-based Hardware-driven layer-ORiented Natural Gradient Descent Computation (THOR) algorithm is proposed in:

THOR: Trace-based Hardware-driven layer-ORiented Natural Gradient Descent Computation

The updating formulas are as follows,

\[\begin{split}\begin{array}{ll} \\ A_i = a_i{a_i}^T \\ G_i = D_{s_i}{ D_{s_i}}^T \\ m_i = \beta * m_i + ({G_i^{(k)}}+\lambda I)^{-1}) g_i ({\overline A_{i-1}^{(k)}}+\lambda I)^{-1} \\ w_i = w_i - \alpha * m_i \\ \end{array}\end{split}\]

\(D_{s_i}\) represents the derivative of the loss function of the output of the i-th layer, \(a_{i-1}\) represents the input of i-th layer,and which is the activations of previous layer, \(\beta\) represents momentum, \(I\) represents the identity matrix, \(\overline A\) represents the transpose of matrix A, \(\lambda\) represents ‘damping’, \(g_i\) represents gradients of the i-th layer, \(\otimes\) represents Kronecker product, \(\alpha\) represents ‘learning rate’

注解

When separating parameter groups, the weight decay in each group will be applied on the parameters if the weight decay is positive. When not separating parameter groups, the weight_decay in the API will be applied on the parameters without ‘beta’ or ‘gamma’ in their names if weight_decay is positive.

When separating parameter groups, if you want to centralize the gradient, set grad_centralization to True, but the gradient centralization can only be applied to the parameters of the convolution layer. If the parameters of the non convolution layer are set to True, an error will be reported.

To improve parameter groups performance, the customized order of parameters can be supported.

参数
  • net (Cell) – The training network.

  • learning_rate (Tensor) – A value for the learning rate.

  • damping (Tensor) – A value for the damping.

  • momentum (float) – Hyper-parameter of type float, means momentum for the moving average. It must be at least 0.0.

  • weight_decay (int, float) – Weight decay (L2 penalty). It must be equal to or greater than 0.0. Default: 0.0.

  • loss_scale (float) – A value for the loss scale. It must be greater than 0.0. In general, use the default value. Default: 1.0.

  • batch_size (int) – The size of a batch. Default: 32

  • use_nesterov (bool) – Enable Nesterov momentum. Default: False.

  • decay_filter (function) – A function to determine which layers the weight decay applied to. And it only works when the weight_decay > 0. Default: lambda x: x.name not in []

  • split_indices (list) – Set allreduce fusion strategy by A/G layer indices . Only works when distributed computing. ResNet50 as an example, there are 54 layers of A/G respectively, when split_indices is set to [26, 53], it means A/G is divided into two groups to allreduce, one is 0~26 layer, and the other is 27~53. Default: None

  • enable_clip_grad (bool) – Whether to clip the gradients. Default: False

  • frequency (int) – The update interval of A/G and $A^{-1}/G^{-1}$. When frequency equals N (N is greater than 1), A/G and $A^{-1}/G^{-1}$ will be updated every N steps, and other steps will use the stale A/G and $A^{-1}/G^{-1}$ to update weights. Default: 100.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

tuple[bool], all elements are True.

引发
  • TypeError – If learning_rate is not Tensor.

  • TypeError – If loss_scale,`momentum` or frequency is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_nesterov is not a bool.

  • ValueError – If loss_scale is less than or equal to 0.

  • ValueError – If weight_decay or momentum is less than 0.

  • ValueError – If frequency is not int.

  • ValueError – If frequency is less than 2.

Supported Platforms:

Ascend GPU

实际案例

>>> from mindspore.nn import thor
>>> from mindspore import Model
>>> from mindspore import FixedLossScaleManager
>>> from mindspore.train.callback import LossMonitor
>>> from mindspore.train.train_thor import ConvertModelUtils
>>> from mindspore import nn
>>> from mindspore import Tensor
>>>
>>> net = Net()
>>> dataset = create_dataset()
>>> temp = Tensor([4e-4, 1e-4, 1e-5, 1e-5], mstype.float32)
>>> optim = thor(net, learning_rate=temp, damping=temp, momentum=0.9, loss_scale=128, frequency=4)
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
>>> loss_scale = FixedLossScaleManager(128, drop_overflow_update=False)
>>> model = Model(net, loss_fn=loss, optimizer=optim, loss_scale_manager=loss_scale, metrics={'acc'},
...               amp_level="O2", keep_batchnorm_fp32=False)
>>> model = ConvertModelUtils.convert_to_thor_model(model=model, network=net, loss_fn=loss, optimizer=optim,
...                                                 loss_scale_manager=loss_scale, metrics={'acc'},
...                                                 amp_level="O2", keep_batchnorm_fp32=False)
>>> loss_cb = LossMonitor()
>>> model.train(1, dataset, callbacks=loss_cb, sink_size=4, dataset_sink_mode=True)
class tinyms.optimizers.AdaFactor(*args, **kwargs)[源代码]

Updates gradients by the Adaptive Learning Rates with Sublinear Memory Cost (Adafactor) algorithm.

The Adafactor algorithm is proposed in Adafactor: Adafactor: Adaptive Learning Rates with Sublinear Memory Cost.

警告

This is an experimental prototype that is subject to change and/or deletion.

Adafactor for weight vector are as follows,

\[\begin{split}\begin{array}{l} \\ \alpha_{t}=\max \left(\epsilon_{2}, \operatorname{RMS}\left(X_{t-1}\right)\right) \rho_{t} \\ G_{t}=\nabla f_{t}\left(X_{t-1}\right) \\ \hat{V}_{t}=\hat{\beta}_{2} \hat{V}_{t-1}+\left(1-\hat{\beta}_{2_{t}}\right)\left(G_{t}^{2}+ \\ \epsilon_{1} 1_{n}\right) \\ U_{t}=G_{t} / \sqrt{\hat{V}_{t}} \\ \hat{U}_{t}=U_{t} / \max \left(1, \operatorname{RMS}\left(U_{t}\right) / d\right) \\ X_{t}=X_{t-1}-\alpha_{t} \hat{U}_{t} \end{array}\end{split}\]

Adafactor for weight matrices are as follows,

\[\begin{split}\begin{array}{l} \\ \alpha_{t}=\max \left(\epsilon_{2}, \operatorname{RMS}\left(X_{t-1}\right)\right) \rho_{t} \\ G_{t}=\nabla f_{t}\left(X_{t-1}\right) \\ R_{t}=\hat{\beta}_{2 t} R_{t-1}+\left(1-\hat{\beta}_{2 t}\right)\left(G_{t}^{2}+ \\ \epsilon_{1} 1_{n} 1_{m}^{\top}\right) 1_{m} \\ C_{t}=\hat{\beta}_{2 t} C_{t-1}+\left(1-\hat{\beta}_{2 t}\right) 1_{n}^{\top}\left(G_{t}^{2}+ \\ \epsilon_{1} 1_{n} 1_{m}^{\top}\right) \\ \hat{V}_{t}=R_{t} C_{t} / 1_{n}^{\top} R_{t} \\ U_{t}=G_{t} / \sqrt{\hat{V}_{t}} \\ \hat{U}_{t}=U_{t} / \max \left(1, \operatorname{RMS}\left(U_{t}\right) / d\right) \\ X_{t}=X_{t-1}-\alpha_{t} U_{t} \end{array}\end{split}\]

Where RMS is:

\[\begin{split}\operatorname{RMS}\left(U_{t}\right)=\operatorname{RMS}_{x \in X}\left(u_{x t}\right)= \\ \sqrt{\operatorname{Mean}_{x \in X}\left(\frac{\left(g_{x t}\right)^{2}}{\hat{v}_{x t}}\right)}\end{split}\]

\(x\) is each individual parameter, \(t\) is assumed to be the current number of steps, \(a_{t}\) is the learning rate, \(f(X)\) is the loss function, \(\epsilon1\) and \(\epsilon2\) is a small positive number to prevent errors, \(d\) is the clipping threshold, \(\beta_{2}\) is the moment decay, \(\rho\) is the relative step size, \(R\) is the running averages of the row sums of the squared gradient, \(C\) is the running averages of the column sums of the squared gradient.

注解

The learning rate depending of this optimizer will be control by the scale_parameter, relative_step and warmup_init options. To use a manual (external) learning rate schedule, it should be set scale_parameter=False and relative_step=False.

If parameters is not used in the network, please do not add it to the optimizer, otherwise the calculation result will be abnormal.

To improve parameter groups performance, the customized order of parameters is supported.

参数
  • params (Union[list[Parameter], list[dict]]) – When the params is a list of Parameter which will be updated, the element in params must be class Parameter.

  • learning_rate (Union[float, Tensor]) – A value or a graph for the learning rate. When the learning_rate is a Tensor in a 1D dimension. If the type of learning_rate is int, it will be converted to float. Default: None.

  • eps (float) – The regularization constans for square gradient and parameter scale respectively. default: (1e-30, 1e-3)

  • clip_threshold (Union[float, Tensor]) – The threshold of root mean square of final gradient update. default: 1.0

  • decay_rate (Union[float, Tensor]) – The coefficient used to compute running averages of square gradient. default: 0.8

  • beta1 (float) – The coefficient to computing running averages of gradient. Should be in range (0.0, 1.0). Default: None.

  • weight_decay (float) – Weight decay (L2 penalty). It must be equal to or greater than 0. Default: 0.0.

  • scale_parameter (bool) – If True, learning rate is scaled by root mean square of parameter. default: True

  • relative_step (bool) – If True, time-dependent learning rate is computed instead of external learning rate. default: True

  • warmup_init (bool) – The time-dependent learning rate computation depends on whether warm-up initialization is being used. default: False

  • compression (bool) – If True, the data type of the running averages exponent will be compression to float16. default: False

  • loss_scale (float) – A floating point value for the loss scale. Should be greater than 0. In general, use the default value. Only when FixedLossScaleManager is used for training and the drop_overflow_update in FixedLossScaleManager is set to False, then this value needs to be the same as the loss_scale in FixedLossScaleManager. Refer to class mindspore.FixedLossScaleManager for more details. Default: 1.0.

Inputs:
  • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.

Outputs:

Tensor[bool], the value is True.

引发
  • TypeError – If learning_rate is not one of int, float, Tensor, Iterable, LearningRateSchedule.

  • TypeError – If element of parameters is neither Parameter nor dict.

  • TypeError – If beta1, beta2, eps or loss_scale is not a float.

  • TypeError – If weight_decay is neither float nor int.

  • TypeError – If use_locking or use_nesterov is not a bool.

  • ValueError – If loss_scale or eps is less than or equal to 0.

  • ValueError – If beta1, beta2 is not in range (0.0, 1.0).

  • ValueError – If weight_decay is less than 0.

Supported Platforms:

Ascend

实际案例

>>> net = Net()
>>> #1) Parameters use the default learning rate with None and weight decay with 0.
>>> optim = nn.AdaFactor(params=net.trainable_params())
>>>
>>> #2) Use parameter groups
>>> all_params = net.trainable_params()
>>> group_params = [{'params': [all_params[0]]}, {'params': [all_params[1]]}]
>>> optim = nn.AdaFactor(group_params, learning_rate=0.1, weight_decay=0.0, relative_step=False)
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=optim)
init_ada_factor_state(beta1)[源代码]

init adafactor variables

property target

The method is used to determine whether the parameter is updated on host or device. The input type is str and can only be ‘CPU’, ‘Ascend’ or ‘GPU’.

tinyms.callbacks

Callback related classes and functions in model training phase.

class tinyms.callbacks.LossTimeMonitor(lr_init=None)[源代码]

Monitor loss and time.

参数

lr_init (numpy.ndarray) – Train learning rate. Default: None.

返回

None

实际案例

>>> from tinyms import Tensor
>>> from tinyms.callbacks import LossTimeMonitor
>>>
>>> LossTimeMonitor(lr_init=Tensor([0.05] * 100).asnumpy())
class tinyms.callbacks.LossTimeMonitorV2[源代码]

Monitor loss and time version 2.0. This version will not show learning rate.

Args:

返回

None

实际案例

>>> from tinyms.callbacks import LossTimeMonitorV2
>>>
>>> LossTimeMonitorV2()
class tinyms.callbacks.BertLossCallBack(dataset_size=1)[源代码]

Monitor the loss in training. If the loss in NAN or INF terminating training.

参数

dataset_size (int) – Print loss every times. Default: 1.

返回

None

实际案例

>>> from tinyms.callbacks import BertLossCallBack
>>>
>>> BertLossCallBack(dataset_size=1)
step_end(run_context)[源代码]

Print loss after each step

class tinyms.callbacks.Callback[源代码]

Abstract base class used to build a callback class. Callbacks are context managers which will be entered and exited when passing into the Model. You can use this mechanism to initialize and release resources automatically.

Callback function will execute some operations in the current step or epoch.

It holds the information of the model. Such as network, train_network, epoch_num, batch_num,

loss_fn, optimizer, parallel_mode, device_number, list_callback, cur_epoch_num, cur_step_num, dataset_sink_mode, net_outputs and so on.

实际案例

>>> from mindspore import Model, nn
>>> from mindspore.train.callback import Callback
>>> class Print_info(Callback):
...     def step_end(self, run_context):
...         cb_params = run_context.original_args()
...         print("step_num: ", cb_params.cur_step_num)
>>>
>>> print_cb = Print_info()
>>> dataset = create_custom_dataset()
>>> net = Net()
>>> 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)
>>> model.train(1, dataset, callbacks=print_cb)
step_num: 1
begin(run_context)[源代码]

Called once before the network executing.

参数

run_context (RunContext) – Include some information of the model.

end(run_context)[源代码]

Called once after network training.

参数

run_context (RunContext) – Include some information of the model.

epoch_begin(run_context)[源代码]

Called before each epoch beginning.

参数

run_context (RunContext) – Include some information of the model.

epoch_end(run_context)[源代码]

Called after each epoch finished.

参数

run_context (RunContext) – Include some information of the model.

step_begin(run_context)[源代码]

Called before each step beginning.

参数

run_context (RunContext) – Include some information of the model.

step_end(run_context)[源代码]

Called after each step finished.

参数

run_context (RunContext) – Include some information of the model.

class tinyms.callbacks.LossMonitor(per_print_times=1)[源代码]

Monitor the loss in training.

If the loss is NAN or INF, it will terminate training.

注解

If per_print_times is 0, do not print loss.

参数

per_print_times (int) – Print the loss every seconds. Default: 1.

引发

ValueError – If per_print_times is not an integer or less than zero.

step_end(run_context)[源代码]

Print training loss at the end of step.

参数

run_context (RunContext) – Context of the train running.

class tinyms.callbacks.TimeMonitor(data_size=None)[源代码]

Monitor the time in training.

参数

data_size (int) – How many steps are the intervals between print information each time. if the program get batch_num during training, data_size will be set to batch_num, otherwise data_size will be used. Default: None.

引发

ValueError – If data_size is not positive int.

epoch_begin(run_context)[源代码]

Record time at the begin of epoch.

参数

run_context (RunContext) – Context of the process running.

epoch_end(run_context)[源代码]

Print process cost time at the end of epoch.

参数

run_context (RunContext) – Context of the process running.

class tinyms.callbacks.ModelCheckpoint(prefix='CKP', directory=None, config=None)[源代码]

The checkpoint callback class.

It is called to combine with train process and save the model and network parameters after training.

注解

In the distributed training scenario, please specify different directories for each training process to save the checkpoint file. Otherwise, the training may fail.

参数
  • prefix (str) – The prefix name of checkpoint files. Default: “CKP”.

  • directory (str) – The path of the folder which will be saved in the checkpoint file. By default, the file is saved in the current directory. Default: None.

  • config (CheckpointConfig) – Checkpoint strategy configuration. Default: None.

引发
  • ValueError – If the prefix is invalid.

  • TypeError – If the config is not CheckpointConfig type.

end(run_context)[源代码]

Save the last checkpoint after training finished.

参数

run_context (RunContext) – Context of the train running.

property latest_ckpt_file_name

Return the latest checkpoint path and file name.

step_end(run_context)[源代码]

Save the checkpoint at the end of step.

参数

run_context (RunContext) – Context of the train running.

class tinyms.callbacks.SummaryCollector(summary_dir, collect_freq=10, collect_specified_data=None, keep_default_action=True, custom_lineage_data=None, collect_tensor_freq=None, max_file_size=None, export_options=None)[源代码]

SummaryCollector can help you to collect some common information.

It can help you to collect loss, learning late, computational graph and so on. SummaryCollector also enables the summary operator to collect data to summary files.

注解

  1. Multiple SummaryCollector instances in callback list are not allowed.

  2. Not all information is collected at the training phase or at the eval phase.

  3. SummaryCollector always record the data collected by the summary operator.

  4. SummaryCollector only supports Linux systems.

参数
  • summary_dir (str) – The collected data will be persisted to this directory. If the directory does not exist, it will be created automatically.

  • collect_freq (int) – Set the frequency of data collection, it should be greater then zero, and the unit is step. If a frequency is set, we will collect data when (current steps % freq) equals to 0, and the first step will be collected at any time. It is important to note that if the data sink mode is used, the unit will become the epoch. It is not recommended to collect data too frequently, which can affect performance. Default: 10.

  • collect_specified_data (Union[None, dict]) –

    Perform custom operations on the collected data. By default, if set to None, all data is collected as the default behavior. You can customize the collected data with a dictionary. For example, you can set {‘collect_metric’: False} to control not collecting metrics. The data that supports control is shown below. Default: None.

    • collect_metric (bool): Whether to collect training metrics, currently only the loss is collected. The first output will be treated as the loss and it will be averaged. Optional: True/False. Default: True.

    • collect_graph (bool): Whether to collect the computational graph. Currently, only training computational graph is collected. Optional: True/False. Default: True.

    • collect_train_lineage (bool): Whether to collect lineage data for the training phase, this field will be displayed on the lineage page of Mindinsight. Optional: True/False. Default: True.

    • collect_eval_lineage (bool): Whether to collect lineage data for the evaluation phase, this field will be displayed on the lineage page of Mindinsight. Optional: True/False. Default: True.

    • collect_input_data (bool): Whether to collect dataset for each training. Currently only image data is supported. If there are multiple columns of data in the dataset, the first column should be image data. Optional: True/False. Default: True.

    • collect_dataset_graph (bool): Whether to collect dataset graph for the training phase. Optional: True/False. Default: True.

    • histogram_regular (Union[str, None]): Collect weight and bias for parameter distribution page and displayed in MindInsight. This field allows regular strings to control which parameters to collect. It is not recommended to collect too many parameters at once, as it can affect performance. Note that if you collect too many parameters and run out of memory, the training will fail. Default: None, it means only the first five parameters are collected.

  • keep_default_action (bool) – This field affects the collection behavior of the ‘collect_specified_data’ field. True: it means that after specified data is set, non-specified data is collected as the default behavior. False: it means that after specified data is set, only the specified data is collected, and the others are not collected. Optional: True/False, Default: True.

  • custom_lineage_data (Union[dict, None]) – Allows you to customize the data and present it on the MingInsight lineage page. In the custom data, the type of the key supports str, and the type of value supports str, int and float. Default: None, it means there is no custom data.

  • collect_tensor_freq (Optional[int]) – The same semantics as the collect_freq, but controls TensorSummary only. Because TensorSummary data is too large to be compared with other summary data, this parameter is used to reduce its collection. By default, The maximum number of steps for collecting TensorSummary data is 20, but it will not exceed the number of steps for collecting other summary data. For example, given collect_freq=10, when the total steps is 600, TensorSummary will be collected 20 steps, while other summary data 61 steps, but when the total steps is 20, both TensorSummary and other summary will be collected 3 steps. Also note that when in parallel mode, the total steps will be split evenly, which will affect the number of steps TensorSummary will be collected. Default: None, which means to follow the behavior as described above.

  • max_file_size (Optional[int]) – The maximum size in bytes of each file that can be written to the disk. For example, to write not larger than 4GB, specify max_file_size=4*1024**3. Default: None, which means no limit.

  • export_options (Union[None, dict]) –

    Perform custom operations on the export data. Note that the size of export files is not limited by the max_file_size. You can customize the export data with a dictionary. For example, you can set {‘tensor_format’: ‘npy’} to export tensor as npy file. The data that supports control is shown below. Default: None, it means that the data is not exported.

    • tensor_format (Union[str, None]): Customize the export tensor format. Supports [“npy”, None]. Default: None, it means that the tensor is not exported.

      • npy: export tensor as npy file.

引发
  • ValueError – If the parameter value is not expected.

  • TypeError – If the parameter type is not expected.

  • RuntimeError – If an error occurs during data collection.

实际案例

>>> import mindspore.nn as nn
>>> from mindspore import context
>>> from mindspore.train.callback import SummaryCollector
>>> from mindspore import Model
>>> from mindspore.nn import Accuracy
>>>
>>> if __name__ == '__main__':
...     # If the device_target is GPU, set the device_target to "GPU"
...     context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
...     mnist_dataset_dir = '/path/to/mnist_dataset_directory'
...     # The detail of create_dataset method shown in model_zoo.official.cv.lenet.src.dataset.py
...     ds_train = create_dataset(mnist_dataset_dir, 32)
...     # The detail of LeNet5 shown in model_zoo.official.cv.lenet.src.lenet.py
...     network = LeNet5(10)
...     net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
...     net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
...     model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}, amp_level="O2")
...
...     # Simple usage:
...     summary_collector = SummaryCollector(summary_dir='./summary_dir')
...     model.train(1, ds_train, callbacks=[summary_collector], dataset_sink_mode=False)
...
...     # Do not collect metric and collect the first layer parameter, others are collected by default
...     specified={'collect_metric': False, 'histogram_regular': '^conv1.*'}
...     summary_collector = SummaryCollector(summary_dir='./summary_dir', collect_specified_data=specified)
...     model.train(1, ds_train, callbacks=[summary_collector], dataset_sink_mode=False)
class tinyms.callbacks.CheckpointConfig(save_checkpoint_steps=1, save_checkpoint_seconds=0, keep_checkpoint_max=5, keep_checkpoint_per_n_minutes=0, integrated_save=True, async_save=False, saved_network=None, append_info=None, enc_key=None, enc_mode='AES-GCM')[源代码]

The configuration of model checkpoint.

注解

During the training process, if dataset is transmitted through the data channel, It is suggested to set ‘save_checkpoint_steps’ to an integer multiple of loop_size. Otherwise, the time to save the checkpoint may be biased. It is recommended to set only one save strategy and one keep strategy at the same time. If both save_checkpoint_steps and save_checkpoint_seconds are set, save_checkpoint_seconds will be invalid. If both keep_checkpoint_max and keep_checkpoint_per_n_minutes are set, keep_checkpoint_per_n_minutes will be invalid.

参数
  • save_checkpoint_steps (int) – Steps to save checkpoint. Default: 1.

  • save_checkpoint_seconds (int) – Seconds to save checkpoint. Can’t be used with save_checkpoint_steps at the same time. Default: 0.

  • keep_checkpoint_max (int) – Maximum number of checkpoint files can be saved. Default: 5.

  • keep_checkpoint_per_n_minutes (int) – Save the checkpoint file every keep_checkpoint_per_n_minutes minutes. Can’t be used with keep_checkpoint_max at the same time. Default: 0.

  • integrated_save (bool) – Whether to merge and save the split Tensor in the automatic parallel scenario. Integrated save function is only supported in automatic parallel scene, not supported in manual parallel. Default: True.

  • async_save (bool) – Whether asynchronous execution saves the checkpoint to a file. Default: False.

  • saved_network (Cell) – Network to be saved in checkpoint file. If the saved_network has no relation with the network in training, the initial value of saved_network will be saved. Default: None.

  • append_info (list) – The information save to checkpoint file. Support “epoch_num”, “step_num” and dict. The key of dict must be str, the value of dict must be one of int float and bool. Default: None.

  • enc_key (Union[None, bytes]) – Byte type key used for encryption. If the value is None, the encryption is not required. Default: None.

  • enc_mode (str) – This parameter is valid only when enc_key is not set to None. Specifies the encryption mode, currently supports ‘AES-GCM’ and ‘AES-CBC’. Default: ‘AES-GCM’.

引发

ValueError – If input parameter is not the correct type.

实际案例

>>> from mindspore import Model, nn
>>> from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
>>>
>>> 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()
...
...     def construct(self, x):
...         x = self.max_pool2d(self.relu(self.conv1(x)))
...         x = self.max_pool2d(self.relu(self.conv2(x)))
...         x = self.flatten(x)
...         x = self.relu(self.fc1(x))
...         x = self.relu(self.fc2(x))
...         x = self.fc3(x)
...         return x
>>>
>>> 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)
>>> data_path = './MNIST_Data'
>>> dataset = create_dataset(data_path)
>>> config = CheckpointConfig(saved_network=net)
>>> ckpoint_cb = ModelCheckpoint(prefix='LeNet5', directory='./checkpoint', config=config)
>>> model.train(10, dataset, callbacks=ckpoint_cb)
property append_dict

Get the value of append_dict.

property async_save

Get the value of _async_save.

property enc_key

Get the value of _enc_key

property enc_mode

Get the value of _enc_mode

get_checkpoint_policy()[源代码]

Get the policy of checkpoint.

property integrated_save

Get the value of _integrated_save.

property keep_checkpoint_max

Get the value of _keep_checkpoint_max.

property keep_checkpoint_per_n_minutes

Get the value of _keep_checkpoint_per_n_minutes.

property save_checkpoint_seconds

Get the value of _save_checkpoint_seconds.

property save_checkpoint_steps

Get the value of _save_checkpoint_steps.

property saved_network

Get the value of _saved_network

class tinyms.callbacks.RunContext(original_args)[源代码]

Provide information about the model.

Provide information about original request to model function. Callback objects can stop the loop by calling request_stop() of run_context.

参数

original_args (dict) – Holding the related information of model.

get_stop_requested()[源代码]

Return whether a stop is requested or not.

返回

bool, if true, model.train() stops iterations.

original_args()[源代码]

Get the _original_args object.

返回

Dict, an object that holds the original arguments of model.

request_stop()[源代码]

Set stop requirement during training.

Callbacks can use this function to request stop of iterations. model.train() checks whether this is called or not.

class tinyms.callbacks.LearningRateScheduler(learning_rate_function)[源代码]

Change the learning_rate during training.

参数

learning_rate_function (Function) – The function about how to change the learning rate during training.

实际案例

>>> from mindspore import Model
>>> from mindspore.train.callback import LearningRateScheduler
>>> import mindspore.nn as nn
...
>>> def learning_rate_function(lr, cur_step_num):
...     if cur_step_num%1000 == 0:
...         lr = lr*0.1
...     return lr
...
>>> lr = 0.1
>>> momentum = 0.9
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> optim = nn.Momentum(net.trainable_params(), learning_rate=lr, momentum=momentum)
>>> model = Model(net, loss_fn=loss, optimizer=optim)
...
>>> dataset = create_custom_dataset("custom_dataset_path")
>>> model.train(1, dataset, callbacks=[LearningRateScheduler(learning_rate_function)],
...             dataset_sink_mode=False)
step_end(run_context)[源代码]

Change the learning_rate at the end of step.

参数

run_context (RunContext) – Context of the train running.

tinyms.metrics

Metrics module provides functions to measure the performance of the machine learning models on the evaluation dataset. It’s used to choose the best model.

class tinyms.metrics.AUCMetric[源代码]

Calculates the auc value. Implement auc metric method.

Computes the Area Under the Curve (AUC) using the trapezoidal rule. This is a general function, given points on a curve. For computing the area under the ROC-curve.

参数
  • x (Union[np.array, list]) – From the ROC curve(fpr), np.array with false positive rates. If multiclass, this is a list of such np.array, one for each class. The shape \((N)\).

  • y (Union[np.array, list]) – From the ROC curve(tpr), np.array with true positive rates. If multiclass, this is a list of such np.array, one for each class. The shape \((N)\).

  • reorder (boolean) – If True, assume that the curve is ascending in the case of ties, as for an ROC curve. If the curve is non-ascending, the result will be wrong. Default: False.

返回

Compute result.

返回类型

area (float)

实际案例

>>> from tinyms.metrics import AUCMetric
>>>
>>> metric = AUCMetric()
clear()[源代码]

Clear the internal evaluation result.

tinyms.metrics.names()[源代码]

Gets the names of the metric methods.

返回

List, the name list of metric methods.

tinyms.metrics.get_metric_fn(name, *args, **kwargs)[源代码]

Gets the metric method based on the input name.

参数
  • name (str) – The name of metric method. Refer to the ‘__factory__’ object for the currently supported metrics.

  • args – Arguments for the metric function.

  • kwargs – Keyword arguments for the metric function.

返回

Metric object, class instance of the metric method.

实际案例

>>> metric = nn.get_metric_fn('precision', eval_type='classification')
class tinyms.metrics.Accuracy(eval_type='classification')[源代码]

Calculates the accuracy for classification and multilabel data.

The accuracy class creates two local variables, the correct number and the total number that are used to compute the frequency with which y_pred matches y. This frequency is ultimately returned as the accuracy: an idempotent operation that simply divides the correct number by the total number.

\[\text{accuracy} =\frac{\text{true_positive} + \text{true_negative}} {\text{true_positive} + \text{true_negative} + \text{false_positive} + \text{false_negative}}\]
参数

eval_type (str) – The metric to calculate the accuracy over a dataset, for classification (single-label), and multilabel (multilabel classification). Default: ‘classification’.

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mindspore.float32)
>>> y = Tensor(np.array([1, 0, 1]), mindspore.float32)
>>> metric = nn.Accuracy('classification')
>>> metric.clear()
>>> metric.update(x, y)
>>> accuracy = metric.eval()
>>> print(accuracy)
0.6666666666666666
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Computes the accuracy.

返回

Float, the computed result.

引发

RuntimeError – If the sample size is 0.

update(*inputs)[源代码]

Updates the internal evaluation result \(y_{pred}\) and \(y\).

参数

inputs – Input y_pred and y. y_pred and y are a Tensor, a list or an array. For the ‘classification’ evaluation type, y_pred is in most cases (not strictly) a list of floating numbers in range \([0, 1]\) and the shape is \((N, C)\), where \(N\) is the number of cases and \(C\) is the number of categories. Shape of y can be \((N, C)\) with values 0 and 1 if one-hot encoding is used or the shape is \((N,)\) with integer values if index of category is used. For ‘multilabel’ evaluation type, y_pred and y can only be one-hot encoding with values 0 or 1. Indices with 1 indicate the positive category. The shape of y_pred and y are both \((N, C)\).

引发

ValueError – If the number of the inputs is not 2.

class tinyms.metrics.MAE[源代码]

Calculates the mean absolute error(MAE).

Creates a criterion that measures the MAE between each element in the input: \(x\) and the target: \(y\).

\[\text{MAE} = \frac{\sum_{i=1}^n \|y_i - x_i\|}{n}\]

Here \(y_i\) is the prediction and \(x_i\) is the true value.

注解

The method update must be called with the form update(y_pred, y).

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]), mindspore.float32)
>>> y = Tensor(np.array([0.1, 0.25, 0.7, 0.9]), mindspore.float32)
>>> error = nn.MAE()
>>> error.clear()
>>> error.update(x, y)
>>> result = error.eval()
>>> print(result)
0.037499990314245224
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Computes the mean absolute error(MAE).

返回

Float, the computed result.

引发

RuntimeError – If the total number of samples is 0.

update(*inputs)[源代码]

Updates the internal evaluation result \(y_{pred}\) and \(y\).

参数

inputs – Input y_pred and y for calculating MAE where the shape of y_pred and y are both N-D and the shape are the same.

引发

ValueError – If the number of the input is not 2.

class tinyms.metrics.MSE[源代码]

Measures the mean squared error(MSE).

Creates a criterion that measures the MSE (squared L2 norm) between each element in the input: \(x\) and the target: \(y\).

\[\text{MSE}(x,\ y) = \frac{\sum_{i=1}^n(y_i - x_i)^2}{n}\]

where \(n\) is batch size.

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]), mindspore.float32)
>>> y = Tensor(np.array([0.1, 0.25, 0.5, 0.9]), mindspore.float32)
>>> error = nn.MSE()
>>> error.clear()
>>> error.update(x, y)
>>> result = error.eval()
clear()[源代码]

Clear the internal evaluation result.

eval()[源代码]

Computes the mean squared error(MSE).

返回

Float, the computed result.

引发

RuntimeError – If the number of samples is 0.

update(*inputs)[源代码]

Updates the internal evaluation result \(y_{pred}\) and \(y\).

参数

inputs – Input y_pred and y for calculating the MSE where the shape of y_pred and y are both N-D and the shape are the same.

引发

ValueError – If the number of input is not 2.

class tinyms.metrics.Metric[源代码]

Base class of metric.

注解

For examples of subclasses, please refer to the definition of class MAE, Recall etc.

abstract clear()[源代码]

An interface describes the behavior of clearing the internal evaluation result.

注解

All subclasses must override this interface.

abstract eval()[源代码]

An interface describes the behavior of computing the evaluation result.

注解

All subclasses must override this interface.

property indexes

The _indexes is a private attribute, and you can retrieve it by self.indexes.

set_indexes(indexes)[源代码]

The _indexes is a private attribute and you can modify it by this function. This allows you to determine the order of logits and labels to be calculated in the inputs, specially when you call the method update within this metrics.

注解

It has been applied in subclass of Metric, eg. Accuracy, BleuScore, ConfusionMatrix, CosineSimilarity, MAE, and MSE.

参数

indexes (List(int)) – The order of logits and labels to be rearranged.

Outputs:

Metric, its original Class instance.

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
>>> y = Tensor(np.array([1, 0, 1]))
>>> y2 = Tensor(np.array([0, 0, 1]))
>>> metric = nn.Accuracy('classification').set_indexes([0, 2])
>>> metric.clear()
>>> metric.update(x, y, y2)
>>> accuracy = metric.eval()
>>> print(accuracy)
0.3333333333333333
abstract update(*inputs)[源代码]

An interface describes the behavior of updating the internal evaluation result.

注解

All subclasses must override this interface.

参数

inputs – A variable-length input argument list.

tinyms.metrics.rearrange_inputs(func)[源代码]

This decorator is used to rearrange the inputs according to its _indexes attributes which is specified by the set_indexes method.

实际案例

>>> class RearrangeInputsExample:
...     def __init__(self):
...         self._indexes = None
...
...     @property
...     def indexes(self):
...         return getattr(self, '_indexes', None)
...
...     def set_indexes(self, indexes):
...         self._indexes = indexes
...         return self
...
...     @rearrange_inputs
...     def update(self, *inputs):
...         return inputs
>>>
>>> rearrange_inputs_example = RearrangeInputsExample().set_indexes([1, 0])
>>> outs = rearrange_inputs_example.update(5, 9)
>>> print(outs)
(9, 5)
参数

func (Callable) – A candidate function to be wrapped whose input will be rearranged.

返回

Callable, used to exchange metadata between functions.

class tinyms.metrics.Precision(eval_type='classification')[源代码]

Calculates precision for classification and multilabel data.

The precision function creates two local variables, \(\text{true_positive}\) and \(\text{false_positive}\), that are used to compute the precision. This value is ultimately returned as the precision, an idempotent operation that simply divides \(\text{true_positive}\) by the sum of \(\text{true_positive}\) and \(\text{false_positive}\).

\[\text{precision} = \frac{\text{true_positive}}{\text{true_positive} + \text{false_positive}}\]

注解

In the multi-label cases, the elements of \(y\) and \(y_{pred}\) must be 0 or 1.

参数

eval_type (str) – Metric to calculate accuracy over a dataset, for classification or multilabel. Default: ‘classification’.

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
>>> y = Tensor(np.array([1, 0, 1]))
>>> metric = nn.Precision('classification')
>>> metric.clear()
>>> metric.update(x, y)
>>> precision = metric.eval()
>>> print(precision)
[0.5 1. ]
clear()[源代码]

Clears the internal evaluation result.

eval(average=False)[源代码]

Computes the precision.

参数

average (bool) – Specify whether calculate the average precision. Default value is False.

返回

Float, the computed result.

update(*inputs)[源代码]

Updates the internal evaluation result with y_pred and y.

参数

inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray. For ‘classification’ evaluation type, y_pred is in most cases (not strictly) a list of floating numbers in range \([0, 1]\) and the shape is \((N, C)\), where \(N\) is the number of cases and \(C\) is the number of categories. Shape of y can be \((N, C)\) with values 0 and 1 if one-hot encoding is used or the shape is \((N,)\) with integer values if index of category is used. For ‘multilabel’ evaluation type, y_pred and y can only be one-hot encoding with values 0 or 1. Indices with 1 indicate positive category. The shape of y_pred and y are both \((N, C)\).

引发

ValueError – If the number of input is not 2.

class tinyms.metrics.HausdorffDistance(distance_metric='euclidean', percentile=None, directed=False, crop=True)[源代码]

Calculates the Hausdorff distance. Hausdorff distance is the maximum and minimum distance between two point sets. Given two feature sets A and B, the Hausdorff distance between two point sets A and B is defined as follows:

\[H(A, B) = \text{max}[h(A, B), h(B, A)] h(A, B) = \underset{a \in A}{\text{max}}\{\underset{b \in B}{\text{min}} \rVert a - b \rVert \} h(A, B) = \underset{b \in B}{\text{max}}\{\underset{a \in A}{\text{min}} \rVert b - a \rVert \}\]
参数
  • distance_metric (string) – The parameter of calculating Hausdorff distance supports three measurement methods, “euclidean”, “chessboard” or “taxicab”. Default: “euclidean”.

  • percentile (float) – Floating point numbers between 0 and 100. Specify the percentile parameter to get the percentile of the Hausdorff distance. Default: None.

  • directed (bool) – It can be divided into directional and non directional Hausdorff distance, and the default is non directional Hausdorff distance, specify the percentile parameter to get the percentile of the Hausdorff distance. Default: False.

  • crop (bool) – Crop input images and only keep the foregrounds. In order to maintain two inputs’ shapes, here the bounding box is achieved by (y_pred | y) which represents the union set of two images. Default: True.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]]))
>>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]]))
>>> metric = nn.HausdorffDistance()
>>> metric.clear()
>>> metric.update(x, y, 0)
>>> mean_average_distance = metric.eval()
>>> print(mean_average_distance)
1.4142135623730951
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Calculate the no-directed or directed Hausdorff distance.

返回

A float with hausdorff_distance.

引发

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[源代码]

Updates the internal evaluation result ‘y_pred’, ‘y’ and ‘label_idx’.

参数

inputs – Input ‘y_pred’, ‘y’ and ‘label_idx’. ‘y_pred’ and ‘y’ are Tensor or numpy.ndarray. ‘y_pred’ is the predicted binary image. ‘y’ is the actual binary image. ‘label_idx’, the data type of label_idx is int.

引发

ValueError – If the number of the inputs is not 3.

class tinyms.metrics.Recall(eval_type='classification')[源代码]

Calculates recall for classification and multilabel data.

The recall class creates two local variables, \(\text{true_positive}\) and \(\text{false_negative}\), that are used to compute the recall. This value is ultimately returned as the recall, an idempotent operation that simply divides \(\text{true_positive}\) by the sum of \(\text{true_positive}\) and \(\text{false_negative}\).

\[\text{recall} = \frac{\text{true_positive}}{\text{true_positive} + \text{false_negative}}\]

注解

In the multi-label cases, the elements of \(y\) and \(y_{pred}\) must be 0 or 1.

参数

eval_type (str) – The metric to calculate the recall over a dataset, for classification or multilabel. Default: ‘classification’.

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
>>> y = Tensor(np.array([1, 0, 1]))
>>> metric = nn.Recall('classification')
>>> metric.clear()
>>> metric.update(x, y)
>>> recall = metric.eval()
>>> print(recall)
[1. 0.5]
clear()[源代码]

Clears the internal evaluation result.

eval(average=False)[源代码]

Computes the recall.

参数

average (bool) – Specify whether calculate the average recall. Default value is False.

返回

Float, the computed result.

update(*inputs)[源代码]

Updates the internal evaluation result with y_pred and y.

参数

inputs – Input y_pred and y. y_pred and y are a Tensor, a list or an array. For ‘classification’ evaluation type, y_pred is in most cases (not strictly) a list of floating numbers in range \([0, 1]\) and the shape is \((N, C)\), where \(N\) is the number of cases and \(C\) is the number of categories. Shape of y can be \((N, C)\) with values 0 and 1 if one-hot encoding is used or the shape is \((N,)\) with integer values if index of category is used. For ‘multilabel’ evaluation type, y_pred and y can only be one-hot encoding with values 0 or 1. Indices with 1 indicate positive category. The shape of y_pred and y are both \((N, C)\).

引发

ValueError – If the number of input is not 2.

class tinyms.metrics.Fbeta(beta)[源代码]

Calculates the fbeta score.

Fbeta score is a weighted mean of precision and recall.

\[F_\beta=\frac{(1+\beta^2) \cdot true\_positive} {(1+\beta^2) \cdot true\_positive +\beta^2 \cdot false\_negative + false\_positive}\]
参数

beta (Union[float, int]) – The weight of precision.

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
>>> y = Tensor(np.array([1, 0, 1]))
>>> metric = nn.Fbeta(1)
>>> metric.clear()
>>> metric.update(x, y)
>>> fbeta = metric.eval()
>>> print(fbeta)
[0.66666667 0.66666667]
clear()[源代码]

Clears the internal evaluation result.

eval(average=False)[源代码]

Computes the fbeta.

参数

average (bool) – Whether to calculate the average fbeta. Default value is False.

返回

Float, computed result.

update(*inputs)[源代码]

Updates the internal evaluation result y_pred and y.

参数

inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray. y_pred is in most cases (not strictly) a list of floating numbers in range \([0, 1]\) and the shape is \((N, C)\), where \(N\) is the number of cases and \(C\) is the number of categories. y contains values of integers. The shape is \((N, C)\) if one-hot encoding is used. Shape can also be \((N,)\) if category index is used.

class tinyms.metrics.BleuScore(n_gram=4, smooth=False)[源代码]

Calculates BLEU score of machine translated text with one or more references.

参数
  • n_gram (int) – The n_gram value ranges from 1 to 4. Default: 4.

  • smooth (bool) – Whether or not to apply smoothing. Default: False.

引发

ValueError – If the value range of n_gram is not from 1 to 4.

Supported Platforms:

Ascend GPU CPU

示例

>>> candidate_corpus = [['i', 'have', 'a', 'pen', 'on', 'my', 'desk']]
>>> reference_corpus = [[['i', 'have', 'a', 'pen', 'in', 'my', 'desk'],
...                      ['there', 'is', 'a', 'pen', 'on', 'the', 'desk']]]
>>> metric = BleuScore()
>>> metric.clear()
>>> metric.update(candidate_corpus, reference_corpus)
>>> bleu_score = metric.eval()
>>> print(bleu_score)
0.5946035575013605
clear()[源代码]

Clear the internal evaluation result.

eval()[源代码]

Computes the bleu score.

返回

A numpy with bleu score.

引发

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[源代码]

Updates the internal evaluation result with candidate_corpus and reference_corpus.

参数

inputs – Input candidate_corpus and reference_corpus. candidate_corpus and reference_corpus are a list. The candidate_corpus is an iterable of machine translated corpus. The reference_corpus is an iterable of iterables of reference corpus.

引发

ValueError – If the number of input is not 2.

class tinyms.metrics.CosineSimilarity(similarity='cosine', reduction='none', zero_diagonal=True)[源代码]

Computes representation similarity

参数
  • similarity (str) – ‘dot’ or ‘cosine’. Default: ‘cosine’

  • reduction (str) – ‘none’, ‘sum’, ‘mean’ (all along dim -1). Default: ‘none’

  • zero_diagonal (bool) – If true, the diagonals are set to zero. Default: True

返回

A square matrix (input1, input1) with the similarity scores between all elements. If sum or mean is used, then returns (b, 1) with the reduced value for each row.

Supported Platforms:

Ascend GPU CPU

示例

>>> import numpy as np
>>> from mindspore import nn
>>>
>>> test_data = np.array([[1, 3, 4, 7], [2, 4, 2, 5], [3, 1, 5, 8]])
>>> metric = nn.CosineSimilarity()
>>> metric.clear()
>>> metric.update(test_data)
>>> square_matrix = metric.eval()
>>> print(square_matrix)
[[0.  0.94025615  0.95162452]
 [0.94025615  0.  0.86146098]
 [0.95162452  0.86146098  0.]]
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Computes the Cosine_Similarity square matrix.

返回

A square matrix.

引发

RuntimeError – If the update method is not called first, an error will be reported.

update(inputs)[源代码]

Updates the internal evaluation result with ‘input1’.

参数

inputs – input_data input1. The input_data is a Tensor or an array.

class tinyms.metrics.OcclusionSensitivity(pad_val=0.0, margin=2, n_batch=128, b_box=None)[源代码]

This function is used to calculate the occlusion sensitivity of the model for a given image. Occlusion sensitivity refers to how the probability of a given prediction changes with the change of the occluded part of the image.

For a given result, the output probability is the probability of a region.

The higher the value in the output image is, the greater the decline of certainty, indicating that the occluded area is more important in the decision-making process.

参数
  • pad_val (float) – What values need to be entered in the image when a part of the image is occluded. Default: 0.0.

  • margin (Union[int, Sequence]) – Create a cuboid / cube around the voxel you want to occlude. Default: 2.

  • n_batch (int) – number of images in a batch before inference. Default: 128.

  • b_box (Sequence) – Bounding box on which to perform the analysis. The output image will also match in size. There should be a minimum and maximum for all dimensions except batch: [min1, max1, min2, max2,...]. If no bounding box is supplied, this will be the same size as the input image. If a bounding box is used, the output image will be cropped to this size. Default: None.

Supported Platforms:

Ascend GPU CPU

示例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> class DenseNet(nn.Cell):
...     def __init__(self):
...         super(DenseNet, self).__init__()
...         w = np.array([[0.1, 0.8, 0.1, 0.1],[1, 1, 1, 1]]).astype(np.float32)
...         b = np.array([0.3, 0.6]).astype(np.float32)
...         self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b))
...
...     def construct(self, x):
...         return self.dense(x)
>>>
>>> model = DenseNet()
>>> test_data = np.array([[0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
>>> label = np.array(1).astype(np.int32)
>>> metric = nn.OcclusionSensitivity()
>>> metric.clear()
>>> metric.update(model, test_data, label)
>>> score = metric.eval()
>>> print(score)
[0.29999995    0.6    1    0.9]
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Computes the occlusion_sensitivity.

返回

A numpy ndarray.

引发

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[源代码]

Updates input, including model, y_pred and label.

Inputs:
  • model (nn.Cell) - classification model to use for inference.

  • y_pred (Union[Tensor, list, np.ndarray]) - image to test. Should be a tensor consisting of 1 batch, can be 2- or 3D.

  • label (Union[int, Tensor]) - classification label to check for changes (normally the true label, but doesn’t have to be

引发
  • ValueError – If the number of input is not 3.

  • RuntimeError – If the batch size is not 1.

  • RuntimeError – If the number of labels is different from the number of batches.

class tinyms.metrics.F1[源代码]

Calculates the F1 score. F1 is a special case of Fbeta when beta is 1. Refer to class mindspore.nn.Fbeta for more details.

\[F_1=\frac{2\cdot true\_positive}{2\cdot true\_positive + false\_negative + false\_positive}\]

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
>>> y = Tensor(np.array([1, 0, 1]))
>>> metric = nn.F1()
>>> metric.update(x, y)
>>> result = metric.eval()
>>> print(result)
[0.66666667 0.66666667]
class tinyms.metrics.Dice(smooth=1e-05)[源代码]

The Dice coefficient is a set similarity metric. It is used to calculate the similarity between two samples. The value of the Dice coefficient is 1 when the segmentation result is the best and is 0 when the segmentation result is the worst. The Dice coefficient indicates the ratio of the area between two objects to the total area. The function is shown as follows:

\[dice = \frac{2 * (pred \bigcap true)}{pred \bigcup true}\]
参数

smooth (float) – A term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-5.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
>>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]))
>>> metric = nn.Dice(smooth=1e-5)
>>> metric.clear()
>>> metric.update(x, y)
>>> dice = metric.eval()
>>> print(dice)
0.20467791371802546
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Computes the Dice.

返回

Float, the computed result.

引发

RuntimeError – If the total number of samples is 0.

update(*inputs)[源代码]

Updates the internal evaluation result \(y_pred\) and \(y\).

参数

inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray. y_pred is the predicted value, y is the true value. The shape of y_pred and y are both \((N, ...)\).

引发
  • ValueError – If the number of the inputs is not 2.

  • RuntimeError – If y_pred and y do not have the same shape.

class tinyms.metrics.ROC(class_num=None, pos_label=None)[源代码]

Calculates the ROC curve. It is suitable for solving binary classification and multi classification problems. In the case of multiclass, the values will be calculated based on a one-vs-the-rest approach.

参数
  • class_num (int) – Integer with the number of classes. For the problem of binary classification, it is not necessary to provide this argument. Default: None.

  • pos_label (int) – Determine the integer of positive class. Default: None. For binary problems, it is translated to 1. For multiclass problems, this argument should not be set, as it is iteratively changed in the range [0,num_classes-1]. Default: None.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> # 1) binary classification example
>>> x = Tensor(np.array([3, 1, 4, 2]))
>>> y = Tensor(np.array([0, 1, 2, 3]))
>>> metric = nn.ROC(pos_label=2)
>>> metric.clear()
>>> metric.update(x, y)
>>> fpr, tpr, thresholds = metric.eval()
>>> print(fpr)
[0. 0. 0.33333333 0.6666667 1.]
>>> print(tpr)
[0. 1. 1. 1. 1.]
>>> print(thresholds)
[5 4 3 2 1]
>>>
>>> # 2) multiclass classification example
>>> x = Tensor(np.array([[0.28, 0.55, 0.15, 0.05], [0.10, 0.20, 0.05, 0.05], [0.20, 0.05, 0.15, 0.05],
...                     [0.05, 0.05, 0.05, 0.75]]))
>>> y = Tensor(np.array([0, 1, 2, 3]))
>>> metric = nn.ROC(class_num=4)
>>> metric.clear()
>>> metric.update(x, y)
>>> fpr, tpr, thresholds = metric.eval()
>>> print(fpr)
[array([0., 0., 0.33333333, 0.66666667, 1.]), array([0., 0.33333333, 0.33333333, 1.]),
array([0., 0.33333333, 1.]), array([0., 0., 1.])]
>>> print(tpr)
[array([0., 1., 1., 1., 1.]), array([0., 0., 1., 1.]), array([0., 1., 1.]), array([0., 1., 1.])]
>>> print(thresholds)
[array([1.28, 0.28, 0.2, 0.1, 0.05]), array([1.55, 0.55, 0.2, 0.05]), array([1.15, 0.15, 0.05]),
array([1.75, 0.75, 0.05])]
clear()[源代码]

Clear the internal evaluation result.

eval()[源代码]

Computes the ROC curve.

返回

A tuple, composed of fpr, tpr, and thresholds.

  • fpr (np.array) - np.array with false positive rates. If multiclass, this is a list of such np.array, one for each class.

  • tps (np.array) - np.array with true positive rates. If multiclass, this is a list of such np.array, one for each class.

  • thresholds (np.array) - thresholds used for computing false- and true positive rates.

引发

RuntimeError – If the update method is not called first, an error will be reported.

roc(y_pred, y, class_num=None, pos_label=None, sample_weights=None)[源代码]
update(*inputs)[源代码]

Update state with predictions and targets.

参数

inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray. In most cases (not strictly), y_pred is a list of floating numbers in range \([0, 1]\) and the shape is \((N, C)\), where \(N\) is the number of cases and \(C\) is the number of categories. y contains values of integers.

tinyms.metrics.auc(x, y, reorder=False)[源代码]

Computes the AUC(Area Under the Curve) using the trapezoidal rule. This is a general function, given points on a curve. For computing the area under the ROC-curve.

参数
  • x (Union[np.array, list]) – From the ROC curve(fpr), np.array with false positive rates. If multiclass, this is a list of such np.array, one for each class. The shape \((N)\).

  • y (Union[np.array, list]) – From the ROC curve(tpr), np.array with true positive rates. If multiclass, this is a list of such np.array, one for each class. The shape \((N)\).

  • reorder (boolean) – If True, assume that the curve is ascending in the case of ties, as for an ROC curve. If the curve is non-ascending, the result will be wrong. Default: False.

返回

Compute result.

返回类型

area (float)

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import nn
>>>
>>> y_pred = np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])
>>> y = np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])
>>> metric = nn.ROC(pos_label=2)
>>> metric.clear()
>>> metric.update(y_pred, y)
>>> fpr, tpr, thre = metric.eval()
>>> output = auc(fpr, tpr)
>>> print(output)
0.5357142857142857
class tinyms.metrics.TopKCategoricalAccuracy(k)[源代码]

Calculates the top-k categorical accuracy.

注解

The method update must receive input of the form \((y_{pred}, y)\). If some samples have the same accuracy, the first sample will be chosen.

参数

k (int) – Specifies the top-k categorical accuracy to compute.

引发

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
...         [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
>>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
>>> topk = nn.TopKCategoricalAccuracy(3)
>>> topk.clear()
>>> topk.update(x, y)
>>> output = topk.eval()
>>> print(output)
0.6666666666666666
clear()[源代码]

Clear the internal evaluation result.

eval()[源代码]

Computes the top-k categorical accuracy.

返回

Float, computed result.

update(*inputs)[源代码]

Updates the internal evaluation result y_pred and y.

参数

inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray. y_pred is in most cases (not strictly) a list of floating numbers in range \([0, 1]\) and the shape is \((N, C)\), where \(N\) is the number of cases and \(C\) is the number of categories. y contains values of integers. The shape is \((N, C)\) if one-hot encoding is used. Shape can also be \((N,)\) if category index is used.

class tinyms.metrics.Top1CategoricalAccuracy[源代码]

Calculates the top-1 categorical accuracy. This class is a specialized class for TopKCategoricalAccuracy. Refer to TopKCategoricalAccuracy for more details.

实际案例

>>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
...         [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
>>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
>>> topk = nn.Top1CategoricalAccuracy()
>>> topk.clear()
>>> topk.update(x, y)
>>> output = topk.eval()
>>> print(output)
0.0
class tinyms.metrics.Top5CategoricalAccuracy[源代码]

Calculates the top-5 categorical accuracy. This class is a specialized class for TopKCategoricalAccuracy. Refer to TopKCategoricalAccuracy for more details.

实际案例

>>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
...            [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
>>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
>>> topk = nn.Top5CategoricalAccuracy()
>>> topk.clear()
>>> topk.update(x, y)
>>> output = topk.eval()
>>> print(output)
1.0
class tinyms.metrics.Loss[源代码]

Calculates the average of the loss. If method ‘update’ is called every \(n\) iterations, the result of evaluation will be:

\[loss = \frac{\sum_{k=1}^{n}loss_k}{n}\]

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array(0.2), mindspore.float32)
>>> loss = nn.Loss()
>>> loss.clear()
>>> loss.update(x)
>>> result = loss.eval()
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Calculates the average of the loss.

返回

Float, the average of the loss.

引发

RuntimeError – If the total number is 0.

update(*inputs)[源代码]

Updates the internal evaluation result.

参数

inputs – Inputs contain only one element, the element is loss. The dimension of loss must be 0 or 1.

引发
  • ValueError – If the length of inputs is not 1.

  • ValueError – If the dimension of loss is not 1.

class tinyms.metrics.MeanSurfaceDistance(symmetric=False, distance_metric='euclidean')[源代码]

This function is used to compute the Average Surface Distance from y_pred to y under the default setting. Mean Surface Distance(MSD), the mean of the vector is taken. This tell us how much, on average, the surface varies between the segmentation and the GT.

参数
  • distance_metric (string) – The parameter of calculating Hausdorff distance supports three measurement methods, “euclidean”, “chessboard” or “taxicab”. Default: “euclidean”.

  • symmetric (bool) – if calculate the symmetric average surface distance between y_pred and y. In addition, if sets symmetric = True, the average symmetric surface distance between these two inputs will be returned. Default: False.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]]))
>>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]]))
>>> metric = nn.MeanSurfaceDistance(symmetric=False, distance_metric="euclidean")
>>> metric.clear()
>>> metric.update(x, y, 0)
>>> mean_average_distance = metric.eval()
>>> print(mean_average_distance)
0.8047378541243649
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Calculate mean surface distance.

返回

A float with mean surface distance.

引发

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[源代码]

Updates the internal evaluation result ‘y_pred’, ‘y’ and ‘label_idx’.

参数

inputs – Input ‘y_pred’, ‘y’ and ‘label_idx’. ‘y_pred’ and ‘y’ are Tensor or numpy.ndarray. ‘y_pred’ is the predicted binary image. ‘y’ is the actual binary image. ‘label_idx’, the data type of label_idx is int.

引发
  • ValueError – If the number of the inputs is not 3.

  • TypeError – If the data type of label_idx is not int or float.

  • ValueError – If the value of label_idx is not in y_pred or y.

  • ValueError – If y_pred and y have different shapes.

class tinyms.metrics.RootMeanSquareDistance(symmetric=False, distance_metric='euclidean')[源代码]

This function is used to compute the Residual Mean Square Distance from y_pred to y under the default setting. Residual Mean Square Distance(RMS), the mean is taken from each of the points in the vector, these residuals are squared (to remove negative signs), summed, weighted by the mean and then the square-root is taken. Measured in mm.

参数
  • distance_metric (string) – The parameter of calculating Hausdorff distance supports three measurement methods, “euclidean”, “chessboard” or “taxicab”. Default: “euclidean”.

  • symmetric (bool) – if calculate the symmetric average surface distance between y_pred and y. In addition, if sets symmetric = True, the average symmetric surface distance between these two inputs will be returned. Default: False.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]]))
>>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]]))
>>> metric = nn.RootMeanSquareDistance(symmetric=False, distance_metric="euclidean")
>>> metric.clear()
>>> metric.update(x, y, 0)
>>> root_mean_square_distance = metric.eval()
>>> print(root_mean_square_distance)
1.0000000000000002
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Calculate residual mean square surface distance.

返回

A float with residual mean square surface distance.

引发

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[源代码]

Updates the internal evaluation result ‘y_pred’, ‘y’ and ‘label_idx’.

参数

inputs – Input ‘y_pred’, ‘y’ and ‘label_idx’. ‘y_pred’ and ‘y’ are Tensor or numpy.ndarray. ‘y_pred’ is the predicted binary image. ‘y’ is the actual binary image. ‘label_idx’, the data type of label_idx is int.

引发
  • ValueError – If the number of the inputs is not 3.

  • TypeError – If the data type of label_idx is not int or float.

  • ValueError – If the value of label_idx is not in y_pred or y.

  • ValueError – If y_pred and y have different shapes.

class tinyms.metrics.Perplexity(ignore_label=None)[源代码]

Computes perplexity. Perplexity is a measurement about how well a probability distribution or a model predicts a sample. A low perplexity indicates the model can predict the sample well. The function is shown as follows:

\[PP(W)=P(w_{1}w_{2}...w_{N})^{-\frac{1}{N}}=\sqrt[N]{\frac{1}{P(w_{1}w_{2}...w_{N})}}\]
参数

ignore_label (int) – Index of an invalid label to be ignored when counting. If set to None, it will include all entries. Default: -1.

Supported Platforms:

Ascend GPU CPU

注解

The method update must be called with the form update(preds, labels).

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
>>> y = Tensor(np.array([1, 0, 1]))
>>> metric = nn.Perplexity(ignore_label=None)
>>> metric.clear()
>>> metric.update(x, y)
>>> perplexity = metric.eval()
>>> print(perplexity)
2.231443166940565
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Returns the current evaluation result.

返回

float, the computed result.

引发

RuntimeError – If the sample size is 0.

update(*inputs)[源代码]

Updates the internal evaluation result: math:preds and :math:labels.

参数

inputs – Input preds and labels. preds and labels are Tensor, list or numpy.ndarray. preds is the predicted values, labels is the label of the data. The shape of preds and labels are both \((N, C)\).

引发
  • ValueError – If the number of the inputs is not 2.

  • RuntimeError – If preds and labels have different lengths.

  • RuntimeError – If label shape is not equal to pred shape.

class tinyms.metrics.ConfusionMatrix(num_classes, normalize='no_norm', threshold=0.5)[源代码]

Computes the confusion matrix. The performance matrix of measurement classification model is the model whose output is binary or multi class. The confusion matrix is calculated. An array of shape [BC4] is returned. The third dimension represents each channel of each sample in the input batch.Where B is the batch size and C is the number of classes to be calculated.

If you only want to find confusion matrix, use this class. If you want to find ‘PPV’, ‘TPR’, ‘TNR’, etc., use class ‘mindspore.metrics.ConfusionMatrixMetric’.

参数
  • num_classes (int) – Number of classes in the dataset.

  • normalize (str) –

    The parameter of calculating ConfusionMatrix supports four Normalization modes, Choose from:

    • ’no_norm’ (None) - No Normalization is used. Default: None.

    • ’target’ (str) - Normalization based on target value.

    • ’prediction’ (str) - Normalization based on predicted value.

    • ’all’ (str) - Normalization over the whole matrix.

  • threshold (float) – A threshold, which is used to compare with the input tensor. Default: 0.5.

Supported Platforms:

Ascend GPU CPU

实际案例

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> x = Tensor(np.array([1, 0, 1, 0]))
>>> y = Tensor(np.array([1, 0, 0, 1]))
>>> metric = nn.ConfusionMatrix(num_classes=2, normalize='no_norm', threshold=0.5)
>>> metric.clear()
>>> metric.update(x, y)
>>> output = metric.eval()
>>> print(output)
[[1. 1.]
 [1. 1.]]
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Computes confusion matrix.

返回

numpy.ndarray, the computed result.

update(*inputs)[源代码]

Update state with y_pred and y.

参数

inputs – Input y_pred and y. y_pred and y are a Tensor, a list or an array. y_pred is the predicted value, y is the true value. The shape of y_pred is \((N, C, ...)\) or \((N, ...)\). The shape of y is \((N, ...)\).

引发

ValueError – If the number of the inputs is not 2.

class tinyms.metrics.ConfusionMatrixMetric(skip_channel=True, metric_name='sensitivity', calculation_method=False, decrease='mean')[源代码]

The performance matrix of measurement classification model is the model whose output is binary or multi class. The correlation measure of confusion matrix was calculated from the full-scale tensor, and the average values of batch, class channel and iteration were collected. This function supports the calculation of all measures described below: the metric name in parameter metric_name.

If you want to use confusion matrix to calculate, such as ‘PPV’, ‘TPR’, ‘TNR’, use this class. If you only want to calculate confusion matrix, please use ‘mindspore.metrics.ConfusionMatrix’.

参数
  • skip_channel (bool) – Whether to skip the measurement calculation on the first channel of the predicted output. Default: True.

  • metric_name (str) – The names of indicators are in the following range. Of course, you can also set the industry common aliases for these indicators. Choose from: [“sensitivity”, “specificity”, “precision”, “negative predictive value”, “miss rate”, “fall out”, “false discovery rate”, “false omission rate”, “prevalence threshold”, “threat score”, “accuracy”, “balanced accuracy”, “f1 score”, “matthews correlation coefficient”, “fowlkes mallows index”, “informedness”, “markedness”].

  • calculation_method (bool) – If true, the measurement for each sample will be calculated first. If not, the confusion matrix of all samples will be accumulated first. As for classification task, ‘calculation_method’ should be False. Default: False.

  • decrease (str) – Define the mode to reduce the calculation result of one batch of data. Decrease is used only if calculation_method is True. Default: “mean”. Choose from: [“none”, “mean”, “sum”, “mean_batch”, “sum_batch”, “mean_channel”, “sum_channel”].

Supported Platforms:

Ascend GPU CPU

实际案例

>>> metric = ConfusionMatrixMetric(skip_channel=True, metric_name="tpr",
...                                calculation_method=False, decrease="mean")
>>> metric.clear()
>>> x = Tensor(np.array([[[0], [1]], [[1], [0]]]))
>>> y = Tensor(np.array([[[0], [1]], [[0], [1]]]))
>>> metric.update(x, y)
>>> x = Tensor(np.array([[[0], [1]], [[1], [0]]]))
>>> y = Tensor(np.array([[[0], [1]], [[1], [0]]]))
>>> avg_output = metric.eval()
>>> print(avg_output)
[0.5]
clear()[源代码]

Clears the internal evaluation result.

eval()[源代码]

Computes confusion matrix metric.

返回

ndarray, the computed result.

update(*inputs)[源代码]

Update state with predictions and targets.

inputs:

Input y_pred and y. y_pred and y are a Tensor, a list or an array.

  • y_pred (ndarray) - Input data to compute. It must be one-hot format and first dim is batch. The shape of y_pred is \((N, C, ...)\) or \((N, ...)\). As for classification tasks, y_pred should has the shape [BN] where N is larger than 1. As for segmentation tasks, the shape should be [BNHW] or [BNHWD].

  • y (ndarray) - Compute the true value of the measure. It must be one-hot format and first dim is batch. The shape of y is \((N, C, ...)\).

引发

ValueError – If the number of the inputs is not 2.

tinyms.hub

tinyms.hub.load(uid, pretrained=True, **kwargs)[源代码]

Load a model from remote TinyMS Hub.

参数
  • uid (str) – Uid. The format should be strictly consistent with the official example: tinyms/0.2/lenet5_v1_mnist.

  • pretrained (bool) – Specified if to load pretrained weight ckpt file. Default: True.

  • kwargs (dict, optional) – Keyword arguments for network initialization.

返回

layers.Layer, the initialized network instance.

实际案例

>>> from tinyms import hub
>>>
>>> hub.load('tinyms/0.2/lenet5_v1_mnist', class_num=10)
tinyms.hub.load_checkpoint(uid, dst, pretrained=True, **kwargs)[源代码]

Load model checkpoint file from remote TinyMS Hub.

参数
  • uid (str) – Uid. The format should be strictly consistent with the official example: tinyms/0.2/lenet5_v1_mnist.

  • dst (str) – Full path of filename where the checkpoint file will be loaded, e.g. /tmp/lenet5.ckpt.

  • pretrained (bool) – Specified if to load pretrained weight ckpt file. Default: True.

  • kwargs (dict, optional) – Keyword arguments for network initialization.

实际案例

>>> from tinyms import hub
>>>
>>> hub.load_checkpoint('tinyms/0.2/lenet5_v1_mnist', '/tmp/lenet5.ckpt', class_num=10)
tinyms.hub.load_weights(uid)[源代码]

Load model pretrained weights from remote TinyMS Hub.

参数

uid (str) – Uid. The format should be strictly consistent with the official example: tinyms/0.2/lenet5_v1_mnist.

返回

Dict, the pretrained network weight dict.

实际案例

>>> from tinyms import hub
>>> from tinyms.model import lenet5
>>> from tinyms.utils.train import load_param_into_net
>>>
>>> param_dict = hub.load_weights('tinyms/0.2/lenet5_v1_mnist')
>>> net = lenet5()
>>> load_param_into_net(net, param_dict)

tinyms.serving

This module refers to the process of serving pre-trained models so that they can quickly and efficiently process data input by users and obtain results. TinyMS provides a complete set of start server (start_server), check backend (list_servables), check start status (server_started) and shut down the server (shutdown) and other functions based on Flask (https://flask.palletsprojects.com/en/1.1.x/).

实际案例

>>> from tinyms.serving import Server, Client
>>>
>>> server = Server()
>>> server.start_server()
Server starts at host 127.0.0.1, port 5000
>>> client = Client()
>>> client.list_servables()
>>> client.predict('example.jpg', 'servable_name', dataset_name='mnist')
class tinyms.serving.Client(host='127.0.0.1', port=5000)[源代码]

Client is the entrance to connecting to serving server, and also send all requests to server side by HTTP request.

参数
  • host (str) – Serving server host ip. Default: ‘127.0.0.1’.

  • port (int) – Serving server listen port. Default: 5000.

实际案例

>>> from tinyms.serving import Client
>>>
>>> client = Client()
list_servables()[源代码]

List the model that is currently served by the backend server.

A GET request will be sent to the server (127.0.0.1:5000) which will then be routed to 127.0.0.1:5000/servables, and the backend servable information will be returned to the client.

返回

res_body[‘servables’] (str) will be returned, the backend servable information. Error message will be returned and printed if requests.status_code is not ok. ‘Server not started’ will be returned if server is not started

实际案例

>>> # Running the quickstart tutorial, after server started and servable json defined
>>> from tinyms.serving import Client
>>>
>>> client = Client()
>>> client.list_servables()
[{
    'description': 'This servable hosts a lenet5 model predicting numbers',
    'model': {
        'class_num': 10,
        'format': 'ckpt',
        'name': 'lenet5'
    },
    'name': 'lenet5'
}]
predict(img_path, servable_name, dataset_name='mnist', strategy='TOP1_CLASS')[源代码]

Send the predict request to the backend server, get the return value and do the post process

Predict the input image, and get the result. User must specify the image_path, servable_name, dataset_name and output_strategy to get the predict result.

参数
  • img_path (str) – path to the image

  • servable_name (str) – the name in servable_json, now supports 6 servables: lenet5, resnet50_imagenet2012, resnet50_cifar10, mobilenetv2, ssd300 and cyclegan_cityscape.

  • dataset_name (str) – the name of the dataset that is used to train the model, now supports 5 datasets: mnist, imagenet2012, cifar10, voc, cityscape

  • strategy (str) – the output strategy, for lenet5, resnet50 and mobilenetv2, select between ‘TOP1_CLASS’ and ‘TOP5_CLASS’, for ssd300, only TOP1_CLASS, for cyclegan_cityscape, select between gray2color and color2gray

返回

For lenet5, resnet50, mobilenetv2, the output is a string of predict result. For ssd300, the output is a string of bounding boxes coordinates and labels, which can be further processed using ImageViewer function For cyclegan, the output is a numpy of image, which can be transformed to image using Image.fromarray

实际案例

>>> # Running the quickstart tutorial, after server started and servable json defined
>>> from tinyms.serving import Client
>>>
>>> client = Client()
>>> print(client.predict('/root/7.png', 'lenet5', 'mnist', 'TOP1_CLASS'))
TOP1: 7, score: 0.99943381547927856445
class tinyms.serving.Server(host='127.0.0.1', port=5000, serving_path='/etc/tinyms/serving/')[源代码]

Server is the entrance to initialize and shutdown the serving server, and also accept all requests from the client side by calling Flask server.

参数
  • host (str) – Serving server host ip. Default: ‘127.0.0.1’.

  • port (int) – Serving server listen port. Default: 5000.

  • serving_path (str, optional) – Set the read path of a service configuration. Default: ‘/etc/tinyms/serving/’.

实际案例

>>> from tinyms.serving import Server
>>>
>>> server = Server()
shutdown()[源代码]

Shutdown the flask server.

Search fot the pid of the process running on port 5000, and kill it. This function will be automatically called when SIGINT, SIGHUP and SIGTERM signals caught.

返回

A string message of server shutting down or not.

实际案例

>>> from tinyms.serving import Server
>>>
>>> server = Server()
>>> server.shutdown()
Server shutting down...
start_server()[源代码]

Start the flask server in a subprocess.

Catch the signal of CTRL + D to shutdown, otherwise call shutdown() function to shutdown the server, if the ip and port already in use, server won’t start for a second time.

返回

Start the server in a sub process.

实际案例

>>> from tinyms.serving import Server
>>>
>>> server = Server()
>>> server.start_server()
Server starts at host 127.0.0.1, port 5000
tinyms.serving.run_flask(host='127.0.0.1', port=5000)[源代码]

Start the flask server, only be used to trigger starting the flask server in subprocess.

Directly calling this function is not recommended, please use start_server(). Only Error message will be displayed.

参数
  • host (str) – the ip address of the flask server. Default: ‘127.0.0.1’.

  • port (int) – the port of the server. Default: 5000.

返回

Server Started

实际案例

>>> # In the start_server function
>>> cmd = ['python -c "from tinyms.serving import run_flask; run_flask()"']
>>> server_process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

tinyms.app

This module is to support vision visualization with opencv, which can help developers use pre-trained models to predict and show the reasoning image fast. Current it only supports object detection model.

class tinyms.app.ObjectDetector(config=None)[源代码]

ObjectDetector is a high-level class defined for building model,preproceing the input image, predicting and postprocessing the prediction output data.

参数

config (dict) – model config parsed from the json file under the app/object_detection/configs dir.

convert2tensor(transform_input)[源代码]

Convert the numpy data to the tensor format.

参数

transform_input (numpy.ndarray) – the preprocessing image.

返回

Tensor, the converted image.

data_postprocess(predictions_output, image_shape)[源代码]

Postprocessing the predictions output data.

参数
  • predictions_output (list) – predictions output data.

  • image_shape (list) – the shape of the input image.

返回

dict, the postprocessing result.

data_preprocess(input)[源代码]

Preprocess the input image.

参数

input (numpy.ndarray) – the input image.

返回

list, the preprocess image shape. numpy.ndarray, the preprocess image result.

model_build(is_training=False)[源代码]

Build the object detection model to predict the image.

参数

is_training (bool) – default: False.

返回

model.Model, generated object detection model.

model_load_and_predict(serve_model, input_tensor)[源代码]

Load the object detection model to predict the image.

参数
  • serve_model (model.Model) – object detection model.

  • input_tensor (Tensor) – the converted input image.

返回

model.Model, object detection model loaded the checkpoint file. list, predictions output result.

tinyms.app.object_detection_predict(input, object_detector, is_training=False)[源代码]

An easy object detection model predicting method for beginning developers to use.

参数
  • input (numpy.ndarray) – the input image.

  • object_detector (ObjectDetector) – the instance of the ObjectDetector class.

  • is_training (bool) – default: False.

返回

dict, the postprocessing result.

tinyms.app.visualize_boxes_on_image(img, bbox_data, box_color=(0, 255, 0), box_thickness=3, text_font=1, font_scale=3, text_color=(0, 0, 255), font_size=3, show_scores=True)[源代码]

Visualize the prediction image.

参数
  • img (numpy.ndarray) – the input image.

  • bbox_data (dict) – the predictions box data.

  • box_color (list) – the box color. Default: (0, 255, 0).

  • box_thickness (int) – box thickness. Default: 3.

  • text_font (Enum) – text font. Default: cv2.FONT_HERSHEY_PLAIN.

  • font_scale (int) – font scale. Default: 3.

  • text_color (list) – text color. Default: (0, 0, 255).

  • font_size (int) – font size. Default: 3.

  • show_scores (bool) – whether to show scores. Default: True.

返回

numpy.ndarray, the output image drawed the prediction box.

tinyms.app.draw_boxes_on_image(img, boxes, box_scores, box_classes, box_color=(0, 255, 0), box_thickness=3, text_font=1, font_scale=3, text_color=(0, 0, 255), font_size=3, show_scores=True)[源代码]

Draw the prediction box for the input image.

参数
  • img (numpy.ndarray) – the input image.

  • boxes (list) – the box coordinates.

  • box_color (list) – the box color. Default: (0, 255, 0).

  • box_thickness (int) – box thickness. Default: 3.

  • text_font (Enum) – text font. Default: cv2.FONT_HERSHEY_PLAIN.

  • font_scale (int) – font scale. Default: 3.

  • text_color (list) – text color. Default: (0, 0, 255).

  • font_size (int) – font size. Default: 3.

  • show_scores (bool) – whether to show scores. Default: True.

返回

numpy.ndarray, the output image drawed the prediction box.

tinyms.app.save_image(img, save_dir='./', img_name='no_name', img_format='jpg')[源代码]

Save the prediction image.

参数
  • img (numpy.ndarray) – the input image.

  • save_dir (str) – the dir to save the prediction image.

  • img_name (str) – the name of the prediction image. Default: ‘no_name’.

  • img_format (str) – the format of the prediction image. Default: ‘jpg’.

tinyms.app.load_and_parse_config(config_path)[源代码]

Load and parse the json file the object detection model.

参数

config_path (numpy.ndarray) – the config json file path.

返回

dict, the model configuration.

class tinyms.app.ObjectDetector(config=None)[源代码]

ObjectDetector is a high-level class defined for building model,preproceing the input image, predicting and postprocessing the prediction output data.

参数

config (dict) – model config parsed from the json file under the app/object_detection/configs dir.

convert2tensor(transform_input)[源代码]

Convert the numpy data to the tensor format.

参数

transform_input (numpy.ndarray) – the preprocessing image.

返回

Tensor, the converted image.

data_postprocess(predictions_output, image_shape)[源代码]

Postprocessing the predictions output data.

参数
  • predictions_output (list) – predictions output data.

  • image_shape (list) – the shape of the input image.

返回

dict, the postprocessing result.

data_preprocess(input)[源代码]

Preprocess the input image.

参数

input (numpy.ndarray) – the input image.

返回

list, the preprocess image shape. numpy.ndarray, the preprocess image result.

model_build(is_training=False)[源代码]

Build the object detection model to predict the image.

参数

is_training (bool) – default: False.

返回

model.Model, generated object detection model.

model_load_and_predict(serve_model, input_tensor)[源代码]

Load the object detection model to predict the image.

参数
  • serve_model (model.Model) – object detection model.

  • input_tensor (Tensor) – the converted input image.

返回

model.Model, object detection model loaded the checkpoint file. list, predictions output result.

tinyms.app.object_detection_predict(input, object_detector, is_training=False)[源代码]

An easy object detection model predicting method for beginning developers to use.

参数
  • input (numpy.ndarray) – the input image.

  • object_detector (ObjectDetector) – the instance of the ObjectDetector class.

  • is_training (bool) – default: False.

返回

dict, the postprocessing result.

class tinyms.app.ObjectDetector(config=None)[源代码]

ObjectDetector is a high-level class defined for building model,preproceing the input image, predicting and postprocessing the prediction output data.

参数

config (dict) – model config parsed from the json file under the app/object_detection/configs dir.

convert2tensor(transform_input)[源代码]

Convert the numpy data to the tensor format.

参数

transform_input (numpy.ndarray) – the preprocessing image.

返回

Tensor, the converted image.

data_postprocess(predictions_output, image_shape)[源代码]

Postprocessing the predictions output data.

参数
  • predictions_output (list) – predictions output data.

  • image_shape (list) – the shape of the input image.

返回

dict, the postprocessing result.

data_preprocess(input)[源代码]

Preprocess the input image.

参数

input (numpy.ndarray) – the input image.

返回

list, the preprocess image shape. numpy.ndarray, the preprocess image result.

model_build(is_training=False)[源代码]

Build the object detection model to predict the image.

参数

is_training (bool) – default: False.

返回

model.Model, generated object detection model.

model_load_and_predict(serve_model, input_tensor)[源代码]

Load the object detection model to predict the image.

参数
  • serve_model (model.Model) – object detection model.

  • input_tensor (Tensor) – the converted input image.

返回

model.Model, object detection model loaded the checkpoint file. list, predictions output result.

tinyms.app.object_detection_predict(input, object_detector, is_training=False)[源代码]

An easy object detection model predicting method for beginning developers to use.

参数
  • input (numpy.ndarray) – the input image.

  • object_detector (ObjectDetector) – the instance of the ObjectDetector class.

  • is_training (bool) – default: False.

返回

dict, the postprocessing result.

版本路标

v0.2.0

主要特性规划

  • 高阶API的RNN支持,侧重在LSTM和SentimentNet

  • ModelPark的持续增加: Alex,Densenet100,Bert.

  • TinyMS Hub, 一键完成预训练模型的调用、部署和推理.

  • 将TinyMS加入到MindSpore CI流水线的用例中看护以确保版本的更新不会影响基本的高阶API体验.

具体规划将在社区规划会议上正式确定下来.

贡献指导

首先非常欢迎加入TinyMS社区的各位开发者👏,如果您还不太清楚TinyMS社区的运作流程,可通过本篇指导文档的学习快速上手社区贡献。

开源社区贡献者协议

在您首次向TinyMS社区提交代码的时候,请知悉在代码合入之前需签署CLA(Contributor License Agreement)协议。

针对个人开发者,请访问CLA签署页面进行协议签署。

快速上手TinyMS

贡献流程

代码风格

当前TinyMS社区制定了编码风格、单元测试以及文档自动生成等方面的规范,请在提交代码之前仔细阅读社区开发规范。

  • 编码风格:Python语言为主,基于Python PEP 8 Coding Style进行开发

  • 单元测试:Python语言为主,基于pytest进行开发

  • 自动文档生成:基于Sphinx进行社区文档的自动生成

Fork-Pull开发模式

  • Fork TinyMS代码仓

    在提交代码之前,请您务必确认TinyMS项目已Fork至您的个人仓。同时,这将意味着在TinyMS官方仓和您的Fork仓之间会存在并行开发的情况,甚至可能造成分支冲突的问题,因此请您在个人仓开发过程中格外注意与主仓代码不一致的情况。

    注意: 当前TinyMS项目的默认分支名已设置为main

  • 克隆远端代码

    如果您想把代码克隆到本地进行开发,那么建议您使用git工具:

    git clone https://github.com/{insert_your_forked_repo}/tinyms.git
    git remote add upstream https://github.com/tinyms-ai/tinyms.git
    
  • 本地开发调试

    考虑到多个分支的同步问题,建议您针对每个Pull Request新建对应的分支名:

    git checkout -b {new_branch_name}
    

    注意: 建议您每次在切换分支之前都要运行git pull upstream main指令拉取最新代码。

  • 推送代码

    本地开发调试之后,您可以通过如下指令生成git commit记录:

    git add .
    git status # Check the update status
    git commit -m "Your commit title"
    git commit -s --amend # Add the concrete description of your commit
    git push origin {new_branch_name}
    
  • 提交PR

    最后您需要在网页创建一个Pull Request,源分支和目标分支分别设置为您个人仓的新建分支和TinyMS主仓的main分支。提交PR之后,系统会自动触发Travis CI工作来确认您的修改是否符合要求。

上报漏洞

为项目做贡献的一种好方法是在遇到问题时发送详细的报告。我们始终感谢写得好的、详尽的错误报告,希望您能遵守社区规则!🤝

报告问题时,请参考以下格式:

  • 您使用的是哪个版本的环境信息(tinyms、mindspore、os、python等)?

  • 这是错误报告还是特性需求?

  • 发生了什么?

  • 您预期会发生什么?

  • 如何复现它(尽可能少且精确)?

  • 给代码审核人员的特别提示?

漏洞咨询:

  • 如果您发现未解决的问题,而这正是您要解决的问题,请对该问题发表一些评论,以告诉其他人您将负责此事。

  • 如果某个问题开放了一段时间,建议贡献者在解决该问题之前进行预先检查。

  • 如果您解决了自己上报的问题,还需要在解决该问题之前让他人知道。

提交更改

不知道如何提交PR?📚建议您阅读How to Contribute to an Open Source Project on GitHubfree章节进行学习📚

提交更改时,请遵守如下规则:

  • GitHub上将您的想法作为issue提出。

  • 如果这是一项需要大量设计细节的新功能,则还应提交设计建议。

  • 在问题讨论和设计方案审查中达成共识后,完成分叉存储库的开发并提交PR。

  • 除非获得批准者的2+ LGTM,否则不允许任何PR。请注意不允许批准者在自己的PR上添加 LGTM的行为。

  • 对PR进行充分讨论之后,它将根据讨论的结果而被合并、放弃或拒绝。

PR咨询:

  • 应避免任何不相关的更改。

  • 确保您的提交历史记录逻辑清晰。

  • 始终使您的提交分支与主分支保持一致。

  • 对于错误修复PR,请确保所有相关问题都已链接。

社区支持

如果您在使用TinyMS过程中遇到任何问题,请立马通过如下渠道向社区寻求帮助:

  • 微信。请搜索mindspore0328微信号添加好友

  • QQ群。TBD

  • Slack。请打开MindSpore Slack加入tinyms小组与他人进行交流

社区交流

技术交流

  • 提交ISSUEs或者PRs 依然是在社区进行交流的最佳方法. 具体方式请参考我们的 指引

社区支持

如果您在使用TinyMS过程中遇到任何问题,请立马通过如下渠道向社区寻求帮助:

  • 微信。请搜索mindspore0328微信号添加好友

  • QQ群。TBD

  • Slack。请打开MindSpore Slack加入tinyms小组与他人进行交流

FAQ

未完待续。。。