Skip to content

Instantly share code, notes, and snippets.

@yaroslavvb
yaroslavvb / gist:856aae221ae2792da9cbe25836ab235a
Created June 14, 2016 01:59
reproducing bazel failure with Python3
cd /Users/yaroslavvb/tfimmediate_hood.cpu_three
git clone https://github.com/yaroslavvb/tensorflow.git
export clonedir=/Users/yaroslavvb/tfimmediate_hood.cpu_three/tensorflow
export dev_branch=hood-devel
export pr_branch=hood
git fetch --all
git checkout $dev_branch
git pull
import tensorflow as tf
from tensorflow.core.framework import op_def_pb2
from google.protobuf import text_format
def get_op_types(op):
for attr in op.attr:
if attr.type != 'type':
continue
return list(attr.allowed_values.list.type)
return []
@yaroslavvb
yaroslavvb / gist:eb91fe4b221c6b365f927c56f8827448
Last active September 29, 2016 00:16
Example of adding a vector to first row of matrix
a = tf.constant([[1,2],[3,4]])
row_to_add = tf.constant([[1, 1]])
updated_row = a[0]+row_to_add
updated_a = tf.concat(0, [updated_row, a[1:]])
sess.run(updated_a)
@yaroslavvb
yaroslavvb / gist:710a16fe93f0c91c7e052fcb5b0bccc0
Created October 19, 2016 18:22
iterating over TensorBoard event files
import tensorflow as tf
fn = "/tmp/efs/yaroslav/g/train_1/events.out.tfevents.1476852642.g-w-1-vblgv"
for summary in tf.train.summary_iterator():
print(summary)
import tensorflow as tf
from numpy.testing.utils import nulp_diff
import time
n = 1024
v1 = tf.Variable(tf.ones_initializer((n, n)))
v2 = tf.Variable(tf.ones_initializer((n, n)))
op = v1.assign(tf.matmul(v1, v2)/n).op
norm = tf.reduce_sum(v1)
@yaroslavvb
yaroslavvb / sessrun
Created November 1, 2016 17:59
Example of wrapper for session.run that returns dictionaries instead of lists
def sessrun(fetches):
values = tf.get_default_session().run(fetches)
return {fetches[i]: values[i] for i in range(len(values))}
a = tf.constant(1)
b = tf.constant(2)
c = tf.constant(3)
sess = tf.InteractiveSession()
result1 = sessrun([a, b])
@yaroslavvb
yaroslavvb / scratch.py
Created December 8, 2016 22:51
Example of using stats summarizer
import tensorflow as tf
a = tf.ones((10, 10))
b = tf.ones((10, 10))
c = tf.matmul(a, b)
ss = tf.contrib.stat_summarizer.NewStatSummarizer(tf.get_default_graph().as_graph_def().SerializeToString())
sess = tf.Session()
for i in range(10):
run_metadata = tf.RunMetadata()
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
# GPU picking
# http://stackoverflow.com/a/41638727/419116
# Nvidia-smi GPU memory parsing.
# must set
# CUDA_DEVICE_ORDER=PCI_BUS_ID
# see https://github.com/tensorflow/tensorflow/issues/152#issuecomment-273663277
# Tested on nvidia-smi 370.23
def run_command(cmd):
@yaroslavvb
yaroslavvb / copy_initializer
Created March 7, 2017 20:02
utility to copy initializer for one variable to another
def copy_initializer(from_var, to_var):
from tensorflow.contrib import graph_editor as ge
assert ge.reroute_a2b_ts(from_var.initial_value, to_var.initial_value), "No copy took place"
# Try to copy "a" value to "c" while simultaneously adding vector of 1's to a.
# If the copy is started before the first assign_add, the copied value will be inconsistent.
#
# Running it on macbook my "c" ends up with a mix of values between 1 and 6
#
#
# 16.017478 copy 1 (0) starting
# 17.006894 write 1 (0) starting
# 28.431654 write 1 ending (11.4247 sec)
# 29.436692 write 1 (1) starting