# Robust Learning from Demonstration Based on GANs and Affine Transformation
## 3 Proposed Method
### Trajectory Learning Based on GANs  
GANs can learn trajectory representations from multiple demonstrations, enabling the generator to generate trajectories similar to the demonstrations, but they are highly sensitive to noise present in the trajectories. GANs demonstrate strong fitting capabilities for noise and non-smooth parts of the trajectories, resulting in trajectories with high jerk. Additionally, the discriminator cannot quantitatively describe the magnitude of trajectory errors. When the discriminator loss reaches a certain value, it fails to provide sufficient gradients for updating the generator, potentially leading to model collapse. To address these issues, this paper introduces a new loss function that combines DILATE [23] loss and jerk measurement, integrating traditional trajectory planning and sequence similarity metrics, building upon the loss function of GANs. Denote the trajectory generated by the generator as \(x\) and the ground truth trajectory as \(y\), and the equations for these new loss functions are as follows:  
\[ \begin{array}{c}L_{DILATE}(x,y)=\beta\cdot L_{shape}(x,y)+(1-\beta)\cdot L_{ temporal}(x,y),\\ L_{lerk}=\int_{0}^{t}\left|\vec{x}\right|\right|^{2}dt,\end{array}  \tag{13}\]  
where \(\vec{x}\) is the third derivative of trajectory \(x\), \(\beta\) is a hyperparameter used to balance the weights between shape and temporal, and \(t\) represents the duration of the motion, under discrete conditions (i.e., the number of trajectory points). \(L_{DILATE}\) consists of both shape loss and temporal loss. The shape loss adopts Soft-DTW [30] to ensure differentiability which is defined as follows:  
\[L_{shape}=DTW_{\gamma}(x,y)=-\gamma log\left(\sum_{A\in A_{n,m}}exp\left(- \frac{\left\langle A\left|\Delta(x,y)\right\rangle\right.}{\gamma}\right) \right), \tag{14}\]  
where \(A_{n,m}\) is the alignment matrices of \(x\) and \(y\), \(\left\langle x|y\right\rangle\) denotes the inner product operation, \(\Delta(x,y)\) is the cost matrix of \(x\) and \(\gamma\) where cost function can be replaced with Euclidian distance. Soft-DTW apply soft-minimum algorithm with a hyperparameter \(\gamma\) to replace the minimum function which make the shape loss differentiable. On the other hand, DILATE models the temporal loss, calculating the temporal error of the trajectory points using the optimal transition matrix \(A_{\gamma}^{*}\) obtained from Soft-DTW. The algorithm is defined as follows:  
\[L_{temporal}(x,y)=\left\langle A_{\gamma}^{*}\left|\Omega\right\rangle, \tag{15}\]where \(\Omega\) is the temporal penalty matrix, defined as the second norm of the temporal sequence, and the function is fully differentiable. Based on this, the revised loss function for the generator aspect in GAN is defined as follows:  
\[L_{Generator}=\alpha_{1}\mathrm{V}(D,G)+\alpha_{2}L_{DILATE}+\alpha_{3}L_{ Jerk}, \tag{16}\]  
where \(\alpha_{1},\ \alpha_{2},\ \alpha_{3}\) are the loss coefficients. The discriminator loss remains unchanged. This model can function as a template library for conditional generation when guided by \(L_{Generator}\), and as a demonstration trajectory generation module when guided by \(L_{Discriminator}\) to generate additional demonstration trajectories. The GAN framework described above demonstrates strong learning capabilities, addressing the issue of current LfD algorithms lacking learning capacity. The proposed method enables rapid learning and guaranteed convergence to demonstration trajectories. Compared to current optimal teaching learning methods, it requires no hyperparameter tuning, converges more easily, and can be applied to multidimensional inputs. With the incorporation of the novel loss function, GANs can extract common features from multiple demonstration trajectories, mitigating the impact of noise when trajectories contain noise.