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中,从而带出来了。
期待看到这篇博客的大神更好的解释!!!!
- Tensorflow 03_: tensorflow中tensor的生命周期
- tensorflow中tensor的理解
- 打印tensorflow中tensor的元素
- 对Tensorflow中tensor的理解
- Tensorflow中Tensor类型解析
- Tensorflow中关于Tensor和Variable的理解
- 机器学习0004 Tensorflow中产生tensor的函数
- 如何在tensorflow中判断tensor(张量)的值
- tensorflow 中tensor与数组之间的转换
- Tensorflow中遇到OOM when allocating tensor
- Tensorflow: 从checkpoint文件中读取tensor
- tensorflow的张量(tensor)的理解
- tensorflow 打印出 tensor 的值
- 学习TensorFlow,打印输出tensor的值
- Tensorflow lesson 2---唯一的结构tensor
- TensorFlow 返回tensor的 维数
- Tensorflow 学习笔记- tensor的声明
- 3、TensorFlow 的数据模型-----张量(Tensor)
- WRK+fabric进行集群部署和压测
- 【LeetCode】7. Reverse Integer
- Eclipse 高亮显示选中的相同变量
- 鹤鸣计算器3.26,一款安卓自定义函数公式的安卓计算器
- Excel常用函数命令
- Tensorflow 03_: tensorflow中tensor的生命周期
- 关于进程有多个aapt.exe 的解决方法
- Cordova for android hello app
- Java强制类型转换原理分析
- 安装IIS及与SQL2012展示ASP.Net网站
- 二、1、defer 和 async区别
- java反射机制
- Linux Kernel 学习笔记11:时间、延时
- Python2与3共存后的pip问题