![](https://img-blog.csdnimg.cn/20201218163121144.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
TensorFlow
文章平均质量分 65
tensorflow
Enzo 想砸电脑
这个作者很懒,什么都没留下…
展开
-
2.3.1 TensorFlow 常量 tf.constant()
TensorFlow 常量 tf.constant()常量一旦初始化后,就不能修改其值, 通过如下函数定义:tf.constant( value, dtype=None, shape=None, name='Const', verify_shape=False )原创 2020-12-29 11:08:13 · 177 阅读 · 0 评论 -
2.3.2-1 TensorFlow 变量tf.Variable()
TensorFlow 变量1. tf.Variable()tf.Variable() 定义多个相同名称的变量情况2. tf.get_variable()使用 tf.get_varibale()==创建新变量==tf.get_variable() 创建或获取指定名称的变量通过设置 reuse=True 的方式来返回已存在的变量通过创建tf.Variable_scope 对象的方式, 来返回已存在的变量可以随时修改变量的值。变量的创建方式有两种: tf.Variable() 和 tf.get_variab原创 2020-12-25 07:49:35 · 264 阅读 · 1 评论 -
2.3.2-2 TensorFlow tf.get_variable()
tf.get_variable1. 使用 tf.get_varibale()创建新变量2. tf.get_variable() 创建或获取指定名称的变量2. 1通过设置 reuse=True 的方式来返回已存在的变量2.2 通过创建tf.Variable_scope 设置reuseget_variable( name, shape=None, dtype=None, initializer=None,原创 2020-12-29 10:06:09 · 746 阅读 · 0 评论 -
2.3.2-3 TensorFlow 变量初始化方法
变量初始化方法tf.Variable() 是通过直接传入初始值的方式来指定初始化的值,而 tf.get_variable() 需要指定初始化器来实现初始化值。常用的初始化方法:tf.constant_initializer # 常量初始化器tf.ones_initializer # 1值初始化器tf.zeros_initializer # 0值初始化器tf.random_normal_initializer # 随机正太分布初始化器tf.random_uniform_initial转载 2020-12-29 10:01:19 · 335 阅读 · 0 评论 -
2.3.2-4 TensorFlow 变量值修改
变量值修改 .assign()使用变量的 .assign()函数为变量赋值# --*== UTF-8 --*--import tensorflow as tf def test_1(): a = tf.Variable([10, 20]) b = tf.assign(a, [20, 30]) c = a + [10, 20] with tf.Session() as sess: sess.run(tf.global_variables_initi转载 2020-12-29 09:59:21 · 138 阅读 · 0 评论 -
2.3.3 TensorFlow 占位符 tf.placeholder
tf.placeholder( dtype, # 占位符数据类型 shape=None, # 占位符的shape name=None # 占位符的名称)tf.placeholder() 既可以看成常量又可以看成变量,是因为在将数据填入到占位符后,其值在图中就不能再做修改(即不能通过asign等函数修改值),但是在图之外又可以不断传入不同的值,比如在训练的时候,可以向图中不断传入不同的图片数据。import tensorflow as .原创 2020-12-25 11:35:55 · 533 阅读 · 0 评论 -
2.4.2/2.4.3 TensorFlow python对象与Tensor对象互相转换 tf.convert_to_tensor() , tf.py_func()
python对象 转 Tensor对象函数原型: (转换后,以常量形式存储)tf.convert_to_tensor( value, # 需要转为Tensor的数据 dtype=None, # 指定Tensor对象的数据类型,如果没有指定,则根据value的值来推断 name=None, # 转成Tensor后的名称 preferred_dtype=None # 备选数据类型,如果dtype为None, 该参数才生效 )以 string原创 2020-12-25 16:54:07 · 605 阅读 · 0 评论 -
2.4.4 TensorFlow 稀疏矩阵(sparse matrix ) 和 稠密矩阵矩阵(dense matrix )
稀疏矩阵(sparse matrix ) 和 稠密矩阵矩阵(dense matrix )在矩阵中,若数值为0的元素数目远远多于非0元素的数目时,则称该矩阵为稀疏矩阵。与之相反,若非0元素数目占大多数时,则称该矩阵为稠密矩阵。...原创 2020-12-16 20:25:09 · 6406 阅读 · 1 评论 -
2.4.5 TensorFlow 强制转换Tensor对象数据类型 tf.cast()
函数原型:cast(x, dtype, name=None)参数:x: 待转换的数据(张量)dtype: 目标数据类型name: 可选参数,定义操作的名称例子:import tensorflow as tfdata_tf = tf.constant([[0, 0], [1, 1]], dtype=tf.int64)int32_tf = tf.cast(data_tf, dtype=tf.int32)int64_tf = tf.cast(int32_tf, dtype=tf.原创 2020-12-25 22:15:38 · 628 阅读 · 0 评论 -
2.5 TensorFlow 获取图中所有operation对象 tf.get_operatios_by_name()
《TensorFlow 与卷积神经网络 (从算法到入门)》学习笔记通过函数 tf.get_operatios_by_name() 获取图中所有operation对象import tensorflow as tfA_tf = tf.Variable([[1, 2, 3], [1, 2, 3]], dtype=tf.float32)A_tf.assign(A_tf+1)graph = tf.get_default_graph()ops = graph.get_operations()prin原创 2020-12-28 22:01:28 · 750 阅读 · 0 评论 -
6.1.1 TensorFlow 构建队列 tf.FIFOQueue, tf.PriorityQueue, tf.RandomShuffleQueue
1. tf.FIFOQueue 先来先服务队列import tensorflow as tfimport numpy as npdef init_data(): data_list = [] for i in range(10): float_data = [i, i, i] str_data = ['str_'+str(i), 'str'+str(i)] data_list.append((float_data, str_data))原创 2020-12-27 19:15:51 · 289 阅读 · 0 评论 -
6.1.2 TensorFlow Queue, QueueRunner 及 Coordinator
《TensorFlow 与卷积神经网络 (从算法到入门)》学习笔记上一节学习了队列的使用,但是入队和出队操作都是在同一个线程中实现的,实际上使用的队列更多的时候是为了将队列做为缓存机制,使用两个线程分别做入队和出队操作。1. 使用QueueRunner 实现异步入队出队举例:import tensorflow as tfqueue = tf.FIFOQueue(capacity=10, dtypes=tf.float32)# 创建一个计数器(变量),其生成的值作为要传递的数据counter原创 2020-12-31 10:11:01 · 144 阅读 · 0 评论 -
6.1.2-1 threading.Thread()
函数原型:threading.Thread(target, name, args, kwargs)参数:target: 执行的目标任务名(调用的 函数/方法 名称)args: 以元组的方式给执行任务传参,例如: threading.Thread(target=sing, args=(10, 100, 100)), 注意:如果参数就只有一个,记得args参数括号里加逗号,否则就不是元组了,会报错。正确写法是: threading.Thread(target=sing, args=(‘singer’原创 2020-12-31 09:21:00 · 757 阅读 · 0 评论 -
6.2.1-1 详解TensorFlow数据读取机制(附代码) tf.train.string_input_producer, tf.train.start_queue_runners
在学习TensorFlow的过程中,有很多小伙伴反映读取数据这一块很难理解。确实这一块官方的教程比较简略,网上也找不到什么合适的学习材料。今天这篇文章就以图片的形式,用最简单的语言,为大家详细解释一下TensorFlow的数据读取机制,文章的最后还会给出实战代码以供参考。TensorFlow读取机制图解首先需要思考的一个问题是,什么是数据读取?以图像数据为例,读取数据的过程可以用下图来表示:假设我们的硬盘中有一个图片数据集0001.jpg,0002.jpg,0003.jpg……我们只需要把它们读取到转载 2021-01-01 23:22:16 · 218 阅读 · 0 评论 -
6.2.1 TensorFlow 读取文本文件
《TensorFlow 与卷积神经网络 (从算法到入门)》学习笔记TensorFlow中的文件存取方法,主要针对:文本类型文件字节类型文件图片类型文件本文将介绍文本类型文件的存取。函数 tf.read_file 用于读取文件,并返回字符串类型的Tensor对象。此外,TensorFlow 还提供了对象tf.TextLineReader 用于按行读取文本。tf.read_fileimport tensorflow as tftxt_data_tf = tf.read_file('/原创 2021-01-01 15:36:46 · 1048 阅读 · 1 评论 -
6.2.2 TensorFlow 读取定长字节文件
《TensorFlow 与卷积神经网络 (从算法到入门)》学习笔记TensorFlow中的文件存取方法,主要针对:文本类型文件字节类型文件图片类型文件读取器对象有:tf.WholeFileReader()tf.TextLineReader()tf.FixedLengthRecordReader()本文将介绍定长字节文件的读写。写入二进制文件我们先来写入一个二进制文件,用来了解二进制文件的存储格式。要存储的数据集如下:代码:import structimport cv原创 2021-01-02 10:56:26 · 171 阅读 · 0 评论 -
6.2.3 TensorFlow 读取图片
《TensorFlow 与卷积神经网络 (从算法到入门)》学习笔记import tensorflow as tfimport numpy as npfile_1 = '/Users/manmi/Documents/GitHub/tensorflow/image_1.jpeg'file_2 = '/Users/manmi/Documents/GitHub/tensorflow/image_2.jpeg'files = [file_1, file_2]imgs_list_tf = tf.con原创 2021-01-02 17:35:34 · 95 阅读 · 0 评论 -
MobileNet V1(depth_wise 和 point_wise),MobileNet V2(shortcut)
depth_wise : tf.nn.depthwise_conv2d()point_wise: 正常卷积操作, kernel_size=[1, 1, input_channels, out_channels]import tensorflow as tfimport numpy as npa = [1, 2, 3]b = [4, 5, 6]# input_shape=[N, C ,H, W]input = [[[[2, 3, -1, 3], [1, 4,原创 2020-12-26 13:38:55 · 616 阅读 · 3 评论 -
TensorFlow 中的图
import tensorflow as tfA = [[1, 2, 3], [4, 5, 6]]B = [[1, 1], [1, 1], [1, 1]]# 自定义图 my_graphmy_graph = tf.Graph()# 在自定义图中添加 数据节点 和 计算节点with my_graph.as_default(): A_tf = tf.constant(A, dtype=tf.int32) B_tf = tf.constant(B,原创 2020-12-24 14:28:22 · 81 阅读 · 0 评论 -
TensorFlow 激活函数, 池化函数
sigmoid 函数tf.nn.sigmoid(x)tanh 函数tf.nn.tanh(x)ReLU 函数tf.nn.relu(x)最大池化 tf.nn.max_pool()tf.nn.max_pool( value, ksize; strides, padding, data_format='NHWC' )均值池化 tf.nn.avg_pool()tf.nn.avg_pool( value, ksize; strides, padding, data_for原创 2020-12-24 12:12:19 · 109 阅读 · 0 评论 -
TensorFlow 全连接层 tf.layers.dense()
函数原型:tf.layers.dense( inputs, units, activation=None, use_bias=True, kernel_initializer=None, bias_initializer=tf.zeros_initializer(), kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_const原创 2020-12-23 21:26:25 · 600 阅读 · 0 评论 -
TensorFlow 卷积
Padding 方式(边缘填充方式)有两种:SAME :在边缘补充0VALID:不做任何填补,当有剩余项时,VALID采用的方式是“丢弃”。采用 SAME 的 padding 方式相比 VALID 的优势是,可以充分利用输入的数据,把所有信息都加入运算。所以大部分时候,使用卷积时Padding 方式都是 SAME。Stride(步长):需要在四个维度上 [batch, hight, width, channel] 设置Stride, 但是常见的设置只有一种: width 和 hight 上的原创 2020-12-23 13:35:17 · 328 阅读 · 0 评论 -
tf.nn.batch_normalization() 和 tf.layer.batch_normalization()
前提基本知识:均值: μ=x1+x2+x3+...+xnn\mu = \frac{x_1 + x_2 + x_3 + ...+x_n}{n}μ=nx1+x2+x3+...+xn方差: σ2=(x1−μ)2+(x2−μ)2+...+(xn−μ)2n\sigma^2 = \frac{(x_1-\mu)^2 +(x_2-\mu)^2 +...+(x_n-\mu)^2}{n}σ2=n(x1−μ)2+(x2−μ)2+...+(xn−μ)2标准差: σ=(x1−μ)2+(x2−μ)2+...+(原创 2020-12-22 10:38:45 · 1915 阅读 · 0 评论 -
TensorFlow 均值方差 tf.nn.moments
函数原型:def moments( x, axes, shift=None, # pylint: disable=unused-argument name=None, keep_dims=False)函数用来计算均值和方差,计算得出的均值和方差 可为batch_nomalization 所用。参数:x: 输入tensor, 四维:[batch_size, height, width, channel]axes: 在指定的维度求解,这里用复数axe原创 2020-12-21 10:58:21 · 935 阅读 · 1 评论 -
concat 和 stack 的比较
stack 会在axis指定的位置多生成一个维度import tensorflow as tfA = [[[1, 2, 3], [4, 5, 6]]]B = [[[7, 8, 9], [10, 11, 12]]]A_tf = tf.constant(A, dtype=tf.int32)B_tf = tf.constant(B, dtype=tf.int32)concat_0_tf = tf.concat([A_tf, B_tf], axis=0)stack_0_tf = tf.stac原创 2020-12-19 13:39:39 · 413 阅读 · 1 评论 -
TensorFlow 卷积函数
函数原型:tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_format=‘NHWC’, dilations=[1, 1, 1, 1], name=None)参数:input:输入数据,四维, 格式可以是[N, H, W, C], 也可以是[N, C, H, W]. 具体方式由data_format决定filter:卷积核, 四维strides:长度为4的一维列表,分别代表四个维度的stride,原创 2020-12-18 18:18:34 · 190 阅读 · 1 评论 -
TensorFlow 高维tenso常用工具函数
tensorflow 常用工具函数1. shape 重定义: tf.reshape()2. 维度交换: tf.transpose()3. 维度扩充: tf.expand_dims()4. 维度消除: tf.squeeze()1. shape 重定义: tf.reshape()函数原型:new_data = tf.reshape(original_data, new_shape)import tensorflow as tfdata = [[0, 1, 2, 3], [4, 5原创 2020-12-17 11:29:55 · 223 阅读 · 0 评论 -
TensorFlow 字符串拼接 tf.string_join
函数原型:tf.string_join(input, separator=’ ', name=True)参数:input: 是一个list,list中存放的数据类型为 tf.stringseparator=’ ': 字符串的连接符, 连接符也是字符串型name: 设置当前operation对象的名称举例 1: 拼接0维字符串import tensorflow as tfA_str_tf = tf.constant('hello', dtype=tf.string)B_str_tf原创 2020-12-16 21:34:36 · 1458 阅读 · 0 评论 -
TensorFlow. 字符串拆分 tf.string_split()
函数原型:tf.string_split(source, delimiter=’ ', skip_empty=True)参数:source:一维tensor 对象,其数据类型为tf_string,作为需要被分割的字符串。这里要注意的是: 输入的字符串必须以列表的格式传入,比如[‘I am Chinese’], 不用方括号[]括起来话,会报错。delimiter=’ ': 分割符,默认为空字符串skip_empty=True:bool 型,表示是否跳过空的字符串输出包括3个值:原创 2020-12-16 21:06:55 · 1724 阅读 · 0 评论 -
TensorFlow 字符串 与数字 互相转换
数字转字符串 tf.as_string()数据类型只能取 int32, int64, complex64, float32, float64, bool, int8 中的一种import tensorflow as tfA_tf = tf.constant([1, 2, 3, 4, 5, 6], dtype=tf.float32)A_str_tf = tf.as_string(A_tf)with tf.Session() as sess: bytes_v = sess.ru.原创 2020-12-16 17:28:31 · 2482 阅读 · 0 评论 -
TensorFlow 位运算
且位运算: tf.bitwise.bitwise_and(x, y, name=None)或位运算: tf.bitwise.bitwise_or(x, y, name=None)异或位运算: tf.bitwise_bitwise_xor(x, y, name=None)取反位运算: tf.bitwis_invert(x, name=None)x, y 数据类型只能是int8, int16, int32, int64, uint8, uint16, uint32, uint64 中.原创 2020-12-16 15:09:40 · 334 阅读 · 0 评论 -
TensorFlow 循环 tf.while_loop
函数原型:tf.while_loop(cond, body, loop_vars)参数:cond: 函数类型, 作为循环结束判断条件, 返回值为 tf.bool 类型body: 函数类型, 作为循环体, 返回值长度 与 loop_vars 长度相同,比如 loop_vars 有3个参数,那么body的返回值也有3个;若 loop_vars 有5个参数,那么body的返回值就有5个;loop_vars: 传入cond 和 body 的参数, 注意哦,传入 cond 和 body 的都是这些个参数原创 2020-12-16 14:28:14 · 1108 阅读 · 1 评论 -
TensorFlow 逻辑运算 tf.logical_and, tf.logical_or, tf.logical_not,tf.logical_xor
tf.logical_and(x, y, name=None)tf.logical_or(x, y, name=None)tf.logical_not(x, name=None)tf.logical_xor(x, y, name=None)import tensorflow as tfA_tf = tf.constant([[True, False, True], [False, True, True]], tf.bool)B_tf = tf.cons原创 2020-12-16 12:27:05 · 1221 阅读 · 0 评论 -
TensorFlow比较判断 tf.less, tf.less_equal, tf.less, tf.equal, tf.not_equal, tf.greater, tf.greater_equal
tf.lesstf.less_equaltf.lesstf.equaltf.not_equaltf.greatertf.greater_equal的用法类似, 以下以 tf.less 来举例函数原型为: tf.less(x, y, name=None)x, y 有相同的shape,并且有相同的数据类型。x, y 中的元素按照对应位置做比较,如果 x 中某元素小于 y 中对应元素,返回 True; 否则,返回 False。tf.less 返回值类型为bool 型,shape 与 x原创 2020-12-16 12:11:45 · 2966 阅读 · 1 评论 -
TensorFlow tf.where( ) 函数
tf.where 函数原型:tf.where(condition, x=None, y=None, name=None)理解: condition 是一个bool 型,如果元素值为True, 取x 的值;如果元素值为False, 取y 的值。以下为3种情况举例:x, y 为None, 做判断的 A_tf 和 B_tf 的维度相同返回值是condition中元素为True对应的索引import tensorflow as tfA_tf = tf.constant([[1, 3, 3],原创 2020-12-16 11:55:23 · 648 阅读 · 0 评论