Skip to content

Instantly share code, notes, and snippets.

View yoel-zeldes's full-sized avatar
💭
Working on my blog at anotherdatum.com

Yoel Zeldes yoel-zeldes

💭
Working on my blog at anotherdatum.com
View GitHub Profile
def plot_samples(samples, num_epochs):
IMAGE_WIDTH = 0.7
epochs = np.linspace(0, len(samples) - 1, num_epochs).astype(int)
plt.figure(figsize=(IMAGE_WIDTH * NUM_DIGITS,
len(epochs) * IMAGE_WIDTH))
for epoch_index, epoch in enumerate(epochs):
for digit, image in enumerate(samples[epoch]):
plt.subplot(len(epochs),
NUM_DIGITS,
epoch_index * NUM_DIGITS + digit + 1)
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
np.random.seed(42)
tf.set_random_seed(42)
mnist = input_data.read_data_sets('MNIST_data')
# ...
def _eval_tensor_if_needed(self, path):
"""
Given a path to a tensor file, evaluate the tensor and cache the result in self._tensor_values.
"""
if self._session is None:
return None
if path not in self._tensor_values:
self._tensor_values[path] = self._session.run(self._graph.get_tensor_by_name(path[1:]))
class TfFs(fuse.Operations):
def __init__(self, mount_point, model_path):
self._graph, self._session = _load_model(model_path)
self._files = {}
self._bin_scripts = {}
self._tensor_values = {}
now = time()
self._files['/'] = _create_dir(now)
self._files['/bin'] = _create_dir(now)
self._populate_bin(mount_point)
def _load_model(model_path):
"""
Load a tensorflow model from the given path.
It's assumed the path is either a directory containing a .meta file, or the .meta file itself.
If there's also a file containing the weights with the same name as the .meta file
(without the .meta extension), it'll be loaded as well.
"""
if os.path.isdir(model_path):
meta_filename = [filename for filename in os.listdir(model_path) if filename.endswith('.meta')]
assert len(meta_filename) == 1, 'expecting to get a .meta file or a directory containing a .meta file'
#!/bin/sh
while read local_ref local_sha remote_ref remote_sha
do
if [ "$remote_ref" = "refs/heads/source" ]
then
echo 'pushing output folder (production version) to master...'
pelican content -o output -s publishconf.py
echo anotherdatum.com > output/CNAME
ghp-import output
git push --no-verify git@github.com:yoel-zeldes/yoel-zeldes.github.io.git gh-pages:master
def plot_samples(samples):
IMAGE_WIDTH = 0.7
plt.figure(figsize=(IMAGE_WIDTH * num_digits,
len(samples) * IMAGE_WIDTH))
for epoch, images in enumerate(samples):
for digit, image in enumerate(images):
plt.subplot(len(samples),
num_digits,
epoch * num_digits + digit + 1)
plt.imshow(image.reshape((28, 28)),
plt.subplot(121)
plt.plot(losses_auto_encode)
plt.title('VAE loss')
plt.subplot(122)
plt.plot(losses_digit_classifier)
plt.title('digit classifier loss')
plt.tight_layout()
samples = []
losses_auto_encode = []
losses_digit_classifier = []
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in xrange(params['epochs']):
for _ in xrange(mnist.train.num_examples / params['batch_size']):
batch_images, batch_digits = mnist.train.next_batch(params['batch_size'])
sess.run(train_op, feed_dict={images: batch_images, digits: batch_digits})
# the loss is composed of how well we can reconstruct the image
loss_reconstruction = -tf.reduce_sum(
tf.contrib.distributions.Normal(
decoded_images,
params['decoder_std']
).log_prob(images),
axis=1
)
# and how off the distribution over the latent space is from the prior.