Basic knowledge of MindSpore [deep learning, basics]

What is MindSpore? How is it different from other deep learning frameworks?

MindSpore is an open source deep learning framework launched by Huawei. It provides an end-to-end set of development tools and algorithm libraries designed to simplify the development, training, and deployment of deep learning models.

Compared with other deep learning frameworks, MindSpore has the following characteristics:

  1. Automatic parallel computing: MindSpore has the capability of automatic parallel computing, and can automatically optimize parallel computing according to hardware resources and network topology. This means you can focus on the sum of your models设计开发 without manually handling parallel computations.

  2. Dynamic calculation graph: MindSpore adopts the method of dynamic calculation graph, which allows you to dynamically build a calculation graph at runtime. Compared with static computation graphs, dynamic computation graphs are more flexible and can easily handle variable-length input data.

What is the core concept of MindSpore?

  1. 张量(Tensor)
    Tensor is a basic data type in MindSpore , which can be considered as a multidimensional array. You can think of a tensor as a container for storing and representing data, which can be a scalar ( 0维tensor), vector ( 1维tensor), matrix ( 2维tensor), or a higher-dimensional array.

Tensors can store numbers, images, audio, or other types of data. Here is a simple tensor example:

import mindspore as ms
import numpy as np

# 创建一个2x3的张量
data = np.array([[1, 2, 3], [4, 5, 6]])
tensor = ms.Tensor(data)
print(tensor)

Output result:

[[1 2 3]
 [4 5 6]]

In the above example, we created a 2x3 using a NumPy array 张量and printed it. You can think of a tensor as a table that contains the data we need.

  1. 计算图(Computational Graph):
    Calculation graph is a concept used by MindSpore to describe the calculation process . It consists of a series of computing nodes and data streams, representing the computing process of the model. In MindSpore, you can build a computational graph to define the structure and operation of a neural network. Here is an example of a simple computation graph:
    import mindspore.nn as nn
    from mindspore import Tensor
    
    class Net(nn.Cell):
        def __init__(self):
            super(Net, self).__init__()
            self.fc = nn.Dense(10, 20)
    
        def construct(self, x):
            return self.fc(x)
    
    net = Net()
    input_data = Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
    output = net(input_data)
    print(output)
    
    Output result:
    [[-0.01353457 -0.02122901 -0.00785669  0.00269206 -0.01708164 -0.00544778
      -0.03082642 -0.0366452   0.04129747  0.03599006]
     [-0.00863788 -0.02421871  0.01378689 -0.01104463  0.01437971 -0.0325471
      -0.01884251 -0.03105863  0.04269245  0.03176927]]
    
    In the above example, we defined a simple neural network model Netwith one fully connected layer. By calling the model instance, we pass the input data to the model and get the output result. In this process, the calculation graph defines the flow path of data from input to output and the corresponding calculation operations.

Computational graphs are a very important concept when you use deep learning frameworks. It describes the calculation process in the model, including the flow path of data and corresponding calculation operations.

In Netthe constructor of the class __init__, we create a fully connected layer ( nn.Dense), specifying the number of input features to be 10 and the number of output features to be 20. This fully connected layer is part of the neural network model and is used to perform linear and nonlinear transformations of the input data.

The linear transformation of the fully connected layer can be expressed as:

output = input * weight + bias

where inputis the input tensor , is the weight matrixweight connecting the input and output , and is the bias vector . By multiplying the input data with the weight matrix and adding the bias vector, the fully connected layer realizes the linear transformation. Then, the fully connected layer also applies an activation function , usually yes . The activation function performs a linear transformation on the result , introducing the nonlinear capability of the model. Commonly used activation functions include ReLU, Sigmoid, and Tanh.bias


非线性非线性映射

Then, we define constructmethods that take input tensors xand perform the forward pass operation of the model. In this method, we pass the input tensor to the fully connected layer ( self.fc), and return the output result.

Next, we instantiate Netthe class to get a model object net. We then created an input tensor input_datacontaining two samples with 5 features each. This input tensor will be used as input to the model.

Finally, we pass the input tensor input_datato the model object net, and get the output tensor output. Finally, we print out the value of the tensor.

Now let's look at the concept of a computational graph. A calculation graph is a concept used to describe the calculation process. It consists of a series of calculation nodes and data flows, representing the calculation process of the model.
In this example, a computational graph describes the flow path of data from input to output and the corresponding computational operations. The input tensor is used as the starting pointinput_data of the data flow , and after the calculation operation of the fully connected layer , the output tensor is finally obtained .output

The advantage of the computational graph is that it canautomatic recordingThe calculation process in the model, and can perform automatic differentiation for backpropagation and parameter update. It enables deep learning frameworks to efficiently perform various complex computational operations.

  1. 操作符(Operator):
    In MindSpore, an operator is a function or method used to perform various mathematical and logical operations. They can be used to perform operations such as addition, multiplication, and convolution on tensors to build complex calculation processes. Here is an example of a simple operator:
    import mindspore.ops as ops
    import mindspore.numpy as mnp
    
    x = mnp.array([1, 2, 3])
    y = mnp.array([4, 5, 6])
    z = ops.add(x, y)
    print(z)
    
    Output result:
    [5 7 9]
    
    In the above example, we used MindSpore's operators ops.addto perform element-wise addition of two tensors. This operator adds elements at corresponding positions in two tensors and returns a new tensor.

By understanding these core concepts, you can better understand and apply MindSpore.

  • 张量As a data container , store and represent data;
  • 计算图Describes the calculation process and connects various operations;
  • 操作符Used to perform various mathematical and logical operations .

Together, these concepts form the basis of the MindSpore framework and help you develop and train deep learning models.

simple example

The following is a simple example that demonstrates how to use MindSpore to create a simple neural network model and train it:

import mindspore.nn as nn
from mindspore import Tensor
from mindspore import context

# 定义一个简单的神经网络模型
class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()
        self.fc = nn.Dense(10, 20)  # 全连接层

    def construct(self, x):
        return self.fc(x)

# 创建模型实例
net = Net()

# 创建输入数据
input_data = Tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], dtype=mindspore.float32)

# 设置运行环境
context.set_context(mode=context.PYNATIVE_MODE)  # 使用动态图模式

# 前向传播计算
output = net(input_data)
print(output)

Output result:

[[ 0.17096008 -0.07126708 -0.15691847  0.1241325  -0.16767085 -0.10899045
-0.12603217 -0.1612322   0.05598223 -0.06014717 -0.02817714 -0.08028036
-0.01887886 -0.07969902  0.16505516 -0.14510706  0.02585109 -0.01945627
0.10038166 -0.08208209]
[-0.02955157 -0.12271015 -0.10049891  0.12457395 -0.12001585 -0.146377
-0.18082708 -0.1842674   0.07435594 -0.09068934 -0.07357719 -0.14754806
0.01935469 -0.13508569  0.1484941  -0.1347757   0.02730994 -0.01618791
0.07225682 -0.02873495]]

It is a 2x20 tensor representing the model's predictions for the input data. Each row corresponds to an input sample, and each column corresponds to a predicted value.

Note that the exact value of the output may vary due to the initialization and randomness of the model. Therefore, you may get slightly different results when you run the code.

But overall, the output should have a similar structure and magnitude .

In the above example, we first imported the related modules of MindSpore and defined a simple neural network model Net. Then, we created an input data input_data, which is a tensor containing two samples. Next, we constructperform the forward propagation calculation by calling the method of the model instance, and print out the result.

Through this simple example, you can see that creating and running a neural network model with MindSpore is very intuitive and simple. You can configure the model design, data processing and training process according to your own needs. At the same time, MindSpore provides more functions and tools to help you perform more complex and practical tasks in the field of deep learning.

Guess you like

Origin blog.csdn.net/qq_22841387/article/details/131523716