文章目录
Tensorflow进阶
合并与分割
合并
使用tf.concat (concatenate)合并张量,不会产生新的维度,合并操作可以在任意的维度上进行,唯一的约束是非合并维度的长度必须一致
使用tf.stack(stack)会产生新的维度,它需要所有合并的张量 shape 完全一致才可合并
a = tf.random.normal([4, 35, 8])
b = tf.random.normal([4, 35, 8])
c = tf.concat([a, b, b], axis = 1)
print(c.shape)
d = tf.stack([a, b], axis=0)
e = tf.stack([a, b], axis=-1)
print(f'd:{d.shape} e:{e.shape}')
分割
tf.split和tf.unstack
a = tf.random.normal([4, 35, 8])
r1 = tf.split(a, axis=0, num_or_size_splits=4)
r2 = tf.split(a, axis=1, num_or_size_splits=[30, 5])
r3 = tf.unstack(a)
print(type(r1))
print(tf.convert_to_tensor(r1).shape)
print(len(r2))
print(tf.convert_to_tensor(r3).shape)
split和unstack的返回值是list类型
数据统计
向量范数
向量范数(Vector norm)是表征向量“长度”的一种度量方法,在神经网络中,常用来表示张量的权值大小,梯度大小,下面是常见的范数:
x = tf.ones([2, 2])
print(tf.norm(x, ord=1))
print(tf.norm(x,