TensorFlow Machine Learning Cookbook(读书笔记)

理工大叔IP属地: 英格兰
字数 62阅读 3,140

第一章 Getting Started With TensoeFlow

 import tensorflow as tf
tf.InteractiveSession();

Fixed tensors

row_dim = tf.constant(3)
col_dim = tf.constant(5)
print("row_dim = ", row_dim.eval())
print("col_dim = ", col_dim.eval(), "\n")

zero_tsr = tf.zeros([row_dim, col_dim])
print(tf.Session().run(zero_tsr), "\n")

ones_tsr = tf.ones([row_dim, col_dim])
print(tf.Session().run(ones_tsr), "\n")

filled_tsr = tf.fill([row_dim, col_dim], 42)
print(tf.Session().run(filled_tsr), "\n")


constant_tsr = tf.constant([1,2,3])
print(tf.Session().run(constant_tsr), "\n")

nrow = 3
ncol = 5
cnst_42 = tf.constant(42, shape = [nrow, ncol])  # must have "shape=", otherwise, there will be error
print(cnst_42.eval(), "\n")
row_dim =  3
col_dim =  5 

[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]] 

[[ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.]] 

[[42 42 42 42 42]
 [42 42 42 42 42]
 [42 42 42 42 42]] 

[1 2 3] 

[[42 42 42 42 42]
 [42 42 42 42 42]
 [42 42 42 42 42]] 

Tensors of similar shape

zeros_similar = tf.zeros_like(constant_tsr)
print(tf.Session().run(zeros_similar), "\n")

ones_similar = tf.ones_like(constant_tsr)
print(tf.Session().run(ones_similar), "\n")
[0 0 0] 

[1 1 1] 

Sequence tensors

# tf.lin_space(start, stop, num, name=None)
# tf.linspace(start, stop, num, name=None)
# There is error in the book

linear_tsr = tf.lin_space(start=0.0, stop=1.0, num=3) # error in book using start=0, because datatype
print(tf.Session().run(linear_tsr), "\n")
[ 0.   0.5  1. ] 
integer_seq_tsr = tf.range(start=6, limit=15, delta=3)
print(integer_seq_tsr.eval(), "\n")
[ 6  9 12] 

Random tensors

randunif_tsr = tf.random_uniform([row_dim, col_dim], minval=0, maxval=1)
print(randunif_tsr.eval())
[[ 0.2380867   0.74371564  0.89179921  0.07101989  0.71987915]
 [ 0.50441158  0.17766714  0.19206154  0.56053674  0.72753906]
 [ 0.58974826  0.87940145  0.43089223  0.83657384  0.72699416]]
randnorm_tsr = tf.random_normal([row_dim, col_dim],mean=0.0, stddev=1.0)
print(randnorm_tsr.eval())
[[ 0.73832607 -1.00233281  0.22258358  0.76114374 -2.26935649]
 [-1.84081531 -1.82415056  0.89691728 -0.90562403  0.13559598]
 [-0.44200319  0.31381497  0.02425084  1.61163747 -1.61524034]]
# 截断在两倍的标准差
runcnorm_tsr = tf.truncated_normal([row_dim, col_dim],mean=0.0, stddev=1.0)
print(runcnorm_tsr.eval())
[[-1.14313436 -1.96878684  0.2865687   1.29139364  0.32751861]
 [ 0.57910866  0.442716    0.64700919  1.43108535  0.60666579]
 [ 0.73394674 -0.33901712  0.49338096 -1.35382307 -0.32706863]]
# 就像洗牌一样
input_tensor = tf.constant([1,2,3,4,5])
shuffled_output = tf.random_shuffle(input_tensor)
print(shuffled_output.eval())
cropped_output = tf.random_crop(input_tensor, tf.constant([4]))
print(cropped_output.eval())
[3 5 2 4 1]
[1 2 3 4]
import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('butterfly.jpg',0)
#plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.imshow(img)
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()


#cropped_image = tf.random_crop(img, [height/2,width/2, 3])
# not work? why? how to transfer image from opencv to tensorflow?
np_img = np.asarray(img)
tf_img = tf.convert_to_tensor(np_img)
#cropped_image = tf.random_crop(tf_img, [20, 20, 3])

Working with Matrices

sess = tf.Session()
identity_matrix = tf.diag([1.0, 1.0, 1.0])
print(sess.run(identity_matrix))

A = tf.truncated_normal([2, 3])
print(sess.run(A))

B = tf.fill([2,3], 5.0)
print(sess.run(B))

C = tf.random_uniform([3,2])
print(sess.run(C))

D = tf.convert_to_tensor(np.array([[1., 2., 3.],[-3., -7.,-1.],[0., 5., -2.]]))
print(sess.run(D))

print(sess.run(C))
print(sess.run(C))
print(sess.run(C))
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
[[ 1.08559239  1.04896152  0.1201797 ]
 [ 1.22814488  0.54292905 -1.32194078]]
[[ 5.  5.  5.]
 [ 5.  5.  5.]]
[[ 0.98243904  0.89024794]
 [ 0.66411185  0.82579648]
 [ 0.81462467  0.08408237]]
[[ 1.  2.  3.]
 [-3. -7. -1.]
 [ 0.  5. -2.]]
[[ 0.40859282  0.42885113]
 [ 0.60115874  0.61496174]
 [ 0.20809424  0.27878082]]
[[ 0.9891181   0.73478913]
 [ 0.93645871  0.67719197]
 [ 0.93193257  0.63833511]]
[[ 0.09559619  0.33333921]
 [ 0.16805577  0.03947639]
 [ 0.44392323  0.50572383]]
#每一次运行,都会重新计算A,也就是说,说整个图的重新运行

print(sess.run(A+B))
print(sess.run(B-B))
[[ 5.32682133  3.99796844  4.38494205]
 [ 5.4356308   4.19038439  4.60341787]]
[[ 0.  0.  0.]
 [ 0.  0.  0.]]
print(sess.run(tf.matmul(B, identity_matrix)))
[[ 5.  5.  5.]
 [ 5.  5.  5.]]
print(sess.run(tf.transpose(C)))
[[ 0.87743354  0.06294203  0.89154458]
 [ 0.20452428  0.2573818   0.97987056]]
print(sess.run(tf.matrix_determinant(D)))
-38.0
print(sess.run(tf.matrix_inverse(D)))
[[-0.5        -0.5        -0.5       ]
 [ 0.15789474  0.05263158  0.21052632]
 [ 0.39473684  0.13157895  0.02631579]]
print(sess.run(tf.cholesky(identity_matrix)))
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
print(sess.run(tf.self_adjoint_eig(D)))
(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]]))

Declaring Operations

print(sess.run(tf.div(3,4)))
0
print(sess.run(tf.truediv(3,4)))
0.75
print(sess.run(tf.floordiv(3.0,4.0)))
0.0
print(sess.run(tf.mod(22.0, 5.0)))
2.0
print(sess.run(tf.cross([1., 0., 0.], [0., 1., 0.])))
[ 0.  0.  1.]

Implementing Activation Functions

print(sess.run(tf.nn.relu([-3., 3., 10.])))
[  0.   3.  10.]
print(sess.run(tf.nn.relu6([-3., 3., 10.])))
[ 0.  3.  6.]
print(sess.run(tf.nn.sigmoid([-1., 0., 1.])))
[ 0.26894143  0.5         0.7310586 ]
print(sess.run(tf.nn.tanh([-1., 0., 1.])))
[-0.76159418  0.          0.76159418]
print(sess.run(tf.nn.softsign([-1., 0., -1.])))
[-0.5  0.  -0.5]
print(sess.run(tf.nn.softplus([-1., 0., -1.])))
[ 0.31326166  0.69314718  0.31326166]
print(sess.run(tf.nn.elu([-1., 0., 100.])))
[  -0.63212055    0.          100.        ]

Working with Data Sources

from sklearn import datasets
iris = datasets.load_iris()
print(len(iris.data))
150
print(len(iris.target))
150
print(iris.target)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]
print(set(iris.target))
{0, 1, 2}

Chapter 2. The TensorFlow Way

2.1 Operations in a Computational Graph

import numpy as np
x_vals = np.array([1., 3., 5., 7., 9.])
x_data = tf.placeholder(tf.float32)
m_const = tf.constant(3.)
my_product = tf.multiply(x_data, m_const)
for x_val in x_vals:
    print(sess.run(my_product, feed_dict={x_data: x_val}))
3.0
9.0
15.0
21.0
27.0

2.2 Layering Nested Operations

my_array = np.array([[1., 3., 5., 7., 9.],
                     [-2., 0., 2., 4., 6.],
                     [-6., -3., 0., 3., 6.]])
x_vals = np.array([my_array, my_array + 1])
x_data = tf.placeholder(tf.float32, shape=(3, 5))

m1 = tf.constant([[1.],[0.],[-1.],[2.],[4.]])
m2 = tf.constant([[2.]])
a1 = tf.constant([[10.]])

prod1 = tf.matmul(x_data, m1)
prod2 = tf.matmul(prod1, m2)
add1 = tf.add(prod2, a1)

for x_val in x_vals:
    print(sess.run(add1, feed_dict={x_data: x_val}))
[[ 102.]
 [  66.]
 [  58.]]
[[ 114.]
 [  78.]
 [  70.]]
print(x_vals)
[[[  1.   3.   5.   7.   9.]
  [ -2.   0.   2.   4.   6.]
  [ -6.  -3.   0.   3.   6.]]

 [[  2.   4.   6.   8.  10.]
  [ -1.   1.   3.   5.   7.]
  [ -5.  -2.   1.   4.   7.]]]

2.3 Working with Multiple Layers

import tensorflow as tf
import numpy as np
sess = tf.Session()

# image number, height, width, and channel
# input tensor of shape [batch, in_height, in_width, in_channels]

x_shape = [1, 4, 4, 1]
x_val = np.random.uniform(size=x_shape)
x_data = tf.placeholder(tf.float32, shape=x_shape)

# filetr: [filter_height, filter_width, in_channels, out_channels]
my_filter = tf.constant(0.25, shape=[2, 2, 1, 1])
my_strides = [1, 2, 2, 1]
mov_avg_layer= tf.nn.conv2d(x_data, my_filter, my_strides, padding='SAME', name='Moving_Avg_Window')

# tf.squeeze, Removes dimensions of size 1 from the shape of a tensor.
# for example 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
# shape(squeeze(t)) ==> [2, 3]

def custom_layer(input_matrix):
    input_matrix_sqeezed = tf.squeeze(input_matrix)
    A = tf.constant([[1., 2.], [-1., 3.]])
    b = tf.constant(1., shape=[2, 2])
    temp1 = tf.matmul(A, input_matrix_sqeezed)
    temp = tf.add(temp1, b) # Ax + b
    return(tf.sigmoid(temp))

with tf.name_scope('Custom_Layer') as scope:
    custom_layer1 = custom_layer(mov_avg_layer)

print(sess.run(custom_layer1, feed_dict={x_data: x_val}))
[[ 0.94253522  0.89769566]
 [ 0.92872465  0.78327972]]

2.4 Implementing Loss Functions

import matplotlib.pyplot as plt
import tensorflow as tf
x_vals = tf.linspace(-1., 1., 500)
target = tf.constant(0.)
l2_y_vals = tf.square(target - x_vals)
l2_y_out = sess.run(l2_y_vals)
l1_y_vals = tf.abs(target - x_vals)
l1_y_out = sess.run(l1_y_vals)
delta1 = tf.constant(0.25)
phuber1_y_vals = tf.multiply(tf.square(delta1), tf.sqrt(1. + tf.square((target - x_vals)/delta1)) - 1.)
phuber1_y_out = sess.run(phuber1_y_vals)
delta2 = tf.constant(5.)
phuber2_y_vals = tf.multiply(tf.square(delta2), tf.sqrt(1. + tf.square((target - x_vals)/delta2)) - 1.)
phuber2_y_out = sess.run(phuber2_y_vals)
x_vals = tf.linspace(-3., 5., 500)
target = tf.constant(1.)
targets = tf.fill([500,], 1.)
hinge_y_vals = tf.maximum(0., 1. - tf.multiply(target, x_vals))
hinge_y_out = sess.run(hinge_y_vals)
xentropy_y_vals = - tf.multiply(target, tf.log(x_vals)) - tf.multiply((1. - target), tf.log(1. - x_vals))
xentropy_y_out = sess.run(xentropy_y_vals)
# ?????????????
# xentropy_sigmoid_y_vals = tf.nn.sigmoid_cross_entropy_with_logits(x_vals, targets)
# xentropy_sigmoid_y_out = sess.run(xentropy_sigmoid_y_vals)
weight = tf.constant(0.5)
xentropy_weighted_y_vals = tf.nn.weighted_cross_entropy_with_logits(x_vals, targets,weight)
xentropy_weighted_y_out = sess.run(xentropy_weighted_y_vals)
unscaled_logits = tf.constant([[1., -3., 10.]])
target_dist = tf.constant([[0.1, 0.02, 0.88]])
# softmax_xentropy = tf.nn.softmax_cross_entropy_with_logits(unscaled_logits, target_dist)
# print(sess.run(softmax_xentropy))
unscaled_logits = tf.constant([[1., -3., 10.]])
sparse_target_dist = tf.constant([2])
# sparse_xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(unscaled_logits, sparse_target_dist)
# print(sess.run(sparse_xentropy))
x_array = sess.run(x_vals)
plt.plot(x_array, l2_y_out, 'b-', label='L2 Loss')
plt.plot(x_array, l1_y_out, 'r--', label='L1 Loss')
plt.plot(x_array, phuber1_y_out, 'k-.', label='P-Huber Loss(0.25)')
plt.plot(x_array, phuber2_y_out, 'g:', label='P-Huber Loss(5.0)')
plt.ylim(-0.2, 0.4)
plt.legend(loc='lower right', prop={'size': 11})
plt.show()
x_array = sess.run(x_vals)
plt.plot(x_array, hinge_y_out, 'b-', label='Hinge Loss')
plt.plot(x_array, xentropy_y_out, 'r--', label='Cross Entropy Loss')
# plt.plot(x_array, xentropy_sigmoid_y_out, 'k-.', label='Cross Entropy Sigmoid Loss')
plt.plot(x_array, xentropy_weighted_y_out, 'g:', label='Weighted Cross Enropy Loss (x0.5)')
plt.ylim(-1.5, 3)
plt.legend(loc='lower right', prop={'size': 11})
plt.show()

2.5 Implementing Back Propagation


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
更多精彩内容,就在简书APP
"小礼物走一走,来简书关注我"
还没有人赞赏,支持一下
理工大叔教育经历:学士、硕士、博士<br><br>工作经历:工程师、博士后、研究员、副教授,高级软件设...
总资产1共写了7653字获得11个赞共43个粉丝

推荐阅读更多精彩内容