import tensorflow as tf
sess = tf.InteractiveSession()
def namestr(obj, namespace):
return [name for name in namespace if namespace[name] is obj][0]
m1 = tf.constant(value = [[1., 2.]])
m2 = tf.constant(value = [[3.],[4.]])
m3 = tf.constant(value = [[[1., 2., 3.], [4., 5., 6.]]])
m4 = tf.constant(value = [[[1., 2., 3.], [4., 5., 6.]], [[7., 8., 9.] ,[10., 11., 12.]]])
m5 = tf.constant(value = [[[1., 2.], [3., 4.]]])
m6 = tf.constant(value = [[3., 4., 5.]])
def printFixture(isShapeOut, tensorMatrixList):
print "======Fixture======="
for m in tensorMatrixList:
print "Tensor Matrix - " + namestr(m, globals())
if (isShapeOut):
print "Shape:", m.get_shape()
print m.eval()
print
print "===================="
printFixture(True, (m1, m2, m3, m4, m5, m6))
======Fixture======= Tensor Matrix - m1 Shape: (1, 2) [[ 1. 2.]] Tensor Matrix - m2 Shape: (2, 1) [[ 3.] [ 4.]] Tensor Matrix - m3 Shape: (1, 2, 3) [[[ 1. 2. 3.] [ 4. 5. 6.]]] Tensor Matrix - m4 Shape: (2, 2, 3) [[[ 1. 2. 3.] [ 4. 5. 6.]] [[ 7. 8. 9.] [ 10. 11. 12.]]] Tensor Matrix - m5 Shape: (1, 2, 2) [[[ 1. 2.] [ 3. 4.]]] Tensor Matrix - m6 Shape: (1, 3) [[ 3. 4. 5.]] ====================
printFixture(True, (m1, m2))
r1 = tf.add(m1, m2)
print r1.eval()
r2 = m1 + m2
print r2.eval()
======Fixture======= Tensor Matrix - m1 Shape: (1, 2) [[ 1. 2.]] Tensor Matrix - m2 Shape: (2, 1) [[ 3.] [ 4.]] ==================== [[ 4. 5.] [ 5. 6.]] [[ 4. 5.] [ 5. 6.]]
printFixture(True, (m1, m2))
r1 = tf.multiply(m1, m2)
print r1.eval()
r2 = tf.multiply(m1, m2)
print r2.eval()
r3 = m1 * m2
print r3.eval()
======Fixture======= Tensor Matrix - m1 Shape: (1, 2) [[ 1. 2.]] Tensor Matrix - m2 Shape: (2, 1) [[ 3.] [ 4.]] ==================== [[ 3. 6.] [ 4. 8.]] [[ 3. 6.] [ 4. 8.]] [[ 3. 6.] [ 4. 8.]]
printFixture(True, (m1, m2))
r1 = tf.matmul(a = m1, b = m2) #(1, 2) x (2, 1)
print r1.eval()
r2 = tf.matmul(a = m2, b = m1) #(2, 1) x (1, 2)
print r2.eval()
======Fixture======= Tensor Matrix - m1 Shape: (1, 2) [[ 1. 2.]] Tensor Matrix - m2 Shape: (2, 1) [[ 3.] [ 4.]] ==================== [[ 11.]] [[ 3. 6.] [ 4. 8.]]