【TensorFlow】常用函数方法列表

tf.nn.l2_loss()的用法 计算张量的误差值 sum(t**2)/2

l2_loss()这个函数的作用是利用L2范数来计算张量的误差值,但是没有开发并且只取L2范数的值的一半
函数:

tf.nn.l2_loss(

    t,

    name=None

)

参数:

t:一个张量(tensor),类型可以为:half, bfloat16, float32, float64

name:op的一个名字,即为这个操作取个名字

作用:不使用sqrt计算张量的L2范数的一半

输出:

一个tensor,数据类型和t相同,是一个标量

计算公式:
output = sum(t2)/2**
简单的可以理解成张量中的每一个元素进行平方,然后求和,最后乘一个1/2
l2_loss一般用于优化目标函数中的正则项,防止参数太多复杂容易过拟合(所谓的过拟合问题是指当一个模型很复杂时,它可以很好的“记忆”每一个训练数据中的随机噪声的部分而忘记了要去“学习”训练数据中通用的趋势)

例子:

#coding:utf-8

import tensorflow as tf

x = tf.constant([1,2,3],dtype=tf.float32)

with tf.Session() as sess:

    print(sess.run(tf.nn.l2_loss(x)))

结果输出:

7.0

计算的过程:

1/2(12+22+3**2) = 1/2(1+4+9) = 7.0

题外话:

正则化的基本思想是向损失函数添加一个惩罚项用于惩罚大的权重,隐式地减少自由参数的数量,所以可以达到弹性地适用不同数据量训练的要求而不产生过拟合的问题。

正则化方法是将惩罚因子加入到各层的参数或激活函数中。其实现位置通常是在模型的optimization里,在计算损失函数时将该惩罚因子加进去。

tensorflow中 tf.reduce_mean函数 降维 计算平均值

tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。

reduce_mean(input_tensor,
                axis=None,
                keep_dims=False,
                name=None,
                reduction_indices=None)
 

第一个参数input_tensor: 输入的待降维的tensor;
第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
第四个参数name: 操作的名称;
第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;

以一个维度是2,形状是[2,3]的tensor举例:

import tensorflow as tf
 
x = [[1,2,3],
      [1,2,3]]
 
xx = tf.cast(x,tf.float32)
 
mean_all = tf.reduce_mean(xx, keep_dims=False)
mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False)
mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False)
 
 
with tf.Session() as sess:
    m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])
 
print m_a    # output: 2.0
print m_0    # output: [ 1.  2.  3.]
print m_1    #output:  [ 2.  2.]

如果设置保持原来的张量的维度,keep_dims=True ,结果:

print m_a # output: [[ 2.]]
print m_0 # output: [[ 1. 2. 3.]]
print m_1 #output: [[ 2.], [ 2.]]

类似函数还有:

tf.reduce_sum :计算tensor指定轴方向上的所有元素的累加和;
tf.reduce_max : 计算tensor指定轴方向上的各个元素的最大值;
tf.reduce_all : 计算tensor指定轴方向上的各个元素的逻辑和(and运算);
tf.reduce_any: 计算tensor指定轴方向上的各个元素的逻辑或(or运算);

tf.reset_default_graph()函数 简化命名规则

如下是官网对tf.reset_default_graph()函数描述的翻译:

tf.reset_default_graph函数用于清除默认图形堆栈并重置全局默认图形。

注意:默认图形是当前线程的一个属性。该tf.reset_default_graph函数只适用于当前线程。当一个tf.Session或者tf.InteractiveSession激活时调用这个函数会导致未定义的行为。调用此函数后使用任何以前创建的tf.Operation或tf.Tensor对象将导致未定义的行为。

上面这句话是什么意思?下面写一段简单的demo进行说明。

demo1,无tf.reset_default_graph()函数:

import tensorflow as tf
'''
Signature: tf.name_scope(*args, **kwds)
Docstring:
Returns a context manager for use when defining a Python op.
'''
# 也就是说,它的主要目的是为了更加方便地管理参数命名。
# 与 tf.Variable() 结合使用。简化了命名
# 注意,这里的 with 和 python 中其他的 with 是不一样的
# 执行完 with 里边的语句之后,这个 conv1/ 和 conv2/ 空间还是在内存中的。这时候如果再次执行此代码,就会再生成其他命名空间
with tf.name_scope('conv1') as scope:
    weights1 = tf.Variable([1.0, 2.0], name='weights')
    bias1 = tf.Variable([0.3], name='bias')
 
# 下面是在另外一个命名空间来定义变量的
with tf.name_scope('conv2') as scope:
    weights2 = tf.Variable([4.0, 2.0], name='weights')
    bias2 = tf.Variable([0.33], name='bias')
 
# 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突
print(weights1.name)
print(weights2.name)
print(bias1.name)
print(bias2.name)

执行结果:

从上述结果可以看出,每次运行jupyter notebook时都会在上一次执行的基础生成新的张量。

即:每在jupyter notebook上运行一次上述程序,就会在图上新增一个节点。

demo2,有tf.reset_default_graph()函数:

import tensorflow as tf
# 利用这个可清空default graph以及nodes
tf.reset_default_graph()
'''
Signature: tf.name_scope(*args, **kwds)
Docstring:
Returns a context manager for use when defining a Python op.
'''
# 也就是说,它的主要目的是为了更加方便地管理参数命名。
# 与 tf.Variable() 结合使用。简化了命名
# 注意,这里的 with 和 python 中其他的 with 是不一样的
# 执行完 with 里边的语句之后,这个 conv1/ 和 conv2/ 空间还是在内存中的。这时候如果再次执行此代码,就会再生成其他命名空间
with tf.name_scope('conv1') as scope:
    weights1 = tf.Variable([1.0, 2.0], name='weights')
    bias1 = tf.Variable([0.3], name='bias')
 
# 下面是在另外一个命名空间来定义变量的
with tf.name_scope('conv2') as scope:
    weights2 = tf.Variable([4.0, 2.0], name='weights')
    bias2 = tf.Variable([0.33], name='bias')
 
# 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突
print(weights1.name)
print(weights2.name)
print(bias1.name)
print(bias2.name)

执行结果:

# 第一次执行结果
conv1/weights:0
conv2/weights:0
conv1/bias:0
conv2/bias:0
 
# 第二次执行结果
conv1/weights:0
conv2/weights:0
conv1/bias:0
conv2/bias:0
 
# 第三次执行结果
conv1/weights:0
conv2/weights:0
conv1/bias:0
conv2/bias:0

无论执行多少次生成的张量始终不变。换句话说就是:tf.reset_default_graph函数用于清除默认图形堆栈并重置全局默认图形。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

和你在一起^_^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值