In [39]:
import tensorflow as tf
import matplotlib.pyplot as plt
a = tf.constant([2, 3], name="a-vector")
b = tf.constant([4, 5], name="b-vector")
x = tf.add(a, b, name="add-vector")
with tf.Session() as sess:
writer = tf.summary.FileWriter('./graphs', sess.graph)
print sess.run(x)
writer.close()
# try tensorboard with command:
# $ tensorboard --logdir="graphs"
In [66]:
# basic tensorflow constants, types
zeros = tf.zeros([2, 3], tf.int32)
two_dim_tensor = tf.constant([[2, 1], [3, 5]], name = "2d-tensor")
zeros_like = tf.zeros_like(two_dim_tensor)
ones = tf.ones([3, 3], tf.float32)
ones_like = tf.ones_like(two_dim_tensor)
# sequence data generation
linspace = tf.linspace(10.0, 14.0, 3);
limit = tf.range(2, 10, 3);
with tf.Session() as sess:
print sess.run(zeros)
# generate random data according to some distribution
%matplotlib inline
n = 500000
A = tf.truncated_normal((n,))
B = tf.random_normal((n,))
with tf.Session() as sess:
a, b = sess.run([A, B])
# plot normal distriution and truncated normal distribution in the same graph.
plt.subplot(3,2,1);
plt.hist(a, 100, (-4.2, 4.2));
plt.subplot(3,2,2);
plt.hist(b, 100, (-4.2, 4.2));
# random uniform distribution
rand_t = tf.random_uniform([5], 0, 10, dtype=tf.int32, seed=0)
with tf.Session() as sess:
t = sess.run(rand_t);
plt.subplot(3, 2, 3);
plt.plot(t);
# random shuffle plot
f_sequence = tf.range(1, 10);
f_shuffle = tf.random_shuffle(f_sequence)
with tf.Session() as sess:
f_s = sess.run(f_shuffle);
plt.subplot(3, 2, 4);
plt.plot(f_s);
# random crop
c_crop_before = tf.constant(([[3, 4, 5], [7, 8, 9], [0, 1, 2]]), name="before-crop");
c_crop_after = tf.random_crop(c_crop_before, [2, 2], name="after-crop");
with tf.Session() as sess:
c_crop_plot = sess.run(c_crop_after)
print c_crop_plot
# multinomial
multi_samples = tf.constant([[10., 10.]]);
multi_before = tf.multinomial(tf.log(multi_samples), 5)
with tf.Session() as sess:
print sess.run(multi_samples)
In [73]:
# multinomial
multi_samples = tf.log([[10., 10., 10.]]);
multi_before = tf.multinomial(multi_samples, 5)
with tf.Session() as sess:
print sess.run(multi_samples)
print sess.run(multi_before)
In [2]:
import tensorflow as tf
# use InteractiveSession
sess = tf.InteractiveSession()
ones = tf.ones([2, 4], dtype=tf.int32);
print ones.eval();
fills = tf.fill([2, 4], 8)
print fills.eval();
linspace = tf.linspace(0.0, 10.0, 5);
print linspace.eval();
rang = tf.range(0.0, 10, 5);
print rang.eval();
In [3]:
import tensorflow as tf
vector = tf.constant([2.0, 8], name="vector")
print tf.get_default_graph().as_graph_def();
In [6]:
import tensorflow as tf
cons = tf.constant(8, name="consant")
var = tf.Variable(8, name="Variable")
init = tf.global_variables_initializer()
with tf.Session() as sess:
print sess.run(cons)
sess.run(init)
print sess.run(var)
In [20]:
import tensorflow as tf
var1 = tf.Variable(8, name="var1")
var2 = var1.assign(1*2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print var2.eval()
print var2.eval()
print var2.eval()
In [21]:
import tensorflow as tf
var1 = tf.Variable(8, name="var1")
var2 = var1.assign(var1*2)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(var1.initializer)
print var2.eval() # result is 16
print var2.eval() # result is 32
print var2.eval() # result is 64
In [ ]: