TensorFlow常用生成tensor函数

更多函数的使用:http://blog.csdn.net/lenbow/article/details/52152766

参考博客:http://www.mamicode.com/info-detail-1823840.html

生成全0和全1的矩阵

tf.ones(shape,type=tf.float32,name=None)
tf.zeros(shape, type=tf.float32,name=None)

>>> x = tf.zeros([2, 3])
>>> sess.run(x)
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> y = tf.ones([2, 3])
>>> sess.run(y)
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)


tf.ones_like(tensor,dype=None,name=None) 

tf.zeros_like(tensor,dype=None,name=None) 
新建一个与给定的tensor类型大小一致的tensor,其所有元素为1和0

>>> xx = tf.ones_like(x)
>>> sess.run(xx)
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)
>>> yy = tf.zeros_like(y)
>>> sess.run(yy)
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)


tf.fill(shape,value,name=None) 
创建一个形状大小为shape的tensor,其初始值为value

>>> z = tf.fill([2, 3], 2)
>>> sess.run(z)
array([[2, 2, 2],
       [2, 2, 2]])


生成随机数:

(1)tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None) 

random_normal: 正太分布随机数,均值mean,标准差stddev 

(2)tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) 

truncated_normal:截断正态分布随机数,均值mean,标准差stddev,不过只保留[mean-2*stddev,mean+2*stddev]范围内的随机数 

(3)tf.random_uniform(shape,minval=0,maxval=None,dtype=tf.float32,seed=None,name=None) 

random_uniform:均匀分布随机数,范围为[minval,maxval]

(4)tf.random_shuffle(value,seed=None,name=None) 
沿着value的第一维进行随机重新排列

>>> sess.run(tf.random_normal([2, 3], 0.0, 0.05))
array([[-0.09716501, -0.00082436,  0.00082046],
       [ 0.04063983,  0.06137555, -0.03889754]], dtype=float32)
>>> sess.run(tf.random_uniform([2, 3], 1, 6))
array([[ 1.53847492,  4.09809971,  2.94473267],
       [ 4.00791836,  3.95637321,  5.24030209]], dtype=float32)

>>> c = tf.constant([[1, 2, 3],[4, 5, 6]])
>>> sess.run(tf.random_shuffle(c))
array([[4, 5, 6],
       [1, 2, 3]])

tf.constant

tf.constant(value,dtype=None,shape=None,name=’Const’) 

>>> sess = tf.InteractiveSession()
>>> a = tf.constant(2, tf.float32, [2, 3])
>>> a.eval()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)
>>> a = tf.constant(2., shape=[2, 3])
>>> a.eval()
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.]], dtype=float32)


tf.shape(Tensor) 

返回张量的形状,tf.shape函数本身也是返回一个张量

>>> sess.run(tf.shape(x))
array([2, 3])

tf.expand_dims(Tensor, dim) 

为tensor增维和降维

>>> a = tf.zeros([3])
>>> sess.run(tf.shape(a))
array([3])
>>> sess.run(tf.shape(tf.expand_dims(a, 0)))
array([1, 3])
>>> sess.run(tf.shape(tf.expand_dims(a, 1)))
array([3, 1])



基本算术运算

 

操作描述
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.maximum(x, y, name=None) 返回最大值 (x > y ? x : y)
tf.minimum(x, y, name=None) 返回最小值 (x < y ? x : y)
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.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_part(input, name=None) 功能与上面相反
tf.trace(x, name=None) 求一个2维tensor足迹,即对角值diagonal之和
tf.transpose(a, perm=None, name=’transpose’) 调换tensor的维度顺序
按照列表perm的维度排列调换tensor顺序,
如为定义,则perm为(n-1…0)
# ‘x’ is [[1 2 3],[4 5 6]]
tf.transpose(x) ==> [[1 4], [2 5],[3 6]]
# Equivalently
tf.transpose(x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]]
tf.matmul(a, b, transpose_a=False, 
transpose_b=False, a_is_sparse=False, 
b_is_sparse=False, name=None)
矩阵相乘
tf.matrix_determinant(input, name=None) 返回方阵的行列式
tf.matrix_inverse(input, adjoint=None, name=None) 求方阵的逆矩阵,adjoint为True时,计算输入共轭矩阵的逆矩阵
tf.cholesky(input, name=None) 对输入方阵cholesky分解,
即把一个对称正定的矩阵表示成一个下三角矩阵L和其转置的乘积的分解A=LL^T
tf.matrix_solve(matrix, rhs, adjoint=None, name=None) 求解tf.matrix_solve(matrix, rhs, adjoint=None, name=None)
matrix为方阵shape为[M,M],rhs的shape为[M,K],output为[M,K]
复数操作

 

操作描述
tf.complex(real, imag, name=None) 将两实数转换为复数形式
# tensor ‘real’ is [2.25, 3.25]
# tensor imag is [4.75, 5.75]
tf.complex(real, imag) ==> [[2.25 + 4.75j], [3.25 + 5.75j]]
tf.complex_abs(x, name=None) 计算复数的绝对值,即长度。
# tensor ‘x’ is [[-2.25 + 4.75j], [-3.25 + 5.75j]]
tf.complex_abs(x) ==> [5.25594902, 6.60492229]
tf.conj(input, name=None) 计算共轭复数
tf.imag(input, name=None)
tf.real(input, name=None)
提取复数的虚部和实部
tf.fft(input, name=None) 计算一维的离散傅里叶变换,输入数据类型为complex64
归约计算(Reduction)

 

操作描述
tf.reduce_sum(input_tensor, reduction_indices=None, 
keep_dims=False, name=None)
计算输入tensor元素的和,或者安照reduction_indices指定的轴进行求和
# ‘x’ is [[1, 1, 1]
# [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
tf.reduce_sum(x, [0, 1]) ==> 6
tf.reduce_prod(input_tensor, 
reduction_indices=None, 
keep_dims=False, name=None)
计算输入tensor元素的乘积,或者安照reduction_indices指定的轴进行求乘积
tf.reduce_min(input_tensor, 
reduction_indices=None, 
keep_dims=False, name=None)
求tensor中最小值
tf.reduce_max(input_tensor, 
reduction_indices=None, 
keep_dims=False, name=None)
求tensor中最大值
tf.reduce_mean(input_tensor, 
reduction_indices=None, 
keep_dims=False, name=None)
求tensor中平均值
tf.reduce_all(input_tensor, 
reduction_indices=None, 
keep_dims=False, name=None)
对tensor中各个元素求逻辑’与’
# ‘x’ is 
# [[True, True]
# [False, False]]
tf.reduce_all(x) ==> False
tf.reduce_all(x, 0) ==> [False, False]
tf.reduce_all(x, 1) ==> [True, False]
tf.reduce_any(input_tensor, 
reduction_indices=None, 
keep_dims=False, name=None)
对tensor中各个元素求逻辑’或’
tf.accumulate_n(inputs, shape=None, 
tensor_dtype=None, name=None)
计算一系列tensor的和
# tensor ‘a’ is [[1, 2], [3, 4]]
# tensor b is [[5, 0], [0, 6]]
tf.accumulate_n([a, b, a]) ==> [[7, 4], [6, 14]]
tf.cumsum(x, axis=0, exclusive=False, 
reverse=False, name=None)
求累积和
tf.cumsum([a, b, c]) ==> [a, a + b, a + b + c]
tf.cumsum([a, b, c], exclusive=True) ==> [0, a, a + b]
tf.cumsum([a, b, c], reverse=True) ==> [a + b + c, b + c, c]
tf.cumsum([a, b, c], exclusive=True, reverse=True) ==> [b + c, c, 0]
序列比较与索引提取(Sequence Comparison and Indexing)

 

操作描述
tf.argmin(input, dimension, name=None) 返回input最小值的索引index
tf.argmax(input, dimension, name=None) 返回input最大值的索引index
tf.listdiff(x, y, name=None) 返回x,y中不同值的索引
tf.where(input, name=None) 返回bool型tensor中为True的位置
# ‘input’ tensor is 
#[[True, False]
#[True, False]]
# ‘input’ 有两个’True’,那么输出两个坐标值.
# ‘input’的rank为2, 所以每个坐标为具有两个维度.
where(input) ==>
[[0, 0],
[1, 0]]
tf.unique(x, name=None) 返回一个元组tuple(y,idx),y为x的列表的唯一化数据列表,
idx为x数据对应y元素的index
# tensor ‘x’ is [1, 1, 2, 4, 4, 4, 7, 8, 8]
y, idx = unique(x)
y ==> [1, 2, 4, 7, 8]
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]
tf.invert_permutation(x, name=None) 置换x数据与索引的关系
# tensor x is [3, 4, 0, 2, 1]
invert_permutation(x) ==> [2, 4, 3, 0, 1]


TensorFlow常用函数包括基本函数和维度变换函数。 基本函数包括: 1. `tf.random_normal()` 用于生成符合正态分布的随机数。 2. `tf.Variable()` 用于定义可训练的变量。 3. `tf.convert_to_tensor()` 用于将Python对象转换为Tensor对象。 4. `tf.placeholder()` 用于定义占位符,可以在运行时传入具体的值。 5. `tf.nn.conv2d()` 用于进行二维卷积操作。 6. `tf.nn.relu()` 用于实现ReLU激活函数。 7. `tf.nn.maxpool()` 用于进行最大池化操作。 维度变换函数包括: 1. `Reshape` 用于改变Tensor的形状。 2. `tf.transpose()` 用于交换Tensor的维度顺序。 3. `tf.expand_dims()` 用于在指定位置增加维度。 4. `tf.squeeze()` 用于去除Tensor中维度为1的维度。 以上是TensorFlow常用的一些函数,可以帮助我们进行数据的处理和模型的构建。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [tensorflow 常用函数](https://blog.csdn.net/hanfei410/article/details/106140878)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [tensorflow 一些常用函数](https://blog.csdn.net/weixin_43915860/article/details/106288956)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值