[Tensorflow]tensor 数学运算和逻辑运算
tensorflow
一个面向所有人的开源机器学习框架
项目地址:https://gitcode.com/gh_mirrors/te/tensorflow

·
一、arthmetic 算术操作(+,-,*,/,Mod)
(1)tensor-tensor操作(element-wise)
- #两个tensor 运算
- #运算规则:element-wise。即c[i,j,..,k]=a[i,j,..,k] op b[i,j,..,k]
- ts1=tf.constant(1.0,shape=[2,2])
- ts2=tf.Variable(tf.random_normal([2,2]))
- sess.run(tf.global_variables_initializer())
- #以ts1和ts2为例:
- #(1)加法+
- ts_add1=tf.add(ts1,ts2,name=None)
- ts_add2=ts1+ts2 #二者等价
- #(2)减法-
- ts_sub1=tf.subtract(ts1,ts2,name=None)
- ts_sub2=ts1-ts2 #二者等价
- #(3)乘法*
- ts_mul1=tf.multiply(ts1,ts2,name=None)
- ts_mul2=ts1*ts2
- #(4)除法/
- ts_div1=tf.divide(ts1,ts2,name=None)
- ts_div2=tf.div(ts1,ts2,name=None) #div 支持 broadcasting(即shape可不同)
- ts_div3=ts1/ts2
- #另外还有truediv(x,y) x,y类型必须一致,floor_div等。
- #(5)取模Mod(估计基本用不到)
(2)tensor-scalar操作
- #scalar-tensor操作。
- #对tensor中所有element执行同样的操作(+,-,*,/)
- #加法
- ts_add=ts1+2
- #减法
- ts_sub=ts1-2
- #乘法
- ts_mul=ts1*2
- #除法
- ts_div=ts1/2
二、基本数学函数
- #以下x,y均代表tensor
- tf.add_n(inputs, name=None) #inputs:tensor数组,所有tensor相加
- tf.abs(x, name=None) #绝对值
- tf.negative(x, name=None) #取反
- tf.sign(x, name=None) #取符号(y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.)
- tf.square(x, name=None) #y=x*x
- tf.round(x, name=None) #Rounds the values of a tensor to the nearest integer, element-wise.
- tf.sqrt(x, name=None) #sqrt
- tf.pow(x, y, name=None) #x,y均为tensor,element-wise求pow
- tf.exp(x, name=None) #y=e^x
- tf.log(x, name=None) #y=log(x)
- tf.ceil(x, name=None) #ceil
- tf.floor(x, name=None) #floor
- tf.maximum(x, y, name=None) #z=max(x,y)
- tf.minimum(x, y, name=None)
- tf.cos(x, name=None) #三角函数,sin,cos,tan,acos,asin,atan
- tf.sin(x, name=None)
- tf.tan(x, name=None)
- tf.acos(x, name=None)
- tf.asin(x, name=None)
- tf.atan(x, name=None)
- #...
- #等等一些函数。
三、Matrix矩阵操作
- tf.diag(diagonal, name=None) #得到以diagonal为对角的tensor
- tf.diag_part(input, name=None) #tf.diag 逆操作,得到input的对角矩阵
- tf.transpose(a, perm=None,name=None) #转置矩阵,y[i,j]=x[j,i]
- #矩阵乘法
- tf.matmul(a, b,
- transpose_a=False, transpose_b=False, #
- adjoint_a=False, adjoint_b=False, #共轭
- a_is_sparse=False, b_is_sparse=False, #矩阵是否稀疏
- name=None)
还有一些其他的矩阵操作,见 matirx
四、Reduction 归约操作
- #(1)tf.reduce_sum
- #当keep_dims=False。rank of tensor会降维度。
- tf.reduce_sum(input_tensor,
- axis=None, #要归约的dimention。值为None或一个数字或者数组。如0,1,[0,3,4]
- keep_dims=False, #if true, retains reduced dimensions with length 1.
- name=None,
- reduction_indices=None)
- #(2)tf.reduce_min / tf.reduce_max / tf.reduce_mean
- #参数与tf.reduce_sum一致。
- #tf.reduce_min : 被归约的数取最小值;
- #tf.reduce_max : 被归约的数取最大值;
- #tf.reduce_mean: 被归约的数取平均值。
- #(3)逻辑操作
- # tf.reduce_all:logical and operation
- # tf.reduce_any: logical or operation
- #(4)自定义操作函数
- tf.einsum(equation, *inputs)
- #例子:
- tf.einsum('ij,jk->ik', ts1,ts2) #矩阵乘法
- tf.einsum('ij->ji',ts1) #矩阵转置
五、tensor大小 比较
- #(1)相等equal (element-wise)
- tf.equal(x, y, name=None) #Returns the truth value of (x == y) element-wise.
- #(2)不等not_equal
- tf.not_equal(x, y, name=None)
- #(3)其他比较
- tf.less(x, y, name=None)
- tf.less_equal(x, y, name=None)
- tf.greater(x, y, name=None)
- tf.greater_equal(x, y, name=None)
六、恒等映射
- #恒等映射
- tf.identity(input, name=None) #Return a tensor with the same shape and contents as the input tensor or value.
七、类型转化
- tf.cast(x, dtype, name=None)
- #Casts a tensor to a new type.
- #For example:
- # tensor `a` is [1.8, 2.2], dtype=tf.float
- #tf.cast(a, tf.int32) ==> [1, 2] dtype=tf.int32
八、例子
tensorflow
一个面向所有人的开源机器学习框架
项目地址:https://gitcode.com/gh_mirrors/te/tensorflow
(1)RELU实现
- import tensorflow as tf
- def relu(x): #要构造一个和x shape一样的Tensor。源码中应该不会用效率这么低的写法。
- y=tf.constant(0.0,shape=x.get_shape())
- return tf.where(tf.greater(x,y),x,y)
- sess=tf.Session()
- x=tf.Variable(tf.random_normal(shape=[10],stddev=10))
- sess.run(tf.global_variables_initializer())
- x_relu=relu(x)
- data_x,data_x_relu=sess.run((x,x_relu))
- for i in range(0,len(data_x)):
- 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()
推荐内容
阅读全文
AI总结




一个面向所有人的开源机器学习框架
最近提交(Master分支:1 个月前 )
4f64a3d5
Instead, check for this case in `ResolveUsers` and `ResolveOperand`, by querying whether the `fused_expression_root` is part of the `HloFusionAdaptor`.
This prevents us from stepping into nested fusions.
PiperOrigin-RevId: 724311958
1 个月前
aa7e952e
Fix a bug in handling negative strides, and add a test case that exposes it.
We can have negative strides that are not just -1, e.g. with a combining
reshape.
PiperOrigin-RevId: 724293790
1 个月前
更多推荐
相关推荐
查看更多
tensorflow

一个面向所有人的开源机器学习框架
tensorflow

TensorFlow for R
TensorFlow

Project containig related material for my TensorFlow articles
热门开源项目
活动日历
查看更多
直播时间 2025-04-07 14:51:20

樱花限定季|G-Star校园行&华中农业大学专场
直播时间 2025-03-26 14:30:09

开源工业物联实战!
直播时间 2025-03-25 14:30:17

Heygem.ai数字人超4000颗星火燎原!
直播时间 2025-03-13 18:32:35

全栈自研企业级AI平台:Java核心技术×私有化部署实战
直播时间 2025-03-11 18:35:18

从0到1:Go IoT 开发平台的架构演进与生态蓝图
所有评论(0)