Welcome to TinyMS’s documentation!¶
TinyMS is an Easy-to-Use deep learning development toolkit based on MindSpore, designed to providing quick-start guidelines for machine learning beginners.
What is TinyMS¶
TinyMS Project¶
Introduction¶
TinyMS is an open source deep learning development kit written in Python. It runs on top of deep learning framework like MindSpore, and provides high level API covers the entire lifecycle and workflow for AI development that ranges from data preparation to model deployment.
TinyMS is composed of several modules including data, model and serving. It provides transform data processing operators for different scenarios and reuses MindSpore datasets like cifar-10.
TinyMS are mainly designed to help user groups like deep learning beginners, researchers who conducts studies related to deep learning, and AI application developers with a crash course.
Combined with video tutorials (available in Chinese on Bilibili site and soon with English version on YouTube channel), TinyMS project offers the best deep learning crash course and entry level development experience todate.
TinyMS vs Keras¶
To quote from Keras’ website
Keras is a deep learning API written in Python, running on top of the machine learning platform TensorFlow. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result as fast as possible is key to doing good research.
Keras is known for its completeness. Keras is composed of modules such as dataset, layer, model and backend. It provides commonly used datasets and data processing functions for different scenarios. The layer module provides all encompassing functionalities such as convolution, embedding, pooling, backend (multiple support for Tensorflow, CNTK and Theano). The model module provides functionalities such as model types (sequential or functional), network construction (input, output, pooling, etc.), model compilation, model training, model verification and inference.
In comparison, TinyMS designs a set of more abstract high level APIs and therefore is less complex than Keras. For example one can complete dataset preprocessing with just one line of code in TinyMS. TinyMS also provides several individual tools and quick model deployment module which Keras has not yet offered.
TinyMS vs Fastai¶
To quote from fastai’s README
fastai is a deep learning library which provides practitioners with high-level components that can quickly and easily provide state-of-the-art results in standard deep learning domains, and provides researchers with low-level components that can be mixed and matched to build new approaches.
With the strength of PyTorch’s flexibility, Fastai could provide out-of-the-box development experience for model types like vision, text, tabular, collab. However unlike Keras’s multi-backend support, Fastai’s backend is tightly coupled with PyTorch and its versioning.
Fastai is known for its “petitness” which provides a great lightweight and easy-to-understand structure. Fastai consists of three major modules: data, models and learner. The data module provide transform data preprocessing operations which is convenient for developers. The model module provides many predefined networks like unet for quick model construction. The learner module defines the relationship between data and model with a set of callback functions to help developers quickly grasp the most common deep learning architectures. It provides a rich set of tools such as data downloading, decompression, figure verification and file processing.
In comparison, while sharing similar design concepts on high level APIs, TinyMS offers predefined MindSpore datasets which could help developers with dataset processing enormously as well as quick model deployment module, both of which Fastai has not yet provided.
TinyMS Community¶
Other than TinyMS project,the community at-large also includes the many other projects and activities:
Specification project:an attempt to standardize the format of model training scripts. Due to the abstract nature of TinyMS APIs, we found it necessary to have a standard or guideline for ModelZoo.
https://tinyms-ai.github.io:a simple website built in open source based upon GitHub Page mechanism.
RustedAI Team:only visible for org members at the moment, RustedAI is an initiative that TinyMS tries to build for more adoption of Rust-lang in the field of deep learning to meet the goal of low runtime footprint.
Community Activities:We will organize TinyMS model competitions and many other activities including Meetups, webinars, etc.
TinyMS and Developers¶
As a new open source project, TinyMS stands on the shoulder of the giants like Keras and Fastai. Although we hope to achieve many innovations in our design, it still depends on a vibrant community and ecosystem to make TinyMS reach the depth and broadness of its predecessors in order to better serve the academia, industry and developers in general.
Install TinyMS¶
Installation For Beginners¶
Pypi¶
For users who own a clean environment, it is recommended to use pypi to install TinyMS given that the following requirements are meet. For those who don’t, Anaconda is a good choice for setting up the python environment.
Prerequisites
OS:
Ubuntu 18.04
orWindows 10
Python:
3.7.5
For China based users it is recommended to run the following command lines to help with faster download
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
Note: There may be some problems during the installation process. The following possible situations are for reference only. If you encounter other problems during the installation process, we welcome you to submit your issues and pull requests in our community, and we will reply you as soon as possible.
Error 1: If you use the mirror source to execute the installation command, it may report
Could not find a version that satisfies the requirement tinyms==0.3.1
Solution:
You can try to use the default official source, directly append
-i https://pypi.python.org/simple/
at the end, the download speed of the default official source may be slower, please be patient :smile:Error 2: If you are a windows user, please make sure that
Microsoft VC++ 14.0
is installed. If not, it may reportERROR: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools” may be reported during the installation process. : https://visualstudio.microsoft.com/visual-cpp-build-tools/
Solution:
Because TinyMS is dependent on the
Python3.7.5
environment, and Python3 is compiled withVC++ 14.0
. According to the error prompt, downloadMicrosoft C++ Build Tools
at the provided link . Note that during the installation process, the two componentswindows 10 SDK
andC++ CMake Tools for Windows
need to be checked inDesktop Development Module Using C++ Desktop
. For installation details, please refer to Visual Studio Build Tool Installation.
Docker¶
For those who don’t want to affect the local develop environment due to difficulty of meeting the prerequisites, using docker to install is recommended
docker:
v18.06.1-ce
If user wants to try the tutorials that are written in .ipynb
files,please pull jupyter version of TinyMS in which jupyter components are installed by default
If user wants to experience the image inference service in a visual WEB UI,please pull nginx version of TinyMS in which nginx components are installed by default
Default version
docker pull tinyms/tinyms:0.3.1
docker run -it tinyms/tinyms:0.3.1
Jupyter version
If user wants to try jupyter, run the following command line
docker pull tinyms/tinyms:0.3.1-jupyter
docker run -it --net=host tinyms/tinyms:0.3.1-jupyter
Open a browser on the local machine, type in
<Your_external_IP_address>:8888
Example: 188.8.8.88:8888
, the default password is tinyms
,then user can log in to jupyter
Nginx version
If user wants to experience the image inference service in a visual WEB UI, run the following command line
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 <Your_host_public_IP_address_not_docker_IP_address>
Open a browser on the local machine, type in
<Your_host_public_IP_address_not_docker_IP_address>:80
Installation For Experienced Developers¶
For developers who want to develop based on TinyMS, install from source
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
Validate installation¶
Create a python
, jupyter
or nginx
kernel, input the following codes
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))
If the output is similar to below, then the installation is valid
[[2. 2. 2.]
[2. 2. 2.]]
Notes¶
When we use TinyMS 0.3.1
, the following error may be reported
Error Details:
[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'.
Solution:
According to the error prompt, we can turn off the Fallback feature
with the following command.
For general users, execute the following commands in the command line tool:
export MS_DEV_ENABLE_FALLBACK=0
For users using jupyter, execute the following command in the cell:
!export MS_DEV_ENABLE_FALLBACK=0
If you report other error while using TinyMS 0.3.1
, after you try to solve the error, there is still a problem, we welcome you to submit your issues and pull requests in our community, and we will reply you as soon as possible.
Implementing an Image Classification App in One Minute¶
In this tutorial, constructing a LeNet5 model, downloading dataset, training, starting the server and making predictions of the model using TinyMS 0.3.1 API will be demonstrated.
Prerequisite¶
Ubuntu:
18.04
docker:
v18.06.1-ce
Jupyter: Using
tinyms 0.3.1-Jupyter
, you can refer to Quick Install tinyms to deploy the environment.
Introduction¶
TinyMS is a high-level API which is designed for amateur of deep learning. It minimizes the number of actions of users required to construct, train, evaluate and serve a model. TinyMS also provides tutorials and documentations for developers.
This tutorial consists of six parts, constructing the model
, downloading dataset
, training
, define servable json
, starting server
and making predictions
in which the server will be run in a sub process.
[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. Construct the model¶
TinyMS encapsulates init and construct of the LeNet5 model, the line of the code is reduced to construct the LeNet5 model:
[2]:
# build the network
net = lenet5(class_num=10)
model = Model(net)
2. Download dataset¶
The MNIST dataset will be downloaded if mnist
folder didn’t exist at the /home/jovyan
folder. If mnist
folder already exists, this step will not be performed.
[3]:
# download the dataset
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. Train the model & evaluation¶
The dataset for both training and evaluation will be defined here, and the parameters for training also set in this block. A trained ckpt file will be saved to /home/jovyan/tinyms/serving/lenet5
folder for later use, meanwhile the evaluation will be performed and the Accuracy
can be checked.
[ ]:
# check lenet folder exists or not
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')
# set environment parameters
device_target = "CPU"
context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
dataset_sink_mode = False
# define the training and evaluation dataset
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)
# parameters for training
lr = 0.01
momentum = 0.9
epoch_size = 1
batch_size = 32
# define the loss function
net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
# define the 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. Define servable.json¶
Define lenet5 servable json file: servable json file defines the servable name, model name, model format and classification quantity for subsequent reasoning.
[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. Start server¶
5.1 Introduction¶
TinyMS Serving is a C/S(client/server) structure. TinyMS using Flask which is a micro web framework written in python as the C/S communication tool. In order to serve a model, user must start server first. If successfully started, the server will be run in a subprocess and listening to POST requests from 127.0.0.1 port 5000 sent by client and handle the requests using MindSpore backend which constructs the model, run the prediction and send the result back to the client.
5.2 Start server¶
Run the following code block to start the server.
[ ]:
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
The above prompt indicates that the server is started and running.
After the server is started, we need to go to the menu bar and click File
=>new Notebook
to create a new jupyter file, and then continue to complete the operation of the client.
[ ]:
import os
from PIL import Image
from tinyms.vision import ImageViewer
from tinyms.serving import Server, Client
6. Make predictions¶
6.1 Upload the pic¶
A picture of a single digit number is required to be the input. The picture we use in this tutorial can be found HERE, then save the picture to the /home/jovyan
folder, and rename it to 7.png
(or any other name you like).
Or run the following code to download the pic for this tutorial:
[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¶
Use list_servables
function to check what model is being served right now.
[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'}]
If the output description
shows it is a lenet5
model, then we can continue to next step to send our request.
6.3 Sending request and get the result¶
Run predict
function to send the request, select between TOP1_CLASS
and TOP5_CLASS
:
[9]:
# Set picture path and output policy(You can choose between Top1 and top5)
image_path = "/home/jovyan/7.png"
strategy = "TOP1_CLASS"
# predict(image_path, servable_name, dataset='mnist', strategy='TOP1_CLASS')
img_viewer = ImageViewer(Image.open(image_path), image_path)
img_viewer.show()
print(client.predict(image_path,'lenet5', 'mnist', strategy))

TOP1: 7, score: 0.9997
If user can see the output similar to this:
TOP1: 7, score: 0.99934917688369750977
that means the prediction is successfully performed.
Shutdown server¶
[10]:
server.shutdown()
[10]:
'Server shutting down...'
TinyMS Reasoning Visualization Experience¶
Combined with OpenCV image vision library, TinyMS V0.3.1 focus visualization features. Through simple and intuitive image visualization, it helps users to understand the effect of model reasoning more quickly.
For users who do not want to write code, TinyMS provides a visual interface of WEB UI. Users only need to upload the image to be inferred on the browser page to experience easily. Currently, it supports for LeNet5
, CycleGan
and SSD300
models.
WEB UI reasoning visualization¶
Users need to deploy the visual server first, details please see TinyMS Nginx Verion Installation. After the server is successfully deployed, the home page and reasoning effect page (taking CycleGan
model as an example) presented by the browser are as follows:
Index Page
Predict Page
For users who want to run code, TinyMS provides model reasoning visualization module, which only needs 5
step code to experience quickly. Currently, it only supports SSD300
object detection model.
Model reasoning visualization module application¶
If users need to experience the model reasoning visualization module application for the first time, they can download code from TinyMS Official Repo, then do the following operations:
Static image detection
Environmental preparation
An operating system with a visual desktop, such as
Window_x64
orUbuntu18.04
Experience the module application
# Download the TinyMS project git clone https://github.com/tinyms-ai/tinyms.git cd tinyms/tests/st/app/object_detection/ # Run static image detection python opencv_image_app.py
The image to be detected and the image after inference are shown as follows:
Input Image
Reasoning Image
Real-time dynamic detection of video images collected by computer camera
Environmental preparation:
An operating system with a visual desktop, such as
Window_x64
orUbuntu18.04
Make sure the operating system can access the camera normally
Note:
Generally speaking, for the operating system under the host, such as
Window_x64
andUbuntu 18.04
, the camera can be accessed normally, but for the operating system under the virtual machine, please make sure that the relevant virtual machine services are enabled and the camera driver is connected. Following, we take the virtual machineVMware Workstation
under the window as an example:First of all, we enter the command
ls /dev/v*
in the terminal to check whether there is a/dev/video0
driver. If there is, it means it is normal, please ignore the following operations, if not, we will perform the following operations.Secondly, enable the relevant virtual machine services. enable the service
VMware USB Arbitration Service
in the windows host, that is, enterservices.msc
on the keyboardWin+R
to find the corresponding service and enable it. After it is turned on, the virtual machine needs to be restarted.Then, connect the camera driver. On the menu bar of
VMware Workstation
, clickVirtual Machine (M)
=>Removable Device
=>Camera Name
=>Host Connection
, and clickVirtual Machine (M)
=>Settings(S)
=>USB
, selectUSB3.0
.Finally, You can use
cheese
to test whether the camera can be accessed normally.
Experience the module application
For the different choices for operating systems and different choices for testing methods, we provide the following specific environment experiences, please verify in the corresponding environment, note that the following environments have all satisfy the two conditions for environmental preparation, which is very important.
If your host operating system is windows
If you test on the host
Environmental Requirements:
Operating system:
Window_64
Environmental dependency:
Git
+Python 3. 7.5
+TinyMS 0.3.1
+Microsoft Visual C++ 14.0 or greater
Command line tool:
Git Bash
Note: For details about the environment dependency of VC++ 14.0, please refer to the notes under Pypi install TinyMS
Execute the following commands after the environment requirements are satisfied:
# 1.Download the TinyMS project in the container git clone https://github.com/tinyms-ai/tinyms.git cd tinyms/tests/st/app/object_detection/ # 2.Run dynamic video image detection collected by camera python opencv_camera_app.py
If you test on a virtual machine
Let’s take the virtual machine
VMware Workstation
underWindow_x64
as an example. Please refer to the notes in the environment preparation for VM connection to the camera:Environmental Requirements:
Operating system:
Ubuntu18.04 LTS Desktop
Environmental dependency:
Docker 18.06.1-ce
Command line tool:
Terminal
Execute the following commands after the environment requirements are satisfied:
# 1.Install xServer on the host and set permissions apt install x11-xserver-utils # 2.Allow all users to access the display interface xhost + # 3.Run container 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.Download the TinyMS project in the container git clone https://github.com/tinyms-ai/tinyms.git cd tinyms/tests/st/app/object_detection/ # 5.Run dynamic video image detection collected by camera python opencv_camera_app.py
If your host operating system is ubuntu
If you test on the host
Environmental Requirements:
Operating system:
Ubuntu 18.04 LTS Desktop
Environmental dependency:
Git
+Python 3. 7.5
+TinyMS 0.3.1
Command line tool:
Terminal
Execute the following commands after the environment requirements are satisfied:
# 1.Download the TinyMS project in the container git clone https://github.com/tinyms-ai/tinyms.git cd tinyms/tests/st/app/object_detection/ # 2.Run dynamic video image detection collected by camera python opencv_camera_app.py
If you use docker access
Environmental Requirements:
Operating system:
Ubuntu18.04 LTS Desktop
Environmental dependency:
Docker 18.06.1-ce
Command line tool:
Terminal
Execute the following commands after the environment requirements are satisfied:
# 1.Install xServer on the host and set permissions apt install x11-xserver-utils # 2.Allow all users to access the display interface xhost + # 3.Run container 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.Download the TinyMS project in the container git clone https://github.com/tinyms-ai/tinyms.git cd tinyms/tests/st/app/object_detection/ # 5.Run dynamic video image detection collected by camera python opencv_camera_app.py
Currently, the document is still being improved:smile:. If your environment is not in the above reference, after you try it, you still have problems, we sincerely invite you to submit your issues and pull requests in our community, and we will reply you as soon as possible.
TinyMS ResNet50 Tutorial¶
In this tutorial, using TinyMS API to train/serve a ResNet50 model will be demonstrated.
Prerequisite¶
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
Introduction¶
TinyMS is a high-level API which is designed for amateur of deep learning. It minimizes the number of actions of users required to construct, train, evaluate and serve a model. TinyMS also provides tutorials and documentations for developers.
This tutorial consists of six parts, constructing the model
, downloading dataset
, training
, define servable json
, starting server
and making predictions
in which the server will be run in a sub process.
[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(12569:140321685239616,MainProcess):2021-03-19-15:21:33.633.399 [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. Construct the model¶
TinyMS encapsulates init and construct of the ResNet50 model, the line of the code is reduced to construct the model:
[2]:
# build the network
net = resnet50(class_num=10)
model = Model(net)
2. Download dataset¶
Training ResNet50 model with cifar10 dataset will be demonstrated, while we provide two pre-trained ckpt files for users to download, one is trained with cifar10
dataset and the other one is trained with ImageNet2012 dataset.
[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.81%************Download complete*************
3. Train the model & evaluation¶
The dataset for both training and evaluation will be defined here, and the parameters for training also set in this block. A trained ckpt file will be saved to /etc/tinyms/serving/resnet50_cifar10
folder for later use, meanwhile the evaluation will be performed and the Accuracy
can be checked
Notice: Since training ResNet50 on CPU is time consuming, we recommend skip training and using provided ckpt files to run.
[ ]:
# check ckpt 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
else:
print('resnet50_cifar10 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
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))
# 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(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))
Notice: If skipped training process, download the pretrained ckpt files and continue to serving
Click resnet_imagenet to download resnet-imagenet ckpt file and click resnet_cifar to download resnet-cifar ckpt file. Save this file to /etc/tinyms/serving/resnet50_<dataset_name>/resnet50.ckpt
.
Or run the following code to download the resnet_imagenet
and resnet_cifar
ckpt file:
[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:23:45-- 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, 121.36.121.44, 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 36.7MB/s in 5.9s
2021-03-19 15:23:52 (30.4 MB/s) - ‘/etc/tinyms/serving/resnet50_imagenet2012/resnet50.ckpt’ saved [188521005/188521005]
cifar10 ckpt folder already exists
--2021-03-19 15:23:52-- 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, 121.36.121.44, 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 35.7MB/s in 5.6s
2021-03-19 15:23:58 (32.3 MB/s) - ‘/etc/tinyms/serving/resnet50_cifar10/resnet50.ckpt’ saved [188462121/188462121]
4. Define servable.json¶
Choose only one of the following two code blocks to define the servable json file which will be used later
Run this code to define the servable json file for ResNet50_imagenet2012
model:
[5]:
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)
Or run this code to define the servable json file for ResNet50_cifar10
model:
[ ]:
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. Start server¶
5.1 Introduction¶
TinyMS Serving is a C/S(client/server) structure. There is a server and client. TinyMS using Flask which is a micro web framework written in python as the C/S communication tool. In order to serve a model, user must start server first. If successfully started, the server will be run in a subprocess and listening to POST requests from 127.0.0.1 port 5000 sent by client and handle the requests using MindSpore backend which will construct the model, run the prediction and send the result back to the client.
5.2 start server¶
Run the following code block to start the server:
[6]:
start_server()
Server starts at host 127.0.0.1, port 5000
6. Make predictions¶
6.1 Upload the pic¶
A picture is required to be the input. The resnet_imagenet
ckpt requires a mushroom picture, while the resnet_cifar
ckpt requires a picture of
['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
Click mushroom which is used in this tutorial for resnet_imagenet
and airplane for resnet-cifar
. Upload the pic, if using terminal, either scp
or wget
will do, if running in Jupyter, click Upload
button at the top right and select the picture. Save the picture at the root
folder, rename to mushroom.jpeg
(for resnet-imagenet) or airplane.jpg
(for resnet-cifar).
Or run this code to download mushroom
pic for resnet_imagenet
and airplane
for resnet_cifar
:
[7]:
# download mushroom pic
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')
# download airplane pic
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:24:11-- 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, 121.36.121.44, 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 370KB/s in 0.2s
2021-03-19 15:24:12 (370 KB/s) - ‘/root/mushroom.jpeg’ saved [76020/76020]
--2021-03-19 15:24:12-- 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, 121.36.121.44, 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 561KB/s in 0.3s
2021-03-19 15:24:12 (561 KB/s) - ‘/root/airplane.jpg’ saved [151188/151188]
6.2 List servables¶
Now, use list_servables
function to check what model is servable right now.
[8]:
list_servables()
[8]:
[{'description': 'This servable hosts a resnet50 model predicting mushrooms',
'model': {'class_num': 9, 'format': 'ckpt', 'name': 'resnet50'},
'name': 'resnet50_imagenet2012'}]
If the output description shows it is a resnet50
model, run the following code which will automatically detect whether it is a imagenet model or a cifar model
6.3 Sending request and get the result¶
Run predict
function to send the request, select between TOP1_CLASS
and TOP5_CLASS
to check the output
[9]:
# set image_path and output strategy
imagenet_image_path = "/root/mushroom.jpeg"
cifar_image_path = "/root/airplane.jpg"
strategy = "TOP1_CLASS"
# predict(image_path, servable_name, dataset_name, strategy='TOP1_CLASS')
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')

TOP1: Amanita毒蝇伞,伞菌目,鹅膏菌科,鹅膏菌属,主要分布于我国黑龙江、吉林、四川、西藏、云南等地,有毒, score: 0.99750286340713500977
Check output¶
If user runs resnet_imagenet
and see output similar to this:
TOP1: Amanita毒蝇伞,伞菌目,鹅膏菌科,鹅膏菌属,主要分布于我国黑龙江、吉林、四川、西藏、云南等地,有毒, score: 0.99119007587432861328
that means the prediction result is returned and the inference is completed.
Or user runs resnet_cifar
, and the output is expected to be like this:
TOP1: airplane, score: 0.99997282028198242188
## Change model
Run the following code to train a ResNet50 model with ImageNet2012
mushroom dataset, then run another servable_json
code block to define servable. The dataset will be downloaded here.
[ ]:
# 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))
Shutdown server¶
[10]:
shutdown()
[10]:
'Server shutting down...'
TinyMS MobileNetV2 Tutorial¶
In this tutorial, using TinyMS API to train/serve a MobileNetV2 model will be demonstrated.
Prerequisite¶
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
Introduction¶
TinyMS is a high-level API which is designed for amateur of deep learning. It minimizes the number of actions of users required to construct, train, evaluate and serve a model. TinyMS also provides tutorials and documentations for developers.
This tutorial consists of six parts, constructing the model
, downloading dataset
, training
, define servable json
, starting server
and making predictions
in which the server will be run in a sub process.
[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(8895:140407127349056,MainProcess):2021-03-19-15:03:40.515.970 [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. Construct the model¶
TinyMS encapsulates init and construct of the MobileNetV2 model, the line of the code is reduced to construct the model:
[2]:
# build the model
net = mobilenetv2(class_num=10, is_training=True)
model = Model(net)
2. Download dataset¶
The cifar10 dataset will be downloaded if cifar10
folder didn’t exist at the root. If cifar10
folder already exists, this step will not be performed.
[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 **************
[████████████████████████████████████████████████████████████████████████████████████████████████████] 100.00%
============== /root/cifar10/cifar-10-binary.tar.gz is ready ==============
************Download complete*************
3. Train the model & evaluation¶
The dataset for both training and evaluation will be defined here, and the parameters for training also set in this block. A trained ckpt file will be saved to /etc/tinyms/serving/mobilenetv2
folder for later use, meanwhile the evaluation will be performed and the Accuracy
can be checked
Notice: Since training MobileNetV2 on CPU is time consuming, we recommend skip training and using provided ckpt files to run.
[ ]:
# check ckpt folder exists or not
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')
# Declare common variables
epoch_size = 60 # default is 60
batch_size = 32
class_num = 10
# set runtime environment
device_target="CPU"
dataset_sink_mode = False
context.set_context(mode=context.GRAPH_MODE, device_target=device_target)
# create cifar10 dataset
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()
# define the loss function
label_smooth = 0.1
loss = CrossEntropyWithLabelSmooth(smooth_factor=label_smooth,num_classes=class_num)
# get learning rate
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)
# define the 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))
Notice: If skipped training process, download the pretrained ckpt file and continue to serving
Click HERE to download MobileNetV2 ckpt file and save this file to /etc/tinyms/serving/mobilenetv2/mobilenetv2.ckpt
.
Or run the following code to download and store the ckpt file:
[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:06:26-- 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.5, 49.4.112.90, 49.4.112.113, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.5|: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.1MB/s in 1.1s
2021-03-19 15:06:29 (16.1 MB/s) - ‘/etc/tinyms/serving/mobilenetv2/mobilenetv2.ckpt’ saved [18509001/18509001]
4. Define servable.json¶
Define the MobileNetV2 servable json file for model name, format and number of classes for later use.
[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. Start server¶
5.1 Introduction¶
TinyMS Serving is a C/S(client/server) structure. There is a server and client. TinyMS using Flask which is a micro web framework written in python as the C/S communication tool. In order to serve a model, user must start server first. If successfully started, the server will be run in a subprocess and listening to POST requests from 127.0.0.1 port 5000 sent by client and handle the requests using MindSpore backend which will construct the model, run the prediction and send the result back to the client.
5.2 start server¶
Run the following code block to start the server:
[6]:
start_server()
Server starts at host 127.0.0.1, port 5000
6. Make predictions¶
6.1 Upload the pic¶
A picture is required to be the input. This ckpt requires a picture containing the following objects:
['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
Click airplane to download the picture used in this tutorial. Upload the pic, if using terminal, either scp
or wget
will do, if running in Jupyter, click Upload
button at the top right and select the picture.
Save the picture at the root folder, rename to airplane.jpg
(or any name you want).
Or run the following code to download the picture used in this tutorial
[7]:
# download the airplane pic
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:06:37-- 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.5, 49.4.112.90, 49.4.112.113, ...
Connecting to ascend-tutorials.obs.cn-north-4.myhuaweicloud.com (ascend-tutorials.obs.cn-north-4.myhuaweicloud.com)|49.4.112.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 151188 (148K) [image/jpeg]
Saving to: ‘/root/airplane.jpg’
airplane.jpg 100%[===================>] 147.64K 560KB/s in 0.3s
2021-03-19 15:06:38 (560 KB/s) - ‘/root/airplane.jpg’ saved [151188/151188]
6.2 List servables¶
Now, we can use list_servables
function to check what model is servable right now.
[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'}]
If the output description
shows it is a MobileNetV2
model, then we can continue to next step to send our request.
6.3 Sending request and get the result¶
Run predict
function to send the request, select between TOP1_CLASS
and TOP5_CLASS
to check the output:
[9]:
# set image path and output strategy(TOP1_CLASS or 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")

TOP1: airplane, score: 0.22268821299076080322
Check output¶
If user can see output similar to this:
TOP1: airplane, score: 0.22268821299076080322
that means the prediction result is returned and the inference is completed.
## Shutdown server
To restart and try another checkpoint file, click Kernel
at the top, then Restart & Clear Output
, replace the servable_json
code and predict()
function
Run the following code to shutdown Flask server:
[10]:
shutdown()
[10]:
'Server shutting down...'
TinyMS SSD300 Tutorial¶
In this tutorial, using TinyMS API to train/serve an SSD300 model will be demonstrated.
Prerequisite¶
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
Introduction¶
TinyMS is a high-level API which is designed for amateur of deep learning. It minimizes the number of actions of users required to construct, train, evaluate and serve a model. TinyMS also provides tutorials and documentations for developers.
This tutorial consists of six parts, constructing the model
, downloading dataset
, training
, define servable json
, starting server
and making predictions
in which the server will be run in a sub process.
[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(14174:140126738016064,MainProcess):2021-03-19-15:33:42.136.028 [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. Construct the model¶
[2]:
# build network
net = ssd300_mobilenetv2(class_num=21)
2. Download dataset¶
The VOC dataset will be downloaded if voc
folder didn’t exist at the root. If voc
folder already exists, this step will not be performed.
[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%
============== /root/voc/VOCtrainval_06-Nov-2007.tar is ready ==============
************Download complete*************
3. Train the model & evaluation¶
The dataset for both training and evaluation will be defined here, and the parameters for training also set in this block. A trained ckpt file will be saved to /etc/tinyms/serving/ssd300
folder for later use, meanwhile the evaluation will be performed and the Accuracy
can be checked
Notice: Since training SSD300 on CPU is time consuming, we recommend skip training and using provided ckpt files to run.
[ ]:
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
# check ckpt folder exists or not
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')
# set parameters
epoch_size = 800 # default is 800
batch_size = 32
voc_path = '/root/voc/VOCdevkit/VOC2007'
# set environment parameters
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
dataset_sink_mode = False
# set dataset parameters
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()
# define the loss function
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))
# define the 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}")
Notice: If skipped training process, download the pretrained ckpt file and continue to serving
Click HERE to download the ckpt file and save this file to /etc/tinyms/serving/ssd300/ssd300.ckpt
.
Or run the following code to download and store the ckpt file:
[4]:
ssd300_ckpt_folder = '/etc/tinyms/serving/ssd300'
ssd300_ckpt_path = '/etc/tinyms/serving/ssd300/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:38:53-- 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, 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: 28056511 (27M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/ssd300/ssd300.ckpt’
ssd300.ckpt 100%[===================>] 26.76M 20.7MB/s in 1.3s
2021-03-19 15:38:55 (20.7 MB/s) - ‘/etc/tinyms/serving/ssd300/ssd300.ckpt’ saved [28056511/28056511]
4. Define servable.json¶
Run this code to define the servable json file for later use:
[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. Start server¶
5.1 Introduction¶
TinyMS Serving is a C/S(client/server) structure. There is a server and client. TinyMS using Flask which is a micro web framework written in python as the C/S communication tool. In order to serve a model, user must start server first. If successfully started, the server will be run in a subprocess and listening to POST requests from 127.0.0.1 port 5000 sent by client and handle the requests using MindSpore backend which will construct the model, run the prediction and send the result back to the client.
5.2 start server¶
Run the following code block to start the server:
[6]:
start_server()
Server starts at host 127.0.0.1, port 5000
6. Make predictions¶
6.1 Upload the pic¶
A picture is required to be the input. In this tutorial, the SSD300 ckpt requires a picture containing objects of
['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
Click HERE to download the picture which is used in this tutorial. Upload the pic, if using terminal, either scp
or wget
will do, if running in Jupyter, click Upload
button at the top right and select the picture. Save the picture at the root folder, rename to ssd300_test.jpeg
(or any name you want).
Or run the following code to download the picture used in this tutorial:
[7]:
# download the test pic
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:38:59-- 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, 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: 70412 (69K) [image/jpeg]
Saving to: ‘/root/ssd300_test.jpeg’
ssd300_test.jpeg 100%[===================>] 68.76K 338KB/s in 0.2s
2021-03-19 15:39:00 (338 KB/s) - ‘/root/ssd300_test.jpeg’ saved [70412/70412]
6.2 List servables¶
Now, use list_servables
function to check what model is servable right now.
[8]:
list_servables()
[8]:
[{'description': 'This servable hosts an ssd300 model predicting bounding boxes',
'model': {'class_num': 21, 'format': 'ckpt', 'name': 'ssd300'},
'name': 'ssd300'}]
If the output description shows it is an ssd300
model, run the following code to predict the bounding boxes
6.3 Sending request and get the result¶
Run predict
function and get the result, right now only TOP1CLASS
strategy is supported for SSD300. Call ImageViewer.draw
to draw the bounding boxes
[9]:
# set image path and output strategy(only 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")

Check output¶
If the input picture is shown with bounding boxes labeled with object classes and score, that means the prediction is successfully performed.
Shutdown server¶
[10]:
shutdown()
[10]:
'Server shutting down...'
TinyMS CycleGAN Tutorial¶
In this tutorial, using TinyMS API to train/serve a CycleGAN model will be demonstrated.
Prerequisite¶
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
Introduction¶
TinyMS is a high-level API which is designed for amateur of deep learning. It minimizes the number of actions of users required to construct, train, evaluate and serve a model. TinyMS also provides tutorials and documentations for developers.
This tutorial consists of five parts, downloading dataset
, training
, define servable json
, starting server
and making predictions
in which the server will be run in a sub process.
[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(25552:140556571072320,MainProcess):2021-03-21-15:01:26.554.568 [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. Download dataset¶
In this tutorial, the cityscapes dataset is used and processed. Click the link before proceeding to the official website to submit and download the dataset.
2. Train the model & evaluation¶
Define parameters and training process
Notice: Training on CPU is time consuming, we recommend skip training and using provided ckpt files to run.
[ ]:
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)
Notice: If skipped training process, download the pretrained ckpt file and continue to serving
Click G_A to download G_A.ckpt file and G_B to download G_B.ckpt file. Save them to /etc/tinyms/serving/cyclegan_cityscape/
Or run the following code to download and store the ckpt files:
[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:01:30-- 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)... 49.4.112.113, 121.36.121.44, 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: 7785480 (7.4M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/cyclegan_cityscape/G_A.ckpt’
G_A.ckpt 100%[===================>] 7.42M 3.58MB/s in 2.1s
2021-03-21 15:01:33 (3.58 MB/s) - ‘/etc/tinyms/serving/cyclegan_cityscape/G_A.ckpt’ saved [7785480/7785480]
--2021-03-21 15:01:34-- 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)... 49.4.112.113, 121.36.121.44, 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: 7785480 (7.4M) [binary/octet-stream]
Saving to: ‘/etc/tinyms/serving/cyclegan_cityscape/G_B.ckpt’
G_B.ckpt 100%[===================>] 7.42M 9.62MB/s in 0.8s
2021-03-21 15:01:36 (9.62 MB/s) - ‘/etc/tinyms/serving/cyclegan_cityscape/G_B.ckpt’ saved [7785480/7785480]
3. Define servable.json¶
Define the servable json file for model name, format and number of classes for later use.
[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. Start server¶
4.1 Introduction¶
TinyMS Serving is a C/S(client/server) structure. There is a server and client. TinyMS using Flask which is a micro web framework written in python as the C/S communication tool. In order to serve a model, user must start server first. If successfully started, the server will be run in a subprocess and listening to POST requests from 127.0.0.1 port 5000 sent by client and handle the requests using MindSpore backend which will construct the model, run the prediction and send the result back to the client.
4.2 start server¶
Run the following code block to start the server:
[4]:
start_server()
Server starts at host 127.0.0.1, port 5000
5. Make predictions¶
5.1 List servables¶
Now, we can use list_servables
function to check what model is servable right now.
[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'}]
If the output description
shows it is a CycleGAN
model, then we can continue to next step to send our request.
5.2 Sending request and get the result¶
Run predict
function to send the request, in this tutorial, both gray to color
and color to gray
will be demonstrated. Recommend using pics from cityscapes/test
to run the predict. If user using own pics, resize to 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")

Check output¶
If user can see 4 pics, that means two on the left column are from original test datset, while two pic on the right column are the generated pic.
## Shutdown server
To restart and try another checkpoint file, click Kernel
at the top, then Restart & Clear Output
, replace the servable_json
code and predict()
function
Run the following code to shutdown Flask server:
[7]:
shutdown()
[7]:
'Server shutting down...'
TinyMS Online Video Tutorial¶
In order to facilitate the learning of machine learning beginners, TinyMS provides a full set of online video tutorials, which will be updated simultaneously with the iteration of each version. We update and archive according to the content of each episode, including but not limited to video URLs, learning documents and Q&A. If you encounter any questions during the learning process, welcome to add the WeChat of the assistant (Wechat ID: mindspore0328, annotation: TinyMS). The assistant will add you to the TinyMS course learning group, and communicate with developers in the group in time. Of course, we also welcome you to directly pull Issue when you encounter problems, which can also help other developers who encounter the same problem.
Design Concept¶
Background¶
In recent years, with the rapid development of AI technology, deep learning frameworks such as TensorFlow, PyTorch, Apache MXNet and MindSpore have emerged. These frameworks are very good at solving problems for academic research or commercial production, however for first time beginners or application developers with limited deep learning knowledge, much simpler APIs are desired. Alongside the existing efforts like Keras for TensorFlow and Fastai for PyTorch to address the issue, the TinyMS project is a new addition in this field to provide simple high level APIs, tiny runtime footprint, modular development and agile deployment. TinyMS begins with initial focus on MindSpore integration and looks forward to more framework adaptations in the long run.
Interestingly enough, MindSpore’s high-level and mid-level Python APIs have already implemented most of the functions of Keras, and also been on par with Fastai’s design for PyTorch’s flexibility. Therefore unlike Keras and Fastai which are developed with most of their goals to compensate the underlying frameworks, TinyMS is designed to further enhance the experience of the framework, especially for all scenario development in the case of MindSpore.
With the help of TinyMS, the following goals should be achieved:
Quicker to learn: Get started with AI application development in one minute
Easier to develop: Complete the task of changing AI models and dataset from one to the other in one hour
Architecture¶
Design goals of TinyMS:
High level API that are extremely simple to learn and use.
Support complete AI development workflow from data preparation to model training/inference and finally deployment.
Decoupled modules that are could be easily extended.
Small runtime footprint that could be used on mobile, edge or cloud.
Standardizing spec for model training script format.
TinyMS Architecture
Workflow analysis¶
Typical model development workflow:
Data Acquisition: Dataset download, decompression, loading, etc.
Data Processing: Data preprocessing (enhancement) operations performed on the original dataset for better model performance.
Model Construction: Construction of the network, and also the definition of loss function, optimizer, etc.
Model Training: The process of model training, including the definition of callbacks
Accuracy Verification: The process of model accuracy verification, including the definition of metrics
Model Deployment: Model application services via an inference server
TinyMS Workflow
Module design¶
TinyMS has the following modules:
Name | Introduction | Example Code |
---|---|---|
app | Support OpenCV to Achieve Model Reasoning Visualization | from tinyms.app import object_detection |
data | Dataset Loading and Downloading | from tinyms.data import MnistDataset, download_dataset |
hub | Pre-trained Model Hub for Inference and Transfer Learning | from tinyms import hub |
model | Model High Level API and Predefined Network | from tinyms.model import Model, lenet5 |
serving | Model Serving | from tinyms.serving import predict |
vision | Computer Vision Related Data Processing | from tinyms.vision import mnist_transform, Resize |
text | Natural Language Processing Related Data Processing | from tinyms.text import Lookup |
callbacks | Callbacks During Model Training | from tinyms.callbacks import ModelCheckpoint |
common | Basic Components Including Tensor, Numpy Style Functions | from tinyms import Tensor, array |
context | Global Context | from tinyms import context |
initializers | Ops Weight Initialization | from tinyms.initializers import Normal |
layers | Neural Network Layer | from tinyms.layers import Layer, Conv2d |
losses | Loss Function | from tinyms.losses import SoftmaxCrossEntropyWithLogits |
metrics | Metrics For Model Verification | from tinyms.metrics import Accuracy |
optimizers | Optimizer | from tinyms.optimizers import Momentum |
primitives | Basic Ops | from tinyms.primitives import Add, tensor_add |
Implementation¶
Data loading (data)¶
The data loading module is mainly divided into two parts: dataset download and loading. Through TinyMS’s data loading API, developers can complete the entire process of downloading, decompressing, formatting, and loading common datasets with just two lines of code.
Most AI frameworks do not provide an interface for dataset download. Users need to prepare the dataset in advance, and at the same time, adjust the format (training/validation data set division, etc.) according to the data loading API format provided by the framework itself. To make dataset API usage much more easier, TinyMS provides the download_dataset
interface, which supports users to complete the download, decompression and format adjustment operations of the data set with one click; use Mnist dataset as an example:
from tinyms.data import download_dataset
mnist_path = download_dataset('mnist', local_path='./')
For data loading operations, TinyMS completely inherits MindSpore’s native Data Loading API. Users can use the xxxDataset interface to instantiate different data sets very conveniently. Take MnistDataset as an example:
from tinyms.data import MnistDataset
mnist_ds = MnistDataset(mnist_path, shuffle=True)
Data preprocessing (vision, text)¶
Usually in the model development workflow, data processing presents a big challenge: insufficient data, heavy manual labeling task, irregular data format and many other issues. Any of them could affect the network accuracy after training. Most frameworks provide data processing modules. Take MindSpore as an example, it currently provides data processing functions for common scenarios such as CV and NLP (for relevant interface definitions, please refer to mindspore.dataset.vision
and mindspore.dataset.text
), the user can directly call the preset data processing operator to process pictures or text, and then construct a data processing pipeline to efficiently parallelize massive data (see here).
TinyMS has made further abstraction and encapsulation on the basis of MindSpore, and directly corresponds to the processing of the dataset itself through the DatasetTransform
interface, allowing users to utilize a single piece of data or the entire dataset with just one line of code regarding preprocessing operation; take MnistTransform
as an example:
from PIL import Image
from tinyms.vision import mnist_transform
# Preprocessing a single one picture
img = mnist_transform(Image.open('picture.jpg'))
# Apply preprocessing to MnistDataset class instance
mnist_ds = mnist_transform.apply_ds(mnist_ds)
Model construction (model)¶
As the core of deep learning model development, the framework’s main responsibility is to provide complete operator expressions to build different network structures. Therefore, the interfaces at the framework level focus more on functional completeness and flexibility, whereas ModelZoo is provided for application development. TinyMS encapsulates the relevant network call API on the ModelZoo script; take the LeNet5
network as an example:
from tinyms.model import lenet5
net = lenet5(class_num=10)
In addition to encapsulating the commonly used network structures, TinyMS also provides a Model
high-level API interface (based on MindSpore Model interface package), by drawing on the design idea of Keras Model interface, it not only improves the original API functionalities, but also provides a consistent development experience for Keras users who wants to try TinyMS:
from tinyms.model import Model
model = Model(net)
model.compile(loss_fn=net_loss, optimizer=net_opt)
Model training (losses, optimizers, callbacks)¶
For the model training phase, the most important factors are the definitions of loss functions, optimizers, and callback functions. For beginners, it is not difficult to understand the basic principles of loss functions and optimizers, but a strong mathematical background is required to understand the principles of implementation. Therefore, the TinyMS high-level API encapsulates the loss function and optimizer at the network level, so that users can complete the initialization work with one line of code whether they are training simple or complex networks; take the LeNet5
network as an example:
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)
Regarding the definition of callback functions, in addition to commonly used callback functions (such as TimeMonitor
, LossMonitor
, etc.), MindSpore itself provides Callback interface to facilitate user-defined callback functions. The TinyMS high-level API also provides network-level encapsulation, so that users can complete the initialization of the callback function with one line of code; take the MobileNetV2
network as an example:
from tinyms.callbacks import mobilenetv2_cb
net_cb = mobilenetv2_cb(device_target, lr, is_saving_checkpoint, save_checkpoint_epochs, step_size)
Model evaluating (metrics)¶
Model accuracy verification is an indispensable process to verify whether the model accuracy meets the SOTA criteria. MindSpore natively provides measurement interfaces for indicators such as Accuracy
and Precision
(see here), while providing users with a custom measurement interface Metric
. In terms of metric measurement, TinyMS directly inherits the native MindSpore API:
from tinyms.model import Model
from tinyms.metrics import Accuracy
model = Model(net)
model.compile(metrics={"Accuracy": Accuracy())
Pre-trained model loading (hub)¶
TinyMS Hub is a pre-trained model application tool, serving as a channel for model developers and application developers.
Provide model developers with a convenient and fast channel for model release and submission.
Provide application developers with high-quality pre-trained models, and complete the work of model migration to deployment quickly using model loading and fine-tuning APIs.
Current pre-trained models in TinyMS Hub mainly cover four mainstream task scenarios including image classification
, object detection
, semantic segmentation
and recommendation
.
There are several of scenarios for users to leverage hub
to easily load the pre-trained model:
Load pre-trained model
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))
Load model checkpoint
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)
Load model weights
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)
Model deployment (serving)¶
Model deployment refers to the process of servicing pre-trained models so that they can quickly and efficiently process data input by users and obtain results. MindSpore provides the predict function for inference. 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 ; Take the LeNet5
network as an example:
from tinyms.serving import Server, Client
server = Server()
# Start prediction server
server.start_server()
client = Client()
# List all servables available
client.list_servables()
# Call predict interface
client.predict(image_path, 'lenet5', dataset_name='mnist')
# Shutdown the prediction server
server.shutdown()
In addition, TinyMS also provides a web visualization interface, convenient for users to directly upload a picture on the web page for reasoning, currently mainly support LeNet5
, CycleGan
and SSD300
network. The developers only need to start the backend reasoning server, then deploy the front-end server through Nginx web server, The front-end project is stored in the tinyms/serving/web directory
of the TinyMS project. If users want to try quickly, can visit Install TinyMS Nginx version
section:
# Start web backend server
from tinyms.serving import Server
server = Server()
server.start_server()
Model reasoning visualization application(app)¶
OpenCV is a library for computer vision, and TinyMS is a high-level API library for deep learning frameworks. Usually after training, when we need to load the pre-training model to verify the effect of the model, the result is usually a bunch of numbers. The data is boring and unintuitive for beginners, and it is very difficult to understand what they represent. Therefore, TinyMS takes model reasoning visualization as its main feature in version 0.3.0, and combines OpenCV to realize real-time detecting and visual detection of images to help users see the effect of reasoning more intuitively. At present, the visual reasoning module only supports object detection model SSD300
, and more image processing models will be added in the future.
Below, I will demonstrate how to use a trained model to detect static images and live, moving video images captured by computer cameras in just 5
steps:
Static image object detection
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.Load and parse the config json file
config_path = '**/ssd300_shanshui.json'
config = load_and_parse_config(config_path=config_path)
# 2.Generate the instance of ObjectDetector
detector = ObjectDetector(config=config)
# 3.Read the input image using OpenCV
img_path = ('./pic/test.jpeg)
image_np = cv2.imread(img_path)
input = image_np.copy()
# 4.Detect the input image
detection_bbox_data = object_detection_predict(input, detector, is_training=False)
# 5.Draw the box for the input image and and view it using 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)
Real-time dynamic detection of video images collected by computer camera
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.Load and parse the config json file
config_path = "**/tinyms/app/object_detection/configs/tinyms/0.3/ssd300_voc.json"
config = load_and_parse_config(config_path=config_path)
# 2.Generate the instance of ObjectDetector
detector = ObjectDetector(config=config)
cap = cv2.VideoCapture(0)
while True:
# 3.Read the frame image from the camera using OpenCV
ret, image_np = cap.read()
input = image_np.copy()
# 4.Detect the input frame image
detection_bbox_data = object_detection_predict(input, detector, is_training=False)
# 5.Draw the box for the input frame image and view it using 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.
Examples
>>> import tinyms as ts
>>>
>>> print(ts.ones([2, 3]))
[[1. 1. 1.]
[1. 1. 1.]]
-
class
tinyms.
QuantDtype
[source]¶ An enum for quant datatype, contains INT1 ~ INT16, UINT1 ~ UINT16.
QuantDtype is defined in mindspore/common/dtype.py, use command below to import:
from mindspore import QuantDtype
-
tinyms.
dtype_to_nptype
(type_)[source]¶ Convert MindSpore dtype to numpy data type.
- Parameters:
type_ (
mindspore.dtype
) – MindSpore’s dtype.- Returns:
The data type of numpy.
-
tinyms.
dtype_to_pytype
(type_)[source]¶ Convert MindSpore dtype to python data type.
- Parameters:
type_ (
mindspore.dtype
) – MindSpore’s dtype.- Returns:
Type of python.
-
tinyms.
pytype_to_dtype
(obj)[source]¶ Convert python type to MindSpore type.
- Parameters:
obj (type) – A python type object.
- Returns:
Type of MindSpore type.
- Raises:
NotImplementedError – If the python type cannot be converted to MindSpore type.
-
tinyms.
get_py_obj_dtype
(obj)[source]¶ Get the MindSpore data type, which corresponds to python type or variable.
- Parameters:
obj (type) – An object of python type, or a variable of python type.
- Returns:
Type of MindSpore type.
-
class
tinyms.
Tensor
(input_data=None, dtype=None, shape=None, init=None, internal=False, const_arg=False)[source]¶ Tensor is a data structure that stores an n-dimensional array.
- Parameters:
input_data (Union[Tensor, float, int, bool, tuple, list, numpy.ndarray]) – The data to be stored. It can be another Tensor, Python number or NumPy ndarray. Default: None.
dtype (
mindspore.dtype
) – Used to indicate the data type of the output Tensor. The argument should be defined in mindspore.dtype. 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]) – Used to indicate the shape of the output Tensor. The argument should be a list of integers, a tuple of integers or an integer. If input_data is available, shape doesn’t need to be set. If None in shape, a tensor of dynamic shape is created, input_data doesn’t need to be set; if None not in shape, a tensor of static shape is created, input_data or init must 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 Tensor in the other conditions. If ‘init’ interface is used to initialize Tensor, the Tensor.init_data API needs to be called to convert Tensor to the actual data. Default: None.
internal (bool) – Whether it is created by the framework. ‘True’ means that the tensor is created by framework. ‘False’ means that the tensor is created by user. Default: False
const_arg (bool) – Whether the tensor is a constant when it is used for the argument of a network. Default: False.
- Outputs:
Tensor.
Note
The default value None of input_data works as a placeholder, it does not mean that we can create a NoneType Tensor.
Examples
>>> import numpy as np >>> import mindspore as ms >>> from mindspore import Tensor >>> from mindspore.common.initializer import One >>> # initialize a tensor with numpy.ndarray >>> t1 = Tensor(np.zeros([1, 2, 3]), ms.float32) >>> print(t1) [[[0. 0. 0.] [0. 0. 0.]]] >>> print(type(t1)) <class 'mindspore.common.tensor.Tensor'> >>> print(t1.shape) (1, 2, 3) >>> print(t1.dtype) Float32 >>> >>> # initialize a tensor with a float scalar >>> t2 = Tensor(0.1) >>> print(t2) 0.1 >>> print(type(t2)) <class 'mindspore.common.tensor.Tensor'> >>> print(t2.shape) () >>> print(t2.dtype) Float32 >>> >>> # initialize a tensor with a tuple >>> t3 = Tensor((1, 2)) >>> print(t3) [1 2] >>> print(type(t3)) <class 'mindspore.common.tensor.Tensor'> >>> print(t3.shape) (2,) >>> print(t3.dtype) Int64 ... >>> # initialize a tensor with init >>> t4 = Tensor(shape = (1, 3), dtype=ms.float32, init=One()) >>> print(t4) [[1. 1. 1.]] >>> print(type(t4)) <class 'mindspore.common.tensor.Tensor'> >>> print(t4.shape) (1, 3) >>> print(t4.dtype) Float32
-
property
H
¶ Returns a view of a matrix (2-D tensor) conjugated and transposed. x.H is equivalent to mindspore.Tensor.swapaxes(0, 1).conj() for complex matrices and mindspore.Tensor.swapaxes(0, 1) for real matrices.
-
property
T
¶ Return the transposed tensor.
-
addbmm
(batch1, batch2, *, beta=1, alpha=1)[source]¶ For details, please refer to
mindspore.ops.addbmm()
.
-
amax
(axis=None, keepdims=False, *, initial=None, where=None)[source]¶ For details, please refer to
mindspore.ops.amax()
.
-
amin
(axis=None, keepdims=False, *, initial=None, where=None)[source]¶ For details, please refer to
mindspore.ops.amin()
.
-
approximate_equal
(other, tolerance=1e-05)[source]¶ For details, please refer to
mindspore.ops.approximate_equal()
.
-
argmax_with_value
(axis=0, keep_dims=False)[source]¶ Returns the maximum value with corresponding index.
Compute the max value of input Tensor on the specified axis, and return the max value and index.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
Warning
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 this tensor.
- Parameters:
- Returns:
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)\) .
value (Tensor) - The maximum value of input tensor, with the same shape as index.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32) >>> output, index = x.argmax_with_value() >>> print(output, index) 0.7 3 >>> output, index = x.argmax_with_value(keep_dims=True) >>> print(output, index) [0.7] [3]
-
argmin_with_value
(axis=0, keep_dims=False)[source]¶ Returns the minimum value with corresponding index.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
Warning
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 this tensor.
- Parameters:
- Returns:
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)\) .
value (Tensor) - The minimum value of input tensor, with the same shape as index.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32) >>> output, index = x.argmin_with_value() >>> print(output, index 0.0 0 >>> output, index = x.argmin_with_value(keep_dims=True) >>> print(output, index) [0.0] [0]
-
asnumpy
()[source]¶ Convert tensor to numpy array. Returns self tensor as a NumPy ndarray. This tensor and the returned ndarray share the same underlying storage. Changes to self tensor will be reflected in the ndarray.
- Returns:
A numpy ndarray which shares the same underlying storage with the tensor.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor(np.array([1, 2], dtype=np.float32)) >>> y = x.asnumpy() >>> y[0] = 11 >>> print(x) [11. 2.] >>> print(y) [11. 2.]
-
asnumpy_of_slice_persistent_data
(param_key, slice_index)[source]¶ Convert a slice of tensor data to numpy array. A slice is part of tensor data. Returns as a NumPy ndarray. This slice tensor data and the returned ndarray share the same underlying storage. Changes to self tensor will be reflected in the ndarray.
- Returns:
A numpy ndarray which shares the same underlying storage with the slice of tensor data.
-
assign_value
(value)[source]¶ Assign another tensor value to this tensor.
- Parameters:
value (Tensor) – Tensor for assignment.
- Returns:
Tensor, Tensor that’s been assigned.
-
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.
Examples
>>> 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)[source]¶ Return a copy of the tensor, cast to a specified type.
- Parameters:
dtype (Union[
mindspore.dtype
, numpy.dtype, str]) – Designated tensor dtype, can be in format of mindspore.dtype.float32 or numpy.float32 or 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. Default: True.
- Returns:
Tensor, with the designated dtype.
- Raises:
TypeError – If the specified dtype cannot be understood.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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
-
baddbmm
(batch1, batch2, beta=1, alpha=1)[source]¶ For details, please refer to
mindspore.ops.baddbmm()
.
-
bitwise_right_shift
(other)[source]¶ For details, please refer to
mindspore.ops.bitwise_right_shift()
.
-
bool
()[source]¶ Converts input tensor dtype to bool. If the value in tensor is zero, it will be False, otherwise it will be True.
- Returns:
Tensor, converted to the bool dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.float32) >>> output = input_x.bool() >>> print(output.dtype) Bool
-
cholesky_inverse
(upper=False)[source]¶ For details, please refer to
mindspore.ops.cholesky_inverse()
.
-
choose
(choices, mode='clip')[source]¶ Construct a tensor from an index tensor and a list of tensors to choose from.
- Parameters:
choices (Union[tuple, list, Tensor]) – Choice tensors. The input tensor and all of the choices must be broadcasted to the same shape. If choices is itself a tensor, 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: Raises an error;
wrap: Wraps around;
clip: Clips to the range. ‘clip’ mode means that values greater than n-1 are mapped to n-1. Note that this disables indexing with negative numbers.
Default: ‘clip’.
- Returns:
Tensor, the merged result.
- Raises:
ValueError – If the input tensor and any of the choices cannot be broadcast.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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]
-
col2im
(output_size, kernel_size, dilation, padding_value, stride)[source]¶ For details, please refer to
mindspore.ops.col2im()
.
-
copy
()[source]¶ Return a copy of the tensor.
Note
The current implementation does not support order argument.
- Returns:
Copied tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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.]]
-
cov
(*, correction=1, fweights=None, aweights=None)[source]¶ For details, please refer to
mindspore.ops.cov()
.
-
data_sync
(self: mindspore._c_expression.Tensor, arg0: bool) → None¶
-
diagonal
(offset=0, axis1=0, axis2=1)[source]¶ For details, please refer to
mindspore.ops.diagonal()
.
-
diff
(n=1, axis=-1, prepend=None, append=None)[source]¶ For details, please refer to
mindspore.ops.diff()
.
-
dim
(self: mindspore._c_expression.Tensor) → int¶ Get tensor’s data dimension.
- Returns:
int, the dimension of tensor.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.dim() 2
-
property
dtype
¶ Return the dtype of the tensor (
mindspore.dtype
).
-
expand_as
(x)[source]¶ Expand the dimension of target tensor to the dimension of input tensor.
- Parameters:
x (Tensor) – The input tensor. The shape of the input tensor must obey the broadcasting rule.
- Returns:
Tensor, has the same dimension as input tensor.
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import dtype as mstype >>> x = Tensor([1, 2, 3], dtype=mstype.float32) >>> y = Tensor(np.ones((2, 3)), dtype=mstype.float32) >>> output = x.expand_as(y) >>> print(output) [[1. 2. 3.] [1. 2. 3.]]
-
flatten
(order='C', *, start_dim=0, end_dim=-1)[source]¶ For details, please refer to
mindspore.ops.flatten()
.
-
float
()[source]¶ Converts input tensor dtype to float32.
- Returns:
Tensor, converted to the float32 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.int32) >>> output = input_x.float() >>> print(output.dtype) Float32
-
flush_from_cache
()[source]¶ Flush cache data to host if tensor is cache enable.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor(np.array([1, 2], dtype=np.float32)) >>> y = x.flush_from_cache() >>> print(y) None
-
fold
(output_size, kernel_size, dilation=1, padding=0, stride=1)[source]¶ For details, please refer to
mindspore.ops.fold()
.
-
static
from_numpy
(array)[source]¶ Convert numpy array to Tensor. If the data is not C contiguous, the data will be copied to C contiguous to construct the tensor. Otherwise, The tensor will be constructed using this numpy array without copy.
- Parameters:
array (numpy.array) – The input array.
- Returns:
Tensor, has the same data type as input array.
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = np.array([1, 2]) >>> output = Tensor.from_numpy(x) >>> print(output) [1 2]
-
gather
(input_indices, axis, batch_dims=0)[source]¶ For details, please refer to
mindspore.ops.gather()
.
-
getitem_index_info
(self: object, arg0: object, arg1: bool_) → object¶
-
half
()[source]¶ Converts input tensor dtype to float16.
- Returns:
Tensor, converted to the float16 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.int32) >>> output = input_x.half() >>> print(output.dtype) Float16
-
property
has_init
¶ Whether tensor is initialized.
-
imag
()[source]¶ Returns a new tensor containing imaginary value of the input tensor. If input tensor is real, it will return zeros.
- Returns:
Tensor, the shape is the same as the input tensor.
- Supported Platforms:
GPU
CPU
Examples
>>> x = Tensor(np.asarray(np.complex(1.3 + 0.4j)), mindspore.complex64) >>> output = x.imag() >>> print(output) 0.4
-
index_add
(dim, index, source, *, alpha=1)[source]¶ For details, please refer to
mindspore.ops.index_add()
.
-
init_data
(slice_index=None, shape=None, opt_shard_group=None)[source]¶ Get the tensor format data of this Tensor.
Note
The init_data function can be called once for the same tensor.
- Parameters:
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.
- Returns:
Initialized Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore.common.initializer import initializer, Constant >>> x = initializer(Constant(1), [2, 2], ms.float32) >>> out = x.init_data() >>> print(out) [[1. 1.] [1. 1.]]
-
int
()[source]¶ Converts input tensor dtype to int32. If the value in tensor is float or half, the decimal will be discarded.
- Returns:
Tensor, converted to the int32 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.float32) >>> output = input_x.int() >>> print(output.dtype) Int32
-
is_init
(self: mindspore._c_expression.Tensor) → bool¶ Get tensor init_flag.
- Returns:
bool, whether the tensor init.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.is_init() False
-
is_persistent_data
()[source]¶ Check if size of tensor is huge, and need save data to persistent storage. If size of tensor is bigger then MS_EMBEDDING_REMOTE_CACHE_MEMORY_SIZE, it will use persistent storage to save tensor data. And will spilt data to some slice.
- Returns:
True or False
-
is_signed
()[source]¶ Judge whether the data type of tensor is a signed data type.
- Returns:
Bool. If the dtype of self is a signed data type, return True. Otherwise, return False.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor >>> x = ms.Tensor([1, 2, 3], ms.int64) >>> y = ms.Tensor([1, 2, 3], ms.uint64) >>> output = x.is_signed() >>> output2 = y.is_signed() >>> print(output) True >>> print(output2) False
-
isclose
(x2, rtol=1e-05, atol=1e-08, equal_nan=False)[source]¶ For details, please refer to
mindspore.ops.isclose()
.
-
item
(index=None)[source]¶ Get the item at the specified index of the tensor.
Note
Tensor.item returns a Tensor scalar instead of a Python scalar.
- Parameters:
index (Union[None, int, tuple(int)]) – The index in Tensor. Default: None.
- Returns:
A Tensor scalar, dtype is the same with the original Tensor.
- Raises:
ValueError – If the length of the index is not equal to self.ndim.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ 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\).
- Parameters:
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.
- Returns:
A new tensor that doesn’t affect the original tensor, with value set by \(Tensor[args] = item\).
- Raises:
ValueError – If the length of the first argument is not equal to self.ndim.
IndexError – If only one argument is provided, and the original Tensor is not scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32)) >>> print(x.itemset((0,1), 4)) [[1. 4. 3.] [4. 5. 6.]] >>> print(x) [[1. 2. 3.] [4. 5. 6.]]
-
property
itemsize
¶ Return the length of one tensor element in bytes.
-
log_matrix_determinant
()[source]¶ For details, please refer to
mindspore.ops.log_matrix_determinant()
.
-
long
()[source]¶ Converts input tensor dtype to int64. If the value in tensor is float or half, the decimal will be discarded.
- Returns:
Tensor, converted to the int64 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.int32) >>> output = input_x.long() >>> print(output.dtype) Int64
-
property
mH
¶ Accessing this property is equivalent to Calling self.adjoint(). For details, please refer to
mindspore.ops.adjoint()
.
-
property
mT
¶ Returns the Tensor that exchanges the last two dimensions. Accessing the attribute, x.mT, is equal to calling the method, x.swapaxes(-2, -1). For details, please refer to
mindspore.Tensor.swapaxes()
.
-
max
(axis=None, keepdims=False, *, initial=None, where=True, return_indices=False)[source]¶ Return the maximum of a tensor or maximum along an axis.
- Parameters:
axis (Union[None, int, list, 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.
- Keyword Arguments:
initial (scalar, optional) – The minimum value of an output element. Must be present to allow computation on empty slice. Default: None.
where (Tensor[bool], optional) – A boolean tensor 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.
return_indices (bool, optional) – Whether to return the index of the maximum value. Default: False. If axis is a list or tuple of ints, it must be False.
- Returns:
Tensor or scalar, maximum of input tensor. If axis is None, the result is a scalar value. If axis is given, the result is a tensor of dimension
self.ndim - 1
.- Raises:
TypeError – If arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.argmin()
: Return the indices of the minimum values along an axis.mindspore.Tensor.argmax()
: Return the indices of the maximum values along an axis.mindspore.Tensor.min()
: Return the minimum of a tensor or minimum along an axis.Examples
>>> 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 >>> value, indices = a.max(axis=0, return_indices=True) >>> print(value) [2. 3.] >>> print(indices) [1 1]
-
min
(axis=None, keepdims=False, *, initial=None, where=True, return_indices=False)[source]¶ Return the minimum of a tensor or minimum along an axis.
- Parameters:
axis (Union[None, int, list, tuple of ints], optional) – An axis or axes along which to operate. By default, flattened input is used. If axis 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 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.
- Keyword Arguments:
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 tensor 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.
return_indices (bool, optional) – Whether to return the index of the minimum value. Default: False. If axis is a list or tuple of ints, it must be False.
- Returns:
Tensor or scalar, minimum of input tensor. If axis is None, the result is a scalar value. If axis is given, the result is a tensor of dimension
self.ndim - 1
.- Raises:
TypeError – If arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.argmin()
: Return the indices of the minimum values along an axis.mindspore.Tensor.argmax()
: Return the indices of the maximum values along an axis.mindspore.Tensor.max()
: Return the minimum of a tensor or minimum along an axis.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> a = Tensor(np.arange(4).reshape((2, 2)).astype('float32')) >>> output = a.min() >>> print(output) 0.0 >>> output = a.min(axis=0) >>> print(output) [0. 1.] >>> output = a.min(axis=0, initial=9, where=Tensor([False])) >>> print(output) [9. 9.] >>> output = a.min(axis=0, initial=9, where=Tensor([False, True])) >>> print(output) [9. 1.] >>> value, indices = a.min(axis=0, return_indices=True) >>> print(value) [0. 1.] >>> print(indices) [0 0]
-
multinomial
(num_samples, replacement=True, seed=None)[source]¶ For details, please refer to
mindspore.ops.multinomial()
.
-
nan_to_num
(nan=0.0, posinf=None, neginf=None)[source]¶ For details, please refer to
mindspore.ops.nan_to_num()
.
-
nansum
(axis=None, keepdims=False, dtype=None)[source]¶ For details, please refer to
mindspore.ops.nansum()
.
-
property
nbytes
¶ Return the total number of bytes taken by the tensor.
-
property
ndim
¶ Return the number of tensor dimensions.
-
new_ones
(size, *, dtype=None)[source]¶ Return a tensor of size filled with ones.
- Parameters:
size (Union[int, tuple, list]) – An int, list or tuple of integers defining the output shape.
- Keyword Arguments:
dtype (mindspore.dtype, optional) – The desired dtype of the output tensor. If None, the returned tensor has the same dtype as self. Default: None.
- Returns:
Tensor, the shape and dtype is defined above and filled with ones.
- Raises:
TypeError – If size is not an int, list or tuple of integers.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> output = x.new_ones((2, 2)) >>> print(output) [[1. 1.] [1. 1.]]
-
new_zeros
(size, *, dtype=None)[source]¶ Return a tensor of size filled with zeros.
- Parameters:
size (Union[int, tuple, list]) – An int, list or tuple of integers defining the output shape.
- Keyword Arguments:
dtype (mindspore.dtype, optional) – The desired dtype of the output tensor. If None, the returned tensor has thesame dtype as self. Default: None.
- Returns:
Tensor, the shape and dtype is defined above and filled with zeros.
- Raises:
TypeError – If size is not an int, list or tuple of integers.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> output = x.new_zeros((2, 2)) >>> print(output) [[0. 0.] [0. 0.]]
-
norm
(ord=None, dim=None, keepdim=False, *, dtype=None)[source]¶ For details, please refer to
mindspore.ops.norm()
.
-
persistent_data_from_numpy
(self: array, arg0: int_) → mindspore._c_expression.Tensor¶ Creates a Tensor from a numpy.ndarray without copy. Use persistent data tensor.
- Arg:
array (numpy.ndarray): The input ndarray. slice_num (int): The slice num of persistent data tensor.
- Returns:
Tensor, tensor with shared data to input ndarray.
Examples
>>> a = np.ones((2, 3)) >>> t = mindspore.Tensor.persistent_data_from_numpy(a, 1)
-
ptp
(axis=None, keepdims=False)[source]¶ The name of the function comes from the acronym for “peak to peak”. Calculate the difference between the maximum value and the minimum value along the axis.
Note
Numpy argument out is not supported.
- Parameters:
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 tensor. 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 tensor. Default is False.
- Returns:
Tensor.
- Raises:
TypeError – If self is not a tensor, or axis and keepdims have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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.]
-
random_categorical
(num_sample, seed=0, dtype=mindspore.int64)[source]¶ For details, please refer to
mindspore.ops.random_categorical()
.
-
ravel
()[source]¶ Return a contiguous flattened tensor.
- Returns:
Tensor, a 1-D tensor, containing the same elements of the input.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.reshape()
: Give a new shape to a tensor without changing its data.mindspore.Tensor.flatten()
: Return a copy of the tensor collapsed into one dimension.Examples
>>> 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)[source]¶ Repeat elements of a tensor.
- Parameters:
- Returns:
Tensor, has the same shape as input tensor except along the given axis.
- Raises:
ValueError – If the axis is out of range.
TypeError – If arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.reshape()
: Give a new shape to a tensor without changing its data.mindspore.Tensor.resize()
: Changes shape and size of tensor in-place.Examples
>>> 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]]
-
repeat_interleave
(repeats, dim=None)[source]¶ For details, please refer to
mindspore.ops.repeat_interleave()
.
-
reshape_as
(other)[source]¶ Change the shape of the Tensor to the shape of other without changing the data.
- Parameters:
other (Tensor) – The result tensor has the same shape as other.
- Returns:
Tensor, has the same shape as other.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]], dtype=ms.float32) >>> y = Tensor(np.arange(6).reshape(3,2)) >>> output = x.reshape_as(y) >>> print(output) [[-0.1 0.3] [ 3.6 0.4] [ 0.5 -3.2]]
-
resize
(*new_shape)[source]¶ Changes shape and size of tensor in-place.
If the shape of the new tensor is larger than the shape of the original tensor, the new tensor will be filled with 0. And if the shape of the new tensor is smaller than the shape of the original tensor, the new tensor is filled with the elements of the original tensor in order.
Note
Instead of changing the size of the input tensor and returns nothing as in numpy, this method returns a new Tensor with the input size. Numpy argument refcheck is not supported.
- Parameters:
new_shape (Union[ints, tuple of ints]) – Shape of resized tensor.
- Returns:
Tensor.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.reshape()
: Give a new shape to a tensor without changing its data.mindspore.Tensor.repeat()
: Repeat elements of a tensor.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)) >>> y = x.resize(3, 3) >>> print(y) [[1. 2. 3.] [4. 5. 6.] [0. 0. 0.]] >>> y = x.resize(2, 2) >>> print(y) [[1. 2.] [3. 4.]]
-
reverse_sequence
(seq_lengths, seq_dim=0, batch_dim=0)[source]¶ For details, please refer to
mindspore.ops.reverse_sequence()
.
-
scatter_sub
(indices, updates)[source]¶ Creates a new tensor by subtracting the values from the positions in self tensor indicated 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
mindspore.ops.ScatterNdSub
, except that the updates are applied on output Tensor instead of input 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 self[indices]. For more details, see use cases.
Note
On GPU, if some values of the indices are out of bound, instead of raising an index error, the corresponding updates will not be updated to self tensor. On CPU, if some values of the indices are out of bound, raising an index error. On Ascend, out of bound checking is not supported, if some values of the indices are out of bound, unknown errors may be caused.
- Parameters:
- Returns:
Tensor, has the same shape and type as self tensor.
- Raises:
TypeError – If dtype of indices is neither int32 nor int64.
ValueError – If length of shape of self tensor is less than the last dimension of shape of indices.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype('float32')) >>> indices = Tensor(np.array([[0, 0], [0, 0]]).astype('int32')) >>> updates = Tensor(np.array([1.0, 2.2]).astype('float32')) >>> output = x.scatter_sub(indices, updates) >>> print(output) [[-3.3000002 0.3 3.6 ] [ 0.4 0.5 -3.2 ]]
-
searchsorted
(v, side='left', sorter=None)[source]¶ Finds indices where elements should be inserted to maintain order.
- Parameters:
v (Union[int, float, bool, list, tuple, Tensor]) – Values to insert into the tensor.
side (str, 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 the tensor). Default: ‘left’.
sorter (Union[int, float, bool, list, tuple, Tensor]) – 1-D optional tensor of integer indices that sort the tensor into ascending order. They are typically the result of argsort. Default: None.
- Returns:
Tensor, array of insertion points with the same shape as v.
- Raises:
ValueError – If argument for side or sorter is invalid.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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._c_expression.typing.Type = None) → None¶
-
set_const_arg
(const_arg=True)[source]¶ Specify whether the tensor is a constant when it is used for the argument of a network.
- Parameters:
const_arg (bool) – Whether the tensor is a constant when it is used for the argument of a network. Default: True.
- Returns:
Tensor, has been specified whether to be a const network argument.
- Raises:
TypeError – If const_arg is not a bool.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32)) >>> x.set_const_arg(True)
-
set_dtype
(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.typing.Type) → mindspore._c_expression.typing.Type¶ Set the tensor’s data type.
- Arg:
dtype (
mindspore.dtype
): The type of output tensor.
Examples
>>> 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.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.set_init_flag(True)
-
setitem_index_info
(self: object, arg0: object, arg1: object, arg2: bool_) → object¶
-
property
shape
¶ For details, please refer to
mindspore.ops.shape()
.
-
short
()[source]¶ Return a copy of the tensor, cast to int16 type, equivalent to self.astype(mstype.int16). If the value in tensor is float or half, the decimal will be discarded. For details, please refer to
mindspore.Tensor.astype()
.- Returns:
Tensor, converted to the int16 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> import numpy as np >>> x = ms.Tensor(np.array([1,2,3,4,5]), ms.int32) >>> output = x.short() >>> output Tensor(shape=[5], dtype=Int16, value= [1, 2, 3, 4, 5])
-
property
size
¶ For details, please refer to
mindspore.ops.size()
.
-
slice_num_of_persistent_data
()[source]¶ Get slice num of a tensor which use persistent storage.
- Returns:
Num of slice.
-
slice_shape_of_persistent_data
()[source]¶ Get slice shape of tensor after cut to slice size.
- Returns:
The slice shape of tensor.
-
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)[source]¶ Return sum of tensor elements over a given axis.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
axis (Union[None, int, tuple(int)]) – Axis or axes along which a sum is performed. Default: None. If None, sum all the elements of the input tensor. 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 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.
- Returns:
Tensor. A tensor with the same shape as input, with the specified axis removed. If the input tensor is a 0-d array, or if the axis is None, a scalar is returned.
- Raises:
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
See also
mindspore.Tensor.cumsum()
: Return the cumulative sum of the elements along a given axis.Examples
>>> 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.]
-
sum_to_size
(*size)[source]¶ Sum self Tensor to the size. size must be expandable to the Tensor size.
- Parameters:
size (Union[tuple(int), int]) – The expected shape of output Tensor.
- Returns:
Tensor, the sum result of self Tensor according to the size.
- Raises:
ValueError – If size is not expandable to the size of self Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.random.randn(3, 3, 3, 3, 3, 3), mindspore.float32) >>> output = x.sum_to_size((1, 3, 1, 3)) >>> print(output.shape) (1, 3, 1, 3)
-
svd
(full_matrices=False, compute_uv=True)[source]¶ For details, please refer to
mindspore.ops.svd()
.
-
take
(indices, axis=None, mode='clip')[source]¶ Takes elements from a tensor along an axis.
- Parameters:
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 tensor is used. Default: None.
mode ('raise', 'wrap', 'clip', optional) –
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’.
- Returns:
Tensor, the indexed result.
- Raises:
ValueError – If axis is out of range, or mode has values other than (‘raise’, ‘wrap’, ‘clip’)
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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]
-
tensor_split
(indices_or_sections, axis=0)[source]¶ For details, please refer to
mindspore.ops.tensor_split()
.
-
to
(dtype)[source]¶ Performs tensor dtype conversion.
- Parameters:
dtype (Number) – The valid data type of the output tensor. Only constant value is allowed.
- Returns:
Tensor, converted to the specified dtype.
- Raises:
TypeError – If dtype is not a Number.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_np = np.random.randn(2, 3, 4, 5).astype(np.float32) >>> input_x = Tensor(input_np) >>> dtype = mindspore.int32 >>> output = input_x.to(dtype) >>> print(output.dtype) Int32
-
to_coo
()[source]¶ Convert a Tensor to COOTensor.
Note
Only 2-D tensor is supported for now.
- Returns:
COOTensor, a sparse representation of the original dense tensor, containing the following parts.
indices (Tensor): 2-D integer tensor, indicates the positions of values of the dense tensor.
values (Tensor): 1-D tensor, indicates the non-zero values of the dense tensor.
shape (tuple(int)): the shape of the COOTensor, is the same as the original dense tensor.
- Raises:
ValueError – If input tensor is not 2-D.
- Supported Platforms:
GPU
Examples
>>> import numpy as np >>> import mindspore >>> from mindspore import Tensor >>> x = Tensor(np.array([[1, 0], [-5, 0]]), mindspore.float32) >>> output = x.to_coo() >>> print(output.indices, output.values, output.shape) [[0 0] [1 0]] [ 1. -5.] (2, 2)
-
to_csr
()[source]¶ Convert a Tensor to CSRTensor.
Note
Only 2-D tensor is supported for now.
- Returns:
CSRTensor, a sparse representation of the original dense tensor, containing the following parts.
indptr (Tensor): 1-D integer tensor, indicates the start and end point for values in each row.
indices (Tensor): 1-D integer tensor, indicates the column positions of all non-zero values of the input.
values (Tensor): 1-D tensor, indicates the non-zero values of the dense tensor.
shape (tuple(int)): the shape of the CSRTensor, is the same as the original dense tensor.
- Raises:
ValueError – If input tensor is not 2-D.
- Supported Platforms:
GPU
Examples
>>> import numpy as np >>> import mindspore >>> from mindspore import Tensor >>> x = Tensor(np.array([[1, 0], [-5, 0]]), mindspore.float32) >>> output = x.to_csr() >>> print(output.indptr, output.indices, output.values, output.shape) [0 1 2] [0 0] [ 1. -5.] (2, 2)
-
topk
(k, dim=None, largest=True, sorted=True)[source]¶ For details, please refer to
mindspore.ops.topk()
.
-
trace
(offset=0, axis1=0, axis2=1, dtype=None)[source]¶ Return the sum along diagonals of the tensor.
- Parameters:
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.
- Returns:
Tensor, the sum along diagonals.
- Raises:
ValueError – If the input tensor has less than two dimensions.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.diagonal()
: Return specified diagonals.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.eye(3, dtype=np.float32)) >>> print(x.trace()) 3.0
-
true_divide
(value)[source]¶ Alias for Tensor.div() with \(rounding\_mode=None\). For details, please refer to
mindspore.ops.div()
.
-
unfold
(kernel_size, dilation=1, padding=0, stride=1)[source]¶ For details, please refer to
mindspore.ops.unfold()
.
-
unique_consecutive
(return_idx=False, return_counts=False, axis=None)[source]¶ For details, please refer to
mindspore.ops.unique_consecutive()
.
-
unsorted_segment_max
(segment_ids, num_segments)[source]¶ For details, please refer to
mindspore.ops.unsorted_segment_max()
.
-
unsorted_segment_min
(segment_ids, num_segments)[source]¶ For details, please refer to
mindspore.ops.unsorted_segment_min()
.
-
unsorted_segment_prod
(segment_ids, num_segments)[source]¶ For details, please refer to
mindspore.ops.unsorted_segment_prod()
.
-
value
()[source]¶ Get the value of the tensor or the parameter.
- Returns:
The value of the tensor or the parameter.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor(np.array([1, 2], dtype=np.float32)) >>> x_value = x.value() >>> print(x_value) [1. 2.]
-
var
(axis=None, ddof=0, keepdims=False)[source]¶ 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.
Note
Numpy arguments dtype, out and where are not supported.
- Parameters:
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.
- Returns:
Variance tensor.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.mean()
: Reduce a dimension of a tensor by averaging all elements in the dimension.mindspore.Tensor.std()
: Compute the standard deviation along the specified axis.Examples
>>> 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)[source]¶ Reshape the tensor according to the input shape. It’s the same as
mindspore.Tensor.reshape()
, implemented by the underlying reshape operator.- Parameters:
shape (Union[tuple(int), int]) – Dimension of the output tensor.
- Returns:
Tensor, which dimension is the input shape’s value.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> a = Tensor(np.array([[1, 2, 3], [2, 3, 4]], dtype=np.float32)) >>> output = a.view((3, 2)) >>> print(output) [[1. 2.] [3. 2.] [3. 4.]]
-
view_as
(other)[source]¶ View self Tensor as the same shape as other .
- Parameters:
other (Tensor) – The returned Tensor has the same shape as other.
- Returns:
Tensor, has the same shape as other.
- Raises:
TypeError – If other is not a Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> a = Tensor([[1, 2, 3], [2, 3, 4]], mstype.float32) >>> b = Tensor([1, 1, 1, 1, 1, 1], mstype.float32) >>> output = a.view_as(b) >>> print(output) [1. 2. 3. 2. 3. 4.]
-
class
tinyms.
RowTensor
(indices=None, values=None, shape=None, row_tensor=None)[source]¶ 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, :, :, :, …].
- For example, if indices is [0], values is [[1, 2]], shape is
\((3, 2)\) , then the dense representation of the row tensor will be:
[[1, 2], [0, 0], [0, 0]]
Warning
This is an experimental API that is subjected to change or deletion.
- Parameters:
indices (Tensor) – A 1-D integer Tensor of shape \((D0)\) . Default: None.
values (Tensor) – A Tensor of any dtype of shape \((D0, D1, ..., Dn)\) . Default: None.
shape (tuple(int)) – An integer tuple which contains the shape of the corresponding dense tensor. Default: None.
row_tensor (RowTensor) – A RowTensor object. Default: None.
- Returns:
RowTensor, composed of indices, values, and shape.
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, RowTensor >>> indices = Tensor([0]) >>> values = Tensor([[1, 2]], dtype=ms.float32) >>> shape = (3, 2) >>> x = RowTensor(indices, values, shape) >>> print(x.values) [[1. 2.]] >>> print(x.indices) [0] >>> print(x.dense_shape) (3, 2)
-
property
dense_shape
¶ Return RowTensor’s shape.
-
property
indices
¶ Return RowTensor’s indices.
-
property
values
¶ Return RowTensor’s non-zero values.
-
class
tinyms.
SparseTensor
(indices, values, shape)[source]¶ A sparse representation of a set of nonzero elements from a tensor at given indices.
SparseTensor can only be used in the Cell’s construct method.
For a tensor dense, its SparseTensor(indices, values, dense_shape) has dense[indices[i]] = values[i].
For example, if indices is [[0, 1], [1, 2]], values is [1, 2], dense_shape is (3, 4), then the dense representation of the sparse tensor will be:
[[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0]]
Note
The interface is deprecated from version 1.7 and will be removed in a future version. Please use ‘COOTensor’ instead.
- Parameters:
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.
shape (tuple(int)) – An integer tuple of size ndims, which specifies the shape of the sparse tensor.
- Returns:
SparseTensor, composed of indices, values, and shape.
Examples
>>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore import Tensor, SparseTensor >>> indices = Tensor([[0, 1], [1, 2]]) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (3, 4) >>> x = SparseTensor(indices, values, shape) >>> print(x.values) [1. 2.] >>> print(x.indices) [[0 1] [1 2]] >>> print(x.shape) (3, 4)
-
property
indices
¶ Return SparseTensor’s indices.
-
property
shape
¶ Return SparseTensor’s shape.
-
property
values
¶ Return SparseTensor’s non-zero values.
-
class
tinyms.
COOTensor
(indices=None, values=None, shape=None, coo_tensor=None)[source]¶ A sparse representation of a set of nonzero elements from a tensor at given indices.
For a tensor dense, its COOTensor(indices, values, shape) has dense[indices[i]] = values[i].
For example, if indices is [[0, 1], [1, 2]], values is [1, 2], shape is (3, 4), then the dense representation of the sparse tensor will be:
[[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0]]
Common arithmetic operations include: addition (+), subtraction (-), multiplication (*), and division (/). For details about operations supported by COOTensor, see operators.
Warning
This is an experimental API that is subject to change or deletion.
Currently, duplicate coordinates in the indices will not be coalesced. If the indices contain out-of-bound values, the result will be undefined.
- Parameters:
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 COOTensor, respectively. Currently, ndims must be 2. Please make sure that the indices are in range of the given shape.
values (Tensor) – A 1-D tensor of any type and shape \((N)\), which supplies the values for each element in indices.
shape (tuple(int)) – An integer tuple of size ndims, which specifies the dense_shape of the sparse tensor.
coo_tensor (COOTensor) – A COOTensor object.
- Returns:
COOTensor, composed of indices, values, and shape.
Examples
>>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore import Tensor, COOTensor >>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (3, 4) >>> x = COOTensor(indices, values, shape) >>> print(x.values) [1. 2.] >>> print(x.indices) [[0 1] [1 2]] >>> print(x.shape) (3, 4)
-
abs
() → mindspore.common.sparse_tensor.COOTensor[source]¶ Return absolute value element-wisely.
- Returns:
COOTensor.
- Supported Platforms:
Ascend
GPU
CPU
-
add
(other: mindspore.common.sparse_tensor.COOTensor, thresh: mindspore.common.tensor.Tensor) → mindspore.common.sparse_tensor.COOTensor[source]¶ Return the sum with another COOTensor.
- Parameters:
- Returns:
COOTensor, representing the sum.
- Raises:
ValueError – If any input(self/other)’s indices’s dim is not equal to 2.
ValueError – If any input(self/other)’s values’s dim is not equal to 1.
ValueError – If any input(self/other)’s shape’s dim is not equal to 1.
ValueError – If thresh’s dim is not equal to 0.
TypeError – If any input(self/other)’s indices’s type is not equal to int64.
TypeError – If any input(self/other)’s shape’s type is not equal to int64.
ValueError – If any input(self/other)’s indices’s length is not equal to its values’s length.
TypeError – If any input(self/other)’s values’s type is not equal to anf of (int8/int16/int32/int64/float32/float64/complex64/complex128)
TypeError – If thresh’s type is not equal to anf of (int8/int16/int32/int64/float32/float64)
TypeError – If self’s indices’s type is not equal to other’s indices’s type
TypeError – If self’s values’s type is not equal to other’s values’s type
TypeError – If self’s shape’s type is not equal to other’s shape’s type
TypeError – If (self/other)’s value’s type is not matched with thresh’s type
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, COOTensor >>> from mindspore import dtype as mstype >>> indics0 = Tensor([[0, 1], [1, 2]], dtype=mstype.int64) >>> values0 = Tensor([1, 2], dtype=mstype.int32) >>> shape0 = (3, 4) >>> input0 = COOTensor(indics0, values0, shape0) >>> indics1 = Tensor([[0, 0], [1, 1]], dtype=mstype.int64) >>> values1 = Tensor([3, 4], dtype=mstype.int32) >>> shape1 = (3, 4) >>> input1 = COOTensor(indics1, values1, shape1) >>> thres = Tensor(0, dtype=mstype.int32) >>> out = input0.add(input1, thres) >>> print(out) COOTensor(shape=[3, 4], dtype=Int32, indices=Tensor(shape=[4, 2], dtype=Int64, value= [[0 0] [0 1] [1 1] [1 2]]), values=Tensor(shape=[4], dtype=Int32, value=[3 1 4 2]))
-
astype
(dtype: mstype) → COOTensor[source]¶ Return a copy of the COOTensor, cast its values to a specified type.
- Parameters:
dtype (Union[
mindspore.dtype
, numpy.dtype, str]) – Designated tensor dtype.- Returns:
COOTensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, COOTensor >>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (3, 4) >>> coo_tensor = COOTensor(indices, values, shape) >>> print(coo_tensor.astype(ms.float64).dtype) Float64
-
coalesce
() → mindspore.common.sparse_tensor.COOTensor[source]¶ Returns a coalesced copy of an uncoalesced sparse tensor.
- Returns:
A COOTensor.
- Supported Platforms:
GPU
Examples
>>> import mindspore as ms >>> import mindspore.ops as ops >>> from mindspore import Tensor, COOTensor >>> x_indices = Tensor([[0, 0, 1], [1, 1, 2]], dtype=ms.int64) >>> x_values = Tensor([1, 5, 4], dtype=ms.float32) >>> x_shape = (3, 3) >>> coo_tensor = COOTensor(x_indices.transpose(), x_values, x_shape) >>> res = coo_tensor.coalesce() >>> print(res) COOTensor(shape=[3, 3], dtype=Float32, indices=Tensor(shape=[2, 2], dtype=Int64, value=[[0 1] [1 2]]), values=Tensor(shape=[2], dtype=Float32, value=[6.00000000e+00 4.00000000e+00]))
-
property
dtype
¶ Return the dtype of the values of COOTensor (
mindspore.dtype
).
-
property
indices
¶ Return COOTensor’s indices.
-
property
itemsize
¶ Return the length of one tensor element in bytes.
-
property
ndim
¶ Return the number of tensor dimensions.
-
property
shape
¶ Return COOTensor’s shape.
-
property
size
¶ Return the number of non-zero values.
-
to_csr
() → mindspore.common.sparse_tensor.CSRTensor[source]¶ Converts COOTensor to CSRTensor.
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Returns:
CSRTensor.
- Supported Platforms:
GPU
CPU
-
to_dense
() → mindspore.common.tensor.Tensor[source]¶ Converts COOTensor to Dense Tensor.
- Returns:
Tensor.
- Supported Platforms:
GPU
-
to_tuple
() → Tuple[mindspore.common.tensor.Tensor, mindspore.common.tensor.Tensor, Tuple[int, ...]][source]¶ Return indices, values and shape as a tuple.
- Returns:
Tuple.
- Supported Platforms:
Ascend
GPU
CPU
-
property
values
¶ Return COOTensor’s non-zero values.
-
class
tinyms.
CSRTensor
(indptr=None, indices=None, values=None, shape=None, csr_tensor=None)[source]¶ Constructs a sparse tensor in CSR (Compressed Sparse Row) format, with specified values indicated by values and row and column positions indicated by indptr and indices.
For example, if indptr is [0, 1, 2, 2], indices is [1, 2], values is [1., 2.], shape is (3, 4), then the dense representation of the sparse tensor will be:
[[0., 1., 0., 0.], [0., 0., 2., 0.], [0., 0., 0., 0.]]
Common arithmetic operations include: addition (+), subtraction (-), multiplication (*), and division (/). For details about operations supported by CSRTensor, see operators.
Warning
This is an experimental API that is subjected to change.
If the values given by indptr or indices are invalid, the results may be undefined. Invalid values include when the length of values or indices exceeds the range indicated by indptr, and when the columns indicated by indices are repeated on the same row.
- Parameters:
indptr (Tensor) – 1-D Tensor of shape \((M)\), which equals to shape[0] + 1, which indicates the start and end point for values in each row. Default: None. If provided, must be int16, int32 or int64.
indices (Tensor) – 1-D Tensor of shape \((N)\), which has the same length as values. indices indicates the which column values should be placed. Default: None. If provided, must be int16, int32 or int64.
values (Tensor) – Tensor, which has the same length as indices (values.shape[0] == indices.shape[0]). values stores the data for CSRTensor. Default: None.
shape (tuple(int)) – A tuple indicates the shape of the CSRTensor, and shape[0] must equal to M - 1, which all equal to number of rows of the CSRTensor. Default: None.
csr_tensor (CSRTensor) – A CSRTensor object. Values’ feature dimension should match with CSRTensor’s feature dimension (values.shape[1:] == csr_tensor.shape[2:]). Default: None.
- Outputs:
CSRTensor, with shape defined by shape, and dtype inferred from value.
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, CSRTensor >>> # initialize a csr_tensor with indptr, indices, values and shape >>> indptr = Tensor([0, 1, 2], dtype=ms.int32) >>> indices = Tensor([0, 1], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, shape) >>> # access a data member of CSRTensor >>> print(indptr == csr_tensor.indptr) [ True True True]
-
abs
() → mindspore.common.sparse_tensor.CSRTensor[source]¶ Return absolute value element-wisely.
- Returns:
CSRTensor, with all values being non-negative.
- Supported Platforms:
Ascend
GPU
CPU
-
add
(b: mindspore.common.sparse_tensor.CSRTensor, alpha: mindspore.common.tensor.Tensor, beta: mindspore.common.tensor.Tensor) → mindspore.common.sparse_tensor.CSRTensor[source]¶ Addition of two CSR Tensors : C = alpha * A + beta * B
- Parameters:
- Returns:
CSRTensor.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> import mindspore.common.dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values_a = Tensor([2, 1], dtype=mstype.float32) >>> values_b = Tensor([1, 2], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> alpha = Tensor(1, mstype.float32) >>> beta = Tensor(1, mstype.float32) >>> a = CSRTensor(indptr, indices, values_a, dense_shape) >>> b = CSRTensor(indptr, indices, values_b, dense_shape) >>> print(a.add(b, alpha, beta)) CSRTensor(shape=[2,4], dtype=Float32, indptr=Tensor(shape=[3], dtype=Int32, value = [0, 1, 2]), indices=Tensor(shape=[2], dtype=Int32, value = [0, 1]), values=Tensor(shape=[2], dtype=Float32, value = [3.0, 3.0]))
-
astype
(dtype: mstype) → CSRTensor[source]¶ Return a copy of the CSRTensor, cast its values to a specified type.
- Parameters:
dtype (Union[
mindspore.dtype
, numpy.dtype, str]) – Designated tensor dtype.- Returns:
CSRTensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, CSRTensor >>> indptr = Tensor([0, 1, 2], dtype=ms.int32) >>> indices = Tensor([0, 1], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, shape) >>> print(csr_tensor.astype(ms.float64).dtype) Float64
-
property
dtype
¶ Return the dtype of the values of CSRTensor (
mindspore.dtype
).
-
property
indices
¶ Return CSRTensor’s column indices.
-
property
indptr
¶ Return CSRTensor’s row indices pointers.
-
property
itemsize
¶ Return the length of one tensor element in bytes.
-
mm
(matrix: Union[mindspore.common.tensor.Tensor, mindspore.common.sparse_tensor.CSRTensor]) → Union[mindspore.common.tensor.Tensor, mindspore.common.sparse_tensor.CSRTensor][source]¶ Return the matrix multiplication result of the right-multiply matrix(dense or CSRTensor) of the CSRTensor. The CSRTensor with shape [M, N] needs to adapt the right matrix with shape [N, K] to get the dense matrix or CSRTensor with result [M, K].
Note
If right matrix is CSRTensor, currently only supports GPU backend. if right matrix is Tensor, currently supports CPU backend with LLVM 12.0.1 or GPU backend.
- Parameters:
matrix (Tensor or CSRTensor) – A dense Tensor or CSRTensor, its shape[0] should be equal to csr_tensor.shape[1]
- Returns:
Tensor or CSRTensor.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> from mindspore import dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values = Tensor([2, 1], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape) >>> dense_matrix = Tensor([[1., 2.], [1, 2.], [1, 2.], [1., 2.]], dtype=mstype.float32) >>> print(csr_tensor.mm(dense_matrix)) [[2. 4.] [1. 2.]]
-
mv
(dense_vector: mindspore.common.tensor.Tensor) → mindspore.common.tensor.Tensor[source]¶ Return the matrix multiplication result of the right-multiply dense matrix of the CSRTensor. The CSRTensor with shape [M, N] needs to adapt the dense vector with shape [N, 1] to get the dense vector with result [M, 1].
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Parameters:
dense_vector (Tensor) – A dense Tensor, its shape must be (csr_tensor.shape[1], 1)
- Returns:
Tensor.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> from mindspore import dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values = Tensor([2, 1], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape) >>> dense = Tensor([[1], [1], [1], [1]], dtype=mstype.float32) >>> print(csr_tensor.mv(dense)) [[2.] [1.]]
-
property
ndim
¶ Return the number of tensor dimensions.
-
property
shape
¶ Return CSRTensor’s shape.
-
property
size
¶ Return the number of non-zero values.
-
sum
(axis: int) → mindspore.common.tensor.Tensor[source]¶ Reduces a dimension of a CSRTensor by summing all elements in the dimension.
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Parameters:
axis (int) – The dimensions to reduce.
- Returns:
Tensor, the dtype is the same as CSRTensor.values.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> from mindspore import dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values = Tensor([2, 1], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape) >>> print(csr_tensor.sum(1)) [[2.] [1.]]
-
to_coo
() → mindspore.common.sparse_tensor.COOTensor[source]¶ Converts CSRTensor to COOTensor.
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Returns:
COOTensor.
- Supported Platforms:
GPU
CPU
-
to_dense
() → mindspore.common.tensor.Tensor[source]¶ Converts CSRTensor to Dense Tensor.
- Returns:
Tensor.
- Supported Platforms:
GPU
-
to_tuple
() → Tuple[mindspore.common.tensor.Tensor, mindspore.common.tensor.Tensor, mindspore.common.tensor.Tensor, Tuple[int, ...]][source]¶ Return indptr, indices, values and shape as a tuple.
- Returns:
Tuple.
- Supported Platforms:
Ascend
GPU
CPU
-
property
values
¶ Return CSRTensor’s non-zero values.
-
tinyms.
ms_function
(fn=None, input_signature=None, hash_args=None, jit_config=None)[source]¶ Create a callable MindSpore graph from a Python function.
This allows the MindSpore runtime to apply optimizations based on graph.
Note
ms_function will be deprecated and removed in a future version. Please use jit instead. If input_signature is specified, each input of fn must be a Tensor. And the input arguments for fn will not accept **kwargs.
- Parameters:
fn (Function) – The Python function that will be run as a graph. 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.
hash_args (Union[Object, List or Tuple of Objects]) – The local free variables used inside fn, like functions or objects of class defined outside fn. Calling fn again with change of hash_args will trigger recompilation.
jit_config (JitConfig) – Jit config for compile. Default: None.
- Returns:
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
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import ops >>> 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) ... ... # Set hash_args as fn, otherwise cache of compiled `closure_fn` will not be reused. ... # While fn differs during calling again, recompilation will be triggered. >>> def func(x): ... return ops.exp(x) ... >>> def closure_fn(x, fn): ... @ms_function(hash_args=fn) ... def inner_fn(a): ... return fn(a) ... return inner_fn(x) ... >>> inputs = Tensor(np.ones([10, 10, 10]).astype(np.float32)) >>> for i in range(10): ... closure_fn(inputs, func)
-
tinyms.
ms_class
(cls)[source]¶ Class decorator for user-defined classes.
This allows MindSpore to identify user-defined classes and thus obtain their attributes and methods.
Note
ms_class will be deprecated and removed in a future version. Please use jit_class instead.
- Parameters:
cls (Class) – User-defined class.
- Returns:
Class.
- Raises:
TypeError – If ms_class is used for non-class types or nn.Cell.
AttributeError – If the private attributes or magic methods of the class decorated with ms_class is called.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.nn as nn >>> from mindspore import ms_class ... >>> @ms_class ... class UserDefinedNet: ... def __init__(self): ... self.value = 10 ... ... def func(self, x): ... return 2 * x ... >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.net = UserDefinedNet() ... ... def construct(self, x): ... out = self.net.value + self.net.func(x) ... return out ... >>> net = Net() >>> out = net(5) >>> print(out) 20
-
tinyms.
jit
(fn=None, input_signature=None, hash_args=None, jit_config=None)[source]¶ Create a callable MindSpore graph from a Python function.
This allows the MindSpore runtime to apply optimizations based on graph.
Note
If input_signature is specified, each input of fn must be a Tensor. And the input arguments for fn will not accept **kwargs.
- Parameters:
fn (Function) – The Python function that will be run as a graph. 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.
hash_args (Union[Object, List or Tuple of Objects]) – The local free variables used inside fn, like functions or objects of class defined outside fn. Calling fn again with change of hash_args will trigger recompilation.
jit_config (JitConfig) – Jit config for compile. Default: None.
- Returns:
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
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import ops >>> from mindspore import jit ... >>> 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 decorator @jit >>> def tensor_add(x, y): ... z = x + y ... return z ... >>> tensor_add_graph = jit(fn=tensor_add) >>> out = tensor_add_graph(x, y) ... >>> # create a callable MindSpore graph through decorator @jit >>> @jit ... 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 @jit with input_signature parameter >>> @jit(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) ... ... # Set hash_args as fn, otherwise cache of compiled `closure_fn` will not be reused. ... # While fn differs during calling again, recompilation will be triggered. >>> def func(x): ... return ops.exp(x) ... >>> def closure_fn(x, fn): ... @jit(hash_args=fn) ... def inner_fn(a): ... return fn(a) ... return inner_fn(x) ... >>> inputs = Tensor(np.ones([10, 10, 10]).astype(np.float32)) >>> for i in range(10): ... closure_fn(inputs, func)
-
tinyms.
jit_class
(cls)[source]¶ Class decorator for user-defined classes.
This allows MindSpore to identify user-defined classes and thus obtain their attributes and methods.
- Parameters:
cls (Class) – User-defined class.
- Returns:
Class.
- Raises:
TypeError – If jit_class is used for non-class types or nn.Cell.
AttributeError – If the private attributes or magic methods of the class decorated with jit_class is called.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.nn as nn >>> from mindspore import jit_class ... >>> @jit_class ... class UserDefinedNet: ... def __init__(self): ... self.value = 10 ... ... def func(self, x): ... return 2 * x ... >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.net = UserDefinedNet() ... ... def construct(self, x): ... out = self.net.value + self.net.func(x) ... return out ... >>> net = Net() >>> out = net(5) >>> print(out) 20
-
class
tinyms.
Parameter
(default_input, name=None, requires_grad=True, layerwise_parallel=False, parallel_optimizer=True)[source]¶ Parameter is a Tensor subclass, when they are assigned as Cell attributes they are automatically added to the list of its parameters, and will appear, e.g. in cell.get_parameters() iterator.
Note
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. Give each Parameter a unique name to facilitate subsequent operations and updates. If there are two or more Parameter objects with the same name in a network, will be prompted to set a unique name when defining.
- Parameters:
default_input (Union[Tensor, int, float, numpy.ndarray, list]) – Parameter data, to initialize the parameter data.
name (str) –
Name of the parameter. Default: None.
1) If the parameter is not given a name, the default name is its variable name. For example, the name of param_a below is name_a, and the name of param_b is the variable name param_b.
self.param_a = Parameter(Tensor([1], ms.float32), name="name_a") self.param_b = Parameter(Tensor([2], ms.float32))
2) If parameter in list or tuple is not given a name, will give it a unique name. For example, the names of parameters below are Parameter$1 and Parameter$2.
self.param_list = [Parameter(Tensor([3], ms.float32)), Parameter(Tensor([4], ms.float32))]
3) If the parameter is given a name, and the same name exists between different parameters, an exception will be thrown. For example, “its name ‘name_a’ already exists.” will be thrown.
self.param_a = Parameter(Tensor([1], ms.float32), name="name_a") self.param_tuple = (Parameter(Tensor([5], ms.float32), name="name_a"), Parameter(Tensor([6], ms.float32)))
4) If a parameter appear multiple times in list or tuple, check the name of the object only once. For example, the following example will not throw an exception.
self.param_a = Parameter(Tensor([1], ms.float32), name="name_a") self.param_tuple = (self.param_a, self.param_a)
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.set_auto_parallel_context(). Default: True.
Examples
>>> 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.
- Returns:
numpy.ndarray.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> array = data.asnumpy() >>> array array([[1., 1., 1.], [1., 1., 1.]])
-
asnumpy_of_slice_persistent_data
(self: mindspore._c_expression.Tensor, arg0: int, arg1: int) → array¶ Convert tensor to numpy.ndarray of a slice.
- Returns:
numpy.ndarray.
Examples
>>> data = mindspore.Tensor(np.ones((2000000000, 256))) >>> data.asnumpy_of_slice_persistent_data(0, 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.
Examples
>>> 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')[source]¶ Clone the parameter.
- Parameters:
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’.
- Returns:
Parameter, a new parameter.
-
property
comm_fusion
¶ Get 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. 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.
-
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.
- Returns:
int, the dimension of tensor.
Examples
>>> 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.
- Returns:
Tensor, tensor with shared data to input ndarray.
Examples
>>> a = np.ones((2, 3)) >>> t = mindspore.Tensor.from_numpy(a)
-
getitem_index_info
(self: object, arg0: object, arg1: bool_) → object¶
-
init_data
(layout=None, set_sliced=False)[source]¶ Initialize the parameter’s data.
- Parameters:
The parameter’s layout info. layout [dev_mat, tensor_map, slice_shape, filed_size, uniform_split, opt_shard_group]. Default: None. It’s not None only in ‘SEMI_AUTO_PARALLEL’ or ‘AUTO_PARALLEL’ mode.
dev_mat (list(int)): The parameter’s device matrix.
tensor_map (list(int)): The parameter’s tensor map.
slice_shape (list(int)): The parameter’s slice shape.
filed_size (int): The parameter’s filed size.
uniform_split (bool): Whether the parameter is split evenly.
opt_shard_group (str): The group of the parameter while running optimizer parallel.
set_sliced (bool) – True if the parameter is set sliced after initializing the data. Default: False.
- Returns:
Parameter, the Parameter after initializing data. If current Parameter was already initialized before, returns the same initialized Parameter.
- Raises:
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 6.
TypeError – If layout is not tuple.
-
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.
-
is_init
(self: mindspore._c_expression.Tensor) → bool¶ Get tensor init_flag.
- Returns:
bool, whether the tensor init.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.is_init() False
-
is_persistent_data
(self: mindspore._c_expression.Tensor) → bool¶ Check if tensor have persistent data.
- Returns:
Bool.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.is_persistent_data()
-
property
key
¶ Return the parameter unique key.
-
property
layerwise_parallel
¶ Get the layerwise parallel status(bool) of the parameter.
When layerwise_parallel is true in DATA_PARALLEL and HYBRID_PARALLEL parallel mode, broadcast and gradients communication would not be applied to parameters.
-
property
name
¶ Get the name of the parameter.
-
property
parallel_optimizer
¶ Get the optimizer parallel status(bool) of the parameter.
It is used to filter the weight shard operation in AUTO_PARALLEL and SEMI_AUTO_PARALLEL mode. It works only when enable parallel optimizer in mindspore.set_auto_parallel_context().
-
property
parallel_optimizer_comm_recompute
¶ Get the communication recompute status(bool) of optimizer parallel for the parameter.
In AUTO_PARALLEL and SEMI_AUTO_PARALLEL mode, when applying parallel optimizer, some
mindspore.ops.AllGather
operators used for parameters gathering are inserted automatically. It is used to control the recompute attr for thosemindspore.ops.AllGather
operators.Note
Only Graph mode is supported.
It is recommended to use cell.recompute(parallel_optimizer_comm_recompute=True/False) to configure the AllGather operators introducing by parallel optimizer rather than using this interface directly.
-
persistent_data_from_numpy
(self: array, arg0: int_) → mindspore._c_expression.Tensor¶ Creates a Tensor from a numpy.ndarray without copy. Use persistent data tensor.
- Arg:
array (numpy.ndarray): The input ndarray. slice_num (int): The slice num of persistent data tensor.
- Returns:
Tensor, tensor with shared data to input ndarray.
Examples
>>> a = np.ones((2, 3)) >>> t = mindspore.Tensor.persistent_data_from_numpy(a, 1)
-
property
requires_grad
¶ Return whether the parameter requires gradient.
-
set_cast_dtype
(self: mindspore._c_expression.Tensor, dtype: mindspore._c_expression.typing.Type = None) → None¶
-
set_dtype
(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.typing.Type) → mindspore._c_expression.typing.Type¶ Set the tensor’s data type.
- Arg:
dtype (
mindspore.dtype
): The type of output tensor.
Examples
>>> 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.
Examples
>>> 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)[source]¶ Set the way of parameter and server interaction.
-
set_param_ps
(init_in_server=False)[source]¶ Set whether the trainable parameter is updated by parameter server and whether the trainable parameter is initialized on server.
Note
It only works when a running task is in the parameter server mode. It is supported only in graph mode.
- Parameters:
init_in_server (bool) – Whether trainable parameter updated by parameter server is initialized on server. Default: False.
-
setitem_index_info
(self: object, arg0: object, arg1: object, arg2: bool_) → object¶
-
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.
-
value
()[source]¶ Return the value of parameter object.
Examples
>>> from mindspore import Tensor, Parameter >>> import numpy as np >>> x = Parameter(Tensor(np.array([1, 2], dtype=np.float32)), name="param") >>> x_value = x.value() >>> print(x_value) [1. 2.]
-
class
tinyms.
ParameterTuple
[source]¶ Inherited from tuple, ParameterTuple is used to save multiple parameter.
Note
It is used to store the parameters of the network into the parameter tuple collection.
-
clone
(prefix, init='same')[source]¶ Clone the parameters in ParameterTuple element-wisely to generate a new ParameterTuple.
- Parameters:
prefix (str) – Namespace of parameter, the prefix string will be added to the names of parameters in parametertuple.
init (Union[Tensor, str, numbers.Number]) –
Clone the shape and dtype of Parameters in ParameterTuple and set data according to init. Default: ‘same’.
If init is a Tensor , set the new Parameter data to the input Tensor.
If init is numbers.Number , set the new Parameter data to the input number.
If init is a str, data will be set according to the initialization method of the same name in the Initializer.
If init is ‘same’, the new Parameter has the same value with the original Parameter.
- Returns:
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)[source]¶ Set global seed.
Note
The global seed is used by numpy.random, mindspore.common.Initializer, mindspore.ops.function.random_func 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.function.random_func 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.
In semi_auto_parallel/auto_parallel mode, when using set_seed, weights with same shape and same sharding strategy in the same device would be initialized to the same result, otherwise, they would be initialized to the different result.
- Parameters:
seed (int) – The seed to be set.
- Raises:
ValueError – If seed is invalid (< 0).
TypeError – If seed isn’t an int.
Examples
>>> import numpy as np >>> import mindspore.ops as ops >>> from mindspore import Tensor, set_seed, Parameter >>> from mindspore.common.initializer import initializer >>> import mindspore as ms >>> # 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.function.random_func 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.function.random_func 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.function.random_func 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.function.random_func 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 different 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.function.random_func 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.
set_dump
(target, enabled=True)[source]¶ Enable or disable dump for the target and its contents.
target should be an instance of
mindspore.nn.Cell
ormindspore.ops.Primitive
. Please note that this API takes effect only when Asynchronous Dump is enabled and the dump_mode field in dump config file is “2”. See the dump document for details. The default enabled status for amindspore.nn.Cell
ormindspore.ops.Primitive
is False.Warning
This is an experimental API that is subject to change or deletion.
Note
This API is only effective for GRAPH_MODE with Ascend backend.
This API only supports being called before training starts. If you call this API during training, it may not be effective.
After using set_dump(Cell, True) , operators in forward and backward computation (computation generated by the grad operations) of the cell will be dumped.
For
mindspore.nn.SoftmaxCrossEntropyWithLogits
layer, the forward computation and backward computation use the same set of operators. So you can only see dump data from backward computation. Please note thatmindspore.nn.SoftmaxCrossEntropyWithLogits
layer will also use the above operators internally when initialized with sparse=True and reduction=”mean” .
- Parameters:
target (Union[Cell, Primitive]) – The Cell instance or Primitive instance to which the dump flag is set.
enabled (bool, optional) – True means enable dump, False means disable dump. Default: True.
- Supported Platforms:
Ascend
Examples
>>> # Please set the dump config file and environment variable before >>> # running this example to actually get the dump data. >>> # See the document of this API for details. >>> import numpy as np >>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore import Tensor, set_dump >>> >>> ms.set_context(device_target="Ascend", mode=ms.GRAPH_MODE) >>> >>> class MyNet(nn.Cell): ... def __init__(self): ... super().__init__() ... self.conv1 = nn.Conv2d(5, 6, 5, pad_mode='valid') ... self.relu1 = nn.ReLU() ... ... def construct(self, x): ... x = self.conv1(x) ... x = self.relu1(x) ... return x >>> >>> if __name__ == "__main__": ... net = MyNet() ... set_dump(net.conv1) ... input_tensor = Tensor(np.ones([1, 5, 10, 10], dtype=np.float32)) ... output = net(input_tensor)
-
tinyms.
ms_memory_recycle
()[source]¶ Recycle memory used by MindSpore. When train multi Neural network models in one process, memory used by MindSpore is very large, this is because MindSpore cached runtime memory for every model. To recycle these cached memory, users can call this function after training of one model.
-
tinyms.
mutable
(input_data, dynamic_len=False)[source]¶ Make a constant value mutable.
Currently, all the inputs of Cell except Tensor such as scalar, tuple, list and dict, are regarded as constant values. The constant values are non-differentiable and used to do constant folding in the optimization process.
Besides, currently when the network input is tuple[Tensor], list[Tensor] or dict[Tensor], even without changing the shape and dtype of the Tensors, the network will be re-compiled when calling this network repeatedly because the these inputs are regarded as constant values.
To solve the above problems, we provide api mutable to make the constant inputs of Cell ‘mutable’. A ‘mutable’ input means that it is changed to be a variable input just like Tensor and the most important thing is that it will be differentiable.
When the input_data is tuple or list and dynamic_len is False, mutable will return a constant length tuple or list with all mutable elements. If dynamic_len is True, the length of the return tuple or list will be dynamic.
If a dynamic length tuple or list is used as the input of the network and the network is repeatedly called, and the length of the tuple or list is different for each run, it does not need to be re-compiled.
- Parameters:
input_data (Union[int, float, Tensor, tuple, list, dict]) – The input data to be made mutable. If ‘input_data’ is list/tuple/dict, the type of each element should also in the valid types.
dynamic_len (bool) – Whether to set the whole sequence to be dynamic length. In graph compilation, if dynamic_len is True, the input_data must be list or tuple and the elements of input_data must have the same type and shape. Default: False.
Warning
This is an experimental API that is subject to change or deletion.
Note
Currently this api only works in GRAPH mode.
- Returns:
The origin input data which has been set mutable.
- Raises:
TypeError – If input_data is not one of int, float, Tensor, tuple, list, dict or their nested structure.
TypeError – If dynamic_len is True and input_data is not tuple or list.
ValueError – If dynamic_len is True, input_data is tuple or list but the elements within input_data do not have the same shape and type.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> from mindspore.common import mutable >>> from mindspore.common import dtype as mstype >>> from mindspore import Tensor >>> from mindspore import context >>> context.set_context(mode=context.GRAPH_MODE) >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.matmul = ops.MatMul() ... ... def construct(self, z): ... x = z[0] ... y = z[1] ... 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 = ops.GradOperation() ... ... def construct(self, z): ... gradient_function = self.grad_op(self.net) ... return gradient_function(z) ... >>> z = mutable((Tensor([[0.5, 0.6, 0.4], [1.2, 1.3, 1.1]], dtype=mstype.float32), ... 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())(z) >>> print(output) (Tensor(shape=[2, 3], dtype=Float32, value= [[ 1.41000009e+00, 1.60000002e+00, 6.59999943e+00], [ 1.41000009e+00, 1.60000002e+00, 6.59999943e+00]]), Tensor(shape=[3, 3], dtype=Float32, value= [[ 1.70000005e+00, 1.70000005e+00, 1.70000005e+00], [ 1.89999998e+00, 1.89999998e+00, 1.89999998e+00], [ 1.50000000e+00, 1.50000000e+00, 1.50000000e+00]]))
-
class
tinyms.
JitConfig
(jit_level='O1', exc_mode='auto', **kwargs)[source]¶ Jit config for compile.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
jit_level (str) –
Option for argument level for Optimization of lift graph. Supports [“O0”, “O1”, “O2”, “O3”]. Default: “O1”.
”O0”: Basic optimization.
”O1”: Manual optimization.
”O2”: Manual optimization and graph computation fusion.
”O3”: Performance optimization, no generalization guaranteed.
exc_mode (str) –
Mode for execute the network. Supports [“auto”, “sink”, “no_sink”]. Default: “auto”.
”auto”: Automatic Policies.
”sink”: Build computational graphs with the sink mode.
”no_sink”: Build computational graphs with no sink mode.
**kwargs (dict) – A dictionary of keyword arguments that the class needs.
Examples
>>> from mindspore import JitConfig >>> >>> jitconfig = JitConfig(jit_level="O1") >>> net = LeNet5() >>> >>> net.set_jit_config(jitconfig)
-
tinyms.
abs
(x, dtype=None)¶ Calculates the absolute value element-wise.
Note
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.- Parameters:
x (Tensor) – Tensor to be used for calculation.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor.
- Raises:
TypeError – If input arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Calculates the absolute value element-wise.
Note
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.- Parameters:
x (Tensor) – Tensor to be used for calculation.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor.
- Raises:
TypeError – If input arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Adds arguments element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
- Returns:
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
Examples
>>> 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)[source]¶ Returns the maximum of an array or maximum along an axis.
Note
Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.
- Parameters:
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.
- Returns:
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
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Returns the minimum of an array or minimum along an axis.
Note
Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.
- Parameters:
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.
- Returns:
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
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Appends values to the end of a tensor.
- Parameters:
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
.
- Returns:
Tensor, a copy of tensor with values appended to axis.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If specified axis exceeds arr.ndim.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ 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.- Parameters:
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.
- Returns:
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
- Raises:
ValueError – If axis is out of the range.
Examples
>>> 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)[source]¶ 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.
- Parameters:
- Returns:
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.
- Raises:
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
Examples
>>> 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)[source]¶ Returns evenly spaced values within a given interval.
- Parameters:
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.
- Returns:
Tensor with evenly spaced values.
- Raises:
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
Examples
>>> 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]
-
tinyms.
arccos
(input, dtype=None)[source]¶ Trigonometric inverse cosine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
input (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.
- Returns:
Tensor.
- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> input = np.asarray([1, -1], np.float32) >>> output = np.arccos(input) >>> print(output) [0. 3.1415927]
-
tinyms.
arccosh
(x, dtype=None)[source]¶ Inverse hyperbolic cosine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Inverse sine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor. y-coordinate on the unit circle.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Output Tensor.
- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Inverse hyperbolic sine element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.array([1., 2., 3., 4.], dtype=np.float32) >>> print(np.arcsinh(x)) [0.8813736 1.4436355 1.8184465 2.0947125]
-
tinyms.
arctan
(x, dtype=None)[source]¶ Trigonometric inverse tangent, element-wise.
The inverse of tan, so that if \(y = tan(x)\) then \(x = arctan(y)\).
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Element-wise arc tangent of \(x1/x2\) choosing the quadrant correctly.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
- Returns:
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
Examples
>>> 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)[source]¶ Inverse hyperbolic tangent element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
CPU
Examples
>>> 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)[source]¶ Returns the indices of the maximum values along an axis.
Note
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.
- Parameters:
- Returns:
Tensor, array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.
- Raises:
ValueError – If axis is out of range.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Returns the indices of the minimum values along an axis.
Note
Numpy argument out is not supported.
- Parameters:
- Returns:
Tensor, array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.
- Raises:
ValueError – If axis is out of range.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Creates a tensor.
This function creates tensors from an array-like object.
- Parameters:
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 isNone
, the data type of the new tensor will be inferred from obj. Default isNone
.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
- Returns:
Tensor, generated tensor with the specified dtype.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If input obj has different sizes at different dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.array([1,2,3])) [1 2 3]
-
tinyms.
array_equal
(a1, a2, equal_nan=False)[source]¶ Returns True if input arrays have same shapes and all elements equal.
Note
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.- Parameters:
- Returns:
Scalar bool tensor, value is True if inputs are equal, False otherwise.
- Raises:
TypeError – If inputs have types not specified above.
- Supported Platforms:
GPU
CPU
Ascend
Examples
>>> 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)[source]¶ 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.
Note
In mindspore, a bool tensor is returned instead, since in Graph mode, the value cannot be traced and computed at compile time.
- Parameters:
a1/a2 (Union[int, float, bool, list, tuple, Tensor]) – Input arrays.
- Returns:
Scalar bool tensor, value is True if inputs are equivalent, False otherwise.
- Raises:
TypeError – If inputs have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Splits a tensor into multiple sub-tensors.
Note
Currently, array_split only supports
mindspore.float32
onCPU
.The only difference between
np.split
andnp.array_split
is thatnp.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\).- Parameters:
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]`and :math:`x[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.
- Returns:
A list of sub-tensors.
- Raises:
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
Examples
>>> 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)[source]¶ 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.
Note
Numpy argument max_line_width, precision and suppress_small are not supported. Graph mode does not support the function.
- Parameters:
a (Tensor) – Input data.
- Returns:
String.
- Supported Platforms:
Ascend
GPU
CPU
- Raises:
TypeError – If input is not tensor.
Examples
>>> import mindspore.numpy as np >>> x = np.arange(5) >>> np.array_str(x) '[0 1 2 3 4]'
-
tinyms.
asarray
(a, dtype=None)[source]¶ Converts the input to tensor.
This function converts tensors from an array-like object.
- Parameters:
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 isNone
, the data type of the new tensor will be inferred from obj. Default isNone
.
- Returns:
Tensor, generated tensor with the specified dtype.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If input a has different sizes at different dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.asarray([1,2,3])) [1 2 3]
-
tinyms.
asfarray
(a, dtype=mindspore.float32)[source]¶ Similar to asarray, converts the input to a float tensor.
If non-float dtype is defined, this function will return a float32 tensor instead.
- Parameters:
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 isNone
, the data type of the new tensor will be inferred from a. Default ismindspore.float32
.
- Returns:
Tensor, generated tensor with the specified float dtype.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If input a has different sizes at different dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.asfarray([1,2,3])) [1. 2. 3.]
-
tinyms.
atleast_1d
(*arys)[source]¶ Converts inputs to arrays with at least one dimension.
Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved.
Note
In graph mode, returns a tuple of tensor instead of a list of tensors.
- Parameters:
*arys (Tensor) – one or more input tensors.
- Returns:
Tensor, or list of tensors, each with
a.ndim >= 1
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Reshapes inputs as arrays with at least two dimensions.
Note
In graph mode, returns a tuple of tensor instead of a list of tensors.
- Parameters:
*arys (Tensor) – one or more input tensors.
- Returns:
Tensor, or list of tensors, each with
a.ndim >= 2
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Reshapes inputs as arrays with at least three dimensions.
Note
In graph mode, returns a tuple of tensor instead of a list of tensors.
- Parameters:
*arys (Tensor) – one or more input tensors.
- Returns:
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).- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Computes the weighted average along the specified axis.
- Parameters:
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.
- Returns:
Averaged Tensor. If returned is True, return tuple.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ 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.
- Parameters:
M (int) – Number of points in the output window. If zero or less, an empty array is returned.
- Returns:
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.
- Raises:
TypeError – If M is not an int.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ 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 ofout[n] += 1
.Note
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.- Parameters:
- Returns:
Tensor, the result of binning the input array. The length of out is equal to
np.amax(x)+1
.- Raises:
ValueError – If x is not one-dimensional, or if x and weights do not have the same shape.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ 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 &.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
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.
- Returns:
Tensor or scalar, this is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
CPU
Examples
>>> 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
.Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Supported dtypes on Ascend: np.int16, np.uint16.
- Parameters:
x (Tensor) – Only integer and boolean types are handled.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar.
- Supported Platforms:
Ascend
Examples
>>> import mindspore.numpy as np >>> print(np.invert(np.array(13, dtype=np.uint16))) 65522
-
tinyms.
bitwise_or
(x1, x2, dtype=None)[source]¶ 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 |.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
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.
- Returns:
Tensor or scalar, this is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.bitwise_or(13, 16)) 29
-
tinyms.
bitwise_xor
(x1, x2, dtype=None)[source]¶ 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 ^.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
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.
- Returns:
Tensor or scalar, this is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.bitwise_xor(13, 17)) 28
-
tinyms.
blackman
(M)[source]¶ 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.
- Parameters:
M (int) – Number of points in the output window. If zero or less, an empty array is returned.
- Returns:
Tensor, the window, with the maximum value normalized to one (the value one appears only if the number of samples is odd).
- Raises:
TypeError – If M is not an int.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Broadcasts any number of arrays against each other.
Note
Numpy argument subok is not supported. In graph mode, returns a tuple of Tensor instead of a list of Tensor.
- Parameters:
*args (Tensor) – The arrays to broadcast.
- Returns:
List of Tensor.
- Raises:
ValueError – If arrays cannot be broadcast.
- Supported Platforms:
Ascend
GPU
CPU
Example
>>> 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)[source]¶ Broadcasts an array to a new shape.
- Parameters:
- Returns:
Tensor, original array broadcast to the given shape.
- Raises:
ValueError – If array cannot be broadcast to shape.
- Supported Platforms:
Ascend
GPU
CPU
Example
>>> 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)[source]¶ Returns the cube-root of a tensor, element-wise.
Note
Numpy arguments casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Returns the ceiling of the input, element-wise.
The ceil of the scalar x is the smallest integer i, such that
i >= x
.Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32.
- Parameters:
x (Tensor) – input values.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar, the floor of each element in x. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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')[source]¶ 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 withshape 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 inBchoices[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.
Note
Numpy argument out is not supported.
mode = 'raise'
is not supported, and the default mode is ‘clip’ instead.- Parameters:
a (int array) – This array must contain integers in
[0, n-1]
, where n is the number of choices, unlessmode=wrap
ormode=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.
- Returns:
Tensor, the merged result.
- Raises:
ValueError – If a and any of the choices cannot be broadcast.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ 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.
- Parameters:
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.
- Returns:
Tensor, a tensor with the elements of x, but where values < xmin are replaced with xmin, and those > xmax with xmax.
- Raises:
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
Examples
>>> 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)[source]¶ Stacks 1-D tensors as columns into a 2-D tensor. 2-D tensors are stacked as-is, like np.hstack.
- Parameters:
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.
- Returns:
2-D Tensor, formed by stacking the given tensors.
- Supported Platforms:
Ascend
GPU
CPU
- Raises:
TypeError – If tup is not Tensor, list or tuple.
ValueError – If tup is empty.
Examples
>>> 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)[source]¶ Joins a sequence of tensors along an existing axis.
Note
To match Numpy behaviour, \(axis >= 32\) will not cause value error, the axis will be treated as
None
instead.- Parameters:
- Returns:
A tensor concatenated from a tensor or a list of tensors.
- Raises:
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
Examples
>>> 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')[source]¶ Returns the discrete, linear convolution of two one-dimensional sequences.
Note
If v is longer than a, the tensors are swapped before computation.
- Parameters:
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.
- Returns:
Tensor, discrete, linear convolution of a and v.
- Raises:
TypeError – If the inputs have types not specified above.
ValueError – If a and v are empty or have wrong dimensions
- Supported Platforms:
GPU
CPU
Examples
>>> 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)[source]¶ 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.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Complex inputs are not supported now.
- Parameters:
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.
- Returns:
Tensor or scalar. The values of x1 with the sign of x2. This is a scalar if both x1 and x2 are scalars.
- Raises:
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
Examples
>>> 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)[source]¶ 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.
Note
Currently, complex numbers are not supported.
- Parameters:
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. Default: 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. Default: True.
dtype (
mindspore.dtype
, optional) – Data-type of the result. By default, the return data-type will have at least float32 precision. Default: None.
- Returns:
Tensor, The correlation coefficient matrix of the variables.
- Raises:
TypeError – If the inputs have types not specified above.
ValueError – If x and y have wrong dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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')[source]¶ 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.
Note
Currently, complex numbers are not supported.
- Parameters:
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.
- Returns:
Tensor. Discrete cross-correlation of a and v.
- Raises:
TypeError – If the inputs can not be converted to tensor.
ValueError – If a and v are empty or have wrong dimensions
- Supported Platforms:
GPU
Examples
>>> 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)[source]¶ Cosine element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Hyperbolic cosine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
CPU
Examples
>>> 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)[source]¶ Counts the number of non-zero values in the tensor x.
- Parameters:
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. Default: None.
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. Default: False.
- Returns:
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.
- Raises:
TypeError – If axis is not int or tuple.
ValueError – If axis is not in range [-x.ndim, x.ndim).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ 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\).
Note
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.
- Parameters:
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 isTrue
, 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 isNone
.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. Default isNone
.
- Returns:
Tensor, the covariance matrix of the variables.
- Raises:
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
Examples
>>> 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)[source]¶ 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.
- Parameters:
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. Defaults to None.
- Returns:
Tensor, vector cross product(s).
- Raises:
ValueError – when the dimensions of the vector(s) in a and/or b does not equal 2 or 3.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> 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)[source]¶ Returns the cumulative product of elements along a given axis.
Note
Numpy argument out is not supported.
- Parameters:
- Returns:
Tensor.
- Raises:
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
Examples
>>> import mindspore.numpy as np >>> x = np.array([1, 2, 3]) >>> print(np.cumprod(x)) [1 2 6]
-