Model training method based on machine learning framework and related device

By creating tensor data through the data structure and quantum modules of the machine learning framework, and combining it with the classical module training layer, the problem of frequent changes in training methods for machine learning models in different scenarios is solved, thus improving development efficiency.

CN116432691BActive Publication Date: 2025-12-09ORIGIN QUANTUM COMPUTING TECH (HEFEI) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111680611.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-30
Publication Date
2025-12-09
Estimated Expiration
2041-12-30

AI Technical Summary

Technical Problem

Existing machine learning models require frequent changes to training methods in different application scenarios, resulting in low development efficiency.

Method used

A model training method based on a machine learning framework is adopted. Tensor data is created through a data structure module, and a machine learning model is created by combining quantum and classical modules. The training layer is used to update parameters, including a loss function layer and an optimizer layer, to achieve model training.

Benefits of technology

It reduces the steps and time required to create training layers, improves the development efficiency of model training, is applicable to different machine learning models and scenarios, reduces adaptation operations, and improves overall development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116432691B_ABST
    Figure CN116432691B_ABST
Patent Text Reader

Abstract

The application discloses a model training method based on a machine learning framework and related equipment, which is applied to an electronic device including a machine learning framework, the machine learning framework including a data structure module, a quantum module and a classical module, and the method includes: calling the data structure module to obtain input training data, creating tensor data including the training data, and calling the quantum module and / or the classical module to create a machine learning model; inputting the tensor data into the machine learning model, and calling the classical module to create a training layer of the machine learning model; inputting an output result of the machine learning model into the training layer to update parameters of the machine learning model, and obtaining a trained machine learning model. The technical solution can improve the training and development efficiency for different machine learning models or different application scenarios thereof.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application belongs to the technical field of quantum computing, and particularly relates to a model training method based on a machine learning framework and related equipment. BACKGROUND

[0002] Machine learning models are widely used in artificial intelligence research due to their excellent performance. By training a machine learning model using labeled training data, an expected machine learning model can be obtained, which can then be used in specific application work such as speech recognition and image recognition. Machine learning models do not need to be manually set up for specific application scenarios. By training a machine learning model, it can establish its own working standards, and it has good adaptability to different application scenarios. With the development of quantum computing, more and more machine learning models containing quantum computing layers have emerged.

[0003] In related technologies, for different machine learning models, especially machine learning models containing quantum computing layers, or when machine learning models are applied to different scenarios, the training method needs to be changed to obtain a suitable training method, which is low in development efficiency. SUMMARY

[0004] The purpose of the present application is to provide a model training method based on a machine learning framework and related equipment, which aims to reduce the changes of the training method of machine learning models when training different machine learning models or machine learning models applied to different scenarios, and improve the development efficiency.

[0005] To achieve the above-mentioned purpose, the first aspect of the embodiments of the present application provides a model training method based on a machine learning framework, applied to an electronic device comprising a machine learning framework, wherein the machine learning framework comprises a data structure module, a quantum module and a classical module, and the method comprises:

[0006] calling the data structure module to obtain input training data, creating tensor data comprising the training data, and calling the quantum module and / or the classical module to create a machine learning model;

[0007] inputting the tensor data into the machine learning model, and calling the classical module to create a training layer of the machine learning model;

[0008] inputting the output result of the machine learning model into the training layer to update the parameters of the machine learning model, and obtaining the trained machine learning model.

[0009] Optionally, the training layer comprises a loss function layer and an optimizer layer, and the classical module comprises:

[0010] a loss function unit configured to calculate a loss function of the machine learning model;

[0011] an optimizer unit configured to update parameters of the machine learning model based on the loss function when training the machine learning model, so as to optimize the machine learning model;

[0012] the calling the classical module to create a training layer of the machine learning model comprises:

[0013] the calling the loss function unit to create the loss function layer;

[0014] the calling the optimizer unit to create the optimizer layer.

[0015] Optionally, the inputting the output result of the machine learning model into the training layer to update the parameters of the machine learning model to obtain the trained machine learning model comprises:

[0016] the inputting the output result of the machine learning model into the loss function layer to calculate a value of the loss function of the machine learning model;

[0017] the inputting the value of the loss function into the optimizer layer to update the parameters of the machine learning model based on the value of the loss function when determining that the value of the loss function does not satisfy a preset condition;

[0018] the determining the value of the loss function of the machine learning model after updating the parameters;

[0019] the determining the machine learning model after updating the parameters as the trained machine learning model when determining that the value of the loss function satisfies a preset condition.

[0020] Optionally, the updating the parameters of the machine learning model based on the value of the loss function comprises:

[0021] calculating a gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function;

[0022] updating the parameters of the machine learning model based on the gradient and a gradient descent algorithm.

[0023] Optionally, if the machine learning model created by calling the quantum module comprises a quantum computing layer created based on the quantum module, the calculating the gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function comprises:

[0024] calculating a first gradient of the loss function with respect to an output result of the quantum computing layer based on the value of the loss function, and calculating a second gradient of the output result of the quantum computing layer with respect to a parameter of the quantum computing layer;

[0025] multiplying the first gradient and the second gradient to obtain a gradient of the loss function with respect to a parameter of the machine learning model.

[0026] Optionally, the calculating the second gradient of the output result of the quantum computing layer with respect to the parameter of the quantum computing layer comprises:

[0027] calculating a first output result of the quantum computing layer after adding a first bias quantity to the parameter of the quantum computing layer, and calculating a second output result of the quantum computing layer after subtracting a second bias quantity from the parameter of the quantum computing layer;

[0028] calculating the second gradient of the output result of the quantum computing layer with respect to the parameter of the quantum computing layer based on a difference between the first output result and the second output result.

[0029] Optionally, after the inputting the tensor data into the machine learning model and the invoking the classical module to create a training layer of the machine learning model, the method further comprises:

[0030] constructing a directed computation graph by taking the loss function, the parameter of the machine learning model, and an output result of each sub-step in a process of calculating the loss function based on the parameter as a computing node, and taking a relationship among the loss function, the parameter, and the output result of the sub-step as an edge;

[0031] The calculating the gradient of the loss function with respect to the parameter of the machine learning model based on the value of the loss function comprises:

[0032] calculating the gradient of the loss function with respect to the parameter of the machine learning model based on the value of the loss function and the computation graph.

[0033] Optionally, the calculating the gradient of the loss function with respect to the parameter of the machine learning model based on the value of the loss function and the computation graph comprises:

[0034] determining a path from a computing node corresponding to the loss function to a computing node corresponding to the parameter of the machine learning model in the computation graph;

[0035] calculating an intermediate gradient of each computing node of a non-leaf node on the path with respect to a predecessor node of the computing node based on the value of the loss function;

[0036] multiply all the intermediate gradients calculated to obtain the gradient of the loss function with respect to the parameters.

[0037] In a second aspect, the present application provides a model training device based on a machine learning framework, applied to an electronic device comprising a machine learning framework, the machine learning framework comprising a data structure module, a quantum module and a classical module, the device comprising:

[0038] a creating module configured to call the data structure module to obtain input training data, create tensor data comprising the training data, and call the quantum module and / or the classical module to create a machine learning model;

[0039] an input training module configured to input the tensor data into the machine learning model and call the classical module to create a training layer of the machine learning model;

[0040] a parameter updating module configured to input an output result of the machine learning model into the training layer and update parameters of the machine learning model based on an output result of the training layer to obtain a trained machine learning model.

[0041] Optionally, the training layer comprises a loss function layer and an optimizer layer, and the classical module comprises:

[0042] a loss function unit configured to calculate a loss function of the machine learning model;

[0043] an optimizer unit configured to update parameters of the machine learning model based on the loss function when training the machine learning model to optimize the machine learning model;

[0044] Optionally, the input training module is further configured to:

[0045] call the loss function unit to create the loss function layer;

[0046] call the optimizer unit to create the optimizer layer.

[0047] Optionally, the parameter updating module is further configured to:

[0048] input the output result of the machine learning model into the loss function layer to calculate a value of the loss function of the machine learning model;

[0049] when determining that the value of the loss function does not satisfy a preset condition, input the value of the loss function into the optimizer layer to update the parameters of the machine learning model based on the value of the loss function;

[0050] determine the value of the loss function of the machine learning model after updating the parameters.

[0051] determining that the value of the loss function satisfies a preset condition, taking the machine learning model after the parameter update as the trained machine learning model.

[0052] Optionally, the parameter update module is further configured to:

[0053] calculate a gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function;

[0054] update the parameters of the machine learning model based on the gradient and a gradient descent algorithm.

[0055] Optionally, if the quantum module is called to create a machine learning model, the machine learning model includes a quantum computing layer created based on the quantum module, and the parameter update module is further configured to:

[0056] calculate a first gradient of the loss function with respect to an output result of the quantum computing layer based on the value of the loss function, and calculate a second gradient of the output result of the quantum computing layer with respect to the parameters of the quantum computing layer;

[0057] multiply the first gradient and the second gradient to obtain a gradient of the loss function with respect to the parameters of the machine learning model.

[0058] Optionally, the parameter update module is further configured to:

[0059] calculate a first output result of the quantum computing layer after adding a first bias to the parameters of the quantum computing layer, and calculate a second output result of the quantum computing layer after subtracting a second bias from the parameters of the quantum computing layer;

[0060] calculate the second gradient of the output result of the quantum computing layer with respect to the parameters of the quantum computing layer based on a difference between the first output result and the second output result.

[0061] Optionally, the apparatus further includes:

[0062] a computation graph construction module configured to, after the input training module inputs the tensor data into the machine learning model and calls the classical module to create a training layer of the machine learning model, take the loss function, the parameters of the machine learning model, and an output result of each sub-step in a process of calculating the loss function based on the parameters as computation nodes, and take a relationship among the loss function, the parameters, and the output result of the sub-step as an edge, to construct a directed computation graph.

[0063] the parameter update module is further configured to:

[0064] compute a gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function and the computation graph.

[0065] Optionally, the parameter updating module is further configured to:

[0066] determine a path from a computing node corresponding to the loss function to a computing node corresponding to the parameters of the machine learning model in the computation graph;

[0067] compute an intermediate gradient of each computing node on the path with respect to a predecessor node of the computing node based on the value of the loss function;

[0068] multiply all the intermediate gradients to obtain the gradient of the loss function with respect to the parameters.

[0069] In a third aspect, the embodiment of the present application provides a storage medium, wherein the storage medium stores a computer program, and the computer program is configured to execute the steps of the method in any one of the first aspect.

[0070] In a fourth aspect, the embodiment of the present application provides an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the steps of the method in any one of the first aspect.

[0071] Based on the above technical solution, whether for different machine learning models or when the machine learning model is applied to different scenarios, the training layer of the machine learning model can be created by calling the classical module, and then the tensor data is input into the machine learning model, and the training result of the machine learning model is input into the training layer, so that the created machine learning model can be trained. The training layer can be created by calling the classical module, without starting from scratch, reducing the steps and time required for creating the training layer, and thereby improving the development efficiency of training when changing the machine learning model or different application scenarios. At the same time, for different machine learning models or different application scenarios of the machine learning model, the tensor data corresponding to the training data is created through the data structure module, and the machine learning model is created based on the quantum module and / or the classical module, so that the tensor data and the machine learning model can follow the same standard based on the machine learning framework, reducing the adaptation operation between them, and thereby improving the development efficiency of training the machine learning model. BRIEF DESCRIPTION OF DRAWINGS

[0072] Figure 1 is a hardware structure block diagram of a computer terminal for a model training method based on a machine learning framework according to an exemplary embodiment.

[0073] Figure 2is a flowchart of a model training method based on a machine learning framework according to an example embodiment.

[0074] Figure 3 is a block diagram of a machine learning framework according to an example embodiment.

[0075] Figure 4 is a block diagram of a classical module according to an example embodiment.

[0076] Figure 5 is a flowchart of step S22 in a model training method based on a machine learning framework according to an example embodiment.

[0077] Figure 6 is a flowchart of step S23 in a model training method based on a machine learning framework according to an example embodiment.

[0078] Figure 7 is a flowchart of step S232 in a model training method based on a machine learning framework according to an example embodiment.

[0079] Figure 8 is a flowchart of step S2321 in a model training method based on a machine learning framework according to an example embodiment.

[0080] Figure 9 is a flowchart of step S23211 in a model training method based on a machine learning framework according to an example embodiment.

[0081] Figure 10 is another flowchart of a model training method based on a machine learning framework according to an example embodiment.

[0082] Figure 11 is another flowchart of a model training method based on a machine learning framework according to an example embodiment.

[0083] Figure 12 is another flowchart of a model training method based on a machine learning framework according to an example embodiment.

[0084] Figure 13 is a flowchart of step S125 in a model training method based on a machine learning framework according to an example embodiment.

[0085] Figure 14 is a computational graph according to an example embodiment.

[0086] Figure 15This is a block diagram illustrating a model training apparatus based on a machine learning framework according to an exemplary embodiment. Detailed Implementation

[0087] The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention.

[0088] The present invention first provides a model training method based on a machine learning framework, which can be applied to electronic devices, such as computer terminals, specifically ordinary computers, quantum computers, etc.

[0089] The following detailed explanation uses a computer terminal as an example. Figure 1 This is a hardware structure block diagram of a computer terminal illustrating a model training method based on a machine learning framework, according to an exemplary embodiment. For example... Figure 1 As shown, a computer terminal may include one or more ( Figure 1 Only one is shown in the diagram. A processor 102 (which may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.) and a memory 104 for storing a model training method based on a quantum circuit and a machine learning framework are also shown. Optionally, the computer terminal may further include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the computer terminal described above. For example, the computer terminal may also include components that are more complex than those described above. Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.

[0090] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / modules corresponding to a model training method based on a machine learning framework in this embodiment of the application. The processor 102 executes various functional applications and data processing by running the software programs and modules stored in the memory 104, thereby implementing the above-described method. The memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to a computer terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0091] The transmission device 106 is configured to receive or send data via a network. The network can include a wireless network provided by a communication provider of a computer terminal. In one example, the transmission device 106 includes a network interface controller (NIC) that can be connected to other network devices through a base station to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module configured to communicate with the Internet wirelessly.

[0092] It should be noted that a real quantum computer is a hybrid structure, which includes two parts: one part is a classical computer responsible for performing classical computation and control; the other part is a quantum device responsible for running a quantum program to implement quantum computation. The quantum program is a sequence of instructions written in a quantum language such as QRunes language that can run on a quantum computer, which supports quantum logic gate operations and ultimately realizes quantum computation. Specifically, the quantum program is a sequence of instructions for operating quantum logic gates in a certain time sequence.

[0093] In practical applications, due to the limitations of the development of quantum device hardware, quantum computation simulation is usually needed to verify quantum algorithms, quantum applications, and the like. Quantum computation simulation is a process of simulating the running of a quantum program corresponding to a specific problem by means of a virtual architecture (i.e., a quantum virtual machine) built by an ordinary computer. Generally, a quantum program corresponding to a specific problem needs to be constructed. The quantum program referred to in the embodiments of the present application is a program written in a classical language representing quantum bits and their evolution, in which quantum bits, quantum logic gates, and the like related to quantum computation are represented by corresponding classical codes.

[0094] As an embodiment of a quantum program, a quantum circuit, also known as a quantum logic circuit, is the most commonly used general quantum computation model, which represents a circuit for operating quantum bits in an abstract concept, and its composition includes quantum bits, a circuit (a time line), and various quantum logic gates, and finally the result needs to be read out through a quantum measurement operation.

[0095] Unlike a traditional circuit connected by metal wires to transmit voltage signals or current signals, in a quantum circuit, the circuit can be regarded as being connected by time, that is, the state of a quantum bit naturally evolves with time, and in this process, the quantum bit is operated according to the instruction of a Hamiltonian operator until it encounters a logic gate.

[0096] A quantum program corresponds to a total quantum circuit as a whole, and the quantum program refers to the total quantum circuit, wherein the total number of qubits in the total quantum circuit is the same as the total number of qubits of the quantum program. It can be understood that a quantum program can be composed of a quantum circuit, a measurement operation for a qubit in the quantum circuit, a register for storing measurement results, and a control flow node (jump instruction). A quantum circuit can include tens, hundreds or even thousands of quantum logic gate operations. The execution process of a quantum program is the process of executing all quantum logic gates in a certain time sequence. It should be noted that the time sequence refers to the time sequence of executing individual quantum logic gates.

[0097] It should be noted that in classical computing, the most basic unit is a bit, and the most basic control mode is a logic gate, which can be used to control the circuit. Similarly, the way to handle qubits is quantum logic gates. Using quantum logic gates can evolve quantum states. Quantum logic gates are the basis of quantum circuits, and quantum logic gates include single-bit quantum logic gates such as Hadamard gate (H gate), Pauli-X gate (X gate), Pauli-Y gate (Y gate), Pauli-Z gate (Z gate), RX gate (RX rotation gate), RY gate (RY rotation gate), RZ gate (RZ rotation gate), etc. Multi-bit quantum logic gates such as CNOT gate, CR gate, iSWAP gate, Toffoli gate, etc. Quantum logic gates are generally represented by unitary matrices, which are not only matrix forms but also operations and transformations. The effect of a general quantum logic gate on a quantum state is calculated by left multiplying the quantum state right vector corresponding to the vector. For example, the vector corresponding to the quantum state right vector |0> can be The vector corresponding to the quantum state right vector |1> can be

[0098] Figure 2 FIG. 1 is a flowchart of a model training method based on a machine learning framework according to an example embodiment. Referring to FIG. 1, the method includes the following steps. Figure 2 The embodiment provides a model training method based on a machine learning framework, which can be applied to an electronic device including a machine learning framework 30 as shown in FIG. 2. Figure 3 The machine learning framework 30 includes a data structure module 31, a quantum module 32, and a classical module 33. The method includes the following steps.

[0099] S21, calling the data structure module to obtain input training data, creating tensor data including the training data, and calling the quantum module and / or the classical module to create a machine learning model.

[0100] S22, input the tensor data into the machine learning model, and call the classical module to create a training layer of the machine learning model.

[0101] S23, input the output result of the machine learning model into the training layer to update the parameters of the machine learning model, and obtain the trained machine learning model.

[0102] Specifically, the machine learning framework integrates a plurality of function sets for creating and training a machine learning model, and through the interface defined thereby, the functions can be conveniently called to implement related operations on the machine learning model. As shown in Figure 3 The machine learning framework 30 can include:

[0103] A data structure module 31 configured to create tensor data for inputting into a machine learning model, and perform operations on the tensor data;

[0104] A quantum module 32 configured to create a quantum computing layer for creating a machine learning model;

[0105] A classical module 33 configured to create a classical computing layer for creating a machine learning model, an abstract class layer for encapsulating the quantum computing layer and the classical computing layer, and a machine learning model training layer for training and optimizing the machine learning model.

[0106] Specifically, the data structure module 31 defines the data structure of the tensor data, and by calling the data structure module 31, the input data can be converted into tensor data for inputting into a machine learning model for forward calculation. The data structure module 31 can also define operations between tensor data, such as mathematical operations and logical operations, and further can call the data structure module 31 to create a classical computing layer of a machine learning model based on the operation relationship between the tensor data, for example, a fully connected layer of a classical neural network defines the relationship between input data x and output data y through a function y = wx + b, where w and b are parameters. By converting the input data x, the parameter w, and the parameter b into tensor data, and calling the data structure module 31 to perform operations corresponding to the function on these tensor data, a fully connected layer can be constructed.

[0107] In a possible implementation, the data structure module 31 can be configured to arrange the input data according to a preset data structure to create tensor data for inputting the machine learning model, and create tensor data for inputting the machine learning model arranged according to the preset data structure and with numerical values determined. Then, in step S21, for the input training data, the training data can be arranged according to the preset data structure to obtain tensor data, and the training data can be stored as part of the tensor data. For example, the training data obtained is 1, 2, 3, and the input training data can be converted into a vector structure [1, 2, 3] as part of the tensor data.

[0108] It should be noted that the tensor data can include not only the data values arranged according to the preset data structure, but also information of the tensor data calculated from the data values and gradient functions of the tensor data relative to the tensor data containing the data values, wherein the information of the tensor data calculated from the data values can include variables of the tensor data, data value storage addresses, and data values, as long as it indicates that the corresponding node of the tensor data is a predecessor node of the corresponding node of the tensor data calculated from the data values. Taking the above function relationship y = wx + b as an example, for the tensor data y, it includes the data values corresponding to y such as [1, 2, 3], and also includes the information of the tensor data calculated from w, x, and b of y and the gradient functions of y relative to w, x, and b, respectively. In a possible implementation, the information can include data value storage addresses of w, x, and b, and the tensor data y includes the gradient function x of y relative to w, the gradient function w of y relative to x, and the gradient function 1 of y relative to b. Then, when training the machine learning model, the gradient values of y relative to w, x, and b are calculated by back propagation, and specifically, the data values of y and the data values and corresponding gradient functions of w, x, and b can be directly obtained from the tensor data y, and the gradient values of y relative to w, x, and b are calculated through the data values and corresponding gradient functions.

[0109] Specifically, for the quantum module 32, a quantum computing layer can be created by calling the quantum module 32, the quantum computing layer is a program module containing a quantum program, and can be used to implement quantum computing of the corresponding quantum program. By encapsulating the quantum program according to a certain standard, the quantum computing layer is convenient to use when creating and training the machine learning model. For the part of the machine learning model implemented by quantum computing, it can be understood as the corresponding quantum computing layer. The quantum program is a program for implementing quantum computing, and the quantum program can be obtained by calling the quantum module 32 to create quantum logic gates acting on quantum bits in a specific order, and the quantum program is encapsulated to obtain the quantum computing layer.

[0110] In particular, the classical computing layer is a classical computing part in the machine learning model, which can be obtained by encapsulating the created classical computing program according to a certain standard by the classical module 33, so that the classical computing layer is convenient to use when training the machine learning model. After creating the quantum computing layer and the classical computing layer, the classical module 33 can be used to encapsulate the quantum computing layer and the classical computing layer to create an abstract class layer conforming to a certain standard, which is realized by a method of a class in a programming language. The quantum computing layer and the classical computing layer can be encapsulated to create a machine learning model conforming to a certain standard, for example, the created abstract class layer defines a way of forward operation of the machine learning model, which is convenient to forward operate the machine learning model to obtain a calculation result used for calculating a loss function when training the machine learning model, and the order relationship of gradient calculation when backward calculating can also be obtained. The classical module 33 can also be used to create a training layer of the machine learning model to train the machine learning model.

[0111] In step S21, in addition to calling the data structure module 31 to create tensor data, the quantum module 32 can be called to create a quantum computing layer to obtain a pure quantum machine learning model including the quantum computing layer, or the classical module 33 can be called to create a classical computing layer to obtain a pure classical machine learning model including the classical computing layer. The quantum module 32 and the classical module 33 can also be called to create a quantum computing layer and a classical computing layer to obtain a hybrid machine learning model including the quantum computing layer and the classical computing layer.

[0112] After creating the training data and the machine learning model, step S22 is performed, in which the tensor data is input into the machine learning model, and the machine learning model is forward operated to obtain an output result of the machine learning model after calculating the tensor data. In addition, to train the machine learning model, the classical module 33 is called to create a training layer of the machine learning model.

[0113] Optionally, the training layer includes a loss function layer and an optimizer layer, as shown in Figure 4 The classical module 33 includes:

[0114] The loss function unit 331 is configured to calculate a loss function of the machine learning model.

[0115] The optimizer unit 332 is configured to update parameters of the machine learning model based on the loss function when training the machine learning model, so as to optimize the machine learning model.

[0116] Optionally, as shown in Figure 5 The classical module is called to create the training layer of the machine learning model, including:

[0117] S221, the loss function unit is called to create the loss function layer;

[0118] S222, invoking the optimizer unit to create the optimizer layer.

[0119] Specifically, the loss function unit 331 is configured to calculate a loss function of the machine learning model, for example, a square difference between a forward operation result of the machine learning model and label data, or a binary cross entropy between the forward operation result and the label data. The optimizer unit 332 is configured to update parameters of the machine learning model by using a gradient descent algorithm according to a gradient of the loss function with respect to the parameters of the machine learning model. For example, the gradient descent algorithm used by the optimizer can be any one of a stochastic gradient descent (SGD), an adaptive gradient algorithm (Adagrad), and an adaptive moment estimation (Adam), and other algorithms can also be used to update the parameters of the machine learning model. The present application does not make specific limitations on which types of loss functions the loss function unit 331 can calculate and which methods the optimizer unit 332 uses to update the parameters.

[0120] To implement the training of the machine learning model, step S221 can be performed to invoke the loss function unit 331 to create the loss function layer. The loss function layer is a packaged calculation module that defines a calculation method of the loss function, and thus when an output result of the machine learning model is input to the loss function layer, the loss function of the machine learning model can be calculated according to the calculation method defined by the loss function layer. After the loss function layer is created, step S222 can be performed to invoke the optimizer unit 332 to create the optimizer layer. After the output result is input to the loss function layer and the loss function is calculated, the parameters of the machine learning model are updated according to the loss function until appropriate parameters are obtained so that the machine learning model can achieve the expected effect, and the optimization of the machine learning model is completed.

[0121] After the training layer is created, step S23 is performed to input an output result of the machine learning model to the training layer, and the parameters of the machine learning model are updated according to a certain method. If the machine learning model after updating the parameters meets a preset condition, for example, a value of a loss function corresponding to the machine learning model is less than a threshold, the updating of the parameters is stopped, and the machine learning model after updating the parameters is taken as a trained machine learning model. If the machine learning model after updating the parameters does not meet the preset condition, for example, the value of the loss function corresponding to the machine learning model is greater than the threshold, the updating of the parameters can be continued until the machine learning model that meets the preset condition is obtained.

[0122] Based on the above technical solution, whether for different machine learning models or when the machine learning model is applied to different scenarios, the training layer of the machine learning model can be created by calling the classical module 33, and then the tensor data is input into the machine learning model, and the training result of the machine learning model is input into the training layer, so that the created machine learning model can be trained. The training layer can be created by calling the classical module 33, without starting from scratch, reducing the steps and time required to create the training layer, and thereby improving the development efficiency of training when changing the machine learning model or different application scenarios. At the same time, for different machine learning models or different application scenarios of the machine learning model, the tensor data corresponding to the training data is created by the data structure module 31, and the machine learning model is created based on the quantum module 32 and / or the classical module 33, so that the tensor data and the machine learning model can follow the same standard based on the machine learning framework, reducing the adaptation operation between them, and thereby improving the development efficiency of training the machine learning model. Moreover, through the above scheme, whether for a pure quantum machine learning model or a pure classical machine learning model, or a hybrid machine learning model including a classical computing layer and a quantum computing layer, the development efficiency of training can be improved, and the application range is wide.

[0123] Optionally, as shown in Figure 6 The output result of the machine learning model is input into the training layer to update the parameters of the machine learning model, and the trained machine learning model is obtained, including:

[0124] S231, input the output result of the machine learning model into the loss function layer to calculate the value of the loss function of the machine learning model.

[0125] S232, when it is determined that the value of the loss function does not satisfy the preset condition, input the value of the loss function into the optimizer layer to update the parameters of the machine learning model based on the value of the loss function.

[0126] S233, determine the value of the loss function of the machine learning model after updating the parameters.

[0127] S234, when it is determined that the value of the loss function satisfies the preset condition, the machine learning model after updating the parameters is taken as the trained machine learning model.

[0128] To train the created machine learning model, enter step S231, input the output result of the machine learning model into the loss function layer, calculate the value of the loss function of the machine learning model, and the loss function represents the gap between the output result of the machine learning model and the label data conforming to the real calculation condition. For example, the training data can include sample data and label data, after the sample data is input into the machine learning model to obtain the output result, the output result is input into the loss function layer, the squared difference between the label data and the output result is calculated, and the sum of all squared differences is calculated to calculate the value of the loss function.

[0129] In step S232, whether the value of the loss function meets the preset condition can be determined by comparing the value of the loss function with a preset threshold value, for example, when it is determined that the value of the loss function is greater than or equal to the threshold value, the value of the loss function is input into the optimizer layer. Of course, other methods can also be used to determine that the value of the loss function does not meet the preset condition, as long as the value of the preset function can be used to determine that the current machine learning model does not meet the expectation. When the preset condition is not met, the value of the loss function is input into the optimizer layer, and the gradient of the loss function with respect to the parameters of the machine learning model can be calculated using the value of the loss function, and then the parameters of the machine learning model are updated based on the gradient descent algorithm.

[0130] In step S233, after updating the parameters of the machine learning model, the value of the corresponding loss function is recalculated. And whether the value of the loss function meets the preset condition is re-determined. If it does not meet the condition, it can return to step S232 to continue updating the parameters of the machine learning model according to the value of the loss function. If it meets, it can enter step S234.

[0131] In step S234, when it is determined that the value of the loss function meets the preset condition, for example, the value of the loss function is less than the threshold value, it means that the output result of the machine learning model for the sample data in the training data and the label data is very small, and the machine learning model can achieve the expected application effect, and then the updated machine learning model is used as the trained machine learning model, and the parameter updating is stopped.

[0132] Optionally, as shown in Figure 7 updating the parameters of the machine learning model based on the value of the loss function includes:

[0133] S2321, calculating the gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function.

[0134] S2322, updating the parameters of the machine learning model based on the gradient and the gradient descent algorithm.

[0135] In step S2321, for example, the partial derivative of the loss function with respect to its parameters can be obtained to obtain the gradient of the loss function with respect to the parameters. In step S2322, according to the obtained gradient, the relevant formula of the gradient descent algorithm is brought in, and the parameters of the machine learning model are updated. For example, the gradient descent algorithm can be batch gradient descent, stochastic gradient descent, etc. The gradient reflects the direction in which the loss function changes fastest, and the parameters can be quickly changed through the gradient descent algorithm, thereby improving the speed of change of the value of the loss function, so as to quickly find the parameters corresponding to the value of the loss function that meets the preset condition, and obtain the machine learning model that meets the requirements.

[0136] Optionally, as shown in Figure 9 If the quantum module 32 is called to create a machine learning model, the machine learning model includes a quantum computing layer created based on the quantum module 32, and the calculation of the gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function includes:

[0137] S23211, based on the value of the loss function, calculating the first gradient of the loss function with respect to the output result of the quantum computing layer, and calculating the second gradient of the output result of the quantum computing layer with respect to the parameters of the quantum computing layer.

[0138] S23212, multiplying the first gradient and the second gradient to obtain the gradient of the loss function with respect to the parameters of the machine learning model.

[0139] The machine learning model includes a quantum computing layer, for example, the machine learning model is a pure quantum machine learning model including only a quantum computing layer, or a hybrid machine learning model including a quantum computing layer and a classical computing layer. At this time, in order to obtain the gradient of the loss function with respect to the parameters in the quantum computing layer of the machine learning model, in step S23211, based on the chain rule, the first gradient of the loss function with respect to the output result of the quantum computing layer and the second gradient of the output result with respect to the parameters of the quantum computing layer are first obtained. Then, step S23211 is executed again, and the first gradient and the second gradient are multiplied to obtain the gradient of the loss function with respect to the parameters of the quantum computing layer.

[0140] For example, the machine learning model includes a quantum computing layer and a classical computing layer, where the quantum computing layer corresponds to a computing function f(x; a) and the classical computing layer corresponds to a computing function g(x; b), where x is input data of the computing function, and a and b are parameters. The obtained input data x1 is first input into f(x; a) to obtain y1, and then y1 is input into g(x; b) to obtain an output result y2 of the machine learning model. A loss function is a sum of squared differences between y2 and label data. To calculate the gradient of the loss function with respect to the parameter a, a first gradient of the loss function with respect to y1 is calculated, and a second gradient of y1 with respect to a is calculated, and then the first gradient and the second gradient are multiplied to obtain the gradient of the loss function with respect to the parameter a.

[0141] Optionally, as shown in Figure 9 calculating a second gradient of the output result of the quantum computing layer with respect to the parameter of the quantum computing layer includes:

[0142] S23211a, calculating a first output result of the quantum computing layer after the parameter of the quantum computing layer is added with a first bias, and calculating a second output result of the quantum computing layer after the parameter of the quantum computing layer is subtracted by a second bias.

[0143] S23211b, calculating a second gradient of the output result of the quantum computing layer with respect to the parameter of the quantum computing layer based on a difference between the first output result and the second output result.

[0144] Since it is difficult to obtain the second gradient of the output result of the quantum computing layer with respect to the parameter in the quantum computing layer by solving an analytical expression, the method of parameter displacement is adopted, which can improve the accuracy of the obtained second gradient, and the gradient can be obtained by this method regardless of whether the quantum circuit of the quantum computing layer runs in a virtual machine or a real quantum device, and has wide adaptability. Specifically, in step S23211a, the first bias and the second bias are added to and subtracted from the parameter of the quantum computing layer respectively to obtain the first output result and the second output result of the quantum computing layer corresponding thereto respectively, and then step S23211b is performed to obtain the second gradient according to the difference between the first output result and the second output result.

[0145] Using the foregoing example, the first bias and the second bias are For the quantum computing layer f(x; a), the first output result and the second output result are calculated first, and then the second gradient is obtained by the following formula:

[0146]

[0147] In one possible implementation, f(x; a) =<0|UH (x; a)HU(x; a) | 0, x is training data, H is an observation, U H (x; a) is the conjugate transpose of U(a), U(a) corresponds to the part of quantum computation in the quantum computing layer for performing a specific algorithm. Of course, in other possible embodiments, other methods can be used to obtain the gradient of the loss function with respect to the parameters, such as finite difference method.

[0148] In one possible embodiment, referring to Figure 10 , when the quantum circuit of the quantum computing layer of the machine learning model is run on a real quantum chip, for a pure quantum machine learning model containing only the quantum circuit, to train the model, the data is first input so that the quantum computer where the quantum chip is located obtains the training data, then the quantum circuit is run on the real chip to perform forward calculation, that is, the forward operation of the machine learning model, the result quantum state of the quantum computing layer, that is, the quantum layer, is measured to obtain the measurement value of the quantum layer output, then the loss function is calculated according to the measurement value, then to calculate the gradient, the above parameter displacement method can be used to run the quantum circuit on the real chip to calculate the gradient, and the gradient is calculated according to the calculation results of the quantum circuit with and without the bias quantity added to the parameters, then the parameter value of the variable parameter of the quantum circuit is obtained according to the gradient function to optimize the parameter value, if the preset condition is not met, for example, the value of the loss function at this time is greater than the threshold value, the above process can be repeated to continue to optimize the variable parameter until the preset condition is met to obtain a machine learning model meeting the requirements.

[0149] In one possible embodiment, referring to Figure 11 , the machine learning model is a neural network model, which includes a classical neural network module, a quantum computing module A and a quantum circuit module B neural network model, wherein the classical neural network module is a classical computing layer for implementing classical computation, and the quantum computing module A and the quantum circuit module B are quantum computing layers for implementing quantum computation. In forward operation, the input data is input into the classical neural network module and the quantum computing module A, and then the output thereof is input into the quantum circuit module B, and finally the loss function is calculated according to the output of the quantum circuit B. After that, to train the model, the reverse operation process is entered, the parameter gradient of the quantum circuit module B, the parameter gradient of the classical neural network module and the parameter gradient of the quantum computing module A are calculated, and the parameters in these modules are updated respectively by using the gradient descent method.

[0150] Figure 12 is another flowchart of a model training method based on a machine learning framework according to an example embodiment, as shown in Figure 12 , the method comprises:

[0151] S121, calling the data structure module to obtain input training data, creating tensor data including the training data, and calling the quantum module and / or the classical module to create a machine learning model.

[0152] S122, inputting the tensor data into the machine learning model, and calling the classical module to create a training layer of the machine learning model.

[0153] S123, constructing a directed computation graph by taking the loss function, the parameters of the machine learning model, and the output results of each sub-step in the process of calculating the loss function as computing nodes, and taking the relationship between the loss function, the parameters, and the output results of each sub-step as edges.

[0154] S124, inputting the output results of the machine learning model into the loss function layer to calculate the value of the loss function of the machine learning model.

[0155] S125, when it is determined that the value of the loss function does not satisfy a preset condition, inputting the value of the loss function into the optimizer layer to calculate the gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function and the computation graph; updating the parameters of the machine learning model based on the gradient and a gradient descent algorithm.

[0156] S126, determining the value of the loss function of the machine learning model after updating the parameters.

[0157] S127, when it is determined that the value of the loss function satisfies a preset condition, taking the machine learning model after updating the parameters as the trained machine learning model.

[0158] Steps S121 and S122 can refer to steps S21 and S22, respectively. Steps S124 to S127 can refer to steps S231 to S234, respectively.

[0159] In step S123, the computation graph is a directed acyclic graph representing the relationship between data in the form of a graph of a data structure, composed of computing nodes and edges. In constructing the above-mentioned computation graph, the loss function, the parameters, and the output results of each sub-step in the process of calculating the loss function through the parameters in the forward calculation of the machine learning model are taken as computing nodes according to the order of the forward calculation of the machine learning model, and the computing relationship is taken as an edge to obtain the above-mentioned computation graph. In the computation graph, the data of the predecessor node is the dependent variable of the data of the successor node.

[0160] Further in step S125, the gradient of the loss function with respect to the parameters is calculated based on the value of the loss function according to the computing relationship shown in the computation graph.

[0161] Optionally, as shown in Figure 13 calculating a gradient of the loss function with respect to a parameter of the machine learning model based on the value of the loss function and the computation graph, comprises:

[0162] S1251, determining a path from a computation node corresponding to the loss function to a computation node corresponding to the parameter of the machine learning model in the computation graph.

[0163] S1252, calculating an intermediate gradient of each computation node on the path with respect to a predecessor node of the computation node based on the value of the loss function.

[0164] S1253, multiplying all the intermediate gradients to obtain the gradient of the loss function with respect to the parameter.

[0165] In step S1251, the shortest path between the loss function and the parameter can be determined. Then in step S1252, for each computation node on the path, an intermediate gradient of the computation node with respect to a predecessor node of the computation node is calculated. Since a non-leaf node has no predecessor node, the intermediate gradient of the non-leaf node cannot be calculated. Moreover, the non-leaf node is generally a parameter, and as the end point of the path, the gradient of the non-leaf node does not need to be calculated.

[0166] After the intermediate gradient is calculated, step S1253 is performed to multiply all the intermediate gradients corresponding to the path, and the gradient of the loss function with respect to the parameter can be obtained according to the chain rule.

[0167] For example, referring to Figure 14The corresponding machine learning model includes a quantum computing layer qvc and a classical computing layer fc. In the calculation graph, there are multiple tensor data Qtensor, wherein the tensor data corresponding to the training data is input, the tensor data k is obtained through the formula k = input / 255, so input is the predecessor node of k, and the information of the predecessor node input of k is saved in the calculation node corresponding to k, which is represented by nodes[0]:prev:input, and the gradient calculation formula of k relative to input is saved, which is represented by df:grad_fun_of_input. According to param and k, x = qvc(k) can be calculated, qvc represents the quantum circuit of the quantum computing layer of the machine learning model, and param is the parameter in the quantum computing layer, so k and param are the predecessor nodes of x, and the information of param and the gradient calculation formula are saved in the calculation node corresponding to x, which are represented by nodes[0]:prev:param and df:grad_fun_of_q_param respectively, and the information of k and the gradient calculation formula are saved, which are represented by nodes[1]:prev:k and df:grad_fun_of_q_k. According to x, y = fc(x) can be calculated, fc represents the classical computing program of the classical computing layer of the machine learning model, which can be a fully connected layer for example, and the output y is calculated through x, W and b, W and b are parameters of the classical computing layer, for example, the calculation formula corresponding to fc() can be y = W*x + b, so the tensor data forms of W, b and x are the predecessor nodes of y, and the information of W and the gradient calculation formula are saved in the calculation node corresponding to y, which are represented by nodes[0]:prev:W and df:grad_fun_of_W(), the information of b and the gradient calculation formula are saved, which are represented by nodes[1]:prev:b and df:grad_fun_of_b(), and the information of x and the gradient calculation formula are saved, which are represented by nodes[2]:prev:x and df:grad_fun_of_x(). The value of the loss function loss = LossFunc(y, label) can be calculated through y, LossFunc is a loss function, for example, it can be the sum of the squares of the differences between y and label, y is the predecessor node of loss, and the information of y and the gradient calculation formula are saved in the calculation node corresponding to loss, which are represented by nodes[0]:prev:y and df:grad_fun_of_y(). Since the gradient of loss relative to the label data label does not need to be calculated, label can not be added to the calculation graph as a calculation node. Specifically, the relationship between the predecessor node and the successor node can be stored through a linked list, and the information of multiple predecessor nodes of a calculation node can be stored through an array

[0168] When the gradient needs to be calculated, taking the calculation of the gradient of the loss with respect to the param as an example, first, the path of the loss to the param is determined, and the corresponding calculation nodes of the loss, y, x, and the param are sequentially traversed. According to the gradient function and the information of the predecessor node saved by the calculation nodes, the intermediate gradient of the loss with respect to y, the intermediate gradient of y with respect to x, and the intermediate gradient of x with respect to the param are calculated. Finally, the three intermediate gradients are multiplied to obtain the gradient of the loss with respect to the param.

[0169] Figure 15 is a block diagram of a model training apparatus based on a machine learning framework according to an exemplary embodiment, which can be applied to an electronic device including a machine learning framework 30 as shown in Figure 3 The machine learning framework 30 includes a data structure module 31, a quantum module 32, and a classical module 33, as shown in Figure 15 The apparatus 150 includes:

[0170] A creation module 151 is configured to call the data structure module 31 to obtain input training data, create tensor data including the training data, and call the quantum module 32 and / or the classical module 33 to create a machine learning model.

[0171] An input training module 152 is configured to input the tensor data into the machine learning model and call the classical module 33 to create a training layer of the machine learning model.

[0172] A parameter updating module 153 is configured to input an output result of the machine learning model into the training layer and update parameters of the machine learning model based on an output result of the training layer, to obtain a trained machine learning model.

[0173] Optionally, as shown in Figure 4 The training layer includes a loss function layer and an optimizer layer, and the classical module 33 includes:

[0174] A loss function unit 331 is configured to calculate a loss function of the machine learning model.

[0175] An optimizer unit 332 is configured to update parameters of the machine learning model based on the loss function when training the machine learning model, to optimize the machine learning model.

[0176] Optionally, the input training module 152 is further configured to:

[0177] Call the loss function unit 331 to create the loss function layer.

[0178] Call the optimizer unit 332 to create the optimizer layer.

[0179] Optionally, the parameter updating module 153 is further configured to:

[0180] input the output result of the machine learning model into the loss function layer to calculate a value of the loss function of the machine learning model;

[0181] when determining that the value of the loss function does not satisfy a preset condition, input the value of the loss function into the optimizer layer to update the parameters of the machine learning model based on the value of the loss function;

[0182] determine the value of the loss function of the machine learning model after updating the parameters;

[0183] when determining that the value of the loss function satisfies a preset condition, take the machine learning model after updating the parameters as the trained machine learning model.

[0184] Optionally, the parameter updating module 153 is further configured to:

[0185] calculate a gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function;

[0186] update the parameters of the machine learning model based on the gradient and a gradient descent algorithm.

[0187] Optionally, if the quantum module 32 is called to create a machine learning model, the machine learning model includes a quantum computing layer created based on the quantum module 32, and the parameter updating module 153 is further configured to:

[0188] calculate a first gradient of the loss function with respect to an output result of the quantum computing layer based on the value of the loss function, and calculate a second gradient of the output result of the quantum computing layer with respect to parameters of the quantum computing layer;

[0189] multiply the first gradient and the second gradient to obtain a gradient of the loss function with respect to the parameters of the machine learning model.

[0190] Optionally, the parameter updating module 153 is further configured to:

[0191] calculate a first output result of the quantum computing layer after adding a first bias quantity to the parameters of the quantum computing layer, and calculate a second output result of the quantum computing layer after subtracting a second bias quantity from the parameters of the quantum computing layer;

[0192] calculate a second gradient of the output result of the quantum computing layer with respect to the parameters of the quantum computing layer based on a difference between the first output result and the second output result.

[0193] Optionally, the apparatus 150 further comprises:

[0194] a computation graph construction module, configured to, after the input training module 152 inputs the tensor data into the machine learning model and invokes the classical module 33 to create a training layer of the machine learning model, construct a directed computation graph with the loss function, parameters of the machine learning model, and outputs of each sub-step in a process of computing the loss function as computation nodes, and relationships among the loss function, the parameters, and the outputs of the sub-step as edges;

[0195] The parameter updating module 153 is further configured to:

[0196] compute a gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function and the computation graph.

[0197] Optionally, the parameter updating module 153 is further configured to:

[0198] determine a path from a computation node corresponding to the loss function to a computation node corresponding to the parameters of the machine learning model in the computation graph;

[0199] compute an intermediate gradient of each computation node on the path with respect to a predecessor node of the computation node based on the value of the loss function;

[0200] multiply all the intermediate gradients to obtain the gradient of the loss function with respect to the parameters.

[0201] As to the apparatus in the above embodiments, the specific manners in which the modules perform operations have been described in detail in the embodiments of the method, and thus will not be described in detail here.

[0202] Still another embodiment of the present application further provides a storage medium having a computer program stored therein, where the computer program is configured to perform the steps in the above embodiment of the model training method based on a machine learning framework when running.

[0203] Specifically, in the present embodiment, the storage medium can include but is not limited to a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and various storage media that can store computer programs.

[0204] Still another embodiment of the present application also provides an electronic device comprising a memory and a processor, the memory having stored therein a computer program, the processor being arranged to execute the computer program to perform the steps in the above embodiment of the method for training a model based on a machine learning framework.

[0205] Specifically, the electronic device can further comprise a transmission device connected to the processor and an input / output device connected to the processor.

[0206] Specifically, in the present embodiment, the processor can be arranged to execute the following steps through the computer program:

[0207] The data structure module is called to obtain input training data, tensor data comprising the training data is created, and the quantum module and / or the classical module are called to create a machine learning model;

[0208] The tensor data is input into the machine learning model, and the classical module is called to create a training layer of the machine learning model;

[0209] The output result of the machine learning model is input into the training layer to update the parameters of the machine learning model, obtaining the trained machine learning model.

[0210] The above embodiments according to the drawings illustrate the structure, features and effects of the present application, and the above description is only the preferred embodiments of the present application, but the present application is not limited to the embodiments shown in the drawings, any changes or modifications made in accordance with the concept of the present application, or equivalent embodiments with equivalent changes, shall be within the scope of protection of the present application.

Claims

1. A model training method based on a machine learning framework, characterized in that, Applied to an electronic device comprising a machine learning framework, the machine learning framework comprising a data structure module, a quantum module and a classical module, the data structure module is configured to create tensor data for inputting a machine learning model, and perform operations on the tensor data; the quantum module is configured to create a quantum computing layer for creating a machine learning model; the classical module is configured to create a classical computing layer for creating a machine learning model, an abstract class layer for encapsulating the quantum computing layer and the classical computing layer, a machine learning model training layer for training and optimizing the machine learning model; The method comprises: calling the data structure module to obtain input training data, creating tensor data comprising the training data, and calling the quantum module and / or the classical module to create a machine learning model; inputting the tensor data into the machine learning model, and calling the classical module to create a training layer of the machine learning model; inputting the output result of the machine learning model into the training layer to update the parameters of the machine learning model, and obtaining the trained machine learning model.

2. The method of claim 1, wherein, The training layer comprises a loss function layer and an optimizer layer, and the classical module comprises: a loss function unit configured to calculate the loss function of the machine learning model; an optimizer unit configured to update the parameters of the machine learning model based on the loss function when training the machine learning model to optimize the machine learning model; The calling of the classical module to create the training layer of the machine learning model comprises: calling the loss function unit to create the loss function layer; calling the optimizer unit to create the optimizer layer.

3. The method of claim 2, wherein, The inputting of the output result of the machine learning model into the training layer to update the parameters of the machine learning model, and obtaining the trained machine learning model, comprises: inputting the output result of the machine learning model into the loss function layer to calculate the value of the loss function of the machine learning model; when it is determined that the value of the loss function does not satisfy a preset condition, inputting the value of the loss function into the optimizer layer to update the parameters of the machine learning model based on the value of the loss function; determining the value of the loss function of the machine learning model after updating the parameters; when it is determined that the value of the loss function satisfies a preset condition, regarding the machine learning model after updating the parameters as the trained machine learning model.

4. The method of claim 3, wherein, The updating of the parameters of the machine learning model based on the value of the loss function comprises: calculating the gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function; updating the parameters of the machine learning model based on the gradient and a gradient descent algorithm.

5. The method of claim 4, wherein, If the quantum module is called to create a machine learning model, the machine learning model comprises a quantum computing layer created based on the quantum module, and the calculating of the gradient of the loss function with respect to the parameters of the machine learning model based on the value of the loss function comprises: calculating a first gradient of the loss function with respect to an output result of the quantum computing layer based on the value of the loss function, and calculating a second gradient of the output result of the quantum computing layer with respect to a parameter of the quantum computing layer; multiplying the first gradient and the second gradient to obtain a gradient of the loss function with respect to a parameter of the machine learning model.

6. The method of claim 5, wherein, The calculating the second gradient of the output result of the quantum computing layer with respect to the parameter of the quantum computing layer comprises: calculating a first output result of the quantum computing layer after adding a first bias quantity to the parameter of the quantum computing layer, and calculating a second output result of the quantum computing layer after subtracting a second bias quantity from the parameter of the quantum computing layer; calculating the second gradient of the output result of the quantum computing layer with respect to the parameter of the quantum computing layer based on a difference between the first output result and the second output result.

7. The method of claim 4, wherein, After the inputting the tensor data into the machine learning model and the calling the classical module to create the training layer of the machine learning model, the method further comprises: taking the loss function, the parameter of the machine learning model, and an output result of each sub-step in the process of calculating the loss function based on the parameter as a computing node, and taking a relationship among the loss function, the parameter, and the output result of the sub-step as an edge to construct a directed computation graph; The calculating the gradient of the loss function with respect to the parameter of the machine learning model based on the value of the loss function comprises: calculating the gradient of the loss function with respect to the parameter of the machine learning model based on the value of the loss function and the computation graph.

8. The method of claim 7, wherein, The calculating the gradient of the loss function with respect to the parameter of the machine learning model based on the value of the loss function and the computation graph comprises: determining a path from a computing node corresponding to the loss function to a computing node corresponding to the parameter of the machine learning model in the computation graph; calculating an intermediate gradient of each computing node of a non-leaf node on the path with respect to a predecessor node of the computing node based on the value of the loss function; multiplying all the intermediate gradients to obtain the gradient of the loss function with respect to the parameter. 9.A model training apparatus based on a machine learning framework, characterized in that, The machine learning framework comprises a data structure module, a quantum module, and a classical module, the data structure module is configured to create tensor data for inputting a machine learning model, and perform operations on the tensor data; the quantum module is configured to create a quantum computing layer for creating a machine learning model; The classical module is configured to create a classical computing layer for creating a machine learning model, an abstract class layer for encapsulating the quantum computing layer and the classical computing layer, a machine learning model training layer for training and optimizing the machine learning model, and the device comprises: a creating module configured to call the data structure module to obtain input training data, create tensor data comprising the training data, and call the quantum module and / or the classical module to create a machine learning model; An input training module is configured to input the tensor data into the machine learning model and call the classical module to create a training layer of the machine learning model. A parameter updating module is configured to input an output result of the machine learning model into the training layer and update parameters of the machine learning model based on an output result of the training layer to obtain a trained machine learning model.

10. A storage medium, characterized by The storage medium stores a computer program, and the computer program is configured to execute the method in any one of claims 1 to 8 when running. 11.An electronic device comprising a memory and a processor, the electronic device characterized by, The memory stores a computer program, and the processor is configured to execute the computer program to execute the method in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Quantum machine learning framework construction method and device and quantum computer

    CN109800883A

  • Model training method for quantum data classification and quantum data classification method

    CN113379059A