学习笔记TF007:Tensor、Graph、Op、Variable、占位符、Session、名称作用域、Board综合例子...

输入采用占位符,模型接收任意长度向量,随时间计算数据流图所有输出总和,采用名称作用域合理划分数据流图,每次运行保存数据流图输出、累加、均值到磁盘。

[None]代表任意长度向量,[]代表标量。update环节更新各Variable对象以及将数据传入TensorBoard汇总Op。与交换工作流分开,独立名称作用域包含Variable对象,存储输出累加和,记录数据流图运行次数。独立名称作用域包含TensorBoard汇总数据,tf.scalar_summary Op。汇总数据在Variable对象更新完成后才添加。

构建数据流图。 导入TensorFlow库。Graph类构造方法tf.Graph(),显式创建Graph对象。两个“全局”Variable对象,追踪模型运行次数,追踪模型所有输出累加和。与其他节点区分开,放入独立名称作用域。trainable=False设置明确指定Variable对象只能手工设置。 模型核心的变换计算,封装到名称作用域"transformation",又划分三个子名称作用域"input"、"intermediate_layer"、"output"。.multiply、.add只能接收标量参数,.reduce_prod、. reduce_sum可以接收向量参数。 在"update"名称作用域内更新Variable对象。.assign_add实现Variable对象递增。 在"summaries"名称作用域内汇总数据供TensorBoard用。.cast()做数据类型转换。.summary.scalar()做标量数据汇总。 在"global_ops"名称作用域创建全局Operation(Op)。初始化所有Variable对象。合并所有汇总数据。

运行数据流图。 .Session()启动Session对象,graph属性加载Graph对象,.summary.FileWriter()启动FileWriter对象,保存汇总数据。 初始化Variable对象。 创建运行数据流图辅助函数,传入向量,运行数据流图,保存汇总数据。创建feed_dict参数字典,以input_tensor替换a句柄的tf.placeholder节点值。使用feed_dict运行output不关心存储,运行increment_step保存到step,运行merged_summaries Op保存到summary。添加汇总数据到FileWriter对象,global_step参数随时间图示折线图横轴。 变换向量长度多次调用运行数据流图辅助函数。.flush()把汇总数据写入磁盘。

查看数据流图。 Graph标签,变换运算流入update方框,为summaries、variables提供输入,global_ops包含变换计算非关键运算。输入层、中间层、输出层分离。 Scalars标签,summary.scalar对象标签查看不同时间点汇总数据变化。

import tensorflow as tf#导入TensorFlow库
#构建数据流图
graph = tf.Graph()#显式创建Graph对象
with graph.as_default():#设为默认Graph对象
with tf.name_scope("variables"):#创建Variable对象名称作用域
    global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")#记录数据流图运行次数的Variable对象,初值为0,数据类型为32位整型,不可自动修改,以global_step标识
    total_output = tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output")#追踪模型所有输出累加和的Variable对象,初值为0.0,数据类型为32位浮点型,不可自动修改,以total_output标识
with tf.name_scope("transformation"):#创建变换计算Op名称作用域
    with tf.name_scope("input"):#创建独立输入层名称作用域
        a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")#创建占位符,接收一个32位浮点型任意长度的向量作为输入,以input_placeholder_a标识
    with tf.name_scope("intermediate_layer"):#创建独立中间层名称作用域
        b = tf.reduce_prod(a, name="product_b")#创建创建归约乘积Op,接收张量输入,输出张量所有分量(元素)的乘积,以product_b标识
        c = tf.reduce_sum(a, name="sum_c")#创建创建归约求和Op,接收张量输入,输出张量所有分量(元素)的求和,以sum_c标识
    with tf.name_scope("output"):#创建独立输出层名称作用域
        output = tf.add(b, c, name="output")#创建创建求和Op,接收两个标量输入,输出标量求和,以output标识
with tf.name_scope("update"):
    update_total = total_output.assign_add(output)#用最新的输出更新Variable对象total_output
    increment_step = global_step.assign_add(1)#增1更新Variable对象global_step,记录数据流图运行次数
with tf.name_scope("summaries"):#创建数据汇总Op名称作用域
    avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")#计算平均值,输出累加和除以数据流图运行次数,把运行次数数据类型转换为32位浮点型,以average标识
    tf.summary.scalar(b'output_summary',output)#创建输出节点标量数据统计汇总,以output_summary标识
    tf.summary.scalar(b'total_summary',update_total)#创建输出累加求和标量数据统计汇总,以total_summary标识
    tf.summary.scalar(b'average_summary',avg)#创建平均值标量数据统计汇总,以average_summary标识
with tf.name_scope("global_ops"):#创建全局Operation(Op)名称作用域
    init = tf.global_variables_initializer()#创建初始化所有Variable对象的Op
    merged_summaries = tf.summary.merge_all()#创建合并所有汇总数据的Op
#运行数据流图
sess = tf.Session(graph=graph)#用显式创建Graph对象启动Session会话对象
writer = tf.summary.FileWriter('./improved_graph', graph)#启动FileWriter对象,保存汇总数据
sess.run(init)#运行Variable对象初始化Op
def run_graph(input_tensor):#定义数据注图运行辅助函数
    """
    辅助函数:用给定的输入张量运行数据流图,
    并保存汇总数据
    """
    feed_dict = {a: input_tensor}#创建feed_dict参数字典,以input_tensor替换a句柄的tf.placeholder节点值
    _, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict)#使用feed_dict运行output不关心存储,运行increment_step保存到step,运行merged_summaries Op保存到summary
    writer.add_summary(summary, global_step=step)#添加汇总数据到FileWriter对象,global_step参数时间图示折线图横轴
#用不同的输入用例运行数据流图
run_graph([2,8])
run_graph([3,1,3,3])
run_graph([8])
run_graph([1,2,3])
run_graph([11,4])
run_graph([4,1])
run_graph([7,3,1])
run_graph([6,3])
run_graph([0,2])
run_graph([4,5,6])
writer.flush()#将汇总数据写入磁盘
writer.close()#关闭FileWriter对象,释放资源
sess.close()#关闭Session对象,释放资源

WX20170513-142835@2x.pnggraph?run=-8.pnggraph?run=-9.png

参考资料: 《面向机器智能的TensorFlow实践》

欢迎加我微信交流:qingxingfengzi

我的微信公众号:qingxingfengzigz

我老婆张幸清的微信公众号:qingqingfeifangz

转载于:https://my.oschina.net/u/3482787/blog/898990

WARNING:tensorflow:From /root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version. Instructions for updating: non-resource variables are not supported in the long term 2025-07-26 19:47:00.316548: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1 2025-07-26 19:47:00.379323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: pciBusID: 0000:39:00.0 name: NVIDIA GeForce RTX 4090 computeCapability: 8.9 coreClock: 2.52GHz coreCount: 128 deviceMemorySize: 23.55GiB deviceMemoryBandwidth: 938.86GiB/s 2025-07-26 19:47:00.379583: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.379632: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcublas.so.10'; dlerror: libcublas.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.380958: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10 2025-07-26 19:47:00.381316: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10 2025-07-26 19:47:00.381386: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcusolver.so.10'; dlerror: libcusolver.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.381440: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcusparse.so.10'; dlerror: libcusparse.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.381492: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcudnn.so.7'; dlerror: libcudnn.so.7: cannot open shared object file: No such file or directory 2025-07-26 19:47:00.381501: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2025-07-26 19:47:00.381919: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA 2025-07-26 19:47:00.396214: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2000000000 Hz 2025-07-26 19:47:00.405365: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f45c0000b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2025-07-26 19:47:00.405415: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2025-07-26 19:47:00.409166: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix: 2025-07-26 19:47:00.409199: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] WARNING:tensorflow:From /root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py:1089: Layer.apply (from tensorflow.python.keras.engine.base_layer_v1) is deprecated and will be removed in a future version. Instructions for updating: Please use `layer.__call__` method instead. loaded ./checkpoint/decom_net_train/model.ckpt loaded ./checkpoint/illumination_adjust_net_train/model.ckpt No restoration pre model! (480, 640, 3) (680, 720, 3) (415, 370, 3) Start evalating! 0 Traceback (most recent call last): File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1365, in _do_call return fn(*args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1350, in _run_fn target_list, run_metadata) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1443, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Restoration_net/de_conv6_1/biases [[{{node Restoration_net/de_conv6_1/biases/read}}]] During handling of the above exception, another exception occurred: Traceback (most recent call last): File "evaluate.py", line 92, in <module> restoration_r = sess.run(output_r, feed_dict={input_low_r: decom_r_low, input_low_i: decom_i_low}) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 958, in run run_metadata_ptr) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1181, in _run feed_dict_tensor, options, run_metadata) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1359, in _do_run run_metadata) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1384, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Restoration_net/de_conv6_1/biases [[node Restoration_net/de_conv6_1/biases/read (defined at /root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/variables.py:256) ]] Original stack trace for 'Restoration_net/de_conv6_1/biases/read': File "evaluate.py", line 28, in <module> output_r = Restoration_net(input_low_r, input_low_i) File "/root/Python/KinD-master/KinD-master/model.py", line 70, in Restoration_net conv6=slim.conv2d(up6, 256,[3,3], rate=1, activation_fn=lrelu,scope='de_conv6_1') File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1191, in convolution2d conv_dims=2) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1089, in convolution outputs = layer.apply(inputs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func return func(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 1695, in apply return self.__call__(inputs, *args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 547, in __call__ outputs = super(Layer, self).__call__(inputs, *args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 758, in __call__ self._maybe_build(inputs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 2131, in _maybe_build self.build(input_shapes) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 172, in build dtype=self.dtype) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/layers/base.py", line 460, in add_weight **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer_v1.py", line 447, in add_weight caching_device=caching_device) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py", line 743, in _add_variable_with_custom_getter **kwargs_for_getter) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1573, in get_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1316, in get_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 551, in get_variable return custom_getter(**custom_getter_kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1793, in layer_variable_getter return _model_variable_getter(getter, *args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py", line 1784, in _model_variable_getter aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/variables.py", line 328, in model_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/arg_scope.py", line 184, in func_with_args return func(*args, **current_args) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/ops/variables.py", line 256, in variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 520, in _true_getter aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 939, in _get_single_variable aggregation=aggregation) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 259, in __call__ return cls._variable_v1_call(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 220, in _variable_v1_call shape=shape) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 198, in <lambda> previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2614, in default_variable_creator shape=shape) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 263, in __call__ return super(VariableMetaclass, cls).__call__(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1666, in __init__ shape=shape) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1854, in _init_from_args self._snapshot = array_ops.identity(self._variable, name="read") File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper return target(*args, **kwargs) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 282, in identity ret = gen_array_ops.identity(input, name=name) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3901, in identity "Identity", input=input, name=name) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 744, in _apply_op_helper attrs=attr_protos, op_def=op_def) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3327, in _create_op_internal op_def=op_def) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1791, in __init__ self._traceback = tf_stack.extract_stack()
最新发布
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值