WO2021053381A1 - 神经网络模型的压缩与加速方法、数据处理方法及装置 - Google Patents

神经网络模型的压缩与加速方法、数据处理方法及装置 Download PDF

Info

Publication number
WO2021053381A1
WO2021053381A1 PCT/IB2019/059565 IB2019059565W WO2021053381A1 WO 2021053381 A1 WO2021053381 A1 WO 2021053381A1 IB 2019059565 W IB2019059565 W IB 2019059565W WO 2021053381 A1 WO2021053381 A1 WO 2021053381A1
Authority
WO
WIPO (PCT)
Prior art keywords
linear layer
layer
quantization
parameter
compression
Prior art date
Application number
PCT/IB2019/059565
Other languages
English (en)
French (fr)
Inventor
金庆
廖震宇
杨林杰
Original Assignee
字节跳动有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 字节跳动有限公司 filed Critical 字节跳动有限公司
Publication of WO2021053381A1 publication Critical patent/WO2021053381A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/04Architecture, e.g. interconnection topology
    • G06N3/045Combinations of networks
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/08Learning methods

Definitions

  • Artificial Intelligence is a theory, method, technology, and application system that uses a digital computer or a machine controlled by a digital computer to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use knowledge to obtain the best results.
  • artificial intelligence is a comprehensive technology of computer science, which attempts to understand the essence of intelligence and produce a new kind of intelligent machine that can react in a similar way to human intelligence.
  • Artificial intelligence is to study the design principles and implementation methods of various intelligent machines, so that the machines have the functions of perception, reasoning and decision-making.
  • At least one embodiment of the present disclosure provides a compression and acceleration method of a neural network model
  • the neural network model includes a linear layer
  • the parameters of the neural network model include preliminary weight parameters
  • the compression and acceleration method includes: Quantizing the parameters of the neural network model to obtain a quantization model, where the parameters of the quantization model include the quantization weight parameter of the linear layer; and performing scale transformation processing on the quantization model to obtain a target quantization model;
  • performing the scale transformation processing on the quantization model includes: calculating the scale transformation parameter of the linear layer based on the number of output neurons of the linear layer or the standard deviation of the preliminary weight parameter of the linear layer And based on the scale transformation parameter of the linear layer, performing the scale transformation processing on the quantization weight parameter of the linear layer to obtain the standard quantization weight parameter of the linear layer.
  • the linear layer package It includes at least one selected from the group consisting of a convolutional layer, a recursive layer, and a fully connected layer.
  • the linear layer is not directly followed by the batch normalization layer.
  • quantizing the parameters of the neural network model to obtain the quantized model includes: performing clamping processing on the preliminary weight parameters of the linear layer , To obtain the clamp weight parameter of the linear layer; and perform quantization processing on the clamp weight parameter of the linear layer to obtain the quantized weight parameter of the linear layer.
  • calculating the scale transformation parameters of the linear layer based on the number of output neurons of the linear layer includes: calculating the scale transformation parameters according to the first scale transformation parameter calculation formula
  • the first scale transformation parameter calculation formula is expressed as: Wherein, RSF represents the scale transformation parameter of the linear layer, A represents the number of output neurons of the linear layer, Q represents the quantization weight matrix of the linear layer, and VAF ⁇ Q represents the quantization weight of the linear layer
  • RSF represents the scale transformation parameter of the linear layer
  • A represents the number of output neurons of the linear layer
  • Q represents the quantization weight matrix of the linear layer
  • VAF ⁇ Q represents the quantization weight of the linear layer
  • the number of bits of the quantization weight parameter of the linear layer is 1-8.
  • the number of bits of the quantization weight parameter of the linear layer is 1-2.
  • calculating the scale transformation parameters of the linear layer based on the number of output neurons of the linear layer includes: calculating the scale transformation parameters according to a second scale transformation parameter calculation formula
  • the second scaling parameter calculation formula is expressed as:
  • RSF represents the scale transformation parameter of the linear layer
  • W represents the auxiliary weight moment of the linear layer
  • the auxiliary weight matrix of the linear layer is expressed as:
  • calculating the scale transformation parameter of the linear layer based on the standard deviation of the preliminary weight parameter of the linear layer includes: according to a third scale transformation parameter calculation formula Calculate the scale transformation parameters of the linear layer, and the third scale transformation parameter calculation formula is expressed as: Wherein, RSF represents the scale transformation parameter of the linear layer, W represents the preliminary weight matrix of the linear layer, and VAR V ⁇ j (represents the auxiliary of the linear layer The variance of the elements; the auxiliary weight matrix of the linear layer is expressed as:
  • W 2W-1 where W represents the clamp weight matrix of the linear layer.
  • the number of bits of the quantization weight parameter of the linear layer is 3-8.
  • the quantization weight parameter of the linear layer is subjected to the scale transformation process based on the scale transformation parameter of the linear layer to obtain the linear layer
  • performing the clamping process on the preliminary weight parameter of the linear layer to obtain the clamping weight parameter of the linear layer includes:
  • the bit formula performs the clamp processing on the preliminary weight parameter of the linear layer, and the clamp formula is expressed as: Wherein, W represents the clamp weight matrix of the linear layer,% represents the parameter in the i-th row and j-th column of the clamp weight matrix, W represents the preliminary weight matrix of the linear layer, and% represents the The parameter in row i and column j of the preliminary weight matrix of the linear layer, ⁇ represents the parameter in row m and column n of the preliminary weight matrix of the linear layer, t anh(.) represents the hyperbolic tangent function, max (.) represents the maximum value function.
  • performing the quantization process on the clamp weight parameter of the linear layer to obtain the quantization weight parameter of the linear layer includes: according to a quantization formula
  • the quantization process is performed on the clamp weight parameter of the linear layer, and the quantization formula is expressed as:
  • the compression and acceleration method further includes: training the target quantization model by using the same training parameter configuration as the neural network model.
  • the training process of the target quantization model includes: a forward propagation phase, a backward propagation phase, and a standard quantization phase;
  • the forward propagation phase includes: using current The target quantization model of the following processes the training input data to obtain training output data, and calculates a loss value based on the training output data;
  • the backward propagation stage includes: calculating a gradient based on the loss value, and based on the gradient Modify the parameters of the current neural network model to obtain an updated neural network model;
  • the standard quantization stage includes: quantizing the parameters of the updated neural network model to obtain an updated quantized model, and
  • the quantization model performs scale transformation processing to obtain an updated target quantization model.
  • the backward propagation stage further includes: calculating an activation value gradient according to an activation value gradient formula, and correcting the current activation value parameter based on the activation value gradient , To obtain the updated activation value parameter, the activation value
  • the training parameter configuration includes: initial learning rate, learning rate adjustment scheme, weight attenuation, number of iterations of the training set, optimizer, and batch size.
  • the compression and acceleration method before quantizing the parameters of the neural network model, further includes: pre-training the neural network model to obtain The preliminary weight parameter of the neural network model.
  • performing the pre-training on the neural network model includes: initializing the parameters of the neural network model by using a Kaiming initialization scheme.
  • the neural network model includes one of ResNet, MobileNet-V1, MobileNet-V2, and YGG-Net.
  • At least one embodiment of the present disclosure further provides a data processing method, including: adopting the present disclosure
  • the target quantization model obtained by the compression and acceleration method provided in any embodiment processes the input data.
  • At least one embodiment of the present disclosure further provides a data processing device, including: a memory for non-transitory storage of computer-readable instructions; and a processor for running computer-readable instructions; wherein, the computer-readable instructions are The processor executes the compression and acceleration method provided by any embodiment of the present disclosure or executes the data processing method provided by any embodiment of the present disclosure while running.
  • At least one embodiment of the present disclosure further provides a storage medium for non-transitory storage of computer-readable instructions, wherein when the non-transitory computer-readable instructions are executed by a computer, the compression provided by any embodiment of the present disclosure can be executed.
  • the instruction of the acceleration method or the instruction of the data processing method provided by any embodiment of the present disclosure can be executed.
  • FIG. 1 is a schematic diagram of a convolutional neural network
  • Figure 2A is a schematic structural diagram of a convolutional neural network
  • Figure 2B is a schematic diagram of the working process of a convolutional neural network
  • Figure 3 is a schematic diagram of another convolutional neural network Schematic diagram of the structure
  • FIG. 4 is a flowchart of a method for compressing and accelerating a neural network model provided by at least one embodiment of the present disclosure
  • FIG. 5 is a method provided by at least one embodiment of the present disclosure corresponding to the steps shown in FIG. 4
  • FIG. 6 is another exemplary flowchart corresponding to step S100 shown in FIG. 4 provided by at least one embodiment of the present disclosure
  • neural network models that apply deep learning usually face the problem of excessive model size.
  • the file size of neural network models generally ranges from tens of megabytes to hundreds of megabytes.
  • the traffic consumed during downloading and the long transmission waiting time caused by the impact of bandwidth are unbearable for users; especially for some embedded systems with limited storage space, it may not be enough at all. Storage space to store such a large neural network model file.
  • neural network models have high requirements for computing resources and computing capabilities; when using large-scale neural network models for calculations, mobile terminals and embedded systems may not be able to provide the computing resources they need, or the calculations are slow, resulting in response delays Too high to meet actual application scenarios.
  • the neural network model consumes a lot of power.
  • the processor needs to read the parameters of the neural network model frequently, so a larger neural network model also brings a higher number of memory accesses, and frequent memory accesses will also greatly increase power consumption , High power consumption is not conducive to deploying the neural network model on the mobile terminal. Therefore, in order to deploy a neural network with good performance on a hardware device with limited resources, it is necessary to compress and accelerate the neural network model.
  • At least one embodiment of the present disclosure provides a compression and acceleration method of a neural network model.
  • the neural network model includes a linear layer, and the parameters of the neural network model include preliminary weight parameters;
  • the compression and acceleration method includes: quantizing the parameters of the neural network model to obtain a quantized model, and the parameters of the quantized model include the quantized weight of the linear layer Value parameter; and, performing scale transformation processing on the quantization model to obtain the target quantization model.
  • performing scale transformation processing on the quantization model includes: calculating the scale transformation parameter of the linear layer based on the number of output neurons of the linear layer or the standard deviation of the preliminary weight parameter of the linear layer; and, the scale transformation parameter based on the linear layer , Perform scale transformation processing on the quantized weight parameter of the linear layer to obtain the standard quantized weight parameter of the linear layer.
  • Some embodiments of the present disclosure also provide data processing methods, devices, and storage media corresponding to the aforementioned compression and acceleration methods.
  • the method for compressing and accelerating the neural network model provided by the embodiments of the present disclosure obtains the target quantization model by performing scale transformation processing on the quantization model, which can improve the accuracy of the target quantization model and improve the performance of the target quantization model.
  • CNN Convolutional Neural Network
  • Figure 1 shows a schematic diagram of a convolutional neural network.
  • the convolutional neural network can be used for image processing, which uses images as input and output, and replaces scalar weights with convolution kernels.
  • the convolutional neural network includes an input layer 101, a hidden layer 102 and an output layer 103.
  • the input layer 101 has 4 inputs
  • the hidden layer 102 has 3 outputs
  • the output layer 103 has 2 outputs.
  • the convolutional neural network finally outputs 2 images.
  • the 4 inputs of the input layer 101 may be 4 images, or 4 feature images of 1 image.
  • the three outputs of the hidden layer 102 may be characteristic images of the image input through the input layer 101.
  • the convolutional layer The core, bias ⁇ is a scalar superimposed on the output of the convolutional layer, where k is the label of the input layer 101, and i and j are the labels of the unit of the input layer 101 and the unit of the hidden layer 102, respectively.
  • the first convolutional layer 201 includes a first set of convolution kernels ( ⁇ in FIG. 1) and a first set of biases (in FIG. 1).
  • the second convolutional layer 202 includes a second set of convolution kernels ( ⁇ in Figure 1) and a second set of biases (b in Figure 1).
  • each convolutional layer includes dozens or hundreds of convolutions.
  • the convolutional neural network may include at least five convolutional layers.
  • the convolutional neural network further includes a first activation layer 203 and a second activation layer 204.
  • the first activation layer 203 is located behind the first convolutional layer 201
  • the second activation layer 204 is located behind the second convolutional layer 202.
  • the activation layer (for example, the first activation layer 203 and the second activation layer 204) includes activation functions, which are used to introduce nonlinear factors to the convolutional neural network, so that the convolutional neural network can better solve more complex problems .
  • the activation function may include a linear correction unit (ReLU) function, a sigmoid function (Sigmoid function), or a hyperbolic tangent function (tanh function), etc.
  • the ReLU function is an unsaturated nonlinear function
  • the Sigmoid function and tanh function are saturated nonlinear functions.
  • the activation layer can be used as a layer of the convolutional neural network alone, or the activation layer can also be included in the convolutional layer (for example, the first convolutional layer 201 can include the first activation layer 203, and the second convolutional layer 202 can be Including the second active layer 204).
  • the first convolutional layer 201 first, several convolution kernels in the first group of convolution kernels and several offsets in the first group of biases are applied to each input to obtain the first convolution The output of the layer 201; then, the output of the first convolutional layer 201 can be processed by the first activation layer 203 to obtain the output of the first activation layer 203.
  • the second convolutional layer 202 In the second convolutional layer 202, first of all, the first input The output of the activation layer 203 applies several convolution kernels in the second group of convolution kernels and several offsets in the second group of biases to obtain the output of the second convolutional layer 202; then, the second convolutional layer The output of 202 can be processed by the second activation layer 204 to obtain the output of the second activation layer 204.
  • the output of the first convolutional layer 201 may be the result of applying a convolution kernel ⁇ to its input and then adding the bias
  • the output of the second convolutional layer 202 may be applying a convolution to the output of the first activation layer 203.
  • Fig. 2A shows a schematic diagram of the structure of a convolutional neural network
  • Fig. 2B shows a schematic diagram of the working process of a convolutional neural network.
  • FIGS. 2A and 2B after the input image is input to the convolutional neural network through the input layer, it goes through several processing procedures (each level in FIG. 2A) in turn, and then outputs the category identification.
  • the main components of a convolutional neural network can include multiple convolutional layers, multiple down-sampling layers, and fully connected layers.
  • a complete convolutional neural network can be composed of these three layers.
  • FIG. 2A only shows three levels of a convolutional neural network, that is, the first level, the second level, and the third level.
  • each level may include a convolution module and a downsampling layer.
  • each convolution module may include a convolution layer. Therefore, the processing process of each level may include: convolution (convolution) and down-sampling (sub-sampling/down-sampling) of the input image.
  • each convolution module may further include a batch normalization (batch normalization) layer, so that the processing process of each level may also include batch normalization processing.
  • the batch normalization layer is used to perform batch normalization processing on the feature image, so that the gray value of the pixel of the feature image changes within a predetermined range, thereby reducing calculation difficulty and improving contrast.
  • the predetermined range can be [-1, 1].
  • the processing method of the batch standardization layer can refer to the common batch standardization process, which will not be repeated here.
  • the convolutional layer is the core layer of the convolutional neural network. In the convolutional layer of a convolutional neural network, a neuron is only connected to some of the neurons in the adjacent layer.
  • the convolutional layer can apply several convolution kernels (also called filters) to the input image to extract multiple types of features of the input image.
  • Each convolution kernel can extract one type of feature.
  • the convolution kernel is generally initialized in the form of a random decimal matrix. During the training of the convolutional neural network, the convolution kernel will learn to obtain reasonable weights. Pair input
  • the result obtained after applying a convolution kernel to an image is called a feature map, and the number of feature images is equal to the number of convolution kernels.
  • Each feature image is composed of some neurons arranged in rectangles.
  • the neurons of the same feature image share weights.
  • the shared weights here are the feature images output by the convolutional layer of one level of the convolution kernel and can be input to the adjacent lower.
  • a level of convolutional layer is processed again to obtain a new feature image.
  • the first-level convolutional layer may output a first feature image, which is input to the second-level convolutional layer and processed again to obtain a second feature image.
  • the convolutional layer can use different convolution kernels to convolve the data of a certain local receptive field of the input image, and the convolution result is input to the activation layer, which is calculated according to the corresponding activation function To get the characteristic information of the input image. For example, as shown in FIGS.
  • the down-sampling layer is arranged between adjacent convolutional layers, and the down-sampling layer is a form of down-sampling.
  • the down-sampling layer can be used to reduce the scale of the input image, simplify the calculation complexity, and reduce the phenomenon of over-fitting to a certain extent; on the other hand, the down-sampling layer can also perform feature compression to extract the input image Main features.
  • the down-sampling layer can reduce the size of feature images, but does not change the number of feature images.
  • FIG. 3 shows a schematic diagram of another convolutional neural network.
  • the output of the last convolutional layer that is, the t-th convolutional layer
  • the flattening layer can convert feature images (2D images) into vectors (1D).
  • the flattening operation can be performed as follows:
  • the output of the planarization layer (ie 1D vector) is input to a fully connected layer (FCN).
  • the fully connected layer can have the same structure as the convolutional neural network, but the difference is that the fully connected layer uses a different scalar value to replace the convolution kernel.
  • the output of the last convolutional layer can also be input to the Averaging Layer (AVG).
  • AVG Averaging Layer
  • the averaging layer is used to average the output, that is, the average value of the feature image is used to represent the output image. Therefore, a 2D feature image is converted into a scalar.
  • the convolutional neural network includes both The planarization layer.
  • the homogenization layer or the fully connected layer can be connected to the classifier, the classifier can classify according to the extracted features, and the output of the classifier can be used as the final output of the convolutional neural network, that is, the category identifier that characterizes the image category (label ).
  • the classifier may be a support vector machine (Support Vector Machine, SVM) classifier, a softmax classifier, a nearest neighbor rule (KNN) classifier, etc.
  • SVM Support Vector Machine
  • KNN nearest neighbor rule
  • the convolutional neural network includes a softmax classifier.
  • the softmax classifier is a generator of logic functions that can compress a K-dimensional vector z containing any real number into a K-dimensional vector cr( z).
  • the formula of the softmax classifier is as follows: Among them, Z j. represents the j-th element in the K-dimensional vector z, cr(z) represents the predicted probability of each category label (label), (2) is a real number, and its range is (0, 1), and the K-dimensional vector (2) The sum is 1. According to the above formula, each category identifier in the K-dimensional vector z is assigned a certain prediction probability, and the category identifier with the largest prediction probability is selected as the identifier or category of the input image.
  • FIG. 4 is a flowchart of a method for compressing and accelerating a neural network model provided by at least one embodiment of the present disclosure.
  • the compression and acceleration method can be used to quantify various neural network models such as ResNet (eg, ResNet-50), MobileNet-Vl, MobileNet-V2, and YGG-Net, so as to realize the compression of the various neural network models mentioned above.
  • ResNet eg, ResNet-50
  • MobileNet-Vl e.g, MobileNet-V2
  • YGG-Net YGG-Net
  • the scope of application of the compression and acceleration method includes but is not limited to the various neural network models listed above.
  • the compression and acceleration method includes steps S000 to S300.
  • Step S000 Perform pre-training on the neural network model to obtain preliminary weight parameters of the neural network model.
  • the neural network model may be an untrained full-precision model.
  • conventional training methods, training techniques (tricks), and training parameter (for example, including hyperparameters) configuration can be used to pre-train the full-precision model.
  • the training parameter configuration usually includes: the initial learning rate, the learning rate scheduler, the weight decay, the number of epochs of the training set, the optimizer ( optimizer) and batch size (batch size) and so on.
  • the initial learning rate can be set to 0.05
  • the learning rate adjustment scheme can be a cosine annealing scheduler
  • the weight attenuation can be set to 4 x 10 5
  • the number of iterations of the training set can be set to For 150 times
  • the optimizer can use a stochastic gradient descent (SGD) optimizer
  • the batch size can be set to 2048 or 1024.
  • SGD stochastic gradient descent
  • the pre-training process of a neural network model usually includes: initializing the parameters of the neural network model; using the neural network model to process the training input data to obtain the training output data; based on the training output data, calculating the loss value through the loss function; The loss value calculates the gradient and corrects the parameters of the neural network model.
  • a Kaiming Initialization scheme may be used to initialize the parameters of the neural network model.
  • the parameters of the neural network model can be initialized to random numbers conforming to the Gaussian distribution.
  • the initial weight parameters of each functional layer (for example, convolutional layer, fully connected layer, etc.) of the neural network model can be made to conform to the Gaussian distribution.
  • the expectation of the Gaussian distribution is 0, and the standard deviation of the Gaussian distribution is the The reciprocal of the number of output neurons of the functional layer.
  • the number of output neurons of the convolutional layer is equal to the product of the number of output channels of the convolutional layer and the number of elements in the convolution kernel of the convolutional layer; for example, for a full
  • the number of output neurons of the fully connected layer is equal to the number of features output by the fully connected layer.
  • the type of training input data depends on the processing object of the neural network model.
  • the training input data may include images, text, voice, and the like.
  • the training input data may be images, for example, images in the ImageNet database may be used as the training input data.
  • the loss function can be selected according to actual needs.
  • the loss function can include, but is not limited to, a 0-1 loss function, a square loss function, a logarithmic loss function, and a cross-entropy cost function (cross-entropy cost function). ), etc., or any combination thereof, the embodiments of the present disclosure do not limit this.
  • a stochastic gradient descent algorithm or a batch gradient descent (batch gradient descent, BGD) algorithm may be used to calculate the gradient, and the neural network may be adjusted according to the gradient.
  • the parameters of the network model are revised.
  • the pre-training process of the neural network model may further include: judging whether the training of the neural network model satisfies a predetermined condition, if the predetermined condition is not met, repeat the training; if the predetermined condition is met, stop the training It is trained to obtain a trained neural network model.
  • the foregoing predetermined condition is that the loss value corresponding to the training input data is no longer significantly reduced; for example, in another example, the foregoing predetermined condition is that the number of training times or training periods of the neural network model reaches a predetermined number; The disclosed embodiment does not limit this.
  • the above description is only a schematic illustration of the training process of the neural network model. Those skilled in the art should know that in the training process, a large amount of sample data needs to be used to train the neural network model; at the same time, the training process of each sample data may include multiple iterations to determine the parameters of the neural network model. Make corrections.
  • the training phase also includes fine-tune the parameters of the neural network model to obtain more optimized parameters.
  • the neural network model includes a linear layer.
  • the linear layer includes at least one of a convolution layer, a recurrent layer, and a fully-connected layer.
  • the neural network model further includes a nonlinear layer.
  • the nonlinear layer includes a batch normalization layer and an activation layer (for example, a nonlinear activation function).
  • the parameters of the neural network model are the preliminary weight parameters.
  • the preliminary weight parameter is a full-precision 32-bit floating point number. It should be noted that in some examples, the compression and acceleration method provided by the embodiments of the present disclosure may not include step S000.
  • steps S100 to S300 may be performed directly based on a neural network model that has been trained in the field.
  • the parameters of the trained neural network model are the preliminary weight parameters.
  • Step S100 quantify the parameters of the neural network model to obtain a quantified model.
  • the DoReFa scheme may be used to quantify the parameters of the neural network model.
  • quantizing the parameters of the neural network model refers to changing at least part of the parameters of the neural network model from, for example, high-precision floating-point numbers (for example, full-precision 32-bit floating-point numbers) to, for example, low-precision fixed-point numbers (for example, , 1-8 digit fixed-point number), so as to realize the compression and acceleration of the neural network model.
  • high-precision floating-point numbers for example, full-precision 32-bit floating-point numbers
  • low-precision fixed-point numbers for example, 1-8 digit fixed-point number
  • FIG. 5 is an exemplary flowchart corresponding to step S100 shown in FIG. 4 provided by at least one embodiment of the present disclosure.
  • step S100 includes step S110 to step S120.
  • Step S110 Perform clamping processing on the preliminary weight parameter of the linear layer to obtain the clamp weight parameter of the linear layer.
  • clamping processing refers to scaling a set of parameters (for example, the preliminary weight parameters of the linear layer) according to certain rules (for example, according to a certain formula), so that the value range of the scaled parameters is limited to A certain interval for further processing.
  • the preliminary weight parameter of the linear layer may be clamped according to the clamping formula, so as to limit the value range of the clamp weight parameter of the linear layer to a predetermined interval, for example, the predetermined interval may be [ 0, 1], but not limited to this.
  • the parameters of the linear layer can be more uniformly distributed in the predetermined interval, thereby helping to reduce the quantization error in the subsequent steps.
  • the clamping formula can be expressed as: Among them, W represents the clamp weight matrix of the linear layer (including the clamp weight parameters of the linear layer),
  • Step S120 Perform quantization processing on the clamp weight parameter of the linear layer to obtain the quantized weight parameter of the linear layer.
  • the clamp weight parameter of the linear layer may be adjusted according to the weight quantization formula.
  • the weight quantification formula can be expressed as:
  • Q j - ⁇ r ound( (2 ⁇ -1)V ⁇ )-1 2 1
  • Q represents the quantization weight matrix of the linear layer (including the quantization weight parameters of the linear layer)
  • Q j represents the linear layer
  • the parameters of the quantization model include the quantization weight parameters of the linear layer.
  • the number of bits b of the quantization weight parameter of the linear layer is generally set to 1-8 bits (bit).
  • FIG. 6 is another exemplary flowchart corresponding to step S100 shown in FIG. 4 provided by at least one embodiment of the present disclosure.
  • Step S100 shown in FIG. 6 includes step S130 in addition to step S110 and step S120 shown in FIG. 5.
  • the neural network model includes an activation layer.
  • the activation layer may include the PACT activation function, but is not limited thereto.
  • the PACT activation function is expressed as: Wherein, x represents the output of the activation layer, x represents the input of the activation layer, and a represents the activation value parameter of the PACT activation function. For example, a is a floating number.
  • the PACT activation function can reduce the quantization error of the output of the activation layer.
  • the parameters of the neural network model are quantified to obtain the quantized model, that is, step S100, which further includes step S130.
  • Step S130 Perform quantization processing on the output of the activation layer.
  • the output of the activation layer may be quantized according to the activation value quantization formula.
  • the activation value quantization formula can be expressed as: q Among them, q represents the quantized value of the output of the active layer, a represents the number of bits of the quantized value of the output of the active layer, and r ound (i) represents a rounding function.
  • q is a dynamic fixed-point number; for example, the bit position of the quantized value of the output of the activation layer
  • the number of digits a is generally set to, for example, 1-8 digits, such as 2-4 digits, etc.
  • performing quantization processing on the output of the activation layer is beneficial to increase the calculation speed of the quantization model, thereby helping to realize the acceleration function of the compression and acceleration method provided by the embodiment of the present disclosure.
  • the batch normalization layer in the neural network model may not be quantified, or the bias of the last fully connected layer in the neural network model may not be quantified. .
  • the quantized model obtained according to step S100 usually has problems of reduced accuracy and performance degradation; on the other hand, in the neural network model or/and the quantized model, if the weight is maintained The gradient of is at the same scale level, which can prevent the problems of gradient explosion and gradient disappearance, thereby helping to improve the accuracy of the quantization model and improve the performance of the quantization model.
  • the batch normalization layer can be directly connected after the linear layer (the output of the linear layer is processed by the batch normalization layer and then input to the subsequent
  • the compression and acceleration method provided by the embodiment of the present disclosure further includes step S200 after step S100 to further process the quantization model.
  • the target quantization model obtained in step S200 may have higher accuracy and better performance.
  • the same efficiency constraint means that the size of the model (corresponding to the storage space occupied by the model), energy consumption, and latency (corresponding to the processing speed of the model) are basically the same.
  • the performance of the target quantization model obtained in step S200 may be equivalent to or better than the performance of the corresponding full-precision model (refer to the subsequent Table 1-2).
  • FIG. 7 is an exemplary flowchart corresponding to step S200 shown in FIG. 4 provided by at least one embodiment of the present disclosure. For example, as shown in FIG.
  • step S200 includes step S210 to step S220.
  • step S210 Calculate the scale transformation parameter of the linear layer based on the number of output neurons of the linear layer or the standard deviation of the preliminary weight parameter of the linear layer. For example, in some examples, calculating the scaling parameter of the linear layer based on the number of output neurons of the linear layer includes: calculating the scaling parameter of the linear layer according to the first scaling parameter calculation formula.
  • the calculation formula of the first scale transformation parameter is expressed as: Among them, RSF represents the scaling parameter of the linear layer, A represents the number of output neurons of the linear layer, Q represents the quantization weight matrix of the linear layer (including the quantization weight parameters of the linear layer), and VAR Q represents the quantization weight of the linear layer.
  • the variance of the elements of the value matrix For example, in some examples, when the number of bits of the quantization weight parameter of the linear layer is 1-2 bits, it is compared with the linear layer scale transformation parameter RSF calculated by using the subsequent two scale transformation parameter calculation formulas.
  • the scale transformation parameter RSF of the linear layer calculated by using the first scale transformation parameter calculation formula can make the target quantization model converge faster.
  • the first scale transformation parameter calculation formula may still be used layer linear scale transformation parameters calculated RSF o
  • calculated parameters of the linear scaling layer comprising: calculating a linear scaling layer according to a second scale parameter calculated Transformation parameters.
  • the calculation formula of the second scale transformation parameter is expressed as: Among them, RSF represents the number of output neurons of the linear layer of the scaling transformation of the linear layer, W represents the auxiliary weight matrix of the linear layer, and the variance of the elements of the auxiliary weight matrix of the linear layer. Auxiliary weight moment of linear layer
  • auxiliary weight matrix W of the linear layer is to illustrate that the second scale transformation parameter meter does not include the auxiliary weight of the linear layer in the neural network model and its quantization model.
  • calculating the scale transformation parameter of the linear layer based on the standard deviation of the preliminary weight parameter of the linear layer includes: calculating the linearity according to the third scale transformation parameter calculation formula The scale transformation parameter of the layer.
  • the calculation formula of the third scale transformation parameter is expressed as: Among them, W represents the clamp weight matrix of the linear layer.
  • the auxiliary weight matrix W of the linear layer is introduced to illustrate the calculation formula of the third scale transformation parameter, and the auxiliary weight of the linear layer is not included in the neural network model and its quantization model.
  • Matrix k It should be noted that, in some examples, the target quantization model obtained by the linear layer scaling parameter RSF calculated based on the first scaling parameter calculation formula, and the linear layer scaling calculated based on the second scaling parameter calculation formula The target quantization model obtained by the parameter RSF and the target quantization model obtained by the linear layer scaling parameter RSF calculated based on the third scaling parameter calculation formula have basically the same accuracy and performance.
  • the first scale transformation parameter calculation formula, the second scale transformation parameter calculation formula, and the third scale transformation can be selected. Any one of the parameter calculation formulas calculates the scale transformation parameter RSF of the linear layer, and at the same time, the accuracy and performance of the obtained target quantization model are basically equivalent. It should be noted that, in at least one embodiment of the present disclosure, when the number of bits of the quantization weight parameter of the linear layer is another value (for example, 1-2 bits), the second scale transformation parameter may still be used.
  • the quantized values of the parameters weight linear scaling layer is processed to obtain standard linear quantizing layer Weight parameter.
  • the quantization weight parameters of the linear layer for example, the linear layer that is not directly followed by the batch normalization layer
  • the gradient of the weight value of is at the same scale, which is beneficial to improve the accuracy of the quantization model and improve the performance of the quantization model.
  • the quantization weight parameter of the linear layer may be scaled according to the scale conversion formula.
  • Step S300 Use the same training parameter configuration as the neural network model to train the target quantization model.
  • step S300 the training parameter configuration of the neural network model can refer to the related description in step S000, which will not be repeated here.
  • FIG. 8 is an exemplary flowchart corresponding to step S300 shown in FIG. 4 provided by at least one embodiment of the present disclosure.
  • the same training parameter configuration as the neural network model is used to train the target quantization model that is, step S300 includes: a forward propagation phase, a backward propagation phase, and a standard quantization phase, and repeated execution of these three Three stages of operation to obtain a trained target quantization model.
  • the forward propagation phase, the backward propagation phase, and the standard quantization phase respectively correspond to the following step S310, step S320, and step S330.
  • Step S310 Use the current target quantization model to process the training input data to obtain training output data, and calculate a loss value based on the training output data.
  • the operation of the forward propagation phase of the training process of the target quantization model may refer to the operation of the forward propagation phase of the neural network model (for example, the full-precision model) accordingly, which will not be repeated here.
  • Step S320 Calculate the gradient based on the loss value, and modify the parameters of the current neural network model based on the gradient to obtain an updated neural network model; for example, the operation of the backward propagation phase of the training process of the target quantization model, that is, step S320,
  • the operation of the backward propagation stage of the neural network model (for example, the full-precision model) can be referred to accordingly, and the details will not be repeated here.
  • step S320 the calculation may be performed according to the activation value gradient formula Activate the activation value gradient, and modify the current activation value parameter based on the activation value gradient to obtain the updated activation value parameter.
  • the activation value gradient formula can be expressed as: Wherein, ⁇ 7/3a represents the activation value gradient.
  • using the above activation value gradient formula to calculate the activation value gradient is beneficial to reduce the quantization error.
  • Step S330 quantify the parameters of the updated neural network model to obtain an updated quantized model, and perform scale transformation processing on the updated quantized model to obtain an updated target quantized model.
  • the operation of the standard quantization phase of the training process of the target quantization model that is, step S330, can refer to the relevant expressions of the aforementioned step S100 and step S200, and the details will not be repeated here.
  • training the target quantization model through the above steps S310 to S330 can improve the accuracy of the target quantization model and improve the performance of the target quantization model.
  • the parameters of the target quantization model (including the standard quantization weight parameters of the linear layer) are not directly updated, but the parameters of the neural network model are modified, and then Perform quantization and scale transformation processing, so as to achieve the update of the parameters of the target quantization model.
  • the scale transformation parameters of the linear layer are calculated with the standard deviation of the preliminary weight parameters based on the linear layer (that is, the scale transformation parameters of the computational layer are calculated using the third scale transformation parameter calculation formula or the second scale transformation parameter calculation formula)
  • the process of calculating the scaling parameters of the linear layer based on the number of output neurons of the linear layer that is, the scaling parameters of the computational layer using the first scaling parameter calculation formula or the second scaling parameter calculation formula
  • VAF ⁇ V ⁇ j( so the amount of calculation can be reduced, which helps to speed up the training speed of the target quantization model.
  • the target quantization model may not store the standard quantization weight parameters of the linear layer, but instead store the quantization weight parameters and scale transformation parameters of the linear layer, thereby reducing the size of the target quantization model (ie, occupying Storage space).
  • the standard quantization weight parameters of the linear layer can be calculated through the quantization weight parameters and scale transformation parameters of the linear layer, or the linear layer quantization weight parameters can be used to The input of the layer is processed to obtain the output of the linear layer, and then the scale transformation parameter is used to process the output of the linear layer, which is not limited in the embodiment of the present disclosure.
  • the target quantization model may not store the bias of the linear layer (such as the fully connected layer) in the target quantization model, but instead store the bias of the linear layer (such as the fully connected layer) in the quantization model; thus,
  • the offset of the linear layer in the quantization model can be converted into the offset of the linear layer in the target quantization model through the scale change parameter, or the linear layer in the quantization model can also be used first.
  • the quantization weight parameter and the offset of the linear layer in the quantization model process the input of the linear layer to obtain the output of the linear layer, and then use the scale transformation parameter to process the output of the linear layer, which is not limited in the embodiments of the present disclosure. .
  • the compression and acceleration methods provided by the embodiments of the present disclosure can selectively (for example, choose one of the two, or both) the weight parameters of the neural network model according to actual needs. (Ie weight quantization) and the output of the activation layer (ie activation value quantization) are quantified.
  • both the neural network model and its quantized model can be implemented by software, hardware, firmware, or any combination thereof, so as to execute corresponding processing procedures.
  • the flow of the compression and acceleration method of the neural network model may include more or fewer operations, and these operations may be executed sequentially or in parallel.
  • the compression and acceleration method of the neural network model described above may be executed once, or may be executed multiple times according to predetermined conditions.
  • the method for compressing and accelerating the neural network model provided by the embodiments of the present disclosure obtains the target quantization model by performing scale transformation processing on the quantization model, which can improve the accuracy of the target quantization model and improve the performance of the target quantization model.
  • At least one embodiment of the present disclosure also provides a data processing method.
  • the data processing method includes Including: The input data is processed using the target quantization model obtained by the compression and acceleration method provided in any embodiment of the present disclosure to obtain output data.
  • the type of input data depends on the processing object of the target quantization model.
  • the input data may include images, text, voice, and the like.
  • the input data can be images.
  • the output data may represent the result of inference and prediction of the target quantization model on the input data.
  • the output data can represent the classification result of the image (that is, the input data).
  • the target quantitative model can be deployed in mobile terminals and embedded systems such as smart phones, tablet computers, and car navigators, so that the mobile terminals and embedded systems can execute the aforementioned data processing methods.
  • Table 1-2 exemplarily shows the accuracy comparison of the quantization schemes under different bit widths.
  • Table 1 is a comparison table of the accuracy of quantization schemes under different bit widths (that is, the number of quantization bits) for MobileNet-Vl and MobileNet-Y2 (quantizing weights and activation values);
  • Table 2 is for MobileNet-Vl and The comparison table of the accuracy of the quantization schemes under different bit widths of MobileNet-Y2 (quantize the weights, and do not quantize the activation values). It should be noted that in Table 1-2, PACT (parameterized clipping activation),
  • HAQ hardware-aware automated quantization
  • Deep Compression Deep Compression are quantization schemes that have been ported.
  • SAT is the quantization scheme (ie compression and acceleration method) provided by the embodiments of the present disclosure.
  • the scaling parameters of the linear layer are based on the linear layer.
  • the number of output neurons is calculated (using the third scale transformation parameter calculation formula).
  • the bit width of the HAQ scheme is flexible, so the bit width of the HAQ scheme in Table 1-2 is the equivalent bit width.
  • the equivalent bit width is 2, 3, 4, 5, 6, 8, etc., so that the accuracy can be compared with other quantization schemes under the corresponding bit width.
  • FP represents the corresponding full-precision model
  • Acc.-l represents the probability that a candidate category output by the model is the correct category of the input image
  • Acc.-5 represents the five candidate categories output by the model Include the probability of the correct category of the input image.
  • the specific details of the PACT program can be found in the literature, Jungwook Choi, Zhuo Wang, Swagath Yenkataramani, Pierce I-Jen Chuang, Yijayalakshmi Srmivasan, and Kailash Gopalakrishnan.
  • PACT Parameterized Clipping Activation for Quantized Neural Networks, arXiv: 1805.06085, 2018;
  • the specific details of the HAQ scheme can be found in the literature, Kuan Wang, Zhijian Liu, Yujun Lin, Ji Lin, and Song Han.
  • HAQ Hardware -Aware Automated Quantization with Mixed Precision, arXiv: 1811.08886, 2019;
  • the specific details of the Deep Compression program can be found in the literature, Song Han, Huizi Mao, and William J Dally.
  • Deep Compression Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding . arXiv: 1510.00149, 2015.
  • the accuracy of the target quantization model obtained by the compression and acceleration method provided by the embodiment of the present disclosure is in most cases higher than that obtained by other known quantization schemes.
  • the quantization model which shows that the compression and acceleration method provided by the embodiments of the present disclosure can improve the accuracy of the target quantization model and improve the performance of the target quantization model.
  • the data processing method provided by the embodiment of the present disclosure reference may be made to the corresponding description of the compression and acceleration method of the neural network model in the foregoing embodiment, which will not be repeated here.
  • At least one embodiment of the present disclosure also provides a data processing device.
  • the data processing apparatus 500 includes a memory 510 and a processor 520 O e.g., non-transitory storage 510 for storing computer-readable instructions, the processor 520 for executing computer-readable instructions, the computer
  • the readable instructions are run by the processor 520, the neural network model compression and acceleration method or/and data processing method provided by any embodiment of the present disclosure is executed.
  • the memory 510 and the processor 520 may directly or indirectly communicate with each other.
  • the data processing apparatus 500 may further include a system bus 530, and the memory 510 and the processor 520 may communicate with each other through the system bus 530.
  • the processor 520 may communicate with each other through the system bus 530.
  • 1006 accesses the memory 510.
  • components such as the memory 510 and the processor 520 may communicate through a network connection.
  • the network may include a wireless network, a wired network, and/or any combination of a wireless network and a wired network.
  • the network may include a local area network, the Internet, a telecommunication network, the Internet of Things (Internet of Things) based on the Internet and/or a telecommunication network, and/or any combination of the above networks, etc.
  • the wired network may, for example, use twisted pair, coaxial cable, or optical fiber transmission for communication
  • the wireless network may use, for example, a 3G/4G/5G mobile communication network, Bluetooth, Zigbee, or WiFi.
  • the present disclosure does not limit the types and functions of the network here.
  • the processor 520 may control other components in the data processing apparatus to perform desired functions.
  • the processor 520 may be a central processing unit (CPU), a tensor processor (TPU), or a graphics processor GPU, and other devices with data processing capabilities and/or program execution capabilities.
  • the central processing unit (CPU) can be X86 or ARM architecture, etc.
  • the GPU can be directly integrated on the motherboard alone or built into the north bridge chip of the motherboard.
  • the GPU can also be built into the central processing unit (CPU).
  • the memory 510 may include any combination of one or more computer program products, and the computer program products may include various forms of computer-readable storage media, such as volatile memory and/or non-volatile memory.
  • Volatile memory may include random access memory (RAM) and/or cache memory (cache), for example.
  • the non-volatile memory may include, for example, read only memory (ROM), hard disk, erasable programmable read only memory (EPROM), portable compact disk read only memory (CD-ROM), USB memory, flash memory, and the like.
  • ROM read only memory
  • EPROM erasable programmable read only memory
  • CD-ROM portable compact disk read only memory
  • USB memory flash memory
  • flash memory flash memory
  • Various application programs and various data can also be stored in the computer-readable storage medium, such as the preliminary weight parameter of the linear layer, the standard quantization weight parameter of the linear layer, the scale transformation parameter of the linear layer, the activation value parameter, and the application program.
  • Various data used and/or generated, etc. when some computer instructions stored in the memory 510 are executed by the processor 520, one or more steps in the compression and acceleration method described above may be executed.
  • one or more steps in the data processing method described above may be executed.
  • the data processing apparatus 500 may further include an input interface 540 that allows an external device to communicate with the data processing apparatus 500.
  • the input interface 540 can be used to receive instructions from external computer devices, from users, and the like.
  • the data processing apparatus 500 may further include an output interface 550 that connects the data processing apparatus 500 and one or more external devices to each other.
  • the data processing device 500 may display images and the like through the output interface 550.
  • External devices that communicate with the data processing apparatus 500 through the input interface 1010 and the output interface 1012 may be included in an environment that provides any type of user interface with which a user can interact. Examples of user interface types include graphical user interfaces, natural user interfaces, and so on.
  • the graphical user interface can accept input from the user using input device(s) such as a keyboard, mouse, remote control, etc., and provide output on an output device such as a display.
  • a natural user interface may enable a user to interact with the data processing apparatus 500 in a manner that does not need to be subject to constraints imposed by input devices such as a keyboard, mouse, remote control, and the like.
  • natural user interfaces may rely on voice recognition, touch and stylus recognition, gesture recognition on and near the screen, aerial gestures, head and eye tracking, voice and voice, vision, touch, gestures, and machine intelligence.
  • the data processing device 500 is shown as a single system in FIG. 9, it can be understood that the data processing device 500 may also be a distributed system, and may also be arranged as a cloud facility (including Public cloud or private cloud).
  • the data processing device may include, but is not limited to, mobile terminals and embedded systems such as smart phones, tablet computers, and car navigators. It should be noted that the data processing device provided by the embodiments of the present disclosure is exemplary rather than restrictive.
  • the data processing device may also include other conventional components or structures, for example, to achieve data processing
  • those skilled in the art can set other conventional components or structures according to specific application scenarios, which are not limited in the embodiments of the present disclosure.
  • FIG. 10 is a schematic diagram of a storage medium provided by an embodiment of the present disclosure. For example, as shown in FIG.
  • the storage medium 600 non-transitory stores computer-readable instructions 601, and when the non-transitory computer-readable instructions 601 are executed by a computer (including a processor), any one of the embodiments of the present disclosure can be executed.
  • the instructions of the compression and acceleration method or the instructions of the data processing method provided by any embodiment of the present disclosure can be executed.
  • one or more computer instructions may be stored on the storage medium 600.
  • Some computer instructions stored on the storage medium 600 may be, for example, instructions for implementing one or more steps in the aforementioned compression and acceleration method.
  • Other computer instructions stored on the storage medium may be, for example, instructions for implementing one or more steps in the above-mentioned data processing method.
  • the storage medium may include the storage component of a tablet computer, the hard disk of a personal computer, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM), compact disk read-only memory (CD -ROM), flash memory, or any combination of the above storage media, can also be other suitable storage media.
  • RAM random access memory
  • ROM read-only memory
  • EPROM erasable programmable read-only memory
  • CD -ROM compact disk read-only memory

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • General Health & Medical Sciences (AREA)
  • Biomedical Technology (AREA)
  • Biophysics (AREA)
  • Computational Linguistics (AREA)
  • Life Sciences & Earth Sciences (AREA)
  • Evolutionary Computation (AREA)
  • Artificial Intelligence (AREA)
  • Molecular Biology (AREA)
  • Computing Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Software Systems (AREA)
  • Health & Medical Sciences (AREA)
  • Image Analysis (AREA)

Abstract

一种神经网络模型的压缩与加速方法、数据处理方法及装置、存储介质。神经网络模型包括线性层,神经网络模型的参数包括预备权值参数;压缩与加速方法包括:对神经网络模型的参数进行量化,以得到量化模型,量化模型的参数包括线性层的量化权值参数;以及,对量化模型进行尺度变换处理,以得到目标量化模型。对量化模型进行尺度变换处理,包括:基于线性层的输出神经元的数量或线性层的预备权值参数的标准差,计算线性层的尺度变换参数;以及,基于线性层的尺度变换参数,对线性层的量化权值参数进行尺度变换处理,以得到线性层的标准量化权值参数。

Description

神经网络 模型的压 缩与加速 方法、 数据处理方法 及装置 本申请 要求于 201 9年 9月 20 日递交、 题为“ 神经网络模型的压缩与 加速 方法、数据处 理方法及 装置” 的中国专利申请第 201 91 0893276. X号的 优先权 , 在此全文引用上 述中国 专利申请公 开的内容 以作为本 申请的一部 分 。 技术领 域 本公 开的实施例 涉及一种 神经网络模 型的压缩 与加速方 法、 数据处理 方 法及装置、 存储介质。 背景技 术 人工 智能 ( Artificial Intelligence, AI ) 是利用数字计算机或者数字计 算机 控制的机 器模拟、 延伸和扩展人的智 能, 感知环境、 获取知识并使 用 知识 获得最佳 结果的理论 、 方法、 技术及应用系统。 换句话说, 人工智能 是计 算机科学 的一个综 合技术, 它企图了解智能的实 质, 并生产出一种新 的 能以人类智 能相似的 方式做 出反应的智 能机器。 人工智能也 就是研 究各 种智 能机器的 设计原理 与实现方法 , 使机器具有感知、推理与 决策的功 能。 发明 内容 本公 开至少一个 实施例提 供一种神 经网络模型 的压缩与 加速方法 , 所 述神 经网络模 型包括线性 层,所述神经网络模 型的参数 包括预备 权值参数 , 所述 压缩与加 速方法包括 : 对所述神经网络模 型的参数 进行量化 , 以得到 量化 模型, 所述量化模 型的参数 包括所述 线性层的量 化权值参 数; 以及对 所述 量化模型 进行尺度 变换处理, 以得到目标量化模 型; 其中, 对所述量 化模 型进行所 述尺度 变换处理, 包括: 基于所述线性层的输出神 经元的数 量或 所述线性层 的预备权 值参数的 标准差, 计算所述线 性层的尺 度变换参 数 ; 以及基于所述线性 层的尺度 变换参数, 对所述线性层的量化 权值参数 进行 所述尺度 变换处理 , 以得到所述线性层 的标准量化 权值参数 。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 所述线性层包 括从 卷积层、 递归层和全 连接层组成 的群组 中选择的至 少一个。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 所述线性层未 被批 量归一化层 直接跟 随。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 对所述神经网 络模 型的参数进 行量化 , 以得到所述量化模 型, 包括: 对所述线性层的预 备权值 参数进行 钳位处理 , 以得到所述线性层 的钳位权值 参数; 以及对所 述线 性层的钳位 权值参数 进行量化 处理, 以得到所述线 性层的量 化权值参 数。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 基于所述线性 层的 输出神经元 的数量 , 计算所述线性层的尺 度变换参 数, 包括: 根据第 一尺 度变换参数 计算公 式计算所述 线性层的尺 度变换参 数, 所述第一尺度 变换 参数计算公 式表示 为:
Figure imgf000004_0001
其中 , RSF表示所述 线性层的 尺度变换参 数, A表示所 述线性层 的输 出神 经元的数量 , Q表示所述 线性层的量 化权值矩 阵, VAF{ Q表示所述线 性层 的量化权值 矩阵的元 素的方差 。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 所述线性层的 量化 权值参数 的比特位 的位数为 1-8。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 所述线性层的 量化 权值参数 的比特位 的位数为 1-2。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 基于所述线性 层的 输出神经元 的数量 , 计算所述线性层的尺 度变换参 数, 包括: 根据第 二尺 度变换参数 计算公 式计算所述 线性层的尺 度变换参 数, 所述第二尺度 变换 参数计算公 式表示 为:
Figure imgf000004_0002
其中 , RSF表示所述 线性层的 尺度变换参 数, 出神 经元的数量 , W表示所述 线性层的辅助 权值矩
Figure imgf000004_0003
性层 的辅助权值 矩阵的元 素的方差 ; 所述 线性层的辅 助权值矩 阵 表示为:
W= 2W- 1 其中 , W表示所述 线性层的钳 位权值矩 阵。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 基于所述线性 层的 预备权值参 数的标准 差, 计算所述线性层 的尺度变换 参数, 包括: 根 据 第三尺度 变换参数计算 公式计算 所述线性层 的尺度 变换参数, 所述第三 尺度 变换参数 计算公式表 示为:
Figure imgf000005_0001
其中 , RSF表示所述 线性层的 尺度变换参 数, W表示所 述线性层 的预 备权值 矩阵, VAR V\j( 示所 述线性层 的辅助
Figure imgf000005_0002
元素 的方差 ; 所述 线性层的辅 助权值矩 阵 表示为:
W= 2W- 1 其中 , W表示所述 线性层的钳 位权值矩 阵。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 所述线性层的 量化 权值参数 的比特位 的位数为 3-8。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 基于所述线性 层的 尺度变换参 数,对所述线性层 的量化权值 参数进行 所述尺度 变换处理 , 以得 到所述线 性层的标准 量化权值 参数, 包括: 根据尺度变换公 式对所述 线性层 的量化权 值参数进 行所述尺 度变换处理 ,所述尺度变换公 式表示为 : cfj = RSF · qj 其中 , Cf表示所述线性 层的标准 量化权值矩 阵, Cfj 表示所述线性层 的标 准量化权值 矩阵的 第 i行第 j列的参数, Q表示所述线性 层的量化权 值 矩阵 , 4 表示所 述线性层 的量化权值 矩阵的第 i行第 j列的参数。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 对所述线性层 的预 备权值参数 进行所述 钳位处理 , 以得到所述线性层 的钳位权值 参数, 包括 : 根据钳位公式对所 述线性层 的预备权值 参数进行 所述钳位 处理, 所 述钳位 公式表 示为: 其中 , W表示所述线性层 的钳位权 值矩阵, %表 示所述钳位 权值矩阵 的 第 i行第 j列的参数, W表示所述线性层 的预备权 值矩阵, %表示所述 线性层 的预备权 值矩阵的 第 i行第 j列的参数, ^ 表示 所述线性 层的预备 权值矩 阵的第 m行第 n列的参数, t anh(.)表示双曲正切函数, max( .)表 示取 最大值函数 。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 对所述线性层 的钳位 权值参数 进行所述 量化处理 , 以得到所述线性层 的量化权 值参数, 包括 : 根据量化公式对 所述线性层 的钳位权值 参数进行 所述量化 处理, 所 述量 化公式表 示为:
Qj = -^― r ound( ( 2^ - 1)V^ ) - 1 2 1 其中 , Q表示所 述线性层 的量化权 值矩阵, 4 表示所 述线性层 的量 化权值 矩阵的 第 i行第 j列的参数, b表示量化比 特位的位数 , r ound㈠表 示四 舍五入函 数。 例如 , 本公开一些实施例 提供的压 缩与加速 方法, 还包括: 采用与所 述神 经网络模型 相同的训 练参数配 置, 对所述目标量化 模型进行 训练。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 所述目标量化 模型 的训练过程 包括: 前向传播阶 段、 后向传播阶段和 标准量化 阶段; 所 述前 向传播阶段 包括 :使用当前的 目标量化模 型对训练输 入数据进 行处理, 以得 到训练输 出数据, 并基于所述 训练输出数 据计算损 失值; 所述后向传 播阶 段包括: 基于所述损失值, 计算梯度, 并基于所述梯度对 当前的神 经 网络模 型的参数 进行修正 , 以得到更新的神 经网络模 型; 所述标准量化 阶 段 包括:对更新的 神经网络模 型的参数 进行量化 , 以得到更新的量化模 型, 以及 对所述更 新的量化模 型进行尺 度变换处 理, 以得到更新的 目标量化模 型。 例如 , 在本公开一些实施 例提供的 压缩与加速 方法中 , 所述神经网络 模型 包括激活 层, 所述激活层包括 PACT激 活函数 , 所述 PACT激活 函数 表示 为: 其 中, x表示所 述激活层 的输出, x表示所述激 活层的输 入, a表示 所述 PACT 激活函 数的激活值 参数; 对 所述神经 网络模型 的参数进行 量化, 以得到所述量化 模型,还包括: 根 据激活值 量化公式 对所述激 活层的输 出进行所述 量化处理 , 所述激 活值量化公 式表示 为: ry w, q = - r ound( ( 2s - 1) — )
2s - 1 a 其 中, q表示 所述激活 层的输出 的量化值 , a 表示所述激活层的输 出 的量化值 的比特位 的位数, r ound(-)表示四舍五入函数。 例 如, 在本公开一些 实施例提 供的压缩 与加速方 法中, 所述后向传播 阶段还包括 : 根据激活值梯度 公式计算 激活值梯度 , 并基于所述激 活值梯 度对当前 的激活值参 数进行修 正, 以得到更新的 激活值参数 , 所述激活值
Figure imgf000007_0001
例 如, 在本公开一些 实施例提 供的压缩 与加速方 法中, 所述训练参数 配置包括 : 初始学习率、 学习率调整方案、 权值衰减、 训练集的迭代 次数、 优化器和批 尺寸。 例 如, 在本公开一些 实施例提 供的压缩 与加速方 法中, 在对所述神经 网络模型 的参数进行 量化前 , 所述压缩与加速方 法还包括 : 对所述神经网 络模型进行 预训练, 以得到所述神经网络 模型的预 备权值参数 。 例 如, 在本公开一些 实施例提 供的压缩 与加速方 法中, 对所述神经 网 络模型进行 所述预训 练, 包括: 采用恺明初始化 方案对所述 神经网络模 型 的参数进 行初始化 。 例 如, 在本公开一些 实施例提 供的压缩 与加速方 法中, 所述神经网络 模 型包括 ResNet、 MobileNet-Vl、 MobileNet-V2和 YGG-Net之一。 本公开至 少一个实施 例还提供 一种数据处 理方法, 包括: 采用本公开 任一 实施例提供 的压缩 与加速方 法得到的所 述目标量 化模型对输 入数据进 行处 理。 本公开 至少一个 实施例还提 供一种数 据处理装 置, 包括: 存储器, 用 于非 暂时性存储 计算机可 读指令 ; 以及处理器, 用于运行计算机可读指令; 其 中, 所述计算机可读指 令被所述 处理器运 行时执行 本公开任一 实施例提 供的 压缩与加速 方法或执 行本公开 任一实施例 提供的数 据处理 方法。 本公开 至少一个 实施例还提 供一种 存储介质, 非暂时性地存储计算 机 可读 指令, 其中, 当所述非暂时性 计算机可 读指令由 计算机执行 时可以执 行本 公开任一 实施例提供 的压缩与 加速方法 的指令或 者可以执 行本公开任 一实 施例提供 的数据处理 方法的指 令。 附图说 明 为了更 清楚地说 明本公开 实施例的技 术方案, 下面将对实施例的 附图 作 简单地介绍 , 显而易见地, 下面描述中的 附图仅仅涉 及本公开 的一些实 施例 , 而非对本公开的 限制。 图 1为一种卷积神经 网络的示 意图; 图 2A为一种卷积神 经网络 的结构示意 图; 图 2B为一种卷积神 经网络的 工作过程 示意图; 图 3为另一种卷积神 经网络的 结构示意 图; 图 4为本公开至少一 实施例提 供的一种 神经网络模 型的压缩 与加速方 法的 流程图; 图 5为本公 开至少一 实施例提供 的一种对 应于图 4中所示 的步骤 S100 的示 例性流程 图; 图 6 为本公开至少一 实施例提 供的另一 种对应于 图 4 中所示的步骤 S100 的示例性 流程图; 图 7为本公 开至少一 实施例提供 的一种对 应于图 4中所示 的步骤 S200 的示 例性流程 图; 图 8为本公开至少一 实施例提供 的一种对 应于图 4中所示 的步骤 S300 的示 例性流程 图; 图 9为本公开至少一 实施例提 供的一种 数据处理 装置的示 意性框图 ; 以及 图 10为本公开至少一 实施例提 供的一种 存储介质 的示意图。 具体 实施方式 为使 本公开实施 例的目的 、 技术方案和优点更 加清楚, 下面将结合本 公开 实施例的 附图, 对本公开实施例 的技术 方案进行清 楚、 完整地描述。 显然 , 所描述的实施例是 本公开的 一部分实 施例, 而不是全部 的实施例 。 基于 所描述的 本公开的 实施例, 本领域普通技术人 员在无需创造 性劳动的 前提 下所获得 的所有其 他实施例 , 都属于本公开保护 的范围。 除非 另外定义 , 本公开使用的技术 术语或者科 学术语应 当为本公开 所 属领 域内具有 一般技能 的人士所 理解的通 常意义。本公开 中使用的“第一” 、 “第二” 以及类似的 词语并不表 示任何顺 序、 数量或者重要 性, 而只是用来 区分 不同的组 成部分 。 “包括 ”或者 “包含 ”等类似的词语意指出现该词前面 的元 件或者物 件涵盖 出现在该词后 面列举的 元件或者 物件及其 等同, 而不 排除 其他元件 或者物件 。 “连接”或者“相连”等类似 的词语并 非限定于 物理 的或 者机械的 连接, 而是可以包括电性 的连接, 不管是直接的 还是间接 的。 “上”、 “下”、 “左”、 “右”等仅用于表示相对位置 关系, 当被描述对象的绝 对位 置改变后 , 则该相对位置关系 也可能相应 地改变。 下面通 过几个具 体的实施 例对本公 开进行说明 。 为了保持本公开 实施 例的 以下说明 清楚且简 明,本公开省略了 已知功能和 已知部件 的详细说 明。 当本 公开实施例 的任一部 件在一个 以上的 附图中出现 时, 该部件在每个 附 图 中由相同或 类似的参 考标号表示 。 在 AI领域的算 法技术 当中, 深度学习 (Deep Learning) 受到学术界 与工 业界的广 泛关注, 各国科学 家、 研究人员、 企业、 网络社区等都在 大 力研 究并推动 深度学 习的神经网络 模型的研 究与发展 。 随着 深度学习在 图像分类 、 目标检测以及 自然语言处理 等领域取得 突 破进 展, 将其应用到实 际生活场景 的需求也愈 发强烈 。 当前, 移动与便携 式 电子设备极 大地方便 了人们的生 活, 而深度学习将极 大地提 高这些设备 的智 能性与娱 乐性。 因此, 将深度学习的神 经网络模型 部署在 移动端与嵌 入式 系统便成 为迫切需 求。 但是 , 应用深度学习的神 经网络模型 在实际部 署中, 通常面临模 型尺 寸过 大的问题 ,例如神经网络模型 的文件 大小一般从 几十兆到 上百兆不 等, 这样 的文件大 小, 对于移动端来说 , 下载时耗费的流量 以及带宽 影响所导 致的 传输等待 时间过长是 用户无法 忍受的; 特别是对于 一些存储 空间有限 的嵌 入式系统 , 可能根本没有足 够的存储 空间来存储 这么大的神 经网络模 型文 件。 同时, 深度学习的神经 网络模型对 计算资源及 计算能力 要求高 ; 在使 用大型神 经网络模 型进行计算 时, 移动端与嵌入 式系统抑或 无法提供 其所 需的计算 资源, 抑或计算缓慢 , 导致响应延迟太 高而无法满 足实际应 用场 景。 此外, 神经网络模型耗 电量也大。 在神经网络计算过程 中, 处理 器 需要频繁读 取神经网络 模型的参 数, 因此较大的神 经网络模型 也相应带 来更 高的内存 访问次数 , 而频繁的内存访 问也会极大提 高耗电量 , 高耗电 量不 利于将神 经网络模 型部署在移 动端。 因此 , 为了在资源有限的 硬件设备 上部署性 能良好的神 经网络, 就需 要对 神经网络模 型进行压 缩和加速 。 由于量化模型在硬 件上移植 会非常 方 便 , 因此, 在众多的对神经网络模 型进行压 缩和加速 方法中, 对神经网络 模型 进行量化 的方法具 有巨大的 发展潜力 。 本公 开至少一 实施例提供 一种神经 网络模型的 压缩与加 速方法。 该神 经 网络模型包括 线性层 , 神经网络模型的参数 包括预备 权值参数 ; 该压缩 与加 速方法 包括: 对神经网络模型 的参数进 行量化, 以得到量化模型, 量 化模 型的参数 包括线性 层的量化权 值参数 ; 以及, 对量化模型进行尺度 变 换处 理, 以得到目标量化 模型。 其中, 对量化模型进 行尺度变换 处理, 包 括 : 基于线性层的输出神 经元的数 量或线性层 的预备权 值参数的 标准差, 计算 线性层的尺 度变换参 数; 以及, 基于线性层的尺度 变换参数 , 对线性 层的 量化权值参 数进行尺 度变换处 理,以得到线性层的 标准量化 权值参数 。 本公 开的一 些实施例 还提供 对应于上 述压缩 与加速 方法的 数据处理 方 法及装置、 存储介质。 本公 开的实 施例提供 的神经 网络模 型的压缩 与加速 方法通 过对量化 模型 进行尺度 变换处理 , 得到目标量化模型 , 可以提高目标量化 模型的精 度 , 改善目标量化模型 的性能。 最初 , 卷积神经网络 ( Convolutional Neural Network, CNN ) 主要用 于识 别二维形状 , 其对图像的平移 、 比例缩放、 倾斜或其他形式 的变形具 有 高度不变性 。 CNN主要通过 局部感知 野和权值共 享来简化 神经网络模 型 的复 杂性、 减少权重的数 量。 随着深度学习技 术的发展 , CNN的应用 范围 已经 不仅仅限于 图像识别领 域, 其也可以应用 在人脸识 别、 文字识别、 动 物分 类、 图像处理等领域 。 图 1示出了一种 卷积神经 网络的示 意图。 例如, 该卷积神经 网络可以 用于 图像处理 , 其使用图像作为输 入和输出 , 并通过卷积核替代 标量的权 重 。 图 1中仅示出了具有 3层结构的卷积神经网络 , 本公开的实施例对 此不 作限 制。如图 1所示,卷积神经网络 包括输入层 101 隐藏层 102和输出层 103。 输入 层 101具有 4个输入, 隐藏层 102具有 3个输出,输出层 103具有 2个输出, 最终 该卷积神经 网络最终输 出 2幅图像。 例如 , 输入层 101的 4个输入可以为 4幅图像, 或者 1幅图像的四种特征 图像 。 隐藏层 102的 3个输出可以为经过输入层 101输入的图像的特征 图像。 例如 , 如图 1所示, 卷积层
Figure imgf000011_0001
核 , 偏置 ^是叠加到 卷积层的输 出的标量 , 其中, k是表示输入层 101的标 签, i和 j分别是输入层 101的单元和隐藏层 102的单元的标签。 例如, 第一 卷积 层 201包括第一组卷积核 (图 1中的 < )和第一组偏置 (图 1中的 )。 第二 卷积层 202包括第二组卷 积核 (图 1中的 <)和第二 组偏置 (图 1中的 b ) 通常, 每个卷积层包括数 十个或数 百个卷积 核, 若卷积神经 网络为 深度 卷积神经 网络, 则其可以包括 至少五层 卷积层。 例如 , 如图 1所示, 该卷积神经网络还包括第一 激活层 203和第二激活 层 204。 第一激活层 203位于第一卷积层 201之后, 第二激活层 204位于第二 卷积 层 202之后。 激活层(例如, 第一激活层 203和第二激活层 204)包括激 活 函数, 激活函数用于 给卷积神经 网络引入 非线性因素 , 以使卷积神经 网 络可 以更 好地解 决较为 复杂 的问题 。 激活函数可 以包括 线性修 正单 元 ( ReLU)函数、 S型函数( Sigmoid函数)或双曲正切函数 ( tanh函数)等。 ReLU 函数 为非饱和非 线性函数 , Sigmoid函数和 tanh函数为饱和非线性函 数 。 例如, 激活层可以单独作为卷 积神经网 络的一层 , 或者激活层也可以 被 包含在卷积层 (例如, 第一卷积层 201可以包括第一激 活层 203, 第二卷 积层 202可以包括第 二激活层 204) 中。 例如 , 在第一卷积层 201中, 首先, 对每个输入应用 第一组卷 积核中 的若 干卷积核 < 和第一 组偏置 中的若干 偏置 ^ , 以得到第一卷 积层 201 的输 出; 然后, 第一卷积层 201的输出可以通过第 一激活层 203进行处理, 以得 到第一激 活层 203的输出。 在第二卷积层 202中, 首先, 对输入的第一 激活 层 203的输出应用第二组 卷积核中 的若干卷积 核 < 和第二组 偏置中 的 若干 偏置 ^ , 以得到第二卷积层 202的输出; 然后, 第二卷积层 202的输出 可以 通过第二 激活层 204进行处理, 以得到第二激 活层 204的输出。 例如, 第一 卷积层 201的输出可 以为对其 输入应用 卷积核 <后 再与偏 置 相 加 的结 果, 第二卷积层 202的输出可以为对 第一激活 层 203的输出应用卷积核
Figure imgf000012_0001
需要 对卷积 神经网 络进行训 练。 经过训练之后, 卷积神经网络 的卷积核和 偏置在图像 处理期 间保持不 变。 在训练过程 中, 各卷积核和偏置通 过多组输入 /输出示例图像 以及优化 算法 进行调整 , 以获取优化后的卷 积神经网络 模型。 图 2A示出了一种 卷积神经 网络的结构 示意图 ,图 2B示出了一种卷 积 神经 网络的工作 过程示意 图。 例如, 如图 2A和 2B所示, 输入图像通过输 入层 输入到卷积 神经网络后 , 依次经过若干个 处理过程 (如图 2A中的每 个层 级) 后输出类 别标识。 卷积神经网 络的主要组 成部分可 以包括多 个卷 积层 、 多个下采样层和全 连接层。 例如, 一个完整的卷积神经 网络可以由 这三种 层叠加组 成。 例如, 图 2A仅示出了一种卷 积神经网络 的三个层 级, 即第 一层级、 第二层级和第三层级 。 例如, 每个层级可以包括一 个卷积模 块和 一个下采样 层。 例如, 每个卷积模块可 以包括卷积 层。 由此, 每个层 级的 处理过程 可以包括 : 对输入图像进行卷 积 ( convolution ) 以及下采样 ( sub-sampling /down-sampling )。 例如, 根据实际需要, 每个卷积模块还 可以 包括批量 归一化 ( batch normalization ) 层, 从而每个层级的处理过程 还可 以包括批 量归一化处 理。 例如 , 批量归一化层用于 对特征图进 行批量 归一化处理 , 以使特征图 像的 像素的灰 度值在预 定范围内 变化, 从而降低计算难 度, 提高对比度 。 例如 , 预定范围可以为 [-1 , 1]。 例如, 批量标准化层的处理方式可以参考 常见 的批量标 准化处理 的过程, 在此不再赘 述。 卷积层 是卷积神 经网络的核 心层。 在卷积神经 网络的卷积 层中, 一个 神经 元只与部 分相邻层 的神经元连接 。 卷积层可以对输入 图像应用 若干个 卷积核 (也称为滤波 器), 以提取输入图像的 多种类型 的特征。 每个卷积核 可以 提取一种 类型的特征 。 卷积核一般以随 机小数矩 阵的形式 初始化, 在 卷积神 经网络 的训练过程 中卷积核将 通过学 习以得到合 理的权值 。 对输入 图像 应用一个 卷积核之后 得到的结 果被称为 特征图像 ( feature map), 特征 图像 的数目与 卷积核的数 目相等 。 每个特征图像由一 些矩形排 列的神经 元 组成 , 同一特征图像的神 经元共 享权值, 这里共享的权 值就是卷 积核 一 个层 级的卷积层 输出的特 征图像可 以被输入到 相邻的下 一个层级 的卷积层 并再 次处理以得 到新的 特征图像 。 例如, 如图 2A所示, 第一层级的卷积 层可 以输出第 一特征图像 , 该第一特征图像被 输入到 第二层级的 卷积层再 次处 理以得到 第二特征 图像。 例如 , 如图 2B所示, 卷积层可以使用不同的卷积 核对输入 图像的某一 个局 部感受域 的数据进 行卷积, 卷积结果被 输入激活层 , 该激活层根据相 应的 激活函数 进行计算 以得到输入 图像的特征 信息。 例如 , 如图 2 A和 2B所示, 下采样层设置在相邻的卷 积层之 间, 下采 样层是 下采样 的一种形式 。 一方面, 下采样层可以用 于缩减输入 图像的规 模 , 简化计算的复杂度 , 在一定程度上减小 过拟合的现 象; 另一方面, 下 采样 层也可以 进行特征压 缩, 提取输入图像 的主要特征 。 下采样层能够减 少特 征图像的 尺寸, 但不改变特征 图像的数 量。 例如, 一个尺寸为 12x12 的输 入图像, 通过 6x6的卷积核 对其进行 采样, 那么可以得到 2x2的输出 图像 ,这意味着输入 图像上 的 36个像素合 并为输出 图像中的 1个像素。最 后一 个下采样 层或卷积层 可以连接 到一个或 多个全连接 层, 全连接层用于 连接 提取的所有 特征。 全连接层的输出为一 个一维矩 阵, 也就是向量。 图 3示出了另一种 卷积神经 网络的结构 示意图。 例如, 参见图 3所示 的示 例, 最后一个卷积 层 (即第 t个卷积层) 的输出被输入到平坦 化层以 进行 平坦化操 作 (Flatten)。 平坦化层可以将特征图像(2D 图像)转换为 向量 ( 1D)。 该平坦化操作可以按照如下的方式进 行:
Vk ~ fklj,k%j 其中 , v是包含 k个元素 的向量 , f是具有 i行 j列的矩阵。 然后 , 平坦化层的输出 (即 1D向量 )被输入到一个全 连接层 ( FCN)。 全连接 层可以 具有与卷 积神经网络 相同的结构 , 但不同之处在于 , 全连接 层使 用不同的 标量值以替 代卷积核 。 例如 , 最后一个卷积层 的输出也 可以被 输入到均 化层 (AVG)。 均化 层用 于对输出进 行平均操 作, 即利用特征图像的 均值表示输 出图像 , 因此, 一个 2D 的特征图像 转换成为 一个标量 。 例如, 如果卷积神经网络包括 均 化层 , 则其可以不包括平 坦化层 。 例如 , 根据实际需要, 均化层或全 连接层可以 连接到分 类器, 分类器 可以 根据提取 的特征进 行分类, 分类器的输 出可以作 为卷积神经 网络的最 终输 出, 即表征图像类别 的类别标识 ( label )。 例如 , 分类器可以为支持 向量机 ( Support Vector Machine, SVM ) 分 类器 、 softmax分类器以及最邻近 规则 ( KNN ) 分类器等。 如图 3所示, 在一 个示例 中, 卷积神经网络包括 softmax分类器, softmax分类器是一种 逻辑 函数的生成 器,可以把一个 包含任意 实数的 K维向量 z压缩成 K维 向 量 cr(z)。 softmax分类器的公式如下:
Figure imgf000014_0001
其中 , Zj.表示 K维向量 z中第 j个元素, cr(z)表示每个类别标识 ( label ) 的预 测概率, ⑵为 实数, 且其范围为 (0, 1), K维向量 ⑵的和为 1。 根 据以 上公式 , K维向量 z中的每个类别 标识均被赋 予一定的 预测概率 , 而 具有 最大预测概 率的类别 标识被选择 作为输入 图像的标识 或类别。 下面 结合附图对 本公开的 一些实施例 及其示例 进行详细说 明。 图 4为本公开至少 一实施例提 供的一种 神经网络模 型的压缩 与加速方 法 的流程 图。 例如, 该压缩与 加速 方法可 以用 于对 ResNet (例如, ResNet-50)、 MobileNet-Vl、 MobileNet-V2和 YGG-Net等各种神经网络模 型进 行量化, 以实现上述各种 神经网络模 型的压缩 与加速。需要 说明的是 , 该压 缩与加 速方法 的适用 范围包括 但不限 于以上列 举的各 种神经 网络模 例如 , 如图 4所示, 该压缩与加速方 法包括步骤 S000至步骤 S300。 步骤 S000: 对神经网络模型进行 预训练, 以得到神经 网络模型的预 备 权值 参数。 例如 , 在步骤 S000 中, 该神经网络模型可 以是未经训 练的全精 度模 型 ( full-precision model )。例如,可以采用常规的训练方法、训练技巧 ( tricks) 以及 训练参数 (例如, 包括超参数) 配置对该全精 度模型进 行预训练 。 例如 , 训练参数配置通常 包括: 初始学习率 (initial learning rate)、 学 习率 调整方案 (learning rate scheduler)、 权值衰减 (weight decay)、 训练集 的迭 代次数 (the number of epochs)、 优化器 (optimizer) 和批尺寸 (batch size) 等。 例如, 在一些示例中, 初始学习率 可以设置 为 0.05, 学习率调 整 方案可以采 用余弦退 火调整方 案 (cosine annealing scheduler), 权值衰减 可 以设置为 4 x 10 5 , 训练集的迭代次数可以设置为 150次, 优化器可以采 用随 机梯度下 降 (stochastic gradient descent, SGD) 优化器, 批尺寸可以 设置 为 2048或 1024等。 需要说明的是 , 上述训练参数配置 是示例性 的, 不应 视作对本公 开的限制 。 在本公开的实施例 中, 训练参数配置 可以根据 实际 需要进行设 定。 例如 , 神经网络模型的预 训练过程通 常包括 : 对神经网络模型的参 数 进行 初始化 ; 使用神经网络模型对 训练输入数 据进行处 理, 得到训练输出 数据 ; 基于训练输出数据 , 通过损失函数计算 损失值 ; 基于损失值计算梯 度 , 并对神经网络模型 的参数进行 修正。 例如 , 在一些示例中, 可以采用彳岂明初始化 ( Kaiming Initialization) 方案 对神经 网络模型的参 数进行初 始化。 例如, 可以将神经 网络模型的参 数初 始化为符 合高斯分 布的随机 数。 例如, 可以使神经网络模 型的各功 能 层 (例如, 卷积层、 全连接层等) 的初始权值参数 符合高斯 分布, 例如, 该高 斯分布的 期望为 0, 该高斯分布的标准差 为该功能层 的输出神 经元的 数量 的倒数。 例如, 对于一个卷积层而言 , 该卷积层的输出神经 元的数量 等于 该卷积层 的输出通道 数与该卷 积层的卷积 核中的元 素数量之 积;例如, 对于 一个全连接 层而言 , 该全连接层的输出神 经元的数 量等于该 全连接层 输 出的特征的 数量。 例如 , 在一些示例中, 训练输入数 据的类型依 赖于神经 网络模型 的处 理对 象, 例如根据神经 网络模型 的处理对象 的不同, 训练输入数据可以包 括 图像、文本、语音等。以 ResNet、MobileNet-Vl、MobileNet-V2和 YGG-Net 等 神经 网络模 型为 例, 其训练输入 数据 可以 为图像 , 例如可 以使 用 ImageNet数据 库中的 图像作为其 训练输入数 据。 例如 , 在一些示例中, 损失函数可 以根据实 际需要进行 选择, 例如, 损失 函数可 以包括但不 限于 0-1损失函数 、 平方损失函数、 对数损失函数、 交叉 熵损失函数 (cross-entropy cost function)等之中的一种或其任意组合, 本公 开的实施例 对此不作 限制。 例如 , 在一些示例 中, 可以采用 随机梯度 下降算 法或批 量梯度 下降 (batch gradient descent, BGD) 算法等计算梯度, 并根据该梯度对神经网 络模 型的参数 进行修正 。 例如 , 在一些示例中, 神经网络模 型的预训 练过程还可 以包括 : 判断 神经 网络模型 的训练是否 满足预定 条件, 若不满足预定 条件, 则重复对其 进行 训练; 若满足预定条 件, 则停止对其进行 训练, 得到训练好的 神经网 络模 型。 例如, 在一个示例中, 上述预定条件为训练输 入数据对 应的损失 值不 再显著减 小; 例如, 在另一个示例中 , 上述预定条件为神 经网络模型 的训 练次数或 训练周期达 到预定数 目; 本公开的实施例 对此不作 限制。 需要说 明的是 , 上述说明仅是示意 性阐述神 经网络模型 的训练过程 。 本领 域技术人 员应当知道 , 在训练过程中, 需要利用大量样本数 据对神经 网络 模型进行 训练; 同时, 在每一个样本数据 的训练过 程中, 都可以包括 多次 反复迭代 以对神经 网络模型的 参数进行 修正。 又例如, 训练阶段还包 括对 神经网络模 型的参数 进行微调 (fine-tune), 以获取更优化的参数。 例如 , 在一些示例中, 神经网络模 型包括线 性层, 例如, 线性层包括 卷 积层 ( convolution layer )、 递归层 ( recurrent layer ) 和全连接层 (fully-connected layer) 等至少之一。 例如, 在一些示例中, 神经网络模型 还包 括非线性层 , 例如, 非线性层包括批量 归一化层 (batch normalization layer) 和激活层 (activation layer, 例如采用非线性激活函数) 等。 例如 , 经过预训练后, 神经网络模型 的参数为 预备权值参 数。 例如, 在一 些示例 中, 预备权值参数为全 精度的 32位的浮点数 。 需要说明的是, 在一 些示例 中, 本公开的 实施例提 供的压 缩与加速 方法可 以不包 括步骤 S000, 例如, 可以直接基于 本领域中 已经训练好 的神经网络 模型, 执行步 骤 S100至步骤 S300, 以得到目标量化模型。 在此情况下 , 该已经训练好 的神 经网络模 型的参数 为预备权值 参数。 步骤 S100: 对神经网络模型的 参数进行 量化, 以得到量化模型。 例如 , 在步骤 S100中, 可以采用 DoReFa方案对神经 网络模型 的参数 进行 量化。 例如, 对神经网络模型 的参数进 行量化, 是指将神经网络模型 的至 少部分参 数从例如 高精度的 浮点数 (例如, 全精度的 32位的浮 点数) 变为例 如低精 度的定点数 (例如, 1-8位的定点数) , 从而实现对神经 网络 模型 的压缩与加 速。 需要说明的是 , 在步骤 S100 中, 也可以采用其他类 型的 量化方案 对神经 网络模型的参 数进行量 化, 本公开的实施例 对此不作 限制 。以下,基于 DoReFa方 案,对步骤 S100中的量化过程 进行详细说 明。 例如 , DoReFa方案的 具体细节 可以参见文 献, Shuchang Zhou, Yuxin Wu, Zekun Ni, Xinyu Zhou, He Wen, and Yuheng Zou. Dorefa-Net: Training Low Bitwidth Convolutional Neural Networks With Low Bitwidth Gradients , arXiv: 1606.06160, 2016。 在此将该文献全文引用结合于此, 以作为本公开 的一 部分。 图 5为本公开至少一 实施例提供 的一种对 应于图 4中所示 的步骤 S100 的示 例性流程 图。 例如, 如图 5所示, 对神经网络模型 的参数进行 量化, 以得 到量化模 型, 即步骤 S100, 包括步骤 S110至步骤 S120。 步骤 S110: 对线性层的预备权值参 数进行钳位 处理, 以得到线性层 的 钳位 权值参数 。 例如 ,“ 钳位处理” 是指对一组参数 (例如, 线性层的预备权值 参数) 按照 一定的规 则 (例如, 根据某一个公式) 进行缩放, 使缩放后的参 数的 取值 范围限制在 某一 区间, 以便于后续进一步 处理。例如, 在一些示例 中, 可 以根据钳位 公式对线性 层的预备 权值参数进 行钳位处理 , 以将线性层的 钳位 权值参数 的取值范 围限制在预 定区间, 例如预定区 间可以为[0, 1], 但 不 限于此。 例如, 通过钳位处理 , 可以使线性层的参数 (即线性层 的钳位 权值 参数) 在预定区间中的 分布更加 均匀, 从而有利于 减小后续 步骤中的 量化 误差。 例如, 在一些示例 中, 钳位公式可以表示 为:
Figure imgf000017_0001
其中 , W表示线性层 的钳位权 值矩阵 (包括线性层的钳 位权值参 数),
% 表示 钳位权值矩 阵的第 i行第 j列的参数, W表示线 性层的预备 权值矩 阵 (包括线性层 的预备权值 参数), %表示线性 层的预备权 值矩阵的 第 i
Figure imgf000017_0002
t anh( -)表示双曲正切函数, max( -)表示取最大值函数。 例如 , 上述钳位公式可 以将线性层 的钳位权值 参数的取 值范围限 制在 区 间[0, 1]中。 步骤 S120: 对线性层的钳位权值参 数进行量 化处理, 以得到线性层 的 量化 权值参数 。 例如 , 在一些示例中, 可以根据权值量化公 式对线性层 的钳位权值 参 数进 行量化处理 , 以得到线性层的 量化权值参 数。 例如, 在一些示例中 , 权值量 化公式 可以表示 为:
Qj = -^― r ound( ( 2^ - 1)V^ ) - 1 2 1 其中 , Q表示线性层 的量化权 值矩阵 (包括线性层的量 化权值参 数), Qj 表示 线性层的 量化权值矩 阵的第 i行第 j列的参数, b表示线性 层的量 化权值 参数的 比特位的位 数, r ound(-)表示四舍五入函数。 例如 , 量化模型的参数 包括线性层 的量化权值 参数。 例如, 为了便于 量化 模型移植 到移动端 与嵌入式 系统, 线性层的量化 权值参数 的比特位的 位数 b —般设定为 1-8位 (bit)。 当然, 线性层的量化权值参数的比特位的 位数 也可以根 据需要设 定为更多位 , 本公开的实施例 对此不作 限制。 图 6 为本公开至少一 实施例提 供的另一 种对应于 图 4 中所示的步骤 S100 的示例性 流程图。 图 6所示的步骤 S100除了 包括图 5中所示的步骤 S110和 步骤 S120之外, 还包括步骤 S130。 例如 , 在一些示例中, 神经网络模 型包括激 活层。 例如, 激活层可以 包括 PACT激 活函数, 但不限于此。 例如, PACT激活 函数表示 为:
Figure imgf000018_0001
其中 , x所述激活层 的输出, x表示激活层 的输入, a表示 PACT激 活函 数的激活 值参数。 例如, a为浮 点数 (floating number)。 例如, PACT 激 活函数可 以减小激活 层的输出 的量化误差 。 例如 , 如图 6所示, 对神经网络模型的 参数进行 量化, 以得到量化模 型, 即步骤 S100, 还包括步骤 S130。 步骤 S130: 对激活层的输出进 行量化处理 。 例如 , 在一些示例中, 可以根据激活值量化 公式对激 活层的输 出进行 量化 处理。 例如, 激活值量化公 式可以表示 为: q
Figure imgf000018_0002
其中 , q表示激活 层的输出 的量化值 , a 表示激活层的输出 的量化值 的 比特位的位 数, r ound㈠表示四舍五 入函数 。 例如, q为动态固定点数 (dynamic fixed-point number) ; 例如, 激活层的输出的量化值的比特位的 位数 a —般设定为例如 1 -8位, 例如 2-4位等。 例如 , 在本公开的实施例 中, 对激活层的输 出进行量化 处理, 有利于 提升 量化模型 的运算速 度, 从而有利于实 现本公开的 实施例提 供的压缩 与 加速 方法的加 速功能。 需要说 明的是 , 在本公开的实施例 中, 可以不对神经 网络模型 中的批 量 归一化层进行 量化处理 , 也可以不对神经 网络模型 中最后的全 连接层的 偏置 (bias) 进行量化处理。 在研 究中, 本申请的发 明人发现: 一方面, 根据步骤 S100得 到的量 化模 型通常存 在精度下 降和性能 退化的 问题; 另一方面, 在神经网络模 型 或 /和量化模型中 , 如果保持权值的梯度 处于同一尺 度量级, 可以防止梯 度 爆炸 和梯度消 失的问题 , 从而有利于提高量 化模型 的精度和 改善量化模 型 的性 能。 例如, 为了保持权值的梯 度处于 同一尺度量 级, 在神经网络模型 中 , 可以在线性层之后 直接连接批 量归一化 层 (线性层的输出经过批量归 一化 层的处理 之后再输 入到后续的 功能层 中); 但是, 在神经网络模型中, 往 往还 包括 未被批 量 归一化 层直 接跟 随的 线性层 , 例如, ResNet、 MobileNet-Vl、 MobileNet-V2和 YGG-Net等神经网络模型中 用于输出的 最 后一 层全连接层 等。 因此, 本公开的实施例提 供的压 缩与加速 方法, 在步 骤 S100之后, 还包括步骤 S200, 以对量化模型进行进一步处 理。 步骤 S200: 对量化模型进行 尺度变换处 理, 以得到目标量化 模型。 例如 , 在一些示例中, 在相同的 效率约束 ( efficiency constraints) 下, 与 步骤 S100得到的量 化模型 相比, 步骤 S200得到的 目标量化模型 可以具 有更 高的精度 以及更好 的性能。例如 ,相同的效率约束 是指模型 的尺寸 (对 应于 模型占用 的存储空 间)、 能耗、 延迟 (对应于模型的处理速度) 等基本 相 同。 例如, 在一些示例中, 步骤 S200 得到的目标量化模 型的性能 可以 与对 应的全精 度模型的 性能相当 或者比全精 度模型 的性能更优 (参考后续 表 1-2)。 图 7为本公 开至少一 实施例提供 的一种对 应于图 4中所示 的步骤 S200 的示 例性流程 图。 例如, 如图 7所示, 对量化模型进行 尺度变换处 理, 以 得到 目标量化模 型, 即步骤 S200包括 步骤 S210至步骤 S220。 步骤 S210:基于线性层的 输出神经 元的数量 或线性层的 预备权值参 数 的标 准差, 计算线性层的 尺度变换 参数。 例如 , 在一些示例中, 基于线性层 的输出神 经元的数量 , 计算线性层 的尺 度变换参数 , 包括: 根据第一尺度变换 参数计算公 式计算 线性层的尺 度 变换参数。 例如, 第一尺度变换 参数计算公 式表示 为:
Figure imgf000020_0001
其中 , RSF表示线性 层的尺度 变换参数 , A表示线性层 的输出神 经元 的数 量, Q表示 线性层的 量化权值 矩阵 (包括线性层的量化 权值参数 ), VAR Q 表示线性 层的量化 权值矩阵 的元素的方 差。 例如 ,在一些示例 中, 当线性层的量化权值参 数的比特位 的位数 为 1-2 位时 , 与采用后续两种尺 度变换参 数计算公 式计算得到 的线性层 的尺度变 换参 数 RSF相 比,采用第一尺 度变换参数 计算公 式计算得到 的线性层 的尺 度 变换参数 RSF, 可以使目标量化模型 更快收敛 。 需要说明的是, 在本公 开的 实施例中 , 当线性层的量化权值参数 的比特位 的位数 为其他值 (例如, 3-8 位) 时, 仍然可以采用第一尺度变换 参数计算 公式计算 线性层的 尺度 变换 参数 RSFo 例如 , 在另一些示例中 , 基于线性层的输出神 经元的数 量, 计算线性 层的 尺度变换参 数, 包括: 根据第二尺度 变换参数计算 公式计算 线性层的 尺度 变换参数 。 例如, 第二尺度变换参数计 算公式表示 为:
Figure imgf000020_0002
其中 , RSF表示线性 层的尺度 变换 线 性层的输 出神经元 的数 量, W表示线性层 的辅助权值 矩阵, 线性层的辅 助权值矩 阵的 元素的方 差。 线性层的辅助权 值矩
Figure imgf000020_0003
W= 2W- 1 其中 , W表示线性层 的钳位权 值矩阵。 需要说 明的是 , 在上述示例中, 线性层的辅助权值矩 阵 W是为了说明 第二 尺度变换 参数计 在神经 网络模型及其 量化模型 中并 不 包括线性层 的辅助
Figure imgf000020_0004
例如 , 在再一些示例中 , 基于线性层的预备权 值参数的 标准差, 计算 线性层 的尺度 变换参数, 包括: 根据第三尺度变换参数 计算公式 计算线性 层的 尺度变换参 数。 例如, 第三尺度变换参 数计算公 式表示为:
Figure imgf000021_0001
其中 , W表示线性层 的钳位权 值矩阵。 需要说 明的是 , 在上述示例中, 线性层的辅助权值矩 阵 W是为了说明 第三 尺度变换 参数计算公 式而引 入的, 在神经网络模 型及其量 化模型中 并 不 包括线性层 的辅助权值 矩阵 k 需要说 明的是 , 在一些示例中, 基于第一尺 度变换参数 计算公 式计算 的 线性层的尺 度变换参数 RSF得到 的目标量化 模型、基于 第二尺度 变换参 数计 算公式计 算的线性层 的尺度 变换参数 RSF得到的 目标量化模 型、 以及 基于 第三尺度 变换参数 计算公 式计算的线 性层的尺 度变换参数 RSF 得到 的 目标量化模 型, 三者的精度和 性能基本 相当。 例如 ,在一些示例 中, 当线性层的量化权值参 数的比特 位的位数 为 3-8 位 时, 可以选用第一尺 度变换参数 计算公式 、 第二尺度变换参数 计算公式 和 第三尺度 变换参数 计算公 式中的 任意一 个计算 线性层的 尺度变换 参数 RSF , 同时, 得到的目标量化模型的 精度和性 能基本相 当。 需要说明的是, 在 本公开的至 少一个实 施例中, 当线性层的量化权值 参数的比 特位的位数 为其 他值 (例如, 1-2 位) 时, 仍然可以采用第二尺度变换参数计算 公式 或 第三尺度 变换参数计算 公式计算 线性层的 尺度变换参 数 RSFo 步骤 S220: 基于线性层的尺度 变换参数, 对线性层的量 化权值参数 进 行尺 度变换处理 , 以得到线性层的 标准量化权 值参数。 例如 , 在一些示例中, 基于线性层的尺 度变换参数 , 对线性层 (例如, 未被 批量归一 化层直接跟 随的线性 层)的量化权值参数 进行尺度 变换处理 , 有利 于保持量 化模型 中的权值的梯 度处于 同一尺度量 级, 从而有利于提 高 量化 模型的精 度和改善 量化模型 的性能。 例如 , 在一些示例中, 可以根据尺度变换公 式对线性层 的量化权值 参 数进 行尺度变换 处理。 例如, 所述尺度变换公 式可以表 示为: cfj = RSF qj 其中 , cf表示线性层的标 准量化权值 矩阵 (包括线性层的标准量化权 值参 数), Cfj表示线性层的标准量化权值 矩阵的第 i行第 j列的参数, Q表 示线 性层的量化 权值矩阵 , Qj表示线性 层的量化权 值矩阵的 第 i行第 j列 的参 数。 需要说 明的是 , 在本公开的实施例 中, 可以只对未被批 量归一化 层直 接跟 随的线性层 的量化权 值参数进 行尺度变换 处理, 即可以不对被 批量归 一化 层直接跟 随的线性层 的量化权 值参数进行 尺度变换 处理。 当然, 也可 以 同时对未被批 量归一化 层直接跟 随的线性层 和被批量 归一化层 直接跟随 的线 性层的量化 权值参数 进行尺度 变换处理。 本公开的实施例对此 不作限 制。 步骤 S300: 采用与神经网络模型 相同的训 练参数配 置, 对目标量化模 型进 行训练。 例如 , 在步骤 S300 中, 神经网络模型的训 练参数配置 可以参考 步骤 S000 中的相关描 述, 在此不再重 复赘述。 图 8为本公开至少一 实施例提供 的一种对 应于图 4中所示 的步骤 S300 的示 例性流程 图。 例如, 如图 8所示, 采用与神经网络模 型相同 的训练参 数配 置, 对目标量化模型 进行训练 , 即步骤 S300 包括: 前向传播阶段、 后 向传播阶段和 标准量化 阶段, 以及重复执行这三个阶 段的操作 , 以得到 训练好 的目标量 化模型 。 前向传播阶段、 后向传播阶段 和标准量 化阶段分 别对应 于下述 步骤 S310、 步骤 S320和步骤 S330。 步骤 S310: 使用当前的目标量化 模型对训 练输入数据 进行处理 , 以得 到训 练输出数据 , 并基于训练输出数 据计算损 失值。 例如 , 目标量化模型的训练过程 的前向传播 阶段的操 作,即步骤 S310, 可 以相应地参 考神经网 络模型 (例如, 全精度模型) 的前向传播阶段的操 作, 在此不再重复赘述 。 步骤 S320: 基于损失值, 计算梯度, 并基于梯度对当前 的神经 网络模 型的 参数进行修 正, 以得到更新的神经网络 模型; 例如 , 目标量化模型的训练过程 的后向传播 阶段的操 作,即步骤 S320, 可 以相应地参 考神经网 络模型 (例如, 全精度模型) 的后向传播阶段的操 作, 在此不再重复赘述 。 例如 , 在一些示例中, 在本公开的 实施例提供 的压缩 与加速方 法还包 括 步骤 S130 (即对激活层的输出进行量化 处理) 的情况下, 在步骤 S320 中, 可以根据激活值梯 度公式计算 激活值梯 度, 并基于激活值 梯度对当前 的激 活值参数进 行修正 , 以得到更新的激活值 参数。例如 , 在一些示例中, 对于 前述 PACT 激活函 数和激活值 量化公 式而言, 激活值梯度 公式可 以表 示为 :
Figure imgf000023_0001
其中 , ^7 / 3a表示所述 激活值梯 度。 例如 , 采用上述激活值梯 度公式计 算激活值梯 度, 有利于减小量 化误 差。 步骤 S330: 对更新的神经网络模 型的参数 进行量化 , 以得到更新的量 化模 型, 以及对更新的 量化模型进 行尺度 变换处理, 以得到更新的目标量 化模 型。 例如 , 目标量化模型的训练过程 的标准量化 阶段的操 作,即步骤 S330, 可 以参考前述 步骤 S100和步骤 S200的相关表 述, 在此不再重复赘 述。 例如 , 通过上述步骤 S310至步骤 S330对目标量化 模型进行 训练, 可 以提 高目标量 化模型的 精度, 并改善目标量 化模型的 性能。 需要说 明的是 , 在目标量化模型的 训练过程 中, 并不直接对目标量 化 模型 的参数 (包括线性层的标准量 化权值参数 ) 进行更新, 而是通过对神 经 网络模型的 参数进行修 正, 再进行量化和 尺度变换处 理, 从而实现目标 量化 模型的参 数的更新 。 需要说 明的是 , 与基于线性层的预备 权值参数 的标准差 计算线性层 的 尺度 变换参数 (即采用第三尺度 变换参数计 算公式或 第二尺度 变换参数计 算公 式计算性层 的尺度 变换参数) 相比, 基于线性层的输出神经 元的数量 计算 线性层的尺 度变换参 数 (即采用第一尺度变换参数 计算公式 或第二尺 度 变换参 数计算 公式计 算性层 的尺 度变换 参数) 的过程中不 需要计 算 VAF^ V\j(,因此可以减少运算量,从而有利于加快目标量化 模型的训 练速度。 需要说 明的是 , 在一些示例中, 目标量化模型可以不 存储线性层 的标 准量 化权值参数 , 而是存储线性层 的量化权值 参数和尺 度变换参数 , 从而 减少 目标量化模 型的尺 寸(即占用的 存储空间)。在应用该 目标量化模 型进 行数 据处理时 , 可以通过线性层的 量化权值参 数和尺度 变换参数 计算得到 线性层 的标准量 化权值参 数, 或者, 还可以先通过线性 层的量化权 值参数 对线性 层的输入 进行处理得 到线性层 的输出, 再用尺度变换参数对 线性层 的输 出进行处理 , 本公开的实施例 对此不作 限制。 例如, 相应地, 目标量 化模 型可以不 存储目标量 化模型 中的线性层 (例如全连接层) 的偏置, 而 是存 储量化模 型中的线性 层 (例如全连接层) 的偏置; 从而, 在应用该目 标量 化模型进行 数据处理 时, 可以通过尺度 变化参数将 量化模型 中的线性 层的 偏置转变 为目标量化模 型中的 线性层的偏 置, 或者, 还可以先通过量 化模 型中的线 性层的量化 权值参数 和量化模 型中的线性 层的偏置 对线性层 的输 入进行处理 得到线性 层的输出 , 再用尺度变换参数 对线性层的 输出进 行处 理, 本公开的实施例 对此不作 限制。 需要说 明的是 , 在实际应用中, 本公开的实施 例提供的 压缩与加速 方 法, 可以根据实际需要 , 选择性地 (例如, 二者择其一, 或者同时) 对神 经 网络模型的权 值参数 (即权值量化) 和激活层的输 出 (即激活值量化) 进行 量化。 需要说 明的是 , 在本公开的实施例 中, 神经网络模型及 其量化模 型均 可 以采用软件 、 硬件、 固件或其任意组合等 方式实现 , 从而执行相应的处 理过程 。 需要说 明的是 , 本公开的实施例中 , 上述神经网络模型 的压缩与加 速 方法 的流程可 以包括更 多或更少 的操作, 这些操作可以顺序执 行或并行 执 行。 虽然上文描述的神 经网络模型 的压缩与加 速方法 的流程包括 特定顺序 出现 的多个操作 , 但是应该清楚地 了解, 多个操作的顺序并不 受限制。 上 文描 述的神经 网络模型 的压缩与加 速方法可 以执行一次 , 也可以按照预 定 条件 执行多次 。 本公 开的实施 例提供 的神经 网络模型 的压缩 与加速 方法通 过对量化 模型 进行尺度 变换处理, 得到目标量化模型 , 可以提高目标量化 模型的精 度, 改善目标量化模型 的性能。 本公 开至少 一实施例 还提供 一种数据 处理 方法, 该数据处 理方法 包 括 : 采用本公开任一实 施例提供 的压缩与加 速方法得 到的目标 量化模型 对 输入 数据进行 处理, 以得到输出数 据。 例如 , 在一些示例中, 输入数据的 类型依赖 于目标量化 模型的处 理对 象 , 例如根据目标量化 模型的处理 对象的不 同, 输入数据可 以包括图像 、 文 本、 语音等。 以 ResNet、 MobileNet-Vl、 MobileNet-V2和 VGG-Net等 神经 网络模型 及其目标量 化模型 为例, 其输入数据可 以为图像 。 例如 , 输出数据可以表示 目标量化 模型对输 入数据进行 推理预测 的结 果 。 以 ResNet、 MobileNet-Vl、 MobileNet-V2和 VGG-Net等神经网络模 型及 其目标量 化模型为例 , 其输出数据可 以表示对 图像 (即输入数据) 的 分 类结果。 例如 , 在一些示例中, 目标量化模型可 以部署在智 能手机 、 平板电脑、 车载 导航仪等移 动端和嵌 入式系统 中, 从而该移动端和 嵌入式 系统等可 以 执行 上述数据 处理方法 。 以下 , 以 MobileNet-Yl神经网络模型和 MobileNet-Y2神经网络模型 为例 , 通过表 1-2示例性地示 出了不 同比特位宽 下的量化 方案精度 对比。 表 1为针对 MobileNet-Vl和 MobileNet-Y2的不同比特位宽 (即量化比特 位 的数量) 下的量化方案精度 对比表 (对权值和激活值 进行量化 ); 表 2 为针 对 MobileNet-Vl 和 MobileNet-Y2的不同比特位宽下的量化 方案精度 对比 表 (对权值进行量化, 不对激活值进行量 化)。 需要说 明的是 , 在表 1-2中, PACT(parameterized clipping activation)、
HAQ (hardware-aware automated quantization)、 Deep Compression为已 口 的量 化方案, SAT为本公开的 实施例提 供的量化 方案(即压缩与加速 方法), 其 中, 线性层的尺度变换 参数是基 于线性层 的输出神经 元的数量 计算得到 的 (采用第三尺度变换 参数计算公 式)。 还需要说明的是, HAQ 方案 的比 特位 宽是灵活 变化的 (flexible), 因此表 1-2中的 HAQ方案的比特位宽是 等效 位宽, 例如, 其等效位宽分别 为 2、 3、 4、 5、 6、 8等, 从而可以与对 应的 比特位宽 下的其他量 化方案进 行精度对 比。 另外, 在表 1-2中, FP表 示对 应的全精 度模型; Acc.-l表示模型输 出的一种候 选类别是输 入图像的 正确 类别的概 率, Acc.-5表示模型输出的 五种候选类 别包括输入 图像的正 确 类别的概率 。例如, PACT方案的具体细 节可以参 见文献, Jungwook Choi, Zhuo Wang, Swagath Yenkataramani, Pierce I-Jen Chuang, Yijayalakshmi Srmivasan, and Kailash Gopalakrishnan. PACT: Parameterized Clipping Activation for Quantized Neural Networks, arXiv: 1805.06085, 2018; HAQ方 案的 具体细节 可以参见文 献, Kuan Wang, Zhijian Liu, Yujun Lin, Ji Lin, and Song Han. HAQ: Hardware- Aware Automated Quantization with Mixed Precision, arXiv: 1811.08886, 2019; Deep Compression方案的具体细节可以 参 见文献, Song Han, Huizi Mao, and William J Dally. Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding. arXiv: 1510.00149, 2015。 在此将上述文献全文引用结合于 此 , 以作为本公开的一部 分。 表 1. 针对 MobileNet-V 1 和 MobileNet-V2的不同比特位宽下的 量化方案精度对比表 (对权值和激活值进行量化)
MobileNet-V 1 MobileNet- V2 量化方案 比特位宽 Acc.-l Acc.-5 Acc.-l Acc.-5
PACT 4bit^ 703 S92 704 89.4
HAQ flexible 67.40 87.90 66.99 87.33
SAT 4bits 71.3 89.9 71.1 90.0
PACT 5bit^ 7U 806 7L2 89.8
HAQ flexible 70.58 89.77 70.90 89.91
SAT 5bits 71.9 90.3 72.0 90.4
PACT 6bit^ TL2 S92 7L5 90.0
HAQ flexible 71.20 90.19 71.89 90.36
SAT 6bits 72.3 90.4 72.3 90.6
PACT 8bit^ 7L3 S9J 7L7 89.9
HAQ flexible 70.82 89.85 71.81 90.25
SAT 8bits 72.6 90.7 72.5 90.7
PACT FP 12A 902 711 90.5
SAT FP 71.7 90.2 71.8 90.2 表 2. 针对 MobileNet-V 1 和 MobileNet- V2的不同比特位宽下的 量化方案精度对比表 (对权值进行量化, 不对激活值进行量化)
MobileNet-Vl MobileNet- V2
Quant. Method Weights Acc.-l Acc.-5 Acc.-l Acc.-5
Deep Compression 2bits 37.62 64.31 58.07
Figure imgf000026_0001
HAQ flexible 57.14 81.87 66.75
Figure imgf000026_0002
SAT 2bits 66.3 86.8 66.8
Figure imgf000026_0003
Deep Compression 3bits 65.93 86.85 68.00 87.96
HAQ flexible 67.66 88.21 70.90 89.76
SAT 3bits 70.7 89.5 71.1 89.9
Deep Compression 4bits 71.14 89.84 71.24 89.93
HAQ flexible 71.74 90.36 71.47 90.23
SAT 4bits 72.1 90.2 72.1 90.6
Deep Compression FP 70.90 89.90 71.87 90.32 HAQ FP 70.90 89.90 71.87 90.32
SAT FP 71.7 90.2 71.8 90.2 根据表 1-2可知, 采用本公开 的实施例提 供的压缩 与加速方 法得到的 目标 量化模型 的精度在 大部分情 况下均高 于其他几种 已知的量 化方案得 到 的量 化模型, 由此说明, 本公开的实施例提 供的压缩 与加速方 法可以提 高 目标 量化模型 的精度, 改善目标量化模型 的性能。 本公 开的实施 例提供 的数据 处理方 法的技 术效果可 以参考 上述实 施 例 中关于神经 网络模型的 压缩与加 速方法的 相应描述 , 在此不再赘述。 本公开 至少一实 施例还提供 一种数据 处理装置 。 图 9为本公开至少一 实施 例提供的 一种数据 处理装置的 示意性框 图。 例如 ,如图 9所示,该数据处理装置 500包括存储器 510和处理器 520 o 例如 , 存储器 510用于非暂时 性存储计算 机可读指 令, 处理器 520用于运 行该 计算机可读 指令, 该计算机可 读指令被处 理器 520运行时执行本公 开 任一 实施例提供 的神经 网络模型的 压缩与加 速方法或 /和数据处理方 法。 例 如, 存储器 510和处理器 520之间可以直接 或间接地 互相通信 。 例 如, 在一些示例中, 如图 9所示, 该数据处理 装置 500还可 以包括系统 总 线 530, 存储器 510和处理器 520之间可 以通过系统总 线 530互相通 信, 例如 , 处理器 520可以通过系统 总线 1006访问存储器 510。 例如, 在另一 些示 例中, 存储器 510和处理 器 520等组件之 间可以通 过网络连接 进行通 信。 网络可以包括无线 网络、 有线网络、 和 /或无线网络和有线网络的任意 组合 。 网络可以包括局域 网、 互联网、 电信网、 基于互联网和 /或电信网的 物联 网 ( Internet of Things )、 和 /或以上网络的任意组合等。 有线网络例如 可 以采用双绞 线、 同轴电缆或光纤传 输等方式 进行通信 , 无线网络例如可 以采 用 3G/4G/5G移动通 信网络、 蓝牙、 Zigbee或者 WiFi等通信方式。 本 公开 对网络的 类型和功能 在此不作 限制。 例如 , 处理器 520可以控制数据 处理装置 中的其 它组件以执 行期望的 功能 。 处理器 520可以是中央 处理单元 (CPU)、 张量处理器 (TPU) 或者 图形 处理器 GPU 等具有数据 处理能 力和 /或程序执行能力的器 件。 中央处 理器 (CPU) 可以为 X86或 ARM 架构等。 GPU 可以单独 地直接集 成到主 板上 , 或者内置 于主板 的北桥 芯片 中。 GPU 也可以内置 于中央 处理器 (CPU) 上。 例如 , 存储器 510可以包括一 个或多个 计算机程序 产品的任 意组合, 计算 机程序产 品可以包括 各种形 式的计算机 可读存储介 质, 例如易失性存 储器 和 /或非易失性存储 器。 易失性存储 器例如可 以包括 随机存取 存储器 (RAM ) 和 /或高速缓冲存储器 (cache) 等。 非易失性存储器例如可以包 括只 读存储器 (ROM)、 硬盘、 可擦除可编程只读 存储器 (EPROM)、 便 携式 紧致盘只读 存储器 (CD-ROM)、 USB存储 器、 闪存等。 例如 , 在存储器 510上可 以存储一个 或多个计算 机指令 , 处理器 520 可 以运行所述 计算机指 令, 以实现各种功能 。 在计算机可读存储 介质中还 可 以存储各种应 用程序和 各种数据 , 例如线性层的预备 权值参数 、 线性层 的标 准量化权值 参数、 线性层的尺度变换参数 、 激活值参数以及 应用程序 使 用和 /或产生的各种数据等 。 例如 , 存储器 510存储的一些 计算机指令 被处理器 520执行时可以 执 行根 据上文所 述的压缩 与加速方 法中的一个 或多个 步骤。 又例如, 存储器 510 存储的另 一些计算机 指令被 处理器 520执行时可以执 行根据上 文所述 的数 据处理方 法中的一 个或多个 步骤。 例如 , 如图 9所示, 数据处理装置 500还可以包括允 许外部设备 与数 据处 理装置 500进行通信的输 入接 口 540。 例如, 输入接口 540可被用于 从外 部计算机设 备、 从用户等处接 收指令。 数据处理装 置 500还可以 包括 使数 据处理装 置 500和一个 或多个外 部设备相 互连接的输 出接口 550。 例 如, 数据处理装置 500可以通 过输出接 口 550显示图像 等。 通过输入接口 1010 和输出接 口 1012与数据 处理装置 500通信的外部 设备可被 包括在提 供任 何类型的 用户可与之 交互的 用户界面的 环境中。 用户界面类型的示例 包括 图形用户界 面、 自然用户界面等。 例如, 图形用户界面可接 受来 自用 户采 用诸如键 盘、 鼠标、 遥控器等之类的 (诸)输入设备的输入, 以及在诸 如显 示器之类 的输出设备 上提供输 出。 此外, 自然用户界面可使 得用户能 够 以无需受到 诸如键盘、 鼠标、 遥控器等之类的输入设 备强加的 约束的方 式来 与数据处 理装置 500交互。 相反, 自然用户界面可依赖 于语音识 别、 触摸 和指示笔 识别、 屏幕上和屏幕 附近的手 势识别、 空中手势、 头部和眼 睛跟 踪、 语音和语音、 视觉、 触摸、 手势、 以及机器智能等。 另外 , 数据处理装置 500尽管在图 9中被示 出为单个 系统, 但可以理 解, 数据处理装置 500也可 以是分布式 系统, 还可以布置 为云设施 (包括 公有 云或私有 云) 。 因此, 例如, 若干设备可以通过网络连接进 行通信并 且可 共同执行被 描述为 由数据处理 装置 500执行的任务。 例如 , 关于压缩与加速方 法的处理过 程的详细说 明可以 参考上述压 缩 与加 速方法的 实施例 中的相关描述 , 关于数据处理方 法的处理 过程的详细 说明 可以参考 上述数据 处理方法 的实施例 中的相关描 述, 重复之处不再赘 述。 例如 , 在一些示例中, 该数据处理 装置可 以包括但不 限于智能 手机、 平板 电脑、 车载导航仪等 移动端和嵌 入式系统 。 需要说 明的是 , 本公开的实施例提供 的数据 处理装置是 示例性的 , 而 非限 制性的, 根据实际应用需要 , 该数据处理装置还 可以包括其 他常规部 件或 结构, 例如, 为实现数据处理 装置的必 要功能, 本领域技术人员可 以 根据 具体应用 场景设置其 他的常规 部件或结 构, 本公开的实施例 对此不作 限制 。 本公 开的实施 例提供 的数据 处理装 置的技 术效果可 以参考 上述实 施 例 中关于压缩 与加速方 法以及数据 处理方法 的相应描 述, 在此不再赘述 。 本公开 至少一实 施例还提供 一种存储 介质。 图 10 为本公开一实施例 提供 的一种存 储介质的示 意图。例如 , 如图 10所示, 该存储介质 600非暂 时性 地存储计算 机可读指 令 601, 当非暂时性计算机 可读指令 601 由计算 机 (包括处理器) 执行时可以执行 本公开任 一实施例提 供的压缩 与加速方 法的 指令或者 可以执行 本公开任一 实施例提 供的数据 处理方法 的指令。 例如 , 在存储介质 600上可以 存储一个或 多个计算 机指令。 存储介质 600 上存储的一些计 算机指令 可以是例 如用于实现 上述压缩 与加速方 法中 的一 个或多个 步骤的指 令。 存储介质上存储 的另一些 计算机指令 可以是例 如 用于实现上 述数据处理 方法中的 一个或 多个步骤的 指令。 例如 , 存储介质可以包括 平板电脑 的存储部件 、 个人计算机的硬 盘、 随机 存取存储 器 (RAM) 、 只读存储器 (ROM) 、 可擦除可编程只读存 储器 (EPROM) 、 光盘只读存储器 (CD-ROM) 、 闪存、 或者上述存储介 质的任 意组合 , 也可以为其他适 用的存储介 质。 本公 开的实施 例提供 的存储 介质的技 术效果 可以参 考上述 实施例 中 关于压 缩与加速 方法以及 数据处理 方法的相 应描述, 在此不再赘 述。 对于 本公开, 有以下几点 需要说明 : ( 1) 本公开实施例附 图中, 只涉及到与本 公开实施例 涉及到的 结构, 其他 结构可参考 通常设计 。
(2) 在不冲突的情 况下, 本公开同一 实施例及 不同实 施例中的 特征 可 以相互组合 。 以上 , 仅为本公开的具体 实施方式 , 但本公开的保护范 围并不局 限于 此, 任何熟悉 本技术领域 的技术人 员在本公 开揭露的技 术范围 内, 可轻易 想到 变化或替换 , 都应涵盖在本公 开的保护 范围之内。 因此, 本公开的保 护范 围应以权利 要求的保 护范围为准 。

Claims

权利要求 书
1、 一种神经 网络模型的 压缩与加 速方法 , 所述神经网络模 型包括 线 性层 , 所述神经网络模型 的参数 包括预备权值 参数, 所述压缩与加 速方法 包括 : 对所述 神经网络模 型的参 数进行量化 , 以得到量化模型 , 所述量化模 型的 参数包括 所述线性层 的量化权 值参数 ; 以及 对所述 量化模型 进行尺度 变换处理 , 以得到目标量化模 型; 其中 , 对所述量化模型进 行所述尺 度变换处理 , 包括: 基于 所述线 性层的输 出神经 元的数量 或所述 线性层 的预备权 值参数 的标 准差, 计算所述线性 层的尺度 变换参数 ; 以及 基于所 述线性层 的尺度 变换参数, 对所述线性 层的量化权 值参数进 行 所述 尺度变换 处理, 以得到所述 线性层的标 准量化权值 参数。
2、 根据权利要求 1 所述的压缩与加速方 法, 其中, 所述线性层包括 从卷 积层、 递归层和全连接 层组成的 群组中选择 的至少一 个。
3、 根据权利要求 1 或 2所述的压 缩与加速 方法, 其中, 所述线性层 未被 批量归一 化层直接跟 随。
4、 根据权利要求 1-3任一项所述的压 缩与加速 方法, 其中, 对所述神 经 网络模型的 参数进行量 化, 以得到所述量 化模型, 包括: 对所述 线性层的 预备权值参 数进行钳位 处理, 以得到所述线性层的钳 位权值 参数; 以及 对所述 线性层的 钳位权值参 数进行量 化处理, 以得到所述线性层的 量 化权值 参数。
5、 根据权利要求 4 所述的压缩与加速方 法, 其中, 基于所述线性层 的输 出神经元 的数量, 计算所述线性层的尺 度变换参数 , 包括: 根据 第一尺度 变换参数计 算公式计算 所述线性 层的尺度 变换参数 , 所 述 第一尺度 变换参数计算 公式表示 为:
Figure imgf000031_0001
其中 , RSF表示所述 线性层的 尺度变换参 数, A表示所 述线性层 的输 出神 经元的数量 , Q表示所述 线性层的量 化权值矩 阵, VAF{ Q表示所述线 性层 的量化权值 矩阵的元 素的方差 。
6、 根据权利要求 5 所述的压缩 与加速方 法, 其中, 所述线性层的量 化权值 参数的 比特位的位 数为 1-8。
7、 根据权利要求 6 所述的压缩 与加速方 法, 其中, 所述线性层的量 化权值 参数的 比特位的位 数为 1-2。
8、 根据权利要求 4 所述的压缩 与加速方 法, 其中, 基于所述线性层 的输 出神经元 的数量, 计算所述线 性层的尺度 变换参数 , 包括: 根据 第二尺度 变换参数计 算公式计算 所述线性 层的尺度 变换参数 , 所 述第 二尺度变换 参数计算 公式表示 为:
1
RSF
Vn - VARVV 其中 , RSF表示所述 线性层的 尺度变换参 数, A表示所 述线性层 的输 出神 经元的数量 , W表示所述线 性层的辅助 权值矩阵 , VAR 表示 所述线 性层 的辅助权值 矩阵的元 素的方差 ; 所述 线性层的辅助 权值矩 阵 表示为: W= 2W- 1 其中 , W表示所述 线性层的钳 位权值矩 阵。
9、 根据权利要求 4 所述的压缩 与加速方 法, 其中, 基于所述线性层 的预备 权值参数 的标准差 , 计算所述线性层的 尺度变换参 数, 包括: 根据 第三尺度 变换参数计 算公式计算 所述线性 层的尺度 变换参数 , 所 述第 三尺度变换 参数计算 公式表示 为:
VAR VV
RSF =
V VAR VV 其中 , RSF表示所述 线性层的 尺度变换参 数, W表示所 述线性层 的预 备权值 矩阵, VAR V\j(表示所述线性层的预备权值矩阵的元 素的方差 , 表 示所 述线性层 的辅助权值矩 阵, VAR 表示 所述线性层 的辅助权值 矩阵的 元素 的方差 ; 所述 线性层的辅助 权值矩 阵 表示为:
W= 2W- 1 其中 , w表示所述 线性层的钳 位权值矩 阵。
10、 根据权利要求 8或 9所述的压 缩与加速 方法, 其中, 所述线性层 的量 化权值参数 的比特位 的位数 为 1-8。
11、 根据权利要求 10所述的压缩与加 速方法 , 其中, 所述线性层的量 化权值 参数的 比特位的位 数为 3-8。
12、 根据权利要求 5-11任一项所述的压缩 与加速方 法, 其中, 基于所 述线 性层的尺度 变换参数 , 对所述线性层的量 化权值参数 进行所述 尺度变 换处理 , 以得到所述线性 层的标准 量化权值参 数, 包括: 根据 尺度变换 公式对 所述线 性层的量 化权值 参数进行 所述尺 度变换 处理 , 所述尺度变换公式 表示为 : cfj = RSF qj 其中 , Cf表示所述线性 层的标准 量化权值矩 阵, Cfj 表示所述线性层 的标 准量化权值 矩阵的第 i行第 j列的参数, Q表示所述线性 层的量化权 值 矩阵 , Qj表示所 述线性层 的量化权值 矩阵的第 i行第 j列的参数。
13、 根据权利要求 4-12任一项所述的压 缩与加速 方法, 其中, 对所述 线性层 的预备权值 参数进行 所述钳位 处理, 以得到所述 线性层的钳 位权值 参数 , 包括: 根据钳 位公式对 所述线性层 的预备权值 参数进行 所述钳位 处理, 所述 钳位公 式表示 为: t anh( V)( W = max It anh(
_
Figure imgf000033_0001
其中 , W表示所述线性层 的钳位权值 矩阵, %表示 所述钳位 权值矩阵 的第 i行第 j列的参数, W表示 所述线性层 的预备权值 矩阵, %表示所述 线性层 的预备权值 矩阵的 第 i行第 j列的参数, ^ 表示 所述线性 层的预备 权值矩 阵的第 m行第 n列的参数, t anh( .)表示双曲正切函数, max( .)表 示取 最大值函数 。
14、 根据权利要求 13 所述的压缩 与加速方 法, 其中, 对所述线性层 的钳位 权值参数 进行所述 量化处理 , 以得到所述线性层 的量化权值 参数, 包括 : 根据权 值量化公 式对所述 线性层的钳 位权值参数 进行所述 量化处理 , 所述 权值量化公 式表示 为:
Qj = -^― r ound( ( 2^ - 1)V^ ) - 1 2 1 其中 , Q表示所 述线性层 的量化权 值矩阵, 4 表示所 述线性层 的量 化权值 矩阵的 第 i行第 j列的参数, b表示所述线性 层的量化 权值参数 的比 特位 的位数, r ound(.)表示四舍五入函数。
15、 根据权利要求 4-14任一项所述的压 缩与加速 方法, 还包括: 采用 与所述神经 网络模型 相同的训 练参数配置 , 对所述目标量化模 型 进行 训练。
16、 根据权利要求 15 所述的压缩 与加速方 法, 其中, 所述目标量化 模型 的训练过程 包括: 前向传播阶 段、 后向传播阶段和 标准量化 阶段; 所述前 向传播 阶段包括 : 使用当前的目标量化 模型对训 练输入数据 进 行处 理, 以得到训练输 出数据, 并基于所述 训练输出数 据计算损 失值; 所述后 向传播 阶段包括 : 基于所述损失值, 计算梯度, 并基于所述梯 度对 当前的神 经网络模型 的参数进 行修正, 以得到更新的神经 网络模型 ; 所述标 准量化 阶段包括 : 对更新的神经网络模 型的参数 进行量化 , 以 得到 更新的量 化模型, 以及对所述更新的量 化模型进 行尺度 变换处理, 以 得到 更新的 目标量化模 型。
17、 根据权利要求 16 所述的压缩 与加速方 法, 其中, 所述神经网络 模型 包括激活 层, 所述激活层包括 PACT激 活函数 , 所述 PACT激活 函数 表示 为:
Figure imgf000034_0001
其中 , X表示所述 激活层的输 出, X表示所 述激活层 的输入, dr表示 所述 PACT激 活函数的 激活值参数 ; 对所述 神经网络模 型的参 数进行量化 , 以得到所述量化模型 ,还包括: 根据激 活值量化 公式对所 述激活层 的输出进行 所述量化 处理, 所述激 活值 量化公式 表示为: q ound( ( 2s - 1) — )
Figure imgf000034_0002
a 其中 , q表示所述 激活层的 输出的量化 值, a 表示所述激活 层的输出 的量 化值的比 特位的位 数, r ound( -)表示四舍五入函数。
18、 根据权利要求 17 所述的压缩 与加速方 法, 其中, 所述后向传播 阶段 还包括: 根据激 活值梯度 公式计算 激活值梯 度, 并基于所述激 活值梯度对 当前 的激 活值参数进 行修正 , 以得到更新的激 活值参数, 式表示为 : ound(
Figure imgf000035_0002
Figure imgf000035_0001
Figure imgf000035_0003
其中 , dq / da表示所述 激活值梯 度。
19、 根据权利要求 15-18任一项所述 的压缩与加 速方法 , 其中, 所述 训练 参数配置 包括: 初始学习率 、 学习率调整方案、 权值衰减、 训练集的 迭代 次数、 优化器和批 尺寸。
20、 根据权利要求 1-19任一项所述的压 缩与加速 方法, 其中, 在对所 述神 经网络模型 的参数进 行量化前 , 所述压缩与加速 方法还包括 : 对所述 神经网络模 型进行预 训练, 以得到所述神经网络模 型的预备 权 值参 数。
21、 根据权利要求 20 所述的压缩 与加速方 法, 其中, 对所述神经网 络模 型进行所 述预训练, 包括: 采用 恺明初始化 方案对所 述神经 网络模型的参 数进行初 始化。
22、 根据权利要求 1-21任一项所述的压 缩与加速 方法, 其中, 所述神 经 网络模型包括 ResNet、 MobileNet-Vl、 MobileNet-V2和 YGG-Net之一。
23、 一种数据处理 方法, 包括: 采用权 利要求 1-22 任一项所述的压 缩与加速 方法得 到的所述 目标量 化模 型对输入数 据进行处 理。
24、 一种数据处理 装置, 包括: 存储器 , 用于非暂时性存 储计算机 可读指令 ; 以及 处理器 , 用于运行计算机 可读指令 ; 其 中, 所述计算机 可读指令 被所述 处理器 运行时执 行根据 权利要 求 1-22任 一项所述 的压缩与加 速方法或 执行根据 权利要求 23所述 的数据处 理方 法。
25、 一种存储介质 , 非暂时性地存储计算 机可读指 令, 其中, 当所述 非暂 时性计算机 可读指令 由计算机 执行时可 以执行根 据权利要 求 1-22 任 一项 所述的压缩 与加速方 法的指令 或者可 以执行根据权 利要求 23 所述的 数据 处理方法 的指令。
PCT/IB2019/059565 2019-09-20 2019-11-07 神经网络模型的压缩与加速方法、数据处理方法及装置 WO2021053381A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201910893276.XA CN110659725B (zh) 2019-09-20 2019-09-20 神经网络模型的压缩与加速方法、数据处理方法及装置
CN201910893276.X 2019-09-20

Publications (1)

Publication Number Publication Date
WO2021053381A1 true WO2021053381A1 (zh) 2021-03-25

Family

ID=69038294

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/IB2019/059565 WO2021053381A1 (zh) 2019-09-20 2019-11-07 神经网络模型的压缩与加速方法、数据处理方法及装置

Country Status (2)

Country Link
CN (1) CN110659725B (zh)
WO (1) WO2021053381A1 (zh)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113920720A (zh) * 2021-09-17 2022-01-11 上海吞山智能科技有限公司 高速公路隧道设备故障处理方法、装置及电子设备
WO2024060002A1 (zh) * 2022-09-20 2024-03-28 华为技术有限公司 通信方法以及相关装置

Families Citing this family (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113222098A (zh) * 2020-01-21 2021-08-06 上海商汤智能科技有限公司 数据处理方法和相关产品
CN111783976B (zh) * 2020-04-21 2023-04-21 北京大学 一种基于窗口梯度更新的神经网络训练过程中间值存储压缩方法和装置
CN111967608A (zh) * 2020-08-06 2020-11-20 北京灵汐科技有限公司 数据处理方法、装置、设备及存储介质
CN112085195B (zh) * 2020-09-04 2022-09-23 西北工业大学 一种基于x-admm的深度学习模型环境自适应方法
CN112598020A (zh) * 2020-11-24 2021-04-02 深兰人工智能(深圳)有限公司 目标识别方法及系统
CN113469324B (zh) * 2021-03-23 2024-03-22 中科创达软件股份有限公司 模型动态量化方法、装置、电子设备和计算机可读介质
CN113537340A (zh) * 2021-07-14 2021-10-22 深圳思悦创新有限公司 一种yolo目标检测模型压缩方法、系统及存储介质
CN113642710B (zh) * 2021-08-16 2023-10-31 北京百度网讯科技有限公司 一种网络模型的量化方法、装置、设备和存储介质

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20160328646A1 (en) * 2015-05-08 2016-11-10 Qualcomm Incorporated Fixed point neural network based on floating point neural network quantization
US20170286830A1 (en) * 2016-04-04 2017-10-05 Technion Research & Development Foundation Limited Quantized neural network training and inference
US20180247180A1 (en) * 2015-08-21 2018-08-30 Institute Of Automation, Chinese Academy Of Sciences Deep convolutional neural network acceleration and compression method based on parameter quantification
US20190114511A1 (en) * 2017-10-16 2019-04-18 Illumina, Inc. Deep Learning-Based Techniques for Training Deep Convolutional Neural Networks

Family Cites Families (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107480770B (zh) * 2017-07-27 2020-07-28 中国科学院自动化研究所 可调节量化位宽的神经网络量化与压缩的方法及装置
US11295208B2 (en) * 2017-12-04 2022-04-05 International Business Machines Corporation Robust gradient weight compression schemes for deep learning applications
CN108334945B (zh) * 2018-01-30 2020-12-25 中国科学院自动化研究所 深度神经网络的加速与压缩方法及装置
CN109840589B (zh) * 2019-01-25 2021-09-24 深兰人工智能芯片研究院(江苏)有限公司 一种在fpga上运行卷积神经网络的方法和装置
CN110096647B (zh) * 2019-05-10 2023-04-07 腾讯科技(深圳)有限公司 优化量化模型的方法、装置、电子设备及计算机存储介质

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20160328646A1 (en) * 2015-05-08 2016-11-10 Qualcomm Incorporated Fixed point neural network based on floating point neural network quantization
US20180247180A1 (en) * 2015-08-21 2018-08-30 Institute Of Automation, Chinese Academy Of Sciences Deep convolutional neural network acceleration and compression method based on parameter quantification
US20170286830A1 (en) * 2016-04-04 2017-10-05 Technion Research & Development Foundation Limited Quantized neural network training and inference
US20190114511A1 (en) * 2017-10-16 2019-04-18 Illumina, Inc. Deep Learning-Based Techniques for Training Deep Convolutional Neural Networks

Non-Patent Citations (4)

* Cited by examiner, † Cited by third party
Title
CHENG YU; WANG DUO; ZHOU PAN; ZHANG TAO: "Model Compression and Acceleration for Deep Neural Networks", IEEE SIGNAL PROCES SING MAGAZINE, 9 January 2018 (2018-01-09), pages 126 - 136, XP011675813, Retrieved from the Internet <URL:https://www.gwern.net/docs/ai/2018-cheng.pdf> [retrieved on 20200914], DOI: 10.1109/MSP.2017.2765695 *
HAN: "Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding", ARXIV: 1510.00149V5 [CS.CV, 15 February 2016 (2016-02-15), pages 1 - 15, XP002798215, Retrieved from the Internet <URL:https://arxiv.org/pdf/1510.00149.pdf> [retrieved on 20200914] *
JUNG SANGIL, CHANGYONG SON; SEOHYUNG LEE; JINWOO SON; YOUNGJUN KWAK; JAE-JOON HAN; SUNG JU HWANG; CHANGKYU CHOI: "Learning to Quantize Deep Networks by Optimizing Quantization Intervals with Task Loss", ARXIV: 1808.05779V3 [CS.CV, 23 November 2018 (2018-11-23), pages 1 - 10, XP081040765, Retrieved from the Internet <URL:https://arxiv.org/pdf/1808.05779.pdf> [retrieved on 20200914] *
WANG KUAN; LIU ZHIJIAN; LIN YUJUN; LIN JI; HAN SONG: "HAQ: Hardware-Aware Automated Quantization with Mixed Precision", ARXIV: 1811.08886V3 [CS.CV, 15 June 2019 (2019-06-15), pages 1 - 10, XP033687500, Retrieved from the Internet <URL:https://arxiv.org/pdf/1811.08886.pdf> DOI: 10.1109/CVPR.2019.00881 *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113920720A (zh) * 2021-09-17 2022-01-11 上海吞山智能科技有限公司 高速公路隧道设备故障处理方法、装置及电子设备
WO2024060002A1 (zh) * 2022-09-20 2024-03-28 华为技术有限公司 通信方法以及相关装置

Also Published As

Publication number Publication date
CN110659725B (zh) 2023-03-31
CN110659725A (zh) 2020-01-07

Similar Documents

Publication Publication Date Title
WO2021053381A1 (zh) 神经网络模型的压缩与加速方法、数据处理方法及装置
CN110852439B (zh) 数据处理方法及装置、存储介质
US10678508B2 (en) Accelerated quantized multiply-and-add operations
US20210089922A1 (en) Joint pruning and quantization scheme for deep neural networks
US11307864B2 (en) Data processing apparatus and method
Paszke et al. Enet: A deep neural network architecture for real-time semantic segmentation
US20190050733A1 (en) Reducing the size of a neural network through reduction of the weight matrices
US20200104573A1 (en) Data processing apparatus and method
CN111095302A (zh) 稀疏深度卷积网络权重的压缩
WO2022156561A1 (zh) 一种自然语言处理方法以及装置
WO2022228425A1 (zh) 一种模型训练方法及装置
WO2023231794A1 (zh) 一种神经网络参数量化方法和装置
US20210005183A1 (en) Orthogonally constrained multi-head attention for speech tasks
WO2021042857A1 (zh) 图像分割模型的处理方法和处理装置
CN113128671B (zh) 一种基于多模态机器学习的服务需求动态预测方法及系统
WO2023051369A1 (zh) 一种神经网络的获取方法、数据处理方法以及相关设备
CN114266897A (zh) 痘痘类别的预测方法、装置、电子设备及存储介质
WO2021253941A1 (zh) 神经网络模型训练、图像分类、文本翻译方法及装置、设备
CN115759237A (zh) 端到端的深度神经网络模型压缩及异构转换系统及方法
CN109754357B (zh) 图像处理方法、处理装置以及处理设备
WO2024046144A1 (zh) 一种视频处理方法及其相关设备
WO2020187029A1 (zh) 图像处理方法及装置、神经网络的训练方法、存储介质
CN117151178A (zh) 一种面向fpga的cnn定制网络量化加速方法
WO2023115814A1 (zh) Fpga硬件架构及其数据处理方法、存储介质
WO2023109748A1 (zh) 一种神经网络的调整方法及相应装置

Legal Events

Date Code Title Description
NENP Non-entry into the national phase

Ref country code: DE

121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 19945532

Country of ref document: EP

Kind code of ref document: A1

122 Ep: pct application non-entry in european phase

Ref document number: 19945532

Country of ref document: EP

Kind code of ref document: A1