## 4 Error Compensation by RNN  
RNN is a set of neural networks used to process sequence data, and the long short-term memory (LSTM) unit network is used to solve the problems of long-term dependency and gradient explosion/disappearance in the early-time RNN. LSTM has made great progress in the preservation of historical information and in the prediction of future information [20]. This study will further explore the application of LSTM in collaborative robot dynamics. The kinematic and dynamic data of collaborative robots represent continuous series in time. Some errors, such as the memory effect of friction and the time-varying characteristics of motor  
Figure 4: Excitation trajectory of test set. (**a**)–(**c**) are the position, velocity, and acceleration of excitation trajectory of test set, respectively. parameters caused by temperature changing, are very suitable to be compensated by LSTM networks. In this study, LSTM cell units with cell state and gate structure are used to achieve a better compensation effect. The detail of an LSTM cell unit is shown in Figure 5, which adds a cell state vector C and related gate structure. It can better solve the problem of gradient explosion and gradient disappearance in ordinary RNN cells by controlling forgetting and memory. The forward calculation process is:  
\[\left\{\begin{array}{l} \begin{aligned} \dot{f_{t}}=\sigma\Big{(}W_{f} \cdot[h_{t-1},x_{t}]+b_{f}\Big{)}\\ \dot{t_{t}}=\sigma\big{(}W_{i}\cdot[h_{t-1},x_{t}]+b_{i}\big{)}\\ \widetilde{C_{t}}=\tanh(W_{C}\cdot[h_{t-1},x_{t}]+b_{C})\\ C_{t}=f_{t}*C_{t-1}+i_{t}*\widetilde{C_{t}}\\ o_{t}=\sigma(W_{o}[h_{t-1},x_{t}]+b_{o})\\ h_{t}=o_{t}*\tanh(C_{t})\end{aligned} \right. \tag{9}\]  
The training method of an LSTM network is Back Propagation Through Time (BPTT), which is similar to BP algorithm, including the steps of forward calculation, reverse calculation, computing gradient, and updating parameters. There are many types of gradient-based optimization algorithms. In this paper, the Adaptive Momentum Estimation (Adam) algorithm is adopted [21]. This algorithm is an effective gradient-based stochastic optimization method, which can calculate adaptive learning rates for different parameters, and takes up less storage resources. Compared with other stochastic optimization methods, the Adam algorithm performs better in most practical applications [22]. A pipeline of LSTM network-based collaborative robot dynamic compensation is depicted in Figure 6. As detailed in Figure 6, this pipeline includes LSTM network, data set generation, and network training methods. The input and output layers of the LSTM network are fully connected networks, and the hidden layer consists of LSTM cell units. The main steps are:  
1. Collect raw data (position, velocity, acceleration, torque);
2. Raw data are used by the dynamic model to predict the torque, i.e., the uncompensated predicted torque;
3. The original data of both training set and test set are normalized, including uncompensated predicted torque, actual torque and actual position;
4. The training set feeds into the fully connected input layer;
5. Iterative calculation in the hidden layer;
6. Output layer gives the predicted torque after LSTM compensation, i.e., the compensated predicted torque;  
Figure 5: Long short-term memory (LSTM) cell of hidden layer. (7) The loss is RMS error of the compensated predicted torque relative to the actual measured torque; (8) Optimize the network parameters by Adam optimizer; (9) Apply the updated weight; (10) The test set calculates the compensated predicted torque through the network and evaluates the compensation effect. The LSTM network is implemented based on Tensorflow 1.15.0, an open source machine learning platform. According to experience and actual conditions, hyper-parameters of the network are listed in Table 1.  
The input vector includes calculated torque, actual torque and the actual position of the 4 th, 5 th, and 6 th joint at each moment.