
TensorFlow 框架
抱紧谷歌大腿
JNingWei
工作后比较忙,不怎么看账号和消息。回复不及时望见谅。
展开
-
【tensorflow】安装tensorflow
安装TensorFlow有Cuda检查可安装的tensorflow-gpu版本范围:安装:pip install tensorflow-gpu无Cuda检查可安装的tensorflow版本范围:安装:pip install tensorflow原创 2020-03-15 15:26:03 · 238 阅读 · 0 评论 -
【tensorflow】设置显存开销
问题一般大家在跑tf时,单个程序往往会占满整块GPU的所有显存。但是实际上,程序很可能并不需要那么大的显存空间。改进方案通过 tf.ConfigProto().gpu_options.allow_growth=True来告诉程序,只需占用实际所需的显存即可:# ---------------- session ----------------config = tf.ConfigProt...原创 2020-03-13 20:33:27 · 418 阅读 · 0 评论 -
【tensorflow】tensor相关
tensor是如何命名的Tensorflow中,tensor的名字是op的名字加上 :0 (如果有多个,这又会有 :1 、:2 。。。。)打印所有nodefor n in tf.get_default_graph().as_graph_def().node: print(n.name)...原创 2020-04-24 13:45:36 · 234 阅读 · 0 评论 -
【tensorflow】模型加载
net.load vs saver.restore.load() 只能加载.npy文件,.restore() 只能加载 ckpt(checkpoint)文件。net.load 会增加assign节点一定会改图,所以要在 sess.graph.finalize() 之前。 saver.restore 不会改图,可以放到 sess.graph.finalize() 之后。Assign节点只会在...原创 2020-04-24 13:44:40 · 480 阅读 · 0 评论 -
【tensorflow】直接读取图片
Tensorflow通过tf.gfile.FastGFile(filename,’rb’).read()读取的图像,是图像的原始数据,还需要经过解码,才能获取图像的数据,数据的格式为RGB(三通道图像),这一点是与Opencv不同。Tensorflow提供了对jpeg和png格式图片的解码函数,例如“decode_jpeg”对jpeg格式的图片进行解码,使用encode_jpeg编码,将图像...原创 2020-04-24 13:43:12 · 477 阅读 · 0 评论 -
【tensorflow】node相关
tf中的op就是node。所有tensorflow操作,都会生成tf node。打印所有node:for n in tf.get_default_graph().as_graph_def().node: print(n.name)train时的graph往往会比test时的graph多很多node(例如:tf预处理操作,甚至包括读tfrecord)。但是output node在tra...原创 2020-04-24 11:28:19 · 577 阅读 · 0 评论 -
【tensorflow】显示pb文件的graph
import tensorflow as tffrom tensorflow.python.platform import gfilepb_file = ‘xxx/xxx/xxx.pb’pb_log_dir = ‘xxx/xxx/log/’def show_pb_graph(): graph = tf.get_default_graph() graph_def = ...原创 2020-04-06 09:26:30 · 545 阅读 · 0 评论 -
【tensorflow】shape与ndim
获取shapeimport tensorflow as tftensor = tf.placeholder(dtype=tf.float32, shape=[200, 200, 3])print('\n=========== get shape ============')print('tensor : ', tensor)print('tenso...原创 2020-03-28 10:42:11 · 1642 阅读 · 0 评论 -
【tensorflow】tf.name_scope与tf.variable_scope区别
共同点作用域函数 包括:命名域:tf.name_scope()变量域:tf.variable_scope()对于使用 tf.Variable()方式创建的变量,具有相同的效果。即:都会在变量名称前面,加上域名称。都可用于:变量共享tensorboard画流程图进行可视化封装变量但是,tf.Variable() 每次都会新建变量。如果希望重用(共享)一些变量,必须用到...原创 2020-03-22 15:14:37 · 385 阅读 · 0 评论 -
【tensorflow】feed操作
feed array1:预设placeholder:self.xxx = tf.placeholder(dtype=tf.float32, shape=[xxx, xxx], name='xxx')2:喂numpy.ndarray格式的矩阵进去:xxx = sess.run(self.xxx, feed_dict={self.xxx: xx_array})feed variable...原创 2020-03-22 14:57:14 · 339 阅读 · 0 评论 -
【tensorflow】按名查看变量
法一specified_var_lst = [v for v in tf.global_variables() if "conv_1/" in v.name]print(specified_var_lst)法二for i, v in enumerate(specified_var_lst): print('\n {}/{} {} : \n {} '.format(i, len(sp...原创 2020-03-15 16:02:16 · 323 阅读 · 0 评论 -
【tensorflow】tensorflow如何避免内存泄漏
注意事项注意graph一定要用 sess.graph.finalize(),相当于把整个图冻住,使图变为只读的形式,不再允许增加节点。注意循环循环中禁用如下可能会改变图节点的函数: tf.convert_to_tensor() tf.reshape() tf.cast() tf.zeros_like() tf.ones_like() tf.assign() tf.train....原创 2020-03-15 15:54:06 · 1851 阅读 · 0 评论 -
【tensorflow】TFRecord读写机理
tfrecord,写的时候是一行一行地写的,读的时候是每batch个行地读的。写的时候,通过for循环(例如:img_path, cls_label = img_paths[i], cls_labels[I])往tf.train.Example里面喂feature。因此,tf.train.Example中的feature喂入的是单行的数据(包括一个img_path、及其对应的encod...原创 2020-03-15 15:48:08 · 287 阅读 · 0 评论 -
tensorflow: 畅玩tensorboard图表(SCALARS)
前言这篇博客建立在你已经会使用tensorboard的基础上。如果你还不会记录数据并使用tensorboard,请移步我之前的另一篇博客:tensorflow: tensorboard 探究关于模型文件夹每启动一轮新的训练时,存放生成模型的文件夹就会生成一个独立的子文件夹。 每当开始初始训练时,该子文件夹下会生成一个events文件用于记录开始: 停掉训练时,该子文件夹下会原创 2018-01-07 11:19:32 · 2425 阅读 · 0 评论 -
tensorflow: variable的值 与 variable.read_value()的值 区别
问题查看 tensorflow api manual 时,看到关于 variable.read_value() 的注解如图:那么在 tensorflow 中,variable的值 与 variable.read_value()的值 到底有何区别?实验代码# coding=utf-8import tensorflow as tf# Create a variable.w = tf.Variable原创 2017-10-05 21:02:43 · 5318 阅读 · 1 评论 -
tensorflow: 为什么 tensor型参数 可以接受 非tensor型输入
问题在日常用 tensorflow 进行编程的时候,我经常会纳罕一个问题: 明明 manual里面 白纸黑字地注明了 某个参数项 的 输入 必须是 tensor型,可是 非tensor型 的数据 输入后 却不会报错,依然能正常算出结果。示例比如,合法的输入应该如下:import tensorflow as tfa = tf.constant(10)b = tf.constant(20)c原创 2017-10-05 10:57:23 · 1157 阅读 · 0 评论 -
tensorflow: (data_format) NHWC、NCHW 区别与转换
区别NHWC [batch, in_height, in_width, in_channels]NCHW [batch, in_channels, in_height, in_width]转换NHWC –> NCHW:import tensorflow as tfx = tf.reshape(tf.range(24), [1, 3, 4, 2])out = tf.transpose(x原创 2017-10-03 20:01:44 · 16481 阅读 · 0 评论 -
tensorflow编程: Variables
Variablestf.VariableProperties: Properties Usage Return Example for Print w.device 返回 该variable 所在的 device 非Tensor – w.dtype 返回 该variable 的 DType 非Tensor <dtype: 'float32_ref'>原创 2017-10-05 11:21:05 · 1236 阅读 · 0 评论 -
tensorflow编程: Tensor Handle Operations
Tensor Handle Operationstf.get_session_handle返回句柄data 。import tensorflow as tfa = tf.constant(10)b = tf.constant(20)c = tf.multiply(a, b)h = tf.get_session_handle(c)sess = tf.InteractiveSession()_原创 2017-10-05 10:09:22 · 1733 阅读 · 0 评论 -
tensorflow编程: Wraps python functions
Script Language Operatorstf.py_func将任意的 python/numpy functions 包装成 TensorFlow op。 tf.py_func (func, inp, Tout, stateful=True, name=None)其中, inp参数项 必须是 list型 ,哪怕 list 里只有一个元素;而 inp参数项 在只有一个元素时可以 不必为原创 2017-10-05 09:03:53 · 1838 阅读 · 2 评论 -
tensorflow编程: Neural Network
Activation Functionstf.nn.relu# coding=utf-8import tensorflow as tf# 保证每次都是 同一组inputsimport numpy as npi_p = np.random.uniform(-10, 10, size=[3, 3])inputs = tf.placeholder_with_default(input=i_p, s原创 2017-10-03 15:01:21 · 1863 阅读 · 0 评论 -
tensorflow编程: Math
Arithmetic Operatorstf.add、tf.subtract、tf.multiply、tf.scalar_mul、tf.div、tf.divide、tf.truediv、tf.floordiv、tf.realdiv、tf.truncatediv、tf.floor_div、tf.truncatemod、tf.floormod、tf.mod、tf.cross# coding=utf-8i原创 2017-10-02 20:39:40 · 3621 阅读 · 1 评论 -
tensorflow编程: Inputs and Readers
Placeholderstf.placeholder插入一个坐等 被feed_dict 的 tensor占位符 。 tf.placeholder (dtype, shape=None, name=None)import tensorflow as tfx = tf.placeholder(dtype=tf.int32)print xwith tf.Session() as sess:原创 2017-10-01 21:14:14 · 1669 阅读 · 2 评论 -
tensorflow编程: Higher Order Functions
Higher Order Operatorstf.scan从 elems列表 中 依次 扫描读取 元素 放入 公式进行 迭代计算。 tf.scan (fn, elems, initializer=None, parallel_iterations=10, back_prop=True, swap_memory=False, infer_shape=True, name=None)import原创 2017-10-01 20:52:20 · 716 阅读 · 0 评论 -
tensorflow编程: Building Graphs
Core graph data structurestf.Graph# 程序从一开始就默认有一个 graph 。任何的 tf.Graph() 操作 都是在新建 graph,但都只能在新建的那个 上下文管理器 内发挥作用a = tf.get_default_graph()# 该graph 继承 a.graph,则 该graph 内 default_graph 就为 a.graphwith a.as原创 2017-09-30 17:24:28 · 812 阅读 · 0 评论 -
tensorflow: 打印内存中的变量
法一:循环打印模板for (x, y) in zip(tf.global_variables(), sess.run(tf.global_variables())): print '\n', x, y实例# coding=utf-8import tensorflow as tfdef func(in_put, layer_name, is_training=True): with原创 2017-09-29 10:16:37 · 2389 阅读 · 1 评论 -
tensorflow编程: Control Flow
Control Flow Operationstf.identity复制tensor tf.identity (input, name=None)import tensorflow as tft = tf.constant(value=[[1, 1, 1], [2, 2, 2]], dtype=tf.int32)a1 = ta2 = tf.identity(t)print a1asser原创 2017-09-30 15:39:03 · 2158 阅读 · 0 评论 -
tensorflow: bn层
Introduction具体见 深度学习: Batch Normalization (归一化)Experiment实验一可视化 batch normalization 过程中的 tensor演化(以输入一张[1, 4 , 4, 1]的图片为例)# -*- coding: utf-8 -*-import tensorflow as tfdef func_convolut原创 2017-09-29 10:46:45 · 2602 阅读 · 0 评论 -
tensorflow编程: Constants, Sequences, and Random Values
Constant Value Tensorstf.zeros tf.zeros (shape, dtype=tf.float32, name=None)tf.zeros_like tf.zeros_like (tensor, dtype=None, name=None, optimize=True)tf.ones tf.ones (shape, dtype=tf.float32, n原创 2017-09-25 17:55:05 · 681 阅读 · 0 评论 -
tensorflow编程: Layers (contrib)
Layers (contrib)Higher level ops for building neural network layerstf.contrib.layers.batch_norm 添加一个 Batch Normalization 层 。tf.contrib.layers.batch_norm ( inputs, ##### decay=0.999, ####原创 2017-09-26 08:55:44 · 3994 阅读 · 0 评论 -
tensorflow: 对variable_scope进行reuse的两种方法
方法一:模板:具体示例:方法二:模板:# -*- coding: utf-8 -*-import tensorflow as tffrom tensorflow.python.ops import variable_scope as vs ### 改动部分 ###def func(..., reuse=False): ### 改动部分 ### if reuse:原创 2017-09-28 15:56:16 · 35052 阅读 · 6 评论 -
tensorflow: bn层 的 decay参数项
实验五:探究 batch normalization 过程中的 decay 参数项 在 train 和 test 过程中的不同作用。 在 decay=0 (即移动偏移无损失)时:import tensorflow as tfdef func(in_put, layer_name, is_training=True): with tf.variable_scope(layer_name,原创 2017-09-29 09:38:34 · 5439 阅读 · 0 评论 -
tensorflow: Shapes and Shaping 探究
定义 Tensor Transformations - Shapes and Shaping: TensorFlow provides several operations that you can use to determine the shape of a tensor and change the shape of a tensor. tensorflow提供了一些操作,让用户可以定义原创 2017-09-23 16:36:30 · 445 阅读 · 0 评论 -
tensorflow: 损失函数(Losses Functions) 探究
API官方定义From tf.nn.l2_loss: tf.nn.l2_loss l2_loss( t, name=None ) Defined in tensorflow/python/ops/gen_nn_ops.py. See the guide: Neural Network > Losses L2 Loss. Computes half the原创 2017-09-04 17:03:05 · 7257 阅读 · 0 评论 -
tensorflow: 激活函数(Activation_Functions) 探究
激活函数概念From TensorFlow - Activation_Functions:在神经网络中,我们有很多的 非线性函数 来作为 激活函数连续 、平滑 tf.sigmoid(x, name = None) == 1 / (1 + exp(-x))import numpy as npimport tensorflow as tfsess = tf.Session()bn原创 2017-09-05 20:19:29 · 4430 阅读 · 0 评论 -
tensorflow: eval()探究
总结 对简单应用的情形(如单元测试),用以下格式可以得到更简洁的代码:with tf.Session(): c.eval() 如果你的代码要处理多个graph和 session,更直白的方式可能是显式调用Session.run():sess = tf.Session()sess.run(c)实验代码>>> c = tf.constant(5.0)>>> with tf.Sessio原创 2017-08-25 14:20:53 · 2333 阅读 · 0 评论 -
tensorflow: 类numpy的api映射表
Annotations tensorflow api numpy api 建立全零张量 tf.zeros(shape=(H, W), dtype=tf.float32) np.zeros(shape=(H, W), dtype=np.float32) 建立全一张量 tf.ones(shape=(H, W), dtype=tf.float32) np.ones(shape原创 2017-08-26 11:30:34 · 1431 阅读 · 1 评论 -
TensorFlow大本营
Source code & ModelsTensorFlow组织的GithubTensorFlow源码源码永远是学习的重中之重。TensorFlow Model Zoo - 预训练模型该存储库包含在TensorFlow中实现的机器学习模型。因为这些型号仅仅与TensorFlow 1.0或更高版本兼容。如果TensorFlow 是0.x版本,需要upgdade tensorfl原创 2017-06-21 16:38:34 · 464 阅读 · 0 评论 -
Tensorflow版本变动细节 & 自动移植代码到新版本下
TensorFlow 1.0 版本 API 变动汇总自动将代码移植到 1.0 版本:tf_upgrade.py --infile foo.py --outfile foo-upgraded.py原创 2017-06-21 16:46:17 · 622 阅读 · 0 评论 -
自动代码移植:从Caffe到Tensorflow
参考这个开源项目:Convert Caffe models to TensorFlow安装好所需依赖后,具体执行起来只需要一句命令行:convert.py原创 2017-06-21 16:47:08 · 843 阅读 · 0 评论