Tensorflow-常用函数

本文详细介绍了Tensorflow中包括算术操作、形状操作、矩阵运算、复数操作、归约计算、切片与合并、分割、序列比较与索引提取等核心函数。还涵盖了激活函数、卷积与池化函数、数据标准化、损失函数、分类函数、符号嵌入、循环神经网络以及变量的保存与恢复等深度学习关键操作。
摘要由CSDN通过智能技术生成

算术操作:

操作组 操作
tf.add(x, y, name=None) 求和
tf.sub(x, y, name=None) 减法
tf.mul(x, y, name=None) 乘法
tf.div(x, y, name=None) 除法
tf.mod(x, y, name=None) 取模
tf.abs(x, name=None) 求绝对值
tf.neg(x, name=None) 取负 (y = -x)
tf.sign(x, name=None) 返回符号 y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0
tf.inv(x, name=None) 取反
tf.square(x, name=None) 计算平方 (y = x * x = x^2)
tf.round(x, name=None) 舍入最接近的整数
#‘a’ is [0.9, 2.5, 2.3, -4.4]
tf.round(a) ==> [ 1.0, 3.0, 2.0, -4.0 ]
tf.sqrt(x, name=None) 开根号 (y = \sqrt{x} = x^{1/2})
tf.pow(x, y, name=None) 幂次方
# tensor ‘x’ is [[2, 2], [3, 3]]
# tensor ‘y’ is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]
tf.exp(x, name=None) 计算e的次方
tf.log(x, name=None) 计算log,一个输入计算e的ln,两输入以第二输入为底
tf.cos(x, name=None) 三角函数cosine
tf.sin(x, name=None) 三角函数sine
tf.tan(x, name=None) 三角函数tan
tf.atan(x, name=None) 三角函数ctan
tf.maximum(x, y, name=None) 返回最大值 (x > y ? x : y)
tf.minimum(x, y, name=None) 返回最小值 (x < y ? x : y)

张量
数据类型转换

操作组 操作
tf.string_to_number(string_tensor, out_type=None, name=None) 字符串转为数字
tf.to_double(x, name=’ToDouble’) 转为64位浮点类型–float64
tf.to_float(x, name=’ToFloat’) 转为32位浮点类型–float32
tf.to_int32(x, name=’ToInt32’) 转为32位整型–int32
tf.to_int64(x, name=’ToInt64’) 转为64位整型–int64
tf.cast(x, dtype, name=None) 将x或者x.values转换为dtype
# tensor a is [1.8, 2.2], dtype=tf.float tf.cast(a, tf.int32) ==> [1, 2]
# dtype=tf.int32

Shape 形状操作

操作组 操作
tf.shape(input, name=None) 返回数据的shape
# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
shape(t) ==> [2, 2, 3]
tf.size(input, name=None) 返回数据的元素数量
# ‘t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]
size(t) ==> 12
tf.rank(input, name=None) 返回tensor的rank注意:此rank不同于矩阵的rank
tensor的rank表示一个tensor需要的索引数目来唯一表示任何一个元素也就是通常所说的 “order”,“degree”或”ndims”
#’t’ is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
# shape of tensor ‘t’ is [2, 2, 3]
rank(t) ==> 3
tf.reshape(tensor, shape, name=None) 改变tensor的形状
# tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor ‘t’ has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
#如果shape有元素[-1],表示在该维度打平至一维
# -1 将自动推导得为
9:reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],[4, 4, 4, 5, 5, 5, 6, 6, 6]]
tf.expand_dims(input, dim, name=None) 插入维度1进入一个tensor中
#该操作要求-1-input.dims()
# ‘t’ is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1] <= dim <= input.dims()

矩阵

操作组 操作
tf.diag(diagonal, name=None) 返回一个给定对角值的对角tensor
# ‘diagonal’ is [1, 2, 3, 4]
tf.diag(diagonal) ==>
[[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]]
tf.diag_par
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值