Skip to content

Instantly share code, notes, and snippets.

@arafatkatze
Created September 28, 2016 09:28
Show Gist options
  • Save arafatkatze/dad51642503e64117a53ae6b4dd44fa1 to your computer and use it in GitHub Desktop.
Save arafatkatze/dad51642503e64117a53ae6b4dd44fa1 to your computer and use it in GitHub Desktop.
import tensorflow as tf
def test1():
input1 = tf.placeholder(tf.float32, [2,3])
input2 = tf.placeholder(tf.float32, [2,3])
output = tf.add(input1, input2)
with tf.Session() as sess:
(sess.run([output], feed_dict={input1:[[1.0,3.0, 5.0],[2.0,4.0, 7.0]], input2:[[-5.0,1.2,4.5],[8.0,2.3, 3.1]]}))
def test2():
input1 = tf.placeholder(tf.float32, [3,2,2])
output = tf.batch_matrix_determinant(input1)
with tf.Session() as sess:
(sess.run([output], feed_dict={input1:[[[2.0,5.0],[1.0,-20.0]],[[124.0,5.0],[53.0,-2.0]],[[1.0,0.0],[0.0,1.0]]]}))
import datetime
from time import time
a = time()
for x in range(0, 100):
test1()
print "Test 1 for adding two matrices", time()-a
b = time()
for x in range(0, 100):
test2()
print "Test 2 for batch determinant", time()-b
# Test 1 for adding two matrices 5.47662401199
# Test 2 for batch determinant 12.334102869
require 'tensorflow'
require 'benchmark'
Benchmark.bm do |x|
puts "Test1 for adding two matrices"
x.report {
(0..100).each do
graph = Graph.new()
input1 = graph.placeholder('input1', Tensorflow::TF_DOUBLE, [2,3])
input2 = graph.placeholder('input2', Tensorflow::TF_DOUBLE, [2,3])
graph.op_definer("Add",'output',[input1,input2],"",nil)
encoder = Tensorflow::GraphDef.encode(graph.graph_def)
session = Session.new()
graph = Graph.new()
graph.graph_def = Tensorflow::GraphDef.decode(encoder)
graph.graph_def_raw = encoder
session.extend_graph(graph)
s = session
input1 = Tensor.new([[1.0,3.0, 5.0],[2.0,4.0, 7.0]])
input2 = Tensor.new([[-5.0,1.2,4.5],[8.0,2.3, 3.1]])
input = Hash.new
input["input1"] = input1.tensor
input["input2"] = input2.tensor
result = s.run(input, ["output"], nil)
end
}
puts "Test2 for batch determinant"
x.report {
(0..100).each do
graph = Graph.new()
input1 = graph.placeholder('input1', Tensorflow::TF_DOUBLE, [3,2,2])
graph.op_definer("BatchMatrixDeterminant",'output',[input1],"",nil)
encoder = Tensorflow::GraphDef.encode(graph.graph_def)
session = Session.new()
graph = Graph.new()
graph.graph_def = Tensorflow::GraphDef.decode(encoder)
graph.graph_def_raw = encoder
session.extend_graph(graph)
s = session
input1 = Tensor.new([
[[2.0,5.0],
[1.0,-20.0]],
[[124.0,5.0],
[53.0,-2.0]],
[[1.0,0.0],
[0.0,1.0]]
], :float64)
input = Hash.new
input["input1"] = input1.tensor
result = s.run(input, ["output"], nil)
end
}
end
'''
Resuts
user system total real
Test1 for adding two matrices
3.770000 0.040000 3.810000 ( 3.640148)
Test2 for batch determinant
3.840000 0.040000 3.880000 ( 3.699848)
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment