Variables and Placeholders
Here we initialize a simple placeholder for a piece of data. We then feed a random number into an identity function. If viewed in Tensorboard, the following graph will be created.
#03_placeholders.py
# Placeholders
#----------------------------------
#
# This function introduces how to
# use placeholders in Tensorflow
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# Using Placeholders
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=(4, 4))
y = tf.identity(x)
rand_array = np.random.rand(4, 4)
####
#merged = tf.merge_all_summaries()
merged = tf.summary.merge_all()
#writer = tf.train.SummaryWriter("/tmp/variable_logs", sess.graph_def)
writer = tf.summary.FileWriter("/tmp/variable_logs", sess.graph)
####
writer.close()
print(sess.run(y, feed_dict={x: rand_array}))
原代码会报错
AttributeError: module 'tensorflow' has no attribute 'merge_all_summaries'
AttributeError: module 'tensorflow.python.training.training' has no attribute 'SummaryWriter'
修改后结果
[[ 0.66682321 0.3293488 0.5245204 0.53379935]
[ 0.07488234 0.10959593 0.68837297 0.17703928]
[ 0.35069257 0.77648026 0.27833292 0.44115242]
[ 0.76638317 0.15466152 0.22070079 0.94068062]]
Working with Matrices
Placeholder for future purposes.
# 04_matrices.py
#Matrices and Matrix Operations
#----------------------------------
#
# This function introduces various ways to create
# matrices and how to use them in Tensorflow
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# Declaring matrices
sess = tf.Session()
# Declaring matrices
# Identity matrix
identity_matrix = tf.diag([1.0,1.0,1.0])
print(sess.run(identity_matrix))
# 2x3 random norm matrix
A = tf.truncated_normal([2,3])
print(sess.run(A))
# 2x3 constant matrix
B = tf.fill([2,3], 5.0)
print(sess.run(B))
# 3x2 random uniform matrix
C = tf.random_uniform([3,2])
print(sess.run(C))
print(sess.run(C)) # Note that we are reinitializing, hence the new random variabels
# Create matrix from np array
D = tf.convert_to_tensor(np.array([[1., 2., 3.], [-3., -7., -1.], [0., 5., -2.]]))
print(sess.run(D))
# Matrix addition/subtraction
print(sess.run(A+B))
print(sess.run(B-B))
# Matrix Multiplication
print(sess.run(tf.matmul(B, identity_matrix)))
# Matrix Transpose
print(sess.run(tf.transpose(C))) # Again, new random variables
# Matrix Determinant
print(sess.run(tf.matrix_determinant(D)))
# Matrix Inverse
print(sess.run(tf.matrix_inverse(D)))
# Cholesky Decomposition
print(sess.run(tf.cholesky(identity_matrix)))
# Eigenvalues and Eigenvectors
print(sess.run(tf.self_adjoint_eig(D)))
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
[[ 0.4898375 -0.65810621 0.20719798]
[-0.45387158 -0.72594666 0.2478656 ]]
[[ 5. 5. 5.]
[ 5. 5. 5.]]
[[ 0.37838769 0.72760344]
[ 0.2227149 0.2249614 ]
[ 0.73352134 0.32676136]]
[[ 0.90364814 0.73233008]
[ 0.61350799 0.37857103]
[ 0.41849363 0.42342687]]
[[ 1. 2. 3.]
[-3. -7. -1.]
[ 0. 5. -2.]]
[[ 3.90597057 4.19510365 6.0628376 ]
[ 4.86557484 6.02541924 3.63600206]]
[[ 0. 0. 0.]
[ 0. 0. 0.]]
[[ 5. 5. 5.]
[ 5. 5. 5.]]
[[ 0.04690075 0.34524703 0.48222351]
[ 0.15054452 0.74442649 0.36477065]]
-38.0
[[-0.5 -0.5 -0.5 ]
[ 0.15789474 0.05263158 0.21052632]
[ 0.39473684 0.13157895 0.02631579]]
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
(array([-10.65907521, -0.22750691, 2.88658212]), array([[ 0.21749542, 0.63250104, -0.74339638],
[ 0.84526515, 0.2587998 , 0.46749277],
[-0.4880805 , 0.73004459, 0.47834331]]))