tensorflow

文章目录

    • Session
    • tf.global_variables_initializer()
    • tf.local_variables_initializer()
    • tf.layers.batch_normalization
    • tf.ones_like
    • tf.transpose
    • tf.concat()
    • tf.gather( )
    • tf.nn.embedding_lookup
    • tf.tile
    • tf.reshape
    • tf.linalg.LinearOperatorLowerTriangular
    • tf.reduce_mean
    • tf.summary.scalar()
    • tf.equal(A, B)
    • tf.cast
    • tf.sequence_mask
    • tf.where
    • tf.nn.sigmoid_cross_entropy_with_logits
    • self.opt.apply_gradients
    • tf.clip_by_global_norm
    • model.global_step.eval()
    • Tensorflow中eval()
    • tf.assign

Session

Session 是 Tensorflow 为了控制,和输出文件的执行的语句. 运行 session.run() 可以获得你要得知的运算结果, 或者是你所要运算的部分.

import tensorflow as tf
 
# create two matrixes
 
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],
                       [2]])
product = tf.matmul(matrix1,matrix2)

因为 product 不是直接计算的步骤, 所以我们会要使用 Session 来激活 product 并得到计算结果. 有两种形式使用会话控制 Session 。

  • 方法一
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close()
  • 方法二
with tf.Session() as sess: 
result2 = sess.run(product) 
print(result2)

参考:
快速理解tf.Session()

tf.global_variables_initializer()

必须要使用global_variables_initializer的场合:含有tf.Variable的环境下,因为tf中建立的变量是没有初始化的,也就是在debug时还不是一个tensor量,而是一个Variable变量类型

size_out = 10
tensor = tf.Variable(tf.random_normal(shape=[size_out]))
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)  # initialization variables
    print(sess.run(tensor))

可以不适用初始化的场合:不含有tf.Variable、tf.get_Variable的环境下,比如只有tf.random_normal或tf.constant等
参考:
tf.global_variables_initializer()什么时候用?

tf.local_variables_initializer()

返回一个初始化所有局部变量的操作(Op)。初始化局部变量(GraphKeys.LOCAL_VARIABLE)。GraphKeys.LOCAL_VARIABLE中的变量指的是被添加入图中,但是未被储存的变量。


sess.run(tf.local_variables_initializer(),
feed_dict={
         learning_rate_dis: learning_rate_val_dis,
         adam_beta1_d_tf: adam_beta1_d,
         learning_rate_proj: learning_rate_val_proj,
         lambda_ratio_tf: lambda_ratio,
         lambda_l2_tf: lambda_l2,
         lambda_latent_tf: lambda_latent,
         lambda_img_tf: lambda_img,
         lambda_de_tf: lambda_de,
         adam_beta1_g_tf: adam_beta1_g,
         })
# learning_rate_dis为设置的变量,learning_rate_val_dis为我设置的具体的值。后续同理
————————————————
版权声明:本文为CSDN博主「Wanderer001」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_36670529/article/details/103930639

tf.layers.batch_normalization

Batch Normalization的作用

  1. 效果
    该方法出自Sergey Ioffe和Christian Szegedy的《Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift》一文,BN方法到底能否减少Internal Covariate Shift可能还无法下最终结论 (参见《How Does Batch Normalization Help Optimization?》),但的确可以加速训练平稳收敛! 所以,BatchNorm成为了居家旅行、训练模型之必备layer。
  2. 计算
    tensorflow_第1张图片
    用大白话描述就是,计算出当前batch的每个channel的均值mean,计算出当前batch的每个channel的方差variance,令输入减去均值再除以标准差delta,得到normalized输出x-hat,最后乘以scale参数gamma,加上shift参数beta,得到最终变换后的输出y。
  3. BN层在train与inference时的差别
    在训练时,我们可以计算出batch的均值和方差,迭代训练过程中,均值和方差一直在发生变化。但是在推理时,均值和方差是固定的,它们在训练过程中就被确定下来。《Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift》中给出的确定方式和TensorFlow中存在不同,这里我们介绍TensorFlow中的方式, 即采用滑动平均MovingAverage的方法,公式为: moving_average_value * momentum + value * (1 - momentum), 其中value为当前batch的平均值或方差,moving_average_value为滑动均值或滑动方差。
    最终,模型训练完毕后,在推理时使用滑动平均值moving_mean和滑动方差moving_variance对feature maps进行变换。
  4. 在模型中的哪些位置插入BN层
    推荐在Conv层或FC层之后,非线性变换激活层之前插入BN层。
tf.layers.batch_normalization(
    inputs,
    axis=-1,
    momentum=0.99,
    epsilon=0.001,
    center=True,
    scale=True,
    beta_initializer=tf.zeros_initializer(),
    gamma_initializer=tf.ones_initializer(),
    moving_mean_initializer=tf.zeros_initializer(),
    moving_variance_initializer=tf.ones_initializer(),
    beta_regularizer=None,
    gamma_regularizer=None,
    beta_constraint=None,
    gamma_constraint=None,
    training=False,
    trainable=True,
    name=None,
    reuse=None,
    renorm=False,
    renorm_clipping=None,
    renorm_momentum=0.99,
    fused=None,
    virtual_batch_size=None,
    adjustment=None
)

这里有几个重要参数需要注意:

  1. axis的值取决于按照input的哪一个维度进行BN,例如输入为channel_last format,即[batch_size, height, width, channel],则axis应该设定为4,如果为channel_first format,则axis应该设定为1.
  2. momentum的值用在训练时,滑动平均的方式计算滑动平均值moving_mean和滑动方差moving_variance。 后面做更详细的说明。
  3. center为True时,添加位移因子beta到该BN层,否则不添加。添加beta是对BN层的变换加入位移操作。注意,beta一般设定为可训练参数,即trainable=True。
  4. scale为True是,添加缩放因子gamma到该BN层,否则不添加。添加gamma是对BN层的变化加入缩放操作。注意,gamma一般设定为可训练参数,即trainable=True。
  5. training表示模型当前的模式,如果为True,则模型在训练模式,否则为推理模式。要非常注意这个模式的设定,这个参数默认值为False。如果在训练时采用了默认值False,则滑动均值moving_mean和滑动方差moving_variance都不会根据当前batch的数据更新,这就意味着在推理模式下,均值和方差都是其初始值,因为这两个值并没有在训练迭代过程中滑动更新。

使用tf.layers.batch_normalization()需要三步:
在卷积层将激活函数设置为None。
使用batch_normalization。
使用激活函数激活。

tf.ones_like

函数目的是创建一个和输入参数(tensor)维度一样,元素都为1的张量。

tf.ones_like 函数
ones_like(
    tensor,
    dtype=None,
    name=None,
    optimize=True
)

tensor:输入参数
dtype:返回的 Tensor 类型.必须是:float32,float64,int8,int16,int32,int64,uint8,complex64,complex128或bool.
name:操作的名称(可选).
optimize:如果为 true,则尝试静态确定 “张量” 的形状并将其编码为常量
————————————————
版权声明:本文为CSDN博主「零度老窖」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39550091/article/details/102880344

tf.transpose

def transpose(a, perm=None, name="transpose"):
  """Transposes `a`. Permutes the dimensions according to `perm`.

a:数组
perm:控制转置的操作,以perm = [0,1,2] 3个维度的数组为例, 0–代表的是最外层的一维, 1–代表外向内数第二维, 2–代表最内层的一维,这种perm是默认的值.如果换成[1,0,2],就是把最外层的两维进行转置,比如原来是2乘3乘4,经过[1,0,2]的转置维度将会变成3乘2乘4
————————————————
版权声明:本文为CSDN博主「路上的病人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40994943/article/details/85270159

tf.concat()

tensorflow中用来拼接张量的函数tf.concat(),用法:

tf.concat([tensor1, tensor2, tensor3,...], axis)
  t1 = [[1, 2, 3], [4, 5, 6]]
  t2 = [[7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 0)  # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
  tf.concat([t1, t2], 1)  # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
 
  # tensor t3 with shape [2, 3]
  # tensor t4 with shape [2, 3]
  tf.shape(tf.concat([t3, t4], 0))  # [4, 3]
  tf.shape(tf.concat([t3, t4], 1))  # [2, 6]

对于一个二维矩阵,第0个维度代表最外层方括号所框下的子集,第1个维度代表内部方括号所框下的子集。维度越高,括号越小。
注意:tf.concat()拼接的张量只会改变一个维度,其他维度是保存不变的。比如两个shape为[2,3]的矩阵拼接,要么通过axis=0变成[4,3],要么通过axis=1变成[2,6]。改变的维度索引对应axis的值。

对于axis等于负数的情况
负数在数组索引里面表示倒数(countdown)。比如,对于列表ls = [1,2,3]而言,ls[-1] = 3,表示读取倒数第一个索引对应值。

tf.gather( )

tf.gather(params,indices,axis=0 )

从params的axis维根据indices的参数值获取切片
tensorflow_第2张图片

tf.nn.embedding_lookup

tf.nn.embedding_lookup(
      params,#表示完整的嵌入张量,或者除了第一维度之外具有相同形状的P个张量的列表,表示经分割的嵌入张量
      ids,#一个类型为int32或int64的Tensor,包含要在params中查找的id
      partition_strategy='mod',#指定分区策略的字符串,如果len(params)> 1,则相关。当前支持“div”和“mod”。 默认为“mod”
      name=None,#操作名称(可选)
      validate_indices=True,#是否验证收集索引
      max_norm=None#如果不是None,嵌入值将被l2归一化为max_norm的值
)

tf.nn.embedding_lookup()函数的用法主要是选取一个张量里面索引对应的元素

tf.nn.embedding_lookup(tensor,id):即tensor就是输入的张量,id 就是张量对应的索引

tf.nn.embedding_lookup()就是根据input_ids中的id,寻找embeddings中的第id行。比如input_ids=[1,3,5],则找出embeddings中第1,3,5行,组成一个tensor返回

embedding_lookup不是简单的查表,id对应的向量是可以训练的,训练参数个数应该是 category num*embedding size,也就是说lookup是一种全连接层

#coding:utf-8
 import tensorflow as tf
 import numpy as np
 c = np.random.random([5,1])  ##随机生成一个5*1的数组
 b = tf.nn.embedding_lookup(c, [1, 3]) ##查找数组中的序号为1和3的
 with tf.Session() as sess:
     sess.run(tf.initialize_all_variables())
     print(sess.run(b))
     print(c)

输出的结果如下所示:
[[0.5687709 ]
[0.61091257]]

[[0.31777381]
[0.5687709 ]
[0.1779548 ]
[0.61091257]
[0.65478204]]
在c中第2个元素为0.5687709,第4个元素是0.61091257(索引从0开始),刚好是b的值

原文链接:https://blog.csdn.net/yangfengling1023/article/details/82910951

tf.tile

tensorflow中的tile()函数是用来对张量(Tensor)进行扩展的,其特点是对当前张量内的数据进行一定规则的复制。最终的输出张量维度不变。

tf.tile(
    input,
    multiples,
    name=None
)

input是待扩展的张量,multiples是扩展方法。
假如input是一个3维的张量。那么mutiples就必须是一个1x3的1维张量。这个张量的三个值依次表示input的第1,第2,第3维数据扩展几倍。

import tensorflow as tf

a = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.float32)
a1 = tf.tile(a, [2, 3])
with tf.Session() as sess:
    print(sess.run(a))
    print(sess.run(tf.shape(a)))
    print(sess.run(a1))
    print(sess.run(tf.shape(a1)))
=======
[[1. 2.]
 [3. 4.]
 [5. 6.]]
[3 2]
[[1. 2. 1. 2. 1. 2.]
 [3. 4. 3. 4. 3. 4.]
 [5. 6. 5. 6. 5. 6.]
 [1. 2. 1. 2. 1. 2.]
 [3. 4. 3. 4. 3. 4.]
 [5. 6. 5. 6. 5. 6.]]
[6 6]

tf.tile()具体的操作过程如下:
(原始矩阵应为3*2)
tensorflow_第3张图片
**请注意:**上面绘图中第一次扩展后第一维由三个数据变成两行六个数据,多一行并不是多了一维,数据扔为顺序排列,只是为了方便绘制而已。
每一维数据的扩展都是将前面的数据进行复制然后直接接在原数据后面。
如果multiples的某一个数据为1,则表示该维数据保持不变。

参考

tf.reshape

tf.reshape(
    tensor, shape, name=None
)

参数
tensor Tensor张量
shape Tensor张量,用于定义输出张量的shape,组成元素类型为 int32或int64.
name 可选参数,用于定义操作名称.
用法
tf.reshape函数用于对输入tensor进行维度调整,但是这种调整方式并不会修改内部元素的数量以及元素之间的顺序,换句话说,reshape函数不能实现类似于矩阵转置的操作。比如,对于矩阵[[1,2,3],[4,5,6]],如果使用reshape,将维度变为[3,2], 其输出结果为:[[1,2],[3,4],[5,6]], 元素之间的顺序并没有改变:1之后还是2,如果是矩阵转置操作,1之后应该为4。
现将输入tensor,flatten铺平,然后再进行维度调整(元素的顺序没有变化)

关于tf.reshape函数我们需要知道的是:

  1. 函数用于张量维度调整,但是不会修改内部元素的数量以及相对顺序
  2. shape中-1表示这个维度的大小,程序运行时会自动计算填充(因为变换前后元素数量不变,我们可以根据其他维度的大小,最终确定-1这个位置应该表示的数字)
  3. 如果需要通过修改内部元素的存储顺序以实现维度调整,需要使用tf.transpose函数

原文链接:https://blog.csdn.net/csdn0006/article/details/106238909/

tf.linalg.LinearOperatorLowerTriangular

tf.linalg 类:
用于线性计算的类,下面包括了好多线性操作的类,tf.linalg.LinearOperator()就是其中之一,还包括了如转置,奇异值分解,计算张量的迹等等关于张量线性运算的一些函数。
tf.linalg.LinearOperatorLowerTriangular()

a = tf.constant([[1.0, 3.0, 3.0], [2.0, 3.0, 4.0], [1.0, 3.0, 4.0]])
tril = tf.linalg.LinearOperatorLowerTriangular(a)

with tf.Session() as sess:
    print(sess.run(tril.to_dense()))
输出结果:
[[1. 0. 0.]
 [2. 3. 0.]
 [1. 3. 4.]]

如果是不规则的张亮会出现什么的情况呢?
首先是shape = [4,3] 的张量:

a = tf.constant([[1.0, 3.0, 3.0], [2.0, 3.0, 4.0], [1.0, 3.0, 4.0], [3, 4, 5]])
tril = tf.linalg.LinearOperatorLowerTriangular(a)

with tf.Session() as sess:
    print(sess.run(tril.to_dense()))

[[1. 0. 0.]
 [2. 3. 0.]
 [1. 3. 4.]
 [3. 4. 5.]]

————————————————
版权声明:本文为CSDN博主「weixin_42135074」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42135074/article/details/90573689

tf.reduce_mean

用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的平均值,主要用作降维或者计算tensor(图像)的平均值。
https://blog.csdn.net/sunmingyang1987/article/details/111409678

import tensorflow as tf

x = [[1,2,3],
     [4,5,6]]
y = tf.cast(x, tf.float32)

mean_all = tf.reduce_mean(y)
mean_0 = tf.reduce_mean(y, axis=0)
mean_1 = tf.reduce_mean(y, axis=1)

with tf.Session() as sess:
    m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])
 
print(m_a)
print(m_0)
print(m_1)

tf.summary.scalar()

添加变量到直方图中,但是不配合其他的方法,根本就显示不出来它的意义!
https://blog.csdn.net/tian_jiangnan/article/details/105121217

tf.equal(A, B)

对⽐这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False,返回的值的矩阵维度和A是⼀样的

tf.cast

tf.cast(x, dtype, name=None)
释义:数据类型转换

x,输入张量
dtype,转换数据类型
name,名称

tf.sequence_mask

sequence_mask(
    lengths,#整数张量,其所有值小于等于maxlen。
    maxlen=None,#标量整数张量,返回张量的最后维度的大小;默认值是lengths中的最大值。
    dtype=tf.bool,#结果张量的输出类型。
    name=None#操作的名字。
)
import tensorflow as tf
a = tf.sequence_mask([1, 2, 3], 5)
b = tf.sequence_mask([[1, 2], [3, 4]])
 with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(a))
    print(sess.run(b))

输出:

[[ True False False False False]
[ True True False False False]
[ True True True False False]]

解析:maxlen是5,所以一共有5列,lengths有三个元素[1,2,3],所以有三行,每一行分别前1、2、3个元素为True

[[[ True False False False]
[ True True False False]]

[[ True True True False]
[ True True True True]]]

解析:因为没有指定maxlen,故maxlen默认取lengths中的最大值4,所以一共有4列,lengths是二维数组,将其看作由两个一维lengths组成,所以输出也可以看作由这两个一维lengths的输出所组成

原文链接:https://blog.csdn.net/weixin_38314865/article/details/83303772

tf.where

作用:该函数的作用是根据condition,返回相对应的x或y,返回值是一个tf.bool类型的Tensor。

import tensorflow as tf
tf.where(
	condition,
	x=None,
	y=None,
	name=None
)

tf.where()的作用就是根据condition返回相对应的X 或 Y值。若condition=True,则返回对应X的值,False则返回对应的Y值。

tf.nn.sigmoid_cross_entropy_with_logits

tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None)

这个函数的输入是logits和targets,logits就是神经网络模型中的 W * X矩阵,注意不需要经过sigmoid,而targets的shape和logits相同,就是正确的label值。

参考:https://blog.csdn.net/luoxuexiong/article/details/90109822

self.opt.apply_gradients

apply_gradients(grads_and_vars, global_step=None, name=None)

该函数的作用是将compute_gradients()返回的值作为输入参数对variable进行更新。
GradientDescentOptimizer。这个方法会自动根据loss计算对应variable的导数。

tf.gradients(ys, xs, 
			 grad_ys=None, 
			 name='gradients',
			 colocate_gradients_with_ops=False,
			 gate_gradients=False,
			 aggregation_method=None,
			 stop_gradients=None)

tensorflow_第4张图片

tf.clip_by_global_norm

tf.clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None) 

通过权重梯度的总和的比率来截取多个张量的值。
t_list 是梯度张量, clip_norm 是截取的比率, 这个函数返回截取过的梯度张量和一个所有张量的全局范数。

Gradient Clipping的引入是为了处理gradient explosion或者gradients vanishing的问题。当在一次迭代中权重的更新过于迅猛的话,很容易导致loss divergence。Gradient Clipping的直观作用就是让权重的更新限制在一个合适的范围。

具体的细节是
1.在solver中先设置一个clip_gradient
2.在前向传播与反向传播之后,我们会得到每个权重的梯度diff,这时不像通常那样直接使用这些梯度进行权重更新,而是先求所有权重梯度的平方和sumsq_diff,如果sumsq_diff > clip_gradient,则求缩放因子scale_factor = clip_gradient / sumsq_diff。这个scale_factor在(0,1)之间。如果权重梯度的平方和sumsq_diff越大,那缩放因子将越小。
3.最后将所有的权重梯度乘以这个缩放因子,这时得到的梯度才是最后的梯度信息。

这样就保证了在一次迭代更新中,所有权重的梯度的平方和在一个设定范围以内,这个范围就是clip_gradient.

原文链接:https://blog.csdn.net/u013713117/article/details/56281715

model.global_step.eval()

使用global_step作为梯度更新次数控制整个训练过程何时停止,就相当于使用迭代次数(num of iterations)作为控制条件。在一次迭代过程,就前向传播了一个batch,并计算之后更新了一次梯度。

Tensorflow中eval()

eval() 其实就是tf.Tensor的Session.run() 的另外一种写法,但两者有差别

eval(): 将字符串string对象转化为有效的表达式参与求值运算返回计算结果
eval()也是启动计算的一种方式。基于Tensorflow的基本原理,首先需要定义图,然后计算图,其中计算图的函数常见的有run()函数,如sess.run()。同样eval()也是此类函数,
要注意的是,eval()只能用于tf.Tensor类对象,也就是有输出的Operation。对于没有输出的Operation,可以用.run()或者Session.run();Session.run()没有这个限制。

如果你有一个Tensor t,在使用t.eval()时,等价于:tf.get_default_session().run(t).
每次使用 eval 和 run时,都会执行整个计算图,为了获取计算的结果,将它分配给tf.Variable,然后获取。

原文链接:https://blog.csdn.net/jiaoyangwm/article/details/79248535

tf.assign

tf.assign()把value的值赋给ref,ref的值必须是Variable

tf.assign(
    ref,
    value,
    validate_shape=None,
    use_locking=None,
    name=None
)

例子

import tensorflow as tf

x = tf.Variable(1)
x = tf.assign(x, 2)
sess = tf.Session()
print(sess.run(x))

你可能感兴趣的:(tensorflow,深度学习,python)