Tensorflow 03_: tensorflow中tensor的生命周期

来源:互联网 发布:乐动力没有数据 编辑:程序博客网 时间:2024/06/05 09:26

前言

在学习上篇博文《Tensorflow 03: 前向神经网络-MIST》代码的过程中,发现一个比较有意思,但同时令人迷惑的一个问题,即tensorflow所定义在计算图中的各种各样的 tensor 的生命周期到底是怎样的???大家都知道:变量的 “作用域” 和 “生命周期” 号称C语言的倚天剑和屠龙刀,威力巨大。但在tensorflow中,tensor的所表现出来的生命周期现象与C语言中的不太一样。

自已产生上述顾虑的一个原因是:在用tensorflow构造自已的深度神经网络模型时,大家都习惯于将 “构图” 和 “图的训练” 分开,即将这两部分分别写到各自的函数中,如build_graph_model()和train_model()。那么这个时候就会产生一个问题,因为构图是在函数build_graph_model()中完成的,那么其中用到的参数变量如W,bias等要不要作为函数的返回值返回呢???如果不返回,因为这些W,bias等tensor 变量是一个局部变量,函数build_graph_model()调用完成后,是不是就死掉了,被程序回收了,那构图岂不是失败???

案例1

比如在上篇博文中有如下一段代码:

# 在 Tensorflow 默认图中创建模型    with tf.Graph().as_default():        images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size)        # 构建前向计算的图模型.        logits = mnist.inference(images_placeholder, FLAGS.hidden1, FLAGS.hidden2)

其中,mnist.inference()是模块文件mnist.py中的一个函数,如下所示:

def inference(images, hidden1_units, hidden2_units):    """    构建网络前向计算图    参数:        images: 输入的 images, placeholder.        hidden1_units: 隐层1神经元大小.        hidden2_units: 隐层2神经元大小.    返回值:        softmax_linear: logits.    """    # 隐层1    # tf.name_scope()函数返回一个context manager    with tf.name_scope('hidden1'):        weights = tf.Variable(tf.truncated_normal([IMAGE_PIXELS, hidden1_units],                                stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))), name='weights')        biases = tf.Variable(tf.zeros([hidden1_units]), name='biases')        hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)    # 隐层2    with tf.name_scope('hidden2'):        weights = tf.Variable(tf.truncated_normal([hidden1_units, hidden2_units],                                stddev=1.0 / math.sqrt(float(hidden1_units))), name='weights')        biases = tf.Variable(tf.zeros([hidden2_units]), name='biases')        hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)    # 输出    with tf.name_scope('softmax_linear'):        weights = tf.Variable(tf.truncated_normal([hidden2_units, NUM_CLASSES],                                stddev=1.0 / math.sqrt(float(hidden2_units))), name='weights')        biases = tf.Variable(tf.zeros([NUM_CLASSES]), name='biases')        logits = tf.matmul(hidden2, weights) + biases    return logits

那么按照C语言中变量的生命周期原理,当程序执行完logits = mnist.inference(images_placeholder, FLAGS.hidden1, FLAGS.hidden2)这行代码后,函数inference中包含的局部变量如weights,biases,hidden1,hidden2等等都应该消失死掉了,但从后面程序的执行结果来看,可以看到tensorflow还是能够构造出完整的前向神经网络模型(该模型必须包含weights,biases,hidden1,hidden2等这些局部变量),说明这些局部变量可能没有“死掉”。那么可能的原因解释是:
(1)这些局部变量weights,biases,hidden1,hidden2等没有死掉,他们始终作为tensorflow计算图的的一部分存在。
(2)返回值logits可能包含这些局部变量的所有信息。虽然函数inference()执行完了,但这些局部变量的信息被存储在了返回值logits中,从而带出来了。

期待看到这篇博客的大神更好的解释!!!!

原创粉丝点击