tensorflow中有很多在维度上的操作,本例以常用的tf.reduce_sum进行说明.官方给的api reduce_sum( input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None ) input_tensor:表示输入 axis:表示在那个维度进行sum操作. keep_dims:表示是否保留原始数据的维度,False相当于执行完后原始数据就会少一个维度. reduction_indices:…
tf.trainable_variables()  返回的是 所有需要训练的变量列表 tf.all_variables() 返回的是 所有变量的列表 v = tf.Variable(0, name='v') v1 = tf.Variable(tf.constant(5, shape=[1], dtype=tf.float32), name='v1') global_step = tf.Variable(6, name='global_step', trainable=False) # 声明不是训…
1.求loss: tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None)) 第一个参数logits:就是神经网络最后一层的输出,如果有batch的话,它的大小就是[batchsize,num_classes],单样本的话,大小就是num_classes 第二个参数labels:实际的标签,大小同上 具体的执行流程大概分为两步: 第一步是先对网络最后一层的输出做一个softmax,这一步通常…
import tensorflow as tfimport numpy as npsess=tf.Session()a=np.ones((5,6))c=tf.cast(tf.reduce_sum(a, axis=1),tf.bool) # tf.reduce_sum 表示按照axis方向求和c=sess.run(c)print('c=',c)print('c.shape',c.shape)k=tf.where(c) # 因为返回的是索引值为列表中k=sess.run(k)print('k=',k…
tf.reduce_sum 函数 reduce_sum ( input_tensor , axis = None , keep_dims = False , name = None , reduction_indices = None ) 定义在:tensorflow/python/ops/math_ops.py. 请参阅指南:数学函数>减少 此函数计算一个张量的各个维度上元素的总和. 函数中的input_tensor是按照axis中已经给定的维度来减少的:除非 keep_dims 是true,…
tf.concat, tf.stack和tf.unstack的用法 tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一个维度(axis)合并起来,例如: a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3) b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3) ab1 = tf.concat([a,b], axis=0) # shape(4,3) ab2 = t…
>>> x=[[1,2,3],[23,13,213]] >>> xx=tf.reduce_sum(x) >>> sess.run(xx) 255 >>> x=[90,10,8] >>> tf.reduce_sum(x) >>> sess.run(xx) 108…
根据官方文档: reduce_sum应该理解为压缩求和,用于降维 tf.reduce_sum(input_tensor,axis=None,keepdims=None,name=None,reduction_indices=None,keep_dims=None) Args: input_tensor: The tensor to reduce. Should have numeric type. #输入 axis: The dimensions to reduce. If None (the…
tf.nn.softmax softmax是神经网络的最后一层将实数空间映射到概率空间的常用方法,公式如下: \[ softmax(x)_i=\frac{exp(x_i)}{\sum_jexp(x_j)} \] 本文意于分析tensorflow中的tf.nn.softmax(),关于softmax的具体推导和相关知识点,参照其它文章. tensorflow的tf.nn.softmax()函数实现位于这里,可以看到,实现起来相当简明: tf.exp(logits)/tf.reduce_sum(tf…
原文地址: https://blog.csdn.net/C_chuxin/article/details/84990176 ------------------------------------------------------------------------------------------ [题目]tf.ConfigProto和tf.GPUOptions用法总结 概述 tf.ConfigProto一般用在创建session的时候,用来对session进行参数配置,而tf.GPUOp…
1234567reduce_sum 是 tensor 内部求和的工具.其参数中: input_tensor 是要求和的 tensor axis 是要求和的 rank,如果为 none,则表示所有 rank 都要仇和 keep_dims 求和后是否要降维 这个操作的名称,可能在 graph 中 用 已被淘汰的,被参数 axis 替代 x = tf.constant([[1, 1, 1], [1, 1, 1]])tf.reduce_sum(x, 0) # 对 tensor 的 0 级进行求和,[1,…
#axis 表示在哪个维度进行sum操作,不写代表所有维 #keep_dims 是否保留原始数据维度 reduce_sum( input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None ) 官方例子: # 'x' is [[1, 1, 1] # [1, 1, 1]] tf.reduce_sum(x) ==> 6 tf.reduce_sum(x, 0) ==> [2, 2, 2] tf.reduce_su…
易错点:注意带上参数axis,否则的话,默认对全部元素求和,返回一个数值int 参考:https://www.jianshu.com/p/30b40b504bae tf.reduce_sum( input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None)下面是个 2 * 3 * 4 的tensor [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]],…
一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_normal([5,5,6]),name='weight') return weight 如果多次调用函数如: result1=repeat_value() result2=repeat_value() # 重复调用 将会重复创建一份变量,却保存相同模型参数.若使用字典可以解决此类问题,却破坏模型封装性…
原文地址: https://blog.csdn.net/c20081052/article/details/82345454 --------------------------------------------------------------------------------------------------- 在服务器上用多GPU做训练时,由于想只用其中的一个GPU设备做训练,可使用深度学习代码运行时往往出现多个GPU显存被占满清理.出现该现象主要是tensorflow训练时默认占…
y = tf.tile(tf.range(2, dtype=tf.int32)[:, tf.newaxis], [2,3]) # tf.tile(input,[a,b]) 输入数据,按照对应维度将矩阵重复a次和b次 y1=tf.range(2, dtype=tf.int32)y2=tf.range(2, dtype=tf.int32)[:, tf.newaxis] # 将一维度矩阵增加一维度,列为1sess=tf.Session()a=sess.run(y)print(a)b=sess.run(…
tf.unstack 原型: unstack( value, num=None, axis=0, name='unstack' ) 官方解释:https://tensorflow.google.cn/api_docs/python/tf/unstack 解释:这是一个对矩阵进行分解的函数,以下为关键参数解释: value:代表需要分解的矩阵变量(其实就是一个多维数组,一般为二维): axis:指明对矩阵的哪个维度进行分解. 要理解tf.unstack函数,我们不妨先来看看tf.stack函数.T…
1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow as tf import numpy as np x = [[1, 2], [3, 4]] Y = tf.split(axis=1, num_or_size_splits=2, value=x) sess = tf.Session() for y in Y: print(sess.run(y))…
keras 构建模型很简单,上手很方便,同时又是 tensorflow 的高级 API,所以学学也挺好. 模型复现在我们的实验中也挺重要的,跑出了一个模型,虽然我们可以将模型的 checkpoint 保存,但再跑一遍,怎么都得不到相同的结果. 用 keras 实现模型,想要能够复现,首先需要设置各个可能的随机过程的 seed,如 np.random.seed(1).然后分为两种情况: 代码不要在 GPU 上跑,而是限制在 CPU 上跑,此时可以自行设置 fit 函数的 batch_size 参数…
1.tf.variable_scope 功能:tf.variable_scope可以让不同命名空间中的变量取相同的名字,无论tf.get_variable或者tf.Variable生成的变量 TensorFlow链接:https://tensorflow.google.cn/api_docs/python/tf/variable_scope?hl=en 举例: with tf.variable_scope('V1'): a1 = tf.get_variable(name='a1', shape=…
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复,同时维护一个条理清晰的graph非常重要. ==因此,tensorflow中用tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()几个…
1. tf.add(x,  y, name) Args: x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `complex64`, `complex128`, `string`. y: A `Tensor`. Must have the same type as `x`.…
https://blog.csdn.net/lanchunhui/article/details/61712830 https://www.cnblogs.com/silence-tommy/p/7029561.html 二者的主要区别在于: tf.Variable:主要在于一些可训练变量(trainable variables),比如模型的权重(weights,W)或者偏执值(bias): 声明时,必须提供初始值: 名称的真实含义,在于变量,也即在真实训练时,其值是会改变的,自然事先需要指定初…
1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的. 当然,变量也可以通过tf.Varivale来创建.当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价…
函数原型: tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)   Defined in tensorflow/python/ops/state_ops.py.   将 value 赋值给 ref,并输出 ref,即 ref = value:   这使得需要使用复位值的连续操作变简单   Defined in tensorflow/python/framework/tensor_shape.py. Arg…
tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable(创建变量)与tf.get_variable(创建变量 或 复用变量) TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的. 变量可以通过tf.Varivale来创建.当tf.get_variable用于变量创建时,和tf.…
import tensorflow as tf import numpy as np W = tf.Variable([[2,1,8],[1,2,5]], dtype=tf.float32, name='weights') b = tf.Variable([[1,2,5]], dtype=tf.float32, name='biases') init= tf.global_variables_initializer() saver = tf.train.Saver() with tf.Sessi…
tf.expand_dims和tf.squeeze函数 一.tf.expand_dims() Function tf.expand_dims(input, axis=None, name=None, dim=None) Inserts a dimension of 1 into a tensor’s shape. 在第axis位置增加一个维度 Given a tensor input, this operation inserts a dimension of 1 at the dimensio…
首先我们分析一下下面的代码: import tensorflow as tf import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]]) b=np.float32(np.random.randn(3,2)) #c=tf.matmul(a,b) c=tf.multiply(a,b) init=tf.global_variables_initializer() with tf.Session() as sess: print(c.eva…
a = tf.Variable(0.0,dtype=tf.float32) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(a)) a = tf.assign(a,10) print(sess.run(a)) a = tf.assign(a,20) print(sess.run(a)) 0.0 10.0 20.0 a = tf.Variable(1,dtype=tf.flo…