CS20SI Tensorflow for Deeplearning课程笔记(一)

本文介绍了TensorFlow的基础概念,包括张量、计算图等,并探讨了如何构建深度学习模型。同时推荐了一些学习资源,如《TensorFlow for Machine Intelligence》等书籍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、 课程目标

  1. 理解TF的计算图方法
  2. 探索TF的内置函数
  3. 学会怎么去构建和组织最好的模型去做深度学习项目

二、书籍

  1. TensorFlow for Machine Intelligence (TFFMI)
  2. Hands-On Machine Learning with Scikit-Learn and TensorFlow. Chapter 9
    Up and running with TensorFlow
  3. Fundamentals of Deep Learning. Chapter 3: Implementing Neural Networks in TensorFlow (FODL)
    书籍容易过时,推荐tensorflow官网

三、 简化版的Tensorflow

  1. TF Learn (tf.contrib.learn)
  2. TF Slim (tf.contrib.slim):
  3. 高级版 API: Keras, TFLearn, Pretty Tensor

四、Graphs and Sessions

1. tensor

一种n维的矩阵
0-d tensor: scalar (number)
1-d tensor: vector
2-d tensor: matrix
and so on

2. Data Flow Graphs

计算a的值的方式

import tensorflow as tf
a = tf.add(3, 5)
# with clause takes care
# of sess.close()
with tf.Session() as sess:
    print sess.run(a)

数据流图如下所示
 
这里写图片描述

3. tf.Session()

主要用于执行需要计算的tensor。

x = 2
y = 3
op1 = tf.add(x, y)
op2 = tf.mul(x, y)
op3 = tf.pow(op2, op1)
with tf.Session() as sess:
    op3 = sess.run(op3)

可以将图分成几部分,然后并行地计算在CPU,GPU上,如下图所示
 
这里写图片描述

4. tf.Graph()

添加一个运算给图,并将其设为默认图

g = tf.Graph()
with g.as_default():
    a = 3
    b = 5
    x = tf.add(a, b)
    sess = tf.Session(graph=g) # session is run on the graph g
# run session
    sess.close()

获取默认的图

g = tf.get_default_graph()

不要将默认的图和用户创建的图混合在一起,如下情况会报错

g = tf.Graph()
# add ops to the default graph
a = tf.constant(3)
# add ops to the user created graph
with g.as_default():
    b = tf.constant(5)
#Prone to errors

正确的使用方式

g1 = tf.get_default_graph()
g2 = tf.Graph()
# add ops to the default graph
with g1.as_default():
    a = tf.Constant(3)
# add ops to the user created graph
    with g2.as_default():
b = tf.Constant(5)

为什么使用Graph?
1. Save computation (only run subgraphs that lead to the values you want to fetch)
2. Break computation into small, differential pieces to facilitates auto-differentiation
3. Facilitate distributed computation, spread the work across multiple CPUs, GPUs, or devices
4. Many common machine learning models are commonly taught and visualized as directed graphs already

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值