摘要:本系列主要对tf的一些常用概念与方法进行描述。本文主要针对tensorflow的数据IO、图的运行等相关函数进行讲解。为‘Tensorflow一些常用基本概念与函数’系列之三。
本文所讲的内容主要为以下相关函数:
操作组操作Data IO (Python functions)TFRecordWrite,rtf_record_iteratorRunning GraphsSession management,Error classes一个TFRecords 文件为一个字符串序列。这种格式并非随机获取,它比较适合大规模的数据流,而不太适合需要快速分区或其他非序列获取方式。
tf.Session
#一个简单的tf.Session例子 # 建立一个graph. a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # 将graph载入到一个会话session中 sess = tf.Session() # 计算tensor `c`. print(sess.run(c)) #一个会话可能会占用一些资源,比如变量、队列和读取器(reader)。释放这些不再使用的资源非常重要。 #使用close()方法关闭会话,或者使用上下文管理器,释放资源。 # 使用`close()`方法. sess = tf.Session() sess.run(...) sess.close() # 使用上下文管理器 with tf.Session() as sess: sess.run(...)tf.Session()的变量设置, ConfigProto protocol buffer为会话提供了不同的配置选项。比如,创建一个会话,对设备布局使用软约束条件,以及对分布
# Launch the graph in a session that allows soft device placement and # logs the placement decisions. sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))tf.Session.run
a = tf.constant([10, 20]) b = tf.constant([1.0, 2.0]) # 'fetches' 可以为单个数 v = session.run(a) # v is the numpy array [10, 20] # 'fetches' 可以为一个list. v = session.run([a, b]) # v a Python list with 2 numpy arrays: the numpy array [10, 20] and the # 1-D array [1.0, 2.0] # 'fetches' 可以是 lists, tuples, namedtuple, dicts中的任意: MyData = collections.namedtuple('MyData', ['a', 'b']) v = session.run({'k1': MyData(a, b), 'k2': [b, a]}) # v 为一个dict,并有 # v['k1'] is a MyData namedtuple with 'a' the numpy array [10, 20] and # 'b' the numpy array [1.0, 2.0] # v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array # [10, 20].tf.Session.as_default() 使用关键字with指定会话, 可以在会话中执行Operation.run()或Tensor.eval(),以得到运行的tensor结果
c = tf.constant(..) sess = tf.Session() with sess.as_default(): assert tf.get_default_session() is sess print(c.eval())使用函数tf.get_default_session()来得到当前默认的会话 需要注意的是,退出该as_default上下文管理器时,并没有关闭该会话(session ),必须明确的关闭会话
c = tf.constant(...) sess = tf.Session() with sess.as_default(): print(c.eval()) # ... with sess.as_default(): print(c.eval()) #关闭会话 sess.close() #使用 with tf.Session()方式可以创建并自动关闭会话tf.InteractiveSession
sess = tf.InteractiveSession() a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # 我们直接使用'c.eval()' 而没有通过'sess' print(c.eval()) sess.close()以上的例子,在非交互会话的版本中为,
a = tf.constant(5.0) b = tf.constant(6.0) c = a * b with tf.Session(): # We can also use 'c.eval()' here. print(c.eval())相关链接:
[1] 安装Tensorflow(Linux ubuntu) http://blog.csdn.net/lenbow/article/details/51203526 [2] ubuntu下CUDA编译的GCC降级安装 http://blog.csdn.net/lenbow/article/details/51596706 [3] ubuntu手动安装最新Nvidia显卡驱动 http://blog.csdn.net/lenbow/article/details/51683783 [4] Tensorflow的CUDA升级,以及相关配置 http://blog.csdn.net/lenbow/article/details/52118116 [5] 基于gensim的Doc2Vec简析 http://blog.csdn.net/lenbow/article/details/52120230 [6] TensorFlow的分布式学习框架简介 http://blog.csdn.net/lenbow/article/details/52130565 [7] Tensorflow一些常用基本概念与函数(1) http://blog.csdn.net/lenbow/article/details/52152766 [8] Tensorflow一些常用基本概念与函数(2) http://blog.csdn.net/lenbow/article/details/52181159
