[Tensorflow]tensor 数学运算和逻辑运算

一、arthmetic 算术操作(+,-,*,/,Mod)

(1)tensor-tensor操作(element-wise)

[python]  view plain  copy
  1. #两个tensor 运算  
  2. #运算规则:element-wise。即c[i,j,..,k]=a[i,j,..,k] op b[i,j,..,k]  
  3. ts1=tf.constant(1.0,shape=[2,2])  
  4. ts2=tf.Variable(tf.random_normal([2,2]))  
  5. sess.run(tf.global_variables_initializer())  
  6. #以ts1和ts2为例:  
  7.   
  8. #(1)加法+  
  9. ts_add1=tf.add(ts1,ts2,name=None)  
  10. ts_add2=ts1+ts2       #二者等价  
  11. #(2)减法-  
  12. ts_sub1=tf.subtract(ts1,ts2,name=None)  
  13. ts_sub2=ts1-ts2       #二者等价  
  14. #(3)乘法*  
  15. ts_mul1=tf.multiply(ts1,ts2,name=None)  
  16. ts_mul2=ts1*ts2  
  17. #(4)除法/  
  18. ts_div1=tf.divide(ts1,ts2,name=None)  
  19. ts_div2=tf.div(ts1,ts2,name=None)   #div 支持 broadcasting(即shape可不同)  
  20. ts_div3=ts1/ts2  
  21. #另外还有truediv(x,y) x,y类型必须一致,floor_div等。  
  22. #(5)取模Mod(估计基本用不到)  

(2)tensor-scalar操作

[python]  view plain  copy
  1. #scalar-tensor操作。  
  2. #对tensor中所有element执行同样的操作(+,-,*,/)  
  3. #加法  
  4. ts_add=ts1+2  
  5. #减法  
  6. ts_sub=ts1-2  
  7. #乘法  
  8. ts_mul=ts1*2  
  9. #除法  
  10. ts_div=ts1/2  


二、基本数学函数

[python]  view plain  copy
  1. #以下x,y均代表tensor  
  2.   
  3. tf.add_n(inputs, name=None)  #inputs:tensor数组,所有tensor相加  
  4. tf.abs(x, name=None)         #绝对值  
  5. tf.negative(x, name=None)    #取反  
  6. tf.sign(x, name=None)        #取符号(y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.)  
  7. tf.square(x, name=None)      #y=x*x  
  8. tf.round(x, name=None)       #Rounds the values of a tensor to the nearest integer, element-wise.  
  9. tf.sqrt(x, name=None)        #sqrt   
  10. tf.pow(x, y, name=None)      #x,y均为tensor,element-wise求pow  
  11. tf.exp(x, name=None)         #y=e^x  
  12. tf.log(x, name=None)         #y=log(x)   
  13. tf.ceil(x, name=None)        #ceil  
  14. tf.floor(x, name=None)       #floor  
  15. tf.maximum(x, y, name=None)  #z=max(x,y)  
  16. tf.minimum(x, y, name=None)  
  17. tf.cos(x, name=None)         #三角函数,sin,cos,tan,acos,asin,atan  
  18. tf.sin(x, name=None)   
  19. tf.tan(x, name=None)  
  20. tf.acos(x, name=None)  
  21. tf.asin(x, name=None)  
  22. tf.atan(x, name=None)  
  23. #...  
  24. #等等一些函数。  


三、Matrix矩阵操作

[python]  view plain  copy
  1. tf.diag(diagonal, name=None)         #得到以diagonal为对角的tensor  
  2. tf.diag_part(input, name=None)       #tf.diag 逆操作,得到input的对角矩阵   
  3. tf.transpose(a, perm=None,name=None#转置矩阵,y[i,j]=x[j,i]  
  4. #矩阵乘法  
  5. tf.matmul(a, b,   
  6.   transpose_a=False, transpose_b=False,  #  
  7.   adjoint_a=False, adjoint_b=False,      #共轭  
  8.   a_is_sparse=False, b_is_sparse=False,  #矩阵是否稀疏  
  9.   name=None)  
  10.     

还有一些其他的矩阵操作,见 matirx

四、Reduction 归约操作

[python]  view plain  copy
  1. #(1)tf.reduce_sum   
  2. #当keep_dims=False。rank of tensor会降维度。  
  3. tf.reduce_sum(input_tensor,   
  4.    axis=None,               #要归约的dimention。值为None或一个数字或者数组。如0,1,[0,3,4]  
  5.    keep_dims=False,         #if true, retains reduced dimensions with length 1.  
  6.    name=None,   
  7.    reduction_indices=None)  
  8.   
  9. #(2)tf.reduce_min / tf.reduce_max / tf.reduce_mean  
  10. #参数与tf.reduce_sum一致。  
  11. #tf.reduce_min : 被归约的数取最小值;  
  12. #tf.reduce_max : 被归约的数取最大值;  
  13. #tf.reduce_mean: 被归约的数取平均值。  
  14.   
  15. #(3)逻辑操作  
  16. # tf.reduce_all:logical and operation  
  17. # tf.reduce_any: logical or operation  
  18.   
  19.   
  20. #(4)自定义操作函数  
  21. tf.einsum(equation, *inputs)  
  22. #例子:  
  23. tf.einsum('ij,jk->ik', ts1,ts2)  #矩阵乘法  
  24. tf.einsum('ij->ji',ts1)          #矩阵转置  

五、tensor大小 比较

[python]  view plain  copy
  1. #(1)相等equal (element-wise)  
  2. tf.equal(x, y, name=None#Returns the truth value of (x == y) element-wise.  
  3.   
  4. #(2)不等not_equal    
  5. tf.not_equal(x, y, name=None)  
  6.   
  7. #(3)其他比较  
  8. tf.less(x, y, name=None)  
  9. tf.less_equal(x, y, name=None)  
  10. tf.greater(x, y, name=None)  
  11. tf.greater_equal(x, y, name=None)  

六、恒等映射

[python]  view plain  copy
  1. #恒等映射  
  2. tf.identity(input, name=None#Return a tensor with the same shape and contents as the input tensor or value.  

七、类型转化

[python]  view plain  copy
  1. tf.cast(x, dtype, name=None)  
  2. #Casts a tensor to a new type.  
  3.   
  4. #For example:  
  5. # tensor `a` is [1.8, 2.2], dtype=tf.float  
  6. #tf.cast(a, tf.int32) ==> [1, 2]  dtype=tf.int32  


八、例子

(1)RELU实现
[html]  view plain  copy
  1. import tensorflow as tf  
  2. def relu(x):        #要构造一个和x shape一样的Tensor。源码中应该不会用效率这么低的写法。  
  3.   y=tf.constant(0.0,shape=x.get_shape())  
  4.   return tf.where(tf.greater(x,y),x,y)  
  5.   
  6. sess=tf.Session()  
  7. x=tf.Variable(tf.random_normal(shape=[10],stddev=10))  
  8. sess.run(tf.global_variables_initializer())  
  9. x_relu=relu(x)  
  10. data_x,data_x_relu=sess.run((x,x_relu))  
  11. for i in range(0,len(data_x)):    
  12.   print("%.5f  --relu--> %.5f" %(data_x[i],data_x_relu[i]))  


11z\

自己写的程序,能在电脑上运行的

import tensorflow as tf
import numpy as np

x = 2
y = 3
add_op = tf.add(x,y)#加法
mul_op = tf.multiply(x,y)#乘法
useless = tf.multiply(x,add_op)
pow_op = tf.pow(add_op,mul_op)#幂次方
with tf.Session() as sess:
    z,not_useless = sess.run([pow_op,useless])
    print(z,not_useless)
import tensorflow as tf
a = tf.add(3,5)
sess = tf.Session()
print(sess.run(a))
sess.close()



猜你喜欢

转载自blog.csdn.net/duanyajun987/article/details/80237999