In [1]:
import tensorflow as tf

In [2]:
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)


Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)

In [3]:
sess = tf.Session()
print(sess.run([node1, node2]))


[3.0, 4.0]

In [4]:
node3 = tf.add(node1, node2)
print("node3:", node3)
print("sess.run(node3):", sess.run(node3))


node3: Tensor("Add:0", shape=(), dtype=float32)
sess.run(node3): 7.0

In [5]:
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b  # + provides a shortcut for tf.add(a, b)
print(sess.run(adder_node, {a: 3, b: 4.5}))
print(sess.run(adder_node, {a: [1, 3], b: [2, 4]}))


7.5
[ 3.  7.]

In [7]:
add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a: 3, b: 4.5}))
print(sess.run(add_and_triple, {a: [1, 3], b: [2, 4]}))


22.5
[  9.  21.]

In [11]:
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b

init = tf.global_variables_initializer()
sess.run(init)

print(sess.run(linear_model, {x: [1, 2, 3, 4]}))


[ 0.          0.30000001  0.60000002  0.90000004]

In [12]:
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
print(sess.run(squared_deltas, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

loss = tf.reduce_sum(squared_deltas)
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))


[  0.           1.68999982   6.75999928  15.21000099]
23.66

In [16]:
fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))


0.0

In [17]:
type(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))


Out[17]:
numpy.float32

In [ ]: