tf.concat([a, b], axis = )
注:
(1)不创建新的维度;
(2)在哪个维度合并,哪个维度数量增加;其他维度必须相同。
a = tf.ones([4, 35, 8])
b = tf.ones([2, 35, 8])
c = tf.ones([4, 35, 8])
d = tf.concat([a, b], axis=0)
print(d.shape) # (6, 35, 8)
e = tf.concat([a, c], axis=-1)
print(e.shape) # (4, 35, 16)
tf.stack([a, b], axis = )
注意,会创建新的维度!
a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])
c = tf.stack([a, b], axis=0)
print(c.shape)
d = tf.stack([a, b], axis=-1)
print(d.shape) # (4, 35, 8, 2)
a、b的原始维度必须完全相同,否则报错:
a = tf.ones([4, 35, 8])
b = tf.ones([2, 35, 8])
c = tf.stack([a, b], axis=0)
print(c.shape)
# ensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [4,35,8] != values[1].shape = [2,35,8] [Op:Pack] name: stack
tf.unstack(a, axis = )
注意,返回一个list,list里面的内容是tensor
a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])
c = tf.stack([a, b], axis=0)
print(c.shape) # (2, 4, 35, 8)
d = tf.unstack(c, axis=0)
print(type(d)) #
print(d[0].shape) # (4, 35, 8)
e = tf.unstack(c, axis=-1)
print(len(e)) # 8
print(e[0].shape) # (2, 4, 35) 范围e[0]~e[7]
tf.split(a, axis = , num_or_size_splits = )
a = tf.ones([4, 35, 8])
b = tf.ones([4, 35, 8])
c = tf.stack([a, b], axis=0)
print(c.shape) # (2, 4, 35, 8)
d = tf.split(c, axis=3, num_or_size_splits=2) # 分成两份
print(type(d)) #
print(len(d)) # 2
print(d[0].shape) # (2, 4, 35, 4)
e = tf.split(c, axis=3, num_or_size_splits=[2, 2, 4])
print(e[0].shape) # (2, 4, 35, 2)
print(e[2].shape) # (2, 4, 35, 4)
a = tf.ones([2, 2])
print(tf.norm(a)) # tf.Tensor(2.0, shape=(), dtype=float32)
# 不指定几范数和轴,默认二范数
print(tf.sqrt(tf.reduce_sum(tf.square(a))))