tensorflow常用函数笔记

函数名称: tf.Graph.as_default()
返回值:返回一个上下文管理器,这个上下文管理器使用这个图作为默认的图

说明:如果你想在一个进程里创建多个图,可能会用到这个方法。
为了方便,如果你没有显式创建一个图的话,系统提供了一个全局默认的图,默认把所有的操作都添加到全局默 认图中。使用这个方法的话,(配合with关键词使用),可以只把with块里的操作添加到默认图中。默认图是当前线程的一个属性,如果你创建了一个新的线程,想使用全局默认图,必须显式调用这个方法。

代码示例:

g = tf.Graph()              # 新建了一个图,将该图显示作为默认图
with g.as_default():
  c = tf.constant(5.0)
  assert c.graph is g

关于session与graph的具体内容看这里


tf.get_variable

get_variable(
    name,
    shape=None,
    dtype=None,
    initializer=None,
    regularizer=None,
    trainable=True,
    collections=None,
    caching_device=None,
    partitioner=None,
    validate_shape=True,
    use_resource=None,
    custom_getter=None
)

具体看这里


tf.train.exponential_decay
根据给定步长完成learn rate decay.
decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)

tf.train.exponential_decay(
    learning_rate,  # 初始学习率
    global_step,    # 总的学习次数
    decay_steps,    # learn rate decay 的步长
    decay_rate,     # 学习率下降的参数
    staircase=False,# 是否使用阶梯函数
    name=None
)

If the argument staircase = True, then global_step / decay_steps is an integer division and the decayed learning rate follows a staircase function.


tensorflow运行session的配置

tfconfig = tf.ConfigProto()

基本配置参数

tfconfig.log_device_placement=True : 是否打印设备分配日志
tfconfig.allow_soft_placement=True : 如果你指定的设备不存在,允许TF自动分配设备
tfconfig.tf.ConfigProto(log_device_placement=True,allow_soft_placement=True)

控制GPU资源率

#allow growth
tfconfig.gpu_options.allow_growth = True
# 使用allow_growth option,刚一开始分配少量的GPU容量,然后按需慢慢的增加,由于不会释放
# 内存,所以会导致碎片
# per_process_gpu_memory_fraction
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
tfconfig.gpu_options=gpu_options
#设置每个GPU应该拿出多少容量给进程使用,0.4代表 40%

tf.reshape

reshape(
    tensor,
    shape,
    name=None
    )

返回一个和tensor值相同,形状为shape的值
如果shape中有哪个维度是-1,这一维度的具体数值将根据其他维度以及输入维度计算得出。e.g.输入是
[a,b,c,d] 输入是[e,f,-1,g],则-1维度的大小为(a * b * c * d)/(e * f * g),前提是可以整除,否则会报错


tf.slice

slice(
    input_,
    begin,
    size,
    name=None
)

函数将input 切片,切片的开始为值由begin给定,size指定了切片的大小;当size[i]= -1时,所有begin[i]后边的元素都被切片:
size[i] = input.dim_size(i) - begin[i]
e.g

# 'input' is [[[1, 1, 1], [2, 2, 2]],
#             [[3, 3, 3], [4, 4, 4]],
#             [[5, 5, 5], [6, 6, 6]]]
tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]]
tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3],
                                            [4, 4, 4]]]
tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]],
                                           [[5, 5, 5]]]

tf.squeeze

squeeze(
    input,
    axis=None,
    name=None,
    squeeze_dims=None
)

压缩tensor,即将size为一的tensor删除。如果不想删除全部的size为一的dimension,可以指定axis删除相应位置。e.g

# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]

tf.stop_gradient

stop_gradient(
    input,
    name=None
)

input节点经过函数后输出的tensor,在计算梯度时将无他通过它往回传。
e.g. (http://blog.csdn.net/u012436149/article/details/53905797)

import tensorflow as tf

w1 = tf.Variable(2.0)
w2 = tf.Variable(2.0)

a = tf.multiply(w1, 3.0)
a_stoped = tf.stop_gradient(a)

# b=w1*3.0*w2
b = tf.multiply(a_stoped, w2)
gradients = tf.gradients(b, xs=[w1, w2])
print(gradients)
#输出
#[None, <tf.Tensor 'gradients/Mul_1_grad/Reshape_1:0' shape=() dtype=float32>]

图模型如下:

这里写图片描述

tf.image.crop_and_resize

crop_and_resize(
    image,
    boxes,
    box_ind,
    crop_size,
    method=None,
    extrapolation_value=None,
    name=None
)

从image中crop出roi,然后双线性差值,得到resize图像。resize的大小由crop_size控制。resize时允许畸变
返回值为[num_boxes, crop_height, crop_width, depth]。
box_ind: 一个1-d tensor,大小为[num_boxes] 值域为[0,batch).其中box_ind[i]指定第i个box所属的图像在batch中的索引值
method:resiz时插值的方法,目前只支持双线性(tf 1.1)


tf.reduce_mean

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

沿给定的axis计算均值。默认被计算均值的axis在返回结果中消失。当keep_dims为True时,返回的Tensor与输入Tensor相同,被求均值的axis的dim=1
e.g.:

# 'x' is [[1., 1.]
#         [2., 2.]]
tf.reduce_mean(x) ==> 1.5
tf.reduce_mean(x, 0) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1.,  2.]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值