pymc.math.stack#
- pymc.math.stack(tensors, axis=0)[源代码]#
在给定的轴上按顺序堆叠张量(默认是0)。
取一系列的张量或类似张量的常量,并在给定的轴上堆叠它们以形成一个单独的张量。结果在维度 axis 的大小将等于传入的张量数量。
示例
>>> a = pytensor.tensor.type.scalar() >>> b = pytensor.tensor.type.scalar() >>> c = pytensor.tensor.type.scalar() >>> x = pytensor.tensor.stack([a, b, c]) >>> x.ndim # x is a vector of length 3. 1 >>> a = pytensor.tensor.type.tensor4() >>> b = pytensor.tensor.type.tensor4() >>> c = pytensor.tensor.type.tensor4() >>> x = pytensor.tensor.stack([a, b, c]) >>> x.ndim # x is a 5d tensor. 5 >>> rval = x.eval(dict((t, np.zeros((2, 2, 2, 2))) for t in [a, b, c])) >>> rval.shape # 3 tensors are stacked on axis 0 (3, 2, 2, 2, 2) >>> x = pytensor.tensor.stack([a, b, c], axis=3) >>> x.ndim 5 >>> rval = x.eval(dict((t, np.zeros((2, 2, 2, 2))) for t in [a, b, c])) >>> rval.shape # 3 tensors are stacked on axis 3 (2, 2, 2, 3, 2) >>> x = pytensor.tensor.stack([a, b, c], axis=-2) >>> x.ndim 5 >>> rval = x.eval(dict((t, np.zeros((2, 2, 2, 2))) for t in [a, b, c])) >>> rval.shape # 3 tensors are stacked on axis -2 (2, 2, 2, 3, 2)