深度学习作业L2W3:Tensorflow Tutorial

本文介绍了使用Tensorflow进行深度学习的实验,包括计算图的概念、搭建及tf.one_hot的运用。通过搭建识别手写数字的神经网络,阐述了如何利用Tensorflow进行前向传播和损失函数计算,并探讨了反向传播的过程。
摘要由CSDN通过智能技术生成

本次实验了解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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值