Skip to content

Instantly share code, notes, and snippets.

batch_size=512
x2_dist = tfd.Normal(loc=0., scale=4.)
x2_samples = x2_dist.sample(batch_size)
x1 = tfd.Normal(loc=.25 * tf.square(x2_samples),
scale=tf.ones(batch_size, dtype=tf.float32))
x1_samples = x1.sample()
x_samples = tf.stack([x1_samples, x2_samples], axis=1)
@ericjang
ericjang / blender_pointcloud_text_2d.py
Last active December 26, 2017 19:12
Generate 2D points randomly distributed on a flat text mesh in Blender.
# run this from a blender interpreter
# First, create text object, then <SPACE> convert it from Text to a Mesh object. Select it.
import bpy_extras.mesh_utils
import pickle
obj = bpy.context.object # get actively selected object
me = obj.data # get mesh
me.calc_tessface() # recalc tessfaces
points = bpy_extras.mesh_utils.face_random_points(10, me.tessfaces)
loss=tf.reduce_mean(-elbo)
lr=tf.constant(0.001)
train_op=tf.train.AdamOptimizer(learning_rate=lr).minimize(loss,var_list=slim.get_model_variables())
init_op=tf.initialize_all_variables()
# get data
data = input_data.read_data_sets('/tmp/', one_hot=True).train
BATCH_SIZE=100
NUM_ITERS=50000
tau0=1.0 # initial temperature
# loss and train ops
kl_tmp = tf.reshape(q_y*(log_q_y-tf.log(1.0/K)),[-1,N,K])
KL = tf.reduce_sum(kl_tmp,[1,2])
elbo=tf.reduce_sum(p_x.log_prob(x),1) - KL
# temperature
tau = tf.Variable(5.0,name="temperature")
# sample and reshape back (shape=(batch_size,N,K))
# set hard=True for ST Gumbel-Softmax
y = tf.reshape(gumbel_softmax(logits_y,tau,hard=False),[-1,N,K])
# generative model p(x|y), i.e. the decoder (shape=(batch_size,200))
net = slim.stack(slim.flatten(y),slim.fully_connected,[256,512])
logits_x = slim.fully_connected(net,784,activation_fn=None)
# (shape=(batch_size,784))
p_x = Bernoulli(logits=logits_x)
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
slim=tf.contrib.slim
Bernoulli = tf.contrib.distributions.Bernoulli
K=10 # number of classes
N=30 # number of categorical distributions
def sample_gumbel(shape, eps=1e-20):
"""Sample from Gumbel(0, 1)"""
U = tf.random_uniform(shape,minval=0,maxval=1)
return -tf.log(-tf.log(U + eps) + eps)
def gumbel_softmax_sample(logits, temperature):
""" Draw a sample from the Gumbel-Softmax distribution"""
y = logits + sample_gumbel(tf.shape(logits))
return tf.nn.softmax( y / temperature)
@ericjang
ericjang / fix.sh
Created September 11, 2016 15:39
TensorFlow + GPU + Mac OSX fix
# need to rename libcuda, otherwise get 'segmentation fault: 11' when trying to load GPU-enabled TensorFlow.
# 9/11/16
cd /usr/local/cuda
sudo ln -s libcuda.dylib libcuda.1.dylib
cd /usr/local/cuda
sudo ln -s libcuda.dylib libcuda.1.dylib
@ericjang
ericjang / mj140_instructions.sh
Last active September 2, 2016 17:16
running headless Mujoco on OSX 10.11
# step 1: install CMake command line on Mac OSX
# download https://cmake.org/files/v3.6/cmake-3.6.1.tar.gz
cd <cmake-root-dir>
./bootstrap
make
make install
# step 2: download GLFW source from http://www.glfw.org/download.html
cd <glfw-root-dir>
cmake .
cmake -D BUILD_SHARED_LIBS=ON . # mujoco attempts to load libglfw.3.dylib so we build a shared lib rather than static .a