TensorFlow Learning Notes #1
基础
1.Tensors are the primary data structure that TensorFlow uses to operate on the computational graph.
#Create fixed tensors
zero_tsr = tf.zeros([row_dim, col_dim])
ones_tsr = tf.ones([row_dim, col_dim])
filled_tsr = tf.fill([row_dim, col_dim], 42)
constant_tsr = tf.constant([1,2,3])
#Create similar shape of tensors
zeros_sim = tf.zeros_like(constant_tsr)
zeros_sim = tf.ones_like(constant_tsr)
#Create sequence tensors
linear_tsr = tf.linspace(start=0.0, stop=1.0, num=3.0)
integer_seq_tsr = tf.range(start=6, limit=15, delta=3)
#Create random tensors
randunif_tsr = tf.random_uniform([row.dim, col_dim], minval=0, maxval=1)
randnorm_tsr = tf.random_normal([row_dim, col_dim], mean=0.0, stddev=1.0)
runcnorm_tsr = tf.truncated_normal([row_dim, col_dim], mean=0.0, stddev=1.0)
shuffled_output = tf.random_shuffle(input_tensor)
cropped_output = tf.random_crop(input_tensor, crop_size)
cropped_image = tf.random_crop(my_image, [height/2, width/2, 3])
2.Variables are the parameters of the algorithm and TensorFlow keeps track of how to change these to optimize the algorithm. Placeholders are objects that allow you to feed in data of a specific type and shape and depend on the results of the computational graph, such as the expected outcome of a computation.
#Create and initialize a variable
my_var = tf.Variable(tf.zeros([2, 3]))
sess = tf.Session()
initialize_op = tf.global_variables_initializer()
sess.run(initialize_op)
#Declar a placeholder and perform operations on it
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[2,2])
y = tf.identity(x)
x_vals = np.random.rand(2,2)
sess.run(y, feed_dict={
x: x_vals})
3.Matrices
#Create matrices
import tensorflow as tf
sess = tf.Session()
identity_matrix = tf.diag([1.0, 1.0, 1.0])
A = tf.truncated_normal([2, 3])
B