In [1]:
import tensorflow as tf
In [2]:
with tf.Graph().as_default():
fibonacci = tf.constant([1, 1, 2, 3, 5, 8], dtype=tf.int32)
ones = tf.ones([6], dtype=tf.int32)
beyond_fibonacci = tf.add(fibonacci, ones)
with tf.Session() as sess:
print beyond_fibonacci.eval()
In [3]:
with tf.Graph().as_default():
scalar = tf.zeros([])
vector = tf.zeros([3])
matrix = tf.zeros([2,3])
with tf.Session() as sess:
print 'scalar has shape', scalar.get_shape(), 'and value: \n', scalar.eval()
print 'vector has shape', vector.get_shape(), 'and value: \n', vector.eval()
print 'matrix has shape', matrix.get_shape(), 'and value: \n', matrix.eval()
In [4]:
with tf.Graph().as_default():
fibonacci = tf.constant([1, 1, 2, 3, 5, 8], dtype=tf.int32)
ones = tf.ones(1, dtype=tf.int32)
beyond_fibonacci = tf.add(fibonacci, ones)
with tf.Session() as sess:
print beyond_fibonacci.eval()
In [5]:
with tf.Graph().as_default():
x = tf.constant([[5, 2, 4, 3], [5, 1, 6, -2], [-1, 3, -1, -2]], dtype=tf.int32)
y = tf.constant([[2, 2], [3, 5], [4, 5], [1, 6]], dtype=tf.int32)
xy = tf.matmul(x, y)
with tf.Session() as sess:
print xy.eval()
In [6]:
with tf.Graph().as_default():
a = tf.constant([5, 2, 4, 3], dtype=tf.int32)
b = tf.reshape(a, [2, 2])
c = tf.reshape(b, [4, 1])
with tf.Session() as sess:
print 'a', a.eval()
print 'b', b.eval()
print 'c', c.eval()
In [7]:
with tf.Graph().as_default():
a = tf.constant([5, 3, 2, 7, 1, 4])
b = tf.constant([4, 6, 3])
c = tf.reshape(a, [2, 3])
d = tf.reshape(b, [3, 1])
cd = tf.matmul(c, d)
with tf.Session() as sess:
print 'a shape', a.get_shape(), 'value \n', a.eval(), '\n'
print 'b shape', b.get_shape(), 'value \n', b.eval(), '\n'
print 'c shape', c.get_shape(), 'value \n', c.eval(), '\n'
print 'd shape', d.get_shape(), 'value \n', d.eval(), '\n'
print 'cd shape', cd.get_shape(), 'value \n', cd.eval(), '\n'
In [8]:
g = tf.Graph()
In [9]:
with g.as_default():
v = tf.Variable([3])
w = tf.Variable(tf.random_normal([1], mean=1.0, stddev=0.35))
In [10]:
with g.as_default():
with tf.Session() as sess:
try:
v.eval()
except tf.errors.FailedPreconditionError as e:
print "Caught expected error", e
In [11]:
with g.as_default():
with tf.Session() as sess:
initialization = tf.initialize_all_variables()
sess.run(initialization)
print v.eval()
print w.eval()
In [12]:
with g.as_default():
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print w.eval()
print w.eval()
print w.eval()
In [13]:
with g.as_default():
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print v.eval()
assignment = tf.assign(v,[7])
print v.eval()
sess.run(assignment)
print v.eval()
In [ ]: