我正在尝试将像素向量提供给卷积神经网络 (CNN),其中像素向量来自 cifar-10 数据集等图像数据。在将像素向量馈送到 CNN 之前,我需要用麦克劳林级数扩展像素向量。关键是,我想出了如何用一个暗淡来扩展张量,但无法为暗淡 >2 的张量找到正确的方法。谁能给我一些关于如何将一个暗张量的麦克劳林级数应用于大于 1 的张量暗淡的想法?是否有任何启发式方法可以在 TensorFlow 或 Keras 中实现这一点?任何可能的想法?
CNN 上的麦克劳林系列 :
我想出了使用 maclaurin 系列扩展 1dim 的张量的方法。以下是如何抓取实现:
def cnn_taylor(input_dim, approx_order=2):
x = Input((input_dim,))
def pwr(x, approx_order):
x = x[..., None]
x = tf.tile(x, multiples=[1, 1, approx_order + 1])
pw = tf.range(0, approx_order + 1, dtype=tf.float32)
x_p = tf.pow(x, pw)
x_p = x_p[..., None]
return x_p
x_p = Lambda(lambda x: pwr(x, approx_order))(x)
h = Dense(1, use_bias=False)(x_p)
def cumu_sum(h):
h = tf.squeeze(h, axis=-1)
s = tf.cumsum(h, axis=-1)
s = s[..., None]
return s
S = Lambda(cumu_sum)(h)
所以上面的实现是关于如何使用 1 个暗淡张量用泰勒展开来展开 CNN 的草图编码尝试。我想知道如何使用多暗淡阵列(即暗淡 = 3)对张量做同样的事情。
如果我想用泰勒展开式以 2 的近似阶展开 CNN,其中输入是来自
RGB
的像素向量图像,我将如何在 TensorFlow 中轻松完成此操作?任何想法?谢谢
最佳答案
如果我理解正确,每个x
在提供的计算图中只是一个标量(一个像素的一个 channel )。在这种情况下,为了将变换应用于每个像素,您可以:
(b, h, w, c)
来自卷积层的输入到形状为 (b, h*w*c)
的张量中. 这可以通过以下方式实现:
shape_cnn = h.shape # Shape=(bs, h, w, c)
flat_dim = h.shape[1] * h.shape[2] * h.shape[3]
h = tf.reshape(h, (-1, flat_dim))
taylor_model = taylor_expansion_network(input_dim=flat_dim, max_pow=approx_order)
h = taylor_model(h)
h = tf.reshape(h, (-1, shape_cnn[1], shape_cnn[2], shape_cnn[3]))
注意:我在借用功能
taylor_expansion_network
来自 this answer .更新:我仍然不清楚最终目标,但也许这次更新让我们更接近预期的输出。我修改了
taylor_expansion_network
将管道的第一部分应用于形状为 (width, height, nb_channels=3)
的 RGB 图像,返回形状为 (width, height, nb_channels=3, max_pow+1)
的张量:def taylor_expansion_network_2(width, height, nb_channels=3, max_pow=2):
input_dim = width * height * nb_channels
x = Input((width, height, nb_channels,))
h = tf.reshape(x, (-1, input_dim))
# Raise input x_i to power p_i for each i in [0, max_pow].
def raise_power(x, max_pow):
x_ = x[..., None] # Shape=(batch_size, input_dim, 1)
x_ = tf.tile(x_, multiples=[1, 1, max_pow + 1]) # Shape=(batch_size, input_dim, max_pow+1)
pows = tf.range(0, max_pow + 1, dtype=tf.float32) # Shape=(max_pow+1,)
x_p = tf.pow(x_, pows) # Shape=(batch_size, input_dim, max_pow+1)
return x_p
h = raise_power(h, max_pow)
# Compute s_i for each i in [0, max_pow]
h = tf.cumsum(h, axis=-1) # Shape=(batch_size, input_dim, max_pow+1)
# Get the input format back
h = tf.reshape(h, (-1, width, height, nb_channels, max_pow+1)) # Shape=(batch_size, w, h, nb_channels, max_pow+1)
# Return Taylor expansion model
model = Model(inputs=x, outputs=h)
model.summary()
return model
在这个修改后的模型中,流水线的最后一步,即
w_i * s_i
的总和。每个i
,不适用。现在,您可以使用形状为 (width, height, nb_channels=3, max_pow+1)
的结果张量以任何你想要的方式。
关于python - 如何使用麦克劳林级数扩展二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61298195/