In [1]:
import tensorflow as tf
x = tf.constant(3)
print(x)
In [2]:
import tensorflow as tf
x = tf.constant([3, 5, 7])
print(x)
In [3]:
import tensorflow as tf
x = tf.constant([[3, 5, 7],
[4, 6, 8]])
print(x)
In [4]:
import tensorflow as tf
x = tf.constant([[[3, 5, 7],[4, 6, 8]],
[[1, 2, 3],[4, 5, 6]]
])
print(x)
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)
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())
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())
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())
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())
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))
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)
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
In [15]:
from google.datalab.ml import TensorBoard
TensorBoard().start('./summaries')
Out[15]:
In [16]:
from google.datalab.ml import TensorBoard
TensorBoard().stop(13045)
print('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() ))