TensorFlow Machine Learning Cookbook(读书笔记)

第一章 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


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,530评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,403评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,120评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,770评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,758评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,649评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,021评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,675评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,931评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,751评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,410评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,004评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,969评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,042评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,493评论 2 343

推荐阅读更多精彩内容