数学运算
功能 | 代码函数 |
---|
加 | tf.add |
减 | tf.subtract |
乘 | tf.multiply |
除 | tf.divide |
整除 | // |
余除 | % |
乘方 | **或者 tf.pow(x, a) |
矩阵乘法 | @ |
平方和 | tf.square(x) |
平方根 | tf.sqrt(x) |
自然指数e | tf.exp(x) |
自然对数 | tf.math.log() |
简单运算
import tensorflow as tf
a = tf.range(1,9)
b = tf.constant(2)
c = tf.constant([[1,2],[3,4]])
print(a)
print(b)
print(c)
print(tf.add(a,a))
print(tf.subtract(a,b))
print(tf.multiply(a,b))
print(tf.divide(a,b))
print(a**b)
print(a//b)
print(a%b)
print(tf.square(a))
print(c @ c)
out:
tf.Tensor([1 2 3 4 5 6 7 8], shape=(8,), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(
[[1 2]
[3 4]], shape=(2, 2), dtype=int32)
tf.Tensor([ 2 4 6 8 10 12 14 16], shape=(8,), dtype=int32)
tf.Tensor([-1 0 1 2 3 4 5 6], shape=(8,), dtype=int32)
tf.Tensor([ 2 4 6 8 10 12 14 16], shape=(8,), dtype=int32)
tf.Tensor([0.5 1. 1.5 2. 2.5 3. 3.5 4. ], shape=(8,), dtype=float64)
tf.Tensor([ 1 4 9 16 25 36 49 64], shape=(8,), dtype=int32)
tf.Tensor([0 1 1 2 2 3 3 4], shape=(8,), dtype=int32)
tf.Tensor([1 0 1 0 1 0 1 0], shape=(8,), dtype=int32)
tf.Tensor([ 1 4 9 16 25 36 49 64], shape=(8,), dtype=int32)
tf.Tensor(
[[ 7 10]
[15 22]], shape=(2, 2), dtype=int32)