Skip to content

Instantly share code, notes, and snippets.

# encoder
from tensorflow.models.rnn.rnn_cell import LSTMCell
read_size = 2*read_n*read_n if FLAGS.read_attn else 2*img_size
lstm_enc = LSTMCell(enc_size, read_size+dec_size) # encoder Op
def encode(state,input):
with tf.variable_scope("encoder",reuse=DO_SHARE):
return lstm_enc(input,state)
def read_attn(x,x_hat,h_dec_prev):
Fx,Fy,gamma=attn_window("read",h_dec_prev,read_n)
def filter_img(img,Fx,Fy,gamma,N):
Fxt=tf.transpose(Fx,perm=[0,2,1])
img=tf.reshape(img,[-1,B,A])
glimpse=tf.batch_matmul(Fy,tf.batch_matmul(img,Fxt))
glimpse=tf.reshape(glimpse,[-1,N*N])
return glimpse*tf.reshape(gamma,[-1,1])
x=filter_img(x,Fx,Fy,gamma,read_n) # batch x (read_n*read_n)
x_hat=filter_img(x_hat,Fx,Fy,gamma,read_n)
def filterbank(gx, gy, sigma2,delta, N):
grid_i = tf.reshape(tf.cast(tf.range(N), tf.float32), [1, -1])
mu_x = gx + (grid_i - N / 2 - 0.5) * delta # eq 19
mu_y = gy + (grid_i - N / 2 - 0.5) * delta # eq 20
a = tf.reshape(tf.cast(tf.range(A), tf.float32), [1, 1, -1])
b = tf.reshape(tf.cast(tf.range(B), tf.float32), [1, 1, -1])
mu_x = tf.reshape(mu_x, [-1, N, 1])
mu_y = tf.reshape(mu_y, [-1, N, 1])
sigma2 = tf.reshape(sigma2, [-1, 1, 1])
Fx = tf.exp(-tf.square((a - mu_x) / (2*sigma2))) # 2*sigma2?
def linear(x,output_dim):
"""
affine transformation Wx+b
assumes x.shape = (batch_size, num_features)
"""
w=tf.get_variable("w", [x.get_shape()[1], output_dim])
b=tf.get_variable("b", [output_dim], initializer=tf.constant_initializer(0.0))
return tf.matmul(x,w)+b
def attn_window(scope,h_dec,N):
def read_no_attn(x,x_hat,h_dec_prev):
return tf.concat(1,[x,x_hat])
@ericjang
ericjang / tf_mm_vectorized.py
Last active February 24, 2016 16:19
vectorized matrix multiplication in TF
# applying read filter. inspired by https://github.com/ikostrikov/TensorFlow-VAE-GAN-DRAW/blob/master/main-draw.py
Fxt=tf.transpose(Fx, [0,2,1]) # batch x N x A
Fxt=tf.reshape(Fxt, [-1,1,A,N,1]) # batch x 1 x A x N x 1
Fxt=tf.tile(Fxt, [1,N,1,1,1]) # batch x N x A x N x 1 (repmat'ed along dim=1)
Fy=tf.reshape(Fy, [-1,N,B,1,1]) # batch x N x B x 1 x 1
x=tf.reshape(x,[-1,1,B,A,1]) # batch x 1 x B x A x 1
x=tf.tile(x,[1,N,1,1,1]) # batch x N x B x A x 1
Fydotx=tf.reduce_sum(Fy*x,2) # batch x N x A x 1
Fydotx=tf.reshape(x,[-1,N,A,1,1]) # batch x N x A x 1 x 1
FydotxdotFxt=tf.reduce_sum(Fydotx*Fxt,2) # batch x N x N x 1
@ericjang
ericjang / draw_0.py
Last active February 24, 2016 05:19
DRAW high-level implementation
cs,mus,logsigmas,sigmas=[0]*T,[0]*T,[0]*T,[0]*T # parameters we'll need to access later
# initial states
DO_SHARE=False
h_dec_prev=tf.zeros((batch_size,dec_size))
enc_state=lstm_enc.zero_state(batch_size, tf.float32)
dec_state=lstm_dec.zero_state(batch_size, tf.float32)
# build the graph
for t in range(T):
c_prev = tf.zeros((batch_size,img_size)) if t==0 else cs[t-1]
x_hat=x-tf.sigmoid(c_prev) # error image
@ericjang
ericjang / gist:33ee2ef334fd833cab85
Last active December 30, 2015 09:07
genadv_algo1
for i in range(TRAIN_ITERS):
x= np.random.normal(mu,sigma,M) # sample minibatch from p_data
z= np.linspace(-5.0,5.0,M)+np.random.random(M)*0.01 # sample minibatch from noise prior
sess.run(opt_d, {x_node: x, z_node: z}) # update discriminator D
z= np.linspace(-5.0,5.0,M)+np.random.random(M)*0.01 # sample noise prior
sess.run(opt_g, {z_node: z}) # update generator G
@ericjang
ericjang / TensorFlow_Windows.md
Last active March 27, 2021 22:19
Setting up TensorFlow on Windows using Docker.

TensorFlow development environment on Windows using Docker

Here are instructions to set up TensorFlow dev environment on Docker if you are running Windows, and configure it so that you can access Jupyter Notebook from within the VM + edit files in your text editor of choice on your Windows machine.

Installation

First, install https://www.docker.com/docker-toolbox

Since this is Windows, creating the Docker group "docker" is not necessary.

@ericjang
ericjang / voleuse.py
Last active November 7, 2015 16:15
Based Prison School Cover Image Download
import requests
from bs4 import BeautifulSoup
import re
for v in range(1,18): # update chapter numbers as needed
fname = 'Volume_%02d.jpg' % v
url = 'http://prison-school.wikia.com/wiki/Category:Volume_Cover_Images?file=%s' % fname
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
pattern = r'http:\/\/vignette\d.wikia.nocookie.net\/prison-school\/images\/\w*\/\w*\/' + fname + '\/revision\/latest\/'
r = re.compile(pattern)