THE DIFFERENTIABLE CROSS-ENTROPY METHOD
Year: 2019

Abstract:
We study the Cross-Entropy Method (CEM) for the non-convex optimization of a continuous and parameterized objective function and introduce a differentiable variant (DCEM) that enables us to differentiate the output of CEM with respect to the objective function’s parameters. In the machine learning setting this brings CEM inside of the end-to-end learning pipeline where this has otherwise been impossible. We show applications in a synthetic energy-based structured prediction task and in non-convex continuous control. In the control setting we show on the simulated cheetah and walker tasks that we can embed their optimal action sequences with DCEM and then use policy optimization to fine-tune components of the controller as a step towards combining model-based and model-free RL.

heading: 1 INTRODUCTION
text: Recent work in the machine learning community has shown how optimization procedures can create new building-blocks for the end-to-end machine learning pipeline (Gould et al., 2016; Johnson et al., 2016; Amos et al., 2017; Amos & Kolter, 2017; Domke, 2012; Metz et al., 2016; Finn et al., 2017; Belanger et al., 2017; Rusu et al., 2018; Srinivas et al., 2018; Amos et al., 2018). In this paper we focus on the setting of optimizing an unconstrained, non-convex, and continuous objective function fθ(x) : Rn × Θ → R as x̂ = arg minx fθ(x), where f is parameterized by θ ∈ Θ and has inputs x ∈ Rn. If it exists, some (sub-)derivative ∇θx̂ is useful in the machine learning setting to make the output of the optimization procedure end-to-end learnable. For example, θ could parameterize a predictive model that is generating potential outcomes conditional on x happening that you want to optimize over. End-to-end learning in these settings can be done by defining a loss function L on top of x̂ and taking gradient steps ∇θL. If fθ were convex this gradient is easy to analyze and compute when it exists and is unique (Gould et al., 2016; Johnson et al., 2016; Amos et al., 2017; Amos & Kolter, 2017). Unfortunately analyzing and computing a “derivative” through the non-convex arg min here is not as easy and is challenging in theory and practice. No such derivative may exist in theory, it might not be unique, and even if it uniquely exists, the numerical solver being used to compute the solution may not find a global or even local optimum of f . One promising direction to sidestep these issues is to approximate the arg min operation with an explicit optimization procedure that is interpreted as just another compute graph and unrolled through. This is most commonly done with gradient descent as in Domke (2012); Metz et al. (2016); Finn et al. (2017); Belanger et al. (2017); Rusu et al. (2018); Srinivas et al. (2018); Foerster et al. (2018). This approximation adds significant definition and structure to an otherwise extremely ill-defined desiderata at the cost of biasing the gradients and enabling the learning procedure to over-fit to the hyper-parameters of the optimization algorithm, such as the number of gradient steps or the learning rate.
In this paper we show that the Cross-Entropy Method (CEM) (De Boer et al., 2005) is a reasonable alternative to unrolling gradient descent for approximating the derivative through an unconstrained, non-convex, and continuous arg min. CEM for optimization is a zeroth-order optimizer and works by generating a sequence of samples from the objective function. We show a simple and computationally negligible way of making CEM differentiable that we call DCEM by using the smooth top-k operation from Amos et al. (2019). This also brings CEM into the end-to-end learning process in cases where there is otherwise a disconnection between the objective that is being learned and the objective that is induced by deploying CEM on top of those models.
We first quickly study DCEM in a simple non-convex energy-based learning setting for regression. We contrast using unrolled gradient descent and DCEM for optimizing over a SPEN (Belanger & McCallum, 2016). We show that unrolling through gradient descent in this setting over-fits to the number of gradient steps taken and that DCEM generates a more reasonable energy surface.
Our main application focus is on using DCEM in the context of non-convex continuous control. This setting is especially interesting as vanilla CEM is the state-of-the-art method for solving the control optimization problem with neural network transition dynamics as in Chua et al. (2018); Hafner et al. (2018). We show that DCEM is useful for embedding action sequences into a lower-dimensional space to make solving the control optimization process significantly less computationally and memory expensive. This gives us a controller that induces a differentiable policy class parameterized by the model-based components. We then use PPO (Schulman et al., 2017) to fine-tune the modelbased components, demonstrating that it is possible to use standard policy learning for model-based RL in addition to just doing maximum-likelihood fitting to observed trajectories.

heading: 2 BACKGROUND AND RELATED WORK

heading: 2.1 DIFFERENTIABLE OPTIMIZATION-BASED MODELING IN MACHINE LEARNING
text: Optimization-based modeling is a way of integrating specialized operations and domain knowledge into end-to-end machine learning pipelines, typically in the form of a parameterized arg min operation. Convex, constrained, and continuous optimization problems, e.g. as in Gould et al. (2016); Johnson et al. (2016); Amos et al. (2017); Amos & Kolter (2017), capture many standard layers as special cases and can be differentiated through by applying the implicit function theorem to a set of optimality conditions from convex optimization theory, such as the KKT conditions. Non-convex and continuous optimization problems, e.g. as in Domke (2012); Belanger & McCallum (2016); Metz et al. (2016); Finn et al. (2017); Belanger et al. (2017); Rusu et al. (2018); Srinivas et al. (2018); Foerster et al. (2018); Amos et al. (2018); Pedregosa (2016); Jenni & Favaro (2018); Rajeswaran et al. (2019), are more difficult to differentiate through. Differentiation is typically done by unrolling gradient descent or applying the implicit function theorem to some set of optimality conditions, sometimes forming a locally convex approximation to the larger non-convex problem. Unrolling gradient descent is the most common way and approximates the arg min operation with gradient descent for the forward pass and interprets the operations as just another compute graph for the backward pass that can all be differentiated through. In contrast to these works, we show how continuous and nonconvex arg min operations can also be approximated with the cross entropy method (De Boer et al., 2005) as an alternative to unrolling gradient descent.

heading: 2.2 EMBEDDING DOMAINS FOR OPTIMIZATION PROBLEMS
text: Oftentimes the solution space of high-dimensional optimization problems may have structural properties that an optimizer can exploit to find a better solution or to find the solution quicker than an otherwise naïve optimizer. This is done in the context of meta-learning in Rusu et al. (2018) where gradient-descent is unrolled over a latent space. In the context of Bayesian optimization this has been explored with random feature embeddings, hand-coded embeddings, and auto-encoder-learned embeddings (Antonova et al., 2019; Oh et al., 2018; Calandra et al., 2016; Wang et al., 2016; Garnett et al., 2013; Ben Salem et al., 2019; Kirschner et al., 2019). We show that DCEM is another reasonable way of learning an embedded domain for exploiting the structure in and efficiently solving larger optimization problems, with the significant advantage of DCEM being that the latent space is directly learned to be optimized over as part of the end-to-end learning pipeline.

heading: 2.3 RL AND CONTROL
text: High-dimensional non-convex optimization problems that have a lot of structure in the solution space naturally arise in the control setting where the controller seeks to optimize the same objective in the same controller dynamical system from different starting states. This has been investigated in, e.g., planning (Ichter et al., 2018; Ichter & Pavone, 2019; Mukadam et al., 2018; Kurutach et al., 2018; Srinivas et al., 2018; Yu et al., 2019; Lynch et al., 2019), and policy distillation (Wang & Ba, 2019). Chandak et al. (2019) shows how to learn an action space for model-free learning and
Co-Reyes et al. (2018); Antonova et al. (2019) embed action sequences with a VAE. There has also been a lot of work on learning reasonable latent state space representations (Tasfi & Capretz, 2018; Zhang et al., 2018; Gelada et al., 2019; Miladinović et al., 2019) that may have structure imposed to make it more controllable (Watter et al., 2015; Banijamali et al., 2017; Ghosh et al., 2018; Anand et al., 2019; Levine et al., 2019; Singh et al., 2019). In contrast to these works, we learn how to encode action sequences directly with DCEM instead of auto-encoding the sequences. This has the advantages of 1) never requiring the expensive expert’s solution to the control optimization problem, 2) potentially being able to surpass the performance of an expert controller that uses the full action space, and 3) being end-to-end learnable through the controller for the purpose of finding a latent space of sequences that DCEM is good at searching over.
Another direction the RL and control has been pursuing is on the combination of model-based and model-free methods (Bansal et al., 2017; Okada et al., 2017; Jonschkowski et al., 2018; Pereira et al., 2018; Amos et al., 2018; Okada & Taniguchi, 2019; Janner et al., 2019; Pong et al., 2018). Amos et al. (2018) proposes differentiable MPC and only do imitation learning on the cartpole and pendulum tasks with known or lightly-parameterized dynamics — in contrast, we are able to 1) scale our differentiable controller up to the cheetah and walker tasks, 2) use neural network dynamics inside of our controller, and 3) backpropagate a policy loss through the output of our controller and into the internal components.

heading: 3 THE DIFFERENTIABLE CROSS-ENTROPY METHOD (DCEM)
text: We focus on uses of the Cross-Entropy Method (CEM) (De Boer et al., 2005) for optimization in this paper. In this setting, suppose we have a non-convex, deterministic, and continuous objective function fθ(x) parameterized by θ over a domain Rn and we want to solve the optimization problem
x̂ = arg min x fθ(x) (1)
The original form of CEM is an iterative and zeroth-order algorithm to approximate the solution of eq. (1) with a sequence of samples from a sequence of parametric sampling distributions gφ defined over the domain Rn, such as Gaussians.
We refer the reader to De Boer et al. (2005) for more details and motivations for using CEM and briefly describe how it works here. Given a sampling distribution gφ, the hyper-parameters of CEM are the number of candidate points sampled in each iteration N , the number of elite candidates k to use to fit the new sampling distribution to, and the number of iterations T . The iterates of CEM are the parameters φ of the sampling distribution. CEM starts with an initial sampling distribution gφ1(X) ∈ Rn, and in each iteration t generates N samples from the domain [Xt,i] N i=1 ∼ gφt(·), evaluates the function at those points vt,i = fθ(Xt,i), and re-fits the sampling distribution to the top-k samples by solving the maximum-likelihood problem1
φt+1 = arg max φ ∑ i 1{vt,i ≤ π(vt)k} log gφ(Xt,i), (2)
where the indicator 1{P} is 1 if P is true and 0 otherwise, gφ(X) is the likelihood of X under gθ and π(x) sorts x ∈ Rn in ascending order so that π(x)1 ≤ π(x)2 ≤ . . . ≤ π(x)n. We can then map from the final distribution gφT back to the domain Rn by taking the mean of it, i.e. x̂ = E[gφT+1(·)]. Proposition 1. For multivariate isotropic Gaussian sampling distributions we have that φ = {µ, σ2} and eq. (2) has a closed-form solution given by the sample mean and variance of the top-k samples as µt+1 = 1/k ∑ i∈It Xt,i and σ 2 t+1 = 1/k ∑ i∈It (Xt,i − µt+1)
2, where the top-k indexing set is It = {i : vt,i ≤ π(vt)k}.
This is well-known in statistics and is discussed in, e.g., Friedman et al. (2001).
1The Cross-Entropy Method’s name comes from eq. (2) more generally optimizing the cross-entropy measure between two distributions.
Algorithm 1 DCEM(fθ, gφ, φ1; τ,N, k, T ) DCEM minimizes a parameterized objective function fθ and is differentiable w.r.t. θ. Each DCEM iteration samples from the distribution gφ, starting with φ1.
for t = 1 to T do [Xt,i] N i=1 ∼ gφt(·) . Sample N points from the domain
vt,i = fθ(Xt,i) . Evaluate the objective function at those points It = ΠLk(vt/τ) . Compute the soft top-k projection of the values with eq. (4) Update φt+1 by solving the maximum weighted likelihood problem in eq. (5)
end for return E[gφT+1(·)]
Differentiating through CEM’s output with respect to the objective function’s parameters with ∇θx̂ is useful, e.g., to bring CEM into the end-to-end learning process in cases where there is otherwise a disconnection between the objective that is being learned and the objective that is induced by deploying CEM on top of those models. Unfortunately in the vanilla form presented above the top-k operation in eq. (2) makes x̂ non-differentiable with respect to θ. The function samples can usually be differentiated through with some estimator (Mohamed et al., 2019) such as the reparameterization trick (Kingma & Welling, 2013), which we use in all of our experiments.
The top-k operation can be made differentiable by replacing it with a soft version as done in Martins & Kreutzer (2017); Malaviya et al. (2018); Amos et al. (2019), or by using a stochastic oracle as in ?. Here we use the Limited Multi-Label Projection (LML) layer (Amos et al., 2019), which projects points from Rn onto the LML polytope defined by
Ln,k = {p ∈ Rn | 0 ≤ p ≤ 1 and 1>p = k}, (3)
which is the set of points in the unit n-hypercube with coordinates that sum to k. Notationally, if n is implied by the context we will leave it out and write Lk. We propose a temperature-scaled LML variant to project onto the interior of the LML polytope with
ΠLk(x/τ) = arg min 0<y<1
−x>y − τHb(y) s. t. 1>y = k (4)
where τ > 0 is the temperature parameter and Hb(y) = − ∑ i yi log yi + (1 − yi) log(1 − yi) is the binary entropy function. Equation (4) is a convex optimization layer and can be solved in a negligible amount of time with a GPU-amenable bracketing method on the univariate dual and quickly backpropagated through with implicit differentiation. We can use the LML layer to make a soft and differentiable version of eq. (2) as
φt+1 = arg max φ ∑ i It,i log gφ(Xt,i) subject to It = ΠLk(vt/τ). (5)
This is now a maximum weighted likelihood estimation problem (Markatou et al., 1997; 1998; Wang, 2001; Hu & Zidek, 2002), which still admits an analytic closed-form solution in many cases, e.g. for the natural exponential family (De Boer et al., 2005). Thus using the soft top-k operation with the reparameterization trick, e.g., on the samples from g results in a differentiable variant of CEM that we call DCEM and summarize in alg. 1. We note that we usually also normalize the values in each iteration to help separate the scaling of the values from the temperature parameter.
Proposition 2. The temperature-scaled LML layer ΠLk(x/τ) approaches the hard top-k operation as τ → 0+ when all components of x are unique.
We prove this in app. A by using the KKT conditions of eq. (4). Corollary 1. DCEM captures CEM as a special case as τ → 0+. Proposition 3. With an isotropic Gaussian sampling distribution, the update in eq. (5) becomes µt+1 = 1/k ∑ i It,iXt,i and σ2t+1 = 1/k ∑ i It,i (Xt,i − µt+1)
2, where the soft top-k indexing set is It = ΠLk(vt/τ).
This can be proved by differentiating eq. (5), as discussed in, e.g., (De Boer et al., 2005). As a corollary, this captures prop. 1 as τ → 0+.

heading: 4 APPLICATIONS

heading: 4.1 ENERGY-BASED LEARNING
text: Energy-based learning for regression and classification estimate the conditional probability P(y|x) of an output y ∈ Y given an input x ∈ X with a parameterized energy function Eθ(y|x) ∈ Y × X → R such that P(y|x) ∝ exp{−Eθ(y|x)}. Predictions are made by solving the optimization problem
ŷ = arg min y
Eθ(y|x). (6)
Historically linear energy functions have been well-studied, e.g. in Taskar et al. (2005); LeCun et al. (2006), as it makes eq. (6) easier to solve and analyze. More recently non-convex energy functions that are parameterized by neural networks are being explored — a popular one being Structured Prediction Energy Networks (SPENs) (Belanger & McCallum, 2016) which propose to model Eθ with neural networks. Belanger et al. (2017) suggests to do supervised learning of SPENs by approximating eq. (6) with gradient descent that is then unrolled for T steps, i.e. by starting with some y0, making gradient updates yt+1 = yt + γ∇yEθ(yt|x) resulting in an output ŷ = yT , defining a loss function L on top of ŷ, and doing learning with gradient updates∇θL that go through the inner gradient steps.
In this context we can alternatively use DCEM to approximate eq. (6). One potential consideration when training deep energy-based models with approximations to eq. (6) is the impact and bias that the approximation is going to have on the energy surface. We note that for gradient descent, e.g., it may cause the energy surface to overfit to the number of gradient steps so that the output of the approximate inference procedure isn’t even a local minimum of the energy surface. One potential advantage of DCEM is that the output is more likely to be near a local minimum of the energy surface so that, e.g., more test-time iterations can be used to refine the solution. We empirically illustrate the impact of the optimizer choice on a synthetic example in sect. 5.1.

heading: 4.2 CONTROL AND REINFORCEMENT LEARNING
text: Our main application focus is in the continuous control setting where we show how to use DCEM to learn a latent control space that is easier to solve than the original problem and induces a differentiable policy class that allows parts of the controller to be fine-tuned with auxiliary policy or imitation losses.
We are interested in controlling discrete-time dynamical systems with continuous state-action spaces. Let H be the horizon length of the controller and UH be the space of control sequences over this horizon length, e.g. U could be a multi-dimensional real space or box therein and UH could be the Cartesian product of those spaces representing the sequence of controls over H timesteps. We are interested in repeatedly solving the control optimization problem2
û1:H = arg min u1:H∈UH H∑ t=1 Ct(xt, ut) subject to xt+1 = f trans(xt, ut), x1 = xinit, (7)
2For notational convenience we omit some explicit variables from the argmin operator when they are can be inferred by the context and not used elsewhere.
Algorithm 2 Learning an embedded control space with DCEM Fixed Inputs: Dynamics f trans, per-step state-action costCt(xt, ut) (inducingCθ(z;xinit)) horizon H , full control space UH , distribution over initial states D Learned Inputs: Decoder fdecθ : Z → UH
while not converged do Sample initial state xinit ∼ D ẑ = arg minz∈Z Cθ(z;xinit) . Solve the embedded control problem eq. (8) θ ← grad-update(∇θCθ(ẑ)) . Update the decoder to improve the controller’s cost end while
where we are in an initial system state xinit governed by deterministic system transition dynamics f trans, and wish to find the optimal sequence of actions û1:H such that we find a valid trajectory {x1:H , u1:H} that optimizes the cost Ct(xt, ut). Typically these controllers are used for receding horizon control (Mayne & Michalska, 1990) where only the first action u1 is deployed on the real system, a new state is obtained from the system, and the eq. (7) is solved again from the new initial state. In this case we can say the controller induces a policy π(xinit) ≡ û13 that solves eq. (7) and depends on the cost and transition dynamics, and potential parameters therein. In all of the cases we consider f trans is deterministic, but may be approximated by a stochastic model for learning. Some model-based reinforcement learning settings consider cases where f trans and C are parameterized and potentially used in conjunction with another policy class.
For sufficiently complex dynamical systems, eq. (7) is computationally expensive and numerically instable to solve and rife with sub-optimal local minima. The Cross-Entropy Method is the state-ofthe-art method for solving eq. (7) with neural network transitions f trans (Chua et al., 2018; Hafner et al., 2018). CEM in this context samples full action sequences and refines the samples towards ones that solve the control problem. Hafner et al. (2018) uses CEM with 1000 samples in each iteration for 10 iterations with a horizon length of 12. This requires 1000 × 10 × 12 = 120, 000 evaluations (!) of the transition dynamics to predict the control to be taken given a system state — and the transition dynamics may use a deep recurrent architecture as in Hafner et al. (2018) or an ensemble of models as in Chua et al. (2018). One comparison point here is a model-free neural network policy takes a single evaluation for this prediction, albeit sometimes with a larger neural network.
The first application we show of DCEM in the continuous control setting is to learn a latent action space Z with a parameterized decoder fdecθ : Z → UH that maps back up to the space of optimal action sequences, which we illustrate in fig. 3. For simplicity starting out, assume that the dynamics and cost functions are known (and perhaps even the ground-truth) and that the only problem is to estimate the decoder in isolation, although we will show later that these assumptions can be relaxed. The motivation for having such a latent space and decoder is that the millions of times eq. (7) is being solved for the same dynamic system with the same cost, the solution space of optimal action sequences û1:H ∈ UH has an extremely large amount of spatial (over U) and temporal (over time in UH ) structure that is being ignored by CEM on the full space. The space of optimal action sequences only contains the knowledge of the trajectories that matter for solving the task at hand, such as different parts of an optimal gait, and not irrelevant control sequences. We argue that CEM over the full action space wastes a lot of computation considering irrelevant action sequences and show that these can be ignored by learning a latent space of more reasonable candidate solutions here that we search over instead. Given a decoder, the control optimization problem in eq. (7) can then be transformed into an optimization problem over Z as
ẑ = arg min z∈Z Cθ(z;xinit) ≡ H∑ t=1 Ct(xt, ut)
subject to xt+1 = f trans(xt, ut), x1 = xinit, u1:H = f dec θ (z),
(8)
which is still a challenging non-convex optimization problem that searches over a decoder’s input space to find the optimal control sequence. We illustrate what this looks like in fig. 3, and note the impact of the decoder initialization in app. C.
3For notational convenience we also omit the dependency of u1 on xinit here.
We propose in alg. 2 to use DCEM to approximately solve eq. (8) and then learn the decoder directly to optimize the performance of eq. (7). Every time we solve eq. (8) with DCEM and obtain an optimal latent representation ẑ along with the induced trajectory {xt, ut}, we can take a gradient step to push down the resulting cost of that trajectory with∇θC(ẑ), which goes through the DCEM process that uses the decoder to generate samples to obtain ẑ. We note that the DCEM machinery behind this is not necessary if a reasonable local minima is consistently found as this is an instance of min-differentiation (Rockafellar & Wets, 2009, Theorem 10.13) but in practice this breaks down in non-convex cases when the minimum cannot be consistently found. Antonova et al. (2019); Wang & Ba (2019) solve related problems in this space and we discuss them in sect. 2.3. We also note that to learn an action embedding we still need to differentiate through the transition dynamics and cost functions to compute ∇θC(ẑ), even if the ground-truth ones are being used, since the latent space needs to have the knowledge of how the control cost will change as the decoder’s parameters change.
DCEM in this setting also induces a differentiable policy class π(xinit) ≡ u1 = fdec(ẑ)1. This enables a policy or imitation loss J to be defined on the policy that can fine-tune the parts of the controller (decoder, cost, and transition dynamics) gradient information from ∇θJ . In theory the same approach could be used with CEM on the full optimization problem in eq. (7). For realistic problems without modification this is intractable and memory-intensive as it would require storing and backpropagating through every sampled trajectory, although as a future direction we note that it may be possible to delete some of the low-influence trajectories to help overcome this.

heading: 5 EXPERIMENTS
text: We use PyTorch (Paszke et al., 2017) and will openly release our DCEM library, model-based control code, and the source, plotting, and analysis code for all of our experiments.

heading: 5.1 UNROLLING OPTIMIZERS FOR REGRESSION AND STRUCTURED PREDICTION
text: In this section we briefly explore the impact of the inner optimizer on the energy surface of a SPEN as discussed in sect. 4.1. For illustrative purposes we consider a simple unidimensional regression task where the ground-truth data is generated from f(x) = x sin(x) for x ∈ [0, 2π]. We model P(y|x) ∝ exp{−Eθ(y|x)} with a single neural network Eθ and make predictions ŷ by solving the optimization problem eq. (6). Given the ground-truth output y?, we use the loss L(ŷ, y?) = ||ŷ − y?||22 and take gradient steps of this loss to shape the energy landscape. We consider approximating eq. (6) with unrolled gradient descent and DCEM with Gaussian sampling distributions. Both of these are trained to take 10 optimizer steps and we use an inner learning rate of 0.1 for gradient descent and with DCEM we use 10 iterations with 100 samples per iteration and 10 elite candidates, with a temperature of 1. For both algorithms we start the initial iterate at y0 = 0. We show in app. B that both of these models attain the same loss on the training dataset
but, since this is a unidimensional regression task, we can visualize the entire energy surfaces over the joint input-output space in fig. 1. This shows that gradient descent has learned to adapt from the initial y0 = 0 position to the final position by descending along the function’s surface as we would expect, but there is no reason why the energy surface should be a local minimum around the last iterate ŷ = y10. The energy surface learned by CEM captures local minima around the regression target as the sequence of Gaussian iterates are able to capture a more global view of the function landscape and need to focus in on a minimum of it for regression. We show ablations in app. B from training for 10 inner iterations and then evaluating with a different number of iterations and show that gradient descent quickly steps away from making reasonable predictions.
Discussion and limitations. We note that other tricks could be used to force the output to be at a local minimum with gradient descent, such as using multiple starting points or randomizing the number of gradient descent steps taken — our intention here is to highlight this behavior in the vanilla case. We also note that DCEM is susceptible to overfitting to the hyper-parameters behind it in similar, albeit less obvious ways.

heading: 5.2 CONTROL

heading: 5.2.1 STARTING SIMPLE: EMBEDDING THE CARTPOLE’S ACTION SPACE
text: We first show that it is possible to learn an embedded control space as discussed in sect. 4.2 in an isolated setting. We use the standard cartpole dynamical system from Barto et al. (1983) with a continuous state-action space. We assume that the ground-truth dynamics and cost are known and use the differentiable ground-truth dynamics and cost implemented in PyTorch from Amos et al. (2018). This isolates the learning problem to only learning the embedding so that we can study what this is doing without the additional complications that arise from exploration, estimating the dynamics, learning a policy, and other non-stationarities. We show experiments with these assumptions relaxed in sect. 5.2.2.
We use DCEM and alg. 2 to learn a 2-dimensional latent space Z = [0, 1]2 that maps back up to the full control space UH = [0, 1]H where we focus on horizons of length H = 20. For DCEM over the embedded space we use 10 iterations with 100 samples in each iteration and 10 elite candidates, again with a temperature of 1. We show the details in app. D that we are able to recover the performance of an expert CEM controller that uses an order-of-magnitude more samples fig. 2 shows a visualization of what the CEM and embedded DCEM iterates look like to solve the control optimization problem from the same initial system state. CEM spends a lot of evaluations on sequences in the control space that are unlikely to be optimal, such as the ones the bifurcate between the boundaries of the control space at every timestep, while our embedded space is able to learn more reasonable proposals.

heading: 5.2.2 SCALING UP TO THE CHEETAH AND WALKER
text: Next we show that we can relax the assumptions of having known transition dynamics and reward and show that we can learn a latent control space on top of a learned model on the cheetah.run and walker.walk tasks with frame skips of 4 and 2, respectively, from the DeepMind control suite (Tassa et al., 2018) using the MuJoCo physics engine (Todorov et al., 2012). We then fine-tune the policy induced by the embedded controller with PPO (Schulman et al., 2017), sending the policy loss directly back into the reward and latent embedding modules underlying the controller.
We start with a state-of-the-art model-based RL approach by noting that the PlaNet (Hafner et al., 2018) restricted state space model (RSSM) is a reasonable architecture for proprioceptive-based control in addition to just pixel-based control. We show the graphical model we use in fig. 3, which maintains deterministic hidden states ht and stochastic (proprioceptive) system observations xt and rewards rt. We model transitions as ht+1 = f transθ (ht, xt), observations with xt ∼ fodecθ (ht), rewards with rt = f rewθ (ht, xt), and map from the latent action space to action sequences with u1:T = f
dec(z). We follow the online training procedure of Hafner et al. (2018) to initialize all of the models except for the action decoder fdec, using approximately 2M timesteps. We then use a variant of alg. 2 to learn fdec to embed the action space for control with DCEM, which we also do online while updating the models. We describe the full training process in app. E.
Our DCEM controller induces a differentiable policy class πθ(xinit) where θ are the parameters of the models that impact the actions that the controller is selecting. We then use PPO to define a loss on top of this policy class and fine-tune the components (the decoder and reward module) so that they improve the episode reward rather than the maximumlikelihood solution of observed trajectories. We chose PPO because we thought it would be able to fine-tune the policy with just a few updates because the policy is starting at a reasonable point, but this did not turn out to be the case and in the future other policy optimizers can be explored. We implement this by making our DCEM controller the policy in the PyTorch PPO implementation by Kostrikov (2018). We provide more details behind our training procedure in app. E.
We evaluate our controllers on 100 test episodes and the rewards in fig. 4 show that DCEM is almost (but not exactly) able to recover the performance of doing CEM over the full action space while using an order-of-magnitude less trajectory samples (1,000 vs 10,0000). PPO fine-tuning helps bridge the gap between the performances.
Videos of our trained models are available at:
https://sites.google.com/view/diff-cross-entropy-method
Discussion and limitations. DCEM in the control setting has many potential future directions to explore and help bring efficiency and policy-based fine-tuning to model-based reinforcement learning. Much more analysis and experimentation is necessary to achieve this as we faced many issues getting the model-based cheetah and walker tasks to work that did not arise in the ground-truth cartpole task. We discuss this more in app. E. We also did not focus on the sample complexity of our algorithms getting these proof-of-concept experiments working. We also note that other reasonable baselines on this task could involve distilling the controller into a model-free policy and then doing search on top of that policy, as done in POPLIN (Wang & Ba, 2019).

heading: 6 CONCLUSIONS AND FUTURE DIRECTIONS
text: We have laid the foundations for differentiating through the cross-entropy method and have brought CEM into the end-to-end learning pipeline. Beyond further explorations in the energy-based learning and control contexts we showed here, DCEM can be used anywhere gradient descent is unrolled. We find this especially promising for meta-learning, potentially building on LEO (Rusu et al., 2018). Inspired by DCEM, other more powerful sampling-based optimizers could be made differentiable in the same way, potentially optimizers that leverage gradient-based information in the inner optimization steps (Sekhon & Mebane, 1998; Theodorou et al., 2010; Stulp & Sigaud, 2012; Maheswaranathan et al., 2018) or by also learning the hyper-parameters of structured optimizers (Li & Malik, 2016; Volpp et al., 2019; Chen et al., 2017).

heading: ACKNOWLEDGMENTS
text: We acknowledge the scientific Python community (Van Rossum & Drake Jr, 1995; Oliphant, 2007) for developing the core set of tools that enabled this work, including PyTorch (Paszke et al., 2017), Jupyter (Kluyver et al., 2016), Matplotlib (Hunter, 2007), seaborn (Waskom et al., 2018), numpy (Oliphant, 2006; Van Der Walt et al., 2011), pandas (McKinney, 2012), and SciPy (Jones et al., 2014).

heading: A PROOF OF PROP. 2
text: Proof. We first note that a solution exists to the projection operation, and it is unique, which comes from the strict convexity of the objective (Rao, 1984). The Lagrangian of the temperature-scaled LML projection in eq. (4) is
L(y, ν) = −x>y − τHb(y) + ν(k − 1>y). (9)
Differentiating eq. (9) gives
∇yL(y, ν) = −x+ τ log y
1− y − ν (10)
and the first-order optimality condition ∇yL(y?, ν?) = 0 gives y?i = σ(τ−1(xi + ν∗)), where σ is the sigmoid function. Using lem. 1 as τ → 0+ gives
y?i =  1 if xi > −ν∗ 0 if xi < −ν∗ 1/2 otherwise.
(11)
Substituting this back into the constraint 1>y? = k gives that π(x)k < −ν∗ < π(x)k+1, where π(x) sorts x ∈ Rn in ascending order so that π(x)1 ≤ π(x)2 ≤ . . . ≤ π(x)n. Thus we have that y?i = 1{xi ≥ π(x)k}, which is 1 when xi is in the top-k components of x and 0 otherwise, and therefore the temperature-scaled LML layer approaches the hard top-k function as τ → 0+.
Lemma 1.
lim τ→0+ σ(x/τ) =  1 if x > 0 0 if x < 0 1/2 otherwise,
(12)
where σ(x/τ) = (1 + exp{−x/τ})−1 is the temperature-scaled sigmoid.

heading: B MORE DETAILS: SIMPLE REGRESSION TASK
text: Figure 5 (left) shows the convergence of unrolled GD and DCEM on the training data, showing that they are able to obtain the same training loss despite inducing very different energy surfaces. Figure 5 (right) and fig. 6 shows the impact of training gradient descent and DCEM to take 10 inner optimization steps and then ablating the number of inner steps at test-time.

heading: C MORE DETAILS: DECODER INITIALIZATIONS AND ACTIVATION FUNCTIONS
text: We have found the decoder to be influenced by the activation function that’s used with it and have found the ELU (Clevert et al., 2015) to perform the best. fig. 7 conveys some intuition behind this choice. We randomly initialize a neural network u = fθ(z) with no biases, where θ = {Wi}i for every layer weight Wi, and then scale the weights with αθ. We then sample z ∼ N (0, I), pass them through fαθ, and plot the outputs. The ReLU (Nair & Hinton, 2010) induces an extremely biased distribution which is seen more prevalently as α grows that is not as present when using the ELU or hyperbolic tangent since they are almost linear around zero. Despite the reasonable looking initializations for the hyperbolic tangent, we found that it does not perform as well in practice in our experiments. We found that the initial scale α of the decoder’s parameters is also important for learning because of the network is not initially producing samples that cover the full output space as shown with α = 1, it seems hard for it to learn how to expand to cover the full output space.

heading: D MORE DETAILS: CARTPOLE EXPERIMENT
text: In this section we discuss some of the ablations we considered when learning the latent action space for the cartpole task. In all settings we use DCEM to unroll 10 inner iterations that samples 100 candidate points in each iteration and has an elite set of 10 candidates.
For training, we randomly sample initial starting points of the cartpole and for validation we use a fixed set of initial points. Figure 8 shows the convergence of models as we vary the latent space dimension and temperature parameter, and fig. 9 shows that DCEM is able to fully recover the expert performance on the cartpole. Because we are operating in the ground-truth dynamics setting we measure the performance by comparing the controller costs. We use τ = 0 to indicate the case where we optimize over the latent space with vanilla CEM and then update the decoder with ∇zC(fdecθ (ẑ)), where the gradient doesn’t go back into the optimization process that produced ẑ. This is non-convex min differentiation and is reasonable when ẑ is near-optimal, but otherwise is susceptible to making the decoder difficult to search over.
These results show a few interesting points that come up in this setting, which of course may be different in other settings. Firstly that with a two-dimensional latent space, all of the temperature values are able to find a reasonable latent space at some point during training. However after more updates, the lower-temperature experiments start updating the decoder in ways that make it more difficult to search over and start achieving worse performance than the τ = 1 case. For higher-dimensional latent spaces, the DCEM machinery is necessary to keep the decoder searchable. Furthermore we notice that just a 16-dimensional latent space for this task can be difficult for learning, one reason this could be is from DCEM having too many degrees of freedom in ways it can update the decoder to improve the performance of the optimizer.
Algorithm 3 PlaNet (Hafner et al., 2018) variant that we use for proprioceptive control with optional DCEM embedding . Models: a deterministic state model, a stochastic state model, a reward model, and (if using DCEM) an action sequence decoder. . Initialize dataset D with S random seed episodes. . Initialize the transition model’s deterministic hidden state h0 and initialize the environment, obtaining the initial state estimate x0. . CEM-Solve can use DCEM or full CEM for t = 1, . . . , T do
ut ← CEM-solve(ht−1, xt−1) Add exploration noise ∼ p( ) to the action ut. {rt, xt+1, dt} ← env.step(ut) . Properly restarting if necessary Add [rt, xt, ut, dt] to D ht = update-hidden(ht−1, xt, ut, dt) if t ≡ 0 (mod update-interval) then
Sample trajectories τ = [rτ , xτ , uτ , dτ ]Hτ=1 ∼ D from the dataset. Obtain the hidden states of the {hτ , x̂τ} from the model. Compute the multi-step likelihood bound L(τ, hτ , x̂τ ) . (Hafner et al., 2018, eq 6.) θ ← grad-update(∇θLθ(τ, hτ , x̂τ )) . Optimize the likelihood bound if using DCEM then
ẑτ = arg minz∈Z Cθ(z;hτ , x̂τ ) . Solve the embedded control problem in eq. (8) θ ← grad-update(∇θ ∑ τ Cθ(ẑτ )) . Update the decoder
end if end if
end for

heading: E MORE DETAILS: CHEETAH AND WALKER EXPERIMENTS
text: For the cheetah.run and walker.walk DeepMind control suite experiments we start with a modified PlaNet (Hafner et al., 2018) architecture that does not have a pixel decoder. We started with this over PETS (Chua et al., 2018) to show that this RSSM is reasonable for proprioceptivebased control and not just pixel-based control. This model is graphically shown in fig. 3 and has 1) a deterministic state model ht = f(ht−1, xt−1, ut−1), 2) a stochastic state model xt ∼ p(xt, ht), and 3) a reward model: rt ∼ p(rt|ht, xt). In the proprioceptive setting, we posit that the deterministic state model is useful for multi-step training even in fully observable environments as it allows the model to “push forward” information about what is potentially going to happen in the future.
For the modeling components, we follow the recommendations in Hafner et al. (2018) and use a GRU (Cho et al., 2014) with 200 units as the deterministic path in the dynamics model and implement all other functions as two fully-connected layers, also with 200 units with ReLU activations. Distributions over the state space are isotropic Gaussians with predicted mean and standard deviation. We train the model to optimize the variational bound on the multi-step likelihood as presented in (Hafner et al., 2018) on batches of size 50 with trajectory sequences of length 50. We start with 5 seed episodes with random actions and in contrast to Hafner et al. (2018), we have found that interleaving the model updates with the environment steps instead of separating the updates slightly improves the performance, even in the pixel-based case, which we do not report results on here.
For the optimizers we either use CEM over the full control space or DCEM over the latent control space and use a horizon length of 12 and 10 iterations here. For full CEM, we sample 1000 candidates in each iteration with 100 elite candidates. For DCEM we use 100 candidates in each iteration with 10 elite candidates.
Our training procedure has the following three phases, which we set up to isolate the DCEM additions. We evaluate the models output from these training runs on 100 random episodes in fig. 4 in the main paper. Now that these ideas have been validated, promising directions of future work include trying to combine them all into a single training run and trying to reduce the sample complexity and number of timesteps needed to obtain the final model.
Phase 1: Model initialization. We start in both environments by launching a single training run of fig. 11 to get initial system dynamics. fig. 11 shows that these starting points converge to near-stateof-the-art performance on these tasks. These models take slightly longer to converge than in (Hafner et al., 2018), likely due to how often we update our models. We note that at this point, it would be ideal to use the policy loss to help fine-tune the components so that policy induced by CEM on top of the models can be guided, but this is not feasible to do by backpropagating through all of the CEM samples due to memory, so we instead next move on to initializing a differentiable controller that is feasible to backprop through.
Phase 2: Embedded DCEM initialization. Our goal in this phase is to obtain a differentiable controller that is feasible to backprop through.
Our first failed attempt to achieve this was to use offline training on the replay buffer, which would have been ideal as it would require no additional transitions to be collected from the environment. We tried using alg. 2, the same procedure we used in the ground-truth cartpole setting, to generate an embedded DCEM controller that achieves the same control cost on the replay buffer as the full CEM controller. However we found that when deploying this controller on the system, it quickly stepped off of the data manifold and failed to control it — this seemed to be from the controller finding holes in the model that causes the reward to be over-predicted.
We then used an online data collection process identical to the one we used for phase 1 to jointly learn the embedded control space while updating the models so that the embedded controller doesn’t find bad regions in them. We show where the DCEM updates fit into alg. 3. One alternative that we tried to updating the decoder to optimize the control cost on the samples from the replay buffer is that the decoder can also be immediately updated after planning at every step. This seemed nice since it didn’t require any additional DCEM solves, but we found that the decoder became too biased during the episode as samples at consecutive timesteps have nearly identical information.
For the hyper-parameters, we kept most of the DCEM hyper-parameters fixed throughout this phase to 100 samples, 10 elites, and a temperature τ = 1. We ablated across 1) the number of DCEM iterations taken to be {3, 5, 10}, 2) deleting the replay buffer from phase 1 or not, and 3) re-initializing the model or not from phase 1. We report the best runs that we use as the starting point for the next phase in fig. 12, which achieve reasonable performance but don’t match the performance of doing CEM over the full action space. These runs all use 10 DCEM iterations and both keep the replay buffer from phase 1. The Cheetah run keeps the models from phase 1 and the Walker re-initializes the models. The cheetah curve around timestep 600k shows, the stability here can be improved as sometimes the decoder finds especially bad regions in the model that induce extremely high losses.
Phase 3: Policy optimization into the controller. Finally now that we have a differentiable policy class induced by this differentiable controller we can do policy learning to fine-tune parts of it. We initially chose Proximal Policy Optimization (PPO) (Schulman et al., 2017) for this phase because we thought that it would be able to fine-tune the policy in a few iterations without requiring a good estimate of the value function, but this phase also ended up consuming many timesteps from the environment. Crucially in this phase, we do not do likelihood fitting at all, as our goal is to show that PPO can be used as another useful signal to update the parts of a controller — we did this to isolate the improvement from PPO but in practice we envision more unified algorithms that use both signals at the same time. Using the standard PPO hyper-parameters, we collect 10 episodes for each PPO training step and ablate across 1) the number of passes to make through these episodes {1, 2, 4}, 2) every combination of the reward, transition, and decoder being fine-tuned or frozen, 3) using a fixed variance of 0.1 around the output of the controller or learning this, 4) the learning rate of the fine-tuned model-based portions {10−4, 10−5}. Figure 13 shows the results of the best runs from this search.
We conclude by showing the PPO-fine-tuned DCEM iterates for solving a single control optimization problem from a random system state for the cheetah fig. 14. and walker fig. 15 tasks.

Review summaries:
Various optimization methods can be wrapped to form black-box differentiable deep learning modules. This allows end-to-end learning of energy functions that can be used, for example, in continuous control. There is a whole cottage industry of designing these modules. Advancements in this field are of broad interest to the ICLR community. This paper proposes to unroll the cross entropy method, which is very different than the standard practice of unrolling gradient descent. Experiments on continuous control benchmarks demonstrate that this can be used to learn a latent space in which test-time optimization is performed. By doing this optimization in latent space, it can be performed much faster than in the raw high-dimensional space.