CS20SI Tensorflow for Deeplearning课程笔记(一)

来源:互联网 发布:linux 执行权限 编辑:程序博客网 时间:2024/06/06 11:36

一、 课程目标

  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 tfa = 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 = 2y = 3op1 = 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 grapha = tf.constant(3)# add ops to the user created graphwith g.as_default():    b = tf.constant(5)#Prone to errors

正确的使用方式

g1 = tf.get_default_graph()g2 = tf.Graph()# add ops to the default graphwith 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

阅读全文
0 0
原创粉丝点击