TensorFlow基础用法入门教程

这篇文章是关于TensorFlow最基础的用法。千里之行,始于足下。只有从最基本的要素Tensor和Flow入手,才能更好更快的理解TensorFlow的设计理论和精髓。


TensorFlow,简单看就是Tensor和Flow,即意味着Tensor和Flow是TensorFlow最为基础的要素。Tensor意味着data,Flow意味着流动,意味着计算,意味着映射,即数据的流动,数据的计算,数据的映射,同时也体现数据是有向的流动、计算和映射。

一、Tensors

TensorFlow所有的数据都称之为Tensor,即TensorFlow图计算过程中,节点之间是数据流转都是采用Tensor的形式进行,而且也只能采用Tensor形式进行。Tensor可以是一个变量,可以是一个数组,也可以是多维数组等。一般的,Tensor可以抽象为n-dimensional array or list。

Ascalaris a tensor

Avectoris a tensor

Amatrixis a tensor

AN-demensional arrayis a tensor

Tensor有几个重要的属性:

Data types,标志该Tensor存储的数据类型,如tf.float32,tf.String等;

Rank,即tensor是几维的数组,scalar rank为0,vector rank为1,matrix rank为3,N-demensional tensor rank为N;

Shape,tensor的形状,scalar shape为[],vector shape为 [D0],matrix shape为 [D0,D1];

import tensorflow as tf

a = tf.zeros((2, 2))

print(a)

输出

Tensor("zeros:0", shape=(2, 2), dtype=float32)

二、Session

Session,会话是TensorFlow的运行上下文环境,TensorFlow的Operations和Compution Graph都必须运行在TensorFlow的Session中。

import tensorflow as tf

# Build a graph

a = tf.constant(5.0)

b = tf.constant(6.0)

c = a * b

# Launch a graph in a sesion

sess = tf.Session()

# Evaluate a tensor c

print(sess.run(c))

输出30.0

从上面可以看出,Session提供了run方法来执行tensor的计算,run方法有四个参数,如下

fetches 必选

feed_dict 可选

options 可选

run_metadata 可选

另外,由于Session提供的是运行环境,所以,是TensorFlow系统宝贵的资源,用完我们要及时释放,释放方式有如下两种:

第一种方式是,直接调用Session.close()方式显示关闭Session;

第二种方式是,采用代码块,当当前代码块执行完毕后,Session会自动被析构释放掉,如下:

with tf.Session() as sess:

result = sess.run(c)

print(result)

InteractiveSession

有一类session是交互式session,这类session能在运行过程中接收外部的输入。交互式的Session能更好的适用于这类场景。结合Tensor.eval()和Operation.run()来取代Session.run()。这样的好处是避免在一个session中长时间使用一个变量,见下例所示:

# Enter an interactive TensorFlow Session.

import tensorflow as tf

sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])

a = tf.constant([3.0, 3.0])

# Initialize 'x' using the run() method of its initializer op.

x.initializer.run()

# Add an op to subtract 'a' from 'x'.  Run it and print the result

sub = tf.sub(x, a)

print(sub.eval())

[-2. -1.]

# Close the sesion when we are done

sess.close()

三、Variables

变量是TensorFlow重要的概念,前面我们的实例更多的是使用tensorflow constant常量。Variables在使用前必须先初始化好。tensorflow variables提供了tf.initialize_all_variables()快速初始化所有变量。

import tensorflow as tf

W = tf.Variable(tf.zeros((2, 2)), name="weights")

R = tf.Variable(tf.random_normal((2, 2)), name="random_weights")

with tf.Session() as sess:

sess.run(tf.initialize_all_variables())

print(sess.run(W))

print(sess.run(R))

输出

[[ 0.  0.]

[ 0.  0.]]

[[ 1.09000123  0.34866104]

[ 1.02729785  0.71264291]]

用一个变量赋值给另一个变量,如下:

import tensorflow as tf

weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="weights")

w2 = tf.Variable(weights.initialized_value(), name="w2")

with tf.Session() as sess:

sess.run(tf.initialize_all_variables())

print(sess.run(weights))

print(sess.run(w2))

Saving and Restoring Variables

tf.train.Saver提供了Variable checkpoint转储功能和恢复功能,你可以将变量某一时刻的值持久化到磁盘,当需要用时再从磁盘恢复出来,如下:

import tensorflow as tf

v1 = tf.Variable(tf.zeros((2, 2)), name="weights")

v2 = tf.Variable(tf.random_normal((2, 2)), name="random_weights")

init_op = tf.initialize_all_variables()

saver = tf.train.Saver()

with tf.Session() as sess:

sess.run(init_op)

save_path = saver.save(sess, "/tmp/model.ckpt")

print("Model saved in file: %s" % save_path)

输出

Model saved in file: /tmp/model.ckpt

with tf.Session() as sess:

saver.restore(sess, "/tmp/model.ckpt")

更新变量的值

import tensorflow as tf

# state = 0

state = tf.Variable(0, name="counter")

# new_value = state + 1

new_value = tf.add(state, tf.constant(1))

# state = state + 1

update = tf.assign(state, new_value)

with tf.Session() as sess:

sess.run(tf.initialize_all_variables())

print(sess.run(state))

for _ in range(3):

sess.run(update)

print(sess.run(state))

输出

0

1

2

3

获取变量

import tensorflow as tf

in1 = tf.constant(3.0)

in2 = tf.constant(2.0)

in3 = tf.constant(5.0)

temp = tf.add(in2, in3)

mul = tf.mul(in1, temp)

with tf.Session() as sess:

result = sess.run([mul, temp])

print(result)

输出

[21.0, 7.0]

Variable Scope

变量Scope,类似变量的名字空间。variable_scope设置命名空间,get_variable获取命名空间,如下:

import tensorflow as tf

with tf.variable_scope("foo"):

with tf.variable_scope("bar"):

v = tf.get_variable("v", [1])

assert v.name == "foo/bar/v:0"

Reuse variable scope

重用变量命名空间,在RNN场景中会用到该功能,如下

import tensorflow as tf

with tf.variable_scope("foo"):

v = tf.get_variable("v", [1])

tf.get_variable_scope().reuse_variables()

v1 = tf.get_variable("v", [1])

assert v == v1

Placeholders and Feed Dictionaries

tensorflow的占位符,在程序运行过程中进行参数的赋值,非常使用,用于接受程序外部传入的值,如下:

import tensorflow as tf

in1 = tf.placeholder(tf.float32)

in2 = tf.placeholder(tf.float32)

out = tf.mul(in1, in2)

with tf.Session() as sess:

print(sess.run([out], feed_dict={in1:[7.], in2:[2.]}))

输出

[array([ 14.], dtype=float32)]

最后再提下,tensorflow提供了一个转换为tensor的方法,convert_to_tensor(),如下:

import numpy as np

import tensorflow as tf

a = np.zeros((3, 3))

ta = tf.convert_to_tensor(a)

with tf.Session as sess:

print(sess.run(ta))

参考资料



作者:chenweicai
链接:http://www.jianshu.com/p/30e179f0b29a
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值