本次实验了解Tensorflow这个强大的框架。
使用Tensorflow框架的好处是,只需要编写前向传播和损失函数,反向传播框架会帮我们自动完成。
一个tensorflow模型的大体结构:
##参数形状设置
W1 = tf.get_variable(...)
.....
##训练集输入设置
X=tf.placeholder(...)
Y=...
...
##前向传播计算图搭建
tf.matmul(W1, X)
...
##损失函数计算
tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(...))
...
##反向传播优化方法选择
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
...
##参数初始化
init = tf.global_variables_initializer()
##训练过程
with tf.Session() as sess:
sess.run(init)
_ , minibatch_cost = sess.run([optimizer, cost], feed_dict={
X: minibatch_X, Y: minibatch_Y})
下面来详细介绍一下实验(只节选我认为的重要部分)
计算图的概念
a = tf.constant(2)
b = tf.constant(10)
c = tf.multiply(a,b)
print(c)
运行这部分代码可以发现,c平不是20,而是:
这表明c = tf.multiply(a,b)不是在做计算,而是在搭建计算图的结构(方便反向传播)。只有利用Session执行才能进行计算。
sess = tf.Session()
print(sess.run(c))
计算图的搭建
矩阵乘法,激活函数等应当调用tf里面的方法
# GRADED FUNCTION: linear_function
def linear_function():
"""
Implements a linear function:
Initializes W to be a random tensor of shape (4,3)
Initializes X to be a random tensor of shape (3,1)
Initializes b to be a random tensor of shape (4,1)
Returns:
result -- runs the session for Y = WX + b
"""
np.random.seed(1)
### START CODE HERE ### (4 lines of code)
X = tf.constant(np.random.randn(3,1), name = "X")
W = tf.constant(np.random.randn(4,3), name = "W")
b = tf.constant(np.random.randn(4,1), name = "b")
Y = tf.matmul(W, X)+b
### END CODE HERE ###
# Create the session using tf.Session() and run it with sess.run(...) on the variable you want to calculate
### START CODE HERE ###
sess = tf.Session()
result = sess.run(Y)
### END CODE HERE ###
# close the session
sess