SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Alejandro Escontrela\* Ademi Adeniji Wilson Yan
Ajay Jain Xue Bin Peng Ken Goldberg
Youngwoon Lee Danijar Hafner Pieter Abbeel
**Specifying reward signals that allow agents to learn complex behaviors is a longstanding challenge in reinforcement learning.** A promising approach is to extract preferences for behaviors from unlabeled videos, which are widely available on the internet. We present Video Prediction Rewards (VIPER), an algorithm that leverages pretrained video prediction models as action-free reward signals for reinforcement learning. Specifically, we first train an autoregressive transformer on expert videos and then use the video prediction likelihoods as reward signals for a reinforcement learning agent. VIPER enables expert-level control without programmatic task rewards across a wide range of DMC, Atari, and RL-Bench tasks. Moreover, generalization of the video prediction model allows us to derive rewards for an out-of-distribution environment where no expert data is available, enabling cross-embodiment generalization for tabletop manipulation. We see our work as starting point for scalable reward specification from unlabeled videos that will benefit from the rapid advances in generative modeling. Source code and datasets are available on the project website: https://escontrela.me/viper
Manually designing a reward function is laborious and often leads to undesirable outcomes [32]. This is a major bottleneck for developing general decision making agents with reinforcement learning (RL). A more scalable approach is to learn complex behaviors from videos, which can be easily acquired at scale for many applications (e.g., Youtube videos).
Previous approaches that learn behaviors from videos reward the similarity between the agent's current observation and the expert data distribution [37, 38, 30, 43]. Since their rewards only condition on the current observation, they cannot capture temporally meaningful behaviors. Moreover, the approaches with adversarial training schemes [30, 43] often result in mode collapse, which hinders generalization.
Figure 1: VIPER uses the next-token likelihoods of a frozen video prediction model as a general reward function for various tasks.
*Corresponding author: escontrela@berkeley.edu
(a) DeepMind Control Suite (15 tasks)
Figure 2: VIPER achieves expert-level control directly from pixels without access to ground truth rewards or expert actions on 28 reinforcement learning benchmark tasks.
(b) Robot Learning Benchmark (6 tasks)
(c) Atari (7 tasks)
Other works fill in actions for the (action-free) videos using an inverse dynamics model [42, 8]. Dai et al. [8] leverage recent advances in generative modeling to capture multi-modal and temporally-coherent behaviors from large-scale video data. However, this multi-stage approach requires performing expensive video model rollouts to then label actions with a learned inverse dynamics model.
In this paper, we propose using Video Prediction Rewards (VIPER) for reinforcement learning, VIPER first learns a video prediction model from expert videos. We then train an agent using reinforcement learning to maximize the log-likelihood of agent trajectories estimated by the video prediction model, as illustrated in Figure 1. Directly leveraging the video model's likelihoods as a reward signal encourages the agent to match the video model's trajectory distribution. Additionally, rewards specified by video models inherently measure the temporal consistency of behavior, unlike observation-level rewards. Further, evaluating likelihoods is significantly faster than performing video model rollouts, enabling faster training times and more interactions with the environment.
We summarize the three key contributions of this paper as follows:
- We present VIPER: a novel, scalable reward specification algorithm which leverages rapid improvements in generative modeling to provide RL agents with rewards from unlabeled videos.
- We perform an extensive evaluation, and show that VIPER can achieve expert-level control without task rewards on 15 DMC tasks [44], 6 RLBench tasks [20], and 7 Atari tasks [3] (see examples in Figure 2 and Appendix A.5).
- We demonstrate that VIPER generalizes to different environments for which no training data was provided, enabling cross-embodiment generalization for tabletop manipulation.
Along the way, we discuss important implementation details that improve the robustness of VIPER.
Learning from observations is an active research area which has led to many advances in imitation learning and inverse reinforcement learning [27, 1, 49]. This line of research is motivated by the fact that learning policies from expert videos is a scalable way of learning a wide variety of complex tasks, which does not require access to ground truth rewards, expert actions, or interaction with the expert. Scalable solutions to this problem would enable learning policies using the vast quantity of videos on the internet. Enabling policy learning from expert videos can be largely categorized into two approaches: (1) Behavioral cloning [31] on expert videos labelled with predicted actions and (2) reinforcement learning with a reward function learned from expert videos.
Labelling videos with predicted actions An intuitive way of leveraging action-free expert videos is to guess which action leads to each transition in expert videos, then to mimic the predicted actions. Labelling videos with actions can be done using an inverse dynamics model,  $p(a|s,s')$ , which models an action distribution given the current and next observations. An inverse dynamics model can be learned from environment interactions [28, 42, 29] or a curated action-labelled dataset [2, 34]. Offline reinforcement learning [24] can be also used instead of behavioral cloning for more efficient use of video data with predicted actions [34]. However, the performance of this approach heavily depends on the quality of action labels predicted by an inverse dynamics model and the quantity and diversity of training data.
Reinforcement learning with videos Leveraging data from online interaction can further improve policies trained using unlabelled video data. To guide policy learning, many approaches have learned a reward function from videos by estimating the progress of tasks [37, 38, 25, 23] or the divergence between expert and agent trajectories [43, 30]. Adversarial imitation learning approaches [43, 30] learn a discriminator that discriminates transitions from the expert data and the rollouts of the current policy. Training a policy to maximize the discriminator error leads to similar expert and policy behaviors. However, the discriminator is prone to mode collapse, as it often finds spurious associations between task-irrelevant features and expert/agent labels [50, 19], which requires a variety of techniques to stabilize the adversarial training process [7]. In contrast, VIPER directly models the expert video distribution using recent generative modeling techniques [10, 47], which offers stable training and strong generalization.
Using video models as policies Recently, UniPi [8] uses advances in text-conditioned video generation models [33] to plan a trajectory. Once the future video trajectory is generated, UniPi executes the plan by inferring low-level controls using a learned inverse dynamics model. Instead of using slow video generations for planning, VIPER uses video prediction likelihoods to guide online learning of a policy.
3 Video Prediction Rewards
In this section, we propose Video Prediction Rewards (VIPER), which learns complex behaviors by leveraging the log-likelihoods of pre-trained video prediction models for reward specification. Our method does not require any ground truth rewards or action annotations, and only requires videos of desired agent behaviors. VIPER implements rewards as part of the environment, and can be paired with any
RL algorithm. We overview the key components of our method below.
Algorithm 1: VIPER
Train video prediction model  $p_{\theta}$  on expert videos.
while not converged do
Choose action:  $a_{t}\sim \pi (x_{t})$
Step environment:  $x_{t + 1} \gets \mathrm{env}(a_t)$
Fill in reward:  $r_t \gets \ln p_\theta(x_{t+1} \mid x_{t-k:t}) + \beta r_t^{\mathrm{expl}}$
Add transition  $(x_{t},a_{t},r_{t},x_{t + 1})$  to replay buffer.
Train  $\pi$  from replay buffer using any RL algorithm.
3.1 Video Modeling
Likelihood-based video models are a popular paradigm of generative models trained to model the data distribution by maximizing an exact or lower bound on the log-likelihood of the data. These models have demonstrated their ability to fit highly multi-modal distributions and produce samples depicting complex dynamics, motion, and behaviors [40, 18, 17, 45].
Our method can integrate any video model that supports computing likelihoods over the joint distribution factorized in the following form:
$$
\log p \left(x _ {1: T}\right) = \sum_ {t = 1} ^ {T} \log p \left(x _ {t} \mid x _ {1: t - 1}\right), \tag {1}
$$
where  $x_{1:T}$  is the full video consisting of  $T$  frames,  $x_1,\ldots ,x_T$ . When using video models with limited context length  $k$ , it is common to approximate likelihoods of an entire video sequence with its subsequences of length  $k$  as follows:
$$
\log p \left(x _ {1: T}\right) \approx \sum_ {t = 1} ^ {T} \log p \left(x _ {t} \mid x _ {\max  (1, t - k): t - 1}\right). \tag {2}
$$
In this paper, we use an autoregressive transformer model based on VideoGPT [47, 36] as our video generation model. We first train a VQ-GAN [10] to encode individual frames  $x_{t}$  into discrete codes  $z_{t}$ . Next, we learn an autoregressive transformer to model the distribution of codes  $z$  through the following maximum likelihood objective:
$$
\max  _ {\theta} \sum_ {t = 1} ^ {T} \sum_ {i = 1} ^ {Z} \log p _ {\theta} \left(z _ {t} ^ {i} \mid z _ {t} ^ {1: i - 1}, z _ {1: t - 1}\right), \tag {3}
$$
Figure 3: Aggregate results across 15 DMC tasks, 7 Atari games, and 6 RLBench tasks. DMC results are provided for DrQ and DreamerV3 (Dv3) RL agents. Atari and RLBench results are reported for DreamerV3. Atari scores are computed using Human-Normalized Mean.
where  $z_{t}^{i}$  is the  $i$ -th code of the  $t$ -th frame, and  $Z$  is the total number of codes per frame. Computing the exact conditional likelihoods  $p_{\theta}(x_{t} \mid x_{1:t-1})$  is intractable, as it requires marginalizing over all possible combinations of codes. Instead, we use the conditional likelihoods over the latent codes  $p_{\theta}(z_{t} \mid z_{1:t-1})$  as an approximation. In our experiments, we show that these likelihoods are sufficient to capture the underlying dynamics of the videos.
Note that our choice of video model does not preclude the use of other video generation models, such as MaskGIT-based models [48, 45, 13] or diffusion models [18, 40]. However, we opted for an autoregressive model due to the favorable properties of being able to model complex distributions while retaining fast likelihood computation. Video model comparisons are performed in Section 4.5.
3.2 Reward Formulation
Given a pretrained video model, VIPER proposes an intuitive reward that maximizes the conditional log-likelihoods for each transition  $(x_{t},a_{t},x_{t + 1})$  observed by the agent:
$$
r _ {t} ^ {\text {V I P E R}} \doteq \ln p _ {\theta} \left(x _ {t + 1} \mid x _ {1: t}\right). \tag {4}
$$
This reward incentivizes the agent to find the most likely trajectory under the expert video distribution as modeled by the video model. However, the most probable sequence does not necessarily capture the distribution of behaviors we want the agent to learn.
For example, when flipping a weighted coin with  $p(\text{heads} = 0.6)$  1000 times, typical sequences will count roughly 600 heads and 400 tails, in contrast to the most probable sequence of 1000 heads that will basically never be seen in practice [39]. Similarly, the most likely image under a density model trained on MNIST images is often the image of only background without a digit, despite this never occurring in the dataset [26]. In the reinforcement learning setting, an additional issue is that solely optimizing a dense reward such as  $r_t^{\text{VIPER}}$  can lead to early convergence to local optima.
To overcome these challenges, we take the more principled approach of matching the agent's trajectory distribution  $q(x_{1:T})$  to the sequence distribution  $p_{\theta}(x_{1:T})$  of the video model by minimizing the KL-divergence between the two distributions [41, 15]:
$$
\begin{array}{l} \operatorname {K L} \left[ q \left(x _ {1: T}\right) \| p \left(x _ {1: T}\right) \right] = \underbrace {\operatorname {E} _ {q} \left[ - \ln p _ {\theta} \left(x _ {1 : T}\right) \right]} _ {\text {c r o s s - e n t r o p y}} - \underbrace {\operatorname {H} \left[ q \left(x _ {1 : T}\right) \right]} _ {\text {e n t r o p y}} \\ = - \sum_ {t = 1} ^ {T} \left[ \mathrm {E} _ {q} \left[ \underbrace {\ln p _ {\theta} \left(x _ {t + 1} \mid x _ {1 : t}\right)} _ {r _ {t} ^ {\mathrm {V I P E R}}} \right] + \underbrace {\mathrm {H} \left[ q \left(x _ {t + 1} \mid x _ {1 : t}\right) \right]} _ {\text {e x p l o r a t i o n t e r m}} \right], \tag {5} \\ \end{array}
$$
The KL objective shows that for the agent to match the video distribution under the video prediction model, it has to not only maximize the VIPER reward but also balance this reward while maintaining high entropy over its input sequences [21]. In the reinforcement learning literature, the entropy bonus over input trajectories corresponds to an exploration term that encourages the agent to explore and inhabit a diverse distribution of sequences within the regions of high probability under the video prediction model. This results in the final reward function that the agent maximizes:
$$
r _ {t} ^ {\mathrm {K L}} \doteq r _ {t} ^ {\text {V I P E R}} + \beta r _ {t} ^ {\text {e x p l}}, \tag {6}
$$
where  $\beta$  determines the amount of exploration. To efficiently compute the likelihood reward, we approximate the context frames with a sliding window as discussed in Equation 2:
$$
r _ {t} ^ {\text {V I P E R}} \approx \ln p _ {\theta} \left(x _ {t + 1} \mid x _ {\max  (1, t - k): t}\right). \tag {7}
$$
3.3 Data Curation
In this work, we explore whether VIPER provides adequate reward signal for learning low-level control. We utilize the video model likelihoods provided by an autoregressive video prediction model pre-trained on data from a wide variety of environments. We curate data by collecting expert video trajectories from task oracles and motion planning algorithms with access to state information. Fine-tuning large text-conditioned video models [33, 45, 40] on expert videos would likely lead to improved generalization performance beyond the curated dataset, and would make for an interesting future research direction. We explore the favorable generalization capabilities of video models trained on small datasets, and explore how this leads to more general reward functions in Section 4.3.
We evaluate VIPER on 28 different tasks across the three domains shown in Figure 2. We utilize 15 tasks from the DeepMind Control (DMC) suite [44], 7 tasks from the Atari Gym suite [4], and 6 tasks from the Robot Learning Benchmark (RLBench) [20]. We compare VIPER agents to variants of Adversarial Motion Priors (AMP) [30], which uses adversarial training to learn behaviors from reward-free and action-free expert data. All agents are trained using raw pixel observations from the environment, with no access to state information or task rewards. In our experiments, we aim to answer the following questions:
1. Does VIPER provide an adequate learning signal for solving a variety of tasks? (Section 4.2)
2. Do video models trained on many different tasks still provide useful rewards? (Section 4.2)
3. Can the rewards generalize to novel scenarios where no expert data is available? (Section 4.3)
4. How does the video model data quality and quantity affect the learned rewards? (Section 4.4)
5. What implementation details matter when using video model likelihoods as rewards? (Section 4.5)
4.1 Video Model Training Details
Training data To collect expert videos in DMC and Atari tasks, Task Oracle RL agents are trained until convergence with access to ground truth state information and task rewards. After training, videos of the top  $k$  episodes out of 1000 episode rollouts for each task are sampled as expert videos, where  $k = 50$  and 100 for DMC and Atari, respectively. For RLBench, a sampling-based motion planning algorithm with access to full state information is used to gather 100 demos for each task, where selected tasks range from "easy" to "medium".
Video model training We train a single autoregressive video model for each suite of tasks. Example images for each suite are shown in Appendix A.5. For DMC, we train a single VQ-GAN across all tasks that encodes  $64 \times 64$  images to  $8 \times 8$  discrete codes. Similarly for RLBench and Atari, we train VQ-GANs across all tasks within each domain, but encode  $64 \times 64$  images to  $16 \times 16$  discrete codes. In practice, the level of VQ compression depends on the visual complexity of the environment – more texturally simple environments (e.g., DMC) allow for higher levels of spatial compression. We follow the original VQ-GAN architecture [10], consisting of a
CNN encoder and decoder. We train VQ-GAN with a batch size of 128 and learning rate  $10^{-4}$  for 200k iterations.
To train our video models, we encode each frame of the trajectory and stack the codes temporally to form an encoded 3D video tensor. Similar to VideoGPT [47], the encoded VQ codes for all video
Figure 1 shows the process of computing rewards using log probabilities under the video prediction model. VIPER is agnostic to the choice of exploration reward and in this paper we opt for Plan2Explore [35] and RND [5].
T=0
T = 16
T = 32
T = 48
T = 64
Figure 4: Video model rollouts for 3 different evaluation environments.
a batch size of 128 and learning rate  $10^{-4}$  for
frames are jointly modeled using an autoregressive transformer in raster scan order. For DMC and Atari, we train on 16 frames at a time with a one-hot label for task conditioning. For RLBench, we train both single-task and multi-task video models on 4 frames with frame skip 4. We add the one-hot task label for the multi-task model used in our cross-embodiment generalization experiments in Section 4.3. We perform task conditioning identical to the class conditioning mechanism in VideoGPT, with learned gains and biases specific to each task ID for each normalization layer.
Figure 4 shows example video model rollouts for each domain. In general, our video models are able to accurately capture the dynamics of each environment to produce probable futures. Further details on model architecture and hyperparameters can be found in Appendix A.2. All models are trained on TPUv3-8 instances which are approximately similar to 4 Nvidia V100 GPUs.
4.2 Video Model Likelihoods as Rewards for Reinforcement Learning
To learn behaviors from expert videos, we provide reward signals using Equation 6 in VIPER and the discriminator error in AMP. Both VIPER and AMP are agnostic to the choice of RL algorithm; but, in this paper, we evaluate our approach and baselines with two popular RL algorithms: DrQ [22] and DreamerV3 [16]. We use Random Network Distillation [5] (model-free) as the exploration objective for DrQ, and Plan2Explore [35] (model-based) for DreamerV3. Hyperparameters for each choice of RL algorithm are shown in Appendix A.3. We compare VIPER to two variants of the AMP algorithm: the single-task variant where only expert videos for the specific task are provided to the agent, and the multi-task case where expert videos for all tasks are provided to the agent.
As outlined in Algorithm 1, we compute Equation 6 for every environment step to label the transition with a reward before adding it to the replay buffer for policy optimization. In practice, batching Equation 6 across multiple environments and leveraging parallel likelihood evaluation leads to only a small decrease in training speed. DMC agents were trained using 1 Nvidia V100 GPU, while Atari and RLBench agents were trained using 1 Nvidia A100 GPU.
Figure 5: VIPER incentivizes the agent to maximize trajectory likelihood under the video model. As such, it provides high rewards for reference (expert) sequences, and low rewards for unlikely behaviors. Mean rewards  $r^{\mathrm{VIPER}}$  and returns are computed across 40 trajectories.
We first verify whether VIPER provides meaningful rewards aligned with the ground truth task rewards. Figure 5 visualizes the ground truth task rewards and our log-likelihood rewards (Equation 7) for a reference (expert) trajectory and a random out-of-distribution trajectory. In the reward curves on the left, VIPER starts to predict high rewards for the expert transitions once the transitions become distinguishable from a random trajectory, while it consistently outputs low rewards for out-of-distribution transitions. The return plot on the right clearly shows positive correlation between returns of the ground truth reward and our proposed reward.
Then, we measure the task rewards when training RL agents with predicted rewards from VIPER and baselines and report the aggregated results for each suite and algorithm in Figure 3. The full results can be found in Appendix A.4.
In DMC, VIPER achieves near expert-level performance from pixels with our video prediction rewards alone. Although VIPER slightly underperforms Task Oracle, this is surprising as the Task Oracle uses full state information along with dense task rewards. VIPER outperforms both variants of AMP. Worth noting is the drastic performance difference between the single-task and multi-task AMP algorithms. This performance gap can possibly be attributed to mode collapse, whereby the discriminator classifies all frames for the current task as fake samples, leading to an uninformative reward signal for the agent. Likelihood-based video models, such as VIPER, are less susceptible to mode collapse than adversarial methods.
In Atari, VIPER approaches the performance of the Task Oracle trained with the original sparse task reward, and outperforms the AMP baseline. Shown in Figure 6, we found that masking the scoreboard in each Atari environment when training the video model improved downstream RL performance.
Figure 6: RL training curves on Atari Pong when using VIPER trained with or without masking the scoreboard.
Since the video model learns to predict all aspects of the data distribution, including the scoreboard, it tends to provide noisier reward signal during policy training when the agent encounters out-of-distributions scores not seen by the video model. For example, expert demos for Pong only contain videos where the player scores and the opponent's score is always zero. During policy training, we observe that the Pong agent tends to exhibit more erratic behaviors as soon as the opponent scores at least one point, whereas when masking the scoreboard, learned policies are generally more stable. These results suggest the potential benefits of finetuning large video models on expert demos to learn more generalizable priors, as opposed to training from scratch.
For RLBench, VIPER outperforms the Task Oracle because RLBench tasks provide very sparse rewards after long sequences of actions, which pose a challenging objective for RL agents. VIPER instead provides a dense reward extracted from the expert videos, which helps learn these challenging tasks. When training the video model, we found it beneficial to train at a reduced frame rate, accomplished by subsampling video sequences by a factor of 4. Otherwise, we observed the video model would assign high likelihoods to stationary trajectories, resulting in learned policies rarely moving and interacting with the scene. We hypothesize that this may be partially due to the high control frequency of the environment, along with the initial slow acceleration of the robot arm in demonstrations, resulting in very little movement between adjacent frames. When calculating likelihoods for reward computation, we similarly input observations strided by time, e.g.,  $p(x_{t} \mid x_{t-4}, x_{t-8}, \ldots)$ .