文章目录
1 【TensorFlow】程序设计模式
TensorFlow 程序设计模式的核心是“计算图”,可分为两部分:建立“计算图”与执行计算图 ,更多说明可以查看 《TensorFlow+Keras》Learning notes
1.1 建立计算图
导入TensorFlow模块
import tensorflow as tf
1.1.1 tf.constant
https://tensorflow.google.cn/api_docs/python/tf/constant
tf.constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
除了直接赋值以外,tf还提供使用tf.ones()、tf.zeros()等初始化张量的方法
ts_c = tf.constant(2,name='ts_c')
查看常数
ts_c
output
<tf.Tensor 'ts_c:0' shape=() dtype=int32>
- tf.Tensor 代表是张量
- shape=() 代表0维的tensor ,也就是数值
- dtype=int32> 数据类型是int32
1.1.2 tf.Variable 建立变量
tensorflow中的变量是通过Variable类来实现的,类初始化函数为tf.Variable():
def __init__(self,
initial_value=None,
trainable=True,
collections=None,
validate_shape=True,
caching_device=None,
name=None,
variable_def=None,
dtype=None,
expected_shape=None,
import_scope=None)
TensorFlow中的变量特指深度学习机制中,控制输入到输出映射的可以变化的数据,这些变化数据随着训练迭代的进行,不断地改变数值,不断优化,使输出的结果越来越接近于正确的结果。
tensorflow中的可以改变的量包括训练过程中的输入数据,输出数据以及控制从输入到输出的学习机制(具体体现为网络参数),输入输出数据在tf中是用placeholder占位符定义的,tf的学习机制使用变量来表示。
tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()
ts_x = tf.Variable(ts_c+5,name='ts_x')
ts_x
output
<tf.Variable 'ts_x:0' shape=() dtype=int32_ref>
必须要执行计算图后才能够看到结果
1.2 执行“计算图”
1.2.1 建立Session
sess=tf.Session()
1.2.2 初始化
init = tf.global_variables_initializer()
sess.run(init)
1.2.3 使用sess.run() 显示常量和变量
print('ts_c=',sess.run(ts_c))
print('ts_x=',sess.run(ts_x))
output
ts_c= 2
ts_x= 7
1.2.4 使用name.eval() 显示常量和变量
print('ts_c=',ts_c.eval(session=sess))
print('ts_x=',ts_x.eval(session=sess))
output
ts_c= 2
ts_x= 7
1.2.5 关闭Session
sess.close()
1.2.6 整合一遍
import tensorflow as tf
# 建立计算图
ts_c = tf.constant(2,name='ts_c')
ts_x = tf.Variable(ts_c+5,name='ts_x')
# 打开Session开始执行“计算图”
sess=tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print('ts_c=',sess.run(ts_c))
print('ts_x=',sess.run(ts_x))
# 关闭计算图
sess.close()
1.2.7 with语句打开Session并且自动关闭
import tensorflow as tf
ts_c = tf.constant(2,name='ts_c')
ts_x = tf.Variable(ts_c+5,name='ts_x')
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('ts_c=',sess.run(ts_c))
print('ts_x=',sess.run(ts_x))
output
ts_c= 2
ts_x= 7
1.3 TensorFlow placeholder
第二节中,在建立“计算图”时,我们设置了ts_c常量值为2,并且设置变量ts_x为 ts_c + 5,这都是在建立“计算图”阶段就已经设置完成的。可是我们如果希望在执行“计算图”阶段才设置数值,那么就需要用到 placeholder。
tensorflow中的可以改变的量包括训练过程中的输入数据,输出数据以及控制从输入到输出的学习机制(具体体现为网络参数),输入输出数据在tf中是用placeholder占位符定义的,tf的学习机制使用变量来表示。
tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()
tf.placeholder(
dtype,
shape=None,
name=None
)
- dtype:表示tensorflow中的数据类型,如常用的tf.float32,tf.float64等数值类型;
- shape:表示数据类型,默认的None是一个一维的数值,shape=[None,5],表示行不定,列是5;
- name:张量名称;
1.3.1 建立“计算图”
width = tf.placeholder("int32")
height = tf.placeholder("int32")
area=tf.multiply(width,height)
1.3.2 执行“计算图”
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('area=',sess.run(area,feed_dict={width: 6, height: 8}))
output
area= 48
1.4 TensorFlow数值方法介绍
- tf.add
- tf.subtract
- tf.multiply
- tf.divide
- tf.mod
- tf.sqrt
- tf.abs
你也许会觉得只是简单的数值相乘,为什么要用tf.multiply
?这样做的目的是让TensorFlow具备跨平台的能力
1.5 TensorBoard
加如name,使得计算图更容易读懂
import tensorflow as tf
width = tf.placeholder("int32",name='width')
height = tf.placeholder("int32",name='height')
area=tf.multiply(width,height,name='area')
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('area=',sess.run(area,feed_dict={width: 6,height: 8}))
把数据写入log中
# 将所有要显示在TensorBoard的数据整合
tf.summary.merge_all()
# 将所有要显示在TensorBoard的数据写入log文件
train_writer = tf.summary.FileWriter('log/area',sess.graph)
生成文件为
events.out.tfevents.1532769241.71efb41a2c4a
本地打开服务器上的tensorboard的方法
# cd到area的路径
cd /path/to/log/area
export CUDA_VISIBLE_DEVICES=2
tensorboard --logdir=./
会显示
TensorBoard 1.8.0 at http://71efb41a2c4a:6006 (Press CTRL+C to quit)
在git下输入
ssh -L 6006:127.0.0.1:6006 root@服务器ip -p 端口号
填下登录密码
网站上输入
http://127.0.0.1:6006
即可打开tensor board
显示的结果为
1.6 建立一维与二维张量
1.6.1 Create dim 1 tensor
ts_X = tf.Variable([0.4,0.2,0.4])
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
X=sess.run(ts_X)
print(X)
output
[0.4 0.2 0.4]
查看一维张量
print(X.shape)
output
(3,)
1.6.2 Create dim 2 tensor
ts_X = tf.Variable([[0.4,0.2,0.4]])
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
X=sess.run(ts_X)
print(X)
output
[[0.4 0.2 0.4]]
查看二维张量
print('shape:',X.shape)
output
shape: (1, 3)
once more
W = tf.Variable([[-0.5,-0.2 ],
[-0.3, 0.4 ],
[-0.5, 0.2 ]])
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
W_array=sess.run(W)
print(W_array)
output
[[-0.5 -0.2]
[-0.3 0.4]
[-0.5 0.2]]
查看二维张量
print(W_array.shape)
结果为
(3, 2)
1.7 矩阵基本运算
1.7.1 矩阵乘法
#1,3
X = tf.Variable([[1.,1.,1.]])
# 3,2
W = tf.Variable([[-0.5,-0.2 ],
[-0.3, 0.4 ],
[-0.5, 0.2 ]])
#(1,3)*(3*2) = (1,2)
XW =tf.matmul(X,W )
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(XW ))
output
[[-1.3 0.4]]
1.7.2 矩阵加法
b = tf.Variable([[ 0.1,0.2]])
XW =tf.Variable([[-1.3,0.4]])
Sum =XW+b
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('Sum:')
print(sess.run(Sum ))
output
Sum:
[[-1.1999999 0.6 ]]
1.7.3 矩阵乘法与加法
X = tf.Variable([[1.,1.,1.]])
W = tf.Variable([[-0.5,-0.2 ],
[-0.3, 0.4 ],
[-0.5, 0.2 ]])
b = tf.Variable([[0.1,0.2]])
XWb =tf.matmul(X,W)+b
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('XWb:')
print(sess.run(XWb ))
output
XWb:
[[-1.1999999 0.6 ]]
2 仿真 Neural Network
2.1 以矩阵运算仿真神经网络
2.1.1 以矩阵运算仿真神经网络的信息传导
[ y 1 y 2 ] = a c t i v a t i o n ( [ x 1 x 2 x 3 ] × [ w 11 w 12 w 21 w 22 w 31 w 32 ] ) + [ b 1 b 2 ] \left [y1 \ y2 \right ]= activation\left ( \left [x1 \ x2 \ x3 \right ]\times \begin{bmatrix} w11 & w12\\ w21 & w22\\ w31 & w32 \end{bmatrix} \right )+\left [b1 \ b2 \right ] [y1 y2]=activation⎝⎛[x1 x2 x3]×⎣⎡w11w21w31w12w22w32⎦⎤⎠⎞+[b1 b2]
1×2 = 1×3 × 3×2 + 1*2
另外一种书写模式
y
=
a
c
t
i
v
a
t
i
o
n
(
X
W
+
b
)
y = activation(XW+b)
y=activation(XW+b)
2.1.2 TensorFlow张量运算仿真神经网络
import tensorflow as tf
import numpy as np
X = tf.Variable([[0.4,0.2,0.4]]) # 1 3
# 3 2
W = tf.Variable([[-0.5,-0.2 ],
[-0.3, 0.4 ],
[-0.5, 0.2 ]])
# 1 2
b = tf.Variable([[0.1,0.2]])
XWb =tf.matmul(X,W)+b
y=tf.nn.relu(tf.matmul(X,W)+b)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('XWb:')
print(sess.run(XWb ))
print('y:')
print(sess.run(y ))
output
XWb:
[[-0.35999998 0.28 ]]
y:
[[0. 0.28]]
2.1.3 矩阵表达式加入sigmoid激活函数
X = tf.Variable([[0.4,0.2,0.4]])
W = tf.Variable([[-0.5,-0.2 ],
[-0.3, 0.4 ],
[-0.5, 0.2 ]])
b = tf.Variable([[0.1,0.2]])
XWb=tf.matmul(X,W)+b
y=tf.nn.sigmoid(tf.matmul(X,W)+b)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('XWb:')
print(sess.run(XWb))
print('y:')
print(sess.run(y ))
output
XWb:
[[-0.35999998 0.28 ]]
y:
[[0.41095957 0.5695462 ]]
2.1.4 以正态分布的随机数生成权重与偏差的初始值
tf.random_normal
ts_norm = tf.random_normal([1000])
with tf.Session() as session:
norm_data=ts_norm.eval()
print(len(norm_data))
print(norm_data[:30])
画一下直方图 plt.hist
import matplotlib.pyplot as plt
plt.hist(norm_data)
plt.show()
output
W = tf.Variable(tf.random_normal([3, 2]))
b = tf.Variable(tf.random_normal([1, 2]))
X = tf.Variable([[0.4,0.2,0.4]])
y=tf.nn.relu(tf.matmul(X,W)+b)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print('b:')
print(sess.run(b ))
print('W:')
print(sess.run(W ))
print('y:')
print(sess.run(y ))
output
b:
[[1.7467148 1.7830558]]
W:
[[ 0.59705985 0.54205817]
[-0.34251437 0.7435389 ]
[ 1.7557956 -0.33301157]]
y:
[[2.6193542 2.0153823]]
2.1.5 执行一次sess.run()可以取得3个TensorFlow变量
(_b,_W,_y) = sess.run((b,W,y))
.
W = tf.Variable(tf.random_normal([3, 2]))
b = tf.Variable(tf.random_normal([1, 2]))
X = tf.Variable([[0.4,0.2,0.4]])
y=tf.nn.relu(tf.matmul(X,W)+b)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
(_b,_W,_y)=sess.run((b,W,y))
print('b:')
print(_b)
print('W:')
print(_W)
print('y:')
print(_y)
output
b:
[[-1.285027 1.5168586]]
W:
[[-0.81171405 -0.81046146]
[ 0.0275692 0.32414046]
[ 0.00839781 1.3495028 ]]
y:
[[0. 1.7973032]]
2.2 以placeholder传入X值
先定义placeholder,X = tf.placeholder("float", [None,3])
, tf.placeholder
共有两个参数,float
是数据类型,[None,3]
表示,传入的X项数不变,每一项只有3个数字
W = tf.Variable(tf.random_normal([3, 2]))
b = tf.Variable(tf.random_normal([1, 2]))
X = tf.placeholder("float", [None,3])
y=tf.nn.relu(tf.matmul(X,W)+b)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
X_array = np.array([[0.4,0.2,0.4]])
(_b,_W,_X,_y)=sess.run((b,W,X,y),feed_dict={X:X_array})
print('b:')
print(_b)
print('W:')
print(_W)
print('X:')
print(_X)
print('y:')
print(_y)
output
b:
[[-1.00988698 -0.90781182]]
W:
[[ 0.77819425 0.74534345]
[ 0.62385881 -0.30757746]
[ 0.84864932 1.10149086]]
X:
[[ 0.40000001 0.2 0.40000001]]
y:
[[ 0. 0.]]
2.3 创建layer函数以矩阵运算仿真神经网络
前面的章节已经示范了以矩阵运算仿真神经网路,下面我们将以相同的方式来建立类神经网络多层感知器,方便后续使用,我们创建layer函数。
def layer(output_dim,input_dim,inputs, activation=None):
W = tf.Variable(tf.random_normal([input_dim, output_dim]))
b = tf.Variable(tf.random_normal([1, output_dim]))
XWb = tf.matmul(inputs, W) + b
if activation is None:
outputs = XWb
else:
outputs = activation(XWb)
return outputs
- output_dim 输出神经元个数
- input_dim 输入神经元个数
- inputs 输入的二维数组 placeholder
- activation 激活函数
2.3.1 两层神经网络
我们建立三层神经网络,输入层4个,输出层3个神经元
X = tf.placeholder("float", [None,4])
y=layer(output_dim=3,input_dim=4,inputs=X,
activation=tf.nn.relu)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
X_array = np.array([[0.4,0.2 ,0.4,0.1],
[0.3,0.4 ,0.5,0.3],
[0.3,-0.4,0.5,0.2]])
(_X,_y)=sess.run((X,y),feed_dict={X:X_array})
print('X:')
print(_X)
print('y:')
print(_y)
output
X:
[[ 0.4 0.2 0.4 0.1]
[ 0.3 0.4 0.5 0.3]
[ 0.3 -0.4 0.5 0.2]]
y:
[[0. 0.03858879 0. ]
[0. 0.45277464 0. ]
[0. 0.23971073 0.55798537]]
2.3.2 三层神经网络
接下来我们建立三层神经网络,输入层4个,隐藏层3个,输出层2个神经元
X = tf.placeholder("float", [None,4])
h=layer(output_dim=3,input_dim=4,inputs=X,
activation=tf.nn.relu)
y=layer(output_dim=2,input_dim=3,inputs=h)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
X_array = np.array([[0.4,0.2 ,0.4,0.5]])
(layer_X,layer_h,layer_y)= \
sess.run((X,h,y),feed_dict={X:X_array})
print('input Layer X:')
print(layer_X)
print('hidden Layer h:')
print(layer_h)
print('output Layer y:')
print(layer_y)
output
input Layer X:
[[0.4 0.2 0.4 0.5]]
hidden Layer h:
[[0. 0.636781 1.0871928]]
output Layer y:
[[-0.29558948 -2.8924227 ]]
2.3.3 显示下W和b
2.3.2节中,我们只显示了输入和输出,为了更好的了解neural network,我们也显示下中间的W和b
def layer_debug(output_dim,input_dim,inputs, activation=None):
W = tf.Variable(tf.random_normal([input_dim, output_dim]))
b = tf.Variable(tf.random_normal([1, output_dim]))
XWb = tf.matmul(inputs, W) + b
if activation is None:
outputs = XWb
else:
outputs = activation(XWb)
return outputs,W,b
在return的时候加上W和b
调用
X = tf.placeholder("float", [None,4])
h,W1,b1=layer_debug(output_dim=3,input_dim=4,inputs=X,
activation=tf.nn.relu)
y,W2,b2=layer_debug(output_dim=2,input_dim=3,inputs=h)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
X_array = np.array([[0.4,0.2 ,0.4,0.5]])
(layer_X,layer_h,layer_y,W1,b1,W2,b2)= \
sess.run((X,h,y,W1,b1,W2,b2),feed_dict={X:X_array})
print('input Layer X:')
print(layer_X)
print('W1:')
print( W1)
print('b1:')
print( b1)
print('hidden Layer h:')
print(layer_h)
print('W2:')
print( W2)
print('b2:')
print( b2)
print('output Layer y:')
print(layer_y)
output
input Layer X:
[[0.4 0.2 0.4 0.5]]
W1:
[[-0.82587284 -0.71107566 1.6258259 ]
[-1.070925 0.820703 -2.161224 ]
[-1.2921784 -1.14421 -1.3333445 ]
[ 0.78470355 0.68681616 0.6445859 ]]
b1:
[[-0.05464231 0.0210133 0.39013696]]
hidden Layer h:
[[0. 0. 0.39717773]]
W2:
[[-0.43815833 -0.69938123]
[-2.0985272 -0.8762365 ]
[-0.53423506 0.8335325 ]]
b2:
[[2.41684 1.1108794]]
output Layer y:
[[2.2046537 1.44194 ]]
X×W1+b1 = h
1×4 × 4×3 + 1×3 = 1×3
h×W2+b2 = y
1×3 + 3×2 + 1×2 = 1×2
声明:代码源于《TensorFlow+Keras深度学习人工智能实践应用》 林大贵版,引用、转载请注明出处,谢谢,如果对书本感兴趣,买一本看看吧!!!