Skip to content

Instantly share code, notes, and snippets.

import tensorflow as tf
from tensorflow.python.ops import control_flow_ops
from tensorflow.contrib import graph_editor as ge
def make_conditional_initializer(v):
"""Makes initializer of variable var lazy, returns new conditional init
op."""
cond = tf.is_variable_initialized(v)
@yaroslavvb
yaroslavvb / github-recipes.txt
Last active April 6, 2017 13:44
TensorFlow github recipes
1. Making Pull Request
First, click "Fork" on github tensorflow page
Then in Terminal:
export user=yaroslavvb
export branch_name
git clone https://github.com/$user/tensorflow.git
cd tensorflow
@yaroslavvb
yaroslavvb / Example of adding a vector to first row of matrix in TensorFlow
Last active August 3, 2017 08:18
Example of dynamic stitch to add vector to first row of matrix
a = tf.constant([[1,2],[3,4]])
row_to_add = tf.constant([1, 1])
original_row = a[0]
updated_row = original_row + row_to_add
unchanged_indices = tf.range(tf.size(a))
changed_indices = tf.range(a.get_shape()[0])
a_flat = tf.reshape(a, [-1])
updated_a_flat = tf.dynamic_stitch([unchanged_indices, changed_indices], [a_flat, updated_row])
updated_a = tf.reshape(updated_a_flat, a.get_shape())
print sess.run(updated_a)
@yaroslavvb
yaroslavvb / resnet_test.py
Created October 28, 2017 22:48
Resnet minimize in Eager mode
import tensorflow as tf
from tensorflow.contrib.eager.python import tfe
tfe.enable_eager_execution()
context = tf.device('/gpu:0')
context.__enter__()
# download resnet_model
import sys, os, urllib.request
resnet_model_url="https://raw.githubusercontent.com/tensorflow/models/master/official/resnet/resnet_model.py"
response = urllib.request.urlopen(resnet_model_url)
import torch
import numpy as np
import time
def benchmark():
SIZE_GB = 1
num_iters = 100
def compute():
a = torch.ones(int(SIZE_GB*1000*250000)).cuda()
@yaroslavvb
yaroslavvb / pytorch_lbfgs.py
Created December 3, 2017 22:52
PyTorch tied autoencoder with l-BFGS
import util as u
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
# todo: make images global
@yaroslavvb
yaroslavvb / session-run-benchmark.py
Last active February 6, 2018 09:22
Example of benchmarking session.run call
# Example of profiling session.run overhead
# for python profiling
# python -m cProfile -o session-run-benchmark-feed.prof session-run-benchmark.py feed_dict
# python -m cProfile -o session-run-benchmark-variable.prof session-run-benchmark.py variable
# pip install snakeviz
# snakeviz session-run-benchmark-feed.prof
# snakeviz session-run-benchmark.prof
#
#
# Feed_dict: 147 usec, no feed dict, 71 usec
@yaroslavvb
yaroslavvb / show_graph
Created December 30, 2016 01:50
Visualizing graph in Jupyter notebook
# make things wide
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
from IPython.display import clear_output, Image, display, HTML
def strip_consts(graph_def, max_const_size=32):
"""Strip large constant values from graph_def."""
strip_def = tf.GraphDef()
for n0 in graph_def.node:
@yaroslavvb
yaroslavvb / count_ops.py
Created December 11, 2016 01:44
count number of ops in TensorFlow low-level API
from google.protobuf import text_format
from tensorflow.core.framework import op_def_pb2
ops = op_def_pb2.OpList()
ops_text = open("/local_home/yaroslav/tensorflow.git/tensorflow/tensorflow/core/ops/ops.pbtxt").read()
text_format.Merge(ops_text, ops)
print(len(ops.op))
@yaroslavvb
yaroslavvb / client_transfer_benchmark.py
Last active February 6, 2018 09:25
benchmark TensorFlow<->Python transfer rate
# Benchmark transferring data from TF into Python runtime
#
## Dependencies:
# portpicker (pip install portpicker)
# tcmalloc4 (sudo apt-get install google-perftools)
# TF 0.12 (for var.read_value(), ones_initializer())
#
# On Linux default malloc is slow
# sudo apt-get install google-perftools
# export LD_PRELOAD="/usr/lib/libtcmalloc.so.4"