In [4]:
import numpy as np
import tensorflow as tf
In [12]:
x = tf.constant([[2, 2]])
y = tf.constant([[1], [3]])
with tf.Session() as sess:
result = sess.run(tf.matmul(x, y))
print(result)
you can evaluate the result with foo.eval()
.
In [15]:
x = tf.Variable([1, 2])
a = tf.constant([3, 3])
with tf.Session() as sess:
x.initializer.run()
sub = tf.sub(x, a)
result = sub.eval()
print(result)
In [22]:
import math
In [25]:
state = tf.Variable(0, name='Ge')
one = tf.constant(1)
new_state = tf.add(state, one)
update = tf.assign(state, new_state)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(state))
for _ in range(3):
sess.run(update)
print(sess.run(state))
Now implement something similar.
In [31]:
some_state = tf.Variable(15, name="awesome")
two = tf.constant(2)
update = tf.assign(some_state, tf.mul(two, some_state))
with tf.Session() as sess:
some_state.initializer.run()
for _ in range(5):
sess.run(update)
print(update.eval())
# eval executes the update twice. Same with `run`.
print(update.eval())
In [37]:
input1 = tf.constant([3.0])
input2 = tf.constant([2.0])
input3 = tf.constant([5.0])
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)
with tf.Session() as sess:
result = sess.run([mul, intermed])
print(result)
In [45]:
import matplotlib.pyplot as plt
%matplotlib inline
In [66]:
initial = tf.truncated_normal([1000], stddev=0.1)
with tf.Session() as sess:
result = initial.eval()
# print(result)
plt.plot(result)
plt.title('Showing the raw initialized data')
plt.show()
plt.hist(result, np.linspace(-0.2, 0.2, 11),
ec='none', rwidth=0.9, color='#47c0fc')
plt.xlim(-0.3, 0.3)
plt.ylim(0, 200)
In [58]:
Out[58]:
In [ ]: