栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

tensorflow 2.x 数学运算

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tensorflow 2.x 数学运算

数学运算

▪ (+) 加 (-) 减(*)乘( /)除
▪ (**)次方, (pow)次方, (square )平方
▪( sqrt)平方根
▪ (//)整除, (%)取余
▪ exp,( log )取对数
▪ @, matmul 矩阵相乘
▪ linear layer

运算类型
  • 元素运算 + - * /
  • 矩阵运算 @, matmul
  • 维度运算 reduce_mean 、max、min、sum
+ -*/%//
import tensorflow as tf
b = tf.fill([2,2],2.)
a = tf.ones([2,2])

print("a : ", a)
print("b :", b)

print("a + b: ", a + b)
print("a - b: ", a - b)
print("a / b: ", a / b)
print("a * b: ", a * b)

print("b // a: ", b // a)
print("b % a: ",b % a)

tf.math.log / tf.exp
import tensorflow as tf
b = tf.fill([2,2],2.)
a = tf.ones([2,2])

print("a : ", a)
print("b :", b)

# print("a + b: ", a + b)
# print("a - b: ", a - b)
# print("a / b: ", a / b)
# print("a * b: ", a * b)
#
# print("b // a: ", b // a)
# print("b % a: ",b % a)

print("log(a):",tf.math.log(a))
#这里的log是以log10为底的
print("exp(a)",tf.exp(a))

补充:怎样才能有log2

print(“log(a):”,tf.math.log(b)/tf.math.log(2.))

pow, sqrt
# pow, sqrt
print("pow(b,3) b的3次方:",tf.pow(b, 3))
print("b**3  b的3次方:",b**3)
print("tf.sqrt(b): ",tf.sqrt(b))

@ matmul 矩阵的相乘
a1 = tf.ones([4,2,3])
a2 = tf.fill([4,3,5],2.)
print("a1.shape:",a1.shape)

print("a2.shape:",a2.shape)
print("(a1 @ a2).shape:",(a1 @ a2).shape)
print("(tf.matmul(a1,a2)).shape : ",(tf.matmul(a1,a2)).shape)


补充:如何计算矩阵

y[0][0] = x[0][0]*w[0][0] + x[0][1]*w[1][0] + b[0]
y[1][0] = x[0][0]*w[0][1] + x[0][1]*w[1][1] + b[1]
......
y[0][1] = x[0][1]*w[0][0] + x[1][1]*w[1][0] + b[0]
......
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号