{'fraction_non_alphanumeric': 0.03698299567876985, 'fraction_numerical': 0.009203659015657444, 'mean_word_length': 4.644599303135888, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Training Structured Mechanical Models by Minimizing Discrete Euler-Lagrange Residual

Model-based paradigms for decision-making and control are becoming ubiquitous in robotics. They rely on the ability to efficiently learn a model of the system from data. Structured Mechanical Models (SMMs) are a data-efficient black-box parameterization of mechanical systems, typically fit to data by minimizing the error between predicted and observed accelerations or next states. In this work, we propose a methodology for fitting SMMs to data by minimizing the discrete Euler-Lagrange residual. To study our methodology, we fit models to joint-angle time-series from undamped and damped double-pendulums, studying the quality of learned models fit to data with and without observation noise. Experiments show that our methodology learns models that are better in accuracy to those of the conventional schemes for fitting SMMs. We identify use cases in which our method is a more appropriate methodology. Source code for reproducing the experiments is available at https://github.com/sisl/delsmm.


Introduction
Many autonomous systems implement model-based control schemes, in which the system uses an internal model of itself, its environment, and the consequences of its own actions. In robotic settings, these models typically map the robot's configuration, its velocities, and the forces applied to it to the acceleration on the robot's degrees-of-freedom. There is a growing need to efficiently learn these models from potentially noisy time-series describing the evolution of the system. Structured Mechanical Models (SMMs) [5,4] provide a black-box but data-efficient parameterization of mechanical systems that have been shown to be well-suited to the task of learning models of dynamics from data.
Instead of directly parameterizing a function that predicts accelerations given the robot configuration, velocity, and inputs, SMMs parameterize the Lagrangian of a mechanical system. The accelerations on the system are then computed from the Lagrangian using the Euler-Lagrange equation [5,4,7,2]. Prior work fit SMMs by minimizing the error between the predicted and observed accelerations [7,2], or the predicted and observed next-states of the system [5]. In cases in which dynamics are continuous, timeseries of observations can typically be filtered or smoothed in order to recover estimates of the system's configurations, velocities, and accelerations, from which we can train a model. However, in the case where dynamics are inherently discontinuous, such as domains involving contact, a continuous formulation of Euler-Lagrange equations will yield predictions of infinite acceleration, and is thus inappropriate.
In this work, we propose an alternative methodology for fitting SMMs to a time-series of system configuration measurements. We observe that for a trajectory to have been generated by a given Lagrangian, the discrete Euler-Lagrange (DEL) equations must be zero along the trajectory [8]. We therefore propose to minimize the DEL residual, attempting to find a Lagrangian such that the DEL equations are zero along the trajectories being fit. This approach is not sufficient in itself, however, due to gauge invariance. Specifically, if the DEL equations are satisfied along a trajectory given a Lagrangian L, then they will also be satisfied given a Lagrangian αL + β, for (α, β) ∈ R. As a result, a Lagrangian that is everywhere equal to a constant satisfies the DEL equations. To avoid this degenerate solution, we propose a regularization term that ensures the learned Lagrangian is non-constant along the trajectory.
To validate our approach, we fit SMMs to data from damped and undamped double-pendulums. We show that when training with our method instead of using acceleration or next-state regression, we are able to reliably learn models with lower error. We demonstrate that this result holds true when fitting SMMs to data recovered from noisy observations of an undamped and damped double-pendulum.
The contributions of this work are as follows: • We propose an optimization objective for fitting SMMs based on the discrete Euler-Lagrange residual, • We propose a regularization term that guarantees our method does not find degenerate solutions, and, • We demonstrate that SMMs fit with our method are of better quality than those fit with conventional approaches.
We conclude this work with a discussion of potential application domains in which minimizing the DEL-residual is more appropriate than acceleration or next-state regression, such as domains involving contact. The codebase containing the experiments conducted and an example implementation of our methodology can be found at https://github.com/sisl/delsmm.

Lagrangian Dynamics
Consider a configuration space of a mechanical system q ∈ R n and associated velocityq ∈ R n . The evolution of the system can be understood by specifying the Lagrangian of the system, i.e.
where 1 2q M(q)q and V (q) are the kinetic and potential energy of the system, respectively, and M(q) 0 is the mass-matrix. The system maybe also be forced via an external forcing function F (q,q, u), where u is a control-input. Note that these forces are non-conservative in that they change the total-energy of the system. For simplicity, we considered uncontrolled systems in this work, and thus drop the dependence on u.

The Euler-Lagrange Equation
The Lagrangian of a system can be used to specify the dynamics of the system via the Euler-Lagrange equation. The continuous-time version of this equation is: This equation can be used to find the accelerationsq on the system, as follows: The trajectory of the system given q 0 ,q 0 can then be found by a numerical integration scheme such as Runge-Kutta [9].
Suppose we instead are given q 0 , q 1 , which are the configurations of the system at the first and second time-step of interest. We can find the configuration q 3 that succeeds these initial conditions by considering a discrete formulation of the Lagrangian and the Euler-Lagrange equation [8]. We define the discrete-Lagrangian L d and discrete generalized-force F d given simulation step-size h to be: and the discrete Euler-Lagrange (DEL) equation as: Here we use the slot derivative D i to indicate partial differentiation with respect to a function's i-th argument.

Gauge Invariance
It should be noted that, given a system's dynamics, the corresponding Lagrangian is not unique. Specifically, ifL = αL + β for some (α, β) ∈ R, then it is trivial to see from Equation (3) and Equation (5) that the dynamics that correspond to L andL are identical. That is, if there is a tuple (q,q,q) and Lagrangian L for which Equation (3) holds, or a tuple q 1:3 and discrete Lagrangian L d for which Equation (5) holds, then it will also hold forL = αL + β.

Structured Mechanical Models
Many works in recent years have proposed a black-box parameterization for the dynamics of a mechanical system [4,5,7,2,3]. Gupta et al. [4] and Lutter et al. [7] proposed to parameterize the components of the Lagrangian of a dynamical system using neural networks, and then derive the accelerations on the system via Equation (3). Specifically, they proposed to parameterize the Cholesky-factor of M(q) using a neural-network mapping q ∈ R n → R n 2 −n 2 , and another neural network representing the potential-energy V (q) of the system, mapping q ∈ R n → R. Gupta et al. [4] also propose to parameterize the generalized forces F (q,q, u) using a neural network, and show that the expressive power of this model is equivalent to that of a neural network directly mapping (q,q, u) toq, though with considerably better generalization properties. Cranmer et al. [2] extend these works by parameterizing the Lagrangian as any neural-network mapping (q,q) → R, allowing for the use of novel architectures such as graph neural-networks.
Each of these works refers to these parameterizations by a different name, such as Deep Lagrangian Networks [7], and Lagrangian Neural Networks [2]. We follow the naming convention of Gupta et al. [5], calling a model parameterizing M(q), V (q) and F (q,q) with neural networks a structured mechanical model (SMM).

Parameter Optimization in Prior Work
We are given a time-series of sampled system configurations y 1:T , possibly corrupted by some observation noise. It is common to use a smoothing routine to try to recover a time-series of the system's configurations q, velocitiesq, and accelerationsq from y 1:T . One popular approach that is used by this paper is to assume the system is a double-integrator and find the desired series using Kalman smoothing [1].
The parameters of an SMM are thus optimized to minimize the empirical risk betweenq and the acceleration predicted by the SMM givenq andq [7,2]. Gupta et al. [5] propose instead to minimize the empirical risk between the predictedq ,q and smoothed next-states givenq,q. We refer to these two approaches as acceleration and next-state regressions respectively.
In the next section, we propose an alternative methodology for optimizing the parameters of an SMM.

Methodology
As shown in Section 2, a system's dynamics can be derived from its Lagrangian using a continuous or discrete-time formulation. In this section, we describe a methodology for fitting an SMM to data using the discrete Euler-Lagrange equation. Again, we assume we have access to a time-seriesq 1:T found by smoothing an observation time-series, but do not assume access to the time-derivatives of this series.
We begin by observing that, given an arbitrary tuple of configurations q 1:3 , we do not expect DEL(q 1:3 ) = 0 as in Equation (5). We define the DEL residual ρ(q 1:3 ) to be: The essence of our methodology is to find the parameters θ of an SMM by minimizing the DEL-residual.
Unfortunately, due to gauge-invariance, a trivial solution exists that has a zero DEL residual while incorrectly estimating the dynamics. Specifically, this solution is a constant Lagrangian, i.e. L(q,q) = β, F (q,q) = 0 ∀(q,q), for any constant β ∈ R, which gives a zero DEL residual for all trajectories. We can avoid this trivial solution by adding the constraint that the mass-matrix is strictly positive definite. We incorporate this constraint by adding a barrier-function commonly used in semidefinite programming that allows us to lower-bound the minimum eigenvalue of the mass-matrix. Let ρ(q 1:3 | θ) be the DEL-residual given that we parameterize the SMM components M θ , V θ , and F θ . The loss L(θ) is defined as follows: where α is an arbitrarily chosen eigenvalue lower-bound, and D is the training dataset. The addition of the regularizer ensures that min eig(M θ (q)) > α ∀q ∈ D. A sensible choice for α is simply a fraction smaller than the smallest eigenvalue of the mass-matrix over the dataset given the initial parameter guess θ 0 . We show in Theorem 1 that minimizing this loss as opposed to the DEL-residual does not yield a biased solution. Theorem 1. Let the model class M = {M θ , V θ , F θ } be closed under multiplication by a positive scalar. Then, there exists a minimizer θ of L(θ) that is also a non-trivial minimizer of L 1 (θ).
In the next section, we compare the accuracy of SMMs learned using acceleration and next-state regression to those learned by minimizing Equation (7).

Experiments
This section demonstrates that, when data is collected in the presence of noise, the SMMs learned by minimizing Equation (7) are more accurate than those learned by acceleration or next-state regression. We do so by studying a damped and undamped double-pendulum for which we learn F θ and set it to zero, respectively.

Double Pendulum Domain
We simulate a double pendulum by specifying its Lagrangian and the forces that act on it. The mass matrix and potential energy forming the Lagrangian are specified in Appendix A.1. In the undamped setting, the forces F (q,q) that act on the system are zero, and in the damped case, the forces are F (q,q) = −η •q, for some positive η ∈ R 2 , where • represents the Hadamard product.
The double pendulum is simulated for 200 time-steps using a step-size of 0.05 and a variational integration scheme. We initialize the system at rest with joint-angles uniformly distributed between [−π/2, π/2). We simulate 16 trajectories, using 8 trajectories as part of the training set, 4 as part of a validation set, and 4 as part of a test set. The trajectories comprising these sets are randomized across seeds. We observe the joint angles of systems at every time-step with an additive Gaussian observation noise with zero-mean and a standard deviation of 0.1 rad, yielding observations y 1:T . We also conduct the experiment using noise standard deviations of 0.05 and 0.4 rad and present the results in Appendix A.2.

Experimental Protocol
We estimate the smoothed joint anglesq 1:T , velocitiesq 1:T , and accelerationsq 1:T from y 1:T . Specifically, we assume the data is generated from a linear dynamical system with dynamics: where w t ∼ N (0, Q) and v t ∼ N (0, R). We use expectation-maximization to find the likelihoodmaximizing observation covariance matrix and the distributions over initial conditions. We use a fixed covariance matrix of Q = diag([10 −3 , 10 −3 , 1.0]). A sample trajectory recovered from this smoothing procedure is depicted in Figure 1, which shows that high-frequency content is not recovered well in the smoothed accelerations.
We fit to the smooth data by minimizing the appropriate loss using stochastic gradient descent. Specifically, we use a batch size of 256 tuples, and use Adam [6] to optimize with varied initial learning rates ξ 0 . All learning rates follow a decay schedule of ξ k = 500ξ 0 /(500 + k), where k is the training epoch. We minimize each loss for 500 epochs.
To avoid overfitting, we select a model with the lowest predicted acceleration error on the validation set. When selecting this model, we use smoothed validation data. We then evaluate the quality of the fit by comparing the predicted and true accelerations on the test data. The model with the lowest error in predicted acceleration is deemed the best performing model.
When fitting to data from an undamped double pendulum, we use a conservative SMM in which F θ = 0. When fitting to data from a damped double pendulum, we represent F θ with a neural network taking inputs q andq.
To compare the three methodologies (minimizing the regularized DEL-residual, acceleration regression, and next-state regression), we randomize the trajectories in the train, test, and validation datasets, as well as the initialization of the SMM, over 10 random seeds. We report the mean error in predicted accelerations on the test set, as well as the standard error of this estimate. For each methodology, we compare results for learning rates ξ 0 ∈ {10 −2 , 10 −3 , 10 −5 }.

Results
In Figure 2, we present the mean and standard errors of test performance for various learning rates, and for all methodologies. We see that in both cases, the quality of models learned by minimizing the DEL-residual via Equation (7) yields improvements over using either of the baseline methodologies, for an appropriate choice of learning rate. We present additional experiments for different noise standard deviations in Appendix A.2, which support these conclusions.
The experiments conducted suggest that fitting SMMs to time-series by minimizing the DEL-residual via Equation (7) yields models that are better than those learned by fitting SMMs via acceleration or next-state regression.

Conclusion
In this work, we proposed a methodology for fitting SMMs to a time-seires of observed states by minimizing the discrete Euler-Lagrange residual. To avoid allowing a trivial solution to be found, we introduced a regularization term that guarantees that the mass matrix has a lower-bounded eigenvalue for all states in the dataset. We proved that using the regularized loss does not bias the learning objective. Furthermore, we showed in experiments on nosiy data from damped and undamped pendulums that our methodology learns better quality solutions than acceleration or next-state regression.
An application of particular interest is the fitting of SMMs to contact-rich systems. In such systems, the use of the continuous formulation of the Euler-Lagrange equations is inappropriate, owing to the presence of infinitely large accelerations on contact events. The discrete formulation of these equations straightforwardly incorporates contact by introducing inequality constraints while minimizing the DELresidual. Extending our methodology to enable the fitting of SMMs to contact-rich avenues is a promising avenue for future work.

A Experiments
In this section, we detail the dynamics of the double pendulum used in our experiments, and display the result of experiments performed with additional noise standard deviations.

A.1 Double Pendulum Dynamics
We use double pendulum dynamics in our experiments. The mass matrix M sys (q) and potential energy V sys (q) of the double pendulum are as follows: M sys (q) = I 11 I 12 where I 11 = I 1 + I 2 + m 2 l 1 2 + m 2 l 1 l 2 cos q 2 (18) For the experiments, we used the parameters specified in Table 1.

A.2 Experiments on More Noise Standard Deviations
In Figure 3 and Figure 4, we compare methodologies on data from undamped and damped pendulums simulated with noise standard deviations of 0.05 and 0.4 rad respectively.   As we can see, training with our methodology continues to outperform acceleration and next-state regression on the undamped pendulum, though has comparable performance on a damped pendulum. We suspect that the reason for this difference is the fact that as the damped signal decays, noise dominates the null signal, thereby equalizing the performance of methodologies.