TensorFlow安装使用入门

来源:互联网 发布:正规网络医生兼职平台 编辑:程序博客网 时间:2024/05/01 13:35

参考网页:http://wiki.jikexueyuan.com/project/tensorflow-zh/

TensorFLow基本特征

  1. 使用图(graph)来表示计算任务。
  2. 在会话(Session)的上下文(context)中执行图。
  3. 使用tensor表示数据。
  4. 使用变量(Variable)维护状态。
  5. 使用feed和fetch可以为任意的操作赋值和从中获取数据。
  6. TensorFlow计算的单位是OP,它表示了某种抽象计算。

Python3安装TensorFlow

pip3 install tensorflow

简单使用

1. 构建图

构建图的第一步是创建源op(source op),源op不需要任何输入,源op的输出被传递给其他op做运算。
TensorFlow Python 库有一个默认图 (default graph), op 构造器可以为其增加节点. 这个默认图对 许多程序来说已经足够用了。

xzw@bogon:~$ python3>>> import tensorflow as tf>>> #创建一个常量op,产生一个1x2矩阵,这个op被作为一个节点,加到默认图中... #构造器的返回值代表常量op的返回值... >>> matrix1 = tf.constant([[3.,3.]])>>> #创建另外一个常量op,产生一个2X1矩阵... >>> matrix2 = tf.constant([[2.],[2.]])>>> #创建一个矩阵乘法matmul op,把‘matrix’... #和‘matrix2’作为输入,返回值代表矩阵乘法的结果... >>> product = tf.matmul(matrix1, matrix2)

2. 启动图

为了真正进行矩阵相乘运算, 并得到矩阵乘法的 结果, 你必须在会话里启动这个图。
启动图第一步书创建一个Session对象,如果没有创建任何参数,会话构造器会启动默认的图。

>>> #启动默认图... >>> sess = tf.Session()>>> #调用Sess的run方法来执行矩阵乘法op,整个执行过程是自动化的,会话负责... #传递op所需要的全部输入,op通常是并发执行的... #返回值‘result’是一个numpy ’ndarry‘对象... >>> result = sess.run(product)>>> print(result)[[ 12.]]>>> #任务结束关闭会话... >>> sess.close()

变量、Fetch、Feed

1. 变量

变量维护图执行过程中的状态信息. 下面的例子演示了如何使用变量实现一个简单的计数器。

>>> #创建一个变量,初始化变量为0...state = tf.Variable(0, name="counter")>>> #创建一个op,其作用是使state增加1... one = tf.constant(1)>>> new_value = tf.add(state, one)>>> update = tf.assign(state,new_value)>>> #启动图后,变量必须经过’初始化‘,首先增加一个’初始化‘op到图中...init_op= tf.global_variables_initializer()>>> with tf.Session() as sess:...     #运行 'init' op...     sess.run(init_op)...     #打印’state‘的初始值...     print (sess.run(state))...     #运行op,更新'state'并打印‘state’...     for _ in range(3):...             sess.run(update)...             print(sess.run(state))... 0123

assign() 操作是图所描绘的表达式的一部分, 正如 add() 操作一样. 所以在调用 run() 执行表达式之前, 它并不会真正执行赋值操作.
你可以将一个神经网络的权重作为某个变量存储在一个 tensor 中. 在训练过程中, 通过重复运行训练图, 更新这个 tensor.

2. Fetch

为了取回操作的输出内容, 可以在使用 Session 对象的 run() 调用 执行图时, 传入一些 tensor, 这些 tensor 会帮助你取回结果.

>>> import tensorflow as tf>>> input1 = tf.constant(3.0)>>> input2 = tf.constant(2.0)>>> input3 = tf.constant(5.0)>>> intermed =  tf.add(input2, input3)>>> mul = tf.mulitiy(input1, intermed)Traceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: module 'tensorflow' has no attribute 'mulitiy'>>> mul = tf.multiple(input1, intermed)Traceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: module 'tensorflow' has no attribute 'multiple'>>> mul = tf.multiply(input1, intermed)>>> with tf.Session() as sess:...     result= sess.run([mul,intermed])...     print(result)... [21.0, 7.0]

需要获取的多个 tensor 值,在 op 的一次运行中一起获得(而不是逐个去获取 tensor)

3. Feed

Feed机制可以临时替代图中的任意操作中的tensor,可以对图中任何操作提交一个补丁,直接插入一个tensor。
最常见的用例是将某些特殊的操作指定为 “feed” 操作, 标记的方法是使用 tf.placeholder() 为这些操作创建占位符。

>>> import tensorflow as tf>>> input1 = tf.placeholder(tf.float32)>>> input2 = tf.placeholder(tf.float32)>>> output = tf.mul(input1, input2)Traceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: module 'tensorflow' has no attribute 'mul'>>> output = tf.multiply(input1, input2)>>> with tf.Session() as sess:...     print(sess.run([output],feed_dict={input1:[7.], input2:[2.]}))... [array([ 14.], dtype=float32)]
原创粉丝点击