为什么Keras Conv1D图层的输出Tensor没有输入维度?

yxyvkwin  于 2022-12-23  发布在  其他
关注(0)|答案(2)|浏览(181)

根据Keras的文件(https://keras.io/layers/convolutional/)Conv1D输出Tensor的形状为(batch_size,new_steps,filters),而输入Tensor形状为(批量大小,步骤,input_dim)。我不明白这是怎么回事,因为这意味着如果您传递长度为8000的一维输入,其中batch_size = 1,steps = 1(我听说steps是指输入中的通道数),则该层将输出形状(1,1,X)其中X是Conv层中的过滤器数量。但是输入维度会发生什么呢?由于层中的X个过滤器应用于整个输入维度,因此不应该' t其中一个输出尺寸为8000(或更少,取决于填充),类似于(1,1,8000,X)-我检查了,Conv2D图层的行为方式更合理,其output_shape为(样本、筛选器、new_rows、new_cols),其中new_rows和new_cols将是再次基于填充调整的输入图像的尺寸。如果Conv2D图层保留了它们的输入尺寸,那么Conv1D图层为什么不保留呢?我是不是漏掉了什么?
背景信息:
我试图可视化的1d卷积层激活我的CNN,但大多数工具在线我发现似乎只是工作的2d卷积层,所以我决定写我自己的代码。我有一个很好的理解它是如何工作的这里是代码,我已经得到了迄今为止:

# all the model's activation layer output tensors
activation_output_tensors = [layer.output for layer in model.layers if type(layer) is keras.layers.Activation]

# make a function that computes activation layer outputs
activation_comp_function = K.function([model.input, K.learning_phase()], activation_output_tensors)

# 0 means learning phase = False (i.e. the model isn't learning right now)
activation_arrays = activation_comp_function([training_data[0,:-1], 0])

这段代码是基于julienr在这篇文章中的第一个评论,对当前版本的keras做了一些修改,果然当我使用它的时候,尽管所有的激活数组都是(1,1,X)的形状......我昨天花了一整天的时间试图弄清楚为什么会这样,但是没有运气,任何帮助都非常感谢。
更新:结果我把input_dimension的意思和steps维度搞错了,这主要是因为我使用的架构来自另一个小组,他们用mathematica构建模型,而mathematica的输入形状是(X,Y)到Conv1D层表示X个"通道"(或X的input_dimension)和Y步骤A感谢Gionni帮助我认识到这一点,并如此好地解释了"input_dimension"如何成为"filter"维度。

nwwlzxa7

nwwlzxa71#

我曾经在2D卷积上遇到过同样的问题,问题是当你应用卷积层时,你所应用的内核大小不是(kernel_size, 1),而是实际上的(kernel_size, input_dim)
如果你不这样想的话,kernel_size = 1的1D卷积层将不会对它接收到的输入做任何事情。
而是计算每个时间步长的输入要素的加权平均值,每个时间步长使用相同的权重(虽然每个滤波器使用不同的一组权重)。我认为将input_dim可视化为图像的2D卷积中channels的数量是有帮助的,其中应用相同的推理(在该情况下是“丢失”并转换成滤波器数目的channels)。
为了让自己相信这一点,您可以使用kernel_size=(1D_kernel_size, input_dim)和相同数量的过滤器,用2D卷积层再现1D卷积。

from keras.layers import Conv1D, Conv2D
import keras.backend as K
import numpy as np

# create an input with 4 steps and 5 channels/input_dim
channels = 5
steps = 4
filters = 3
val = np.array([list(range(i * channels, (i + 1) * channels)) for i in range(1, steps + 1)])
val = np.expand_dims(val, axis=0)
x = K.variable(value=val)

# 1D convolution. Initialize the kernels to ones so that it's easier to compute the result by hand

conv1d = Conv1D(filters=filters, kernel_size=1, kernel_initializer='ones')(x)

# 2D convolution that replicates the 1D one

# need to add a dimension to your input since conv2d expects 4D inputs. I add it at axis 4 since my keras is setup with `channel_last`
val1 = np.expand_dims(val, axis=3)
x1 = K.variable(value=val1)

conv2d = Conv2D(filters=filters, kernel_size=(1, 5), kernel_initializer='ones')(x1)

# evaluate and print the outputs

print(K.eval(conv1d))
print(K.eval(conv2d))

正如我所说,我也花了一段时间才理解这一点,我想主要是因为没有教程解释清楚

eagi6jfj

eagi6jfj2#

谢谢,这很有用.
这里,相同的代码使用最新版本的tensorflow +角速度进行调整,并在轴0上叠加以构建4D

# %%
from tensorflow.keras.layers import Conv1D, Conv2D
from tensorflow.keras.backend import eval
import tensorflow as tf
import numpy as np

# %%
# create an 3D input with format BLC (Batch, Layer, Channel)
batch = 10
layers = 3
channels = 5
kernel = 2

val3D = np.random.randint(0, 100, size=(batch, layers, channels))
x = tf.Variable(val3D.astype('float32'))

# %%
# 1D convolution. Initialize the kernels to ones so that it's easier to compute the result by hand / compare
conv1d = Conv1D(filters=layers, kernel_size=kernel, kernel_initializer='ones')(x)

# %%
# 2D convolution that replicates the 1D one

# need to add a dimension to your input since conv2d expects 4D inputs. I add it at axis 0 since my keras is setup with `channel_last`
# stack 3 time the same
val4D = np.stack([val3D,val3D,val3D], axis=0)
x1 = tf.Variable(val4D.astype('float32'))

# %%
# 2D convolution. Initialize the kernel_size to one for the 1st kernel size so that replicate the conv1D
conv2d = Conv2D(filters=layers, kernel_size=(1, kernel), kernel_initializer='ones')(x1)

# %%
# evaluate and print the outputs

print(eval(conv1d))
print('---------------------------------------------')
# display only one of the stacked
print(eval(conv2d)[0])

相关问题