Tensorflow快速入门教程

来源:互联网 发布:贴吧网盘网络出错 编辑:程序博客网 时间:2024/06/06 10:54

什么是Tensorflow?

  • Tensorflow是google发布的深度学习开源框架
  • Tensorflow提供了定义张量的函数,而且可以自动的计算导数

TensorFlow VS. Numpy

  • Few people make this comparison, but TensorFlow and
    Numpy are quite similar. (Both are N-d array libraries!)
  • Numpy has Ndarray support, but doesn’t offer methods to
    create tensor functions and automatically compute
    derivatives (+ no GPU support).

Simple Numpy Recap

In [23]: import numpy as npIn [24]: a = np.zeros((2,2)); b = np.ones((2,2))In [25]: np.sum(b, axis=1)Out[25]: array([ 2., 2.])In [26]: a.shapeOut[26]: (2, 2)In [27]: np.reshape(a, (1,4))Out[27]: array([[ 0., 0., 0., 0.]])

Repeat in TensorFlow

In [31]: import tensorflow as tfIn [32]: tf.InteractiveSession()    ##More on SessionsoonIn [33]: a = tf.zeros((2,2)); b = tf.ones((2,2))In [34]: tf.reduce_sum(b, reduction_indices=1).eval()Out[34]: array([ 2., 2.], dtype=float32)In [35]: a.get_shape()      ##TensorShape behaveslike a python tuple.Out[35]: TensorShape([Dimension(2), Dimension(2)])In [36]: tf.reshape(a, (1, 4)).eval()Out[36]: array([[ 0., 0., 0., 0.]], dtype=float32)

这里写图片描述
这里写图片描述

● tf.InteractiveSession() is just convenient syntactic sugar for keeping a default session open in ipython.
● sess.run(c) is an example of a TensorFlow Fetch. Will say more on this soon

Tensorflow Computation Graph

  • TensorFlow programs are usually structured into a construction phase, that assembles a graph, and an execution phase that uses a session to execute ops in the graph.
  • All computations add nodes to global default graph

Tensorflow Variables

-“When you train a model you use variables to hold and update parameters. Variables are in-memory buffers containing tensors
- All tensors we’ve used previously have been constant tensors, not variables
这里写图片描述
这里写图片描述

Updating Variable State

这里写图片描述

Fetch Variable State

这里写图片描述
这里写图片描述

Inputing Data

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

Variable Scope

这里写图片描述
这里写图片描述
这里写图片描述

Understanding get_variable()

这里写图片描述

这里写图片描述

Tensorflow实战:线性回归

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

计算流图如下所示:

这里写图片描述
这里写图片描述

Tips: 关于更多的Tensorflow教程请参考Tensorflow中文社区http://www.tensorfly.cn/以及莫烦老师一系列的Tensorflow教程http://list.youku.com/albumlist/show?id=27327189&asce,谢谢大家!

0 0
原创粉丝点击