Diagrams for Course #3


In [1]:
import tensorflow as tf
x = tf.constant(3)
print(x)


/usr/local/envs/py3env/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Tensor("Const:0", shape=(), dtype=int32)

In [2]:
import tensorflow as tf
x = tf.constant([3, 5, 7])
print(x)


Tensor("Const_1:0", shape=(3,), dtype=int32)

In [3]:
import tensorflow as tf
x = tf.constant([[3, 5, 7],
                 [4, 6, 8]])
print(x)


Tensor("Const_2:0", shape=(2, 3), dtype=int32)

In [4]:
import tensorflow as tf
x = tf.constant([[[3, 5, 7],[4, 6, 8]],
                 [[1, 2, 3],[4, 5, 6]]
                 ])
print(x)


Tensor("Const_3:0", shape=(2, 2, 3), dtype=int32)

In [5]:
import tensorflow as tf
x1 = tf.constant([2, 3, 4])
x2 = tf.stack([x1, x1])
x3 = tf.stack([x2, x2, x2, x2])
x4 = tf.stack([x3, x3])
print(x1)
print(x2)
print(x3)
print(x4)


Tensor("Const_4:0", shape=(3,), dtype=int32)
Tensor("stack:0", shape=(2, 3), dtype=int32)
Tensor("stack_1:0", shape=(4, 2, 3), dtype=int32)
Tensor("stack_2:0", shape=(2, 4, 2, 3), dtype=int32)

In [6]:
import tensorflow as tf
x = tf.constant([[3, 5, 7],
                 [4, 6, 8]])
y = x[:, 1]
with tf.Session() as sess:
  print(y.eval())


[5 6]

In [7]:
import tensorflow as tf
x = tf.constant([[3, 5, 7],
                 [4, 6, 8]])
y = tf.reshape(x, [3, 2])
with tf.Session() as sess:
  print(y.eval())


[[3 5]
 [7 4]
 [6 8]]

In [8]:
import tensorflow as tf
x = tf.constant([[3, 5, 7],
                 [4, 6, 8]])
y = tf.reshape(x, [3, 2])[1, :]
with tf.Session() as sess:
  print(y.eval())


[7 4]

In [ ]:
import tensorflow as tf
from tensorflow.contrib.eager.python import tfe

tfe.enable_eager_execution()

x = tf.constant([[3, 5, 7],
                 [4, 6, 8]])
y = tf.reshape(x, [3, 2])[1, :]
print(y)

In [ ]:
import tensorflow as tf
from tensorflow.contrib.eager.python import tfe

tfe.enable_eager_execution()

x = tf.constant([3, 5, 7])
y = tf.constant([1, 2, 3])
print(x-y)

In [10]:
import tensorflow as tf

x = tf.constant([3, 5, 7])
y = tf.constant([1, 2, 3])
z = tf.add(x, y)

with tf.Session() as sess:
  print(z.eval())


[ 4  7 10]

In [11]:
import tensorflow as tf

x = tf.constant([3, 5, 7])
y = tf.constant([1, 2, 3])
z = tf.add(x, y)

with tf.Session() as sess:
  print(sess.run(z))


[ 4  7 10]

In [12]:
import tensorflow as tf

x = tf.constant([3, 5, 7])
y = tf.constant([1, 2, 3])
z1 = x + y
z2 = x * y
z3 = z2 - z1

with tf.Session() as sess:
  a1, a3 = sess.run([z1, z3])
  print(a1)
  print(a3)


[ 4  7 10]
[-1  3 11]

In [13]:
import tensorflow as tf

x = tf.constant([3, 5, 7], name="x")
y = tf.constant([1, 2, 3], name="y")
z1 = tf.add(x, y, name="z1")
z2 = x * y
z3 = z2 - z1

with tf.Session() as sess:
  with tf.summary.FileWriter('summaries', sess.graph) as writer:
      a1, a3 = sess.run([z1, z3])

In [14]:
!ls summaries


events.out.tfevents.1536178690.b7bd1d29df22
events.out.tfevents.1536178873.b7bd1d29df22

In [15]:
from google.datalab.ml import TensorBoard
TensorBoard().start('./summaries')


TensorBoard was started successfully with pid 18325. Click here to access it.

Out[15]:
18325

In [16]:
from google.datalab.ml import TensorBoard
TensorBoard().stop(13045)
print('stopped TensorBoard')


stopped TensorBoard

In [17]:
import tensorflow as tf

def forward_pass(w, x):
    return tf.matmul(w, x)

def train_loop(x, niter=5):
  with tf.variable_scope("model", reuse=tf.AUTO_REUSE):
    w = tf.get_variable("weights", 
                    shape=(1,2),  # 1 x 2 matrix
                    initializer=tf.truncated_normal_initializer(),
                    trainable=True)
  preds = []
  for k in range(niter):
    preds.append(forward_pass(w, x))
    w = w + 0.1 # "gradient update"
  return preds

with tf.Session() as sess:
  preds = train_loop(tf.constant([[3.2, 5.1, 7.2],[4.3, 6.2, 8.3]])) # 2 x 3 matrix
  tf.global_variables_initializer().run()
  for i in range(len(preds)):
      print("{}:{}".format( i, preds[i].eval() ))


0:[[ 8.568541 12.702375 17.271353]]
1:[[ 9.318541  13.8323765 18.821354 ]]
2:[[10.068541 14.962376 20.371353]]
3:[[10.818541 16.092377 21.921354]]
4:[[11.56854  17.222376 23.471352]]