Dive-into-DL数学基础
线性代数
1. 标量
import tensorflow as tf
x = tf.constant(3.0)
y = tf.constant(2.0)
x + y, x * y, x / y, x**y

2.向量
x = tf.range(4)
x
x[3]
3.矩阵
A = tf.reshape(tf.range(20), (5, 4))
A
tf.transpose(A)
B = tf.constant([[1, 2, 3], [2, 0, 4], [3, 4, 5]])
B
B == tf.transpose(B)
张量
更高阶的向量
X = tf.reshape(tf.range(24), (2, 3, 4))
X
降维
x = tf.range(4, dtype=tf.float32)
x, tf.reduce_sum(x)
A.shape, tf.reduce_sum(A)
#沿某个轴降维
A_sum_axis0 = tf.reduce_sum(A, axis=0)
A_sum_axis0, A_sum_axis0.shape
没弄懂
非降维求和
keepdims能让张量不变
sum_A = tf.reduce_sum(A, axis=1, keepdims=True)
sum_A
如果我们想沿某个轴计算A元素的累积总和, 比如axis=0(按行计算),可以调用cumsum函数。 此函数不会沿任何轴降低输入张量的维度。
点积
y = tf.ones(4, dtype=tf.float32)
x, y, tf.tensordot(x, y, axes=1)
#或者
tf.reduce_sum(x * y)
矩阵向量积
A.shape, x.shape, tf.linalg.matvec(A, x)
矩阵-矩阵乘法
B = tf.ones((4, 3), tf.float32)
tf.matmul(A, B)
范数
#L2 范数
u = tf.constant([3.0, -4.0])
tf.norm(u)
#L1 范数
tf.reduce_sum(tf.abs(u))
808



被折叠的 条评论
为什么被折叠?



