In [1]:
import tensorflow as tf
In [2]:
print(tf.__version__)
In [3]:
hello = tf.constant('Hello')
In [4]:
type(hello)
Out[4]:
In [5]:
world = tf.constant('World')
In [6]:
result = hello + world
In [7]:
result
Out[7]:
In [8]:
type(result)
Out[8]:
In [9]:
with tf.Session() as sess:
result = sess.run(hello + world)
In [10]:
result
Out[10]:
Computations
In [11]:
tensor_1 = tf.constant(1)
tensor_2 = tf.constant(2)
In [12]:
type(tensor_1)
Out[12]:
In [13]:
tensor_1 + tensor_2
Out[13]:
In [14]:
sess = tf.Session()
In [15]:
sess.run(tensor_1 + tensor_2)
Out[15]:
In [16]:
sess.close()
In [17]:
# Constant
const = tf.constant(10)
In [18]:
# Create a tensor of a specific dimension
# with a given value
fill_mat = tf.fill(dims = (4, 4), value = 10)
In [19]:
# Generate a tensor of zeros
myzeros = tf.zeros(shape = (4, 4))
In [20]:
# Generate a tensor of ones
myones = tf.ones(shape = (4, 4))
In [21]:
# Generate a tensor of random numbers
# Sampled from the normal distribution
# With given mean and standard deviation
myrandn = tf.random_normal(shape = (4, 4),
mean = 0,
stddev = 0.5)
In [22]:
# Generate a tensor of random numbers
# Sampled from the uniform distribution
# With given min and max values
myrandu = tf.random_uniform(shape = (4, 4),
minval = 0,
maxval = 1)
In [23]:
my_ops = [const, fill_mat, myzeros, myones, myrandn, myrandu]
In [24]:
# Using an Interactive Session
sess = tf.InteractiveSession()
In [25]:
# For interactive session
# Some functions allow you to run .eval() on them
for op in my_ops:
print(op.eval())
print('\n')
In [26]:
a = tf.constant([ [1, 2],
[3, 4] ])
In [27]:
# Shape: 2 x 2
a.get_shape()
Out[27]:
In [28]:
# Shape: 2 x 1
b = tf.constant([[10], [100]])
In [29]:
b.get_shape()
Out[29]:
In [30]:
result = tf.matmul(a,b)
In [31]:
result.eval()
Out[31]: