卷积块的数组切片

发布于 2025-01-14 11:57:16 字数 696 浏览 0 评论 0原文

我对数组切片符号感到困惑。

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,
        ),
    )

I get confused by a array slicing notation.

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

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

what is the return? what does ..., mean? The self.channels is defined as the number of channels of the input. I think x is just the input feature block. Below is the relevant code for self.fc and self.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,
        ),
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み青杉依旧 2025-01-21 11:57:16

使用 ... 时,您指的是除最后一个维度(您正在切片)之外的所有维度。它等价于其他表示法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)

You are referring to all the dimensions except the last one (which you are slicing) when using .... It is equivalent to the other notation 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)
软糯酥胸 2025-01-21 11:57:16

... 表示所有维度的所有元素,直到您开始显式引用(使用 :self.channels 进行引用)。
因此,总而言之,如果 x 是一个 10x4x6 数组,而 self.channels 是 4,则输出将是一个 10x4x4 数组和一个 10x4x2 数组。
如果 x 是 10x6 并且 self.channels 是 2,您将得到一个 10x2 和一个 10x4 数组。您将沿着最后一个维度分割数组。

... means all elements of all dimensions until you start explicitely referencing, which you do with the :self.channels.
So in conclusion if x is e.g. a 10x4x6 array and self.channels is 4, the output will be one 10x4x4 array and one 10x4x2 array.
If x is 10x6 and self.channels is 2, you'll get a 10x2 and a 10x4 array. You're splitting the array along the last dimension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文