卷积块的数组切片

原文标题array slicing for convolutional block

我对数组切片表示法感到困惑。

def hypernetwork(self, inputs):
    x = self.fc(inputs)

    return x[..., :self.channels], x[..., self.channels:]

什么是回报? ...,是什么意思? self.channels定义为输入的通道数。我认为x只是输入特征块。下面是self.fcself.channels的相关代码

def build(self, input_shape):
    self.channels = input_shape[0][-1]  # input_shape: [x, z].

    self.fc = KL.Dense(
        int(2 * self.channels),
        kernel_initializer=self.init,
        kernel_regularizer=tf.keras.regularizers.l2(
            l=self.wt_decay,
        ),
        bias_regularizer=tf.keras.regularizers.l2(
            l=self.wt_decay,
        ),
    )

原文链接:https://stackoverflow.com//questions/71493834/array-slicing-for-convolutional-block

回复

我来回复
  • AloneTogether的头像
    AloneTogether 评论

    使用...时,您指的是除最后一个(您正在切片)之外的所有尺寸。等价于另一种记法x[:, :, :channels]

    import tensorflow as tf
    tf.random.set_seed(111)
    channels = 2
    x = tf.random.normal((1, 2, 3))
    print(x)
    print(x[..., :channels], x[:, :, :channels]) # Equivalent
    print(x[..., channels:], x[:, :, channels:]) # Equivalent
    
    tf.Tensor(
    [[[ 0.7558127   1.5447265   1.6315602 ]
      [-0.19868968  0.08828261  0.01711658]]], shape=(1, 2, 3), dtype=float32)
    tf.Tensor(
    [[[ 0.7558127   1.5447265 ]
      [-0.19868968  0.08828261]]], shape=(1, 2, 2), dtype=float32) tf.Tensor(
    [[[ 0.7558127   1.5447265 ]
      [-0.19868968  0.08828261]]], shape=(1, 2, 2), dtype=float32)
    tf.Tensor(
    [[[1.6315602 ]
      [0.01711658]]], shape=(1, 2, 1), dtype=float32) tf.Tensor(
    [[[1.6315602 ]
      [0.01711658]]], shape=(1, 2, 1), dtype=float32)
    
    2年前 0条评论
  • LukasNeugebauer的头像
    LukasNeugebauer 评论

    ...表示所有维度的所有元素,直到您开始显式引用,您可以使用:self.channels。所以总而言之,如果x是 eg。一个 10x4x6 数组和self.channels是 4,输出将是一个 10x4x4 数组和一个 10x4x2 数组。如果x是 10×6 而self.channels是 2,你会得到一个 10×2 和一个 10×4 的数组。你沿着最后一个维度分割数组。

    2年前 0条评论