generative temporal models with spatial memory for...

10
Generative Temporal Models with Spatial Memory for Partially Observed Environments Marco Fraccaro 1* Danilo Jimenez Rezende 2 Yori Zwols 2 Alexander Pritzel 2 S. M. Ali Eslami 2 Fabio Viola 2 Abstract In model-based reinforcement learning, genera- tive and temporal models of environments can be leveraged to boost agent performance, either by tuning the agent’s representations during train- ing or via use as part of an explicit planning mechanism. However, their application in prac- tice has been limited to simplistic environments, due to the difficulty of training such models in larger, potentially partially-observed and 3D en- vironments. In this work we introduce a novel action-conditioned generative model of such chal- lenging environments. The model features a non-parametric spatial memory system in which we store learned, disentangled representations of the environment. Low-dimensional spatial up- dates are computed using a state-space model that makes use of knowledge on the prior dynamics of the moving agent, and high-dimensional vi- sual observations are modelled with a Variational Auto-Encoder. The result is a scalable architec- ture capable of performing coherent predictions over hundreds of time steps across a range of par- tially observed 2D and 3D environments. 1. Introduction Consider a setup in which an agent walks and observes an environment (e.g., a three-dimensional maze) for hundreds of time steps, and is then asked to predict subsequent obser- vations given a sequence of actions. This is a challenging task, as it requires the ability to first remember the visual observations and the position in which they were observed in the environment, and secondly to predict where a possi- bly long sequence of actions would bring the agent in the environment. Building models that can solve this problem * Work done during an internship at DeepMind. 1 Technical University of Denmark 2 DeepMind, London, UK. Correspondence to: Marco Fraccaro <[email protected]>. Proceedings of the 35 th International Conference on Machine Learning, Stockholm, Sweden, PMLR 80, 2018. Copyright 2018 by the author(s). can be useful for model-based reinforcement learning in- volving spatial tasks that require long-term memories and other spatial downstream goals (Sutton, 1990; Deisenroth & Rasmussen, 2011; Levine & Abbeel, 2014; Watter et al., 2015; Wahlstr ¨ om et al., 2015; Lenz et al., 2015; Higgins et al., 2017; Finn & Levine, 2017). This requires however agents that are able to remember the past over hundreds of steps, that know both where they are in the environment and how each action changes their position, and that can coher- ently predict hundreds of steps into the future. Therefore the main focus of this work is to develop an action-conditioned generative model that is able to memorize all the required information while exploring the environment and succes- sively use it in the prediction phase for long-term generation of high-dimensional visual observations. Recently, several powerful generative models for sequential data have been proposed in a wide range of applications, such as modelling speech, handwriting, polyphonic music and videos (Chung et al., 2015; Fraccaro et al., 2016; Oh et al., 2015; Chiappa et al., 2017). They build on recur- rent neural architectures such as Long Short-Term Memory (LSTM) (Hochreiter & Schmidhuber, 1997) or Gated Recur- rent Units (GRU) (Chung et al., 2014), that use an internal state vector to perform computations and store the long- term information needed when making predictions. Since the number of parameters in these models scales quadrat- ically with the dimensionality of the state vector, they are not suitable for applications that require high memory ca- pacity, such as the one considered in this paper. The high dimensional state vector needed to be able to memorize the hundreds of time steps in which the agent has visited the whole environment, make these models in practice both very slow and hard to train. An alternative approach is to use an external memory architecture, for storing of large amount of information while drastically reducing the number of param- eters with respect to models with similar memory capacity that build on LSTMs or GRUs. Gemici et al. (2017) present a general architecture for generative temporal models with external memory, and test four different types of memo- ries that are dynamically updated at each time step (Graves et al., 2014; 2016; Santoro et al., 2016). They focus on differentiable addressing mechanisms for memory storage and retrieval (soft-attention), that are based on deep neural

Upload: others

Post on 14-Nov-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memoryfor Partially Observed Environments

Marco Fraccaro 1 * Danilo Jimenez Rezende 2 Yori Zwols 2 Alexander Pritzel 2 S. M. Ali Eslami 2 Fabio Viola 2

AbstractIn model-based reinforcement learning, genera-tive and temporal models of environments can beleveraged to boost agent performance, either bytuning the agent’s representations during train-ing or via use as part of an explicit planningmechanism. However, their application in prac-tice has been limited to simplistic environments,due to the difficulty of training such models inlarger, potentially partially-observed and 3D en-vironments. In this work we introduce a novelaction-conditioned generative model of such chal-lenging environments. The model features anon-parametric spatial memory system in whichwe store learned, disentangled representations ofthe environment. Low-dimensional spatial up-dates are computed using a state-space model thatmakes use of knowledge on the prior dynamicsof the moving agent, and high-dimensional vi-sual observations are modelled with a VariationalAuto-Encoder. The result is a scalable architec-ture capable of performing coherent predictionsover hundreds of time steps across a range of par-tially observed 2D and 3D environments.

1. IntroductionConsider a setup in which an agent walks and observes anenvironment (e.g., a three-dimensional maze) for hundredsof time steps, and is then asked to predict subsequent obser-vations given a sequence of actions. This is a challengingtask, as it requires the ability to first remember the visualobservations and the position in which they were observedin the environment, and secondly to predict where a possi-bly long sequence of actions would bring the agent in theenvironment. Building models that can solve this problem

* Work done during an internship at DeepMind. 1TechnicalUniversity of Denmark 2DeepMind, London, UK. Correspondenceto: Marco Fraccaro <[email protected]>.

Proceedings of the 35 th International Conference on MachineLearning, Stockholm, Sweden, PMLR 80, 2018. Copyright 2018by the author(s).

can be useful for model-based reinforcement learning in-volving spatial tasks that require long-term memories andother spatial downstream goals (Sutton, 1990; Deisenroth& Rasmussen, 2011; Levine & Abbeel, 2014; Watter et al.,2015; Wahlstrom et al., 2015; Lenz et al., 2015; Higginset al., 2017; Finn & Levine, 2017). This requires howeveragents that are able to remember the past over hundreds ofsteps, that know both where they are in the environment andhow each action changes their position, and that can coher-ently predict hundreds of steps into the future. Therefore themain focus of this work is to develop an action-conditionedgenerative model that is able to memorize all the requiredinformation while exploring the environment and succes-sively use it in the prediction phase for long-term generationof high-dimensional visual observations.

Recently, several powerful generative models for sequentialdata have been proposed in a wide range of applications,such as modelling speech, handwriting, polyphonic musicand videos (Chung et al., 2015; Fraccaro et al., 2016; Ohet al., 2015; Chiappa et al., 2017). They build on recur-rent neural architectures such as Long Short-Term Memory(LSTM) (Hochreiter & Schmidhuber, 1997) or Gated Recur-rent Units (GRU) (Chung et al., 2014), that use an internalstate vector to perform computations and store the long-term information needed when making predictions. Sincethe number of parameters in these models scales quadrat-ically with the dimensionality of the state vector, they arenot suitable for applications that require high memory ca-pacity, such as the one considered in this paper. The highdimensional state vector needed to be able to memorize thehundreds of time steps in which the agent has visited thewhole environment, make these models in practice both veryslow and hard to train. An alternative approach is to use anexternal memory architecture, for storing of large amount ofinformation while drastically reducing the number of param-eters with respect to models with similar memory capacitythat build on LSTMs or GRUs. Gemici et al. (2017) presenta general architecture for generative temporal models withexternal memory, and test four different types of memo-ries that are dynamically updated at each time step (Graveset al., 2014; 2016; Santoro et al., 2016). They focus ondifferentiable addressing mechanisms for memory storageand retrieval (soft-attention), that are based on deep neural

Page 2: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

networks that learn to write information to the memory andread from it. While this approach is very general and can beused to model complex long-term temporal dependencies ina wide range of applications, it has not been successful inmodeling the data coming from an agent freely moving in a3d maze, even for a single room [private communicationswith the authors of (Gemici et al., 2017)].

To define a scalable model capable of exploring larger envi-ronments and coherently predicting hundreds of time stepsin the future, in this work we build a spatial memory archi-tecture that exploits some knowledge of the specific struc-ture of the problem in consideration. In particular, at eachtime step we split the internal latent representation of thesystem in to two separate vectors, a low-dimensional onethat encodes the position of the agent in the environmentand a high dimensional one that encodes what the agentis seeing. We model the low dimensional dynamics of theagent with a state-space model in which we encode prior in-formation on the physical principles that govern the agent’smovements, and learn a higher dimensional latent represen-tation of the visual input (the frames from the environment)with a Variational Auto-Encoder (Kingma & Welling, 2014;Rezende et al., 2014). While exploring the environment,at each time step we store the position of the agent andthe corresponding visual information in a DifferentiableNeural Dictionary (DND) (Pritzel et al., 2017), a scalablenon-parametric memory developed for episodic control. Theresulting model is able to coherently generate hundreds oftime steps into the future in simulated 3D environments,by retrieving at each time step the observations stored inmemory that were collected when passing in nearby posi-tions during the exploration phase. Making predictions withour model is scalable because of the efficient rollouts ina low dimensional space made possible by the state-spaceassumption and the efficient retrieval of the necessary in-formation from DND. The proposed model can be trainedend-to-end on videos with corresponding action sequencesof agents walking in an environment. Importantly, unlikethe work in (Gemici et al., 2017) we do not need to learn acomplex memory addressing mechanisms, as in our modelthe DND represents a non-parametric component where westore encodings of the positions and visual information thatare learned from the data in an unsupervised way.

2. BackgroundWe now provide a brief overview of the building blocks forthe model introduced in section 3.

Variational auto-encoders. Variational auto-encoders(VAEs) (Kingma & Welling, 2014; Rezende et al., 2014)define a generative model for high-dimensional data xt byintroducing a latent state zt. The joint probability distribu-tion pθ(xt, zt) is factorized as pθ(xt, zt) = pθ(xt|zt)p(zt),

where p(zt) is the prior of the latent state and the decoderpθ(xt|zt) defines a mapping using deep neural networksparameterized by θ from the states zt to the data xt. Ina VAE, the intractable posterior distribution over the la-tent states is approximated using the variational distributionqφ(zt|xt), also known as the encoder or inference network.The parameters θ and φ of the decoder and the encoder, re-spectively, are learned jointly by maximizing the EvidenceLower Bound (ELBO) with stochastic gradient ascent.

DND memory. The Differentiable Neural Dictionary(DND) is a scalable, non-parametric memory module firstintroduced in Reinforcement Learning (RL) to allow agentsto store and retrieve their experiences of an environment(Pritzel et al., 2017). The write operation consists of in-serting (key, value) pairs into the memory; similarly to adictionary, this associates a value to each key. Given a querykey, we can then read from the memory by finding amongthe keys stored in the DND the nearest neighbours to thequery key and returning the corresponding values. The DNDcan be used in applications that require very large memo-ries, since the nearest-neighbour search can be efficientlyapproximated using space-partitioning data structures, suchas kd-trees (Bentley, 1975).

State-space models. State-space models (SSM) are a classof probabilistic graphical models widely used in the tempo-ral setting to model sequences of vectors z1:T = [z1, .., zT ]conditioned on some actions a1:T = [a1, ..,aT ]. SSMs in-troduce at each time step a continuous stochastic variable st,used as a latent representation of the state of the system. Thetemporal dynamics of the system are described by the tran-sition density p(st|st−1,at) of the SSM, that defines how toupdate the state at time t given the previous state st−1 andthe current action at. The output variable zt depends on thestate st through the emission density p(zt|st).

3. ModelAn important component of model-based reinforcementlearning is the ability to plan many steps ahead in timeleveraging previous experiences (Sutton, 1990; Racaniereet al., 2017). This requires agents that can remember thepast and use it to predict what may happen in the futuregiven certain actions. With this purpose in mind, we definean action-conditioned generative model with memory, thatcan be used within RL agents for model-based planning.

The input of our model consists of T -step videos with cor-responding action sequences, generated by an agent actingin an environment. We split each sequence of T time stepsinto two parts, corresponding to two different model phases:

1. Memorization phase. For t = 1, .., τ , the model re-ceives at each time step a frame xt and action at (e.g.move forwards/backwards, rotate left/right) that led to

Page 3: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

st−1 st

Memory Memory

zt−1 zt

xt−1 xt

at−1 at

VAE

SSM

(a) Generative model of the GTM-SM. Red arrows represent de-pendencies on the DND memory.Dashed arrows represent the VAEinference network.

st

s1s2s3s4s5s6s7s8. . .s256

mz1z2z3z4z5z6z7z8. . .z256

d2

d6

d125

s2s6s125

z2z6z125

zt

DND memory

(b) The dependence of zt on st and the DND memory min the VAE prior p(zt|st,m). This corresponds to the redrectangle in Figure 1a. Here, the memory contains thefirst τ = 256 time steps of the video, and we retrieve theK = 3 closest neighbours to st (d is the distance).

st−1 st

zt−1 zt

xt−1 xt

at−1 at

(c) Inference network for the GTM-SM.

Figure 1: Generative Temporal Model with Spatial Memory

it. In this phase, the model has to store in memoryall the information needed in the following predictionphase. During this phase the agent sees most of theenvironment (but from a restricted set of viewpoints),in order to give the model sufficient information tomake accurate predictions in the subsequent phase.

2. Prediction phase. For t = τ + 1, .., T , the modelreceives the actions aτ+1:T that move the data-generating agent across the previously explored en-vironment (although perhaps viewed from a differentangle) and needs to predict the observations xτ+1:T

using the information about the past that is stored inthe memory.

Storing what the agent sees at each time step is not sufficient:in order to retrieve the correct information from memorywhen returning to the same location during the predictionphase, we also need to store where the agent is. The locationof the agent is a latent variable that can be inferred giventhe actions as explained in the rest of this section.

As shown in Figure 1a, in a Generative Temporal Modelwith Spatial Memory (GTM-SM) we introduce two sets oflatent variables that disentangle visual and dynamics infor-mation, similarly to (Fraccaro et al., 2017). At each timestep we have a VAE whose latent state zt is an encodingof the frame of the video and therefore captures the visualinformation. The priors of the VAEs are temporally depen-dent through their dependence on the states st of the SSM,a latent representation of the location of the agent in theenvironment. The transition density of the SSM is used toinclude prior knowledge on the environment dynamics, i.e.the underlying physics.

During the initial memorization phase, the GTM-SM infersthe states s1:τ of the agent (i.e. the position) and the frameencodings z1:τ , and stores these (si, zi) pairs as (key, value)in the DND memory. Probabilistically, we can view this asinserting an approximation of the intractable the posteriorp(s1:τ , z1:τ |x1:τ ,a1:τ ) into the DND; see Section 3.3 fordetails. As the latent variables are stochastic, in practicewe store in memory the sufficient statistics of the distribu-tion (e.g. the mean and variance in the case of Gaussianvariables). To keep the notation simple, we will refer to theinformation in the DND by the name of the random variable(as done in Figure 1b), rather than introducing a new symbolfor the sufficient statistics. An alternative is to insert one ormore samples from the distribution into the memory instead,but this would introduce some sampling noise.

In the subsequent prediction phase, we forward-generatefrom the SSM using the actions aτ+1:T to predict sτ+1:T ,and we use the VAE’s generative model to generate theframes xτ+1:T given the predicted states and the informa-tion from the first τ time steps stored in the DND mem-ory; see Section 3.1 for details. In our experiments, a low-dimensional state vector st (2- or 3-dimensional) suffices.Because of this, we can perform efficient rollouts in latentspace without the need to generate high-dimensional framesat each time step as in autoregressive models (Oh et al.,2015; Chiappa et al., 2017; Gemici et al., 2017). Also,thanks to the scalability properties of the DND memory, wecan efficiently explore very large environments.

There are three key components that define the GTM-SMand that will be introduced in the following, namely the gen-erative model, the inference network, and the past encoder.As we will see, these components share many parameters.

Page 4: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

3.1. Generative model

For brevity, we write the observations and actions in thememorization phase as v = {x1:τ ,a1:τ} and we writethe observations and actions in the prediction phase asx = xτ+1:T and a = aτ+1:T respectively. Letting θ be theparameters of the generative model, we model pθ(x|a,v) asfollows. We introduce two sets of latent variables: the frameencodings z = zτ+1:T and the SSM states s = sτ+1:T , anddefine the joint probability density pθ(x, z, s|a,v) follow-ing the factorization shown in Figure 1a:

pθ(x, z, s|a,v) =T∏

t=τ+1

pθ(xt|zt)pθ(zt|st,m)·

· pθ(st|st−1,at), (1)

where pθ(st|st−1,at) is a Gaussian SSM transition probabil-ity density and pθ(xt|zt) is the VAE decoder (Bernoulli orGaussian distributed, depending on the data). pθ(zt|st,m)can be seen as the prior of the VAE, that is conditioned ateach time step on the current state st and the content of theDND memory m = {s1:τ , z1:τ} = PastEncoder(v) as il-lustrated in Figure 1b (see Section 3.3 for details on the pastencoder). Its sufficient statistics are computed as follows.First, we calculate the distances di = d(si, st), i = 1, .., τ ,between st and all the states si in the DND memory. Wethen retrieve from the memory the K nearest states andthe corresponding frame encodings, thus forming a set oftriplets {(d(k), s(k), z(k)), k = 1, ..,K} that will be used asconditioning variables when computing the parameters ofthe VAE prior pθ(zt|st,m). Using low-dimensional st andprior knowledge of the environment dynamics when defin-ing pθ(st|st−1,at), we can make the GTM-SM learn to usest to represent its position in the environment. At each timestep the model will then retrieve from the memory what ithas seen when it was previously close to the same location,and use this information to generate the current frame xt.The exact form of the VAE prior pθ(zt|st,m) and transitionmodel pθ(st|st−1,at) is environment-dependent, and willbe therefore introduced separately for each experiment inSection 4.

3.2. Inference network

Due to the non-linearities in the VAE and the fact thatpθ(zt|st,m) depends on the DND memory, the poste-rior distribution pθ(z, s|x,a,v) of the GTM-SM is in-tractable. We therefore introduce a variational approxima-tion qφ(z, s|x,a) that factorizes as

qφ(z, s|x,a) = qφ(z|x)pθ(s|a)

=

T∏t=τ+1

qφ(zt|xt)pθ(st|st−1,at) . (2)

A graphical representation of the inference network of theGTM-SM is shown in Figure 1c. qφ(zt|xt) is an inferencenetwork that outputs the mean and variance of a Gaussiandistribution, as typically done in VAEs. In (2) we thenuse the SSM transition probabilities, and we are thereforeassuming that the GTM-SM can learn the prior dynamics ofthe moving agents accurately enough to infer the position ofthe agent given the sequence of actions. Notice that withoutthis assumption it would be impossible to perform longterm generation with the model during the prediction phase.In this phase, we can in fact only rely on the generativemodel, and not on the inference network as we do not knowwhat the agent is seeing at each time step. To relax thisassumption, the inference network could be extended tomake use of the information stored in memory, for exampleby using landmark information when inferring the currentposition of the agent. This is discussed more in detail inAppendix D in the supplementary material, together with aninitial experiment to assess the feasibility of the proposedmethod.

3.3. Past encoder

The past encoder is used during the memorization phaseto extract the information to store in the DND memory. Itcreates a mapping from st to zt that is exploited at times t =τ +1 : T in the generative model. During the memorizationphase, at each time step we store in the DND the sufficientstatistics of the inferred states st and visual information zt,t = 1, .., τ , obtained from an approximation to the smoothedposterior pθ(zt, st|v). We first factorize this distribution aspθ(zt, st|v) = pθ(st|v)pθ(zt|v) and only condition on theinformation up to time t (i.e. we are doing filtering in-stead of smoothing): pθ(zt, st|v) ≈ pθ(st|v1:t)pθ(zt|v1:t),where v1:t = {x1:t,a1:t} Second, we approximate each ofthe terms using the inference network, without introducingany additional parameters in the model. Using the VAEencoder, we have pθ(zt|v1:t) ≈ qφ(zt|xt). We then assumepθ(st|v1:t) ≈ pθ(st|v1:t−1,at) with

pθ(st|v1:t−1,at) =

∫pθ(st|st−1,at)p?θ(st−1) dst−1 ,

p?θ(st) =

∫pθ(st|st−1,at)p?θ(st−1) dst−1 . (3)

p?θ(st) is the marginal distribution of st obtained by inte-grating over the past states (samples from p?θ(st), as neededin the ELBO, are easily obtained with ancestral sampling).

3.4. Training

We learn the parameters θ and φ of the GTM-SM by max-imizing the ELBO, a lower bound to the log-likelihoodlog pθ(x|a,v) obtained using Jensen’s inequality and the

Page 5: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

inference network introduced in Section 3.2:

log pθ(x|a,v) = log

∫pθ(x, z, s|a,v) dz ds

≥ Eqφ(z,s|x,a,v)[log

pθ(x, z, s|a,v)qφ(z, s|x,a,v)

]= F(θ, φ) .

Exploiting the temporal factorization of both the joint dis-tribution pθ(x, z, s|a,v) and the variational approximationqφ(z, s|x,a,v), we obtain after some calculations:

F(θ, φ) =T∑

t=τ+1

Eqφ(zt|xt) [log pθ(xt|zt)] +

− Ep?θ(st) [KL[qφ(zt|xt)||pθ(zt|st,m)]] .

The ELBO is then formed by two terms: a reconstructionterm and an expected KL divergence for the VAE, whichdepends on the transition model through the expectationover the marginal distribution p?θ(st) in (3). F(θ, φ) can bemaximized jointly with respect to θ and φ with stochasticgradient ascent, approximating the intractable expectationswith Monte Carlo integration with a single sample and usingthe reparameterization trick to obtain low-variance gradients(Kingma & Welling, 2014; Rezende et al., 2014). For agiven training sequence, we compute the distribution neededin the ELBO as follows: first, we use the past encoder toextract the information to be stored in the DND memorym. We then obtain samples from the distribution p?θ(st) byiteratively applying the transition model, and use them toread from the DND memory as needed to compute the VAEprior pθ(zt|st,m). Finally, the variational approximationand the likelihood are computed as usual in VAEs.

4. ExperimentsWe test the memorization and long-term generation capabil-ities of the GTM-SM on several 2D and 3D environments ofincreasing complexity. We use videos with action data fromRL agents walking in the environments, that are split so thatboth the memorization and prediction phase are hundreds oftime steps. Experimental details can be found in AppendixA. Videos of long-term generations from the model for allthe experiments are available in the supplementary material,see Appendix B for details.

4.1. Image navigation experiment

In this experiment the data-generating agent walks on topof an image and observes a cropped version of the image(centered at the agent’s position). As illustrated in Figure2a, the 2D environment is a 32x32 image from the CelebAdataset (Liu et al., 2015) and the agent sees an 8x8 crop (theyellow square in the figure). There are five possible actions:move one step up/down/left/right or stay still. At each timestep we sample a random action, but to favor exploring the

whole environment the action is repeated in the subsequenttime steps. The number of repetitions is sampled froma Poisson distribution. The agent cannot walk outside ofthe image: the “move right” action, for example, does notchange the position of an agent on the right edge of theimage. We can interpret this as an environment with walls.The agent walks on the image while adding information inthe DND memory for τ = 256 time steps. During trainingthe prediction phase has 32 time steps; during testing weinstead generate from the model for 256 time steps, so thatT = 512. In each of the two dimensions, there are ninepossible positions (the crops can overlap). This is illustratedin Figure 2b, which shows the ground truth positions thatthe agent has visited in the 256 steps of the memorizationphase of a test sequence.

We use a 2-dimensional state space that the GTM-SM learnsto use to represent the position of the agent. With no wallsin the environment all possible transitions are linear, andthey can be modelled as st = st−1 + Mat + εt withεt ∼ N (0, r2I). In all experiments we use small values forr2, that make the transitions close to being deterministic.The transition matrix M can be learned from the data, anddescribes how to update the state given each of the 5 actions(Mat is the displacement at time t). The environment inour experiment has walls however, and we need the modelto be able to learn not to move with actions that would makeit hit a wall. We do this by multiplying the displacementby a neural network σd that receives as input the projectedposition of the agent after taking the action and outputs avalue between 0 and 1, and that can therefore learn to cancelout any displacements that would bring the agent out ofthe environment. These non-linear transitions are thereforemodelled as

st = st−1 +Mat · σd(st−1 +Mat) + εt . (4)

The VAE prior used in this experiments is obtained by cre-ating a mixture distribution from the sufficient statistics ofthe frame encodings retrieved from the DND memory, seeAppendix A.1 for details.

In Figure 2c we show an example of the states inferred bythe model for a test sequence. We see that the model haslearned the correct transitions, in a state space that is rotatedand stretched with respect to the ground truth one. To testthe memorization and prediction capabilities of the GTM-SM, Figure 2d shows a comparison between the groundtruth frames of the video and the predicted ones during theprediction phase. The model produces almost perfect predic-tions, even after more than 200 generation steps (t = 471).This shows that it has learned to store all relevant informa-tion in the DND, as well as retrieve all relevant informationfrom it. The state-of-the-art generative temporal modelswith memory introduced in (Gemici et al., 2017), are notable to capture the spatial structure of large environments

Page 6: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

Environment

(a) Environment and current observation(the yellow square).

−0.75−0.50−0.25 0.00 0.25 0.50 0.75x

−0.8

−0.6

−0.4

−0.2

0.0

0.2

0.4

0.6

0.8

y

True states

(b) Ground truth positions explored duringthe 256 steps of the memorization phase.The blue square represents the final position.

−0.75 −0.50 −0.25 0.00 0.25 0.50s1

−0.6

−0.4

−0.2

0.0

0.2

0.4

0.6

s 2

Inferred states

(c) Inferred positions during the 256 stepsof the memorization phase, st = (s1, s2).

True

Predicted

t=257......

t=264 t=464 t=471

(d) Comparison between true and predicted frames during the prediction phase of a test sequence (time steps t257:264 and t464:471).

Figure 2: Image navigation experiment

as in the GTM-SM, and would therefore struggle to coher-ently generate hundreds of time steps into the future. TheMNIST maze experiment in (Gemici et al., 2017) can beseen as a simpler version of the image navigation experi-ment presented above, with agents moving on a 4x4 grid,linear transitions and 25-step sequences.

4.2. Labyrinth experiments

We now show that the GTM-SM is able to remember the pastand perform spatio-temporally coherent generations overhundreds of time steps in simulated 3D environments. Weuse the Labyrinth environment (Mnih et al., 2016; Beattieet al., 2016), procedurally-generated 3D mazes with randomtextures, objects and wall configurations. There are eightpossible actions that can both move and rotate the agent inthe maze, and we observe images from a first-person pointof view. Labyrinth can be seen as a 3D extension of theimage navigation experiments in Section 4.1, but in this casethe task is much harder for two main reasons that will betackled below: (1) dealing with rotations and (2) projectivetransformations in a partially observable environment.

First, the state of the agent is no longer only described bythe position, but also from the direction in which the agentis looking and moving. We need to take into account twodifferent coordinate systems, a global one that coincideswith the state-space (s-space), and one that is fixed with theagent (agent-space). In the image navigation experimentsthese coordinate systems coincided. The actions act in agent-space, e.g. a “move right” action will make the agent goright in its reference frame, but depending on its orientation

this could correspond to a move to the left in s-space. Todeal with this issues we can introduce a rotation matrix R inthe state transition equation, that translates a displacementMat in agent-space to a displacements RMat in s-space.More in detail, we consider a 3-dimensional state-space, anddefine the state transition equations as

st = st−1 +R(s(3)t−1)Mat + εt (5)

with s(3)t−1 being the 3rd component of the vector st−1 and

R(s(3)t−1) =

cos(s(3)t−1) − sin(s(3)t−1) 0

sin(s(3)t−1) cos(s

(3)t−1) 0

0 0 1

.

As for the image navigation experiment, we learn the param-eters of M . While we do not explicitly tell the GTM-SM touse the first two component of the state vector as a positionand the third one as an angle, the model will learn to usethem in this way in order to maximize the ELBO. In thenearest neighbor search in the DND memory we need totake into account the periodicity of the angle, e.g. that anagent oriented at 30◦ or 390◦ is actually looking in the samedirection. When computing distances, instead of using s

(3)t

we then use cos(s(3)t ) and sin(s(3)t ), that are possibly passed

together with the first two components of st through a linearlayer that maps the resulting vector in a learned space wherewe use the Euclidean distance.

The second challenge arises from the fact that, unlike theimage navigation experiment where there were a limiteda number of possible positions for the agent, in the 3D

Page 7: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

Observation

−1.0 −0.5 0.0 0.5 1.0

cos(α)−1.00

−0.75

−0.50

−0.25

0.00

0.25

0.50

0.75

1.00

sin(α)

True angle

−1.0 −0.5 0.0 0.5 1.0

cos(s3)−1.00

−0.75

−0.50

−0.25

0.00

0.25

0.50

0.75

1.00

sin(s 3

)

Inferred angle

(a) Rotating agent, environment and angle information for t = 38.The red line denotes the current orientation of the agent, the blackpoints indicate the orientations explored in the memorization phase.α denotes the ground truth orientation.

Observation

200 300 400

x

300

400

500

600

700

800

y

True state

−10 −8 −6 −4 −2 0

s1

−3

−2

−1

0

1

s 2

Inferred state

(b) Walking agent, environment and state information for t = 297.The red circle and line represent the current position and orientationof the agent respectively, the black points indicate the states exploredin the memorization phase.

True

Predicted

t=34......

t=41 t=70 t=77

(c) Rotating agent in Labyrinth, prediction phase of a test sequence. Time steps t34:41 and t70:77.

True

Predicted

t=284 t=299

(d) Walking agent in Labyrinth, prediction phase of a test sequence. Time steps t284:299.

Figure 3: Labyrinth experiment

labyrinth environment it is not reasonable to assume thatduring the memorization phase the agent will pass in allpositions and look from them in all directions. To deal withthis issue, we use as VAE prior pθ(zt|st,m) a GenerativeQuery Network (Eslami et al., 2018), a neural architecturethat given the frames from the closest positions retrievedfrom the DND memory learns to combine the differentviews taking into account projective transformations

4.2.1. ROTATING AGENT IN LABYRINTH

In the first experiment we test the abilities of the GTM-SMto learn to model rotations with the transition model in (5)as well as to combine the information from different views.We use videos with action data of an agent that does twocomplete rotations while standing still in the same position.The rotational period is around 41 time steps, but we onlystore in memory the first τ = 33 (approximately 300◦).We then ask the model to generate the remaining 60◦ tofinish the first rotation and a whole new rotation. FromFigure 3a we see an example of an observation from thetest data and that the model has correctly learned to usethe third component of the state vector st to represent theorientation of the agent. In the prediction phase in Figure 3c,we notice that the predictions from the model are very closeto the ground truth, meaning that the model has learned to

use the memory correctly. In particular, despite the factthat the frames from t = 33 to t = 41 were never seenduring the memorization phase, the GTM-SM has learnedto combine the information from other views. Notice thatthis experiment can be seen as a more challenging version ofthe Labyrinth rotation experiment of (Gemici et al., 2017),that used a fully observed first rotation with a rotationalperiod of 15 time steps.

4.2.2. WALKING AGENT IN LABYRINTH

We now use videos of a pre-trained RL agent walking ina room and solving a scavenger hunt task. In this case itis fundamental to extend the transition equation in (5) tomodel more carefully the physics of the walking agent, thatmake the displacement at a given time step depend not onlyon the current action, but also on the displacement at theprevious time step. The agent is subject to momentum andfriction, so that if it is moving in a certain direction at timet−1, it will still continue to move a bit in the same directioneven at time t, regardless of the action at. Also, despite themomentum, the displacement of the agent cannot increaseindefinitely, i.e. there is saturation. We can model this byextending the way the displacement dt =Mat is calculatedin (5). To take into account momentum and friction, we firstadd to dt =Mat a damped version of the displacement at

Page 8: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

the previous time step, i.e. σ(cf ) � dt−1, where σ(cf ) isa learned vector between 0 and 1 of the same size of dt (σis the sigmoid function and � represents the element-wiseproduct). To deal with saturation, we then limit the rangeof the displacements by squashing them through a tanhnon-linearity that is pre-multiplied by a learned vector cs.The resulting transition model then becomes

st = st−1+R(s(3)t−1) cs � tanh(σ(cf )� dt−1 +Mat)︸ ︷︷ ︸

dt

+εt

with M , cs and cf parameters to be learned. Notice thatmodelling the displacements in this way essentiallycorresponds to extending the state space with an additionalvector dt that models the velocities of the agent. Wecan then model accelerations/decelerations of the agentby non-linearly changing the velocities over time in thisextended state-space. This is a very challenging experiment,as from only one-hot encoded actions and the frames ofthe video we need to learn a complex transition model.Moreover, due to the low resolution of the images (32x32),small variations in state-space may be impossible toinfer using only the images. To solve this, we make thereasonable assumption that at training time the agent feelsits movement, i.e. the displacements. We then add aregression loss as an extra term to the objective function,that helps the GTM-SM to learn transition parameters suchthat the estimated dt is close to its true value. We only addthe true displacements information as a target in the loss,and never pass it directly into the model.

We let the agent walk around the room while adding infor-mation in the DND memory for τ = 150 time steps, andthen predict during testing the following 150 time steps(T = 300). In Figure 3b, we notice that the GTM-SM isable to learn a very accurate transition model, that providesa sufficiently good approximation of the true state even aftert = 297 time steps. In Figure 3d we can appreciate thememorization and long-term generation capabilities of theGTM-SM by looking at the comparison between the trueand predicted frames of the video in the end of the predictionphase (t284:299). We also notice in the predicted frames, thatthe model correctly draws the walls and the floors but fails torender the objects, probably due to difficulties in modellingwith the VAE the very diverse and complex textures thatform objects in this environment. We also tested the sametrained model on longer videos of larger environments withmultiple rooms (τ = 150 and T = 450). As explained indetail in Appendix C, the model is able to correctly predictthe textures of the environment even after 300 time steps.

5. Related workA number of recent works have augmented deep generativemodels with learned external memories, both in the staticsetting (Li et al., 2016; Bornschein et al., 2017) and in the

temporal one (Gemici et al., 2017). More in general, neuralnetworks have been combined with different memories ina wide range of tasks such as supervised learning (Graveset al., 2014; 2016), reinforcement learning (Oh et al., 2016;Pritzel et al., 2017; Parisotto & Salakhutdinov, 2018), one-shot learning (Santoro et al., 2016), question answering andlanguage modelling (Sukhbaatar et al., 2015; Miller et al.,2016). Each memory architecture uses different addressingmechanisms to write or read information, that are usuallychosen depending on the specific application being consid-ered. As discussed in the introduction, our work is closelyrelated to (Gemici et al., 2017), but more suitable for long-term generation for this task and more scalable thanks to theusage of the spatial memory architecture that exploits knowl-edge on the dynamics of the agent and does not require tolearn a parametric memory addressing scheme.

In the deep reinforcement learning community, severalworks have exploited different memory architectures to storelong term information to be used within an agent’s policy,such as in (Zaremba & Sutskever, 2015; Oh et al., 2016).In particular, in (Gupta et al., 2017a;b; Zhang et al., 2017;Parisotto & Salakhutdinov, 2018) the memory architectureshave a fixed number of slots that are spatially structured as a2D grid, and can therefore store information on the movingagent. Similarly to the GTM-SM, these memories are builtto exploit the spatial structure of the problem, although forthe different task of constructing agents that can learn tonavigate and explore the environment, as opposed to thefocus on generative modelling of this paper. SimultaneousLocalization And Mapping (SLAM) (Smith et al., 1987;Leonard & Durrant-Whyte, 1991) is a popular techniqueused in robotics to estimate the position of a robot and themap of the environment at the same time using sensor data,recently applied to deep reinforcement learning for examplein (Bhatti et al., 2016; Zhang et al., 2017). It is reminis-cent to the memorization phase of the GTM-SM, that couldbe therefore extended using ideas introduced in the visualSLAM community (Taketomi et al., 2017).

6. ConclusionIn this work we introduced an action-conditioned generativemodel that uses a scalable non-parametric memory to storespatial and visual information. Our experiments on simu-lated 2D and 3D environments show that the model is ableto coherently memorize and perform long-term generation.To our knowledge this is the first published work that buildsa generative model for agents walking in an environmentthat, thanks to the separation of the dynamics and visualinformation in the DND memory, can coherently generatefor hundreds of time steps in a scalable way. Future workwill focus on exploiting these capabilities in model-basedplanning by integrating the GTM-SM within an RL agent.

Page 9: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

AcknowledgementsWe would like to thank Charles Blundell and Timothy Lilli-crap for many helpful discussions, and Ivo Danihelka andJoshua Abramson for generating the datasets used in theLabyrinth experiments.

ReferencesAbadi, M., Agarwal, A., Barham, P., Brevdo, E., Chen, Z.,

Citro, C., Corrado, G. S., Davis, A., Dean, J., Devin, M.,Ghemawat, S., Goodfellow, I., Harp, A., Irving, G., Isard,M., Jia, Y., Jozefowicz, R., Kaiser, L., Kudlur, M., Lev-enberg, J., Mane, D., Monga, R., Moore, S., Murray, D.,Olah, C., Schuster, M., Shlens, J., Steiner, B., Sutskever,I., Talwar, K., Tucker, P., Vanhoucke, V., Vasudevan,V., Viegas, F., Vinyals, O., Warden, P., Wattenberg, M.,Wicke, M., Yu, Y., and Zheng, X. TensorFlow: Large-scale machine learning on heterogeneous systems, 2015.

Beattie, C., Leibo, J. Z., Teplyashin, D., Ward, T., Wain-wright, M., Kuttler, H., Lefrancq, A., Green, S., Valdes,V., Sadik, A., Schrittwieser, J., Anderson, K., York, S.,Cant, M., Cain, A., Bolton, A., Gaffney, S., King, H.,Hassabis, D., Legg, S., and Petersen, S. DeepMind Lab.CoRR, abs/1612.03801, 2016.

Bentley, J. Multidimensional binary search trees used forassociative searching. Commun. ACM, 1975.

Bhatti, S., Desmaison, A., Miksik, O., Nardelli, N.,Siddharth, N., and Torr, P. H. S. Playing doomwith slam-augmented deep reinforcement learning.arXiv:1612.00380, 2016.

Bornschein, J., Mnih, A., Zoran, D., and Jimenez Rezende,D. Variational memory addressing in generative models.In Advances in Neural Information Processing Systems30. 2017.

Chiappa, S., Racaniere, S., Wierstra, D., and Mohamed, S.Recurrent environment simulators. In ICLR, 2017.

Chung, J., Gulcehre, C., Cho, K., and Bengio, Y. Empiricalevaluation of gated recurrent neural networks on sequencemodeling. arXiv:1412.3555, 2014.

Chung, J., Kastner, K., Dinh, L., Goel, K., Courville, A. C.,and Bengio, Y. A recurrent latent variable model forsequential data. In NIPS, 2015.

Deisenroth, M. P. and Rasmussen, C. E. Pilco: A model-based and data-efficient approach to policy search. InProceedings of the 28th International Conference on In-ternational Conference on Machine Learning, ICML’11,2011.

Eslami, S. M. A., Jimenez Rezende, D., Besse, F., Viola,F., Morcos, A. S., Garnelo, M., Ruderman, A., Rusu,A. A., Danihelka, I., Gregor, K., Reichert, D. P., Buesing,L., Weber, T., Vinyals, O., Rosenbaum, D., Rabinowitz,N., King, H., Hillier, C., Botvinick, M., Wierstra, D.,Kavukcuoglu, K., and Hassabis, D. Neural scene repre-sentation and rendering. Science, 2018. ISSN 0036-8075.

Finn, C. and Levine, S. Deep visual foresight for planningrobot motion. In Robotics and Automation (ICRA), 2017IEEE International Conference on, pp. 2786–2793. IEEE,2017.

Fraccaro, M., Sønderby, S. K., Paquet, U., and Winther, O.Sequential neural models with stochastic layers. In NIPS,2016.

Fraccaro, M., Kamronn, S., Paquet, U., and Winther, O. Adisentangled recognition and nonlinear dynamics modelfor unsupervised learning. Advances in Neural Informa-tion Processing Systems 30, NIPS, 2017.

Gemici, M., Hung, C., Santoro, A., Wayne, G., Mohamed,S., Rezende, D. J., Amos, D., and Lillicrap, T. P. Genera-tive temporal models with memory. arXiv:1702.04649,2017.

Graves, A., Wayne, G., and Danihelka, I. Neural turingmachines. arXiv:1410.5401, 2014.

Graves, A., Wayne, G., Reynolds, M., Harley, T., Danihelka,I., Grabska-Barwiska, A., Colmenarejo, S. G., Grefen-stette, E., Ramalho, T., Agapiou, J., Badia, A. P., Her-mann, K. M., Zwols, Y., Ostrovski, G., Cain, A., King,H., Summerfield, C., Blunsom, P., Kavukcuoglu, K., andHassabis, D. Hybrid computing using a neural networkwith dynamic external memory. Nature, 2016.

Gupta, S., Davidson, J., Levine, S., Sukthankar, R., andMalik, J. Cognitive mapping and planning for visualnavigation. In Proceedings of the IEEE Conference onComputer Vision and Pattern Recognition, 2017a.

Gupta, S., Fouhey, D., Levine, S., and Malik, J. Unify-ing map and landmark based representations for visualnavigation. arXiv:1712.08125, 2017b.

Higgins, I., Pal, A., Rusu, A. A., Matthey, L., Burgess, C.,Pritzel, A., Botvinick, M., Blundell, C., and Lerchner, A.DARLA: improving zero-shot transfer in reinforcementlearning. In Proceedings of the 34th International Con-ference on Machine Learning, ICML 2017, Sydney, NSW,Australia, 6-11 August 2017, 2017.

Hochreiter, S. and Schmidhuber, J. Long short-term memory.Neural Computation, 9(8):1735–1780, 1997.

Page 10: Generative Temporal Models with Spatial Memory for ...proceedings.mlr.press/v80/fraccaro18a/fraccaro18a.pdfGenerative Temporal Models with Spatial Memory for Partially Observed Environments

Generative Temporal Models with Spatial Memory

Kingma, D. and Ba, J. Adam: A method for stochasticoptimization. arXiv:1412.6980, 2014.

Kingma, D. and Welling, M. Auto-encoding variationalBayes. In ICLR, 2014.

Lenz, I., Knepper, R. A., and Saxena, A. Deepmpc: Learn-ing deep latent features for model predictive control. InRobotics: Science and Systems, 2015.

Leonard, J. J. and Durrant-Whyte, H. F. Mobile robot lo-calization by tracking geometric beacons. IEEE Trans.Robotics and Automation, 1991.

Levine, S. and Abbeel, P. Learning neural network policieswith guided policy search under unknown dynamics. InAdvances in Neural Information Processing Systems 27.2014.

Li, C., Zhu, J., and Zhang, B. Learning to generate withmemory. In Proceedings of the 33nd International Con-ference on Machine Learning, ICML, 2016.

Liu, Z., Luo, P., Wang, X., and Tang, X. Deep learning faceattributes in the wild. In Proceedings of InternationalConference on Computer Vision (ICCV), 2015.

Miller, A. H., Fisch, A., Dodge, J., Karimi, A.-H., Bor-des, A., and Weston, J. Key-value memory networks fordirectly reading documents. In EMNLP, 2016.

Mnih, V., Badia, A. P., Mirza, M., Graves, A., Lillicrap,T., Harley, T., Silver, D., and Kavukcuoglu, K. Asyn-chronous methods for deep reinforcement learning. InICML, 2016.

Oh, J., Guo, X., Lee, H., Lewis, R. L., and Singh, S. Action-conditional video prediction using deep networks in atarigames. In NIPS, 2015.

Oh, J., Chockalingam, V., Singh, S. P., and Lee, H. Controlof memory, active perception, and action in minecraft.In Proceedings of the 33nd International Conference onMachine Learning, ICML, 2016.

Parisotto, E. and Salakhutdinov, R. Neural map: Structuredmemory for deep reinforcement learning. In ICLR, 2018.

Pritzel, A., Uria, B., Srinivasan, S., Badia, A. P., Vinyals,O., Hassabis, D., Wierstra, D., and Blundell, C. Neuralepisodic control. In Proceedings of the 34th Interna-tional Conference on Machine Learning, Proceedings ofMachine Learning Research, 2017.

Racaniere, S., Weber, T., Reichert, D., Buesing, L., Guez, A.,Jimenez Rezende, D., Puigdomenech Badia, A., Vinyals,O., Heess, N., Li, Y., Pascanu, R., Battaglia, P., Hassabis,D., Silver, D., and Wierstra, D. Imagination-augmentedagents for deep reinforcement learning. In Advances inNeural Information Processing Systems 30. 2017.

Rezende, D. J., Mohamed, S., and Wierstra, D. Stochasticbackpropagation and approximate inference in deep gen-erative models. In ICML, 2014. ISBN 9781634393973.

Santoro, A., Bartunov, S., Botvinick, M., Wierstra, D.,and Lillicrap, T. P. One-shot learning with memory-augmented neural networks. arXiv:1605.06065, 2016.

Smith, R., Self, M., and Cheeseman, P. Estimating uncertainspatial relationships in robotics. In Proceedings. 1987IEEE International Conference on Robotics and Automa-tion, 1987.

Sukhbaatar, S., Szlam, A., Weston, J., and Fergus, R. End-to-end memory networks. In Neural Information ProcessingSystems, NIPS’15, 2015.

Sutton, R. S. Integrated architectures for learning, planning,and reacting based on approximating dynamic program-ming. In In Proceedings of the Seventh InternationalConference on Machine Learning, 1990.

Taketomi, T., Uchiyama, H., and Ikeda, S. Visual slam algo-rithms: a survey from 2010 to 2016. IPSJ Transactionson Computer Vision and Applications, 9:1–11, 2017.

Wahlstrom, N., Schon, T. B., and Deisenroth, M. P. Frompixels to torques: Policy learning with deep dynamicalmodels. arXiv:1502.02251, 2015.

Watter, M., Springenberg, J., Boedecker, J., and Riedmiller,M. Embed to control: A locally linear latent dynamicsmodel for control from raw images. In NIPS, 2015.

Zaremba, W. and Sutskever, I. Reinforcement learningneural turing machines. arXiv:1505.00521, 2015.

Zhang, J., Tai, L., Boedecker, J., Burgard, W., and Liu, M.Neural slam: Learning to explore with external memory.arXiv:1706.09520, 2017.