tensorflow 2.0 基础操作 之 数学运算

常用的数学运算有:

  1. 加减乘除
  2. 平方,开方,幂方
  3. 平方根
  4. 地板除,取余
  5. 次幂,取对数
  6. 矩阵乘

运算类型:

  1. element-wise (元素相关)
    - + - * \
  2. matrix-wise (矩阵相关)
    - @ matmul
  3. dim-wise (维度相关)
    - reduce_mean/max/min/sum

+ - * / // %

a = tf.fill([2,2], 3.)
b = tf.fill([2,2], 2.)
a+b, a-b, a*b, a/b
# (<tf.Tensor: id=26, shape=(2, 2), dtype=int32, numpy=
#  array([[5, 5],
#         [5, 5]])>, <tf.Tensor: id=27, shape=(2, 2), dtype=int32, numpy=
#  array([[1, 1],
#         [1, 1]])>, <tf.Tensor: id=28, shape=(2, 2), dtype=int32, numpy=
#  array([[6, 6],
#         [6, 6]])>, <tf.Tensor: id=31, shape=(2, 2), dtype=float64, numpy=
#  array([[1.5, 1.5],
#         [1.5, 1.5]])>)
a//b, a%b
# (<tf.Tensor: id=12, shape=(2, 2), dtype=int32, numpy=
#  array([[1, 1],
#         [1, 1]])>, <tf.Tensor: id=13, shape=(2, 2), dtype=int32, numpy=
#  array([[1, 1],
#         [1, 1]])>)

tf.math.log && tf.exp

tensorflow 2.0 alpha 中 tf.math.log && tf.exp
log 是以自然对数为底。

a = tf.fill([2,2], 3.)
# 指数对数
tf.math.log(a)
# <tf.Tensor: id=118, shape=(2, 2), dtype=float32, numpy=
# array([[1.0986123, 1.0986123],
#        [1.0986123, 1.0986123]], dtype=float32)>

tf.exp(a)
# <tf.Tensor: id=164, shape=(2, 2), dtype=float32, numpy=
# array([[20.085537, 20.085537],
#        [20.085537, 20.085537]], dtype=float32)>

log 但是 没有以其他 为底的 API
实现 以2,10 为底:
l o g e b l o g e a = l o g a b \frac{log_eb}{log_ea}=log_ab logealogeb=logab
l o g 2 x = l o g x l o g 2 log_2x = \frac{log x}{log 2} log2x=log2logx

tf.math.log(8.)/tf.math.log(2.)
# <tf.Tensor: id=304, shape=(), dtype=float32, numpy=3.0>

l o g 10 x = l o g x l o g 10 log_{10}x = \frac{log x}{log 10} log10x=log10logx

tf.math.log(1000.)/tf.math.log(10.)
# <tf.Tensor: id=407, shape=(), dtype=float32, numpy=3.0>

pow , sqrt

tf.pow(b, 3)
# <tf.Tensor: id=514, shape=(2, 2), dtype=float32, numpy=
# array([[8., 8.],
#        [8., 8.]], dtype=float32)>
b**3
# <tf.Tensor: id=573, shape=(2, 2), dtype=float32, numpy=
# array([[8., 8.],
#        [8., 8.]], dtype=float32)>
tf.sqrt(b)
# <tf.Tensor: id=634, shape=(2, 2), dtype=float32, numpy=
# array([[1.4142135, 1.4142135],
#        [1.4142135, 1.4142135]], dtype=float32)>

@ tf.matmul

a,b
# (<tf.Tensor: id=64, shape=(2, 2), dtype=float32, numpy=
#  array([[3., 3.],
#         [3., 3.]], dtype=float32)>,
#  <tf.Tensor: id=67, shape=(2, 2), dtype=float32, numpy=
#  array([[2., 2.],
#         [2., 2.]], dtype=float32)>)
tf.matmul(a,b)
# <tf.Tensor: id=925, shape=(2, 2), dtype=float32, numpy=
# array([[12., 12.],
#        [12., 12.]], dtype=float32)>
a @ b
# <tf.Tensor: id=1008, shape=(2, 2), dtype=float32, numpy=
# array([[12., 12.],
#        [12., 12.]], dtype=float32)>

高维 tensor 低两维作矩阵运算

a = tf.ones([4,1,2])
b = tf.fill([4,2,2], 2.)
(a@b).shape   # TensorShape([4, 1, 2])

需 手动 broadcasting

a = tf.ones([4,5,6])
b = tf.fill([6,4], 2.)
bb = tf.broadcast_to(b, [4,6,4])
(a@bb).shape   # TensorShape([4, 5, 4])

liner layer

为什么要写成矩阵形式 而非标量形式?
并行加速 且符合其数学逻辑

  • y = w ∗ x + b y = w*x + b y=wx+b
  • Y = W @ X + b Y = W@X +b Y=W@X+b
    在这里插入图片描述
  • [b, 2] → [b, 3]
    Y = X @ W + b Y = X@W + b Y=X@W+b
x = tf.ones([4, 2])
W = tf.ones([2, 1])
b = tf.constant(0.1)
Y = x@W + b
# <tf.Tensor: id=1765, shape=(4, 1), dtype=float32, numpy=
# array([[2.1],
#        [2.1],
#        [2.1],
#        [2.1]], dtype=float32)>

o u t = r e l u ( X @ W + b ) out = relu(X@W + b) out=relu(X@W+b)

out = x@W + b
out = tf.nn.relu(out)
# <tf.Tensor: id=1953, shape=(4, 1), dtype=float32, numpy=
# array([[2.1],
#        [2.1],
#        [2.1],
#        [2.1]], dtype=float32)>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,TensorFlow2.0是一个非常强大的深度学习框架,它提供了许多基本API,下面是一个简单的教程,介绍如何使用TensorFlow2.0中的基本API: 1. 安装TensorFlow2.0 首先,您需要安装TensorFlow2.0。您可以使用pip install tensorflow命令进行安装。如果您已经安装了旧版本的TensorFlow,可以使用pip install --upgrade tensorflow命令进行升级。 2. 导入TensorFlow2.0 在您的Python代码中,您需要导入TensorFlow2.0。您可以使用以下命令导入: ``` import tensorflow as tf ``` 3. 定义张量 张量是TensorFlow中的基本数据类型。您可以将它们视为多维数组。您可以使用以下命令定义一个张量: ``` #定义一个2x3的张量 tensor = tf.constant([[1, 2, 3], [4, 5, 6]]) ``` 4. 运行计算图 在TensorFlow中,您需要构建一个计算图并运行它。您可以使用以下命令运行计算图: ``` #运行计算图 with tf.Session() as sess: result = sess.run(tensor) ``` 5. 定义变量 变量是在计算图中可以改变值的节点。您可以使用以下命令定义一个变量: ``` #定义一个变量 variable = tf.Variable(0, name='counter') ``` 6. 定义占位符 占位符是在运行计算图时可以传递值的节点。您可以使用以下命令定义一个占位符: ``` #定义一个占位符 placeholder = tf.placeholder(tf.float32, shape=[None, 10]) ``` 7. 定义操作 操作是计算图中的节点,它们执行各种数学运算。您可以使用以下命令定义一个操作: ``` #定义一个操作 operation = tf.add(1, 2) ``` 8. 计算梯度 在TensorFlow中,您可以使用自动微分来计算梯度。您可以使用以下命令计算梯度: ``` #计算梯度 x = tf.Variable(3.0) with tf.GradientTape() as tape: y = x**2 grad = tape.gradient(y, x) ``` 以上是TensorFlow2.0基本API的简单介绍。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TransientYear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值