Skip to content

Instantly share code, notes, and snippets.

Avatar

Danijar Hafner danijar

View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

  • I am danijar on github.
  • I am danijar (https://keybase.io/danijar) on keybase.
  • I have a public key whose fingerprint is 2907 D9B0 3FDD 28B2 6FBB 41B7 E4EB 8875 D9AB FF13

To claim this, I am signing this object:

View tf_gaussian_blur.py
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
def gaussian_blur(image, diameter):
padding = [[0, 0]] + [[(diameter) // 2, (diameter - 1) // 2]] * 2 + [[0, 0]]
diameter = tf.to_float(diameter)
filter_ = tf.range(-(diameter - 1) // 2, (diameter - 1) // 2 + 1)
filter_ = tf.exp(-0.5 * filter_ ** 2 / (diameter / 4) ** 2) # 2 stds.
@danijar
danijar / agents_on_dm_control.py
Last active Aug 21, 2020
Training TensorFlow Agents PPO on dm_control environments.
View agents_on_dm_control.py
import argparse
import os
import agents
import gym
import gym.spaces
import numpy as np
import tensorflow as tf
from dm_control import suite # Must be imported after TensorFlow.
@danijar
danijar / blog_tensorflow_variational_auto_encoder.py
Last active Dec 22, 2020
TensorFlow Variational Auto-Encoder
View blog_tensorflow_variational_auto_encoder.py
# Full example for my blog post at:
# https://danijar.com/building-variational-auto-encoders-in-tensorflow/
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
tfd = tf.contrib.distributions
@danijar
danijar / share_variables_decorator.py
Last active Dec 27, 2018
TensorFlow decorator to share variables between calls. Works for both functions and methods.
View share_variables_decorator.py
import functools
import tensorflow as tf
class share_variables(object):
def __init__(self, callable_):
self._callable = callable_
self._wrappers = {}
@danijar
danijar / tensorflow-agents-ppo-minitaur.py
Created Oct 23, 2017
Configuration for TensorFlow Agents PPO on MinitaurBulletEnv-v0
View tensorflow-agents-ppo-minitaur.py
def minitaur_config():
# General
algorithm = ppo.PPOAlgorithm
num_agents = 10
eval_episodes = 30
use_gpu = False
# Environment
env = 'MinitaurBulletEnv-v0'
max_length = 1000
steps = 1e7 # 10M
@danijar
danijar / bench_gym_speed.sh
Created Sep 13, 2017
One-liner to benchmark speed of Gym environments
View bench_gym_speed.sh
python -c "import gym,time;d=10000;e=gym.make('Ant-v1');s=time.time();e.reset();[e.reset() if e.step(e.action_space.sample())[2] else 0 for _ in range(d)];print(d/(time.time()-s),'FPS')"
@danijar
danijar / agents_pybullet_example.md
Last active Jan 10, 2021
Example for using TensorFlow Agents with custom environments
View agents_pybullet_example.md

TensorFlow Agents PyBullet Usage Example

This example shows how to install TensorFlow agents and use it on custom environments, such as the environments that come with PyBullet.

It works for both Python 3 and Python 2. Just replace pip3 and python3 with pip2 and python2.

Set up the dependencies:

@danijar
danijar / gru.py
Last active Apr 11, 2021
Gated Recurrent Unit with Layer norm and Xavier initializer
View gru.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
class GRU(tf.contrib.rnn.RNNCell):
@danijar
danijar / edit_dirs
Last active Apr 29, 2018
Text based tool move and delete directories
View edit_dirs
#!/usr/bin/python3
"""Move or delete directories via your text editor.
Installation:
- Install sh.py via `pip3 install sh`.
- Save this file into a directory in your $PATH, for example `~/bin`.
"""
import argparse
import tempfile